CoolProp 7.2.0
An open-source fluid property and humid air property database
FluidLibrary.h
Go to the documentation of this file.
1
2#ifndef FLUIDLIBRARY_H
3#define FLUIDLIBRARY_H
4
5#include "CoolPropFluid.h"
6
7#include "rapidjson_include.h"
9
10#include <map>
11#include <algorithm>
12#include "Configuration.h"
14#include "Helmholtz.h"
15
16namespace CoolProp {
17
18// Forward declaration of the necessary debug function to avoid including the whole header
19extern int get_debug_level();
20
22
28{
30 std::map<std::size_t, CoolPropFluid> fluid_map;
32 std::map<std::size_t, std::string> JSONstring_map;
33 std::vector<std::string> name_vector;
34 std::map<std::string, std::size_t> string_to_index_map;
35 bool _is_empty;
36
37 public:
39 static ResidualHelmholtzContainer parse_alphar(rapidjson::Value& jsonalphar) {
41
42 for (rapidjson::Value::ValueIterator itr = jsonalphar.Begin(); itr != jsonalphar.End(); ++itr) {
43 // A reference for code cleanness
44 rapidjson::Value& contribution = *itr;
45
46 // Get the type (required!)
47 std::string type = contribution["type"].GetString();
48
49 if (!type.compare("ResidualHelmholtzPower")) {
50 std::vector<CoolPropDbl> n = cpjson::get_long_double_array(contribution["n"]);
51 std::vector<CoolPropDbl> d = cpjson::get_long_double_array(contribution["d"]);
52 std::vector<CoolPropDbl> t = cpjson::get_long_double_array(contribution["t"]);
53 std::vector<CoolPropDbl> l = cpjson::get_long_double_array(contribution["l"]);
54 assert(n.size() == d.size());
55 assert(n.size() == t.size());
56 assert(n.size() == l.size());
57
58 alphar.GenExp.add_Power(n, d, t, l);
59 } else if (!type.compare("ResidualHelmholtzGaussian")) {
60 std::vector<CoolPropDbl> n = cpjson::get_long_double_array(contribution["n"]);
61 std::vector<CoolPropDbl> d = cpjson::get_long_double_array(contribution["d"]);
62 std::vector<CoolPropDbl> t = cpjson::get_long_double_array(contribution["t"]);
63 std::vector<CoolPropDbl> eta = cpjson::get_long_double_array(contribution["eta"]);
64 std::vector<CoolPropDbl> epsilon = cpjson::get_long_double_array(contribution["epsilon"]);
65 std::vector<CoolPropDbl> beta = cpjson::get_long_double_array(contribution["beta"]);
66 std::vector<CoolPropDbl> gamma = cpjson::get_long_double_array(contribution["gamma"]);
67 assert(n.size() == d.size());
68 assert(n.size() == t.size());
69 assert(n.size() == eta.size());
70 assert(n.size() == epsilon.size());
71 assert(n.size() == beta.size());
72 assert(n.size() == gamma.size());
73 alphar.GenExp.add_Gaussian(n, d, t, eta, epsilon, beta, gamma);
74 } else if (!type.compare("ResidualHelmholtzGaoB")) {
75 std::vector<CoolPropDbl> n = cpjson::get_long_double_array(contribution["n"]);
76 std::vector<CoolPropDbl> t = cpjson::get_long_double_array(contribution["t"]);
77 std::vector<CoolPropDbl> d = cpjson::get_long_double_array(contribution["d"]);
78 std::vector<CoolPropDbl> eta = cpjson::get_long_double_array(contribution["eta"]);
79 std::vector<CoolPropDbl> beta = cpjson::get_long_double_array(contribution["beta"]);
80 std::vector<CoolPropDbl> gamma = cpjson::get_long_double_array(contribution["gamma"]);
81 std::vector<CoolPropDbl> epsilon = cpjson::get_long_double_array(contribution["epsilon"]);
82 std::vector<CoolPropDbl> b = cpjson::get_long_double_array(contribution["b"]);
83 assert(n.size() == t.size());
84 assert(n.size() == d.size());
85 assert(n.size() == eta.size());
86 assert(n.size() == epsilon.size());
87 assert(n.size() == beta.size());
88 assert(n.size() == gamma.size());
89 assert(n.size() == b.size());
90 alphar.GaoB = ResidualHelmholtzGaoB(n, t, d, eta, beta, gamma, epsilon, b);
91 } else if (!type.compare("ResidualHelmholtzNonAnalytic")) {
92 if (alphar.NonAnalytic.N > 0) {
93 throw ValueError("Cannot add ");
94 }
95 std::vector<CoolPropDbl> n = cpjson::get_long_double_array(contribution["n"]);
96 std::vector<CoolPropDbl> a = cpjson::get_long_double_array(contribution["a"]);
97 std::vector<CoolPropDbl> b = cpjson::get_long_double_array(contribution["b"]);
98 std::vector<CoolPropDbl> beta = cpjson::get_long_double_array(contribution["beta"]);
99 std::vector<CoolPropDbl> A = cpjson::get_long_double_array(contribution["A"]);
100 std::vector<CoolPropDbl> B = cpjson::get_long_double_array(contribution["B"]);
101 std::vector<CoolPropDbl> C = cpjson::get_long_double_array(contribution["C"]);
102 std::vector<CoolPropDbl> D = cpjson::get_long_double_array(contribution["D"]);
103 assert(n.size() == a.size());
104 assert(n.size() == b.size());
105 assert(n.size() == beta.size());
106 assert(n.size() == A.size());
107 assert(n.size() == B.size());
108 assert(n.size() == C.size());
109 assert(n.size() == D.size());
110 alphar.NonAnalytic = ResidualHelmholtzNonAnalytic(n, a, b, beta, A, B, C, D);
111 } else if (!type.compare("ResidualHelmholtzLemmon2005")) {
112 std::vector<CoolPropDbl> n = cpjson::get_long_double_array(contribution["n"]);
113 std::vector<CoolPropDbl> d = cpjson::get_long_double_array(contribution["d"]);
114 std::vector<CoolPropDbl> t = cpjson::get_long_double_array(contribution["t"]);
115 std::vector<CoolPropDbl> l = cpjson::get_long_double_array(contribution["l"]);
116 std::vector<CoolPropDbl> m = cpjson::get_long_double_array(contribution["m"]);
117 assert(n.size() == d.size());
118 assert(n.size() == t.size());
119 assert(n.size() == l.size());
120 assert(n.size() == m.size());
121 alphar.GenExp.add_Lemmon2005(n, d, t, l, m);
122 } else if (!type.compare("ResidualHelmholtzDoubleExponential")) {
123 std::vector<CoolPropDbl> n = cpjson::get_long_double_array(contribution["n"]);
124 std::vector<CoolPropDbl> d = cpjson::get_long_double_array(contribution["d"]);
125 std::vector<CoolPropDbl> t = cpjson::get_long_double_array(contribution["t"]);
126 std::vector<CoolPropDbl> gt = cpjson::get_long_double_array(contribution["gt"]);
127 std::vector<CoolPropDbl> lt = cpjson::get_long_double_array(contribution["lt"]);
128 std::vector<CoolPropDbl> gd = cpjson::get_long_double_array(contribution["gd"]);
129 std::vector<CoolPropDbl> ld = cpjson::get_long_double_array(contribution["ld"]);
130
131 assert(n.size() == d.size());
132 assert(n.size() == t.size());
133 assert(n.size() == gt.size());
134 assert(n.size() == lt.size());
135 assert(n.size() == gd.size());
136 assert(n.size() == ld.size());
137 alphar.GenExp.add_DoubleExponential(n, d, t, gd, ld, gt, lt);
138 } else if (!type.compare("ResidualHelmholtzExponential")) {
139 std::vector<CoolPropDbl> n = cpjson::get_long_double_array(contribution["n"]);
140 std::vector<CoolPropDbl> d = cpjson::get_long_double_array(contribution["d"]);
141 std::vector<CoolPropDbl> t = cpjson::get_long_double_array(contribution["t"]);
142 std::vector<CoolPropDbl> g = cpjson::get_long_double_array(contribution["g"]);
143 std::vector<CoolPropDbl> l = cpjson::get_long_double_array(contribution["l"]);
144 assert(n.size() == d.size());
145 assert(n.size() == t.size());
146 assert(n.size() == g.size());
147 assert(n.size() == l.size());
148 alphar.GenExp.add_Exponential(n, d, t, g, l);
149 } else if (!type.compare("ResidualHelmholtzAssociating")) {
150 if (alphar.SAFT.disabled == false) {
151 throw ValueError("Cannot add ");
152 }
153 CoolPropDbl a = cpjson::get_double(contribution, "a");
154 CoolPropDbl m = cpjson::get_double(contribution, "m");
155 CoolPropDbl epsilonbar = cpjson::get_double(contribution, "epsilonbar");
156 CoolPropDbl vbarn = cpjson::get_double(contribution, "vbarn");
157 CoolPropDbl kappabar = cpjson::get_double(contribution, "kappabar");
158 alphar.SAFT = ResidualHelmholtzSAFTAssociating(a, m, epsilonbar, vbarn, kappabar);
159 } else {
160 throw ValueError(format("Unsupported Residual helmholtz type: %s", type.c_str()));
161 }
162 }
163
164 // Finish adding parts to the Generalized Exponential term, build other vectors
165 alphar.GenExp.finish();
166
167 return alphar;
168 };
169
171 static IdealHelmholtzContainer parse_alpha0(rapidjson::Value& jsonalpha0) {
172 if (!jsonalpha0.IsArray()) {
173 throw ValueError();
174 }
175
177
178 for (rapidjson::Value::ConstValueIterator itr = jsonalpha0.Begin(); itr != jsonalpha0.End(); ++itr) {
179 // A reference for code cleanness
180 const rapidjson::Value& contribution = *itr;
181
182 // Get the type (required!)
183 std::string type = contribution["type"].GetString();
184
185 if (!type.compare("IdealGasHelmholtzLead")) {
186 if (alpha0.Lead.is_enabled() == true) {
187 throw ValueError("Cannot add ");
188 }
189 CoolPropDbl a1 = cpjson::get_double(contribution, "a1");
190 CoolPropDbl a2 = cpjson::get_double(contribution, "a2");
191
192 alpha0.Lead = IdealHelmholtzLead(a1, a2);
193 } else if (!type.compare("IdealGasHelmholtzPower")) {
194 if (alpha0.Power.is_enabled() == true) {
195 throw ValueError("Cannot add ");
196 }
197 std::vector<CoolPropDbl> n = cpjson::get_long_double_array(contribution["n"]);
198 std::vector<CoolPropDbl> t = cpjson::get_long_double_array(contribution["t"]);
199
200 alpha0.Power = IdealHelmholtzPower(n, t);
201 } else if (!type.compare("IdealGasHelmholtzLogTau")) {
202 if (alpha0.LogTau.is_enabled() == true) {
203 throw ValueError("Cannot add ");
204 }
205 CoolPropDbl a = cpjson::get_double(contribution, "a");
206
207 alpha0.LogTau = IdealHelmholtzLogTau(a);
208 } else if (!type.compare("IdealGasHelmholtzPlanckEinsteinGeneralized")) {
209 // Retrieve the values
210 std::vector<CoolPropDbl> n = cpjson::get_long_double_array(contribution["n"]);
211 std::vector<CoolPropDbl> t = cpjson::get_long_double_array(contribution["t"]);
212
213 std::vector<CoolPropDbl> c = cpjson::get_long_double_array(contribution["c"]);
214 std::vector<CoolPropDbl> d = cpjson::get_long_double_array(contribution["d"]);
215
216 if (alpha0.PlanckEinstein.is_enabled() == true) {
217 alpha0.PlanckEinstein.extend(n, t, c, d);
218 } else {
220 }
221 } else if (!type.compare("IdealGasHelmholtzPlanckEinstein")) {
222 // Retrieve the values
223 std::vector<CoolPropDbl> n = cpjson::get_long_double_array(contribution["n"]);
224 std::vector<CoolPropDbl> t = cpjson::get_long_double_array(contribution["t"]);
225 // Flip the sign of theta
226 for (std::size_t i = 0; i < t.size(); ++i) {
227 t[i] *= -1;
228 }
229 std::vector<CoolPropDbl> c(n.size(), 1);
230 std::vector<CoolPropDbl> d(c.size(), -1);
231
232 if (alpha0.PlanckEinstein.is_enabled() == true) {
233 alpha0.PlanckEinstein.extend(n, t, c, d);
234 } else {
236 }
237 } else if (!type.compare("IdealGasHelmholtzPlanckEinsteinFunctionT")) {
238 // Retrieve the values
239 std::vector<CoolPropDbl> n = cpjson::get_long_double_array(contribution["n"]);
240 std::vector<CoolPropDbl> v = cpjson::get_long_double_array(contribution["v"]), theta(n.size(), 0.0);
241 // Calculate theta
242 double Tc = cpjson::get_double(contribution, "Tcrit");
243 for (std::size_t i = 0; i < v.size(); ++i) {
244 theta[i] = -v[i] / Tc;
245 }
246 std::vector<CoolPropDbl> c(n.size(), 1);
247 std::vector<CoolPropDbl> d(c.size(), -1);
248
249 if (alpha0.PlanckEinstein.is_enabled() == true) {
250 alpha0.PlanckEinstein.extend(n, theta, c, d);
251 } else {
253 }
254 } else if (!type.compare("IdealGasHelmholtzGERG2004Cosh")) {
255 // Retrieve the values
256 std::vector<CoolPropDbl> n = cpjson::get_long_double_array(contribution["n"]);
257 std::vector<CoolPropDbl> theta = cpjson::get_long_double_array(contribution["theta"]);
258 double Tc = cpjson::get_double(contribution, "Tcrit");
259 if (alpha0.GERG2004Cosh.is_enabled() == true) {
260 alpha0.GERG2004Cosh.extend(n, theta);
261 } else {
262 alpha0.GERG2004Cosh = IdealHelmholtzGERG2004Cosh(n, theta, Tc);
263 }
264 } else if (!type.compare("IdealGasHelmholtzGERG2004Sinh")) {
265 // Retrieve the values
266 std::vector<CoolPropDbl> n = cpjson::get_long_double_array(contribution["n"]);
267 std::vector<CoolPropDbl> theta = cpjson::get_long_double_array(contribution["theta"]);
268 double Tc = cpjson::get_double(contribution, "Tcrit");
269 if (alpha0.GERG2004Sinh.is_enabled() == true) {
270 alpha0.GERG2004Sinh.extend(n, theta);
271 } else {
272 alpha0.GERG2004Sinh = IdealHelmholtzGERG2004Sinh(n, theta, Tc);
273 }
274 } else if (!type.compare("IdealGasHelmholtzCP0Constant")) {
275 if (alpha0.CP0Constant.is_enabled() == true) {
276 throw ValueError("Cannot add another IdealGasHelmholtzCP0Constant term; join them together");
277 }
278 CoolPropDbl cp_over_R = cpjson::get_double(contribution, "cp_over_R");
279 CoolPropDbl Tc = cpjson::get_double(contribution, "Tc");
280 CoolPropDbl T0 = cpjson::get_double(contribution, "T0");
281 alpha0.CP0Constant = IdealHelmholtzCP0Constant(cp_over_R, Tc, T0);
282 } else if (!type.compare("IdealGasHelmholtzCP0PolyT")) {
283 if (alpha0.CP0PolyT.is_enabled() == true) {
284 throw ValueError("Cannot add another CP0PolyT term; join them together");
285 }
286 std::vector<CoolPropDbl> c = cpjson::get_long_double_array(contribution["c"]);
287 std::vector<CoolPropDbl> t = cpjson::get_long_double_array(contribution["t"]);
288 CoolPropDbl Tc = cpjson::get_double(contribution, "Tc");
289 CoolPropDbl T0 = cpjson::get_double(contribution, "T0");
290 alpha0.CP0PolyT = IdealHelmholtzCP0PolyT(c, t, Tc, T0);
291 } else if (!type.compare("IdealGasHelmholtzCP0AlyLee")) {
292
293 std::vector<CoolPropDbl> constants = cpjson::get_long_double_array(contribution["c"]);
294 CoolPropDbl Tc = cpjson::get_double(contribution, "Tc");
295 CoolPropDbl T0 = cpjson::get_double(contribution, "T0");
296
297 // Take the constant term if nonzero and set it as a polyT term
298 if (std::abs(constants[0]) > 1e-14) {
299 std::vector<CoolPropDbl> c(1, constants[0]), t(1, 0);
300 if (alpha0.CP0PolyT.is_enabled() == true) {
301 alpha0.CP0PolyT.extend(c, t);
302 } else {
303 alpha0.CP0PolyT = IdealHelmholtzCP0PolyT(c, t, Tc, T0);
304 }
305 }
306 std::vector<CoolPropDbl> n, c, d, t;
307 if (std::abs(constants[1]) > 1e-14) {
308 // sinh term can be converted by setting a_k = C, b_k = 2*D, c_k = -1, d_k = 1
309 n.push_back(constants[1]);
310 t.push_back(-2 * constants[2] / Tc);
311 c.push_back(1);
312 d.push_back(-1);
313 }
314 if (std::abs(constants[3]) > 1e-14) {
315 // cosh term can be converted by setting a_k = C, b_k = 2*D, c_k = 1, d_k = 1
316 n.push_back(-constants[3]);
317 t.push_back(-2 * constants[4] / Tc);
318 c.push_back(1);
319 d.push_back(1);
320 }
321 if (alpha0.PlanckEinstein.is_enabled() == true) {
322 alpha0.PlanckEinstein.extend(n, t, c, d);
323 } else {
325 }
326 } else if (!type.compare("IdealGasHelmholtzEnthalpyEntropyOffset")) {
327 CoolPropDbl a1 = cpjson::get_double(contribution, "a1");
328 CoolPropDbl a2 = cpjson::get_double(contribution, "a2");
329 std::string reference = cpjson::get_string(contribution, "reference");
330 alpha0.EnthalpyEntropyOffsetCore.set(a1, a2, reference);
331 } else {
332 std::cout << format("Unsupported ideal-gas Helmholtz type: %s\n", type.c_str());
333 //throw ValueError(format("Unsupported ideal-gas Helmholtz type: %s",type.c_str()));
334 }
335 }
336 return alpha0;
337 };
338
339 protected:
341 void parse_environmental(rapidjson::Value& json, CoolPropFluid& fluid) {
342 fluid.environment.ASHRAE34 = cpjson::get_string(json, "ASHRAE34");
343 fluid.environment.GWP20 = cpjson::get_double(json, "GWP20");
344 fluid.environment.GWP100 = cpjson::get_double(json, "GWP100");
345 fluid.environment.GWP500 = cpjson::get_double(json, "GWP500");
346 fluid.environment.HH = cpjson::get_double(json, "HH");
347 fluid.environment.FH = cpjson::get_double(json, "FH");
348 fluid.environment.PH = cpjson::get_double(json, "PH");
349 fluid.environment.ODP = cpjson::get_double(json, "ODP");
350 }
351
353 void parse_EOS(rapidjson::Value& EOS_json, CoolPropFluid& fluid) {
355 fluid.EOSVector.push_back(E);
356
357 EquationOfState& EOS = fluid.EOSVector.at(fluid.EOSVector.size() - 1);
358
359 // Universal gas constant [J/mol/K]
360 EOS.R_u = cpjson::get_double(EOS_json, "gas_constant");
361 EOS.molar_mass = cpjson::get_double(EOS_json, "molar_mass");
362 EOS.acentric = cpjson::get_double(EOS_json, "acentric");
363
364 EOS.pseudo_pure = cpjson::get_bool(EOS_json, "pseudo_pure");
365 EOS.limits.Tmax = cpjson::get_double(EOS_json, "T_max");
366 EOS.limits.pmax = cpjson::get_double(EOS_json, "p_max");
367
368 rapidjson::Value& reducing_state = EOS_json["STATES"]["reducing"];
369 rapidjson::Value& satminL_state = EOS_json["STATES"]["sat_min_liquid"];
370 rapidjson::Value& satminV_state = EOS_json["STATES"]["sat_min_vapor"];
371
372 // Reducing state
373 EOS.reduce.T = cpjson::get_double(reducing_state, "T");
374 EOS.reduce.rhomolar = cpjson::get_double(reducing_state, "rhomolar");
375 EOS.reduce.p = cpjson::get_double(reducing_state, "p");
376 EOS.reduce.hmolar = cpjson::get_double(reducing_state, "hmolar");
377 EOS.reduce.smolar = cpjson::get_double(reducing_state, "smolar");
378
379 EOS.sat_min_liquid.T = cpjson::get_double(satminL_state, "T");
380 EOS.sat_min_liquid.p = cpjson::get_double(satminL_state, "p");
381 EOS.sat_min_liquid.rhomolar = cpjson::get_double(satminL_state, "rhomolar");
382 EOS.sat_min_vapor.T = cpjson::get_double(satminV_state, "T");
383 EOS.sat_min_vapor.p = cpjson::get_double(satminV_state, "p");
384 EOS.sat_min_vapor.rhomolar = cpjson::get_double(satminV_state, "rhomolar");
385
387 EOS.limits.Tmin = cpjson::get_double(satminL_state, "T");
388 EOS.ptriple = cpjson::get_double(satminL_state, "p");
389 EOS.Ttriple = EOS.limits.Tmin;
390
391 // BibTex keys
392 EOS.BibTeX_EOS = cpjson::get_string(EOS_json, "BibTeX_EOS");
393 EOS.BibTeX_CP0 = cpjson::get_string(EOS_json, "BibTeX_CP0");
394
395 if (EOS_json.HasMember("SUPERANCILLARY")) {
396 if (getenv("COOLPROP_DISABLE_SUPERANCILLARIES_ENTIRELY")) {
397 } else {
398 // This is inefficient as we do JSON(rapidjson) -> string -> JSON(nlohmann)
399 // which implies two large parsing passes
400 EOS.set_superancillaries_str(cpjson::json2string(EOS_json["SUPERANCILLARY"]));
401 }
402 }
403
404 EOS.alphar = parse_alphar(EOS_json["alphar"]);
405 EOS.alpha0 = parse_alpha0(EOS_json["alpha0"]);
406
407 // Store the prefactor multipliying alpha0 if present
408 if (EOS_json.HasMember("alpha0_prefactor")) {
409 EOS.alpha0.set_prefactor(cpjson::get_double(EOS_json, "alpha0_prefactor"));
410 }
411 if (EOS_json["STATES"].HasMember("hs_anchor")) {
412 rapidjson::Value& hs_anchor = EOS_json["STATES"]["hs_anchor"];
413 EOS.hs_anchor.T = cpjson::get_double(hs_anchor, "T");
414 EOS.hs_anchor.p = cpjson::get_double(hs_anchor, "p");
415 EOS.hs_anchor.rhomolar = cpjson::get_double(hs_anchor, "rhomolar");
416 EOS.hs_anchor.hmolar = cpjson::get_double(hs_anchor, "hmolar");
417 EOS.hs_anchor.smolar = cpjson::get_double(hs_anchor, "smolar");
418 }
419
420 if (EOS_json["STATES"].HasMember("pressure_max_sat")) {
421 rapidjson::Value& s = EOS_json["STATES"]["pressure_max_sat"];
422 EOS.max_sat_p.T = cpjson::get_double(s, "T");
423 EOS.max_sat_p.p = cpjson::get_double(s, "p");
424 EOS.max_sat_p.rhomolar = cpjson::get_double(s, "rhomolar");
425 if (s.HasMember("hmolar")) {
426 EOS.max_sat_p.hmolar = cpjson::get_double(s, "hmolar");
427 EOS.max_sat_p.smolar = cpjson::get_double(s, "smolar");
428 }
429 }
430
431 if (EOS_json["STATES"].HasMember("temperature_max_sat")) {
432 rapidjson::Value& s = EOS_json["STATES"]["temperature_max_sat"];
433 EOS.max_sat_T.T = cpjson::get_double(s, "T");
434 EOS.max_sat_T.p = cpjson::get_double(s, "p");
435 EOS.max_sat_T.rhomolar = cpjson::get_double(s, "rhomolar");
436 if (s.HasMember("hmolar")) {
437 EOS.max_sat_T.hmolar = cpjson::get_double(s, "hmolar");
438 EOS.max_sat_T.smolar = cpjson::get_double(s, "smolar");
439 }
440 }
441
442 if (EOS_json.HasMember("critical_region_splines")) {
443 rapidjson::Value& spline = EOS_json["critical_region_splines"];
444 EOS.critical_region_splines.T_min = cpjson::get_double(spline, "T_min");
445 EOS.critical_region_splines.T_max = cpjson::get_double(spline, "T_max");
446 EOS.critical_region_splines.rhomolar_min = cpjson::get_double(spline, "rhomolar_min");
447 EOS.critical_region_splines.rhomolar_max = cpjson::get_double(spline, "rhomolar_max");
451 }
452
453 // Validate the equation of state that was just created
454 EOS.validate();
455 }
456
458 void parse_EOS_listing(rapidjson::Value& EOS_array, CoolPropFluid& fluid) {
459 for (rapidjson::Value::ValueIterator itr = EOS_array.Begin(); itr != EOS_array.End(); ++itr) {
460 parse_EOS(*itr, fluid);
461 }
462 };
463
465 void parse_dilute_viscosity(rapidjson::Value& dilute, CoolPropFluid& fluid) {
466 if (dilute.HasMember("hardcoded")) {
467 std::string target = cpjson::get_string(dilute, "hardcoded");
468 if (!target.compare("Ethane")) {
470 return;
471 } else if (!target.compare("Cyclohexane")) {
473 return;
474 } else if (!target.compare("CarbonDioxideLaeseckeJPCRD2017")) {
476 return;
477 } else {
478 throw ValueError(format("hardcoded dilute viscosity [%s] is not understood for fluid %s", target.c_str(), fluid.name.c_str()));
479 }
480 }
481 std::string type = cpjson::get_string(dilute, "type");
482 if (!type.compare("collision_integral")) {
483 // Get a reference to the entry in the fluid instance
485
486 // Set the type flag
488
489 // Load up the values
490 CI.a = cpjson::get_long_double_array(dilute["a"]);
491 CI.t = cpjson::get_long_double_array(dilute["t"]);
492 CI.molar_mass = cpjson::get_double(dilute, "molar_mass");
493 CI.C = cpjson::get_double(dilute, "C");
494 } else if (!type.compare("kinetic_theory")) {
496 } else if (!type.compare("powers_of_T")) {
497 // Get a reference to the entry in the fluid instance
499
500 // Load up the values
501 CI.a = cpjson::get_long_double_array(dilute["a"]);
502 CI.t = cpjson::get_long_double_array(dilute["t"]);
503
505 } else if (!type.compare("powers_of_Tr")) {
506 // Get a reference to the entry in the fluid instance
508 // Load up the values
509 CI.a = cpjson::get_long_double_array(dilute["a"]);
510 CI.t = cpjson::get_long_double_array(dilute["t"]);
511 CI.T_reducing = cpjson::get_double(dilute, "T_reducing");
513 } else if (!type.compare("collision_integral_powers_of_Tstar")) {
514 // Get a reference to the entry in the fluid instance
516
517 // Load up the values
518 CI.a = cpjson::get_long_double_array(dilute["a"]);
519 CI.t = cpjson::get_long_double_array(dilute["t"]);
520 CI.T_reducing = cpjson::get_double(dilute, "T_reducing");
521 CI.C = cpjson::get_double(dilute, "C");
522
524 } else {
525 throw ValueError(format("type [%s] is not understood for fluid %s", type.c_str(), fluid.name.c_str()));
526 }
527 };
528
530 void parse_initial_density_viscosity(rapidjson::Value& initial_density, CoolPropFluid& fluid) {
531 std::string type = cpjson::get_string(initial_density, "type");
532 if (!type.compare("Rainwater-Friend")) {
533 // Get a reference to the entry in the fluid instance
535
536 // Load up the values
537 RF.b = cpjson::get_long_double_array(initial_density["b"]);
538 RF.t = cpjson::get_long_double_array(initial_density["t"]);
539
540 // Set the type flag
542 } else if (!type.compare("empirical")) {
543 // Get a reference to the entry in the fluid instance
545
546 // Load up the values
547 EM.n = cpjson::get_long_double_array(initial_density["n"]);
548 EM.d = cpjson::get_long_double_array(initial_density["d"]);
549 EM.t = cpjson::get_long_double_array(initial_density["t"]);
550 EM.T_reducing = cpjson::get_double(initial_density, "T_reducing");
551 EM.rhomolar_reducing = cpjson::get_double(initial_density, "rhomolar_reducing");
552
553 // Set the type flag
555 } else {
556 throw ValueError(format("type [%s] is not understood for fluid %s", type.c_str(), fluid.name.c_str()));
557 }
558 };
559
561 void parse_higher_order_viscosity(rapidjson::Value& higher, CoolPropFluid& fluid) {
562 // First check for hardcoded higher-order term
563 if (higher.HasMember("hardcoded")) {
564 std::string target = cpjson::get_string(higher, "hardcoded");
565 if (!target.compare("Hydrogen")) {
567 return;
568 } else if (!target.compare("n-Hexane")) {
570 return;
571 } else if (!target.compare("n-Heptane")) {
573 return;
574 } else if (!target.compare("Toluene")) {
576 return;
577 } else if (!target.compare("Ethane")) {
579 return;
580 } else if (!target.compare("Benzene")) {
582 return;
583 } else if (!target.compare("CarbonDioxideLaeseckeJPCRD2017")) {
585 return;
586 } else {
587 throw ValueError(
588 format("hardcoded higher order viscosity term [%s] is not understood for fluid %s", target.c_str(), fluid.name.c_str()));
589 }
590 }
591
592 std::string type = cpjson::get_string(higher, "type");
593 if (!type.compare("modified_Batschinski_Hildebrand")) {
594 // Get a reference to the entry in the fluid instance to simplify the code that follows
596
597 // Set the flag for the type of this model
599
600 BH.T_reduce = cpjson::get_double(higher, "T_reduce");
601 BH.rhomolar_reduce = cpjson::get_double(higher, "rhomolar_reduce");
602 // Load up the values
603 BH.a = cpjson::get_long_double_array(higher["a"]);
604 BH.t1 = cpjson::get_long_double_array(higher["t1"]);
605 BH.d1 = cpjson::get_long_double_array(higher["d1"]);
606 BH.gamma = cpjson::get_long_double_array(higher["gamma"]);
607 BH.l = cpjson::get_long_double_array(higher["l"]);
608 assert(BH.a.size() == BH.t1.size());
609 assert(BH.a.size() == BH.d1.size());
610 assert(BH.a.size() == BH.gamma.size());
611 assert(BH.a.size() == BH.l.size());
612 BH.f = cpjson::get_long_double_array(higher["f"]);
613 BH.t2 = cpjson::get_long_double_array(higher["t2"]);
614 BH.d2 = cpjson::get_long_double_array(higher["d2"]);
615 assert(BH.f.size() == BH.t2.size());
616 assert(BH.f.size() == BH.d2.size());
617 BH.g = cpjson::get_long_double_array(higher["g"]);
618 BH.h = cpjson::get_long_double_array(higher["h"]);
619 assert(BH.g.size() == BH.h.size());
620 BH.p = cpjson::get_long_double_array(higher["p"]);
621 BH.q = cpjson::get_long_double_array(higher["q"]);
622 assert(BH.p.size() == BH.q.size());
623 } else if (!type.compare("friction_theory")) {
624 // Get a reference to the entry in the fluid instance to simplify the code that follows
626
627 // Set the flag for the type of this model
629
630 // Always need these terms
631 F.Ai = cpjson::get_long_double_array(higher["Ai"]);
632 F.Aa = cpjson::get_long_double_array(higher["Aa"]);
633 F.Aaa = cpjson::get_long_double_array(higher["Aaa"]);
634 F.Ar = cpjson::get_long_double_array(higher["Ar"]);
635
636 F.Na = cpjson::get_integer(higher, "Na");
637 F.Naa = cpjson::get_integer(higher, "Naa");
638 F.Nr = cpjson::get_integer(higher, "Nr");
639 F.Nrr = cpjson::get_integer(higher, "Nrr");
640 F.c1 = cpjson::get_double(higher, "c1");
641 F.c2 = cpjson::get_double(higher, "c2");
642 assert(F.Aa.size() == 3);
643 assert(F.Aaa.size() == 3);
644 assert(F.Ar.size() == 3);
645
646 F.T_reduce = cpjson::get_double(higher, "T_reduce");
647
648 if (higher.HasMember("Arr") && !higher.HasMember("Adrdr")) {
649 F.Arr = cpjson::get_long_double_array(higher["Arr"]);
650 assert(F.Arr.size() == 3);
651 } else if (higher.HasMember("Adrdr") && !higher.HasMember("Arr")) {
652 F.Adrdr = cpjson::get_long_double_array(higher["Adrdr"]);
653 assert(F.Adrdr.size() == 3);
654 } else {
655 throw ValueError(format("can only provide one of Arr or Adrdr for fluid %s", fluid.name.c_str()));
656 }
657 if (higher.HasMember("Aii")) {
658 F.Aii = cpjson::get_long_double_array(higher["Aii"]);
659 F.Nii = cpjson::get_integer(higher, "Nii");
660 }
661 if (higher.HasMember("Aaaa") && higher.HasMember("Arrr")) {
662 F.Aaaa = cpjson::get_long_double_array(higher["Aaaa"]);
663 F.Arrr = cpjson::get_long_double_array(higher["Arrr"]);
664 F.Naaa = cpjson::get_integer(higher, "Naaa");
665 F.Nrrr = cpjson::get_integer(higher, "Nrrr");
666 }
667
668 } else {
669 throw ValueError(format("type [%s] is not understood for fluid %s", type.c_str(), fluid.name.c_str()));
670 }
671 };
672
673 void parse_ECS_conductivity(rapidjson::Value& conductivity, CoolPropFluid& fluid) {
674 fluid.transport.conductivity_ecs.reference_fluid = cpjson::get_string(conductivity, "reference_fluid");
675
676 // Parameters for correction polynomials
677 fluid.transport.conductivity_ecs.psi_a = cpjson::get_long_double_array(conductivity["psi"]["a"]);
678 fluid.transport.conductivity_ecs.psi_t = cpjson::get_long_double_array(conductivity["psi"]["t"]);
679 fluid.transport.conductivity_ecs.psi_rhomolar_reducing = cpjson::get_double(conductivity["psi"], "rhomolar_reducing");
680 fluid.transport.conductivity_ecs.f_int_a = cpjson::get_long_double_array(conductivity["f_int"]["a"]);
681 fluid.transport.conductivity_ecs.f_int_t = cpjson::get_long_double_array(conductivity["f_int"]["t"]);
682 fluid.transport.conductivity_ecs.f_int_T_reducing = cpjson::get_double(conductivity["f_int"], "T_reducing");
683
685 }
686
687 void parse_ECS_viscosity(rapidjson::Value& viscosity, CoolPropFluid& fluid) {
688 fluid.transport.viscosity_ecs.reference_fluid = cpjson::get_string(viscosity, "reference_fluid");
689
690 // Parameters for correction polynomial
691 fluid.transport.viscosity_ecs.psi_a = cpjson::get_long_double_array(viscosity["psi"]["a"]);
692 fluid.transport.viscosity_ecs.psi_t = cpjson::get_long_double_array(viscosity["psi"]["t"]);
693 fluid.transport.viscosity_ecs.psi_rhomolar_reducing = cpjson::get_double(viscosity["psi"], "rhomolar_reducing");
694
695 fluid.transport.viscosity_using_ECS = true;
696 }
697
698 void parse_Chung_viscosity(rapidjson::Value& viscosity, CoolPropFluid& fluid) {
699 // These in base SI units
700 fluid.transport.viscosity_Chung.rhomolar_critical = cpjson::get_double(viscosity, "rhomolar_critical");
701 fluid.transport.viscosity_Chung.T_critical = cpjson::get_double(viscosity, "T_critical");
702 fluid.transport.viscosity_Chung.molar_mass = cpjson::get_double(viscosity, "molar_mass");
703 fluid.transport.viscosity_Chung.dipole_moment_D = cpjson::get_double(viscosity, "dipole_moment_D");
704 fluid.transport.viscosity_Chung.acentric = cpjson::get_double(viscosity, "acentric");
705 fluid.transport.viscosity_using_Chung = true;
706 }
707
708 void parse_rhosr_viscosity(rapidjson::Value& viscosity, CoolPropFluid& fluid) {
709 fluid.transport.viscosity_rhosr.C = cpjson::get_double(viscosity, "C");
710 fluid.transport.viscosity_rhosr.c_liq = cpjson::get_double_array(viscosity, "c_liq");
711 fluid.transport.viscosity_rhosr.c_vap = cpjson::get_double_array(viscosity, "c_vap");
712 fluid.transport.viscosity_rhosr.rhosr_critical = cpjson::get_double(viscosity, "rhosr_critical");
713 fluid.transport.viscosity_rhosr.x_crossover = cpjson::get_double(viscosity, "x_crossover");
714 fluid.transport.viscosity_using_rhosr = true;
715 }
716
718 void parse_viscosity(rapidjson::Value& viscosity, CoolPropFluid& fluid) {
719 // If an array, use the first one, and then stop;
720 if (viscosity.IsArray()) {
721 rapidjson::Value::ValueIterator itr = viscosity.Begin();
722 parse_viscosity(*itr, fluid);
723 return;
724 }
725
726 // Load the BibTeX key
727 fluid.transport.BibTeX_viscosity = cpjson::get_string(viscosity, "BibTeX");
728
729 // Set the Lennard-Jones 12-6 potential variables, or approximate them from method of Chung
730 if (!viscosity.HasMember("sigma_eta") || !viscosity.HasMember("epsilon_over_k")) {
731 default_transport(fluid);
732 } else {
733 fluid.transport.sigma_eta = cpjson::get_double(viscosity, "sigma_eta");
734 fluid.transport.epsilon_over_k = cpjson::get_double(viscosity, "epsilon_over_k");
735 }
736
737 // If it is using ECS, set ECS parameters and quit
738 if (viscosity.HasMember("type") && !cpjson::get_string(viscosity, "type").compare("ECS")) {
739 parse_ECS_viscosity(viscosity, fluid);
740 return;
741 }
742
743 // If it is using rho*sr CS, set parameters and quit
744 if (viscosity.HasMember("type") && !cpjson::get_string(viscosity, "type").compare("rhosr-CS")) {
745 parse_rhosr_viscosity(viscosity, fluid);
746 return;
747 }
748
749 // Use the method of Chung
750 // If it is using ECS, set ECS parameters and quit
751 if (viscosity.HasMember("type") && !cpjson::get_string(viscosity, "type").compare("Chung")) {
752 parse_Chung_viscosity(viscosity, fluid);
753 return;
754 }
755
756 if (viscosity.HasMember("hardcoded")) {
757 std::string target = cpjson::get_string(viscosity, "hardcoded");
758 if (!target.compare("Water")) {
760 return;
761 } else if (!target.compare("HeavyWater")) {
763 return;
764 } else if (!target.compare("Helium")) {
766 return;
767 } else if (!target.compare("R23")) {
769 return;
770 } else if (!target.compare("Methanol")) {
772 return;
773 } else if (!target.compare("m-Xylene")) {
775 return;
776 } else if (!target.compare("o-Xylene")) {
778 return;
779 } else if (!target.compare("p-Xylene")) {
781 return;
782 } else {
783 throw ValueError(format("hardcoded viscosity [%s] is not understood for fluid %s", target.c_str(), fluid.name.c_str()));
784 }
785 }
786
787 // Load dilute viscosity term
788 if (viscosity.HasMember("dilute")) {
789 parse_dilute_viscosity(viscosity["dilute"], fluid);
790 }
791 // Load initial density term
792 if (viscosity.HasMember("initial_density")) {
793 parse_initial_density_viscosity(viscosity["initial_density"], fluid);
794 }
795 // Load higher_order term
796 if (viscosity.HasMember("higher_order")) {
797 parse_higher_order_viscosity(viscosity["higher_order"], fluid);
798 }
799 };
800
802 void parse_dilute_conductivity(rapidjson::Value& dilute, CoolPropFluid& fluid) {
803 if (dilute.HasMember("hardcoded")) {
804 std::string target = cpjson::get_string(dilute, "hardcoded");
805 if (!target.compare("CO2")) {
807 return;
808 } else if (!target.compare("CarbonDioxideHuberJPCRD2016")) {
810 return;
811 } else if (!target.compare("Ethane")) {
813 return;
814 } else if (!target.compare("none")) {
816 return;
817 } else {
818 throw ValueError(
819 format("hardcoded dilute conductivity term [%s] is not understood for fluid %s", target.c_str(), fluid.name.c_str()));
820 }
821 }
822 std::string type = cpjson::get_string(dilute, "type");
823 if (!type.compare("ratio_of_polynomials")) {
824 // Get a reference to the entry in the fluid instance
826
827 // Set the type flag
829
830 // Load up the values
831 data.A = cpjson::get_long_double_array(dilute["A"]);
832 data.B = cpjson::get_long_double_array(dilute["B"]);
833 data.n = cpjson::get_long_double_array(dilute["n"]);
834 data.m = cpjson::get_long_double_array(dilute["m"]);
835 data.T_reducing = cpjson::get_double(dilute, "T_reducing");
836 } else if (!type.compare("eta0_and_poly")) {
837 // Get a reference to the entry in the fluid instance
839
840 // Set the type flag
842
843 // Load up the values
844 data.A = cpjson::get_long_double_array(dilute["A"]);
845 data.t = cpjson::get_long_double_array(dilute["t"]);
846 } else {
847 throw ValueError(format("type [%s] is not understood for fluid %s", type.c_str(), fluid.name.c_str()));
848 }
849 };
850
852 void parse_residual_conductivity(rapidjson::Value& dilute, CoolPropFluid& fluid) {
853 if (dilute.HasMember("hardcoded")) {
854 std::string target = cpjson::get_string(dilute, "hardcoded");
855 if (!target.compare("CO2")) {
857 return;
858 } else {
859 throw ValueError(
860 format("hardcoded residual conductivity term [%s] is not understood for fluid %s", target.c_str(), fluid.name.c_str()));
861 }
862 }
863 std::string type = cpjson::get_string(dilute, "type");
864 if (!type.compare("polynomial")) {
865 // Get a reference to the entry in the fluid instance
867
868 // Set the type flag
870
871 // Load up the values
872 data.B = cpjson::get_long_double_array(dilute["B"]);
873 data.d = cpjson::get_long_double_array(dilute["d"]);
874 data.t = cpjson::get_long_double_array(dilute["t"]);
875 data.T_reducing = cpjson::get_double(dilute, "T_reducing");
876 data.rhomass_reducing = cpjson::get_double(dilute, "rhomass_reducing");
877 } else if (!type.compare("polynomial_and_exponential")) {
878 // Get a reference to the entry in the fluid instance
880
881 // Set the type flag
883
884 // Load up the values
885 data.A = cpjson::get_long_double_array(dilute["A"]);
886 data.d = cpjson::get_long_double_array(dilute["d"]);
887 data.t = cpjson::get_long_double_array(dilute["t"]);
888 data.gamma = cpjson::get_long_double_array(dilute["gamma"]);
889 data.l = cpjson::get_long_double_array(dilute["l"]);
890 } else {
891 throw ValueError(format("type [%s] is not understood for fluid %s", type.c_str(), fluid.name.c_str()));
892 }
893 };
894
895 void parse_critical_conductivity(rapidjson::Value& critical, CoolPropFluid& fluid) {
896 if (critical.HasMember("hardcoded")) {
897 std::string target = cpjson::get_string(critical, "hardcoded");
898 if (!target.compare("R123")) {
900 return;
901 } else if (!target.compare("Ammonia")) {
903 return;
904 } else if (!target.compare("CarbonDioxideScalabrinJPCRD2006")) {
907 return;
908 } else if (!target.compare("None")) {
910 return;
911 } else {
912 throw ValueError(format("critical conductivity term [%s] is not understood for fluid %s", target.c_str(), fluid.name.c_str()));
913 }
914 }
915 std::string type = cpjson::get_string(critical, "type");
916 if (!type.compare("simplified_Olchowy_Sengers")) {
919
920 // Set the type flag
922
923 // Set values if they are found - otherwise fall back to default values
924 if (critical.HasMember("qD")) {
925 data.qD = cpjson::get_double(critical, "qD");
926 }
927 if (critical.HasMember("zeta0")) {
928 data.zeta0 = cpjson::get_double(critical, "zeta0");
929 }
930 if (critical.HasMember("GAMMA")) {
931 data.GAMMA = cpjson::get_double(critical, "GAMMA");
932 }
933 if (critical.HasMember("gamma")) {
934 data.gamma = cpjson::get_double(critical, "gamma");
935 }
936 if (critical.HasMember("R0")) {
937 data.R0 = cpjson::get_double(critical, "R0");
938 }
939 if (critical.HasMember("T_ref")) {
940 data.T_ref = cpjson::get_double(critical, "T_ref");
941 }
942 } else {
943 throw ValueError(format("type [%s] is not understood for fluid %s", type.c_str(), fluid.name.c_str()));
944 }
945 };
946
948 void parse_thermal_conductivity(rapidjson::Value& conductivity, CoolPropFluid& fluid) {
949 // Load the BibTeX key
950 fluid.transport.BibTeX_conductivity = cpjson::get_string(conductivity, "BibTeX");
951
952 // If it is using ECS, set ECS parameters and quit
953 if (conductivity.HasMember("type") && !cpjson::get_string(conductivity, "type").compare("ECS")) {
954 parse_ECS_conductivity(conductivity, fluid);
955 return;
956 }
957
958 if (conductivity.HasMember("hardcoded")) {
959 std::string target = cpjson::get_string(conductivity, "hardcoded");
960 if (!target.compare("Water")) {
962 return;
963 } else if (!target.compare("HeavyWater")) {
965 return;
966 } else if (!target.compare("Methane")) {
968 return;
969 } else if (!target.compare("R23")) {
971 return;
972 } else if (!target.compare("Helium")) {
974 return;
975 } else {
976 throw ValueError(
977 format("hardcoded residual conductivity term [%s] is not understood for fluid %s", target.c_str(), fluid.name.c_str()));
978 }
979 }
980
981 // Load dilute conductivity term
982 if (conductivity.HasMember("dilute")) {
983 parse_dilute_conductivity(conductivity["dilute"], fluid);
984 }
985 // Load residual conductivity term
986 if (conductivity.HasMember("residual")) {
987 parse_residual_conductivity(conductivity["residual"], fluid);
988 }
989 // Load critical conductivity term
990 if (conductivity.HasMember("critical")) {
991 parse_critical_conductivity(conductivity["critical"], fluid);
992 }
993 };
994
996 void parse_transport(rapidjson::Value& transport, CoolPropFluid& fluid) {
997
998 // Parse viscosity
999 if (transport.HasMember("viscosity")) {
1000 parse_viscosity(transport["viscosity"], fluid);
1002 }
1003
1004 // Parse thermal conductivity
1005 if (transport.HasMember("conductivity")) {
1006 parse_thermal_conductivity(transport["conductivity"], fluid);
1008 }
1009 };
1010
1012 // Use the method of Chung to approximate the values for epsilon_over_k and sigma_eta
1013 // Chung, T.-H.; Ajlan, M.; Lee, L. L.; Starling, K. E. Generalized Multiparameter Correlation for Nonpolar and Polar Fluid Transport Properties. Ind. Eng. Chem. Res. 1988, 27, 671-679.
1014 // rhoc needs to be in mol/L to yield a sigma in nm,
1015 CoolPropDbl rho_crit_molar = fluid.EOS().reduce.rhomolar / 1000.0; // [mol/m3 to mol/L]
1016 CoolPropDbl Tc = fluid.EOS().reduce.T;
1017 fluid.transport.sigma_eta = 0.809 / pow(rho_crit_molar, static_cast<CoolPropDbl>(1.0 / 3.0)) / 1e9; // 1e9 is to convert from nm to m
1018 fluid.transport.epsilon_over_k = Tc / 1.2593; // [K]
1019 }
1020
1021 void parse_melting_line(rapidjson::Value& melting_line, CoolPropFluid& fluid) {
1022 fluid.ancillaries.melting_line.T_m = cpjson::get_double(melting_line, "T_m");
1023 fluid.ancillaries.melting_line.BibTeX = cpjson::get_string(melting_line, "BibTeX");
1024
1025 if (melting_line.HasMember("type")) {
1026 std::string type = cpjson::get_string(melting_line, "type");
1027 if (!type.compare("Simon")) {
1028 rapidjson::Value& parts = melting_line["parts"];
1030 for (rapidjson::Value::ValueIterator itr = parts.Begin(); itr != parts.End(); ++itr) {
1032 data.a = cpjson::get_double((*itr), "a");
1033 data.c = cpjson::get_double((*itr), "c");
1034 data.T_min = cpjson::get_double((*itr), "T_min");
1035 data.T_max = cpjson::get_double((*itr), "T_max");
1036 data.T_0 = cpjson::get_double((*itr), "T_0");
1037 data.p_0 = cpjson::get_double((*itr), "p_0");
1038 fluid.ancillaries.melting_line.simon.parts.push_back(data);
1039 }
1040 } else if (!type.compare("polynomial_in_Tr")) {
1041 rapidjson::Value& parts = melting_line["parts"];
1043 for (rapidjson::Value::ValueIterator itr = parts.Begin(); itr != parts.End(); ++itr) {
1045 data.a = cpjson::get_long_double_array((*itr), "a");
1046 data.t = cpjson::get_long_double_array((*itr), "t");
1047 data.T_min = cpjson::get_double((*itr), "T_min");
1048 data.T_max = cpjson::get_double((*itr), "T_max");
1049 data.T_0 = cpjson::get_double((*itr), "T_0");
1050 data.p_0 = cpjson::get_double((*itr), "p_0");
1051 fluid.ancillaries.melting_line.polynomial_in_Tr.parts.push_back(data);
1052 }
1053 } else if (!type.compare("polynomial_in_Theta")) {
1054 rapidjson::Value& parts = melting_line["parts"];
1056 for (rapidjson::Value::ValueIterator itr = parts.Begin(); itr != parts.End(); ++itr) {
1058 data.a = cpjson::get_long_double_array((*itr), "a");
1059 data.t = cpjson::get_long_double_array((*itr), "t");
1060 data.T_min = cpjson::get_double((*itr), "T_min");
1061 data.T_max = cpjson::get_double((*itr), "T_max");
1062 data.T_0 = cpjson::get_double((*itr), "T_0");
1063 data.p_0 = cpjson::get_double((*itr), "p_0");
1064 fluid.ancillaries.melting_line.polynomial_in_Theta.parts.push_back(data);
1065 }
1066 } else {
1067 throw ValueError(format("melting line type [%s] is not understood for fluid %s", type.c_str(), fluid.name.c_str()));
1068 }
1069 // Set the limits for the melting line curve
1071 } else {
1072 throw ValueError(format("melting line does not have \"type\" for fluid %s", fluid.name.c_str()));
1073 }
1074 };
1075
1077 void parse_states(rapidjson::Value& states, CoolPropFluid& fluid) {
1078 if (!states.HasMember("critical")) {
1079 throw ValueError(format("fluid[\"STATES\"] [%s] does not have \"critical\" member", fluid.name.c_str()));
1080 }
1081 rapidjson::Value& crit = states["critical"];
1082 fluid.crit.T = cpjson::get_double(crit, "T");
1083 fluid.crit.p = cpjson::get_double(crit, "p");
1084 fluid.crit.rhomolar = cpjson::get_double(crit, "rhomolar");
1085 fluid.crit.hmolar = cpjson::get_double(crit, "hmolar");
1086 fluid.crit.smolar = cpjson::get_double(crit, "smolar");
1087
1088 if (!states.HasMember("triple_liquid")) {
1089 throw ValueError(format("fluid[\"STATES\"] [%s] does not have \"triple_liquid\" member", fluid.name.c_str()));
1090 }
1091 rapidjson::Value& triple_liquid = states["triple_liquid"];
1092 if (triple_liquid.ObjectEmpty()) {
1093 // State is empty - probably because the triple point temperature is below the minimum saturation temperature
1094 fluid.triple_liquid.T = -1;
1095 fluid.triple_liquid.p = -1;
1096 fluid.triple_liquid.rhomolar = -1;
1097 fluid.triple_liquid.hmolar = _HUGE;
1098 fluid.triple_liquid.smolar = _HUGE;
1099 } else {
1100 fluid.triple_liquid.T = cpjson::get_double(triple_liquid, "T");
1101 fluid.triple_liquid.p = cpjson::get_double(triple_liquid, "p");
1102 fluid.triple_liquid.rhomolar = cpjson::get_double(triple_liquid, "rhomolar");
1103 fluid.triple_liquid.hmolar = cpjson::get_double(triple_liquid, "hmolar");
1104 fluid.triple_liquid.smolar = cpjson::get_double(triple_liquid, "smolar");
1105 }
1106
1107 if (!states.HasMember("triple_vapor")) {
1108 throw ValueError(format("fluid[\"STATES\"] [%s] does not have \"triple_vapor\" member", fluid.name.c_str()));
1109 }
1110 rapidjson::Value& triple_vapor = states["triple_vapor"];
1111 if (triple_vapor.ObjectEmpty()) {
1112 // State is empty - probably because the triple point temperature is below the minimum saturation temperature
1113 fluid.triple_vapor.T = -1;
1114 fluid.triple_vapor.p = -1;
1115 fluid.triple_vapor.rhomolar = -1;
1116 fluid.triple_vapor.hmolar = _HUGE;
1117 fluid.triple_vapor.smolar = _HUGE;
1118 } else {
1119 fluid.triple_vapor.T = cpjson::get_double(triple_vapor, "T");
1120 fluid.triple_vapor.p = cpjson::get_double(triple_vapor, "p");
1121 fluid.triple_vapor.rhomolar = cpjson::get_double(triple_vapor, "rhomolar");
1122 fluid.triple_vapor.hmolar = cpjson::get_double(triple_vapor, "hmolar");
1123 fluid.triple_vapor.smolar = cpjson::get_double(triple_vapor, "smolar");
1124 }
1125 };
1126
1128 void parse_ancillaries(rapidjson::Value& ancillaries, CoolPropFluid& fluid) {
1129 if (!ancillaries.HasMember("rhoL") || !ancillaries.HasMember("rhoV")) {
1130 throw ValueError("Ancillary curves for either rhoL or rhoV are missing");
1131 }
1132 fluid.ancillaries.rhoL = SaturationAncillaryFunction(ancillaries["rhoL"]);
1133 fluid.ancillaries.rhoV = SaturationAncillaryFunction(ancillaries["rhoV"]);
1134
1135 // If a pseudo-pure fluid, has pL and pV curves
1136 if (ancillaries.HasMember("pL") && ancillaries.HasMember("pV")) {
1137 fluid.ancillaries.pL = SaturationAncillaryFunction(ancillaries["pL"]);
1138 fluid.ancillaries.pV = SaturationAncillaryFunction(ancillaries["pV"]);
1139 }
1140 // Otherwise has a single pS curve and not pL and not pV
1141 else if (!ancillaries.HasMember("pL") && !ancillaries.HasMember("pV") && ancillaries.HasMember("pS")) {
1142 fluid.ancillaries.pL = SaturationAncillaryFunction(ancillaries["pS"]);
1143 fluid.ancillaries.pV = SaturationAncillaryFunction(ancillaries["pS"]);
1144 } else {
1145 throw ValueError("Pressure ancillary curves are missing or invalid");
1146 }
1147
1148 if (ancillaries.HasMember("hL")) {
1149 fluid.ancillaries.hL = SaturationAncillaryFunction(ancillaries["hL"]);
1150 } else {
1151 if (get_debug_level() > 0) {
1152 std::cout << "Missing hL ancillary for fluid " << fluid.name;
1153 }
1154 }
1155 if (ancillaries.HasMember("hLV")) {
1156 fluid.ancillaries.hLV = SaturationAncillaryFunction(ancillaries["hLV"]);
1157 } else {
1158 if (get_debug_level() > 0) {
1159 std::cout << "Missing hLV ancillary for fluid " << fluid.name;
1160 }
1161 }
1162
1163 if (ancillaries.HasMember("sL")) {
1164 fluid.ancillaries.sL = SaturationAncillaryFunction(ancillaries["sL"]);
1165 } else {
1166 if (get_debug_level() > 0) {
1167 std::cout << "Missing sL ancillary for fluid " << fluid.name;
1168 }
1169 }
1170 if (ancillaries.HasMember("sLV")) {
1171 fluid.ancillaries.sLV = SaturationAncillaryFunction(ancillaries["sLV"]);
1172 } else {
1173 if (get_debug_level() > 0) {
1174 std::cout << "Missing sLV ancillary for fluid " << fluid.name;
1175 }
1176 }
1177 if (!ValidNumber(fluid.ancillaries.sL.get_Tmin()) && get_debug_level() > 0) {
1178 std::cout << "Tmin invalid for sL for " << fluid.name << std::endl;
1179 }
1180 };
1181
1183 void parse_surface_tension(rapidjson::Value& surface_tension, CoolPropFluid& fluid) {
1184 fluid.ancillaries.surface_tension = SurfaceTensionCorrelation(surface_tension);
1185 };
1186
1189 assert(fluid.EOSVector.size() > 0);
1190 assert(fluid.CAS.length() > 0);
1191 assert(fluid.name.length() > 0);
1192 }
1193
1194 public:
1195 // Default constructor;
1197 _is_empty = true;
1198 };
1199 bool is_empty(void) {
1200 return _is_empty;
1201 };
1202
1204 static void add_many(const std::string& JSON_string);
1205
1207 void add_many(rapidjson::Value& listing);
1208
1209 void add_one(rapidjson::Value& fluid_json);
1210
1211 std::string get_JSONstring(const std::string& key) {
1212 // Try to find it
1213 std::map<std::string, std::size_t>::const_iterator it = string_to_index_map.find(key);
1214 if (it != string_to_index_map.end()) {
1215
1216 std::map<std::size_t, std::string>::const_iterator it2 = JSONstring_map.find(it->second);
1217 if (it2 != JSONstring_map.end()) {
1218 // Then, load the fluids we would like to add
1219 rapidjson::Document doc;
1220 cpjson::JSON_string_to_rapidjson(it2->second, doc);
1221 rapidjson::Document doc2;
1222 doc2.SetArray();
1223 doc2.PushBack(doc, doc.GetAllocator());
1224 return cpjson::json2string(doc2);
1225 } else {
1226 throw ValueError(format("Unable to obtain JSON string for this identifier [%d]", it->second));
1227 }
1228 } else {
1229 throw ValueError(format("Unable to obtain index for this identifier [%s]", key.c_str()));
1230 }
1231 }
1232
1234
1237 CoolPropFluid get(const std::string& key) {
1238 // Try to find it
1239 std::map<std::string, std::size_t>::const_iterator it = string_to_index_map.find(key);
1240 // If it is found
1241 if (it != string_to_index_map.end()) {
1242 return get(it->second);
1243 } else {
1244 // Here we check for the use of a cubic Helmholtz energy transformation for a multi-fluid model
1245 std::vector<std::string> endings;
1246 endings.push_back("-SRK");
1247 endings.push_back("-PengRobinson");
1248 for (std::vector<std::string>::const_iterator end = endings.begin(); end != endings.end(); ++end) {
1249 if (endswith(key, *end)) {
1250 std::string used_name = key.substr(0, key.size() - (*end).size());
1251 it = string_to_index_map.find(used_name);
1252 if (it != string_to_index_map.end()) {
1253 // We found the name of the fluid within the library of multiparameter
1254 // Helmholtz-explicit models. We will load its parameters from the
1255 // multiparameter EOS
1256 //
1257 CoolPropFluid fluid = get(it->second);
1258 // Remove all the residual contributions to the Helmholtz energy
1259 fluid.EOSVector[0].alphar.empty_the_EOS();
1260 // Get the parameters for the cubic EOS
1261 CoolPropDbl Tc = fluid.EOSVector[0].reduce.T;
1262 CoolPropDbl pc = fluid.EOSVector[0].reduce.p;
1263 CoolPropDbl rhomolarc = fluid.EOSVector[0].reduce.rhomolar;
1264 CoolPropDbl acentric = fluid.EOSVector[0].acentric;
1265 CoolPropDbl R = 8.3144598; // fluid.EOSVector[0].R_u;
1266 // Set the cubic contribution to the residual Helmholtz energy
1267 shared_ptr<AbstractCubic> ac;
1268 if (*end == "-SRK") {
1269 ac.reset(new SRK(Tc, pc, acentric, R));
1270 } else if (*end == "-PengRobinson") {
1271 ac.reset(new PengRobinson(Tc, pc, acentric, R));
1272 } else {
1273 throw CoolProp::ValueError(format("Unable to match this ending [%s]", (*end).c_str()));
1274 }
1275 ac->set_Tr(Tc);
1276 ac->set_rhor(rhomolarc);
1277 fluid.EOSVector[0].alphar.cubic = ResidualHelmholtzGeneralizedCubic(ac);
1278 return fluid;
1279 } else {
1280 // Let's look in the library of cubic EOS
1282 // Set the cubic contribution to the residual Helmholtz energy
1283 shared_ptr<AbstractCubic> ac;
1284 if (*end == "-SRK") {
1285 ac.reset(new SRK(vals.Tc, vals.pc, vals.acentric, get_config_double(R_U_CODATA)));
1286 } else if (*end == "-PengRobinson") {
1287 ac.reset(new PengRobinson(vals.Tc, vals.pc, vals.acentric, get_config_double(R_U_CODATA)));
1288 } else {
1289 throw CoolProp::ValueError(format("Unable to match this ending [%s]", (*end).c_str()));
1290 }
1291 ac->set_Tr(vals.Tc);
1292 if (vals.rhomolarc > 0) {
1293 ac->set_rhor(vals.rhomolarc);
1294 } else {
1295 // Curve fit from all the pure fluids in CoolProp (thanks to recommendation of A. Kazakov)
1296 double v_c_Lmol = 2.14107171795 * (vals.Tc / vals.pc * 1000) + 0.00773144012514; // [L/mol]
1297 ac->set_rhor(1 / (v_c_Lmol / 1000.0));
1298 }
1299 if (vals.alpha_type == "Twu") {
1300 std::vector<double>& c = vals.alpha_coeffs;
1301 ac->set_C_Twu(0, c[0], c[1], c[2]);
1302 }
1303 CoolPropFluid fluid;
1304 fluid.CAS = vals.CAS;
1306 E.acentric = vals.acentric;
1307 E.sat_min_liquid.T = _HUGE;
1308 E.sat_min_liquid.p = _HUGE;
1309 E.reduce.T = vals.Tc;
1310 E.reduce.p = vals.pc;
1311 E.reduce.rhomolar = ac->get_rhor();
1312 fluid.EOSVector.push_back(E);
1314 fluid.EOS().alpha0 = vals.alpha0;
1315 fluid.crit.T = vals.Tc;
1316 fluid.crit.p = vals.pc;
1317 fluid.crit.rhomolar = ac->get_rhor();
1318
1319 return fluid;
1320 }
1321 }
1322 }
1323 throw ValueError(format("key [%s] was not found in string_to_index_map in JSONFluidLibrary", key.c_str()));
1324 }
1325 };
1326
1328
1331 CoolPropFluid get(std::size_t key) {
1332 // Try to find it
1333 std::map<std::size_t, CoolPropFluid>::iterator it = fluid_map.find(key);
1334 // If it is found
1335 if (it != fluid_map.end()) {
1336 return it->second;
1337 } else {
1338 throw ValueError(format("key [%d] was not found in JSONFluidLibrary", key));
1339 }
1340 };
1341 void set_fluid_enthalpy_entropy_offset(const std::string& fluid, double delta_a1, double delta_a2, const std::string& ref);
1343 std::string get_fluid_list(void) {
1344 return strjoin(name_vector, get_config_string(LIST_STRING_DELIMITER));
1345 };
1346};
1347
1349JSONFluidLibrary& get_library(void);
1350
1352std::string get_fluid_list(void);
1353
1355CoolPropFluid get_fluid(const std::string& fluid_string);
1356
1358std::string get_fluid_as_JSONstring(const std::string& indentifier);
1359
1361void set_fluid_enthalpy_entropy_offset(const std::string& fluid, double delta_a1, double delta_a2, const std::string& ref);
1362
1363} /* namespace CoolProp */
1364#endif