2 #define _CRTDBG_MAP_ALLOC
3 #define _CRT_SECURE_NO_WARNINGS
10 #if defined(__ISWINDOWS__)
15 #define DBL_EPSILON std::numeric_limits<double>::epsilon()
39 double _PropsSI(std::string Output,std::string Name1,
double Prop1, std::string Name2,
double Prop2, std::string Ref);
40 double _Props1SI(std::string FluidName, std::string Output);
42 static std::string err_string;
43 static std::string warning_string;
45 static Fluid * pFluid;
59 void set_warning(std::string warning){ warning_string = warning; }
66 std::make_pair(
"E",
iPcrit),
67 std::make_pair(
"M",
iMM),
71 std::make_pair(
"B",
iTcrit),
73 std::make_pair(
"pcrit",
iPcrit),
74 std::make_pair(
"PCRIT",
iPcrit),
75 std::make_pair(
"molemass",
iMM),
76 std::make_pair(
"MOLEMASS",
iMM),
79 std::make_pair(
"dipole",
iDipole),
80 std::make_pair(
"DIPOLE",
iDipole),
81 std::make_pair(
"Tmin",
iTmin),
82 std::make_pair(
"TMIN",
iTmin),
83 std::make_pair(
"t",
iTmin),
84 std::make_pair(
"Tmax",
iTmax),
85 std::make_pair(
"TMAX",
iTmax),
86 std::make_pair(
"pmax",
iPmax),
87 std::make_pair(
"PMAX",
iPmax),
94 std::make_pair(
"Tcrit",
iTcrit),
95 std::make_pair(
"TCRIT",
iTcrit),
100 std::make_pair(
"Hcrit",
iHcrit),
101 std::make_pair(
"HCRIT",
iHcrit),
102 std::make_pair(
"Scrit",
iScrit),
103 std::make_pair(
"SCRIT",
iScrit),
105 std::make_pair(
"Q",
iQ),
106 std::make_pair(
"T",
iT),
107 std::make_pair(
"P",
iP),
108 std::make_pair(
"D",
iD),
109 std::make_pair(
"C",
iC),
110 std::make_pair(
"C0",
iC0),
111 std::make_pair(
"O",
iO),
112 std::make_pair(
"U",
iU),
113 std::make_pair(
"H",
iH),
114 std::make_pair(
"S",
iS),
115 std::make_pair(
"A",
iA),
116 std::make_pair(
"G",
iG),
117 std::make_pair(
"V",
iV),
118 std::make_pair(
"L",
iL),
121 std::make_pair(
"Tmax",
iTmax),
123 std::make_pair(
"Psat",
iPsat),
124 std::make_pair(
"I",
iI),
125 std::make_pair(
"SurfaceTension",
iI),
130 std::make_pair(
"Phase",
iPhase),
135 std::make_pair(
"ODP",
iODP),
136 std::make_pair(
"GWP20",
iGWP20),
137 std::make_pair(
"GWP100",
iGWP100),
138 std::make_pair(
"GWP500",
iGWP500),
142 std::make_pair(
"Z",
iDERZ),
145 std::make_pair(
"VB",
iDERB),
147 std::make_pair(
"VC",
iDERC),
193 err_string = error_string;
197 if (!Phase_str.compare(
"Two-Phase")){
203 else if (!Phase_str.compare(
"Liquid")){
209 else if (!Phase_str.compare(
"Gas")){
215 else if (!Phase_str.compare(
"Supercritical")){
221 else if (!Phase_str.compare(
"SaturatedL")){
227 else if (!Phase_str.compare(
"SaturatedV")){
255 std::vector<double>
x(100);
257 if (!(FluidName.find(
"REFPROP-") == 0))
return false;
268 std::cout <<
format(
"%s:%d: Added the fluid %s\n",__FILE__,__LINE__,FluidName.c_str()).c_str();
285 return "Fluid name invalid";
290 std::map<std::string,long>::iterator it;
305 static int IsCoolPropFluid(std::string FluidName)
325 static int IsBrine(
const char* Ref)
330 strcmp(Ref,
"HC-10")==0 ||
331 strncmp(Ref,
"PG-",3)==0 ||
332 strncmp(Ref,
"EG-",3)==0 ||
333 strncmp(Ref,
"EA-",3)==0 ||
334 strncmp(Ref,
"MA-",3)==0 ||
335 strncmp(Ref,
"Glycerol-",9)==0 ||
336 strncmp(Ref,
"K2CO3-",6)==0 ||
337 strncmp(Ref,
"CaCl2-",6)==0 ||
338 strncmp(Ref,
"MgCl2-",6)==0 ||
339 strncmp(Ref,
"NaCl-",5)==0 ||
340 strncmp(Ref,
"KAC-",4)==0 ||
341 strncmp(Ref,
"KFO-",4)==0 ||
342 strncmp(Ref,
"LiCl-",4)==0 ||
343 strncmp(Ref,
"NH3/H2O-",8)==0
349 else if ( (strncmp(Ref,
"Methanol-",9)==0 && Ref[8] ==
'-') ||
350 (strncmp(Ref,
"Ethanol-",8)==0 && Ref[7] ==
'-') ||
351 (strncmp(Ref,
"NH3-",4)==0 && Ref[3] ==
'-')
361 static int IsREFPROP(std::string Ref)
363 if (!Ref.compare(0,8,
"REFPROP-"))
384 throw ValueError(
format(
"Bad Fluid name [%s] - not a CoolProp fluid",FluidName.c_str()));
395 if (!strcmp(Type,
"Brine")){
403 if (!strcmp(Type,
"Solution")){
411 if (!strcmp(Type,
"Liquid")){
418 else if (IsREFPROP(Ref)){
419 if (!strcmp(Type,
"PureFluid")){
426 else if (!pFluid->
pure()){
427 if (!strcmp(Type,
"PseudoPure") || !strcmp(Type,
"PseudoPureFluid")){
434 else if (pFluid->
pure()){
435 if (!strcmp(Type,
"PureFluid")){
448 double _T_hp_secant(std::string Ref,
double h,
double p,
double T_guess)
450 double x1=0,x2=0,x3=0,y1=0,y2=0,eps=1e-8,change=999,f=999,T=300;
453 while ((iter<=3 || fabs(f)>eps) && iter<100)
455 if (iter==1){x1=T_guess; T=x1;}
456 if (iter==2){x2=T_guess+0.1; T=x2;}
458 f=
PropsSI(
"H",
"T",T,
"P",p,Ref)-h;
463 x3=x2-y2/(y2-y1)*(x2-x1);
464 change=fabs(y2/(y2-y1)*(x2-x1));
470 throw SolutionError(
format(
"iter %d: T_hp not converging with inputs(%s,%g,%g,%g) value: %0.12g\n",iter,(
char*)Ref.c_str(),h,p,T_guess,f));
481 double pL,pV,rhoL,rhoV;
482 return pFluid->
phase_Trho(T,rho, pL, pV, rhoL, rhoV);
485 return std::string(
"");
487 return std::string(
"");
495 double pL,pV,rhoL,rhoV;
496 return pFluid->
phase_Tp(T, p, pL, pV, rhoL, rhoV);
499 return std::string(
"");
501 return std::string(
"");
506 return Phase(Fluid,T,p);
514 double _Props1SI(std::string FluidName, std::string Output)
525 throw ValueError(
format(
"Your output key [%s] is not valid. (names are case sensitive)",Output.c_str()));
530 else if (IsREFPROP(FluidName))
543 out =
Props(Output,
'T',0,
'P',0,FluidName);
546 throw ValueError(
format(
"Output parameter \"%s\" is invalid for REFPROP fluid",Output.c_str()));
552 throw ValueError(
format(
"Fluid \"%s\" is an invalid fluid",FluidName.c_str()));
556 double _Props1(std::string FluidName, std::string Output)
558 double val =
_Props1SI(FluidName, Output);
570 double Props1SI(std::string FluidName,std::string Output){
576 catch(
const std::exception& e){
577 err_string = std::string(
"CoolProp error: ").append(e.what());
581 err_string = std::string(
"CoolProp error: Indeterminate error");
586 double Props1(std::string FluidName, std::string Output){
591 return _Props1(FluidName, Output);
593 catch(
const std::exception& e){
594 err_string = std::string(
"CoolProp error: ").append(e.what());
598 err_string = std::string(
"CoolProp error: Indeterminate error");
613 double _PropsSI(std::string Output, std::string Name1,
double Prop1, std::string Name2,
double Prop2, std::string Ref)
616 std::cout <<
format(
"%s:%d: _Props(%s,%s,%g,%s,%g,%s)\n",__FILE__,__LINE__,Output.c_str(),Name1.c_str(),Prop1,Name2.c_str(),Prop2,Ref.c_str()).c_str();
622 throw ValueError(
format(
"Your output key [%s] is not valid. (names are case sensitive)",Output.c_str()));
625 throw ValueError(
format(
"Your input key #1 [%s] is not valid. (names are case sensitive)",Name1.c_str()));
628 throw ValueError(
format(
"Your input key #2 [%s] is not valid. (names are case sensitive)",Name2.c_str()));
641 if (
get_debug_level()>7) std::cout<<__FILE__<<
": Identified Refprop fluid - "<<Ref.c_str()<<std::endl;
644 return REFPROPSI(iOutput,iName1,Prop1,iName2,Prop2,Ref);
646 throw AttributeError(
format(
"Your fluid [%s] is from REFPROP, but CoolProp does not support REFPROP on this platform, yet.",Ref.c_str()));
650 else if (IsCoolPropFluid(Ref))
652 if (
get_debug_level()>7) std::cout <<
format(
"%s:%d: Identified CoolProp fluid - %s\n",__FILE__,__LINE__,Ref.c_str()).c_str();
658 else if (IsBrine(Ref.c_str()))
660 if (
get_debug_level()>7) std::cout<<__FILE__<<
": Identified brine - "<<Ref.c_str()<<std::endl;
662 if ((iName1 ==
iH && iName2 ==
iP) || (iName2 ==
iH && iName1 ==
iP))
664 if (iName2 ==
iH && iName1 ==
iP)
666 std::swap(Prop1,Prop2);
667 std::swap(Name1,Name2);
670 double Tguess =
SecFluids(
"Tmax",Prop1,Prop2,Ref)-10;
676 else if ((iName1 ==
iT && iName2 ==
iP) || (iName1 ==
iP && iName2 ==
iT))
678 if (iName1 ==
iP && iName2 ==
iT){
679 std::swap(Prop1,Prop2);
685 throw ValueError(
"For brine, inputs must be (order does not matter) 'T' and 'P', or 'H' and 'P'");
691 if (
get_debug_level()>7) std::cout<<__FILE__<<
": Identified incompressible liquid - "<<Ref.c_str()<<std::endl;
693 if ((iName1 ==
iH && iName2 ==
iP) || (iName2 ==
iH && iName1 ==
iP))
695 if (iName2 ==
iH && iName1 ==
iP)
697 std::swap(Prop1,Prop2);
698 std::swap(Name1,Name2);
703 double T_guess = Tma - 10.0 ;
708 else if ((iName1 ==
iT && iName2 ==
iP) || (iName1 ==
iP && iName2 ==
iT))
710 if (iName1 ==
iP && iName2 ==
iT){
711 std::swap(Prop1, Prop2);
717 throw ValueError(
"For incompressible fluids, inputs must be (order does not matter) 'T' and 'P', or 'H' and 'P'");
723 if (
get_debug_level()>7) std::cout<<__FILE__<<
": Identified incompressible solution - "<<Ref.c_str()<<std::endl;
725 if ((iName1 ==
iH && iName2 ==
iP) || (iName2 ==
iH && iName1 ==
iP))
727 if (iName2 ==
iH && iName1 ==
iP)
729 std::swap(Prop1,Prop2);
730 std::swap(Name1,Name2);
736 double T_guess = (Tma+Tmi)/2.0 ;
741 else if ((iName1 ==
iT && iName2 ==
iP) || (iName1 ==
iP && iName2 ==
iT))
743 if (iName1 ==
iP && iName2 ==
iT){
744 std::swap(Prop1,Prop2);
750 throw ValueError(
"For incompressible solutions, inputs must be (order does not matter) 'T' and 'P', or 'H' and 'P'");
755 throw ValueError(
format(
"Your fluid name [%s] is not a CoolProp fluid, a REFPROP fluid, a brine or a liquid",Ref.c_str()));
762 double out =
IPropsSI(iOutput,iName1,Prop1,iName2,Prop2,iFluid);
767 double val = _HUGE, T = _HUGE;
771 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();
775 else if (iName2 ==
iT){
847 CPS.
update(iName1,Prop1,iName2,Prop2);
850 if (
get_debug_level()>9){std::cout <<
format(
"%s:%d: State update successful\n",__FILE__,__LINE__).c_str();}
856 if (
get_debug_level()>5){std::cout <<
format(
"%s:%d: _CoolProp_Fluid_PropsSI returns: %g\n",__FILE__,__LINE__,val).c_str();}
866 err_string=std::string(
"CoolProp error: ").append(
format(
"%d is an invalid fluid index to IProps",iFluid));
875 catch(std::exception &e){
876 err_string=std::string(
"CoolProp error: ").append(e.what());
880 err_string=std::string(
"CoolProp error: Indeterminate error");
886 double PropsSI(std::string Output, std::string Name1,
double Prop1, std::string Name2,
double Prop2, std::string Ref)
890 return _PropsSI(Output,Name1,Prop1,Name2,Prop2,Ref);
892 catch(
const std::exception& e){
897 set_err_string(std::string(
"CoolProp error: Indeterminate error"));
902 double Props(std::string Output, std::string Name1,
double Prop1, std::string Name2,
double Prop2, std::string Ref)
905 return PropsS(Output.c_str(),Name1.c_str(),Prop1,Name2.c_str(),Prop2,Ref.c_str());
907 double Props(std::string Output,
char Name1,
double Prop1,
char Name2,
double Prop2, std::string Ref)
909 return Props(Output, std::string(1,Name1), Prop1, std::string(1,Name2), Prop2, Ref);
919 std::cout<<__FILE__<<
" _CoolProp_Deriv_Terms return: "<<val<<std::endl;
990 throw ValueError(
format(
"Sorry DerivTerms is a work in progress, your derivative term [%d] is not available!",iTerm));
994 std::cout<<__FILE__<<
" _CoolProp_Deriv_Terms return: "<<val<<std::endl;
1004 double DerivTerms(std::string Term,
double T,
double rho, std::string Fluidname){
1006 std::cout<<__FILE__<<
": "<<Term.c_str()<<
",T="<<T<<
",rho="<<rho<<
","<<Fluidname.c_str()<<std::endl;
1011 if (IsCoolPropFluid(Fluidname))
1015 if ((!Term.compare(
"B")) || (!Term.compare(
"C"))) {
1016 Term = std::string(
"V").append(Term);
1021 throw ValueError(
format(
"Your output key [%s] is not valid. (names are case sensitive)",Term.c_str()));
1033 throw ValueError(
format(
"Your fluid name [%s] is not a CoolProp fluid.",Fluidname.c_str()));
1052 if (!reference_state.compare(
"IIR"))
1057 double h1 = CPS.
h();
1058 double s1 = CPS.
s();
1059 double deltah = h1-200000;
1060 double deltas = s1-1000;
1066 else if (!reference_state.compare(
"ASHRAE"))
1071 double h1 = CPS.
h();
1072 double s1 = CPS.
s();
1073 double deltah = h1-0;
1074 double deltas = s1-0;
1080 else if (!reference_state.compare(
"NBP"))
1085 double h1 = CPS.
h();
1086 double s1 = CPS.
s();
1087 double deltah = h1-0;
1088 double deltas = s1-0;
1108 double h1 = CPS.
h();
1109 double s1 = CPS.
s();
1110 double deltah = h1-h0;
1111 double deltas = s1-s0;
1129 else if (!item.compare(
"CP0")){
return pFluid->
BibTeXKeys.
CP0; }
1135 else{
return "Bad key";}
1138 return std::string(
"");
1144 if (!ParamName.compare(
"version"))
1146 return std::string(version);
1148 else if (!ParamName.compare(
"errstring"))
1150 std::string temp = err_string;
1151 err_string = std::string(
"");
1154 else if (!ParamName.compare(
"warnstring"))
1156 std::string temp = warning_string;
1157 warning_string = std::string(
"");
1160 else if (!ParamName.compare(
"gitrevision"))
1164 else if (!ParamName.compare(
"FluidsList") || !ParamName.compare(
"fluids_list"))
1170 return format(
"Input value [%s] is invalid",ParamName.c_str()).c_str();
1178 if (pFluid == NULL){
1179 err_string=std::string(
"CoolProp error: ").append(
format(
"%s is an invalid fluid for get_fluid_param_string",FluidName.c_str()));
1180 return format(
"%s is an invalid fluid for get_fluid_param_string",FluidName.c_str()).c_str();
1183 if (!ParamName.compare(
"aliases"))
1185 std::vector<std::string> v = pFluid->
get_aliases();
1188 else if (!ParamName.compare(
"CAS") || !ParamName.compare(
"CAS_number"))
1192 else if (!ParamName.compare(
"ASHRAE34"))
1196 else if (!ParamName.compare(
"REFPROPName") || !ParamName.compare(
"REFPROP_name") || !ParamName.compare(
"REFPROPname"))
1200 else if (!ParamName.compare(
"TTSE_mode"))
1215 return format(
"Input value [%s] is invalid for Fluid [%s]",ParamName.c_str(),FluidName.c_str()).c_str();
1219 catch(std::exception &e)
1221 return(std::string(
"CoolProp error: ").append(e.what()));
1224 return(std::string(
"CoolProp error: Indeterminate error"));
1229 #ifndef DISABLE_CATCH
1231 TEST_CASE(
"Check ancillaries and surface tension",
"[fast]" )
Fluid * get_fluid(long iFluid)
void set_warning(std::string warning)
EXPORT_CODE double CONVENTION IProps(long iOutput, long iName1, double Prop1, long iName2, double Prop2, long iFluid)
std::pair< std::string, long > map_data[]
int set_reference_stateP(Fluid *pFluid, std::string reference_state)
std::map< std::string, long > param_map(map_data, map_data+sizeof map_data/sizeof map_data[0])
double keyed_output(long iInput)
Return an output based on the integer key for the term.
long get_Fluid_index(std::string FluidName)
double _CoolProp_Deriv_Terms(long iTerm, double T, double rho, Fluid *pFluid)
Calculate some interesting derivatives.
std::string get_global_param_string(std::string ParamName)
std::vector< std::string > get_aliases()
void _set_standard_unit_system(int unit_sys)
EXPORT_CODE int CONVENTION get_debug_level()
double Props(std::string Output, std::string Name1, double Prop1, std::string Name2, double Prop2, std::string Ref)
double _Props1(std::string FluidName, std::string Output)
int _get_standard_unit_system()
virtual double psat(double T)
double Props1(std::string FluidName, std::string Output)
bool set_REFPROP_fluid(std::string Ref, std::vector< double > &x)
double REFPROPSI(long iOutput, long iName1, double Prop1, long iName2, double Prop2, std::string Ref)
void update(long iInput1, double Value1, long iInput2, double Value2, double T0=-1, double rho0=-1)
EXPORT_CODE int CONVENTION get_standard_unit_system(void)
struct CriticalStruct reduce
A pointer to the point that is used to reduce the T and rho for EOS.
double _T_hp_secant(std::string Ref, double h, double p, double T_guess)
std::string get_BibTeXKey(std::string Ref, std::string item)
std::string get_REFPROPname()
std::string get_name()
Returns a std::string with the name of the fluid.
double _PropsSI(std::string Output, std::string Name1, double Prop1, std::string Name2, double Prop2, std::string Ref)
virtual double surface_tension_T(double T)
virtual double rhosatV(double T)
long getFluidType(std::string FluidName)
Fluid * pFluid
A pointer to a CoolProp fluid.
double IncompLiquidSI(long iOutput, double T, double p_SI, long iFluid)
Fluid is the abstract base class that is employed by all the other fluids.
double convert_from_SI_to_unit_system(long iInput, double value, int new_system)
double PropsSI(std::string Output, std::string Name1, double Prop1, std::string Name2, double Prop2, std::string Ref)
EnvironmentalFactorsStruct environment
int set_reference_stateD(std::string Ref, double T, double rho, double h0, double s0)
static bool refpropSupported()
TTSESinglePhaseTableClass TTSESinglePhase
std::vector< double > x(ncmax, 0)
std::string ECS_LENNARD_JONES
void set_phase(std::string Phase_str)
Nearly deprecated function.
struct OtherParameters params
std::string SURFACE_TENSION
std::string FluidList()
Returns a std::string of a comma-separated list of the CoolProp names of all the fluids that are load...
double _CoolProp_Fluid_PropsSI(long iOutput, long iName1, double Value1, long iName2, double Value2, Fluid *pFluid)
The lower-level methods that can throw exceptions.
TEST_CASE("Check ancillaries and surface tension","[fast]")
EXPORT_CODE double CONVENTION PropsS(const char *Output, const char *Name1, double Prop1, const char *Name2, double Prop2, const char *Ref)
double SecFluids(std::string Output, double T, double p, std::string Ref)
virtual double psatV(double T)
bool add_REFPROP_fluid(std::string FluidName)
Add a REFPROP fluid to the fluid listing in CoolProp.
bool IsIncompressibleLiquid(std::string name)
#define REQUIRE_THROWS(expr)
std::string Phase_Trho(std::string Fluid, double T, double rho)
bool IsIncompressibleSolution(std::string name)
BibTeXKeysStruct BibTeXKeys
EXPORT_CODE int CONVENTION IsFluidType(const char *Ref, const char *Type)
double DerivTerms(long iTerm, double T, double rho, Fluid *pFluid)
double SecFluidsSI(std::string Output, double T, double p, std::string Ref)
std::string phase_Tp(double T, double p, double &pL, double &pV, double &rhoL, double &rhoV)
Return the phase given the temperature and pressure.
std::string Phase(std::string Fluid, double T, double p)
long get_param_index(std::string param)
#define REQUIRE_NOTHROW(expr)
std::vector< phi_BC * > phi0list
A vector of instances of the phi_BC classes for the residual Helmholtz energy contribution.
void update(long iInput1, double Value1, long iInput2, double Value2)
Fluid * get_fluid(std::string name)
std::string get_ASHRAE34(std::string fluid)
double convert_from_unit_system_to_SI(long iInput, double value, int old_system)
void set_err_string(std::string error_string)
bool pure()
Returns true if the fluid is pure, false if pseudo-pure or a mixture.
#define SECTION(name, description)
virtual double psatL(double T)
long get_fluid_index(Fluid *pFluid)
virtual double rhosatL(double T)
bool add_REFPROP_fluid(std::string FluidName, std::vector< double > xmol)
int set_reference_stateS(std::string Ref, std::string reference_state)
std::string Phase_Tp(std::string Fluid, double T, double p)
std::string get_fluid_param_string(std::string FluidName, std::string ParamName)
double keyed_output(long iInput)
Returns an output based on the key provided where iInput is one of iT,iP,iH,iS,....
double _Props1SI(std::string FluidName, std::string Output)
EXPORT_CODE double CONVENTION IPropsSI(long iOutput, long iName1, double Prop1, long iName2, double Prop2, long iFluid)
double Props1SI(std::string FluidName, std::string Output)
std::string phase_Trho(double T, double rho, double &pL, double &pV, double &rhoL, double &rhoV)
Return the phase given the temperature and the density.
double IncompSolutionSI(long iOutput, double T, double p, double x, long iFluid)