From 4f5d1a041dda8aee823efcb895aff454bfbaa2bd Mon Sep 17 00:00:00 2001 From: Ian Bell Date: Sat, 29 Aug 2015 16:38:00 -0600 Subject: [PATCH] Add linear and Lorentz-Berthelot mixing rules; call apply_simple_mixing_rule(), then twiddle parameters closes #798 closes #792 --- src/Backends/Helmholtz/MixtureParameters.cpp | 65 +++++++++++++++++++- src/Backends/Helmholtz/MixtureParameters.h | 8 +++ wrappers/Python/CoolProp/CoolProp.pxd | 1 + wrappers/Python/CoolProp/CoolProp.pyx | 3 + 4 files changed, 76 insertions(+), 1 deletion(-) diff --git a/src/Backends/Helmholtz/MixtureParameters.cpp b/src/Backends/Helmholtz/MixtureParameters.cpp index ec3f104b..c0d244da 100644 --- a/src/Backends/Helmholtz/MixtureParameters.cpp +++ b/src/Backends/Helmholtz/MixtureParameters.cpp @@ -142,8 +142,72 @@ public: } } } + /// Add a simple mixing rule + void add_simple_mixing_rule(const std::string &CAS1, const std::string &CAS2, const std::string &rule){ + // Get the empty dictionary to be filled by the appropriate reducing parameter filling function + Dictionary dict; + + // Get the vector of CAS numbers + std::vector CAS; + CAS.push_back(CAS1); + CAS.push_back(CAS2); + + // Sort the CAS number vector + std::sort(CAS.begin(), CAS.end()); + + // Get the names of the compounds + std::vector names1(1,CAS[0]); + shared_ptr HEOS1(new CoolProp::HelmholtzEOSMixtureBackend(names1)); + std::string name1 = HEOS1->name(); + std::vector names2(1,CAS[1]); + shared_ptr HEOS2(new CoolProp::HelmholtzEOSMixtureBackend(names2)); + std::string name2 = HEOS2->name(); + + // Populate the dictionary with common terms + dict.add_string("name1", name1); + dict.add_string("name2", name2); + dict.add_string("BibTeX", "N/A - LINEAR MIXING"); + dict.add_number("F", 0); + dict.add_string("type","GERG-2008"); + + if (rule == "linear"){ + // Terms for linear mixing + dict.add_number("gammaT", 0.5*(HEOS1->T_critical()+HEOS2->T_critical())/sqrt(HEOS1->T_critical()*HEOS2->T_critical())); + double rhoc1 = HEOS1->rhomolar_critical(), rhoc2 = HEOS2->rhomolar_critical(); + dict.add_number("gammaV", 4*(1/rhoc1+1/rhoc2)/pow(1/pow(rhoc1,1.0/3.0)+1/pow(rhoc2,1.0/3.0),3)); + dict.add_number("betaV", 1.0); + dict.add_number("betaT", 1.0); + } + else if (rule == "Lorentz-Berthelot"){ + // Terms for Lorentz-Berthelot quadratic mixing + dict.add_number("gammaT", 1.0); + dict.add_number("gammaV", 1.0); + dict.add_number("betaV", 1.0); + dict.add_number("betaT", 1.0); + } + else{ + throw ValueError(format("Your simple mixing rule [%s] was not understood", rule.c_str())); + } + + if (binary_pair_map.find(CAS) == binary_pair_map.end()){ + // Add to binary pair map by creating one-element vector + binary_pair_map.insert(std::pair, std::vector >(CAS, std::vector(1, dict))); + } + else + { + binary_pair_map[CAS].push_back(dict); + } + } }; +// The modifiable parameter library static MixtureBinaryPairLibrary mixturebinarypairlibrary; +// A fixed parameter library containing the default values +static MixtureBinaryPairLibrary mixturebinarypairlibrary_default; + +/// Add a simple mixing rule +void apply_simple_mixing_rule(const std::string &CAS1, const std::string &CAS2, const std::string &rule){ + mixturebinarypairlibrary.add_simple_mixing_rule(CAS1, CAS2, rule); +} std::string get_csv_mixture_binary_pairs() { @@ -225,7 +289,6 @@ void set_mixture_binary_pair_data(const std::string &CAS1, const std::string &CA else{ throw ValueError(format("Could not match the binary pair [%s,%s] - for now this is an error.",CAS1.c_str(), CAS2.c_str())); } - } } diff --git a/src/Backends/Helmholtz/MixtureParameters.h b/src/Backends/Helmholtz/MixtureParameters.h index 51a02c29..1bbe6035 100644 --- a/src/Backends/Helmholtz/MixtureParameters.h +++ b/src/Backends/Helmholtz/MixtureParameters.h @@ -41,6 +41,14 @@ std::string get_mixture_binary_pair_data(const std::string &CAS1, const std::str */ void set_mixture_binary_pair_data(const std::string &CAS1, const std::string &CAS2, const std::string ¶m, const double val); +/** + * @brief Apply a simple mixing rule for a given binary pair + * @param CAS1 The CAS # for the first fluid (order matters!) + * @param CAS2 The CAS # for the second fluid (order matters!) + * @param rule The simple mixing rule to be used ("linear", "Lorentz-Berthelot") + */ +void apply_simple_mixing_rule(const std::string &CAS1, const std::string &CAS2, const std::string &rule); + class MixtureParameters { public: diff --git a/wrappers/Python/CoolProp/CoolProp.pxd b/wrappers/Python/CoolProp/CoolProp.pxd index fc51a9e3..f5b814e4 100644 --- a/wrappers/Python/CoolProp/CoolProp.pxd +++ b/wrappers/Python/CoolProp/CoolProp.pxd @@ -23,6 +23,7 @@ cdef extern from "Configuration.h" namespace "CoolProp": cdef extern from "DataStructures.h" namespace "CoolProp": string _get_mixture_binary_pair_data "CoolProp::get_mixture_binary_pair_data"(const string CAS1, const string CAS2, const string key) except + void _set_mixture_binary_pair_data "CoolProp::set_mixture_binary_pair_data"(const string CAS1, const string CAS2, const string key, const double val) except + + void _apply_simple_mixing_rule "CoolProp::apply_simple_mixing_rule"(const std::string &CAS1, const std::string &CAS2, const std::string &rule) except + string _get_parameter_information "CoolProp::get_parameter_information"(int, string) except + int _get_parameter_index "CoolProp::get_parameter_index"(string) except + int _get_phase_index "CoolProp::get_phase_index"(string) except + diff --git a/wrappers/Python/CoolProp/CoolProp.pyx b/wrappers/Python/CoolProp/CoolProp.pyx index 6690a77d..2d2f1501 100644 --- a/wrappers/Python/CoolProp/CoolProp.pyx +++ b/wrappers/Python/CoolProp/CoolProp.pyx @@ -171,6 +171,9 @@ cpdef is_trivial_parameter(int key): cpdef get_fluid_param_string(string fluid, string param): return _get_fluid_param_string(fluid, param) + +cpdef apply_simple_mixing_rule(CAS1, CAS2, rule): + return _apply_simple_mixing_rule(CAS1, CAS2, rule) cpdef double saturation_ancillary(string name, string output, int Q, string input, double value): """