7 #if defined(COOLPROP_NO_INCBIN)
20 INCBIN(all_fluids_JSON_z,
"all_fluids.json.z");
26 static JSONFluidLibrary library;
30 uLong outlen =
static_cast<uLong>(outbuffer.size());
32 std::string buf(outbuffer.begin(), outbuffer.begin() + outlen);
34 throw ValueError(
"Unable to uncompress the fluid data from z compressed form");
37 rapidjson::Document dd;
39 dd.Parse<0>(buf.c_str());
40 if (dd.HasParseError()) {
41 throw ValueError(
"Unable to load all_fluids.json");
45 }
catch (std::exception& e) {
46 std::cout << e.what() << std::endl;
53 std::map<std::string, std::size_t>::const_iterator it = string_to_index_map.find(fluid);
54 if (it != string_to_index_map.end()) {
55 std::map<std::size_t, CoolPropFluid>::iterator it2 = fluid_map.find(it->second);
57 if (it2 != fluid_map.end()) {
59 throw ValueError(
format(
"Not possible to set reference state for fluid %s because offset values are NAN", fluid.c_str()));
61 it2->second.EOS().alpha0.EnthalpyEntropyOffset.set(delta_a1, delta_a2, ref);
66 HEOS->update(
DmolarT_INPUTS, it2->second.EOS().hs_anchor.rhomolar, it2->second.EOS().hs_anchor.T);
67 it2->second.EOS().hs_anchor.hmolar = HEOS->hmolar();
68 it2->second.EOS().hs_anchor.smolar = HEOS->smolar();
70 double f = (HEOS->name() ==
"Water" || HEOS->name() ==
"CarbonDioxide") ? 1.00001 : 1.0;
73 HEOS->update(
DmolarT_INPUTS, it2->second.EOS().reduce.rhomolar * f, it2->second.EOS().reduce.T * f);
74 it2->second.EOS().reduce.hmolar = HEOS->hmolar();
75 it2->second.EOS().reduce.smolar = HEOS->smolar();
78 HEOS->update(
DmolarT_INPUTS, it2->second.crit.rhomolar * f, it2->second.crit.T * f);
79 it2->second.crit.hmolar = HEOS->hmolar();
80 it2->second.crit.smolar = HEOS->smolar();
83 HEOS->update(
DmolarT_INPUTS, it2->second.triple_liquid.rhomolar, it2->second.triple_liquid.T);
84 it2->second.triple_liquid.hmolar = HEOS->hmolar();
85 it2->second.triple_liquid.smolar = HEOS->smolar();
88 HEOS->update(
DmolarT_INPUTS, it2->second.triple_vapor.rhomolar, it2->second.triple_vapor.T);
89 it2->second.triple_vapor.hmolar = HEOS->hmolar();
90 it2->second.triple_vapor.smolar = HEOS->smolar();
92 if (!HEOS->is_pure()) {
94 HEOS->update(
DmolarT_INPUTS, it2->second.EOS().max_sat_T.rhomolar, it2->second.EOS().max_sat_T.T);
95 it2->second.EOS().max_sat_T.hmolar = HEOS->hmolar();
96 it2->second.EOS().max_sat_T.smolar = HEOS->smolar();
98 HEOS->update(
DmolarT_INPUTS, it2->second.EOS().max_sat_p.rhomolar, it2->second.EOS().max_sat_p.T);
99 it2->second.EOS().max_sat_p.hmolar = HEOS->hmolar();
100 it2->second.EOS().max_sat_p.smolar = HEOS->smolar();
103 throw ValueError(
format(
"fluid [%s] was not found in JSONFluidLibrary", fluid.c_str()));
117 rapidjson::Document doc;
123 if (!listing.IsArray()) {
127 for (rapidjson::Value::ValueIterator itr = listing.Begin(); itr != listing.End(); ++itr) {
138 std::size_t index = fluid_map.size();
145 fluid.
name = fluid_json[
"INFO"][
"NAME"].GetString();
149 name_vector.push_back(fluid.
name);
153 if (!fluid_json[
"INFO"].HasMember(
"CAS")) {
156 fluid.
CAS = fluid_json[
"INFO"][
"CAS"].GetString();
159 if (!fluid_json[
"INFO"].HasMember(
"REFPROP_NAME")) {
160 throw ValueError(
format(
"fluid [%s] does not have \"REFPROP_NAME\" member", fluid.
name.c_str()));
162 fluid.
REFPROPname = fluid_json[
"INFO"][
"REFPROP_NAME"].GetString();
165 if (fluid_json[
"INFO"].HasMember(
"FORMULA")) {
172 if (fluid_json[
"INFO"].HasMember(
"INCHI_STRING")) {
178 if (fluid_json[
"INFO"].HasMember(
"INCHI_KEY")) {
184 if (fluid_json[
"INFO"].HasMember(
"SMILES")) {
190 if (fluid_json[
"INFO"].HasMember(
"CHEMSPIDER_ID")) {
196 if (fluid_json[
"INFO"].HasMember(
"2DPNG_URL")) {
203 if (!(fluid_json[
"INFO"].HasMember(
"ENVIRONMENTAL"))) {
205 std::cout <<
format(
"Environmental data are missing for fluid [%s]\n", fluid.
name.c_str());
215 if (!fluid_json.HasMember(
"STATES")) {
221 std::cout <<
format(
"Loading fluid %s with CAS %s; %d fluids loaded\n", fluid.
name.c_str(), fluid.
CAS.c_str(), index);
231 if (!fluid_json.HasMember(
"ANCILLARIES")) {
237 if (!(fluid_json[
"ANCILLARIES"].HasMember(
"surface_tension"))) {
239 std::cout <<
format(
"Surface tension curves are missing for fluid [%s]\n", fluid.
name.c_str());
246 if (!(fluid_json[
"ANCILLARIES"].HasMember(
"melting_line"))) {
248 std::cout <<
format(
"Melting line curves are missing for fluid [%s]\n", fluid.
name.c_str());
255 if (!(fluid_json.HasMember(
"TRANSPORT"))) {
269 if (string_to_index_map.find(fluid.
CAS) != string_to_index_map.end()) {
270 index = string_to_index_map.find(fluid.
CAS)->second;
271 }
else if (string_to_index_map.find(fluid.
name) != string_to_index_map.end()) {
272 index = string_to_index_map.find(fluid.
name)->second;
273 }
else if (string_to_index_map.find(
upper(fluid.
name)) != string_to_index_map.end()) {
274 index = string_to_index_map.find(
upper(fluid.
name))->second;
277 for (std::size_t i = 0; i < fluid.
aliases.size(); ++i) {
278 if (string_to_index_map.find(fluid.
aliases[i]) != string_to_index_map.end()) {
279 index = string_to_index_map.find(fluid.
aliases[i])->second;
282 if (string_to_index_map.find(
upper(fluid.
aliases[i])) != string_to_index_map.end()) {
283 index = string_to_index_map.find(
upper(fluid.
aliases[i]))->second;
289 bool fluid_exists =
false;
291 if (index != fluid_map.size()) {
293 name_vector.pop_back();
295 throw ValueError(
format(
"Cannot load fluid [%s:%s] because it is already in library; index = [%i] of [%i]; Consider enabling the "
296 "config boolean variable OVERWRITE_FLUIDS",
297 fluid.
name.c_str(), fluid.
CAS.c_str(), index, fluid_map.size()));
309 if (fluid_exists) fluid_map.erase(fluid_map.find(index));
311 fluid_map[index] = fluid;
318 if (fluid_exists) JSONstring_map.erase(JSONstring_map.find(index));
326 string_to_index_map[fluid.
CAS] = index;
331 string_to_index_map[fluid.
name] = index;
336 for (std::size_t i = 0; i < fluid.
aliases.size(); ++i) {
337 string_to_index_map[fluid.
aliases[i]] = index;
345 std::cout <<
format(
"Loaded fluid: %s - Number of fluids = %d\n", fluid.
name, fluid_map.size());
348 }
catch (
const std::exception& e) {
349 throw ValueError(
format(
"Unable to load fluid [%s] due to error: %s", fluid.
name.c_str(), e.what()));
364 return library.
get(fluid_string);