2 # define _CRTDBG_MAP_ALLOC
3 # define _CRT_SECURE_NO_WARNINGS
24 void str2buf(
const std::string& str,
char* buf,
int n) {
25 if (str.size() <
static_cast<unsigned int>(n))
30 void 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) {
408 }
catch (std::exception& e) {
417 }
catch (std::exception& e) {
426 }
catch (std::exception& e) {
443 return HumidAir::HAPropsSI(std::string(Output), std::string(Name1), Prop1, std::string(Name2), Prop2, std::string(Name3), Prop3);
450 const char* Name3,
const double* Prop3,
double* output) {
451 *output =
HAPropsSI(Output, Name1, *Prop1, Name2, *Prop2, Name3, *Prop3);
457 return HumidAir::HAProps(std::string(Output), std::string(Name1), Prop1, std::string(Name2), Prop2, std::string(Name3), Prop3);
458 }
catch (std::exception& e) {
466 const char* Name3,
const double* Prop3,
double* output) {
467 *output =
HAProps(Output, Name1, *Prop1, Name2, *Prop2, Name3, *Prop3);
472 std::map<std::size_t, shared_ptr<CoolProp::AbstractState>> ASlibrary;
474 std::mutex ASLib_mutex;
478 long add(shared_ptr<CoolProp::AbstractState> AS) {
479 std::lock_guard<std::mutex> guard(ASLib_mutex);
480 ASlibrary.insert(std::pair<std::size_t, shared_ptr<CoolProp::AbstractState>>(this->next_handle, AS));
482 return next_handle - 1;
485 std::lock_guard<std::mutex> guard(ASLib_mutex);
486 std::size_t count_removed = ASlibrary.erase(handle);
487 if (count_removed != 1) {
491 shared_ptr<CoolProp::AbstractState>&
get(
long handle) {
492 std::lock_guard<std::mutex> guard(ASLib_mutex);
493 std::map<std::size_t, shared_ptr<CoolProp::AbstractState>>::iterator it = ASlibrary.find(handle);
494 if (it != ASlibrary.end()) {
504 const long buffer_length) {
508 return handle_manager.
add(AS);
515 const long buffer_length) {
519 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
520 std::vector<std::string> _fluids = AS->fluid_names();
522 if (fluidsstring.size() <
static_cast<std::size_t
>(buffer_length)) {
523 strcpy(fluids, fluidsstring.c_str());
526 static_cast<std::size_t
>(buffer_length)));
536 handle_manager.
remove(handle);
542 const long buffer_length) {
544 std::vector<double> _fractions(fractions, fractions + N);
546 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
547 if (AS->using_mole_fractions()) {
548 AS->set_mole_fractions(_fractions);
549 }
else if (AS->using_mass_fractions()) {
550 AS->set_mass_fractions(_fractions);
551 }
else if (AS->using_volu_fractions()) {
552 AS->set_volu_fractions(_fractions);
559 char* message_buffer,
const long buffer_length) {
563 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
564 std::vector<double> _fractions = AS->get_mole_fractions();
565 *N = _fractions.size();
567 for (
int i = 0; i < *N; i++)
568 fractions[i] = _fractions[i];
577 const long maxN,
long* N,
long* errcode,
char* message_buffer,
578 const long buffer_length) {
582 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
583 std::vector<double> _fractions;
584 double quality = AS->Q();
585 std::string string_state(saturated_state);
586 if (0 <= quality && quality <= 1) {
587 if (string_state ==
"liquid") {
588 _fractions = AS->mole_fractions_liquid();
589 }
else if (string_state ==
"gas") {
590 _fractions = AS->mole_fractions_vapor();
593 format(
"Bad info string [%s] to saturated state mole fractions, options are \"liquid\" and \"gas\"", saturated_state));
596 throw CoolProp::ValueError(
format(
"AbstractState_get_mole_fractions_satState only returns outputs for saturated states if AbstractState "
597 "quality [%g] is within two-phase region (0 <= quality <= 1)",
598 static_cast<double>(quality)));
600 *N = _fractions.size();
602 for (
int i = 0; i < *N; i++) {
603 fractions[i] = _fractions[i];
613 const long buffer_length) {
616 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
617 return AS->fugacity(i);
624 const long buffer_length) {
627 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
628 return AS->fugacity_coefficient(i);
635 char* message_buffer,
const long buffer_length) {
638 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
645 const long buffer_length) {
648 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
657 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
658 return AS->unspecify_phase();
664 const long buffer_length) {
667 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
676 char* message_buffer,
const long buffer_length) {
679 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
688 char* message_buffer,
const long buffer_length) {
691 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
701 const long Wrt2,
const long Constant2,
long* errcode,
char* message_buffer,
702 const long buffer_length) {
705 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
716 const long Wrt2,
const long Constant2,
long* errcode,
char* message_buffer,
717 const long buffer_length) {
720 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
731 long* errcode,
char* message_buffer,
const long buffer_length) {
734 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
744 const double x_end,
long* errcode,
char* message_buffer,
745 const long buffer_length) {
748 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
759 const long length,
double* T,
double* p,
double* rhomolar,
double* hmolar,
760 double* smolar,
long* errcode,
char* message_buffer,
const long buffer_length) {
763 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
765 for (
int i = 0; i < length; i++) {
770 *(rhomolar + i) = AS->rhomolar();
771 *(hmolar + i) = AS->hmolar();
772 *(smolar + i) = AS->smolar();
782 const long length,
const long output,
double* out,
long* errcode,
char* message_buffer,
783 const long buffer_length) {
786 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
788 for (
int i = 0; i < length; i++) {
801 const long length,
long* outputs,
double* out1,
double* out2,
double* out3,
double* out4,
802 double* out5,
long* errcode,
char* message_buffer,
const long buffer_length) {
805 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
807 for (
int i = 0; i < length; i++) {
824 const double value,
long* errcode,
char* message_buffer,
825 const long buffer_length) {
828 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
829 AS->set_binary_interaction_double(
static_cast<std::size_t
>(i),
static_cast<std::size_t
>(j), parameter, value);
836 const double c3,
long* errcode,
char* message_buffer,
const long buffer_length) {
839 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
840 AS->set_cubic_alpha_C(
static_cast<std::size_t
>(i), parameter, c1, c2, c3);
847 long* errcode,
char* message_buffer,
const long buffer_length) {
850 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
851 AS->set_fluid_parameter_double(
static_cast<std::size_t
>(i), parameter, value);
858 const long buffer_length) {
861 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
862 AS->build_phase_envelope(level);
869 double* rhomolar_liq,
double* x,
double* y,
long* errcode,
char* message_buffer,
870 const long buffer_length) {
873 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
875 if (pe.T.size() >
static_cast<std::size_t
>(length)) {
877 static_cast<int>(pe.T.size()),
static_cast<int>(length)));
879 std::size_t N = pe.x.size();
880 for (std::size_t i = 0; i < pe.T.size(); i++) {
883 *(rhomolar_vap + i) = pe.rhomolar_vap[i];
884 *(rhomolar_liq + i) = pe.rhomolar_liq[i];
885 for (std::size_t j = 0; j < N; ++j) {
886 *(x + i * N + j) = pe.x[j][i];
887 *(y + i * N + j) = pe.y[j][i];
896 double* p,
double* rhomolar_vap,
double* rhomolar_liq,
double* x,
double* y,
897 long* actual_length,
long* actual_components,
long* errcode,
char* message_buffer,
898 const long buffer_length) {
901 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
903 *actual_length = pe.T.size();
904 if (pe.T.size() >
static_cast<std::size_t
>(length)) {
906 static_cast<int>(pe.T.size()),
static_cast<int>(length)));
908 *actual_components = pe.x.size();
909 if (*actual_components >
static_cast<std::size_t
>(maxComponents)) {
910 throw CoolProp::ValueError(
format(
"Length of phase envelope composition vectors [%d] is greater than allocated buffer length [%d]",
911 static_cast<int>(*actual_components),
static_cast<int>(maxComponents)));
913 for (std::size_t i = 0; i < pe.T.size(); i++) {
916 *(rhomolar_vap + i) = pe.rhomolar_vap[i];
917 *(rhomolar_liq + i) = pe.rhomolar_liq[i];
918 for (std::size_t j = 0; j < *actual_components; ++j) {
919 *(x + i * *actual_components + j) = pe.x[j][i];
920 *(y + i * *actual_components + j) = pe.y[j][i];
931 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
932 AS->build_spinodal();
939 long* errcode,
char* message_buffer,
const long buffer_length) {
942 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
944 if (spin.
tau.size() >
static_cast<std::size_t
>(length)) {
946 static_cast<int>(spin.
tau.size()),
static_cast<int>(length)));
948 for (std::size_t i = 0; i < spin.
tau.size(); ++i) {
949 *(tau + i) = spin.
tau[i];
950 *(delta + i) = spin.
delta[i];
951 *(M1 + i) = spin.
M1[i];
959 long* errcode,
char* message_buffer,
const long buffer_length) {
962 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
963 std::vector<CoolProp::CriticalState> pts = AS->all_critical_points();
964 if (pts.size() >
static_cast<std::size_t
>(length)) {
966 static_cast<int>(pts.size()),
static_cast<int>(length)));
968 for (std::size_t i = 0; i < pts.size(); ++i) {
971 *(rhomolar + i) = pts[i].rhomolar;
972 *(stable + i) = pts[i].stable;
980 char* message_buffer,
const long buffer_length) {
984 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
985 double quality = AS->Q();
986 std::string string_state(saturated_state);
987 if (0 <= quality && quality <= 1) {
988 if (string_state ==
"liquid") {
990 }
else if (string_state ==
"gas") {
994 format(
"Bad info string [%s] to saturated state output, options are \"liquid\" and \"gas\"", saturated_state));
997 throw CoolProp::ValueError(
format(
"AbstractState_keyed_output_satState only returns outputs for saturated states if AbstractState "
998 "quality [%g] is within two-phase region (0 <= quality <= 1)",
999 static_cast<double>(quality)));
1008 const long buffer_length) {
1012 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
1013 std::string backendstring = AS->backend_name();
1014 if (backendstring.size() <
static_cast<std::size_t
>(buffer_length)) {
1015 strcpy(backend, backendstring.c_str());
1017 throw CoolProp::ValueError(
format(
"Length of string [%d] is greater than allocated buffer length [%d]", backendstring.size(),
1018 static_cast<std::size_t
>(buffer_length)));
1028 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
1037 const long return_buffer_length,
long* errcode,
char* message_buffer,
1038 const long buffer_length) {
1041 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
1042 std::string temp = AS->fluid_param_string(param);
1043 if (temp.size() <
static_cast<std::size_t
>(return_buffer_length)) {
1044 strcpy(return_buffer, temp.c_str());
1054 const long buffer_length) {
1057 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
1066 const long buffer_length) {
1069 shared_ptr<CoolProp::AbstractState>& AS = handle_manager.
get(handle);
1078 const long buffer_length) {
1093 const long fluid_length) {
1094 std::string _fluid, _backend;
1096 if (_backend.size() <
static_cast<std::size_t
>(backend_length)) {
1097 strcpy(backend, _backend.c_str());
1101 if (_fluid.size() <
static_cast<std::size_t
>(fluid_length)) {
1102 strcpy(fluid, _fluid.c_str());