2 # define _CRTDBG_MAP_ALLOC
3 # define _CRT_SECURE_NO_WARNINGS
23 void str2buf(
const std::string& str,
char* buf,
int n) {
24 if (str.size() <
static_cast<unsigned int>(n))
29 void HandleException(
long* errcode,
char* message_buffer,
const long buffer_length) {
33 std::string errmsg = std::string(
"HandleError: ") + e.
what();
34 if (errmsg.size() <
static_cast<std::size_t
>(buffer_length)) {
36 strcpy(message_buffer, errmsg.c_str());
41 std::string errmsg = std::string(
"Error: ") + e.
what();
42 if (errmsg.size() <
static_cast<std::size_t
>(buffer_length)) {
44 strcpy(message_buffer, errmsg.c_str());
62 #elif defined(FE_ALL_EXCEPT)
63 feclearexcept(FE_ALL_EXCEPT);
69 std::cout <<
format(
"%s:%d: convert_from_kSI_to_SI(i=%d,value=%g)\n", __FILE__, __LINE__, iInput, value).c_str();
82 return value * 1000.0;
98 std::cout <<
format(
"%s:%d: convert_from_SI_to_kSI(%d,%g)\n", __FILE__, __LINE__, iInput, value).c_str();
111 return value / 1000.0;
125 FILE* fp = freopen(file,
"a+", stdout);
133 }
catch (std::exception& e) {
145 }
catch (std::exception& e) {
156 double val =
Props1SI(Output, FluidName);
165 return Props(Output, Name1[0], Prop1, Name2[0], Prop2, Ref);
171 std::string sName1 = std::string(1, Name1), sName2 = std::string(1, Name2);
183 double val =
PropsSI(Output, sName1.c_str(), Prop1, sName2.c_str(), Prop2, Ref);
187 }
catch (std::exception& e) {
198 }
catch (std::exception& e) {
210 const long length_fractions,
double* result,
long* resdim1) {
216 if (delim.length() > 1)
218 std::vector<std::string> _outputs =
strsplit(Outputs, delim[0]);
220 std::vector<std::string> _fluidNames =
strsplit(FluidNames, delim[0]);
221 if (_fluidNames.size() != length_fractions)
223 format(
"Length of fractions vector [%d] is not equal to length of fluidNames vector [%d]", _fluidNames.size(), length_fractions));
224 std::vector<double> _fractions(fractions, fractions + length_fractions);
225 std::vector<std::vector<double>> _result =
CoolProp::Props1SImulti(_outputs, backend, _fluidNames, _fractions);
227 if (_result.size() == 0) {
230 if (_result.size() > *resdim1)
232 *resdim1 = _result[0].size();
233 for (
int i = 0; i < _result[0].size(); i++) {
234 result[i] = _result[0][i];
237 }
catch (std::exception& e) {
245 return CoolProp::PropsSI(std::string(Output), std::string(Name1), Prop1, std::string(Name2), Prop2, std::string(FluidName));
248 double* Prop2,
const long size_Prop2,
char* backend,
const char* FluidNames,
const double* fractions,
249 const long length_fractions,
double* result,
long* resdim1,
long* resdim2) {
255 if (delim.length() > 1)
257 std::vector<std::string> _outputs =
strsplit(Outputs, delim[0]);
258 if (size_Prop1 != size_Prop2)
260 format(
"Length of input parameter 1 [%d] is not equal to length of input parameter 2 [%d]", size_Prop1, size_Prop2));
262 std::vector<double> _prop1(Prop1, Prop1 + size_Prop1);
263 std::vector<double> _prop2(Prop2, Prop2 + size_Prop2);
265 std::vector<std::string> _fluidNames =
strsplit(FluidNames, delim[0]);
266 if (_fluidNames.size() != length_fractions)
268 format(
"Length of fractions vector [%d] is not equal to length of fluidNames vector [%d]", _fluidNames.size(), length_fractions));
269 std::vector<double> _fractions(fractions, fractions + length_fractions);
270 std::vector<std::vector<double>> _result =
271 CoolProp::PropsSImulti(_outputs, std::string(Name1), _prop1, std::string(Name2), _prop2, backend, _fluidNames, _fractions);
273 if (_result.size() == 0) {
277 if (_result.size() > *resdim1 || _result[0].size() > *resdim2)
279 format(
"Result matrix [%d x %d] is bigger than allocated memory [%d x %d]", _result.size(), _result[0].size(), *resdim1, *resdim2));
280 *resdim1 = _result.size();
281 *resdim2 = _result[0].size();
282 for (
int i = 0; i < _result.size(); i++) {
283 for (
int j = 0; j < _result[i].size(); j++) {
284 result[j + _result[i].size() * i] = _result[i][j];
288 }
catch (std::exception& e) {
303 std::string s =
CoolProp::PhaseSI(std::string(Name1), Prop1, std::string(Name2), Prop2, std::string(FluidName));
306 }
catch (std::exception& e) {
323 const char* FluidName,
double* output) {
324 *output =
PropsSI(Output, Name1, *Prop1, Name2, *Prop2, FluidName);
328 return T * 9 / 5 - 459.67;
331 return (T_F + 459.67) * 5 / 9;
342 }
catch (std::exception& e) {
352 }
catch (std::exception& e) {
364 }
catch (std::exception& e) {
377 }
catch (std::exception& e) {
397 }
catch (std::exception& e) {
407 }
catch (std::exception& e) {
416 }
catch (std::exception& e) {
425 }
catch (std::exception& e) {
442 return HumidAir::HAPropsSI(std::string(Output), std::string(Name1), Prop1, std::string(Name2), Prop2, std::string(Name3), Prop3);
449 const char* Name3,
const double* Prop3,
double* output) {
450 *output =
HAPropsSI(Output, Name1, *Prop1, Name2, *Prop2, Name3, *Prop3);
456 return HumidAir::HAProps(std::string(Output), std::string(Name1), Prop1, std::string(Name2), Prop2, std::string(Name3), Prop3);
457 }
catch (std::exception& e) {
465 const char* Name3,
const double* Prop3,
double* output) {
466 *output =
HAProps(Output, Name1, *Prop1, Name2, *Prop2, Name3, *Prop3);
471 std::map<std::size_t, shared_ptr<CoolProp::AbstractState>> ASlibrary;
476 long add(shared_ptr<CoolProp::AbstractState> AS) {
477 ASlibrary.insert(std::pair<std::size_t, shared_ptr<CoolProp::AbstractState>>(this->next_handle, AS));
479 return next_handle - 1;
482 std::size_t count_removed = ASlibrary.erase(handle);
483 if (count_removed != 1) {
487 shared_ptr<CoolProp::AbstractState>&
get(
long handle) {
488 std::map<std::size_t, shared_ptr<CoolProp::AbstractState>>::iterator it = ASlibrary.find(handle);
489 if (it != ASlibrary.end()) {
499 const long buffer_length) {
503 return handle_manager.
add(AS);
510 const long buffer_length) {
514 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
515 std::vector<std::string> _fluids = AS->fluid_names();
517 if (fluidsstring.size() <
static_cast<std::size_t
>(buffer_length)) {
518 strcpy(fluids, fluidsstring.c_str());
521 static_cast<std::size_t
>(buffer_length)));
531 handle_manager.
remove(handle);
537 const long buffer_length) {
539 std::vector<double> _fractions(fractions, fractions + N);
541 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
542 if (AS->using_mole_fractions()) {
543 AS->set_mole_fractions(_fractions);
544 }
else if (AS->using_mass_fractions()) {
545 AS->set_mass_fractions(_fractions);
546 }
else if (AS->using_volu_fractions()) {
547 AS->set_volu_fractions(_fractions);
554 char* message_buffer,
const long buffer_length) {
558 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
559 std::vector<double> _fractions = AS->get_mole_fractions();
560 *N = _fractions.size();
562 for (
int i = 0; i < *N; i++)
563 fractions[i] = _fractions[i];
572 const long maxN,
long* N,
long* errcode,
char* message_buffer,
573 const long buffer_length) {
577 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
578 std::vector<double> _fractions;
579 double quality = AS->Q();
580 std::string string_state(saturated_state);
581 if (0 <= quality && quality <= 1) {
582 if (string_state ==
"liquid") {
583 _fractions = AS->mole_fractions_liquid();
584 }
else if (string_state ==
"gas") {
585 _fractions = AS->mole_fractions_vapor();
588 format(
"Bad info string [%s] to saturated state mole fractions, options are \"liquid\" and \"gas\"", saturated_state));
591 throw CoolProp::ValueError(
format(
"AbstractState_get_mole_fractions_satState only returns outputs for saturated states if AbstractState "
592 "quality [%g] is within two-phase region (0 <= quality <= 1)",
593 static_cast<double>(quality)));
595 *N = _fractions.size();
597 for (
int i = 0; i < *N; i++) {
598 fractions[i] = _fractions[i];
608 const long buffer_length) {
611 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
612 return AS->fugacity(i);
619 const long buffer_length) {
622 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
623 return AS->fugacity_coefficient(i);
630 char* message_buffer,
const long buffer_length) {
633 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
640 const long buffer_length) {
643 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
652 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
653 return AS->unspecify_phase();
659 const long buffer_length) {
662 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
671 char* message_buffer,
const long buffer_length) {
674 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
683 char* message_buffer,
const long buffer_length) {
686 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
696 const long Wrt2,
const long Constant2,
long* errcode,
char* message_buffer,
697 const long buffer_length) {
700 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
711 const long Wrt2,
const long Constant2,
long* errcode,
char* message_buffer,
712 const long buffer_length) {
715 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
726 long* errcode,
char* message_buffer,
const long buffer_length) {
729 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
739 const double x_end,
long* errcode,
char* message_buffer,
740 const long buffer_length) {
743 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
754 const long length,
double* T,
double* p,
double* rhomolar,
double* hmolar,
755 double* smolar,
long* errcode,
char* message_buffer,
const long buffer_length) {
758 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
760 for (
int i = 0; i < length; i++) {
765 *(rhomolar + i) = AS->rhomolar();
766 *(hmolar + i) = AS->hmolar();
767 *(smolar + i) = AS->smolar();
777 const long length,
const long output,
double* out,
long* errcode,
char* message_buffer,
778 const long buffer_length) {
781 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
783 for (
int i = 0; i < length; i++) {
796 const long length,
long* outputs,
double* out1,
double* out2,
double* out3,
double* out4,
797 double* out5,
long* errcode,
char* message_buffer,
const long buffer_length) {
800 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
802 for (
int i = 0; i < length; i++) {
819 const double value,
long* errcode,
char* message_buffer,
820 const long buffer_length) {
823 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
824 AS->set_binary_interaction_double(
static_cast<std::size_t
>(i),
static_cast<std::size_t
>(j), parameter, value);
831 const double c3,
long* errcode,
char* message_buffer,
const long buffer_length) {
834 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
835 AS->set_cubic_alpha_C(
static_cast<std::size_t
>(i), parameter, c1, c2, c3);
842 long* errcode,
char* message_buffer,
const long buffer_length) {
845 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
846 AS->set_fluid_parameter_double(
static_cast<std::size_t
>(i), parameter, value);
853 const long buffer_length) {
856 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
857 AS->build_phase_envelope(level);
864 double* rhomolar_liq,
double* x,
double* y,
long* errcode,
char* message_buffer,
865 const long buffer_length) {
868 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
870 if (pe.T.size() >
static_cast<std::size_t
>(length)) {
872 static_cast<int>(pe.T.size()),
static_cast<int>(length)));
874 std::size_t N = pe.x.size();
875 for (std::size_t i = 0; i < pe.T.size(); i++) {
878 *(rhomolar_vap + i) = pe.rhomolar_vap[i];
879 *(rhomolar_liq + i) = pe.rhomolar_liq[i];
880 for (std::size_t j = 0; j < N; ++j) {
881 *(x + i * N + j) = pe.x[j][i];
882 *(y + i * N + j) = pe.y[j][i];
891 double* p,
double* rhomolar_vap,
double* rhomolar_liq,
double* x,
double* y,
892 long* actual_length,
long* actual_components,
long* errcode,
char* message_buffer,
893 const long buffer_length) {
896 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
898 *actual_length = pe.T.size();
899 if (pe.T.size() >
static_cast<std::size_t
>(length)) {
901 static_cast<int>(pe.T.size()),
static_cast<int>(length)));
903 *actual_components = pe.x.size();
904 if (*actual_components >
static_cast<std::size_t
>(maxComponents)) {
905 throw CoolProp::ValueError(
format(
"Length of phase envelope composition vectors [%d] is greater than allocated buffer length [%d]",
906 static_cast<int>(*actual_components),
static_cast<int>(maxComponents)));
908 for (std::size_t i = 0; i < pe.T.size(); i++) {
911 *(rhomolar_vap + i) = pe.rhomolar_vap[i];
912 *(rhomolar_liq + i) = pe.rhomolar_liq[i];
913 for (std::size_t j = 0; j < *actual_components; ++j) {
914 *(x + i * *actual_components + j) = pe.x[j][i];
915 *(y + i * *actual_components + j) = pe.y[j][i];
926 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
927 AS->build_spinodal();
934 long* errcode,
char* message_buffer,
const long buffer_length) {
937 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
939 if (spin.
tau.size() >
static_cast<std::size_t
>(length)) {
941 static_cast<int>(spin.
tau.size()),
static_cast<int>(length)));
943 for (std::size_t i = 0; i < spin.
tau.size(); ++i) {
944 *(tau + i) = spin.
tau[i];
945 *(delta + i) = spin.
delta[i];
946 *(M1 + i) = spin.
M1[i];
954 long* errcode,
char* message_buffer,
const long buffer_length) {
957 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
958 std::vector<CoolProp::CriticalState> pts = AS->all_critical_points();
959 if (pts.size() >
static_cast<std::size_t
>(length)) {
961 static_cast<int>(pts.size()),
static_cast<int>(length)));
963 for (std::size_t i = 0; i < pts.size(); ++i) {
966 *(rhomolar + i) = pts[i].rhomolar;
967 *(stable + i) = pts[i].stable;
975 char* message_buffer,
const long buffer_length) {
979 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
980 double quality = AS->Q();
981 std::string string_state(saturated_state);
982 if (0 <= quality && quality <= 1) {
983 if (string_state ==
"liquid") {
985 }
else if (string_state ==
"gas") {
989 format(
"Bad info string [%s] to saturated state output, options are \"liquid\" and \"gas\"", saturated_state));
992 throw CoolProp::ValueError(
format(
"AbstractState_keyed_output_satState only returns outputs for saturated states if AbstractState "
993 "quality [%g] is within two-phase region (0 <= quality <= 1)",
994 static_cast<double>(quality)));
1003 const long buffer_length) {
1007 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
1008 std::string backendstring = AS->backend_name();
1009 if (backendstring.size() <
static_cast<std::size_t
>(buffer_length)) {
1010 strcpy(backend, backendstring.c_str());
1012 throw CoolProp::ValueError(
format(
"Length of string [%d] is greater than allocated buffer length [%d]", backendstring.size(),
1013 static_cast<std::size_t
>(buffer_length)));
1023 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
1032 const long return_buffer_length,
long* errcode,
char* message_buffer,
1033 const long buffer_length) {
1036 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
1037 std::string temp = AS->fluid_param_string(param);
1038 if (temp.size() <
static_cast<std::size_t
>(return_buffer_length)) {
1039 strcpy(return_buffer, temp.c_str());
1049 const long buffer_length) {
1052 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
1061 const long buffer_length) {
1064 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
1073 const long buffer_length) {
1088 const long fluid_length) {
1089 std::string _fluid, _backend;
1091 if (_backend.size() <
static_cast<std::size_t
>(backend_length)) {
1092 strcpy(backend, _backend.c_str());
1096 if (_fluid.size() <
static_cast<std::size_t
>(fluid_length)) {
1097 strcpy(fluid, _fluid.c_str());