CoolProp 6.8.1dev
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");
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 // This is inefficient as we do JSON(rapidjson) -> string -> JSON(nlohmann)
397 // which implies two large parsing passes
398 EOS.set_superancillaries_str(cpjson::json2string(EOS_json["SUPERANCILLARY"]));
399 }
400
401 EOS.alphar = parse_alphar(EOS_json["alphar"]);
402 EOS.alpha0 = parse_alpha0(EOS_json["alpha0"]);
403
404 // Store the prefactor multipliying alpha0 if present
405 if (EOS_json.HasMember("alpha0_prefactor")) {
406 EOS.alpha0.set_prefactor(cpjson::get_double(EOS_json, "alpha0_prefactor"));
407 }
408 if (EOS_json["STATES"].HasMember("hs_anchor")) {
409 rapidjson::Value& hs_anchor = EOS_json["STATES"]["hs_anchor"];
410 EOS.hs_anchor.T = cpjson::get_double(hs_anchor, "T");
411 EOS.hs_anchor.p = cpjson::get_double(hs_anchor, "p");
412 EOS.hs_anchor.rhomolar = cpjson::get_double(hs_anchor, "rhomolar");
413 EOS.hs_anchor.hmolar = cpjson::get_double(hs_anchor, "hmolar");
414 EOS.hs_anchor.smolar = cpjson::get_double(hs_anchor, "smolar");
415 }
416
417 if (EOS_json["STATES"].HasMember("pressure_max_sat")) {
418 rapidjson::Value& s = EOS_json["STATES"]["pressure_max_sat"];
419 EOS.max_sat_p.T = cpjson::get_double(s, "T");
420 EOS.max_sat_p.p = cpjson::get_double(s, "p");
421 EOS.max_sat_p.rhomolar = cpjson::get_double(s, "rhomolar");
422 if (s.HasMember("hmolar")) {
423 EOS.max_sat_p.hmolar = cpjson::get_double(s, "hmolar");
424 EOS.max_sat_p.smolar = cpjson::get_double(s, "smolar");
425 }
426 }
427
428 if (EOS_json["STATES"].HasMember("temperature_max_sat")) {
429 rapidjson::Value& s = EOS_json["STATES"]["temperature_max_sat"];
430 EOS.max_sat_T.T = cpjson::get_double(s, "T");
431 EOS.max_sat_T.p = cpjson::get_double(s, "p");
432 EOS.max_sat_T.rhomolar = cpjson::get_double(s, "rhomolar");
433 if (s.HasMember("hmolar")) {
434 EOS.max_sat_T.hmolar = cpjson::get_double(s, "hmolar");
435 EOS.max_sat_T.smolar = cpjson::get_double(s, "smolar");
436 }
437 }
438
439 if (EOS_json.HasMember("critical_region_splines")) {
440 rapidjson::Value& spline = EOS_json["critical_region_splines"];
441 EOS.critical_region_splines.T_min = cpjson::get_double(spline, "T_min");
442 EOS.critical_region_splines.T_max = cpjson::get_double(spline, "T_max");
443 EOS.critical_region_splines.rhomolar_min = cpjson::get_double(spline, "rhomolar_min");
444 EOS.critical_region_splines.rhomolar_max = cpjson::get_double(spline, "rhomolar_max");
448 }
449
450 // Validate the equation of state that was just created
451 EOS.validate();
452 }
453
455 void parse_EOS_listing(rapidjson::Value& EOS_array, CoolPropFluid& fluid) {
456 for (rapidjson::Value::ValueIterator itr = EOS_array.Begin(); itr != EOS_array.End(); ++itr) {
457 parse_EOS(*itr, fluid);
458 }
459 };
460
462 void parse_dilute_viscosity(rapidjson::Value& dilute, CoolPropFluid& fluid) {
463 if (dilute.HasMember("hardcoded")) {
464 std::string target = cpjson::get_string(dilute, "hardcoded");
465 if (!target.compare("Ethane")) {
467 return;
468 } else if (!target.compare("Cyclohexane")) {
470 return;
471 } else if (!target.compare("CarbonDioxideLaeseckeJPCRD2017")) {
473 return;
474 } else {
475 throw ValueError(format("hardcoded dilute viscosity [%s] is not understood for fluid %s", target.c_str(), fluid.name.c_str()));
476 }
477 }
478 std::string type = cpjson::get_string(dilute, "type");
479 if (!type.compare("collision_integral")) {
480 // Get a reference to the entry in the fluid instance
482
483 // Set the type flag
485
486 // Load up the values
487 CI.a = cpjson::get_long_double_array(dilute["a"]);
488 CI.t = cpjson::get_long_double_array(dilute["t"]);
489 CI.molar_mass = cpjson::get_double(dilute, "molar_mass");
490 CI.C = cpjson::get_double(dilute, "C");
491 } else if (!type.compare("kinetic_theory")) {
493 } else if (!type.compare("powers_of_T")) {
494 // Get a reference to the entry in the fluid instance
496
497 // Load up the values
498 CI.a = cpjson::get_long_double_array(dilute["a"]);
499 CI.t = cpjson::get_long_double_array(dilute["t"]);
500
502 } else if (!type.compare("powers_of_Tr")) {
503 // Get a reference to the entry in the fluid instance
505 // Load up the values
506 CI.a = cpjson::get_long_double_array(dilute["a"]);
507 CI.t = cpjson::get_long_double_array(dilute["t"]);
508 CI.T_reducing = cpjson::get_double(dilute, "T_reducing");
510 } else if (!type.compare("collision_integral_powers_of_Tstar")) {
511 // Get a reference to the entry in the fluid instance
513
514 // Load up the values
515 CI.a = cpjson::get_long_double_array(dilute["a"]);
516 CI.t = cpjson::get_long_double_array(dilute["t"]);
517 CI.T_reducing = cpjson::get_double(dilute, "T_reducing");
518 CI.C = cpjson::get_double(dilute, "C");
519
521 } else {
522 throw ValueError(format("type [%s] is not understood for fluid %s", type.c_str(), fluid.name.c_str()));
523 }
524 };
525
527 void parse_initial_density_viscosity(rapidjson::Value& initial_density, CoolPropFluid& fluid) {
528 std::string type = cpjson::get_string(initial_density, "type");
529 if (!type.compare("Rainwater-Friend")) {
530 // Get a reference to the entry in the fluid instance
532
533 // Load up the values
534 RF.b = cpjson::get_long_double_array(initial_density["b"]);
535 RF.t = cpjson::get_long_double_array(initial_density["t"]);
536
537 // Set the type flag
539 } else if (!type.compare("empirical")) {
540 // Get a reference to the entry in the fluid instance
542
543 // Load up the values
544 EM.n = cpjson::get_long_double_array(initial_density["n"]);
545 EM.d = cpjson::get_long_double_array(initial_density["d"]);
546 EM.t = cpjson::get_long_double_array(initial_density["t"]);
547 EM.T_reducing = cpjson::get_double(initial_density, "T_reducing");
548 EM.rhomolar_reducing = cpjson::get_double(initial_density, "rhomolar_reducing");
549
550 // Set the type flag
552 } else {
553 throw ValueError(format("type [%s] is not understood for fluid %s", type.c_str(), fluid.name.c_str()));
554 }
555 };
556
558 void parse_higher_order_viscosity(rapidjson::Value& higher, CoolPropFluid& fluid) {
559 // First check for hardcoded higher-order term
560 if (higher.HasMember("hardcoded")) {
561 std::string target = cpjson::get_string(higher, "hardcoded");
562 if (!target.compare("Hydrogen")) {
564 return;
565 } else if (!target.compare("n-Hexane")) {
567 return;
568 } else if (!target.compare("n-Heptane")) {
570 return;
571 } else if (!target.compare("Toluene")) {
573 return;
574 } else if (!target.compare("Ethane")) {
576 return;
577 } else if (!target.compare("Benzene")) {
579 return;
580 } else if (!target.compare("CarbonDioxideLaeseckeJPCRD2017")) {
582 return;
583 } else {
584 throw ValueError(
585 format("hardcoded higher order viscosity term [%s] is not understood for fluid %s", target.c_str(), fluid.name.c_str()));
586 }
587 }
588
589 std::string type = cpjson::get_string(higher, "type");
590 if (!type.compare("modified_Batschinski_Hildebrand")) {
591 // Get a reference to the entry in the fluid instance to simplify the code that follows
593
594 // Set the flag for the type of this model
596
597 BH.T_reduce = cpjson::get_double(higher, "T_reduce");
598 BH.rhomolar_reduce = cpjson::get_double(higher, "rhomolar_reduce");
599 // Load up the values
600 BH.a = cpjson::get_long_double_array(higher["a"]);
601 BH.t1 = cpjson::get_long_double_array(higher["t1"]);
602 BH.d1 = cpjson::get_long_double_array(higher["d1"]);
603 BH.gamma = cpjson::get_long_double_array(higher["gamma"]);
604 BH.l = cpjson::get_long_double_array(higher["l"]);
605 assert(BH.a.size() == BH.t1.size());
606 assert(BH.a.size() == BH.d1.size());
607 assert(BH.a.size() == BH.gamma.size());
608 assert(BH.a.size() == BH.l.size());
609 BH.f = cpjson::get_long_double_array(higher["f"]);
610 BH.t2 = cpjson::get_long_double_array(higher["t2"]);
611 BH.d2 = cpjson::get_long_double_array(higher["d2"]);
612 assert(BH.f.size() == BH.t2.size());
613 assert(BH.f.size() == BH.d2.size());
614 BH.g = cpjson::get_long_double_array(higher["g"]);
615 BH.h = cpjson::get_long_double_array(higher["h"]);
616 assert(BH.g.size() == BH.h.size());
617 BH.p = cpjson::get_long_double_array(higher["p"]);
618 BH.q = cpjson::get_long_double_array(higher["q"]);
619 assert(BH.p.size() == BH.q.size());
620 } else if (!type.compare("friction_theory")) {
621 // Get a reference to the entry in the fluid instance to simplify the code that follows
623
624 // Set the flag for the type of this model
626
627 // Always need these terms
628 F.Ai = cpjson::get_long_double_array(higher["Ai"]);
629 F.Aa = cpjson::get_long_double_array(higher["Aa"]);
630 F.Aaa = cpjson::get_long_double_array(higher["Aaa"]);
631 F.Ar = cpjson::get_long_double_array(higher["Ar"]);
632
633 F.Na = cpjson::get_integer(higher, "Na");
634 F.Naa = cpjson::get_integer(higher, "Naa");
635 F.Nr = cpjson::get_integer(higher, "Nr");
636 F.Nrr = cpjson::get_integer(higher, "Nrr");
637 F.c1 = cpjson::get_double(higher, "c1");
638 F.c2 = cpjson::get_double(higher, "c2");
639 assert(F.Aa.size() == 3);
640 assert(F.Aaa.size() == 3);
641 assert(F.Ar.size() == 3);
642
643 F.T_reduce = cpjson::get_double(higher, "T_reduce");
644
645 if (higher.HasMember("Arr") && !higher.HasMember("Adrdr")) {
646 F.Arr = cpjson::get_long_double_array(higher["Arr"]);
647 assert(F.Arr.size() == 3);
648 } else if (higher.HasMember("Adrdr") && !higher.HasMember("Arr")) {
649 F.Adrdr = cpjson::get_long_double_array(higher["Adrdr"]);
650 assert(F.Adrdr.size() == 3);
651 } else {
652 throw ValueError(format("can only provide one of Arr or Adrdr for fluid %s", fluid.name.c_str()));
653 }
654 if (higher.HasMember("Aii")) {
655 F.Aii = cpjson::get_long_double_array(higher["Aii"]);
656 F.Nii = cpjson::get_integer(higher, "Nii");
657 }
658 if (higher.HasMember("Aaaa") && higher.HasMember("Arrr")) {
659 F.Aaaa = cpjson::get_long_double_array(higher["Aaaa"]);
660 F.Arrr = cpjson::get_long_double_array(higher["Arrr"]);
661 F.Naaa = cpjson::get_integer(higher, "Naaa");
662 F.Nrrr = cpjson::get_integer(higher, "Nrrr");
663 }
664
665 } else {
666 throw ValueError(format("type [%s] is not understood for fluid %s", type.c_str(), fluid.name.c_str()));
667 }
668 };
669
670 void parse_ECS_conductivity(rapidjson::Value& conductivity, CoolPropFluid& fluid) {
671 fluid.transport.conductivity_ecs.reference_fluid = cpjson::get_string(conductivity, "reference_fluid");
672
673 // Parameters for correction polynomials
674 fluid.transport.conductivity_ecs.psi_a = cpjson::get_long_double_array(conductivity["psi"]["a"]);
675 fluid.transport.conductivity_ecs.psi_t = cpjson::get_long_double_array(conductivity["psi"]["t"]);
676 fluid.transport.conductivity_ecs.psi_rhomolar_reducing = cpjson::get_double(conductivity["psi"], "rhomolar_reducing");
677 fluid.transport.conductivity_ecs.f_int_a = cpjson::get_long_double_array(conductivity["f_int"]["a"]);
678 fluid.transport.conductivity_ecs.f_int_t = cpjson::get_long_double_array(conductivity["f_int"]["t"]);
679 fluid.transport.conductivity_ecs.f_int_T_reducing = cpjson::get_double(conductivity["f_int"], "T_reducing");
680
682 }
683
684 void parse_ECS_viscosity(rapidjson::Value& viscosity, CoolPropFluid& fluid) {
685 fluid.transport.viscosity_ecs.reference_fluid = cpjson::get_string(viscosity, "reference_fluid");
686
687 // Parameters for correction polynomial
688 fluid.transport.viscosity_ecs.psi_a = cpjson::get_long_double_array(viscosity["psi"]["a"]);
689 fluid.transport.viscosity_ecs.psi_t = cpjson::get_long_double_array(viscosity["psi"]["t"]);
690 fluid.transport.viscosity_ecs.psi_rhomolar_reducing = cpjson::get_double(viscosity["psi"], "rhomolar_reducing");
691
692 fluid.transport.viscosity_using_ECS = true;
693 }
694
695 void parse_Chung_viscosity(rapidjson::Value& viscosity, CoolPropFluid& fluid) {
696 // These in base SI units
697 fluid.transport.viscosity_Chung.rhomolar_critical = cpjson::get_double(viscosity, "rhomolar_critical");
698 fluid.transport.viscosity_Chung.T_critical = cpjson::get_double(viscosity, "T_critical");
699 fluid.transport.viscosity_Chung.molar_mass = cpjson::get_double(viscosity, "molar_mass");
700 fluid.transport.viscosity_Chung.dipole_moment_D = cpjson::get_double(viscosity, "dipole_moment_D");
701 fluid.transport.viscosity_Chung.acentric = cpjson::get_double(viscosity, "acentric");
702 fluid.transport.viscosity_using_Chung = true;
703 }
704
705 void parse_rhosr_viscosity(rapidjson::Value& viscosity, CoolPropFluid& fluid) {
706 fluid.transport.viscosity_rhosr.C = cpjson::get_double(viscosity, "C");
707 fluid.transport.viscosity_rhosr.c_liq = cpjson::get_double_array(viscosity, "c_liq");
708 fluid.transport.viscosity_rhosr.c_vap = cpjson::get_double_array(viscosity, "c_vap");
709 fluid.transport.viscosity_rhosr.rhosr_critical = cpjson::get_double(viscosity, "rhosr_critical");
710 fluid.transport.viscosity_rhosr.x_crossover = cpjson::get_double(viscosity, "x_crossover");
711 fluid.transport.viscosity_using_rhosr = true;
712 }
713
715 void parse_viscosity(rapidjson::Value& viscosity, CoolPropFluid& fluid) {
716 // If an array, use the first one, and then stop;
717 if (viscosity.IsArray()) {
718 rapidjson::Value::ValueIterator itr = viscosity.Begin();
719 parse_viscosity(*itr, fluid);
720 return;
721 }
722
723 // Load the BibTeX key
724 fluid.transport.BibTeX_viscosity = cpjson::get_string(viscosity, "BibTeX");
725
726 // Set the Lennard-Jones 12-6 potential variables, or approximate them from method of Chung
727 if (!viscosity.HasMember("sigma_eta") || !viscosity.HasMember("epsilon_over_k")) {
728 default_transport(fluid);
729 } else {
730 fluid.transport.sigma_eta = cpjson::get_double(viscosity, "sigma_eta");
731 fluid.transport.epsilon_over_k = cpjson::get_double(viscosity, "epsilon_over_k");
732 }
733
734 // If it is using ECS, set ECS parameters and quit
735 if (viscosity.HasMember("type") && !cpjson::get_string(viscosity, "type").compare("ECS")) {
736 parse_ECS_viscosity(viscosity, fluid);
737 return;
738 }
739
740 // If it is using rho*sr CS, set parameters and quit
741 if (viscosity.HasMember("type") && !cpjson::get_string(viscosity, "type").compare("rhosr-CS")) {
742 parse_rhosr_viscosity(viscosity, fluid);
743 return;
744 }
745
746 // Use the method of Chung
747 // If it is using ECS, set ECS parameters and quit
748 if (viscosity.HasMember("type") && !cpjson::get_string(viscosity, "type").compare("Chung")) {
749 parse_Chung_viscosity(viscosity, fluid);
750 return;
751 }
752
753 if (viscosity.HasMember("hardcoded")) {
754 std::string target = cpjson::get_string(viscosity, "hardcoded");
755 if (!target.compare("Water")) {
757 return;
758 } else if (!target.compare("HeavyWater")) {
760 return;
761 } else if (!target.compare("Helium")) {
763 return;
764 } else if (!target.compare("R23")) {
766 return;
767 } else if (!target.compare("Methanol")) {
769 return;
770 } else if (!target.compare("m-Xylene")) {
772 return;
773 } else if (!target.compare("o-Xylene")) {
775 return;
776 } else if (!target.compare("p-Xylene")) {
778 return;
779 } else {
780 throw ValueError(format("hardcoded viscosity [%s] is not understood for fluid %s", target.c_str(), fluid.name.c_str()));
781 }
782 }
783
784 // Load dilute viscosity term
785 if (viscosity.HasMember("dilute")) {
786 parse_dilute_viscosity(viscosity["dilute"], fluid);
787 }
788 // Load initial density term
789 if (viscosity.HasMember("initial_density")) {
790 parse_initial_density_viscosity(viscosity["initial_density"], fluid);
791 }
792 // Load higher_order term
793 if (viscosity.HasMember("higher_order")) {
794 parse_higher_order_viscosity(viscosity["higher_order"], fluid);
795 }
796 };
797
799 void parse_dilute_conductivity(rapidjson::Value& dilute, CoolPropFluid& fluid) {
800 if (dilute.HasMember("hardcoded")) {
801 std::string target = cpjson::get_string(dilute, "hardcoded");
802 if (!target.compare("CO2")) {
804 return;
805 }
806 else if (!target.compare("CarbonDioxideHuberJPCRD2016")) {
808 return;
809 } else if (!target.compare("Ethane")) {
811 return;
812 } else if (!target.compare("none")) {
814 return;
815 } else {
816 throw ValueError(
817 format("hardcoded dilute conductivity term [%s] is not understood for fluid %s", target.c_str(), fluid.name.c_str()));
818 }
819 }
820 std::string type = cpjson::get_string(dilute, "type");
821 if (!type.compare("ratio_of_polynomials")) {
822 // Get a reference to the entry in the fluid instance
824
825 // Set the type flag
827
828 // Load up the values
829 data.A = cpjson::get_long_double_array(dilute["A"]);
830 data.B = cpjson::get_long_double_array(dilute["B"]);
831 data.n = cpjson::get_long_double_array(dilute["n"]);
832 data.m = cpjson::get_long_double_array(dilute["m"]);
833 data.T_reducing = cpjson::get_double(dilute, "T_reducing");
834 } else if (!type.compare("eta0_and_poly")) {
835 // Get a reference to the entry in the fluid instance
837
838 // Set the type flag
840
841 // Load up the values
842 data.A = cpjson::get_long_double_array(dilute["A"]);
843 data.t = cpjson::get_long_double_array(dilute["t"]);
844 } else {
845 throw ValueError(format("type [%s] is not understood for fluid %s", type.c_str(), fluid.name.c_str()));
846 }
847 };
848
850 void parse_residual_conductivity(rapidjson::Value& dilute, CoolPropFluid& fluid) {
851 if (dilute.HasMember("hardcoded")) {
852 std::string target = cpjson::get_string(dilute, "hardcoded");
853 if (!target.compare("CO2")) {
855 return;
856 } else {
857 throw ValueError(
858 format("hardcoded residual conductivity term [%s] is not understood for fluid %s", target.c_str(), fluid.name.c_str()));
859 }
860 }
861 std::string type = cpjson::get_string(dilute, "type");
862 if (!type.compare("polynomial")) {
863 // Get a reference to the entry in the fluid instance
865
866 // Set the type flag
868
869 // Load up the values
870 data.B = cpjson::get_long_double_array(dilute["B"]);
871 data.d = cpjson::get_long_double_array(dilute["d"]);
872 data.t = cpjson::get_long_double_array(dilute["t"]);
873 data.T_reducing = cpjson::get_double(dilute, "T_reducing");
874 data.rhomass_reducing = cpjson::get_double(dilute, "rhomass_reducing");
875 } else if (!type.compare("polynomial_and_exponential")) {
876 // Get a reference to the entry in the fluid instance
878
879 // Set the type flag
881
882 // Load up the values
883 data.A = cpjson::get_long_double_array(dilute["A"]);
884 data.d = cpjson::get_long_double_array(dilute["d"]);
885 data.t = cpjson::get_long_double_array(dilute["t"]);
886 data.gamma = cpjson::get_long_double_array(dilute["gamma"]);
887 data.l = cpjson::get_long_double_array(dilute["l"]);
888 } else {
889 throw ValueError(format("type [%s] is not understood for fluid %s", type.c_str(), fluid.name.c_str()));
890 }
891 };
892
893 void parse_critical_conductivity(rapidjson::Value& critical, CoolPropFluid& fluid) {
894 if (critical.HasMember("hardcoded")) {
895 std::string target = cpjson::get_string(critical, "hardcoded");
896 if (!target.compare("R123")) {
898 return;
899 } else if (!target.compare("Ammonia")) {
901 return;
902 } else if (!target.compare("CarbonDioxideScalabrinJPCRD2006")) {
905 return;
906 } else if (!target.compare("None")) {
908 return;
909 } else {
910 throw ValueError(format("critical conductivity term [%s] is not understood for fluid %s", target.c_str(), fluid.name.c_str()));
911 }
912 }
913 std::string type = cpjson::get_string(critical, "type");
914 if (!type.compare("simplified_Olchowy_Sengers")) {
917
918 // Set the type flag
920
921 // Set values if they are found - otherwise fall back to default values
922 if (critical.HasMember("qD")) {
923 data.qD = cpjson::get_double(critical, "qD");
924 }
925 if (critical.HasMember("zeta0")) {
926 data.zeta0 = cpjson::get_double(critical, "zeta0");
927 }
928 if (critical.HasMember("GAMMA")) {
929 data.GAMMA = cpjson::get_double(critical, "GAMMA");
930 }
931 if (critical.HasMember("gamma")) {
932 data.gamma = cpjson::get_double(critical, "gamma");
933 }
934 if (critical.HasMember("R0")) {
935 data.R0 = cpjson::get_double(critical, "R0");
936 }
937 if (critical.HasMember("T_ref")) {
938 data.T_ref = cpjson::get_double(critical, "T_ref");
939 }
940 } else {
941 throw ValueError(format("type [%s] is not understood for fluid %s", type.c_str(), fluid.name.c_str()));
942 }
943 };
944
946 void parse_thermal_conductivity(rapidjson::Value& conductivity, CoolPropFluid& fluid) {
947 // Load the BibTeX key
948 fluid.transport.BibTeX_conductivity = cpjson::get_string(conductivity, "BibTeX");
949
950 // If it is using ECS, set ECS parameters and quit
951 if (conductivity.HasMember("type") && !cpjson::get_string(conductivity, "type").compare("ECS")) {
952 parse_ECS_conductivity(conductivity, fluid);
953 return;
954 }
955
956 if (conductivity.HasMember("hardcoded")) {
957 std::string target = cpjson::get_string(conductivity, "hardcoded");
958 if (!target.compare("Water")) {
960 return;
961 } else if (!target.compare("HeavyWater")) {
963 return;
964 } else if (!target.compare("Methane")) {
966 return;
967 } else if (!target.compare("R23")) {
969 return;
970 } else if (!target.compare("Helium")) {
972 return;
973 } else {
974 throw ValueError(
975 format("hardcoded residual conductivity term [%s] is not understood for fluid %s", target.c_str(), fluid.name.c_str()));
976 }
977 }
978
979 // Load dilute conductivity term
980 if (conductivity.HasMember("dilute")) {
981 parse_dilute_conductivity(conductivity["dilute"], fluid);
982 }
983 // Load residual conductivity term
984 if (conductivity.HasMember("residual")) {
985 parse_residual_conductivity(conductivity["residual"], fluid);
986 }
987 // Load critical conductivity term
988 if (conductivity.HasMember("critical")) {
989 parse_critical_conductivity(conductivity["critical"], fluid);
990 }
991 };
992
994 void parse_transport(rapidjson::Value& transport, CoolPropFluid& fluid) {
995
996 // Parse viscosity
997 if (transport.HasMember("viscosity")) {
998 parse_viscosity(transport["viscosity"], fluid);
1000 }
1001
1002 // Parse thermal conductivity
1003 if (transport.HasMember("conductivity")) {
1004 parse_thermal_conductivity(transport["conductivity"], fluid);
1006 }
1007 };
1008
1010 // Use the method of Chung to approximate the values for epsilon_over_k and sigma_eta
1011 // 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.
1012 // rhoc needs to be in mol/L to yield a sigma in nm,
1013 CoolPropDbl rho_crit_molar = fluid.EOS().reduce.rhomolar / 1000.0; // [mol/m3 to mol/L]
1014 CoolPropDbl Tc = fluid.EOS().reduce.T;
1015 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
1016 fluid.transport.epsilon_over_k = Tc / 1.2593; // [K]
1017 }
1018
1019 void parse_melting_line(rapidjson::Value& melting_line, CoolPropFluid& fluid) {
1020 fluid.ancillaries.melting_line.T_m = cpjson::get_double(melting_line, "T_m");
1021 fluid.ancillaries.melting_line.BibTeX = cpjson::get_string(melting_line, "BibTeX");
1022
1023 if (melting_line.HasMember("type")) {
1024 std::string type = cpjson::get_string(melting_line, "type");
1025 if (!type.compare("Simon")) {
1026 rapidjson::Value& parts = melting_line["parts"];
1028 for (rapidjson::Value::ValueIterator itr = parts.Begin(); itr != parts.End(); ++itr) {
1030 data.a = cpjson::get_double((*itr), "a");
1031 data.c = cpjson::get_double((*itr), "c");
1032 data.T_min = cpjson::get_double((*itr), "T_min");
1033 data.T_max = cpjson::get_double((*itr), "T_max");
1034 data.T_0 = cpjson::get_double((*itr), "T_0");
1035 data.p_0 = cpjson::get_double((*itr), "p_0");
1036 fluid.ancillaries.melting_line.simon.parts.push_back(data);
1037 }
1038 } else if (!type.compare("polynomial_in_Tr")) {
1039 rapidjson::Value& parts = melting_line["parts"];
1041 for (rapidjson::Value::ValueIterator itr = parts.Begin(); itr != parts.End(); ++itr) {
1043 data.a = cpjson::get_long_double_array((*itr), "a");
1044 data.t = cpjson::get_long_double_array((*itr), "t");
1045 data.T_min = cpjson::get_double((*itr), "T_min");
1046 data.T_max = cpjson::get_double((*itr), "T_max");
1047 data.T_0 = cpjson::get_double((*itr), "T_0");
1048 data.p_0 = cpjson::get_double((*itr), "p_0");
1049 fluid.ancillaries.melting_line.polynomial_in_Tr.parts.push_back(data);
1050 }
1051 } else if (!type.compare("polynomial_in_Theta")) {
1052 rapidjson::Value& parts = melting_line["parts"];
1054 for (rapidjson::Value::ValueIterator itr = parts.Begin(); itr != parts.End(); ++itr) {
1056 data.a = cpjson::get_long_double_array((*itr), "a");
1057 data.t = cpjson::get_long_double_array((*itr), "t");
1058 data.T_min = cpjson::get_double((*itr), "T_min");
1059 data.T_max = cpjson::get_double((*itr), "T_max");
1060 data.T_0 = cpjson::get_double((*itr), "T_0");
1061 data.p_0 = cpjson::get_double((*itr), "p_0");
1062 fluid.ancillaries.melting_line.polynomial_in_Theta.parts.push_back(data);
1063 }
1064 } else {
1065 throw ValueError(format("melting line type [%s] is not understood for fluid %s", type.c_str(), fluid.name.c_str()));
1066 }
1067 // Set the limits for the melting line curve
1069 } else {
1070 throw ValueError(format("melting line does not have \"type\" for fluid %s", fluid.name.c_str()));
1071 }
1072 };
1073
1075 void parse_states(rapidjson::Value& states, CoolPropFluid& fluid) {
1076 if (!states.HasMember("critical")) {
1077 throw ValueError(format("fluid[\"STATES\"] [%s] does not have \"critical\" member", fluid.name.c_str()));
1078 }
1079 rapidjson::Value& crit = states["critical"];
1080 fluid.crit.T = cpjson::get_double(crit, "T");
1081 fluid.crit.p = cpjson::get_double(crit, "p");
1082 fluid.crit.rhomolar = cpjson::get_double(crit, "rhomolar");
1083 fluid.crit.hmolar = cpjson::get_double(crit, "hmolar");
1084 fluid.crit.smolar = cpjson::get_double(crit, "smolar");
1085
1086 if (!states.HasMember("triple_liquid")) {
1087 throw ValueError(format("fluid[\"STATES\"] [%s] does not have \"triple_liquid\" member", fluid.name.c_str()));
1088 }
1089 rapidjson::Value& triple_liquid = states["triple_liquid"];
1090 if (triple_liquid.ObjectEmpty()) {
1091 // State is empty - probably because the triple point temperature is below the minimum saturation temperature
1092 fluid.triple_liquid.T = -1;
1093 fluid.triple_liquid.p = -1;
1094 fluid.triple_liquid.rhomolar = -1;
1095 fluid.triple_liquid.hmolar = _HUGE;
1096 fluid.triple_liquid.smolar = _HUGE;
1097 } else {
1098 fluid.triple_liquid.T = cpjson::get_double(triple_liquid, "T");
1099 fluid.triple_liquid.p = cpjson::get_double(triple_liquid, "p");
1100 fluid.triple_liquid.rhomolar = cpjson::get_double(triple_liquid, "rhomolar");
1101 fluid.triple_liquid.hmolar = cpjson::get_double(triple_liquid, "hmolar");
1102 fluid.triple_liquid.smolar = cpjson::get_double(triple_liquid, "smolar");
1103 }
1104
1105 if (!states.HasMember("triple_vapor")) {
1106 throw ValueError(format("fluid[\"STATES\"] [%s] does not have \"triple_vapor\" member", fluid.name.c_str()));
1107 }
1108 rapidjson::Value& triple_vapor = states["triple_vapor"];
1109 if (triple_vapor.ObjectEmpty()) {
1110 // State is empty - probably because the triple point temperature is below the minimum saturation temperature
1111 fluid.triple_vapor.T = -1;
1112 fluid.triple_vapor.p = -1;
1113 fluid.triple_vapor.rhomolar = -1;
1114 fluid.triple_vapor.hmolar = _HUGE;
1115 fluid.triple_vapor.smolar = _HUGE;
1116 } else {
1117 fluid.triple_vapor.T = cpjson::get_double(triple_vapor, "T");
1118 fluid.triple_vapor.p = cpjson::get_double(triple_vapor, "p");
1119 fluid.triple_vapor.rhomolar = cpjson::get_double(triple_vapor, "rhomolar");
1120 fluid.triple_vapor.hmolar = cpjson::get_double(triple_vapor, "hmolar");
1121 fluid.triple_vapor.smolar = cpjson::get_double(triple_vapor, "smolar");
1122 }
1123 };
1124
1126 void parse_ancillaries(rapidjson::Value& ancillaries, CoolPropFluid& fluid) {
1127 if (!ancillaries.HasMember("rhoL") || !ancillaries.HasMember("rhoV")) {
1128 throw ValueError("Ancillary curves for either rhoL or rhoV are missing");
1129 }
1130 fluid.ancillaries.rhoL = SaturationAncillaryFunction(ancillaries["rhoL"]);
1131 fluid.ancillaries.rhoV = SaturationAncillaryFunction(ancillaries["rhoV"]);
1132
1133 // If a pseudo-pure fluid, has pL and pV curves
1134 if (ancillaries.HasMember("pL") && ancillaries.HasMember("pV")) {
1135 fluid.ancillaries.pL = SaturationAncillaryFunction(ancillaries["pL"]);
1136 fluid.ancillaries.pV = SaturationAncillaryFunction(ancillaries["pV"]);
1137 }
1138 // Otherwise has a single pS curve and not pL and not pV
1139 else if (!ancillaries.HasMember("pL") && !ancillaries.HasMember("pV") && ancillaries.HasMember("pS")) {
1140 fluid.ancillaries.pL = SaturationAncillaryFunction(ancillaries["pS"]);
1141 fluid.ancillaries.pV = SaturationAncillaryFunction(ancillaries["pS"]);
1142 } else {
1143 throw ValueError("Pressure ancillary curves are missing or invalid");
1144 }
1145
1146 if (ancillaries.HasMember("hL")) {
1147 fluid.ancillaries.hL = SaturationAncillaryFunction(ancillaries["hL"]);
1148 } else {
1149 if (get_debug_level() > 0) {
1150 std::cout << "Missing hL ancillary for fluid " << fluid.name;
1151 }
1152 }
1153 if (ancillaries.HasMember("hLV")) {
1154 fluid.ancillaries.hLV = SaturationAncillaryFunction(ancillaries["hLV"]);
1155 } else {
1156 if (get_debug_level() > 0) {
1157 std::cout << "Missing hLV ancillary for fluid " << fluid.name;
1158 }
1159 }
1160
1161 if (ancillaries.HasMember("sL")) {
1162 fluid.ancillaries.sL = SaturationAncillaryFunction(ancillaries["sL"]);
1163 } else {
1164 if (get_debug_level() > 0) {
1165 std::cout << "Missing sL ancillary for fluid " << fluid.name;
1166 }
1167 }
1168 if (ancillaries.HasMember("sLV")) {
1169 fluid.ancillaries.sLV = SaturationAncillaryFunction(ancillaries["sLV"]);
1170 } else {
1171 if (get_debug_level() > 0) {
1172 std::cout << "Missing sLV ancillary for fluid " << fluid.name;
1173 }
1174 }
1175 if (!ValidNumber(fluid.ancillaries.sL.get_Tmin()) && get_debug_level() > 0) {
1176 std::cout << "Tmin invalid for sL for " << fluid.name << std::endl;
1177 }
1178 };
1179
1181 void parse_surface_tension(rapidjson::Value& surface_tension, CoolPropFluid& fluid) {
1182 fluid.ancillaries.surface_tension = SurfaceTensionCorrelation(surface_tension);
1183 };
1184
1187 assert(fluid.EOSVector.size() > 0);
1188 assert(fluid.CAS.length() > 0);
1189 assert(fluid.name.length() > 0);
1190 }
1191
1192 public:
1193 // Default constructor;
1195 _is_empty = true;
1196 };
1197 bool is_empty(void) {
1198 return _is_empty;
1199 };
1200
1202 static void add_many(const std::string& JSON_string);
1203
1205 void add_many(rapidjson::Value& listing);
1206
1207 void add_one(rapidjson::Value& fluid_json);
1208
1209 std::string get_JSONstring(const std::string& key) {
1210 // Try to find it
1211 std::map<std::string, std::size_t>::const_iterator it = string_to_index_map.find(key);
1212 if (it != string_to_index_map.end()) {
1213
1214 std::map<std::size_t, std::string>::const_iterator it2 = JSONstring_map.find(it->second);
1215 if (it2 != JSONstring_map.end()) {
1216 // Then, load the fluids we would like to add
1217 rapidjson::Document doc;
1218 cpjson::JSON_string_to_rapidjson(it2->second, doc);
1219 rapidjson::Document doc2;
1220 doc2.SetArray();
1221 doc2.PushBack(doc, doc.GetAllocator());
1222 return cpjson::json2string(doc2);
1223 } else {
1224 throw ValueError(format("Unable to obtain JSON string for this identifier [%d]", it->second));
1225 }
1226 } else {
1227 throw ValueError(format("Unable to obtain index for this identifier [%s]", key.c_str()));
1228 }
1229 }
1230
1232
1235 CoolPropFluid get(const std::string& key) {
1236 // Try to find it
1237 std::map<std::string, std::size_t>::const_iterator it = string_to_index_map.find(key);
1238 // If it is found
1239 if (it != string_to_index_map.end()) {
1240 return get(it->second);
1241 } else {
1242 // Here we check for the use of a cubic Helmholtz energy transformation for a multi-fluid model
1243 std::vector<std::string> endings;
1244 endings.push_back("-SRK");
1245 endings.push_back("-PengRobinson");
1246 for (std::vector<std::string>::const_iterator end = endings.begin(); end != endings.end(); ++end) {
1247 if (endswith(key, *end)) {
1248 std::string used_name = key.substr(0, key.size() - (*end).size());
1249 it = string_to_index_map.find(used_name);
1250 if (it != string_to_index_map.end()) {
1251 // We found the name of the fluid within the library of multiparameter
1252 // Helmholtz-explicit models. We will load its parameters from the
1253 // multiparameter EOS
1254 //
1255 CoolPropFluid fluid = get(it->second);
1256 // Remove all the residual contributions to the Helmholtz energy
1257 fluid.EOSVector[0].alphar.empty_the_EOS();
1258 // Get the parameters for the cubic EOS
1259 CoolPropDbl Tc = fluid.EOSVector[0].reduce.T;
1260 CoolPropDbl pc = fluid.EOSVector[0].reduce.p;
1261 CoolPropDbl rhomolarc = fluid.EOSVector[0].reduce.rhomolar;
1262 CoolPropDbl acentric = fluid.EOSVector[0].acentric;
1263 CoolPropDbl R = 8.3144598; // fluid.EOSVector[0].R_u;
1264 // Set the cubic contribution to the residual Helmholtz energy
1265 shared_ptr<AbstractCubic> ac;
1266 if (*end == "-SRK") {
1267 ac.reset(new SRK(Tc, pc, acentric, R));
1268 } else if (*end == "-PengRobinson") {
1269 ac.reset(new PengRobinson(Tc, pc, acentric, R));
1270 } else {
1271 throw CoolProp::ValueError(format("Unable to match this ending [%s]", (*end).c_str()));
1272 }
1273 ac->set_Tr(Tc);
1274 ac->set_rhor(rhomolarc);
1275 fluid.EOSVector[0].alphar.cubic = ResidualHelmholtzGeneralizedCubic(ac);
1276 return fluid;
1277 } else {
1278 // Let's look in the library of cubic EOS
1280 // Set the cubic contribution to the residual Helmholtz energy
1281 shared_ptr<AbstractCubic> ac;
1282 if (*end == "-SRK") {
1283 ac.reset(new SRK(vals.Tc, vals.pc, vals.acentric, get_config_double(R_U_CODATA)));
1284 } else if (*end == "-PengRobinson") {
1285 ac.reset(new PengRobinson(vals.Tc, vals.pc, vals.acentric, get_config_double(R_U_CODATA)));
1286 } else {
1287 throw CoolProp::ValueError(format("Unable to match this ending [%s]", (*end).c_str()));
1288 }
1289 ac->set_Tr(vals.Tc);
1290 if (vals.rhomolarc > 0) {
1291 ac->set_rhor(vals.rhomolarc);
1292 } else {
1293 // Curve fit from all the pure fluids in CoolProp (thanks to recommendation of A. Kazakov)
1294 double v_c_Lmol = 2.14107171795 * (vals.Tc / vals.pc * 1000) + 0.00773144012514; // [L/mol]
1295 ac->set_rhor(1 / (v_c_Lmol / 1000.0));
1296 }
1297 if (vals.alpha_type == "Twu") {
1298 std::vector<double>& c = vals.alpha_coeffs;
1299 ac->set_C_Twu(0, c[0], c[1], c[2]);
1300 }
1301 CoolPropFluid fluid;
1302 fluid.CAS = vals.CAS;
1304 E.acentric = vals.acentric;
1305 E.sat_min_liquid.T = _HUGE;
1306 E.sat_min_liquid.p = _HUGE;
1307 E.reduce.T = vals.Tc;
1308 E.reduce.p = vals.pc;
1309 E.reduce.rhomolar = ac->get_rhor();
1310 fluid.EOSVector.push_back(E);
1312 fluid.EOS().alpha0 = vals.alpha0;
1313 fluid.crit.T = vals.Tc;
1314 fluid.crit.p = vals.pc;
1315 fluid.crit.rhomolar = ac->get_rhor();
1316
1317 return fluid;
1318 }
1319 }
1320 }
1321 throw ValueError(format("key [%s] was not found in string_to_index_map in JSONFluidLibrary", key.c_str()));
1322 }
1323 };
1324
1326
1329 CoolPropFluid get(std::size_t key) {
1330 // Try to find it
1331 std::map<std::size_t, CoolPropFluid>::iterator it = fluid_map.find(key);
1332 // If it is found
1333 if (it != fluid_map.end()) {
1334 return it->second;
1335 } else {
1336 throw ValueError(format("key [%d] was not found in JSONFluidLibrary", key));
1337 }
1338 };
1339 void set_fluid_enthalpy_entropy_offset(const std::string& fluid, double delta_a1, double delta_a2, const std::string& ref);
1341 std::string get_fluid_list(void) {
1342 return strjoin(name_vector, get_config_string(LIST_STRING_DELIMITER));
1343 };
1344};
1345
1347JSONFluidLibrary& get_library(void);
1348
1350std::string get_fluid_list(void);
1351
1353CoolPropFluid get_fluid(const std::string& fluid_string);
1354
1356std::string get_fluid_as_JSONstring(const std::string& indentifier);
1357
1359void set_fluid_enthalpy_entropy_offset(const std::string& fluid, double delta_a1, double delta_a2, const std::string& ref);
1360
1361} /* namespace CoolProp */
1362#endif