diff --git a/src/CoolProp.cpp b/src/CoolProp.cpp index b4c4ae17..a7048bad 100644 --- a/src/CoolProp.cpp +++ b/src/CoolProp.cpp @@ -33,6 +33,10 @@ #include "Backends/Helmholtz/MixtureParameters.h" #include "DataStructures.h" +#if defined(ENABLE_CATCH) + #include "catch.hpp" +#endif + namespace CoolProp { @@ -47,185 +51,12 @@ int get_debug_level(void){return debug_level;} #include "gitrevision.h" // Contents are like "std::string gitrevision = "aa121435436ggregrea4t43t433";" #include "cpversion.h" // Contents are like "char version [] = "2.5";" -//int global_Phase = -1; - void set_warning_string(std::string warning){ warning_string = warning; } void set_error_string(std::string error){ error_string = error; } -// -//static int IsCoolPropFluid(std::string FluidName) -//{ -// // Try to get the fluid from Fluids by name -// try -// { -// pFluid = Fluids.get_fluid(FluidName); -// } -// catch (NotImplementedError &) -// { -// return false; -// } -// // If NULL, didn't find it (or its alias) -// if (pFluid!=NULL) -// { -// return true; -// } -// else -// return false; -//} -// -//static int IsBrine(const char* Ref) -//{ -// // First check whether it is one of the Brines that does -// // not have a pure-fluid equivalent in CoolProp -// if ( -// strcmp(Ref,"HC-10")==0 || -// strncmp(Ref,"PG-",3)==0 || -// strncmp(Ref,"EG-",3)==0 || -// strncmp(Ref,"EA-",3)==0 || -// strncmp(Ref,"MA-",3)==0 || -// strncmp(Ref,"Glycerol-",9)==0 || -// strncmp(Ref,"K2CO3-",6)==0 || -// strncmp(Ref,"CaCl2-",6)==0 || -// strncmp(Ref,"MgCl2-",6)==0 || -// strncmp(Ref,"NaCl-",5)==0 || -// strncmp(Ref,"KAC-",4)==0 || -// strncmp(Ref,"KFO-",4)==0 || -// strncmp(Ref,"LiCl-",4)==0 || -// strncmp(Ref,"NH3/H2O-",8)==0 -// ) -// { -// return 1; -// } -// // Then check for diluants that are also pure fluids in CoolProp -// else if ( (strncmp(Ref,"Methanol-",9)==0 && Ref[8] == '-') || -// (strncmp(Ref,"Ethanol-",8)==0 && Ref[7] == '-') || -// (strncmp(Ref,"NH3-",4)==0 && Ref[3] == '-') -// ) -// { -// return 1; -// } -// else -// { -// return 0; -// } -//} -// -//long getFluidType(std::string FluidName){ -// if (IsREFPROP(FluidName)) { return FLUID_TYPE_REFPROP;} -// else if(IsIncompressibleLiquid(FluidName)){ return FLUID_TYPE_INCOMPRESSIBLE_LIQUID;} -// // TODO SOLUTION: Check if working -// else if(IsIncompressibleSolution(FluidName)){ return FLUID_TYPE_INCOMPRESSIBLE_SOLUTION; } -// else { -// // Try to get the index of the fluid -// long iFluid = get_Fluid_index(FluidName); -// // If iFluid is greater than -1, it is a CoolProp Fluid, otherwise not -// if (iFluid > -1) { -// // Get a pointer to the fluid object -// pFluid = get_fluid(iFluid); -// if (pFluid->pure()) { return FLUID_TYPE_PURE;} -// else { return FLUID_TYPE_PSEUDOPURE; } -// } else { -// throw ValueError(format("Bad Fluid name [%s] - not a CoolProp fluid",FluidName.c_str())); -// } -// } -// return -1; -//} -// -//EXPORT_CODE int CONVENTION IsFluidType(const char *Ref, const char *Type) -//{ -// pFluid = Fluids.get_fluid(Ref); -// -// if (IsBrine(Ref)){ // TODO Solution: Remove this part -// if (!strcmp(Type,"Brine")){ -// return 1; -// } -// else{ -// return 0; -// } -// } -// else if (IsIncompressibleSolution(Ref)){ -// if (!strcmp(Type,"Solution")){ -// return 1; -// } -// else{ -// return 0; -// } -// } -// else if (IsIncompressibleLiquid(Ref)){ -// if (!strcmp(Type,"Liquid")){ -// return 1; -// } -// else{ -// return 0; -// } -// } -// else if (IsREFPROP(Ref)){ -// if (!strcmp(Type,"PureFluid")){ -// return 1; -// } -// else{ -// return 0; -// } -// } -// else if (!pFluid->pure()){ -// if (!strcmp(Type,"PseudoPure") || !strcmp(Type,"PseudoPureFluid")){ -// return 1; -// } -// else{ -// return 0; -// } -// } -// else if (pFluid->pure()){ -// if (!strcmp(Type,"PureFluid")){ -// return 1; -// } -// else{ -// return 0; -// } -// } -// else -// { -// return 0; -// } -//} -// -// -//std::string Phase_Trho(std::string Fluid, double T, double rho) -//{ -// try{ -// // Try to load the CoolProp Fluid -// pFluid = Fluids.get_fluid(Fluid); -// double pL,pV,rhoL,rhoV; -// return pFluid->phase_Trho(T,rho, pL, pV, rhoL, rhoV); -// } -// catch(NotImplementedError &){ -// return std::string(""); -// } -// return std::string(""); -//} -// -//std::string Phase(std::string Fluid, double T, double p) -//{ -// try{ -// // Try to load the CoolProp Fluid -// pFluid = Fluids.get_fluid(Fluid); -// double pL,pV,rhoL,rhoV; -// return pFluid->phase_Tp(T, p, pL, pV, rhoL, rhoV); -// } -// catch(NotImplementedError &){ -// return std::string(""); -// } -// return std::string(""); -//} -// -//std::string Phase_Tp(std::string Fluid, double T, double p) -//{ -// return Phase(Fluid,T,p); -//} -// // Return true if the string has "BACKEND::*" format where * signifies a wildcard bool has_backend_in_string(const std::string &fluid_string, std::size_t &i) @@ -546,253 +377,6 @@ bool is_valid_fluid_string(std::string &input_fluid_string) return false; } } - -//EXPORT_CODE double CONVENTION IProps(long iOutput, long iName1, double Prop1, long iName2, double Prop2, long iFluid) -//{ -// Prop1 = convert_from_unit_system_to_SI(iName1, Prop1, get_standard_unit_system()); -// Prop2 = convert_from_unit_system_to_SI(iName2, Prop2, get_standard_unit_system()); -// double out = IPropsSI(iOutput,iName1,Prop1,iName2,Prop2,iFluid); -// return convert_from_SI_to_unit_system(iOutput,out,get_standard_unit_system()); -//} -//double _CoolProp_Fluid_PropsSI(long iOutput, long iName1, double Prop1, long iName2, double Prop2, Fluid *pFluid) -//{ -// double val = _HUGE, T = _HUGE; -// // This private method uses the indices directly for speed -// -// if (get_debug_level()>3){ -// std::cout << format("%s:%d: _CoolProp_Fluid_PropsSI(%d,%d,%g,%d,%g,%s)\n",__FILE__,__LINE__,iOutput,iName1, Prop1, iName2, Prop2, pFluid->get_name().c_str()).c_str(); -// } -// if (iName1 == iT){ -// T = Prop1;} -// else if (iName2 == iT){ -// T = Prop2;} -// -// // Generate a State instance wrapped around the Fluid instance -// CoolPropStateClassSI CPS(pFluid); -// -// // Check if it is an output that doesn't require a state input -// // Deal with it and return -// switch (iOutput) -// { -// case iI: -// { -// if (!ValidNumber(T)){throw ValueError(format("T must be provided as an input to use this output").c_str());} -// return CPS.pFluid->surface_tension_T(T); -// } -// case iRhosatLanc: -// { -// if (!ValidNumber(T)){throw ValueError(format("T must be provided as an input to use this output").c_str());} -// return CPS.pFluid->rhosatL(T); -// } -// case iRhosatVanc: -// { -// if (!ValidNumber(T)){throw ValueError(format("T must be provided as an input to use this output").c_str());} -// return CPS.pFluid->rhosatV(T); -// } -// case iPsatLanc: -// { -// if (!ValidNumber(T)){throw ValueError(format("T must be provided as an input to use this output").c_str());} -// if (CPS.pFluid->pure()){ -// return CPS.pFluid->psat(T); -// } -// else{ -// return CPS.pFluid->psatL(T); -// } -// } -// case iPsatVanc: -// { -// if (!ValidNumber(T)){throw ValueError(format("T must be provided as an input to use this output").c_str());} -// if (CPS.pFluid->pure()){ -// return CPS.pFluid->psat(T); -// } -// else{ -// return CPS.pFluid->psatV(T); -// } -// } -// case iMM: -// case iPcrit: -// case iTcrit: -// case iTtriple: -// case iPtriple: -// case iPmax: -// case iTmax: -// case iRhocrit: -// case iTmin: -// case iAccentric: -// case iPHASE_LIQUID: -// case iPHASE_GAS: -// case iPHASE_SUPERCRITICAL: -// case iPHASE_TWOPHASE: -// case iGWP20: -// case iGWP100: -// case iGWP500: -// case iODP: -// case iCritSplineT: -// case iScrit: -// case iHcrit: -// case iTreduce: -// case iRhoreduce: -// return CPS.keyed_output(iOutput); -// } -// -// // Update the class -// CPS.update(iName1,Prop1,iName2,Prop2); -// -// // Debug -// if (get_debug_level()>9){std::cout << format("%s:%d: State update successful\n",__FILE__,__LINE__).c_str();} -// -// // Get the output -// val = CPS.keyed_output(iOutput); -// -// // Debug -// if (get_debug_level()>5){std::cout << format("%s:%d: _CoolProp_Fluid_PropsSI returns: %g\n",__FILE__,__LINE__,val).c_str();} -// -// // Return the value -// return val; -//} -//EXPORT_CODE double CONVENTION IPropsSI(long iOutput, long iName1, double Prop1, long iName2, double Prop2, long iFluid) -//{ -// pFluid = Fluids.get_fluid(iFluid); -// // Didn't work -// if (pFluid == NULL){ -// err_string=std::string("CoolProp error: ").append(format("%d is an invalid fluid index to IProps",iFluid)); -// return _HUGE; -// } -// else{ -// // In this function the error catching happens; -// try{ -// // This is already converted to the right units since we take in SI units -// return _CoolProp_Fluid_PropsSI(iOutput,iName1,Prop1,iName2,Prop2,pFluid); -// } -// catch(std::exception &e){ -// err_string=std::string("CoolProp error: ").append(e.what()); -// return _HUGE; -// } -// catch(...){ -// err_string=std::string("CoolProp error: Indeterminate error"); -// return _HUGE; -// } -// } -//} - -///// Calculate some interesting derivatives -//double _CoolProp_Deriv_Terms(long iTerm, double T, double rho, Fluid * pFluid) -//{ -// double val = _HUGE; -// // This private method uses the indices directly for speed -// -// if (get_debug_level()>3){ -// std::cout<<__FILE__<<" _CoolProp_Deriv_Terms return: "<5){ -// std::cout<<__FILE__<<" _CoolProp_Deriv_Terms return: "<5){ -// std::cout<<__FILE__<<": "<get_components()[0]; - if (!ParamName.compare("aliases")) - { + if (!ParamName.compare("aliases")){ return strjoin(fluid->aliases, ", "); } - else if (!ParamName.compare("CAS") || !ParamName.compare("CAS_number")) - { + else if (!ParamName.compare("CAS") || !ParamName.compare("CAS_number")){ return fluid->CAS; } - else if (!ParamName.compare("ASHRAE34")) - { + else if (!ParamName.compare("ASHRAE34")){ return fluid->environment.ASHRAE34; } - else if (!ParamName.compare("REFPROPName") || !ParamName.compare("REFPROP_name") || !ParamName.compare("REFPROPname")) - { + else if (!ParamName.compare("REFPROPName") || !ParamName.compare("REFPROP_name") || !ParamName.compare("REFPROPname")){ return fluid->REFPROPname; } else if (ParamName.find("BibTeX") == 0) // Starts with "BibTeX" { std::vector parts = strsplit(ParamName,'-'); if (parts.size() != 2){ throw ValueError(format("Unable to parse BibTeX string %s",ParamName.c_str()));} - // - std::string item = parts[1]; - if (item == "EOS"){ - return fluid->pEOS->BibTeX_EOS; - } - else if (item == "CP0"){ - return fluid->pEOS->BibTeX_CP0; - } - else if (item == "SURFACE_TENSION"){ - return fluid->ancillaries.surface_tension.BibTeX; - } - else if (item == "MELTING_LINE"){ - return fluid->ancillaries.melting_line.BibTeX; - } - else if (item == "VISCOSITY"){ - return fluid->transport.BibTeX_viscosity; - } - else if (item == "CONDUCTIVITY"){ - return fluid->transport.BibTeX_conductivity; - } - else{ - throw ValueError(format("Could not match BibTeX item: %s", item.c_str())); - } + return get_BibTeXKey( FluidName, parts[1]); } - else - { + else{ throw ValueError(format("Input value [%s] is invalid for Fluid [%s]",ParamName.c_str(),FluidName.c_str())); } } - catch(std::exception &e) - { - throw ValueError(format("CoolProp error: %s", e.what())); - } - catch(...){ - throw ValueError("CoolProp error: Indeterminate error"); - } + catch(std::exception &e){ throw ValueError(format("CoolProp error: %s", e.what())); } + catch(...){ throw ValueError("CoolProp error: Indeterminate error"); } } +#if defined(ENABLE_CATCH) +TEST_CASE("Check inputs to get_fluid_param_string", "[get_fluid_param_string]") +{ + const int num_good_inputs = 10; + std::string good_inputs[num_good_inputs] = {"aliases", "CAS", "ASHRAE34", "REFPROPName", "BibTeX-CONDUCTIVITY", "BibTeX-EOS", "BibTeX-CP0", "BibTeX-SURFACE_TENSION","BibTeX-MELTING_LINE","BibTeX-VISCOSITY"}; + std::ostringstream ss3c; + for (int i = 0; i < num_good_inputs; ++i){ + ss3c << "Test for" << good_inputs[i]; + SECTION(ss3c.str(), ""){ + CHECK_NOTHROW(CoolProp::get_fluid_param_string("Water", good_inputs[i])); + }; + } + CHECK_THROWS(CoolProp::get_fluid_param_string("","aliases")); + CHECK_THROWS(CoolProp::get_fluid_param_string("Water","")); + CHECK_THROWS(CoolProp::get_fluid_param_string("Water","BibTeX-")); +}; +#endif std::string phase_lookup_string(phases Phase) { switch (Phase) @@ -1034,36 +613,4 @@ std::string PhaseSI(const std::string &Name1, double Prop1, const std::string &N return phase_lookup_string(static_cast(Phase_int)); } -} /* namespace CoolProp */ - -#if defined(ENABLE_CATCH) -#include "catch.hpp" -TEST_CASE("Check inputs to get_global_param_string","[get_global_param_string]") -{ - const int num_good_inputs = 7; - std::string good_inputs[num_good_inputs] = {"version", "gitrevision", "fluids_list", "incompressible_list_pure", "incompressible_list_solution", "mixture_binary_pairs_list","parameter_list"}; - std::ostringstream ss3c; - for (int i = 0; i