Repository navigation
Expand file tree
/
Copy pathCoolPropLib.cpp
More file actions
320 lines (307 loc) · 10.5 KB
/
Copy pathCoolPropLib.cpp
File metadata and controls
320 lines (307 loc) · 10.5 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
#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 <string.h>
// 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
void reset_fpu()
{
#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());
break;
}
return _HUGE;
}
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());
break;
}
return _HUGE;
}
EXPORT_CODE long CONVENTION redirect_stdout(const char* file){
freopen(file, "a+", stdout);
reset_fpu();
return 0;
}
EXPORT_CODE int CONVENTION set_reference_stateS(const char *Ref, const char *reference_state)
{
std::string _Ref = Ref, _reference_state = reference_state;
try{
CoolProp::set_reference_stateS(_Ref, _reference_state);
reset_fpu();
return true;
}
catch(std::exception &){
reset_fpu();
return false;
}
}
EXPORT_CODE int CONVENTION set_reference_stateD(const char *Ref, double T, double rho, double h0, double s0)
{
try{
CoolProp::set_reference_stateD(std::string(Ref), T, rho, h0, s0);
reset_fpu();
return true;
}
catch(std::exception &){
reset_fpu();
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){
double val = PropsS(Output, "", 0, "", 0, FluidName);
reset_fpu();
return val;
}
EXPORT_CODE double CONVENTION PropsS(const char *Output, const char* Name1, double Prop1, const char* Name2, double Prop2, const char * Ref){
double val = Props(Output,Name1[0],Prop1,Name2[0],Prop2,Ref);
reset_fpu();
return val;
}
EXPORT_CODE double CONVENTION Props(const char *Output, const char Name1, double Prop1, const char Name2, double Prop2, const char * Ref)
{
try
{
// Get parameter indices
std::string sName1 = std::string(1, Name1), sName2 = std::string(1, Name2);
long iOutput = get_param_index(Output);
long iName1 = CoolProp::get_parameter_index(sName1);
long 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);
reset_fpu();
// Convert back to unit system
return convert_from_SI_to_kSI(iOutput, val);
}
catch(std::exception &e){CoolProp::set_error_string(e.what()); return _HUGE;}
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)
{
try
{
std::string _output = output, _input = input;
double val = CoolProp::saturation_ancillary(fluid_name, _output, Q, _input, value);
reset_fpu();
return val;
}
catch(std::exception &e){CoolProp::set_error_string(e.what()); return _HUGE;}
catch(...){CoolProp::set_error_string("Undefined error"); return _HUGE;}
}
EXPORT_CODE double CONVENTION Props1SI(const char *FluidName, const char *Output)
{
std::string _Output = Output, _FluidName = FluidName;
double val = CoolProp::Props1SI(_FluidName, _Output);
reset_fpu();
return val;
}
EXPORT_CODE double CONVENTION PropsSI(const char *Output, const char *Name1, double Prop1, const char *Name2, double Prop2, const char * FluidName)
{
std::string _Output = Output, _Name1 = Name1, _Name2 = Name2, _FluidName = FluidName;
double val = CoolProp::PropsSI(_Output, _Name1, Prop1, _Name2, Prop2, _FluidName);
reset_fpu();
return val;
}
EXPORT_CODE long CONVENTION PhaseSI(const char *Name1, double Prop1, const char *Name2, double Prop2, const char * FluidName, char *phase, int n)
{
std::string _Name1 = Name1, _Name2 = Name2, _FluidName = FluidName;
std::string s = CoolProp::PhaseSI(_Name1, Prop1, _Name2, Prop2, _FluidName);
if (s.size() < static_cast<unsigned int>(n)){
strcpy(phase, s.c_str());
reset_fpu();
return 1;
}
else{
reset_fpu();
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, double *Prop1, const char *Name2, double *Prop2, const char * FluidName, double *output)
{
std::string _Output = Output, _Name1 = Name1, _Name2 = Name2, _FluidName = FluidName;
*output = CoolProp::PropsSI(_Output, _Name1, *Prop1, _Name2, *Prop2, _FluidName);
reset_fpu();
}
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 &){
return -1;
}
}
EXPORT_CODE long CONVENTION get_global_param_string(const char *param, char * Output, int n)
{
std::string s;
try{
s = CoolProp::get_global_param_string(param);
}
catch(std::exception &){
return 0;
}
if (s.size() < static_cast<unsigned int>(n)){
strcpy(Output, s.c_str());
return 1;
}
else{
return 0;
}
}
EXPORT_CODE long CONVENTION get_parameter_information_string(const char *param, char * Output, int n)
{
int key;
try{
key = CoolProp::get_parameter_index(param);
}
catch(std::exception &){
return 0;
}
if (key >= 0){
std::string s = CoolProp::get_parameter_information(key, Output);
if (s.size() < static_cast<unsigned int>(n)){
strcpy(Output, s.c_str());
return 1;
}
else{
return 0;
}
}
else{
strcpy(Output, format("parameter is invalid: %s", param).c_str());
return 0;
}
}
EXPORT_CODE long CONVENTION get_fluid_param_string(const char *fluid, const char *param, char * Output, int n)
{
std::string s;
try{
s = CoolProp::get_fluid_param_string(std::string(fluid), std::string(param));
}
catch(std::exception &){
return 0;
}
if (s.size() < static_cast<unsigned int>(n)){
strcpy(Output, s.c_str());
return 1;
}
else{
return 0;
}
}
EXPORT_CODE double CONVENTION HAPropsSI(const char *Output, const char *Name1, double Prop1, const char *Name2, double Prop2, const char * Name3, double Prop3)
{
std::string _Output = Output, _Name1 = Name1, _Name2 = Name2, _Name3 = Name3;
double val = HumidAir::HAPropsSI(Output, _Name1, Prop1, _Name2, Prop2, _Name3, Prop3);
reset_fpu();
return val;
}
EXPORT_CODE void CONVENTION hapropssi_(const char *Output, const char *Name1, double *Prop1, const char *Name2, double *Prop2, const char * Name3, double * Prop3, double *output)
{
std::string _Output = Output, _Name1 = Name1, _Name2 = Name2, _Name3 = Name3;
*output = HumidAir::HAPropsSI(_Output, _Name1, *Prop1, _Name2, *Prop2, _Name3, *Prop3);
reset_fpu();
}
EXPORT_CODE double CONVENTION HAProps(const char *Output, const char *Name1, double Prop1, const char *Name2, double Prop2, const char * Name3, double Prop3)
{
std::string _Output = Output, _Name1 = Name1, _Name2 = Name2, _Name3 = Name3;
double val = HumidAir::HAProps(Output, _Name1, Prop1, _Name2, Prop2, _Name3, Prop3);
reset_fpu();
return val;
}
EXPORT_CODE void CONVENTION haprops_(const char *Output, const char *Name1, double *Prop1, const char *Name2, double *Prop2, const char * Name3, double * Prop3, double *output)
{
*output = HAProps(Output, Name1, *Prop1, Name2, *Prop2, Name3, *Prop3);
reset_fpu();
}