bool is_valid_second_derivative(const std::string & name, parameters &iOf1, parameters &iWrt1, parameters &iConstant1, parameters &iWrt2, parameters &iConstant2) { if (get_debug_level() > 5){std::cout << format("is_valid_second_derivative(%s)",name.c_str());} // Suppose we start with "d(d(P)/d(Dmolar)|T)/d(Dmolar)|T" std::size_t i_bar = name.rfind('|'); if (i_bar == std::string::npos){return false;} std::string constant2 = name.substr(i_bar+1); // "T" if (!is_valid_parameter(constant2, iConstant2)){return false;}; std::string left_of_bar = name.substr(0, i_bar); // "d(d(P)/d(Dmolar)|T)/d(Dmolar)" std::size_t i_slash = left_of_bar.rfind('/'); if (i_slash == std::string::npos){return false;} std::string left_of_slash = left_of_bar.substr(0, i_slash); // "d(d(P)/d(Dmolar)|T)" std::size_t iN0 = left_of_slash.find("("); std::size_t iN1 = left_of_slash.rfind(")"); if (!(iN0 > 0 && iN1 > 0 && iN1 > iN0)){return false;} std::string num = left_of_slash.substr(iN0+1, iN1-2); // "d(P)/d(Dmolar)|T" if (!is_valid_first_derivative(num, iOf1, iWrt1, iConstant1)){return false;} std::string right_of_slash = left_of_bar.substr(i_slash+1); // "d(Dmolar)" std::size_t iD0 = right_of_slash.find("("); std::size_t iD1 = right_of_slash.rfind(")"); if (!(iD0 > 0 && iD1 > 0 && iD1 > iD0)){return false;} std::string den = right_of_slash.substr(iD0+1, iD1-2); // "Dmolar" if (!is_valid_parameter(den, iWrt2)){return false;} // If we haven't quit yet, all is well return true; }
bool is_valid_first_derivative(const std::string & name, parameters &iOf, parameters &iWrt, parameters &iConstant) { std::size_t iN0, iN1, iD0, iD1; parameters Of, Wrt, Constant; if (get_debug_level() > 5){std::cout << format("is_valid_first_derivative(%s)",name.c_str());} // There should be exactly one / // There should be exactly one | // Suppose we start with "d(P)/d(T)|Dmolar" std::vector<std::string> split_at_bar = strsplit(name, '|'); // "d(P)/d(T)" and "Dmolar" if (split_at_bar.size() != 2){return false;} std::vector<std::string> split_at_slash = strsplit(split_at_bar[0], '/'); // "d(P)" and "d(T)" if (split_at_slash.size() != 2){return false;} iN0 = split_at_slash[0].find("("); iN1 = split_at_slash[0].find(")", iN0); if (!(iN0 > 0 && iN1 > 0 && iN1 > iN0)){return false;} std::string num = split_at_slash[0].substr(iN0+1, iN1-2); iD0 = split_at_slash[1].find("("); iD1 = split_at_slash[1].find(")", iD0); if (!(iD0 > 0 && iD1 > 0 && iD1 > iD0)){return false;} std::string den = split_at_slash[1].substr(iD0+1, iD1-2); if (is_valid_parameter(num, Of) && is_valid_parameter(den, Wrt) && is_valid_parameter(split_at_bar[1], Constant)){ iOf = Of; iWrt = Wrt; iConstant = Constant; return true; } else{ return false; } }
bool is_valid_second_derivative(const std::string &name, parameters &iOf1, parameters &iWrt1, parameters &iConstant1, parameters &iWrt2, parameters &iConstant2) { if (get_debug_level() > 5){std::cout << format("is_valid_second_derivative(%s)",name.c_str());} // Suppose we start with "d(d(P)/d(Dmolar)|T)/d(Dmolar)|T" std::size_t i = name.rfind('|'); if ((i == 0) || (i == std::string::npos)){return false;} std::string constant2 = name.substr(i+1); // "T" if (!is_valid_parameter(constant2, iConstant2)){return false;}; std::string left_of_bar = name.substr(0, i); // "d(d(P)/d(Dmolar)|T)/d(Dmolar)" i = left_of_bar.rfind('/'); if ((i == 0) || (i == std::string::npos)){return false;} std::string left_of_slash = left_of_bar.substr(0, i); // "d(d(P)/d(Dmolar)|T)" std::string right_of_slash = left_of_bar.substr(i + 1); // "d(Dmolar)" i = left_of_slash.find("("); std::size_t i1 = left_of_slash.rfind(")"); if (!((i > 0) && (i != std::string::npos) && (i1 > (i+1)) && (i1 != std::string::npos))){return false;} std::string num = left_of_slash.substr(i+1, i1-i-1); // "d(P)/d(Dmolar)|T" if (!is_valid_first_derivative(num, iOf1, iWrt1, iConstant1)){return false;} i = right_of_slash.find("("); i1 = right_of_slash.rfind(")"); if (!((i > 0) && (i != std::string::npos) && (i1 > (i+1)) && (i1 != std::string::npos))){return false;} std::string den = right_of_slash.substr(i+1, i1-i-1); // "Dmolar" if (!is_valid_parameter(den, iWrt2)){return false;} // If we haven't quit yet, all is well return true; }
bool is_valid_first_saturation_derivative(const std::string &name, parameters &iOf, parameters &iWrt) { if (get_debug_level() > 5){ std::cout << format("is_valid_first_saturation_derivative(%s)", name.c_str()); } // There should be exactly one / // There should be exactly one | // Suppose we start with "d(P)/d(T)|sigma" std::vector<std::string> split_at_bar = strsplit(name, '|'); // "d(P)/d(T)" and "sigma" if (split_at_bar.size() != 2){ return false; } std::vector<std::string> split_at_slash = strsplit(split_at_bar[0], '/'); // "d(P)" and "d(T)" if (split_at_slash.size() != 2){ return false; } std::size_t i0 = split_at_slash[0].find("("); std::size_t i1 = split_at_slash[0].find(")", i0); if (!((i0 > 0) && (i0 != std::string::npos) && (i1 > (i0+1)) && (i1 != std::string::npos))){ return false; } std::string num = split_at_slash[0].substr(i0+1, i1-i0-1); i0 = split_at_slash[1].find("("); i1 = split_at_slash[1].find(")", i0); if (!((i0 > 0) && (i0 != std::string::npos) && (i1 > (i0+1)) && (i1 != std::string::npos))){ return false; } std::string den = split_at_slash[1].substr(i0+1, i1-i0-1); parameters Of, Wrt; if (is_valid_parameter(num, Of) && is_valid_parameter(den, Wrt) && upper(split_at_bar[1]) == "SIGMA"){ iOf = Of; iWrt = Wrt; return true; } else{ return false; } }
/// Parse a '&' separated string into a data structure with one entry per output /// Covers both normal and derivative outputs static std::vector<output_parameter> get_output_parameters(const std::vector<std::string> &Outputs){ std::vector<output_parameter> outputs; for (std::vector<std::string>::const_iterator str = Outputs.begin(); str != Outputs.end(); ++str){ output_parameter out; CoolProp::parameters iOutput; if (is_valid_parameter(*str, iOutput)){ out.Of1 = iOutput; if (is_trivial_parameter(iOutput)){ out.type = OUTPUT_TYPE_TRIVIAL; } else{ out.type = OUTPUT_TYPE_NORMAL; } } else if (is_valid_first_saturation_derivative(*str, out.Of1, out.Wrt1)){ out.type = OUTPUT_TYPE_FIRST_SATURATION_DERIVATIVE; } else if (is_valid_first_derivative(*str, out.Of1, out.Wrt1, out.Constant1)){ out.type = OUTPUT_TYPE_FIRST_DERIVATIVE; } else if (is_valid_second_derivative(*str, out.Of1, out.Wrt1, out.Constant1, out.Wrt2, out.Constant2)){ out.type = OUTPUT_TYPE_SECOND_DERIVATIVE; } else{ throw ValueError(format("Output string is invalid [%s]", str->c_str())); } outputs.push_back(out); } return outputs; };
void _PropsSImulti(const std::vector<std::string> &Outputs, const std::string &Name1, const std::vector<double> &Prop1, const std::string &Name2, const std::vector<double> &Prop2, const std::string &backend, const std::vector<std::string> &fluids, const std::vector<double> &fractions, std::vector<std::vector<double> > &IO) { shared_ptr<AbstractState> State; CoolProp::parameters key1, key2; CoolProp::input_pairs input_pair; std::vector<output_parameter> output_parameters; std::vector<double> v1, v2; try{ // Initialize the State class _PropsSI_initialize(backend, fluids, fractions, State); } catch(std::exception &e){ // Initialization failed. Stop. throw ValueError(format("Initialize failed for backend: \"%s\", fluid: \"%s\" fractions \"%s\"; error: %s",backend.c_str(), strjoin(fluids,"&").c_str(), vec_to_string(fractions, "%0.10f").c_str(), e.what()) ); } try{ // Get update pair is_valid_parameter(Name1, key1); is_valid_parameter(Name2, key2); input_pair = generate_update_pair(key1, Prop1, key2, Prop2, v1, v2); } catch (std::exception &e){ // Input parameter parsing failed. Stop throw ValueError(format("Input pair parsing failed for Name1: \"%s\", Name2: \"%s\"; err: %s", Name1.c_str(), Name2.c_str(), e.what())); } try{ output_parameters = output_parameter::get_output_parameters(Outputs); } catch (std::exception &e){ // Output parameter parsing failed. Stop. throw ValueError(format("Output parameter parsing failed; error: %s", e.what())); } // Calculate the output(s). In the case of a failure, all values will be filled with _HUGE _PropsSI_outputs(State, output_parameters, input_pair, v1, v2, IO); }
int get_parameter_index(const std::string ¶m_name) { parameters iOutput; if (is_valid_parameter(param_name, iOutput)){ return iOutput; } else{ throw ValueError(format("Your input name [%s] is not valid in get_parameter_index (names are case sensitive)",param_name.c_str())); } }
/* for `SYN_FUNCTION` */ static struct type *function_ast(struct ast_op *op) { /* get return */ struct type *ret = type_new_from_ast(op->childs[0]); /* get parameters */ struct ast *param_list = op->childs[1]; assert( is_valid_parameter_list(param_list) || is_valid_parameter_list_variadic(param_list) ); size_t n_params = param_list->val.op.n_childs; struct type *params[n_params]; for (unsigned i = 0; i < n_params; ++i) { struct ast *decl = param_list->val.op.childs[i]; assert(is_valid_parameter(decl)); params[i] = type_new_from_ast(decl->val.op.childs[1]); } /* create and return type */ return type_new_function( ret, n_params, params, param_list->type == SYN_PARAMETER_LIST_VARIADIC ); }
// Internal function to do the actual calculations, make this a wrapped function so // that error bubbling can be done properly double _PropsSI(const std::string &Output, const std::string &Name1, double Prop1, const std::string &Name2, double Prop2, const std::string &backend, const std::string &Ref, const std::vector<double> &z) { parameters iOutput = iundefined_parameter; parameters iOf = iundefined_parameter, iWrt = iundefined_parameter, iConstant = iundefined_parameter, iOf1 = iundefined_parameter, iWrt1 = iundefined_parameter, iConstant1 = iundefined_parameter, iWrt2 = iundefined_parameter, iConstant2 = iundefined_parameter; double x1, x2; if (get_debug_level()>5){ std::cout << format("%s:%d: _PropsSI(%s,%s,%g,%s,%g,%s,%s)\n",__FILE__,__LINE__,Output.c_str(),Name1.c_str(),Prop1,Name2.c_str(),Prop2,backend.c_str(),Ref.c_str(), vec_to_string(z).c_str()).c_str(); } // The state we are going to use shared_ptr<AbstractState> State; // If the fractions of the components have been encoded in the string, extract them // If they have not, this function does nothing std::vector<double> fractions; if (z.empty()) { // Make a one-element vector fractions = std::vector<double>(1, 1); } else{ // Make a copy fractions = z; } std::string fluid_string = extract_fractions(Ref, fractions); // We are going to let the factory function load the state State.reset(AbstractState::factory(backend, fluid_string)); // First check if it is a trivial input (critical/max parameters for instance) if (is_valid_parameter(Output, iOutput) && is_trivial_parameter(iOutput)) { double val = State->trivial_keyed_output(iOutput); return val; } long iName1 = get_parameter_index(Name1); long iName2 = get_parameter_index(Name2); if (State->using_mole_fractions()){ State->set_mole_fractions(fractions); } else if (State->using_mass_fractions()){ State->set_mass_fractions(fractions); } else if (State->using_volu_fractions()){ State->set_volu_fractions(fractions); } else { if (get_debug_level()>50) std::cout << format("%s:%d: _PropsSI, could not set composition to %s, defaulting to mole fraction.\n",__FILE__,__LINE__, vec_to_string(z).c_str()).c_str(); } // Obtain the input pair CoolProp::input_pairs pair = generate_update_pair(iName1, Prop1, iName2, Prop2, x1, x2); // Update the state State->update(pair, x1, x2); if (iOutput != iundefined_parameter){ // Get the desired output double val = State->keyed_output(iOutput); // Return the value return val; } else if (is_valid_first_derivative(Output, iOf, iWrt, iConstant)){ // Return the desired output double val = State->first_partial_deriv(iOf, iWrt, iConstant); // Return the value return val; } else if (is_valid_second_derivative(Output, iOf1, iWrt1, iConstant1, iWrt2, iConstant2)){ // Return the desired output double val = State->second_partial_deriv(iOf1, iWrt1, iConstant1, iWrt2, iConstant2); // Return the value return val; } else{ throw ValueError(format("Output [%s] is not a parameter or a string representation of a derivative",Output.c_str()).c_str()); } }