Skip to content

Commit

Permalink
Merge pull request #493 from mikekaganski/master
Browse files Browse the repository at this point in the history
Avoid copying of parameters; some fixes for _HAPropsSI_inputs
  • Loading branch information
ibell committed Feb 24, 2015
2 parents e41783a + f94a249 commit 6d39bf8
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 37 deletions.
8 changes: 4 additions & 4 deletions include/CoolPropTools.h
Expand Up @@ -141,8 +141,8 @@

void MatInv_2(double A[2][2] , double B[2][2]);

double root_sum_square(std::vector<double> x);
double interp1d(std::vector<double> *x, std::vector<double> *y, double x0);
double root_sum_square(const std::vector<double> &x);
double interp1d(const std::vector<double> *x, const std::vector<double> *y, double x0);
double powInt(double x, int y);

#define POW2(x) ((x)*(x))
Expand Down Expand Up @@ -283,7 +283,7 @@
{
return get_double(s);
};
std::vector<double> get_double_vector(std::string s)
const std::vector<double>& get_double_vector(std::string s)
{
if (double_vectors.find(s) != double_vectors.end()){
return double_vectors[s];
Expand All @@ -292,7 +292,7 @@
throw CoolProp::ValueError(format("%s could not be matched in get_double_vector",s.c_str()));
}
};
std::vector<std::string> get_string_vector(std::string s)
const std::vector<std::string>& get_string_vector(std::string s)
{
if (string_vectors.find(s) != string_vectors.end()){
return string_vectors[s];
Expand Down
12 changes: 5 additions & 7 deletions include/DataStructures.h
Expand Up @@ -16,9 +16,7 @@ namespace CoolProp {
struct SimpleState
{
double rhomolar, T, p, hmolar, smolar, umolar, Q;
SimpleState(){rhomolar = _HUGE; T = _HUGE; p = _HUGE;
hmolar = _HUGE; smolar = _HUGE, umolar = _HUGE;
Q = _HUGE;}
SimpleState() : rhomolar(_HUGE), T(_HUGE), p(_HUGE), hmolar(_HUGE), smolar(_HUGE), umolar(_HUGE), Q(_HUGE) {}
bool is_valid(){return ValidNumber(rhomolar) && ValidNumber(T) && ValidNumber(hmolar) && ValidNumber(p);}
};

Expand All @@ -27,7 +25,7 @@ struct SsatSimpleState : public SimpleState
{
enum SsatSimpleStateEnum {SSAT_MAX_NOT_SET=0, SSAT_MAX_DOESNT_EXIST, SSAT_MAX_DOES_EXIST};
SsatSimpleStateEnum exists;
SsatSimpleState(){ SimpleState(); }
SsatSimpleState() : SimpleState() {}
};


Expand All @@ -42,7 +40,7 @@ enum parameters{
INVALID_PARAMETER = 0,

// General parameters
igas_constant,
igas_constant,
imolar_mass,
iacentric_factor,
irhomolar_reducing,
Expand All @@ -52,7 +50,7 @@ enum parameters{
irhomass_reducing,
irhomass_critical,
iP_critical,
iP_reducing,
iP_reducing,
iT_triple,
iP_triple,
iT_min,
Expand Down Expand Up @@ -185,7 +183,7 @@ enum fluid_types{FLUID_TYPE_PURE, FLUID_TYPE_PSEUDOPURE, FLUID_TYPE_REFPROP, FLU
// !! If you add a parameter, update the map in the corresponding CPP file !!
/// These are input pairs that can be used (in each pair, input keys are sorted alphabetically)
enum input_pairs{
INPUT_PAIR_INVALID = 0, // Default (invalid) value
INPUT_PAIR_INVALID = 0, // Default (invalid) value
QT_INPUTS, ///< Molar quality, Temperature in K
PQ_INPUTS, ///< Pressure in Pa, Molar quality
QSmolar_INPUTS, ///< Molar quality, Entropy in J/mol/K
Expand Down
2 changes: 1 addition & 1 deletion src/Backends/Helmholtz/Fluids/FluidLibrary.h
Expand Up @@ -1169,7 +1169,7 @@ class JSONFluidLibrary
/**
@param key Either a CAS number or the name (CAS number should be preferred)
*/
CoolPropFluid& get(std::string key)
CoolPropFluid& get(const std::string &key)
{
std::map<std::string, std::size_t>::iterator it;
// Try to find it
Expand Down
4 changes: 2 additions & 2 deletions src/Backends/Helmholtz/HelmholtzEOSBackend.h
Expand Up @@ -33,8 +33,8 @@ class HelmholtzEOSBackend : public HelmholtzEOSMixtureBackend {
}
}
else{
components = std::vector<CoolPropFluid*>(1,&(get_library().get(name)));
mole_fractions = std::vector<double>(1,1);
components.push_back(&(get_library().get(name))); // Until now it's empty
mole_fractions.push_back(1.);
}
// Set the components
set_components(components);
Expand Down
8 changes: 4 additions & 4 deletions src/Backends/Helmholtz/HelmholtzEOSMixtureBackend.cpp
Expand Up @@ -51,15 +51,15 @@ HelmholtzEOSMixtureBackend::HelmholtzEOSMixtureBackend(const std::vector<std::st
// Set the phase to default unknown value
_phase = iphase_unknown;
}
HelmholtzEOSMixtureBackend::HelmholtzEOSMixtureBackend(std::vector<CoolPropFluid*> components, bool generate_SatL_and_SatV) {
HelmholtzEOSMixtureBackend::HelmholtzEOSMixtureBackend(const std::vector<CoolPropFluid*> &components, bool generate_SatL_and_SatV) {

// Set the components and associated flags
set_components(components, generate_SatL_and_SatV);

// Set the phase to default unknown value
_phase = iphase_unknown;
}
void HelmholtzEOSMixtureBackend::set_components(std::vector<CoolPropFluid*> components, bool generate_SatL_and_SatV) {
void HelmholtzEOSMixtureBackend::set_components(const std::vector<CoolPropFluid*> &components, bool generate_SatL_and_SatV) {

// Copy the components
this->components = components;
Expand Down Expand Up @@ -99,8 +99,8 @@ void HelmholtzEOSMixtureBackend::set_mole_fractions(const std::vector<long doubl
throw ValueError(format("size of mole fraction vector [%d] does not equal that of component vector [%d]",mole_fractions.size(), N));
}
// Copy values without reallocating memory
this->resize(N);
std::copy( mole_fractions.begin(), mole_fractions.end(), this->mole_fractions.begin() );
this->mole_fractions = mole_fractions; // Most effective copy
this->resize(N); // No reallocation of this->mole_fractions happens
// Resize the vectors for the liquid and vapor, but only if they are in use
if (this->SatL.get() != NULL){
this->SatL->resize(N);
Expand Down
4 changes: 2 additions & 2 deletions src/Backends/Helmholtz/HelmholtzEOSMixtureBackend.h
Expand Up @@ -35,7 +35,7 @@ class HelmholtzEOSMixtureBackend : public AbstractState {
public:
HelmholtzEOSMixtureBackend(){
imposed_phase_index = iphase_not_imposed; _phase = iphase_unknown;};
HelmholtzEOSMixtureBackend(std::vector<CoolPropFluid*> components, bool generate_SatL_and_SatV = true);
HelmholtzEOSMixtureBackend(const std::vector<CoolPropFluid*> &components, bool generate_SatL_and_SatV = true);
HelmholtzEOSMixtureBackend(const std::vector<std::string> &component_names, bool generate_SatL_and_SatV = true);
virtual ~HelmholtzEOSMixtureBackend(){};
shared_ptr<ReducingFunction> Reducing;
Expand Down Expand Up @@ -119,7 +119,7 @@ class HelmholtzEOSMixtureBackend : public AbstractState {
* @param components The components that are to be used in this mixture
* @param generate_SatL_and_SatV true if SatL and SatV classes should be added, false otherwise. Added so that saturation classes can be added without infinite recursion of adding saturation classes
*/
void set_components(std::vector<CoolPropFluid*> components, bool generate_SatL_and_SatV = true);
void set_components(const std::vector<CoolPropFluid*> &components, bool generate_SatL_and_SatV = true);

/** \brief Specify the phase - this phase will always be used in calculations
*
Expand Down
2 changes: 1 addition & 1 deletion src/Backends/Helmholtz/MixtureParameters.cpp
Expand Up @@ -47,7 +47,7 @@ std::string get_csv_predefined_mixtures()
return strjoin(out, ",");
}

bool is_predefined_mixture(const std::string name, Dictionary &dict){
bool is_predefined_mixture(const std::string &name, Dictionary &dict){
std::map<std::string, Dictionary>::iterator iter = predefined_mixtures_library.predefined_mixture_map.find(name);
if (iter != predefined_mixtures_library.predefined_mixture_map.end()){
dict = iter->second;
Expand Down
2 changes: 1 addition & 1 deletion src/Backends/Helmholtz/MixtureParameters.h
Expand Up @@ -14,7 +14,7 @@ std::string get_csv_mixture_binary_pairs();
/** \brief Get the parameters for a predefined mixture - R410A, R404A, etc.
*
*/
bool is_predefined_mixture(const std::string name, Dictionary &dict);
bool is_predefined_mixture(const std::string &name, Dictionary &dict);

/** \brief Get a comma-separated list of predefined mixtures in
*
Expand Down
4 changes: 2 additions & 2 deletions src/CoolPropTools.cpp
Expand Up @@ -12,7 +12,7 @@
#include "MatrixMath.h"
#include "Exceptions.h"

double root_sum_square(std::vector<double> x)
double root_sum_square(const std::vector<double> &x)
{
double sum = 0;
for (unsigned int i=0; i<x.size(); i++)
Expand Down Expand Up @@ -67,7 +67,7 @@ std::vector<std::string> strsplit(std::string s, char del)
return v;
}

double interp1d(std::vector<double> *x, std::vector<double> *y, double x0)
double interp1d(const std::vector<double> *x, const std::vector<double> *y, double x0)
{
std::size_t i,L,R,M;
L=0;
Expand Down
23 changes: 10 additions & 13 deletions src/HumidAirProp.cpp
Expand Up @@ -1350,23 +1350,21 @@ bool match_input_key(std::vector<givens> input_keys, givens key)
/// Calculate T (dry bulb temp) and psi_w (water mole fraction) given the pair of inputs
double _HAPropsSI_inputs(double p, const std::vector<givens> &input_keys, const std::vector<double> &input_vals, double &T, double &psi_w)
{
if (match_input_key(input_keys, GIVEN_T)) // Found T (or alias) as an input
long key = get_input_key(input_keys, GIVEN_T);
if (key >= 0) // Found T (or alias) as an input
{
long key = get_input_key(input_keys, GIVEN_T);
long other = 1 - key; // 2 element vector
T = input_vals[key];
switch(input_keys[other]){
switch(givens othergiven = input_keys[other]){
case GIVEN_RH:
psi_w = MoleFractionWater(T, p, GIVEN_RH, input_vals[other]); break;
case GIVEN_HUMRAT:
psi_w = MoleFractionWater(T, p, GIVEN_HUMRAT, input_vals[other]); break;
case GIVEN_TDP:
psi_w = MoleFractionWater(T, p, GIVEN_TDP, input_vals[other]); break;
psi_w = MoleFractionWater(T, p, othergiven, input_vals[other]); break;
default:
{
// Find the value for W
double W_guess = 0.0001;
double W = Secant_HAProps_W(p,T,input_keys[other],input_vals[other],W_guess);
double W = Secant_HAProps_W(p, T, othergiven, input_vals[other], W_guess);
// Mole fraction of water
psi_w = MoleFractionWater(T, p, GIVEN_HUMRAT, W);
}
Expand All @@ -1375,15 +1373,14 @@ double _HAPropsSI_inputs(double p, const std::vector<givens> &input_keys, const
else
{
// Need to iterate to find dry bulb temperature since temperature is not provided
long key, other;
if ((key = get_input_key(input_keys, GIVEN_HUMRAT)) && key >= 0){} // Humidity ratio is given
else if ((key = get_input_key(input_keys, GIVEN_RH)) && key >= 0){} // Relative humidity is given
else if ((key = get_input_key(input_keys, GIVEN_TDP)) && key >= 0){} // Dewpoint temperature is given
if ((key = get_input_key(input_keys, GIVEN_HUMRAT)) >= 0){} // Humidity ratio is given
else if ((key = get_input_key(input_keys, GIVEN_RH)) >= 0){} // Relative humidity is given
else if ((key = get_input_key(input_keys, GIVEN_TDP)) >= 0){} // Dewpoint temperature is given
else{
CoolProp::ValueError("Sorry, but currently at least one of the variables as an input to HAPropsSI() must be temperature, relative humidity, humidity ratio, or dewpoint\n Eventually will add a 2-D NR solver to find T and psi_w simultaneously, but not included now\n");
throw CoolProp::ValueError("Sorry, but currently at least one of the variables as an input to HAPropsSI() must be temperature, relative humidity, humidity ratio, or dewpoint\n Eventually will add a 2-D NR solver to find T and psi_w simultaneously, but not included now\n");
}
// 2-element vector
other = 1 - key;
long other = 1 - key;

// Main input is the one that you are using in the call to HAPropsSI
double MainInputValue = input_vals[key];
Expand Down

0 comments on commit 6d39bf8

Please sign in to comment.