Skip to content

Commit

Permalink
Implement 95% of the BIP setting/getting
Browse files Browse the repository at this point in the history
See #1000
  • Loading branch information
ibell committed Mar 20, 2016
1 parent 5a4ce3d commit 2160b02
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 44 deletions.
1 change: 1 addition & 0 deletions include/CoolPropTools.h
Expand Up @@ -74,6 +74,7 @@
bool is_empty(void) const {return numbers.empty() && strings.empty() && double_vectors.empty() && string_vectors.empty();}
void add_string(const std::string &s1, const std::string &s2){ strings.insert(std::pair<std::string, std::string>(s1, s2));}
void add_number(const std::string &s1, double d){ numbers.erase(s1); numbers.insert(std::pair<std::string, double>(s1, d));}
bool has_number(const std::string &s1){ return numbers.find(s1) != numbers.end(); }
void add_double_vector(const std::string &s1, const std::vector<double> &d){ double_vectors.insert(std::pair<std::string, std::vector<double> >(s1, d));}
void add_string_vector(const std::string &s1, const std::vector<std::string> &d){ string_vectors.insert(std::pair<std::string, std::vector<std::string> >(s1, d));}
std::string get_string(const std::string &s) const
Expand Down
40 changes: 23 additions & 17 deletions src/Backends/Helmholtz/HelmholtzEOSMixtureBackend.cpp
Expand Up @@ -192,24 +192,30 @@ std::string HelmholtzEOSMixtureBackend::fluid_param_string(const std::string &Pa
throw ValueError(format("fluid parameter [%s] is invalid",ParamName.c_str()));
}
}
/// Set binary mixture floating point parameter
void HelmholtzEOSMixtureBackend::set_binary_interaction_double(const std::string &CAS1, const std::string &CAS2, const std::string &parameter, const double value){
// Set the value in the library
CoolProp::set_mixture_binary_pair_data(CAS1, CAS2, parameter, value);

// Update the values in this instance and the saturation states too
this->set_mixture_parameters();
if (this->SatL) this->SatL->set_mixture_parameters();
if (this->SatV) this->SatV->set_mixture_parameters();

/// Set binary mixture floating point parameter for this instance
void HelmholtzEOSMixtureBackend::set_binary_interaction_double(const std::size_t i, const std::size_t j, const std::string &parameter, const double value){
if (parameter == "Fij"){
residual_helmholtz->Excess.F[i][j] = value;
residual_helmholtz->Excess.F[j][i] = value;
}
else{
Reducing->set_binary_interaction_double(i,j,parameter,value);
}
};
/// Get binary mixture double value
double HelmholtzEOSMixtureBackend::get_binary_interaction_double(const std::string &CAS1, const std::string &CAS2, const std::string &parameter){
return atof(CoolProp::get_mixture_binary_pair_data(CAS1, CAS2, parameter).c_str());
}
/// Get binary mixture string value
std::string HelmholtzEOSMixtureBackend::get_binary_interaction_string(const std::string &CAS1, const std::string &CAS2, const std::string &parameter){
return CoolProp::get_mixture_binary_pair_data(CAS1, CAS2, parameter);
}
/// Get binary mixture floating point parameter for this instance
double HelmholtzEOSMixtureBackend::get_binary_interaction_double(const std::size_t i, const std::size_t j, const std::string &parameter){
if (parameter == "Fij"){
return residual_helmholtz->Excess.F[i][j];
}
else{
return Reducing->get_binary_interaction_double(i,j,parameter);
}
};
///// Get binary mixture string value
//std::string HelmholtzEOSMixtureBackend::get_binary_interaction_string(const std::string &CAS1, const std::string &CAS2, const std::string &parameter){
// return CoolProp::get_mixture_binary_pair_data(CAS1, CAS2, parameter);
//}

void HelmholtzEOSMixtureBackend::calc_change_EOS(const std::size_t i, const std::string &EOS_name){

Expand Down
8 changes: 4 additions & 4 deletions src/Backends/Helmholtz/HelmholtzEOSMixtureBackend.h
Expand Up @@ -79,11 +79,11 @@ class HelmholtzEOSMixtureBackend : public AbstractState {
std::string fluid_param_string(const std::string &);

/// Set binary mixture floating point parameter
virtual void set_binary_interaction_double(const std::string &CAS1, const std::string &CAS2, const std::string &parameter, const double value);
virtual void set_binary_interaction_double(const std::size_t i, const std::size_t j, const std::string &parameter, const double value);
/// Get binary mixture double value
virtual double get_binary_interaction_double(const std::string &CAS1, const std::string &CAS2, const std::string &parameter);
/// Get binary mixture string value
virtual std::string get_binary_interaction_string(const std::string &CAS1, const std::string &CAS2, const std::string &parameter);
virtual double get_binary_interaction_double(const std::size_t i, const std::size_t j, const std::string &parameter);
///// Get binary mixture string value
//virtual std::string get_binary_interaction_string(const std::size_t &i, const std::size_t &j, const std::string &parameter);

phases calc_phase(void){return _phase;};
void calc_specify_phase(phases phase){ specify_phase(phase); }
Expand Down
12 changes: 4 additions & 8 deletions src/Backends/Helmholtz/MixtureParameters.cpp
Expand Up @@ -267,16 +267,12 @@ void set_mixture_binary_pair_data(const std::string &CAS1, const std::string &CA

if (mixturebinarypairlibrary.binary_pair_map.find(CAS) != mixturebinarypairlibrary.binary_pair_map.end()){
std::vector<Dictionary> &v = mixturebinarypairlibrary.binary_pair_map[CAS];
try{
if (v[0].has_number(key)){
v[0].add_number(key, value);
double got = v[0].get_double(key);
if (std::abs(got-value) > 1e-10){
throw ValueError("Did not set value properly");
}
}
catch(std::exception &e){
throw ValueError(format("Could not set the parameter [%s] for the binary pair [%s,%s] - for now this is an error; error: %s",
key.c_str(), CAS1.c_str(), CAS2.c_str(), e.what()));
else{
throw ValueError(format("Could not set the parameter [%s] for the binary pair [%s,%s] - for now this is an error",
key.c_str(), CAS1.c_str(), CAS2.c_str()));
}
}
else{
Expand Down
45 changes: 45 additions & 0 deletions src/Backends/Helmholtz/ReducingFunctions.h
Expand Up @@ -34,6 +34,10 @@ class ReducingFunction
ReducingFunction():N(0){};
virtual ~ReducingFunction(){};

virtual void set_binary_interaction_double(const std::size_t i, const std::size_t j, const std::string &parameter, double value) = 0;

virtual double get_binary_interaction_double(const std::size_t i, const std::size_t j, const std::string &parameter) = 0;

/// A factory function to generate the requiredreducing function
static shared_ptr<ReducingFunction> factory(const std::vector<CoolPropFluid*> &components, STLMatrix &F);

Expand Down Expand Up @@ -143,6 +147,44 @@ class GERG2008ReducingFunction : public ReducingFunction

/// Default destructor
~GERG2008ReducingFunction(){};

/// Set a parameter
virtual void set_binary_interaction_double(const std::size_t i, const std::size_t j, const std::string &parameter, double value){
if (parameter == "betaT"){
beta_T[i][j] = value; beta_T[j][i] = 1/value;
}
else if (parameter == "gammaT"){
gamma_T[i][j] = value; gamma_T[j][i] = value;
}
else if (parameter == "betaV"){
beta_v[i][j] = value; beta_v[j][i] = 1/value;
}
else if (parameter == "gammaV"){
gamma_v[i][j] = value; gamma_v[j][i] = value;
}
else{
throw KeyError(format("This key [%s] is invalid to set_binary_interaction_double",parameter.c_str()));
}
}
/// Get a parameter
virtual double get_binary_interaction_double(const std::size_t i, const std::size_t j, const std::string &parameter){
if (parameter == "betaT"){
return beta_T[i][j];
}
else if (parameter == "gammaT"){
return gamma_T[i][j];
}
else if (parameter == "betaV"){
return beta_v[i][j];
}
else if (parameter == "gammaV"){
return gamma_v[i][j];
}
else{
throw KeyError(format("This key [%s] is invalid to get_binary_interaction_double",parameter.c_str()));
}
}

/** \brief The reducing temperature
* Calculated from \ref Yr with \f$T = Y\f$
*/
Expand Down Expand Up @@ -368,6 +410,9 @@ class ConstantReducingFunction : public ReducingFunction
public:
ConstantReducingFunction(const double T_c, const double rhomolar_c) : T_c(T_c), rhomolar_c(rhomolar_c) {};

void set_binary_interaction_double(const std::size_t i, const std::size_t j, const std::string &parameter, double value){return;}
double get_binary_interaction_double(const std::size_t i, const std::size_t j, const std::string &parameter){return _HUGE; }

/// \brief The reducing temperature
CoolPropDbl Tr(const std::vector<CoolPropDbl> &x){ return T_c; };
/// \brief The derivative of reducing temperature with respect to component i mole fraction
Expand Down
10 changes: 8 additions & 2 deletions wrappers/Python/CoolProp/AbstractState.pxd
Expand Up @@ -7,9 +7,15 @@ cimport cAbstractState
from typedefs cimport *
cimport constants_header

ctypedef fused string_or_size_t:
ctypedef fused string_t:
cython.p_char
bytes
unicode
string
size_t

ctypedef fused string_or_size_t:
string_t
cython.integral

cdef class PyPhaseEnvelopeData:
cpdef public bool TypeI
Expand Down
26 changes: 16 additions & 10 deletions wrappers/Python/CoolProp/AbstractState.pyx
Expand Up @@ -56,14 +56,20 @@ cdef class AbstractState:

cpdef set_binary_interaction_double(self, string_or_size_t arg1, string_or_size_t arg2, string parameter, double val):
""" Set a double precision interaction parameter - wrapper of c++ function :cpapi:`CoolProp::AbstractState::set_binary_interaction_double` """
self.thisptr.set_binary_interaction_double(arg1, arg2, parameter, val)
if string_or_size_t in cython.integral:
self.thisptr.set_binary_interaction_double(<size_t>arg1, <size_t>arg2, parameter, val)
else:
self.thisptr.set_binary_interaction_double(<string>arg1, <string>arg2, parameter, val)
cpdef double get_binary_interaction_double(self, string_or_size_t arg1, string_or_size_t arg2, string parameter) except *:
""" Get a double precision interaction parameter - wrapper of c++ function :cpapi:`CoolProp::AbstractState::get_binary_interaction_double` """
if string_or_size_t in cython.integral:
return self.thisptr.get_binary_interaction_double(<size_t>arg1, <size_t>arg2, parameter)
else:
return self.thisptr.get_binary_interaction_double(<string>arg1, <string>arg2, parameter)

cpdef set_binary_interaction_string(self, string CAS1, string CAS2, string parameter, string val):
""" Set a string interaction parameter - wrapper of c++ function :cpapi:`CoolProp::AbstractState::set_binary_interaction_string` """
self.thisptr.set_binary_interaction_string(CAS1, CAS2, parameter, val)
cpdef double get_binary_interaction_double(self, string_or_size_t arg1, string_or_size_t arg2, string parameter) except *:
""" Get a double precision interaction parameter - wrapper of c++ function :cpapi:`CoolProp::AbstractState::get_binary_interaction_double` """
return self.thisptr.get_binary_interaction_double(arg1, arg2, parameter)

cpdef string get_binary_interaction_string(self, string CAS1, string CAS2, string parameter) except *:
""" Get a string interaction parameter - wrapper of c++ function :cpapi:`CoolProp::AbstractState::get_binary_interaction_string` """
return self.thisptr.get_binary_interaction_string(CAS1, CAS2, parameter)
Expand Down Expand Up @@ -147,7 +153,7 @@ cdef class AbstractState:
return collection
cpdef tuple criticality_contour_values(self):
"""
Gets the crticality matrix values L1* and M1* - wrapper of c++ function :cpapi:`CoolProp::AbstractState::criticality_values`
Gets the criticality matrix values L1* and M1* - wrapper of c++ function :cpapi:`CoolProp::AbstractState::criticality_values`
Returns a tuple of (L1*, M1*)
"""
cdef CoolPropDbl L1star = 0, M1star = 0
Expand Down Expand Up @@ -175,16 +181,16 @@ cdef class AbstractState:
## ----------------------------------------

cpdef double keyed_output(self, parameters iOutput) except *:
""" Update :cpapi:`CoolProp::AbstractState::keyed_output(parameters key)` """
""" Get a keyed output :cpapi:`CoolProp::AbstractState::keyed_output(parameters key)` """
return self.thisptr.keyed_output(iOutput)
cpdef double trivial_keyed_output(self, parameters iOutput) except *:
""" Update :cpapi:`CoolProp::AbstractState::trivial_keyed_output(parameters key)` """
""" Get a trivial keyed output not requiring any iteration :cpapi:`CoolProp::AbstractState::trivial_keyed_output(parameters key)` """
return self.thisptr.trivial_keyed_output(iOutput)
cpdef double saturated_liquid_keyed_output(self, parameters iOutput) except *:
""" Update :cpapi:`CoolProp::AbstractState::saturated_liquid_keyed_output(parameters key)` """
""" Get a trivial output for the saturated liquid :cpapi:`CoolProp::AbstractState::saturated_liquid_keyed_output(parameters key)` """
return self.thisptr.saturated_liquid_keyed_output(iOutput)
cpdef double saturated_vapor_keyed_output(self, parameters iOutput) except *:
""" Update :cpapi:`CoolProp::AbstractState::saturated_vapor_keyed_output(parameters key)` """
""" Get a trivial output for the saturated vapor :cpapi:`CoolProp::AbstractState::saturated_vapor_keyed_output(parameters key)` """
return self.thisptr.saturated_vapor_keyed_output(iOutput)

cpdef double T(self) except *:
Expand Down
10 changes: 7 additions & 3 deletions wrappers/Python/CoolProp/Plots/ConsistencyPlots.py
Expand Up @@ -63,7 +63,7 @@ def split_pair_xy(pair):
raise ValueError(pair)

class ConsistencyFigure(object):
def __init__(self, fluid, figsize = (15, 23), backend = 'HEOS', additional_skips = []):
def __init__(self, fluid, figsize = (15, 23), backend = 'HEOS', additional_skips = [], mole_fractions = None):

self.fluid = fluid
self.backend = backend
Expand All @@ -72,6 +72,9 @@ def __init__(self, fluid, figsize = (15, 23), backend = 'HEOS', additional_skips
pairs_generator = iter(self.pairs)

states = [CP.AbstractState(backend, fluid) for _ in range(3)]
if mole_fractions is not None:
for state in states:
state.set_mole_fractions(mole_fractions)
self.axes_list = []
for row in self.axes:
for ax in row:
Expand Down Expand Up @@ -435,13 +438,14 @@ def cross_out_axis(self):

if __name__=='__main__':
PVT = PdfPages('Consistency.pdf')
for fluid in ['Helium']:#CP.__fluids__:
CP.CoolProp.set_debug_level(10)
for fluid in ['R410A.mix']:#CP.__fluids__:
print('************************************************')
print(fluid)
print('************************************************')
skips = ['DmolarHmolar','DmolarSmolar','DmolarUmolar','HmolarSmolar']
skips = []
ff = ConsistencyFigure(fluid, backend = 'HEOS', additional_skips = skips)
ff = ConsistencyFigure(fluid, backend = 'BICUBIC&REFPROP', additional_skips = skips, mole_fractions = [0.7,0.3])
ff.add_to_pdf(PVT)
ff.savefig(fluid + '.png')
ff.savefig(fluid + '.pdf')
Expand Down

0 comments on commit 2160b02

Please sign in to comment.