From f8bb1c00443d1bcb99b4e5a147f253a387308eb6 Mon Sep 17 00:00:00 2001 From: mikekaganski Date: Mon, 23 Feb 2015 17:36:15 +1000 Subject: [PATCH 1/2] Avoid copying parameters --- include/CoolPropTools.h | 8 ++++---- include/DataStructures.h | 12 +++++------- src/Backends/Helmholtz/Fluids/FluidLibrary.h | 2 +- src/Backends/Helmholtz/HelmholtzEOSBackend.h | 4 ++-- .../Helmholtz/HelmholtzEOSMixtureBackend.cpp | 8 ++++---- src/Backends/Helmholtz/HelmholtzEOSMixtureBackend.h | 4 ++-- src/Backends/Helmholtz/MixtureParameters.cpp | 2 +- src/Backends/Helmholtz/MixtureParameters.h | 2 +- src/CoolPropTools.cpp | 4 ++-- 9 files changed, 22 insertions(+), 24 deletions(-) diff --git a/include/CoolPropTools.h b/include/CoolPropTools.h index dbd4990f..05542ea1 100644 --- a/include/CoolPropTools.h +++ b/include/CoolPropTools.h @@ -141,8 +141,8 @@ void MatInv_2(double A[2][2] , double B[2][2]); - double root_sum_square(std::vector x); - double interp1d(std::vector *x, std::vector *y, double x0); + double root_sum_square(const std::vector &x); + double interp1d(const std::vector *x, const std::vector *y, double x0); double powInt(double x, int y); #define POW2(x) ((x)*(x)) @@ -283,7 +283,7 @@ { return get_double(s); }; - std::vector get_double_vector(std::string s) + const std::vector& get_double_vector(std::string s) { if (double_vectors.find(s) != double_vectors.end()){ return double_vectors[s]; @@ -292,7 +292,7 @@ throw CoolProp::ValueError(format("%s could not be matched in get_double_vector",s.c_str())); } }; - std::vector get_string_vector(std::string s) + const std::vector& get_string_vector(std::string s) { if (string_vectors.find(s) != string_vectors.end()){ return string_vectors[s]; diff --git a/include/DataStructures.h b/include/DataStructures.h index 294ad059..6f5a5a0f 100644 --- a/include/DataStructures.h +++ b/include/DataStructures.h @@ -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);} }; @@ -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() {} }; @@ -42,7 +40,7 @@ enum parameters{ INVALID_PARAMETER = 0, // General parameters - igas_constant, + igas_constant, imolar_mass, iacentric_factor, irhomolar_reducing, @@ -52,7 +50,7 @@ enum parameters{ irhomass_reducing, irhomass_critical, iP_critical, - iP_reducing, + iP_reducing, iT_triple, iP_triple, iT_min, @@ -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 diff --git a/src/Backends/Helmholtz/Fluids/FluidLibrary.h b/src/Backends/Helmholtz/Fluids/FluidLibrary.h index 3f29a31e..620bff66 100644 --- a/src/Backends/Helmholtz/Fluids/FluidLibrary.h +++ b/src/Backends/Helmholtz/Fluids/FluidLibrary.h @@ -1169,7 +1169,7 @@ public: /** @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::iterator it; // Try to find it diff --git a/src/Backends/Helmholtz/HelmholtzEOSBackend.h b/src/Backends/Helmholtz/HelmholtzEOSBackend.h index 610064b9..105fc1f9 100644 --- a/src/Backends/Helmholtz/HelmholtzEOSBackend.h +++ b/src/Backends/Helmholtz/HelmholtzEOSBackend.h @@ -33,8 +33,8 @@ public: } } else{ - components = std::vector(1,&(get_library().get(name))); - mole_fractions = std::vector(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); diff --git a/src/Backends/Helmholtz/HelmholtzEOSMixtureBackend.cpp b/src/Backends/Helmholtz/HelmholtzEOSMixtureBackend.cpp index f53e31dd..f5c50b50 100644 --- a/src/Backends/Helmholtz/HelmholtzEOSMixtureBackend.cpp +++ b/src/Backends/Helmholtz/HelmholtzEOSMixtureBackend.cpp @@ -51,7 +51,7 @@ HelmholtzEOSMixtureBackend::HelmholtzEOSMixtureBackend(const std::vector components, bool generate_SatL_and_SatV) { +HelmholtzEOSMixtureBackend::HelmholtzEOSMixtureBackend(const std::vector &components, bool generate_SatL_and_SatV) { // Set the components and associated flags set_components(components, generate_SatL_and_SatV); @@ -59,7 +59,7 @@ HelmholtzEOSMixtureBackend::HelmholtzEOSMixtureBackend(std::vector components, bool generate_SatL_and_SatV) { +void HelmholtzEOSMixtureBackend::set_components(const std::vector &components, bool generate_SatL_and_SatV) { // Copy the components this->components = components; @@ -99,8 +99,8 @@ void HelmholtzEOSMixtureBackend::set_mole_fractions(const std::vectorresize(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); diff --git a/src/Backends/Helmholtz/HelmholtzEOSMixtureBackend.h b/src/Backends/Helmholtz/HelmholtzEOSMixtureBackend.h index 677628cc..955cd5e5 100644 --- a/src/Backends/Helmholtz/HelmholtzEOSMixtureBackend.h +++ b/src/Backends/Helmholtz/HelmholtzEOSMixtureBackend.h @@ -35,7 +35,7 @@ protected: public: HelmholtzEOSMixtureBackend(){ imposed_phase_index = iphase_not_imposed; _phase = iphase_unknown;}; - HelmholtzEOSMixtureBackend(std::vector components, bool generate_SatL_and_SatV = true); + HelmholtzEOSMixtureBackend(const std::vector &components, bool generate_SatL_and_SatV = true); HelmholtzEOSMixtureBackend(const std::vector &component_names, bool generate_SatL_and_SatV = true); virtual ~HelmholtzEOSMixtureBackend(){}; shared_ptr Reducing; @@ -119,7 +119,7 @@ public: * @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 components, bool generate_SatL_and_SatV = true); + void set_components(const std::vector &components, bool generate_SatL_and_SatV = true); /** \brief Specify the phase - this phase will always be used in calculations * diff --git a/src/Backends/Helmholtz/MixtureParameters.cpp b/src/Backends/Helmholtz/MixtureParameters.cpp index 2a4211ac..6dbd0bc5 100644 --- a/src/Backends/Helmholtz/MixtureParameters.cpp +++ b/src/Backends/Helmholtz/MixtureParameters.cpp @@ -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::iterator iter = predefined_mixtures_library.predefined_mixture_map.find(name); if (iter != predefined_mixtures_library.predefined_mixture_map.end()){ dict = iter->second; diff --git a/src/Backends/Helmholtz/MixtureParameters.h b/src/Backends/Helmholtz/MixtureParameters.h index 751defff..97ad54da 100644 --- a/src/Backends/Helmholtz/MixtureParameters.h +++ b/src/Backends/Helmholtz/MixtureParameters.h @@ -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 * diff --git a/src/CoolPropTools.cpp b/src/CoolPropTools.cpp index ddedb3a5..710d5fe2 100644 --- a/src/CoolPropTools.cpp +++ b/src/CoolPropTools.cpp @@ -12,7 +12,7 @@ #include "MatrixMath.h" #include "Exceptions.h" -double root_sum_square(std::vector x) +double root_sum_square(const std::vector &x) { double sum = 0; for (unsigned int i=0; i strsplit(std::string s, char del) return v; } -double interp1d(std::vector *x, std::vector *y, double x0) +double interp1d(const std::vector *x, const std::vector *y, double x0) { std::size_t i,L,R,M; L=0; From f94a2498959ddba440b1ab3bbbf304d2de88e5ce Mon Sep 17 00:00:00 2001 From: mikekaganski Date: Mon, 23 Feb 2015 18:13:28 +1000 Subject: [PATCH 2/2] tidy _HAPropsSI_inputs avoided double lookup for input key (match_input_key + get_input_key); unified MoleFractionWater call; if ((key = get_input_key(input_keys, GIVEN_HUMRAT)) && key >= 0) may give false negatives if get_input_key returns 0; added throw --- src/HumidAirProp.cpp | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/HumidAirProp.cpp b/src/HumidAirProp.cpp index 79e85a42..30534127 100644 --- a/src/HumidAirProp.cpp +++ b/src/HumidAirProp.cpp @@ -1350,23 +1350,21 @@ bool match_input_key(std::vector 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 &input_keys, const std::vector &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); } @@ -1375,15 +1373,14 @@ double _HAPropsSI_inputs(double p, const std::vector &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];