2# define _CRTDBG_MAP_ALLOC
3# define _CRT_SECURE_NO_WARNINGS
24void str2buf(
const std::string& str,
char* buf,
int n) {
25 if (str.size() <
static_cast<unsigned int>(n))
30void HandleException(
long* errcode,
char* message_buffer,
const long buffer_length) {
34 std::string errmsg = std::string(
"HandleError: ") + e.
what();
35 if (errmsg.size() <
static_cast<std::size_t
>(buffer_length)) {
37 strcpy(message_buffer, errmsg.c_str());
42 std::string errmsg = std::string(
"Error: ") + e.
what();
43 if (errmsg.size() <
static_cast<std::size_t
>(buffer_length)) {
45 strcpy(message_buffer, errmsg.c_str());
63#elif defined(FE_ALL_EXCEPT)
64 feclearexcept(FE_ALL_EXCEPT);
70 std::cout <<
format(
"%s:%d: convert_from_kSI_to_SI(i=%d,value=%g)\n", __FILE__, __LINE__, iInput, value).c_str();
83 return value * 1000.0;
99 std::cout <<
format(
"%s:%d: convert_from_SI_to_kSI(%d,%g)\n", __FILE__, __LINE__, iInput, value).c_str();
112 return value / 1000.0;
126 FILE* fp = freopen(file,
"a+", stdout);
134 }
catch (std::exception& e) {
146 }
catch (std::exception& e) {
157 double val =
Props1SI(Output, FluidName);
166 return Props(Output, Name1[0], Prop1, Name2[0], Prop2, Ref);
172 std::string sName1 = std::string(1, Name1), sName2 = std::string(1, Name2);
184 double val =
PropsSI(Output, sName1.c_str(), Prop1, sName2.c_str(), Prop2, Ref);
188 }
catch (std::exception& e) {
199 }
catch (std::exception& e) {
211 const long length_fractions,
double* result,
long* resdim1) {
217 if (delim.length() > 1)
219 std::vector<std::string> _outputs =
strsplit(Outputs, delim[0]);
221 std::vector<std::string> _fluidNames =
strsplit(FluidNames, delim[0]);
222 if (_fluidNames.size() != length_fractions)
224 format(
"Length of fractions vector [%d] is not equal to length of fluidNames vector [%d]", _fluidNames.size(), length_fractions));
225 std::vector<double> _fractions(fractions, fractions + length_fractions);
226 std::vector<std::vector<double>> _result =
CoolProp::Props1SImulti(_outputs, backend, _fluidNames, _fractions);
228 if (_result.size() == 0) {
231 if (_result.size() > *resdim1)
233 *resdim1 = _result[0].size();
234 for (
int i = 0; i < _result[0].size(); i++) {
235 result[i] = _result[0][i];
238 }
catch (std::exception& e) {
246 return CoolProp::PropsSI(std::string(Output), std::string(Name1), Prop1, std::string(Name2), Prop2, std::string(FluidName));
249 double* Prop2,
const long size_Prop2,
char* backend,
const char* FluidNames,
const double* fractions,
250 const long length_fractions,
double* result,
long* resdim1,
long* resdim2) {
256 if (delim.length() > 1)
258 std::vector<std::string> _outputs =
strsplit(Outputs, delim[0]);
259 if (size_Prop1 != size_Prop2)
261 format(
"Length of input parameter 1 [%d] is not equal to length of input parameter 2 [%d]", size_Prop1, size_Prop2));
263 std::vector<double> _prop1(Prop1, Prop1 + size_Prop1);
264 std::vector<double> _prop2(Prop2, Prop2 + size_Prop2);
266 std::vector<std::string> _fluidNames =
strsplit(FluidNames, delim[0]);
267 if (_fluidNames.size() != length_fractions)
269 format(
"Length of fractions vector [%d] is not equal to length of fluidNames vector [%d]", _fluidNames.size(), length_fractions));
270 std::vector<double> _fractions(fractions, fractions + length_fractions);
271 std::vector<std::vector<double>> _result =
272 CoolProp::PropsSImulti(_outputs, std::string(Name1), _prop1, std::string(Name2), _prop2, backend, _fluidNames, _fractions);
274 if (_result.size() == 0) {
278 if (_result.size() > *resdim1 || _result[0].size() > *resdim2)
280 format(
"Result matrix [%d x %d] is bigger than allocated memory [%d x %d]", _result.size(), _result[0].size(), *resdim1, *resdim2));
281 *resdim1 = _result.size();
282 *resdim2 = _result[0].size();
283 for (
int i = 0; i < _result.size(); i++) {
284 for (
int j = 0; j < _result[i].size(); j++) {
285 result[j + _result[i].size() * i] = _result[i][j];
289 }
catch (std::exception& e) {
304 std::string s =
CoolProp::PhaseSI(std::string(Name1), Prop1, std::string(Name2), Prop2, std::string(FluidName));
307 }
catch (std::exception& e) {
324 const char* FluidName,
double* output) {
325 *output =
PropsSI(Output, Name1, *Prop1, Name2, *Prop2, FluidName);
329 return T * 9 / 5 - 459.67;
332 return (T_F + 459.67) * 5 / 9;
343 }
catch (std::exception& e) {
353 }
catch (std::exception& e) {
365 }
catch (std::exception& e) {
378 }
catch (std::exception& e) {
398 }
catch (std::exception& e) {
409 }
catch (std::exception& e) {
419 }
catch (std::exception& e) {
428 }
catch (std::exception& e) {
437 }
catch (std::exception& e) {
454 return HumidAir::HAPropsSI(std::string(Output), std::string(Name1), Prop1, std::string(Name2), Prop2, std::string(Name3), Prop3);
461 const char* Name3,
const double* Prop3,
double* output) {
462 *output =
HAPropsSI(Output, Name1, *Prop1, Name2, *Prop2, Name3, *Prop3);
468 return HumidAir::HAProps(std::string(Output), std::string(Name1), Prop1, std::string(Name2), Prop2, std::string(Name3), Prop3);
469 }
catch (std::exception& e) {
477 const char* Name3,
const double* Prop3,
double* output) {
478 *output =
HAProps(Output, Name1, *Prop1, Name2, *Prop2, Name3, *Prop3);
483 std::map<std::size_t, shared_ptr<CoolProp::AbstractState>> ASlibrary;
485 std::mutex ASLib_mutex;
489 long add(shared_ptr<CoolProp::AbstractState> AS) {
490 std::lock_guard<std::mutex> guard(ASLib_mutex);
491 ASlibrary.insert(std::pair<std::size_t, shared_ptr<CoolProp::AbstractState>>(this->next_handle, AS));
493 return next_handle - 1;
496 std::lock_guard<std::mutex> guard(ASLib_mutex);
497 std::size_t count_removed = ASlibrary.erase(handle);
498 if (count_removed != 1) {
502 shared_ptr<CoolProp::AbstractState>&
get(
long handle) {
503 std::lock_guard<std::mutex> guard(ASLib_mutex);
504 std::map<std::size_t, shared_ptr<CoolProp::AbstractState>>::iterator it = ASlibrary.find(handle);
505 if (it != ASlibrary.end()) {
515 const long buffer_length) {
519 return handle_manager.
add(AS);
526 const long buffer_length) {
530 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
531 std::vector<std::string> _fluids = AS->fluid_names();
533 if (fluidsstring.size() <
static_cast<std::size_t
>(buffer_length)) {
534 strcpy(fluids, fluidsstring.c_str());
537 static_cast<std::size_t
>(buffer_length)));
547 handle_manager.
remove(handle);
553 const long buffer_length) {
555 std::vector<double> _fractions(fractions, fractions + N);
557 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
558 if (AS->using_mole_fractions()) {
559 AS->set_mole_fractions(_fractions);
560 }
else if (AS->using_mass_fractions()) {
561 AS->set_mass_fractions(_fractions);
562 }
else if (AS->using_volu_fractions()) {
563 AS->set_volu_fractions(_fractions);
570 char* message_buffer,
const long buffer_length) {
574 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
575 std::vector<double> _fractions = AS->get_mole_fractions();
576 *N = _fractions.size();
578 for (
int i = 0; i < *N; i++)
579 fractions[i] = _fractions[i];
588 const long maxN,
long* N,
long* errcode,
char* message_buffer,
589 const long buffer_length) {
593 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
594 std::vector<double> _fractions;
595 double quality = AS->Q();
596 std::string string_state(saturated_state);
597 if (0 <= quality && quality <= 1) {
598 if (string_state ==
"liquid") {
599 _fractions = AS->mole_fractions_liquid();
600 }
else if (string_state ==
"gas") {
601 _fractions = AS->mole_fractions_vapor();
604 format(
"Bad info string [%s] to saturated state mole fractions, options are \"liquid\" and \"gas\"", saturated_state));
607 throw CoolProp::ValueError(
format(
"AbstractState_get_mole_fractions_satState only returns outputs for saturated states if AbstractState "
608 "quality [%g] is within two-phase region (0 <= quality <= 1)",
609 static_cast<double>(quality)));
611 *N = _fractions.size();
613 for (
int i = 0; i < *N; i++) {
614 fractions[i] = _fractions[i];
624 const long buffer_length) {
627 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
628 return AS->fugacity(i);
635 const long buffer_length) {
638 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
639 return AS->fugacity_coefficient(i);
646 char* message_buffer,
const long buffer_length) {
649 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
656 const long buffer_length) {
659 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
668 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
669 return AS->unspecify_phase();
675 const long buffer_length) {
678 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
687 char* message_buffer,
const long buffer_length) {
690 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
699 char* message_buffer,
const long buffer_length) {
702 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
712 const long Wrt2,
const long Constant2,
long* errcode,
char* message_buffer,
713 const long buffer_length) {
716 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
727 const long Wrt2,
const long Constant2,
long* errcode,
char* message_buffer,
728 const long buffer_length) {
731 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
742 long* errcode,
char* message_buffer,
const long buffer_length) {
745 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
755 const double x_end,
long* errcode,
char* message_buffer,
756 const long buffer_length) {
759 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
770 const long length,
double* T,
double* p,
double* rhomolar,
double* hmolar,
771 double* smolar,
long* errcode,
char* message_buffer,
const long buffer_length) {
774 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
776 for (
int i = 0; i < length; i++) {
781 *(rhomolar + i) = AS->rhomolar();
782 *(hmolar + i) = AS->hmolar();
783 *(smolar + i) = AS->smolar();
793 const long length,
const long output,
double* out,
long* errcode,
char* message_buffer,
794 const long buffer_length) {
797 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
799 for (
int i = 0; i < length; i++) {
812 const long length,
long* outputs,
double* out1,
double* out2,
double* out3,
double* out4,
813 double* out5,
long* errcode,
char* message_buffer,
const long buffer_length) {
816 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
818 for (
int i = 0; i < length; i++) {
835 const double value,
long* errcode,
char* message_buffer,
836 const long buffer_length) {
839 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
840 AS->set_binary_interaction_double(
static_cast<std::size_t
>(i),
static_cast<std::size_t
>(j), parameter, value);
847 const double c3,
long* errcode,
char* message_buffer,
const long buffer_length) {
850 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
851 AS->set_cubic_alpha_C(
static_cast<std::size_t
>(i), parameter, c1, c2, c3);
858 long* errcode,
char* message_buffer,
const long buffer_length) {
861 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
862 AS->set_fluid_parameter_double(
static_cast<std::size_t
>(i), parameter, value);
869 const long buffer_length) {
872 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
873 AS->build_phase_envelope(level);
880 double* rhomolar_liq,
double* x,
double* y,
long* errcode,
char* message_buffer,
881 const long buffer_length) {
884 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
886 if (pe.T.size() >
static_cast<std::size_t
>(length)) {
888 static_cast<int>(pe.T.size()),
static_cast<int>(length)));
890 std::size_t N = pe.x.size();
891 for (std::size_t i = 0; i < pe.T.size(); i++) {
894 *(rhomolar_vap + i) = pe.rhomolar_vap[i];
895 *(rhomolar_liq + i) = pe.rhomolar_liq[i];
896 for (std::size_t j = 0; j < N; ++j) {
897 *(x + i * N + j) = pe.x[j][i];
898 *(y + i * N + j) = pe.y[j][i];
907 double* p,
double* rhomolar_vap,
double* rhomolar_liq,
double* x,
double* y,
908 long* actual_length,
long* actual_components,
long* errcode,
char* message_buffer,
909 const long buffer_length) {
912 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
914 *actual_length = pe.T.size();
915 if (pe.T.size() >
static_cast<std::size_t
>(length)) {
917 static_cast<int>(pe.T.size()),
static_cast<int>(length)));
919 *actual_components = pe.x.size();
920 if (*actual_components >
static_cast<std::size_t
>(maxComponents)) {
921 throw CoolProp::ValueError(
format(
"Length of phase envelope composition vectors [%d] is greater than allocated buffer length [%d]",
922 static_cast<int>(*actual_components),
static_cast<int>(maxComponents)));
924 for (std::size_t i = 0; i < pe.T.size(); i++) {
927 *(rhomolar_vap + i) = pe.rhomolar_vap[i];
928 *(rhomolar_liq + i) = pe.rhomolar_liq[i];
929 for (std::size_t j = 0; j < *actual_components; ++j) {
930 *(x + i * *actual_components + j) = pe.x[j][i];
931 *(y + i * *actual_components + j) = pe.y[j][i];
942 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
943 AS->build_spinodal();
950 long* errcode,
char* message_buffer,
const long buffer_length) {
953 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
955 if (spin.
tau.size() >
static_cast<std::size_t
>(length)) {
957 static_cast<int>(spin.
tau.size()),
static_cast<int>(length)));
959 for (std::size_t i = 0; i < spin.
tau.size(); ++i) {
960 *(tau + i) = spin.
tau[i];
961 *(delta + i) = spin.
delta[i];
962 *(M1 + i) = spin.
M1[i];
970 long* errcode,
char* message_buffer,
const long buffer_length) {
973 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
974 std::vector<CoolProp::CriticalState> pts = AS->all_critical_points();
975 if (pts.size() >
static_cast<std::size_t
>(length)) {
977 static_cast<int>(pts.size()),
static_cast<int>(length)));
979 for (std::size_t i = 0; i < pts.size(); ++i) {
982 *(rhomolar + i) = pts[i].rhomolar;
983 *(stable + i) = pts[i].stable;
991 char* message_buffer,
const long buffer_length) {
995 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
996 double quality = AS->Q();
997 std::string string_state(saturated_state);
998 if (0 <= quality && quality <= 1) {
999 if (string_state ==
"liquid") {
1001 }
else if (string_state ==
"gas") {
1005 format(
"Bad info string [%s] to saturated state output, options are \"liquid\" and \"gas\"", saturated_state));
1008 throw CoolProp::ValueError(
format(
"AbstractState_keyed_output_satState only returns outputs for saturated states if AbstractState "
1009 "quality [%g] is within two-phase region (0 <= quality <= 1)",
1010 static_cast<double>(quality)));
1019 const long buffer_length) {
1023 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
1024 std::string backendstring = AS->backend_name();
1025 if (backendstring.size() <
static_cast<std::size_t
>(buffer_length)) {
1026 strcpy(backend, backendstring.c_str());
1028 throw CoolProp::ValueError(
format(
"Length of string [%d] is greater than allocated buffer length [%d]", backendstring.size(),
1029 static_cast<std::size_t
>(buffer_length)));
1039 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
1048 const long return_buffer_length,
long* errcode,
char* message_buffer,
1049 const long buffer_length) {
1052 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
1053 std::string temp = AS->fluid_param_string(param);
1054 if (temp.size() <
static_cast<std::size_t
>(return_buffer_length)) {
1055 strcpy(return_buffer, temp.c_str());
1065 const long buffer_length) {
1068 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
1077 const long buffer_length) {
1080 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
1089 const long buffer_length) {
1104 const long fluid_length) {
1105 std::string _fluid, _backend;
1107 if (_backend.size() <
static_cast<std::size_t
>(backend_length)) {
1108 strcpy(backend, _backend.c_str());
1112 if (_fluid.size() <
static_cast<std::size_t
>(fluid_length)) {
1113 strcpy(fluid, _fluid.c_str());