CoolProp  4.2.5
An open-source fluid property and humid air property database
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Units.cpp
Go to the documentation of this file.
1 #include "Units.h"
2 #include "GlobalConstants.h"
3 #include "CoolProp.h"
4 
5 double convert_from_unit_system_to_SI(long iInput, double value, int old_system)
6 {
7  if (get_debug_level() > 8)
8  {
9  std::cout << format("%s:%d: convert_from_unit_system_to_SI(i=%d,value=%g,old_system=%d)\n",__FILE__,__LINE__,iInput,value,old_system).c_str();
10  }
11  if (old_system == UNIT_SYSTEM_SI)
12  {
13  return value;
14  }
15 
16  switch (iInput)
17  {
18  case iP:
19  case iPtriple:
20  case iPcrit:
21  case iPsat:
22  case iPmax:
23  case iC:
24  case iC0:
25  case iS:
26  case iG:
27  case iO:
28  case iH:
29  case iU:
30  case iL:
31  case iDERdp_dT__rho:
32  case iDERdp_drho__T:
33  case iDERdh_dT__rho:
34  case iDERdh_drho__T:
35  switch (old_system)
36  {
37  case UNIT_SYSTEM_KSI:
38  return value*1000.0;
39  break;
40  default:
41  throw ValueError(format("Unit System [%d] is undefined",old_system).c_str());
42  break;
43  }
44  break;
45  case iDERdrho_dh__p:
46  case iDERdrho_dp__h:
52  switch (old_system)
53  {
54  case UNIT_SYSTEM_KSI:
55  return value/1000.0;
56  break;
57  default:
58  throw ValueError(format("Unit System [%d] is undefined",old_system).c_str());
59  break;
60  }
61  break;
62  case iD:
63  case iA:
64  case iQ:
65  case iV:
66  case iT:
67  case iTcrit:
68  case iTtriple:
69  case iTreduce:
70  case iPrandtl:
71  case iTmin:
72  case iTmax:
73  case iTfreeze:
74  case iPHASE_LIQUID:
75  case iPHASE_GAS:
77  case iPHASE_TWOPHASE:
78  case iODP:
79  case iGWP100:
80  case iMM:
81  case iRhocrit:
82  case iRhoreduce:
83  case iAccentric:
84  case iI:
85  case iCritSplineT:
86  case iDERdh_dp__rho:
87  case iDERdh_dp__v:
88  case iDERZ:
89  case iDERdZ_dDelta:
90  case iDERdZ_dTau:
91  case iDERB:
92  case iDERdB_dT:
93  case iDERC:
94  case iDERdC_dT:
95  case iDERphir:
96  case iDERdphir_dTau:
97  case iDERdphir_dDelta:
98  case iDERd2phir_dTau2:
99  case iDERd2phir_dDelta2:
101  case iDERd3phir_dDelta3:
104  case iDERd3phir_dTau3:
105  case iDERphi0:
106  case iDERdphi0_dTau:
107  case iDERd2phi0_dTau2:
108  case iDERdphi0_dDelta:
109  case iDERd2phi0_dDelta2:
111  case iDERd3phi0_dTau3:
112  case iDERdrho_dT__p:
113  case iDERrho_smoothed:
114  return value;
115  break;
116  default:
117  throw ValueError(format("index [%d] is invalid in convert_from_unit_system_to_SI",iInput).c_str());
118  break;
119  }
120  return _HUGE;
121 }
122 
123 double convert_from_SI_to_unit_system(long iInput, double value, int new_system)
124 {
125  if (get_debug_level() > 8)
126  {
127  std::cout << format("%s:%d: convert_from_SI_to_unit_system(%d,%g,%d)\n",__FILE__,__LINE__,iInput,value,new_system).c_str();
128  }
129 
130  if (new_system == UNIT_SYSTEM_SI)
131  {
132  return value;
133  }
134 
135  switch (iInput)
136  {
137  case iP:
138  case iPtriple:
139  case iPcrit:
140  case iPsat:
141  case iPmax:
142  case iC:
143  case iC0:
144  case iS:
145  case iG:
146  case iO:
147  case iH:
148  case iU:
149  case iL:
150  case iDERdp_dT__rho:
151  case iDERdp_drho__T:
152  case iDERdh_dT__rho:
153  case iDERdh_drho__T:
154  switch (new_system)
155  {
156  case UNIT_SYSTEM_KSI:
157  return value/1000.0;
158  break;
159  default:
160  throw ValueError(format("Unit System [%d] is undefined",new_system).c_str());
161  break;
162  }
163  break;
164  case iDERdrho_dh__p:
165  case iDERdrho_dp__h:
171  switch (new_system)
172  {
173  case UNIT_SYSTEM_KSI:
174  return value*1000.0;
175  break;
176  default:
177  throw ValueError(format("Unit System [%d] is undefined",new_system).c_str());
178  break;
179  }
180  break;
181  case iD:
182  case iQ:
183  case iA:
184  case iV:
185  case iT:
186  case iTmin:
187  case iTmax:
188  case iTfreeze:
189  case iTcrit:
190  case iTtriple:
191  case iTreduce:
192  case iPHASE_LIQUID:
193  case iPHASE_GAS:
195  case iPHASE_TWOPHASE:
196  case iODP:
197  case iGWP100:
198  case iMM:
199  case iRhocrit:
200  case iRhoreduce:
201  case iAccentric:
202  case iI:
203  case iCritSplineT:
204  case iDERdh_dp__rho:
205  case iDERdh_dp__v:
206  case iDERZ:
207  case iDERdZ_dDelta:
208  case iDERdZ_dTau:
209  case iDERB:
210  case iDERdB_dT:
211  case iDERC:
212  case iDERdC_dT:
213  case iDERphir:
214  case iDERdphir_dTau:
215  case iDERdphir_dDelta:
216  case iDERd2phir_dTau2:
217  case iDERd2phir_dDelta2:
219  case iDERd3phir_dDelta3:
222  case iDERd3phir_dTau3:
223  case iDERphi0:
224  case iDERdphi0_dTau:
225  case iDERd2phi0_dTau2:
226  case iDERdphi0_dDelta:
227  case iDERd2phi0_dDelta2:
229  case iDERd3phi0_dTau3:
230  case iDERdrho_dT__p:
231  case iDERrho_smoothed:
232  case iDERdh_dp__T:
233  return value;
234  break;
235  default:
236  throw ValueError(format("index [%d] is invalid in convert_from_SI_to_unit_system",iInput).c_str());
237  break;
238  }
239  return _HUGE;
240 }
241 
242 double convert_from_SI_to_unit_system(std::string input, double value, std::string new_system)
243 {
244  int iSys = -1;
245  if (!new_system.compare("kSI")) iSys = UNIT_SYSTEM_KSI;
246  if (!new_system.compare( "SI")) iSys = UNIT_SYSTEM_SI;
247  double val = convert_from_SI_to_unit_system(get_param_index(input), value, iSys);
248  return val;
249 }
250 double convert_from_unit_system_to_SI(std::string input, double value, std::string old_system)
251 {
252  int iSys = -1;
253  if (!old_system.compare("kSI")) iSys = UNIT_SYSTEM_KSI;
254  if (!old_system.compare( "SI")) iSys = UNIT_SYSTEM_SI;
255  double val = convert_from_unit_system_to_SI(get_param_index(input), value, iSys);
256  return val;
257 }
258 double convert_from_SI_to_unit_system(char *input, double value, char *new_system)
259 {
260  return convert_from_SI_to_unit_system(std::string(input), value, std::string(new_system));
261 }
262 double convert_from_unit_system_to_SI(char *input, double value, char *old_system)
263 {
264  return convert_from_unit_system_to_SI(std::string(input), value, std::string(old_system));
265 }
266 
267 double conversion_factor(std::string num)
268 {
269  double factor = 1.0;
270 
271  // Parse the first entry
272  if (num[0] != '1')
273  {
274  std::string key;
275  key.resize(1);
276  key[0] = num[0];
278  }
279 
280  for (int i = 1; i < (int)num.size(); i += 2)
281  {
282  char op = num[i];
283 
284  std::string key;
285  key.resize(1);
286  key[0] = num[i+1];
288  if (op == '*')
289  {
290  factor *= term;
291  }
292  else
293  {
294  factor /= term;
295  }
296 
297  }
298  return factor;
299 }
EXPORT_CODE int CONVENTION get_debug_level()
EXPORT_CODE int CONVENTION get_standard_unit_system(void)
std::string format(const char *fmt,...)
double convert_from_SI_to_unit_system(long iInput, double value, int new_system)
Definition: Units.cpp:123
double conversion_factor(std::string num)
Definition: Units.cpp:267
long get_param_index(std::string param)
Definition: CoolProp.cpp:288
double convert_from_unit_system_to_SI(long iInput, double value, int old_system)
Definition: Units.cpp:5