forked from CoolProp/CoolProp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoolPropLib.cpp
More file actions
1107 lines (1054 loc) · 50.3 KB
/
Copy pathCoolPropLib.cpp
File metadata and controls
1107 lines (1054 loc) · 50.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#if defined(_MSC_VER)
# define _CRTDBG_MAP_ALLOC
# define _CRT_SECURE_NO_WARNINGS
# include <crtdbg.h>
#else
# include <fenv.h>
#endif
#include "CoolPropLib.h"
#include "CoolProp.h"
#include "HumidAirProp.h"
#include "DataStructures.h"
#include "Exceptions.h"
#include "float.h"
#include "crossplatform_shared_ptr.h"
#include "AbstractState.h"
#include "Exceptions.h"
#include "Configuration.h"
#include "Backends/Helmholtz/MixtureParameters.h"
#include <string.h>
#include <mutex>
void str2buf(const std::string& str, char* buf, int n) {
if (str.size() < static_cast<unsigned int>(n))
strcpy(buf, str.c_str());
else
throw CoolProp::ValueError("Buffer size is too small");
}
void HandleException(long* errcode, char* message_buffer, const long buffer_length) {
try {
throw; // Rethrow the error, and here we handle the error
} catch (CoolProp::HandleError& e) {
std::string errmsg = std::string("HandleError: ") + e.what();
if (errmsg.size() < static_cast<std::size_t>(buffer_length)) {
*errcode = 1;
strcpy(message_buffer, errmsg.c_str());
} else {
*errcode = 2;
}
} catch (CoolProp::CoolPropBaseError& e) {
std::string errmsg = std::string("Error: ") + e.what();
if (errmsg.size() < static_cast<std::size_t>(buffer_length)) {
*errcode = 1;
strcpy(message_buffer, errmsg.c_str());
} else {
*errcode = 2;
}
} catch (...) {
*errcode = 3;
}
}
// In Microsoft Excel, they seem to check the FPU exception bits and error out because of it.
// By calling the _clearfp(), we can reset these bits, and not get the error
// See also http://stackoverflow.com/questions/11685441/floating-point-error-when-calling-dll-function-from-vba/27336496#27336496
// See also http://stackoverflow.com/questions/16849009/in-linux-do-there-exist-functions-similar-to-clearfp-and-statusfp for linux and OSX
struct fpu_reset_guard
{
~fpu_reset_guard() {
#if defined(_MSC_VER)
_clearfp(); // For MSVC, clear the floating point error flags
#elif defined(FE_ALL_EXCEPT)
feclearexcept(FE_ALL_EXCEPT);
#endif
}
};
double convert_from_kSI_to_SI(long iInput, double value) {
if (get_debug_level() > 8) {
std::cout << format("%s:%d: convert_from_kSI_to_SI(i=%d,value=%g)\n", __FILE__, __LINE__, iInput, value).c_str();
}
switch (iInput) {
case CoolProp::iP:
case CoolProp::iCpmass:
case CoolProp::iCp0mass:
case CoolProp::iSmass:
case CoolProp::iGmass:
case CoolProp::iCvmass:
case CoolProp::iHmass:
case CoolProp::iUmass:
case CoolProp::iconductivity:
return value * 1000.0;
case CoolProp::iDmass:
case CoolProp::ispeed_sound:
case CoolProp::iQ:
case CoolProp::iviscosity:
case CoolProp::iT:
case CoolProp::iPrandtl:
case CoolProp::isurface_tension:
return value;
default:
throw CoolProp::ValueError(format("index [%d] is invalid in convert_from_kSI_to_SI", iInput).c_str());
}
}
double convert_from_SI_to_kSI(long iInput, double value) {
if (get_debug_level() > 8) {
std::cout << format("%s:%d: convert_from_SI_to_kSI(%d,%g)\n", __FILE__, __LINE__, iInput, value).c_str();
}
switch (iInput) {
case CoolProp::iP:
case CoolProp::iCpmass:
case CoolProp::iCp0mass:
case CoolProp::iSmass:
case CoolProp::iGmass:
case CoolProp::iCvmass:
case CoolProp::iHmass:
case CoolProp::iUmass:
case CoolProp::iconductivity:
return value / 1000.0;
case CoolProp::iDmass:
case CoolProp::iQ:
case CoolProp::ispeed_sound:
case CoolProp::iviscosity:
case CoolProp::iT:
case CoolProp::isurface_tension:
return value;
default:
throw CoolProp::ValueError(format("index [%d] is invalid in convert_from_SI_to_kSI", iInput).c_str());
}
}
EXPORT_CODE long CONVENTION redirect_stdout(const char* file) {
FILE* fp = freopen(file, "a+", stdout);
return (fp) ? 1 : 0; // 0 = failure if redirection could not be done; original stdout is already closed
}
EXPORT_CODE int CONVENTION set_reference_stateS(const char* Ref, const char* reference_state) {
fpu_reset_guard guard;
try {
CoolProp::set_reference_stateS(std::string(Ref), std::string(reference_state));
return true;
} catch (std::exception& e) {
CoolProp::set_error_string(e.what());
} catch (...) {
CoolProp::set_error_string("Undefined error");
}
return false;
}
EXPORT_CODE int CONVENTION set_reference_stateD(const char* Ref, double T, double rhomolar, double hmolar0, double smolar0) {
fpu_reset_guard guard;
try {
CoolProp::set_reference_stateD(std::string(Ref), T, rhomolar, hmolar0, smolar0);
return true;
} catch (std::exception& e) {
CoolProp::set_error_string(e.what());
} catch (...) {
CoolProp::set_error_string("Undefined error");
}
return false;
}
// All the function interfaces that point to the single-input Props function
EXPORT_CODE double CONVENTION Props1(const char* FluidName, const char* Output) {
fpu_reset_guard guard;
double val = Props1SI(Output, FluidName);
if (!ValidNumber(val))
// Error code was already set in Props1SI
return val;
// val is valid; so, Output is already checked in Props1SI -> get_parameter_index won't throw
CoolProp::parameters iOutput = CoolProp::get_parameter_index(Output);
return convert_from_SI_to_kSI(iOutput, val);
}
EXPORT_CODE double CONVENTION PropsS(const char* Output, const char* Name1, double Prop1, const char* Name2, double Prop2, const char* Ref) {
return Props(Output, Name1[0], Prop1, Name2[0], Prop2, Ref);
}
EXPORT_CODE double CONVENTION Props(const char* Output, const char Name1, double Prop1, const char Name2, double Prop2, const char* Ref) {
fpu_reset_guard guard;
try {
// Get parameter indices
std::string sName1 = std::string(1, Name1), sName2 = std::string(1, Name2);
CoolProp::parameters iOutput = CoolProp::get_parameter_index(Output);
if (!CoolProp::is_trivial_parameter(iOutput)) {
CoolProp::parameters iName1 = CoolProp::get_parameter_index(sName1);
CoolProp::parameters iName2 = CoolProp::get_parameter_index(sName2);
// Convert inputs to SI
Prop1 = convert_from_kSI_to_SI(iName1, Prop1);
Prop2 = convert_from_kSI_to_SI(iName2, Prop2);
}
// Call the SI function
double val = PropsSI(Output, sName1.c_str(), Prop1, sName2.c_str(), Prop2, Ref);
// Convert back to unit system
return convert_from_SI_to_kSI(iOutput, val);
} catch (std::exception& e) {
CoolProp::set_error_string(e.what());
} catch (...) {
CoolProp::set_error_string("Undefined error");
}
return _HUGE;
}
EXPORT_CODE double CONVENTION saturation_ancillary(const char* fluid_name, const char* output, int Q, const char* input, double value) {
fpu_reset_guard guard;
try {
return CoolProp::saturation_ancillary(fluid_name, std::string(output), Q, std::string(input), value);
} catch (std::exception& e) {
CoolProp::set_error_string(e.what());
} catch (...) {
CoolProp::set_error_string("Undefined error");
}
return _HUGE;
}
EXPORT_CODE double CONVENTION Props1SI(const char* FluidName, const char* Output) {
fpu_reset_guard guard;
return CoolProp::Props1SI(std::string(FluidName), std::string(Output));
}
EXPORT_CODE void CONVENTION Props1SImulti(const char* Outputs, char* backend, const char* FluidNames, const double* fractions,
const long length_fractions, double* result, long* resdim1) {
fpu_reset_guard guard;
try {
// Outputs is a delimited string separated by LIST_STRING_DELIMITER
std::string delim = CoolProp::get_config_string(LIST_STRING_DELIMITER);
// strsplit only support char delimiter
if (delim.length() > 1)
throw CoolProp::ValueError(format("Length of string delimiter [%d] is bigger than 1 [%d]", delim.length(), delim.size()));
std::vector<std::string> _outputs = strsplit(Outputs, delim[0]);
// FluidNames is a delimited string separated by LIST_STRING_DELIMITER
std::vector<std::string> _fluidNames = strsplit(FluidNames, delim[0]);
if (_fluidNames.size() != length_fractions)
throw CoolProp::ValueError(
format("Length of fractions vector [%d] is not equal to length of fluidNames vector [%d]", _fluidNames.size(), length_fractions));
std::vector<double> _fractions(fractions, fractions + length_fractions);
std::vector<std::vector<double>> _result = CoolProp::Props1SImulti(_outputs, backend, _fluidNames, _fractions);
// if CoolProp::Props1SImulti fails it will return an empty vector -> set result dimensions to 0
if (_result.size() == 0) {
*resdim1 = 0;
} else {
if (_result.size() > *resdim1)
throw CoolProp::ValueError(format("Result vector [%d] is bigger than allocated memory [%d]", _result[0].size(), *resdim1));
*resdim1 = _result[0].size();
for (int i = 0; i < _result[0].size(); i++) {
result[i] = _result[0][i];
}
}
} catch (std::exception& e) {
CoolProp::set_error_string(e.what());
} catch (...) {
CoolProp::set_error_string("Undefined error");
}
}
EXPORT_CODE double CONVENTION PropsSI(const char* Output, const char* Name1, double Prop1, const char* Name2, double Prop2, const char* FluidName) {
fpu_reset_guard guard;
return CoolProp::PropsSI(std::string(Output), std::string(Name1), Prop1, std::string(Name2), Prop2, std::string(FluidName));
}
EXPORT_CODE void CONVENTION PropsSImulti(const char* Outputs, const char* Name1, double* Prop1, const long size_Prop1, const char* Name2,
double* Prop2, const long size_Prop2, char* backend, const char* FluidNames, const double* fractions,
const long length_fractions, double* result, long* resdim1, long* resdim2) {
fpu_reset_guard guard;
try {
// Outputs is a delimited string separated by LIST_STRING_DELIMITER
std::string delim = CoolProp::get_config_string(LIST_STRING_DELIMITER);
// strsplit only support char delimiter
if (delim.length() > 1)
throw CoolProp::ValueError(format("Length of string delimiter [%d] is bigger than 1 [%d]", delim.length(), delim.size()));
std::vector<std::string> _outputs = strsplit(Outputs, delim[0]);
if (size_Prop1 != size_Prop2)
throw CoolProp::ValueError(
format("Length of input parameter 1 [%d] is not equal to length of input parameter 2 [%d]", size_Prop1, size_Prop2));
// make vectors out of double pointer
std::vector<double> _prop1(Prop1, Prop1 + size_Prop1);
std::vector<double> _prop2(Prop2, Prop2 + size_Prop2);
// FluidNames is a delimited string separated by LIST_STRING_DELIMITER
std::vector<std::string> _fluidNames = strsplit(FluidNames, delim[0]);
if (_fluidNames.size() != length_fractions)
throw CoolProp::ValueError(
format("Length of fractions vector [%d] is not equal to length of fluidNames vector [%d]", _fluidNames.size(), length_fractions));
std::vector<double> _fractions(fractions, fractions + length_fractions);
std::vector<std::vector<double>> _result =
CoolProp::PropsSImulti(_outputs, std::string(Name1), _prop1, std::string(Name2), _prop2, backend, _fluidNames, _fractions);
// if CoolProp::PropsSImulti fails it will return an empty vector -> set result dimensions to 0
if (_result.size() == 0) {
*resdim1 = 0;
*resdim2 = 0;
} else {
if (_result.size() > *resdim1 || _result[0].size() > *resdim2)
throw CoolProp::ValueError(
format("Result matrix [%d x %d] is bigger than allocated memory [%d x %d]", _result.size(), _result[0].size(), *resdim1, *resdim2));
*resdim1 = _result.size();
*resdim2 = _result[0].size();
for (int i = 0; i < _result.size(); i++) {
for (int j = 0; j < _result[i].size(); j++) {
result[j + _result[i].size() * i] = _result[i][j];
}
}
}
} catch (std::exception& e) {
// set result dimensions to 0 to signalize, an error occured
*resdim1 = 0;
*resdim2 = 0;
CoolProp::set_error_string(e.what());
} catch (...) {
// set result dimensions to 0 to signalize, an error occured
*resdim1 = 0;
*resdim2 = 0;
CoolProp::set_error_string("Undefined error");
}
}
EXPORT_CODE long CONVENTION PhaseSI(const char* Name1, double Prop1, const char* Name2, double Prop2, const char* FluidName, char* phase, int n) {
fpu_reset_guard guard;
try {
std::string s = CoolProp::PhaseSI(std::string(Name1), Prop1, std::string(Name2), Prop2, std::string(FluidName));
str2buf(s, phase, n);
return 1;
} catch (std::exception& e) {
CoolProp::set_error_string(e.what());
} catch (...) {
CoolProp::set_error_string("Undefined error");
}
return 0;
}
/*
* EXPORT_CODE double CONVENTION PropsSIZ(const char *Output, const char *Name1, double Prop1, const char *Name2, double Prop2, const char * FluidName, const double *z, int n)
{
std::string _Output = Output, _Name1 = Name1, _Name2 = Name2, _FluidName = FluidName;
double val = CoolProp::PropsSI(_Output, _Name1, Prop1, _Name2, Prop2, _FluidName, std::vector<double>(z, z+n));
reset_fpu();
return val;
}
* */
EXPORT_CODE void CONVENTION propssi_(const char* Output, const char* Name1, const double* Prop1, const char* Name2, const double* Prop2,
const char* FluidName, double* output) {
*output = PropsSI(Output, Name1, *Prop1, Name2, *Prop2, FluidName);
}
EXPORT_CODE double CONVENTION K2F(double T) {
return T * 9 / 5 - 459.67;
}
EXPORT_CODE double CONVENTION F2K(double T_F) {
return (T_F + 459.67) * 5 / 9;
}
EXPORT_CODE int CONVENTION get_debug_level() {
return CoolProp::get_debug_level();
}
EXPORT_CODE void CONVENTION set_debug_level(int level) {
CoolProp::set_debug_level(level);
}
EXPORT_CODE long CONVENTION get_param_index(const char* param) {
try {
return CoolProp::get_parameter_index(param);
} catch (std::exception& e) {
CoolProp::set_error_string(e.what());
} catch (...) {
CoolProp::set_error_string("Undefined error");
}
return -1;
}
EXPORT_CODE long CONVENTION get_input_pair_index(const char* pair) {
try {
return CoolProp::get_input_pair_index(pair);
} catch (std::exception& e) {
CoolProp::set_error_string(e.what());
} catch (...) {
CoolProp::set_error_string("Undefined error");
}
return -1;
}
EXPORT_CODE long CONVENTION get_global_param_string(const char* param, char* Output, int n) {
try {
std::string s = CoolProp::get_global_param_string(param);
str2buf(s, Output, n);
return 1;
} catch (std::exception& e) {
CoolProp::set_error_string(e.what());
} catch (...) {
CoolProp::set_error_string("Undefined error");
}
return 0;
}
EXPORT_CODE long CONVENTION get_parameter_information_string(const char* param, char* Output, int n) {
try {
int key = CoolProp::get_parameter_index(param);
std::string s = CoolProp::get_parameter_information(key, Output);
str2buf(s, Output, n);
return 1;
} catch (std::exception& e) {
// if param is wrong, CoolProp::get_parameter_index throws string like
// "Your input name [%s] is not valid in get_parameter_index (names are case sensitive)"
// CoolProp::get_parameter_information throws string like
// "Bad info string [%s] to get_parameter_information" (if Output is wrong)
// or "Unable to match the key [%d] in get_parameter_information for info [%s]"
// (see src/DataStructures.cpp)
// if n is too small, str2buf throws string
// "Buffer size is too small"
CoolProp::set_error_string(format("get_parameter_information_string(\"%s\", \"%s\", %d): %s", param, Output, n, e.what()));
} catch (...) {
CoolProp::set_error_string(format("get_parameter_information_string(\"%s\", \"%s\", %d): Undefined error", param, Output, n));
}
return 0;
}
EXPORT_CODE long CONVENTION get_fluid_param_string(const char* fluid, const char* param, char* Output, int n) {
try {
std::string s = CoolProp::get_fluid_param_string(std::string(fluid), std::string(param));
str2buf(s, Output, n);
return 1;
} catch (std::exception& e) {
CoolProp::set_error_string(e.what());
} catch (...) {
CoolProp::set_error_string("Undefined error");
}
return 0;
}
EXPORT_CODE void CONVENTION set_config_string(const char* key, const char* val) {
try {
CoolProp::set_config_string(CoolProp::config_string_to_key(std::string(key)), std::string(val));
} catch (std::exception& e) {
CoolProp::set_error_string(e.what());
} catch (...) {
CoolProp::set_error_string("Undefined error");
}
}
EXPORT_CODE void CONVENTION set_config_double(const char* key, const double val) {
try {
CoolProp::set_config_double(CoolProp::config_string_to_key(std::string(key)), val);
} catch (std::exception& e) {
CoolProp::set_error_string(e.what());
} catch (...) {
CoolProp::set_error_string("Undefined error");
}
}
EXPORT_CODE void CONVENTION set_config_bool(const char* key, const bool val) {
try {
CoolProp::set_config_bool(CoolProp::config_string_to_key(std::string(key)), val);
} catch (std::exception& e) {
CoolProp::set_error_string(e.what());
} catch (...) {
CoolProp::set_error_string("Undefined error");
}
}
EXPORT_CODE void CONVENTION set_departure_functions(const char* string_data, long* errcode, char* message_buffer, const long buffer_length) {
*errcode = 0;
try {
CoolProp::set_departure_functions(string_data);
} catch (...) {
HandleException(errcode, message_buffer, buffer_length);
}
}
EXPORT_CODE double CONVENTION HAPropsSI(const char* Output, const char* Name1, double Prop1, const char* Name2, double Prop2, const char* Name3,
double Prop3) {
fpu_reset_guard guard;
return HumidAir::HAPropsSI(std::string(Output), std::string(Name1), Prop1, std::string(Name2), Prop2, std::string(Name3), Prop3);
}
EXPORT_CODE double CONVENTION cair_sat(double T) {
fpu_reset_guard guard;
return HumidAir::cair_sat(T);
}
EXPORT_CODE void CONVENTION hapropssi_(const char* Output, const char* Name1, const double* Prop1, const char* Name2, const double* Prop2,
const char* Name3, const double* Prop3, double* output) {
*output = HAPropsSI(Output, Name1, *Prop1, Name2, *Prop2, Name3, *Prop3);
}
EXPORT_CODE double CONVENTION HAProps(const char* Output, const char* Name1, double Prop1, const char* Name2, double Prop2, const char* Name3,
double Prop3) {
fpu_reset_guard guard;
try {
return HumidAir::HAProps(std::string(Output), std::string(Name1), Prop1, std::string(Name2), Prop2, std::string(Name3), Prop3);
} catch (std::exception& e) {
CoolProp::set_error_string(e.what());
} catch (...) {
CoolProp::set_error_string("Undefined error");
}
return _HUGE;
}
EXPORT_CODE void CONVENTION haprops_(const char* Output, const char* Name1, const double* Prop1, const char* Name2, const double* Prop2,
const char* Name3, const double* Prop3, double* output) {
*output = HAProps(Output, Name1, *Prop1, Name2, *Prop2, Name3, *Prop3);
}
class AbstractStateLibrary
{
private:
std::map<std::size_t, shared_ptr<CoolProp::AbstractState>> ASlibrary;
long next_handle;
std::mutex ASLib_mutex;
public:
AbstractStateLibrary() : next_handle(0){};
long add(shared_ptr<CoolProp::AbstractState> AS) {
std::lock_guard<std::mutex> guard(ASLib_mutex);
ASlibrary.insert(std::pair<std::size_t, shared_ptr<CoolProp::AbstractState>>(this->next_handle, AS));
this->next_handle++;
return next_handle - 1;
}
void remove(long handle) {
std::lock_guard<std::mutex> guard(ASLib_mutex);
std::size_t count_removed = ASlibrary.erase(handle);
if (count_removed != 1) {
throw CoolProp::HandleError("could not free handle");
}
}
shared_ptr<CoolProp::AbstractState>& get(long handle) {
std::lock_guard<std::mutex> guard(ASLib_mutex);
std::map<std::size_t, shared_ptr<CoolProp::AbstractState>>::iterator it = ASlibrary.find(handle);
if (it != ASlibrary.end()) {
return it->second;
} else {
throw CoolProp::HandleError("could not get handle");
}
}
};
static AbstractStateLibrary handle_manager;
EXPORT_CODE long CONVENTION AbstractState_factory(const char* backend, const char* fluids, long* errcode, char* message_buffer,
const long buffer_length) {
*errcode = 0;
try {
shared_ptr<CoolProp::AbstractState> AS(CoolProp::AbstractState::factory(backend, fluids));
return handle_manager.add(AS);
} catch (...) {
HandleException(errcode, message_buffer, buffer_length);
}
return -1;
}
EXPORT_CODE void CONVENTION AbstractState_fluid_names(const long handle, char* fluids, long* errcode, char* message_buffer,
const long buffer_length) {
*errcode = 0;
try {
shared_ptr<CoolProp::AbstractState>& AS = handle_manager.get(handle);
std::vector<std::string> _fluids = AS->fluid_names();
std::string fluidsstring = strjoin(_fluids, CoolProp::get_config_string(LIST_STRING_DELIMITER));
if (fluidsstring.size() < static_cast<std::size_t>(buffer_length)) {
strcpy(fluids, fluidsstring.c_str());
} else {
throw CoolProp::ValueError(format("Length of string [%d] is greater than allocated buffer length [%d]", fluidsstring.size(),
static_cast<std::size_t>(buffer_length)));
}
} catch (...) {
HandleException(errcode, message_buffer, buffer_length);
}
}
EXPORT_CODE void CONVENTION AbstractState_free(const long handle, long* errcode, char* message_buffer, const long buffer_length) {
*errcode = 0;
try {
handle_manager.remove(handle);
} catch (...) {
HandleException(errcode, message_buffer, buffer_length);
}
}
EXPORT_CODE void CONVENTION AbstractState_set_fractions(const long handle, const double* fractions, const long N, long* errcode, char* message_buffer,
const long buffer_length) {
*errcode = 0;
std::vector<double> _fractions(fractions, fractions + N);
try {
shared_ptr<CoolProp::AbstractState>& AS = handle_manager.get(handle);
if (AS->using_mole_fractions()) {
AS->set_mole_fractions(_fractions);
} else if (AS->using_mass_fractions()) {
AS->set_mass_fractions(_fractions);
} else if (AS->using_volu_fractions()) {
AS->set_volu_fractions(_fractions);
}
} catch (...) {
HandleException(errcode, message_buffer, buffer_length);
}
}
EXPORT_CODE void CONVENTION AbstractState_get_mole_fractions(const long handle, double* fractions, const long maxN, long* N, long* errcode,
char* message_buffer, const long buffer_length) {
*errcode = 0;
try {
shared_ptr<CoolProp::AbstractState>& AS = handle_manager.get(handle);
std::vector<double> _fractions = AS->get_mole_fractions();
*N = _fractions.size();
if (*N <= maxN) {
for (int i = 0; i < *N; i++)
fractions[i] = _fractions[i];
} else {
throw CoolProp::ValueError(format("Length of array [%d] is greater than allocated buffer length [%d]", *N, maxN));
}
} catch (...) {
HandleException(errcode, message_buffer, buffer_length);
}
}
EXPORT_CODE void CONVENTION AbstractState_get_mole_fractions_satState(const long handle, const char* saturated_state, double* fractions,
const long maxN, long* N, long* errcode, char* message_buffer,
const long buffer_length) {
*errcode = 0;
try {
shared_ptr<CoolProp::AbstractState>& AS = handle_manager.get(handle);
std::vector<double> _fractions;
double quality = AS->Q();
std::string string_state(saturated_state);
if (0 <= quality && quality <= 1) {
if (string_state == "liquid") {
_fractions = AS->mole_fractions_liquid();
} else if (string_state == "gas") {
_fractions = AS->mole_fractions_vapor();
} else {
throw CoolProp::ValueError(
format("Bad info string [%s] to saturated state mole fractions, options are \"liquid\" and \"gas\"", saturated_state));
}
} else {
throw CoolProp::ValueError(format("AbstractState_get_mole_fractions_satState only returns outputs for saturated states if AbstractState "
"quality [%g] is within two-phase region (0 <= quality <= 1)",
static_cast<double>(quality)));
}
*N = _fractions.size();
if (*N <= maxN) {
for (int i = 0; i < *N; i++) {
fractions[i] = _fractions[i];
}
} else {
throw CoolProp::ValueError(format("Length of array [%d] is greater than allocated buffer length [%d]", *N, maxN));
}
} catch (...) {
HandleException(errcode, message_buffer, buffer_length);
}
}
EXPORT_CODE double CONVENTION AbstractState_get_fugacity(const long handle, const long i, long* errcode, char* message_buffer,
const long buffer_length) {
*errcode = 0;
try {
shared_ptr<CoolProp::AbstractState>& AS = handle_manager.get(handle);
return AS->fugacity(i);
} catch (...) {
HandleException(errcode, message_buffer, buffer_length);
}
return _HUGE;
}
EXPORT_CODE double CONVENTION AbstractState_get_fugacity_coefficient(const long handle, const long i, long* errcode, char* message_buffer,
const long buffer_length) {
*errcode = 0;
try {
shared_ptr<CoolProp::AbstractState>& AS = handle_manager.get(handle);
return AS->fugacity_coefficient(i);
} catch (...) {
HandleException(errcode, message_buffer, buffer_length);
}
return _HUGE;
}
EXPORT_CODE void CONVENTION AbstractState_update(const long handle, const long input_pair, const double value1, const double value2, long* errcode,
char* message_buffer, const long buffer_length) {
*errcode = 0;
try {
shared_ptr<CoolProp::AbstractState>& AS = handle_manager.get(handle);
AS->update(static_cast<CoolProp::input_pairs>(input_pair), value1, value2);
} catch (...) {
HandleException(errcode, message_buffer, buffer_length);
}
}
EXPORT_CODE void CONVENTION AbstractState_specify_phase(const long handle, const char* phase, long* errcode, char* message_buffer,
const long buffer_length) {
*errcode = 0;
try {
shared_ptr<CoolProp::AbstractState>& AS = handle_manager.get(handle);
return AS->specify_phase(CoolProp::get_phase_index(std::string(phase)));
} catch (...) {
HandleException(errcode, message_buffer, buffer_length);
}
}
EXPORT_CODE void CONVENTION AbstractState_unspecify_phase(const long handle, long* errcode, char* message_buffer, const long buffer_length) {
*errcode = 0;
try {
shared_ptr<CoolProp::AbstractState>& AS = handle_manager.get(handle);
return AS->unspecify_phase();
} catch (...) {
HandleException(errcode, message_buffer, buffer_length);
}
}
EXPORT_CODE double CONVENTION AbstractState_keyed_output(const long handle, const long param, long* errcode, char* message_buffer,
const long buffer_length) {
*errcode = 0;
try {
shared_ptr<CoolProp::AbstractState>& AS = handle_manager.get(handle);
return AS->keyed_output(static_cast<CoolProp::parameters>(param));
} catch (...) {
HandleException(errcode, message_buffer, buffer_length);
}
return _HUGE;
}
EXPORT_CODE double CONVENTION AbstractState_first_saturation_deriv(const long handle, const long Of, const long Wrt, long* errcode,
char* message_buffer, const long buffer_length) {
*errcode = 0;
try {
shared_ptr<CoolProp::AbstractState>& AS = handle_manager.get(handle);
return AS->first_saturation_deriv(static_cast<CoolProp::parameters>(Of), static_cast<CoolProp::parameters>(Wrt));
} catch (...) {
HandleException(errcode, message_buffer, buffer_length);
}
return _HUGE;
}
EXPORT_CODE double CONVENTION AbstractState_first_partial_deriv(const long handle, const long Of, const long Wrt, const long Constant, long* errcode,
char* message_buffer, const long buffer_length) {
*errcode = 0;
try {
shared_ptr<CoolProp::AbstractState>& AS = handle_manager.get(handle);
return AS->first_partial_deriv(static_cast<CoolProp::parameters>(Of), static_cast<CoolProp::parameters>(Wrt),
static_cast<CoolProp::parameters>(Constant));
} catch (...) {
HandleException(errcode, message_buffer, buffer_length);
}
return _HUGE;
}
EXPORT_CODE double CONVENTION AbstractState_second_partial_deriv(const long handle, const long Of1, const long Wrt1, const long Constant1,
const long Wrt2, const long Constant2, long* errcode, char* message_buffer,
const long buffer_length) {
*errcode = 0;
try {
shared_ptr<CoolProp::AbstractState>& AS = handle_manager.get(handle);
return AS->second_partial_deriv(static_cast<CoolProp::parameters>(Of1), static_cast<CoolProp::parameters>(Wrt1),
static_cast<CoolProp::parameters>(Constant1), static_cast<CoolProp::parameters>(Wrt2),
static_cast<CoolProp::parameters>(Constant2));
} catch (...) {
HandleException(errcode, message_buffer, buffer_length);
}
return _HUGE;
}
EXPORT_CODE double CONVENTION AbstractState_second_two_phase_deriv(const long handle, const long Of1, const long Wrt1, const long Constant1,
const long Wrt2, const long Constant2, long* errcode, char* message_buffer,
const long buffer_length) {
*errcode = 0;
try {
shared_ptr<CoolProp::AbstractState>& AS = handle_manager.get(handle);
return AS->second_two_phase_deriv(static_cast<CoolProp::parameters>(Of1), static_cast<CoolProp::parameters>(Wrt1),
static_cast<CoolProp::parameters>(Constant1), static_cast<CoolProp::parameters>(Wrt2),
static_cast<CoolProp::parameters>(Constant2));
} catch (...) {
HandleException(errcode, message_buffer, buffer_length);
}
return _HUGE;
}
EXPORT_CODE double CONVENTION AbstractState_first_two_phase_deriv(const long handle, const long Of, const long Wrt, const long Constant,
long* errcode, char* message_buffer, const long buffer_length) {
*errcode = 0;
try {
shared_ptr<CoolProp::AbstractState>& AS = handle_manager.get(handle);
return AS->first_two_phase_deriv(static_cast<CoolProp::parameters>(Of), static_cast<CoolProp::parameters>(Wrt),
static_cast<CoolProp::parameters>(Constant));
} catch (...) {
HandleException(errcode, message_buffer, buffer_length);
}
return _HUGE;
}
EXPORT_CODE double CONVENTION AbstractState_first_two_phase_deriv_splined(const long handle, const long Of, const long Wrt, const long Constant,
const double x_end, long* errcode, char* message_buffer,
const long buffer_length) {
*errcode = 0;
try {
shared_ptr<CoolProp::AbstractState>& AS = handle_manager.get(handle);
return AS->first_two_phase_deriv_splined(static_cast<CoolProp::parameters>(Of), static_cast<CoolProp::parameters>(Wrt),
static_cast<CoolProp::parameters>(Constant), x_end);
} catch (...) {
HandleException(errcode, message_buffer, buffer_length);
}
return _HUGE;
}
EXPORT_CODE void CONVENTION AbstractState_update_and_common_out(const long handle, const long input_pair, const double* value1, const double* value2,
const long length, double* T, double* p, double* rhomolar, double* hmolar,
double* smolar, long* errcode, char* message_buffer, const long buffer_length) {
*errcode = 0;
try {
shared_ptr<CoolProp::AbstractState>& AS = handle_manager.get(handle);
for (int i = 0; i < length; i++) {
try {
AS->update(static_cast<CoolProp::input_pairs>(input_pair), *(value1 + i), *(value2 + i));
*(T + i) = AS->T();
*(p + i) = AS->p();
*(rhomolar + i) = AS->rhomolar();
*(hmolar + i) = AS->hmolar();
*(smolar + i) = AS->smolar();
} catch (...) {
}
};
} catch (...) {
HandleException(errcode, message_buffer, buffer_length);
}
}
EXPORT_CODE void CONVENTION AbstractState_update_and_1_out(const long handle, const long input_pair, const double* value1, const double* value2,
const long length, const long output, double* out, long* errcode, char* message_buffer,
const long buffer_length) {
*errcode = 0;
try {
shared_ptr<CoolProp::AbstractState>& AS = handle_manager.get(handle);
for (int i = 0; i < length; i++) {
try {
AS->update(static_cast<CoolProp::input_pairs>(input_pair), *(value1 + i), *(value2 + i));
*(out + i) = AS->keyed_output(static_cast<CoolProp::parameters>(output));
} catch (...) {
}
};
} catch (...) {
HandleException(errcode, message_buffer, buffer_length);
}
}
EXPORT_CODE void CONVENTION AbstractState_update_and_5_out(const long handle, const long input_pair, const double* value1, const double* value2,
const long length, long* outputs, double* out1, double* out2, double* out3, double* out4,
double* out5, long* errcode, char* message_buffer, const long buffer_length) {
*errcode = 0;
try {
shared_ptr<CoolProp::AbstractState>& AS = handle_manager.get(handle);
for (int i = 0; i < length; i++) {
try {
AS->update(static_cast<CoolProp::input_pairs>(input_pair), *(value1 + i), *(value2 + i));
*(out1 + i) = AS->keyed_output(static_cast<CoolProp::parameters>(outputs[0]));
*(out2 + i) = AS->keyed_output(static_cast<CoolProp::parameters>(outputs[1]));
*(out3 + i) = AS->keyed_output(static_cast<CoolProp::parameters>(outputs[2]));
*(out4 + i) = AS->keyed_output(static_cast<CoolProp::parameters>(outputs[3]));
*(out5 + i) = AS->keyed_output(static_cast<CoolProp::parameters>(outputs[4]));
} catch (...) {
}
};
} catch (...) {
HandleException(errcode, message_buffer, buffer_length);
}
}
EXPORT_CODE void CONVENTION AbstractState_set_binary_interaction_double(const long handle, const long i, const long j, const char* parameter,
const double value, long* errcode, char* message_buffer,
const long buffer_length) {
*errcode = 0;
try {
shared_ptr<CoolProp::AbstractState>& AS = handle_manager.get(handle);
AS->set_binary_interaction_double(static_cast<std::size_t>(i), static_cast<std::size_t>(j), parameter, value);
} catch (...) {
HandleException(errcode, message_buffer, buffer_length);
}
}
EXPORT_CODE void CONVENTION AbstractState_set_cubic_alpha_C(const long handle, const long i, const char* parameter, const double c1, const double c2,
const double c3, long* errcode, char* message_buffer, const long buffer_length) {
*errcode = 0;
try {
shared_ptr<CoolProp::AbstractState>& AS = handle_manager.get(handle);
AS->set_cubic_alpha_C(static_cast<std::size_t>(i), parameter, c1, c2, c3);
} catch (...) {
HandleException(errcode, message_buffer, buffer_length);
}
}
EXPORT_CODE void CONVENTION AbstractState_set_fluid_parameter_double(const long handle, const long i, const char* parameter, const double value,
long* errcode, char* message_buffer, const long buffer_length) {
*errcode = 0;
try {
shared_ptr<CoolProp::AbstractState>& AS = handle_manager.get(handle);
AS->set_fluid_parameter_double(static_cast<std::size_t>(i), parameter, value);
} catch (...) {
HandleException(errcode, message_buffer, buffer_length);
}
}
EXPORT_CODE void CONVENTION AbstractState_build_phase_envelope(const long handle, const char* level, long* errcode, char* message_buffer,
const long buffer_length) {
*errcode = 0;
try {
shared_ptr<CoolProp::AbstractState>& AS = handle_manager.get(handle);
AS->build_phase_envelope(level);
} catch (...) {
HandleException(errcode, message_buffer, buffer_length);
}
}
EXPORT_CODE void CONVENTION AbstractState_get_phase_envelope_data(const long handle, const long length, double* T, double* p, double* rhomolar_vap,
double* rhomolar_liq, double* x, double* y, long* errcode, char* message_buffer,
const long buffer_length) {
*errcode = 0;
try {
shared_ptr<CoolProp::AbstractState>& AS = handle_manager.get(handle);
CoolProp::PhaseEnvelopeData pe = AS->get_phase_envelope_data();
if (pe.T.size() > static_cast<std::size_t>(length)) {
throw CoolProp::ValueError(format("Length of phase envelope vectors [%d] is greater than allocated buffer length [%d]",
static_cast<int>(pe.T.size()), static_cast<int>(length)));
}
std::size_t N = pe.x.size();
for (std::size_t i = 0; i < pe.T.size(); i++) {
*(T + i) = pe.T[i];
*(p + i) = pe.p[i];
*(rhomolar_vap + i) = pe.rhomolar_vap[i];
*(rhomolar_liq + i) = pe.rhomolar_liq[i];
for (std::size_t j = 0; j < N; ++j) {
*(x + i * N + j) = pe.x[j][i];
*(y + i * N + j) = pe.y[j][i];
}
}
} catch (...) {
HandleException(errcode, message_buffer, buffer_length);
}
}
EXPORT_CODE void CONVENTION AbstractState_get_phase_envelope_data_checkedMemory(const long handle, const long length, const long maxComponents, double* T,
double* p, double* rhomolar_vap, double* rhomolar_liq, double* x, double* y,
long* actual_length, long* actual_components, long* errcode, char* message_buffer,
const long buffer_length) {
*errcode = 0;
try {
shared_ptr<CoolProp::AbstractState>& AS = handle_manager.get(handle);
CoolProp::PhaseEnvelopeData pe = AS->get_phase_envelope_data();
*actual_length = pe.T.size();
if (pe.T.size() > static_cast<std::size_t>(length)) {
throw CoolProp::ValueError(format("Length of phase envelope vectors [%d] is greater than allocated buffer length [%d]",
static_cast<int>(pe.T.size()), static_cast<int>(length)));
}
*actual_components = pe.x.size();
if (*actual_components > static_cast<std::size_t>(maxComponents)) {
throw CoolProp::ValueError(format("Length of phase envelope composition vectors [%d] is greater than allocated buffer length [%d]",
static_cast<int>(*actual_components), static_cast<int>(maxComponents)));
}
for (std::size_t i = 0; i < pe.T.size(); i++) {
*(T + i) = pe.T[i];
*(p + i) = pe.p[i];
*(rhomolar_vap + i) = pe.rhomolar_vap[i];
*(rhomolar_liq + i) = pe.rhomolar_liq[i];
for (std::size_t j = 0; j < *actual_components; ++j) {
*(x + i * *actual_components + j) = pe.x[j][i];
*(y + i * *actual_components + j) = pe.y[j][i];
}
}
} catch (...) {
HandleException(errcode, message_buffer, buffer_length);
}
}
EXPORT_CODE void CONVENTION AbstractState_build_spinodal(const long handle, long* errcode, char* message_buffer, const long buffer_length) {
*errcode = 0;
try {
shared_ptr<CoolProp::AbstractState>& AS = handle_manager.get(handle);
AS->build_spinodal();
} catch (...) {
HandleException(errcode, message_buffer, buffer_length);
}
}
EXPORT_CODE void CONVENTION AbstractState_get_spinodal_data(const long handle, const long length, double* tau, double* delta, double* M1,
long* errcode, char* message_buffer, const long buffer_length) {
*errcode = 0;
try {
shared_ptr<CoolProp::AbstractState>& AS = handle_manager.get(handle);
CoolProp::SpinodalData spin = AS->get_spinodal_data();
if (spin.tau.size() > static_cast<std::size_t>(length)) {
throw CoolProp::ValueError(format("Length of spinodal vectors [%d] is greater than allocated buffer length [%d]",
static_cast<int>(spin.tau.size()), static_cast<int>(length)));
}
for (std::size_t i = 0; i < spin.tau.size(); ++i) {
*(tau + i) = spin.tau[i];
*(delta + i) = spin.delta[i];
*(M1 + i) = spin.M1[i];
}
} catch (...) {
HandleException(errcode, message_buffer, buffer_length);
}
}
EXPORT_CODE void CONVENTION AbstractState_all_critical_points(const long handle, long length, double* T, double* p, double* rhomolar, long* stable,
long* errcode, char* message_buffer, const long buffer_length) {
*errcode = 0;
try {
shared_ptr<CoolProp::AbstractState>& AS = handle_manager.get(handle);
std::vector<CoolProp::CriticalState> pts = AS->all_critical_points();
if (pts.size() > static_cast<std::size_t>(length)) {
throw CoolProp::ValueError(format("Length of critical point vector [%d] is greater than allocated buffer length [%d]",
static_cast<int>(pts.size()), static_cast<int>(length)));
}
for (std::size_t i = 0; i < pts.size(); ++i) {
*(T + i) = pts[i].T;
*(p + i) = pts[i].p;
*(rhomolar + i) = pts[i].rhomolar;
*(stable + i) = pts[i].stable;
}
} catch (...) {
HandleException(errcode, message_buffer, buffer_length);
}
}
EXPORT_CODE double CONVENTION AbstractState_keyed_output_satState(const long handle, const char* saturated_state, const long param, long* errcode,
char* message_buffer, const long buffer_length) {
*errcode = 0;
try {
shared_ptr<CoolProp::AbstractState>& AS = handle_manager.get(handle);
double quality = AS->Q();
std::string string_state(saturated_state);
if (0 <= quality && quality <= 1) {
if (string_state == "liquid") {
return AS->saturated_liquid_keyed_output(static_cast<CoolProp::parameters>(param));
} else if (string_state == "gas") {
return AS->saturated_vapor_keyed_output(static_cast<CoolProp::parameters>(param));
} else {
throw CoolProp::ValueError(
format("Bad info string [%s] to saturated state output, options are \"liquid\" and \"gas\"", saturated_state));
}
} else {
throw CoolProp::ValueError(format("AbstractState_keyed_output_satState only returns outputs for saturated states if AbstractState "
"quality [%g] is within two-phase region (0 <= quality <= 1)",
static_cast<double>(quality)));
}