25namespace PCSAFTLibrary {
38 }
catch (std::exception& e) {
39 std::cout << e.what() <<
'\n';
43 throw ValueError(
format(
"Unable to load PC-SAFT library with error: %s", errstr.c_str()));
62 add_fluids_from_JSON_string(*
this, all_pcsaft_JSON);
65 if (m_binary_pair_map.size() == 0) {
66 PCSAFTLibraryClass::load_from_string(mixture_binary_pairs_pcsaft_JSON);
73 auto it = string_to_index_map.find(key);
75 if (it != string_to_index_map.end()) {
76 return get(it->second);
78 throw ValueError(
format(
"key [%s] was not found in string_to_index_map in PCSAFTLibraryClass", key.c_str()));
88 auto it = fluid_map.find(key);
90 if (it != fluid_map.end()) {
93 throw ValueError(
format(
"key [%d] was not found in PCSAFTLibraryClass", key));
103 std::string fluid_name;
104 for (
const auto& fluid_json : listing) {
112 bool already_present =
false;
114 if (string_to_index_map.find(fluid.
getCAS()) != string_to_index_map.end()
115 || string_to_index_map.find(fluid_name) != string_to_index_map.end()
116 || string_to_index_map.find(
upper(fluid_name)) != string_to_index_map.end()) {
117 already_present =
true;
120 for (std::size_t i = 0; i < fluid.
getAliases().size(); ++i) {
121 if (string_to_index_map.find(fluid.
getAliases()[i]) != string_to_index_map.end()) {
122 already_present =
true;
125 if (string_to_index_map.find(
upper(fluid.
getAliases()[i])) != string_to_index_map.end()) {
126 already_present =
true;
132 if (already_present) {
135 "Cannot load fluid [%s:%s] because it is already in library; consider enabling the config boolean variable OVERWRITE_FLUIDS",
141 std::size_t index = string_to_index_map.find(fluid_name)->second;
143 if (fluid_map.find(index) != fluid_map.end()) {
144 fluid_map.erase(fluid_map.find(index));
147 if (string_to_index_map.find(fluid_name) != string_to_index_map.end()) {
148 fluid_map.erase(fluid_map.find(index));
152 if (string_to_index_map.find(fluid.
getCAS()) != string_to_index_map.end()) {
153 string_to_index_map.erase(string_to_index_map.find(fluid.
getCAS()));
155 if (string_to_index_map.find(fluid_name) != string_to_index_map.end()) {
156 string_to_index_map.erase(string_to_index_map.find(fluid_name));
159 for (std::size_t i = 0; i < fluid.
getAliases().size(); ++i) {
160 if (string_to_index_map.find(fluid.
getAliases()[i]) != string_to_index_map.end()) {
161 string_to_index_map.erase(string_to_index_map.find(fluid.
getAliases()[i]));
163 if (string_to_index_map.find(
upper(fluid.
getAliases()[i])) != string_to_index_map.end()) {
164 string_to_index_map.erase(string_to_index_map.find(
upper(fluid.
getAliases()[i])));
173 std::size_t index = fluid_map.size();
176 fluid_map[index] = fluid;
181 string_to_index_map[fluid.
getCAS()] = index;
184 string_to_index_map[fluid_name] = index;
187 for (std::size_t i = 0; i < fluid.
getAliases().size(); ++i) {
188 string_to_index_map[fluid.
getAliases()[i]] = index;
196 std::cout <<
format(
"Loaded.\n");
198 }
catch (
const std::exception& e) {
199 throw ValueError(
format(
"Unable to load fluid [%s] due to error: %s", fluid_name.c_str(), e.what()));
206 return pcsaft_fluids_schema_JSON;
211 std::vector<std::string> CAS;
215 std::vector<std::string> CASrev;
216 CASrev.push_back(CAS2);
217 CASrev.push_back(CAS1);
219 if (m_binary_pair_map.find(CAS) != m_binary_pair_map.end()) {
220 std::vector<Dictionary>& v = m_binary_pair_map[CAS];
222 if (key ==
"name1") {
223 return v[0].get_string(
"name1");
224 }
else if (key ==
"name2") {
225 return v[0].get_string(
"name2");
226 }
else if (key ==
"BibTeX") {
227 return v[0].get_string(
"BibTeX");
228 }
else if (key ==
"kij") {
230 }
else if (key ==
"kijT") {
234 return format(
"%0.16g", 0.0);
242 throw ValueError(
format(
"Could not match the parameter [%s] for the binary pair [%s,%s] - for now this is an error.", key.c_str(),
243 CAS1.c_str(), CAS2.c_str()));
244 }
else if (m_binary_pair_map.find(CASrev) != m_binary_pair_map.end()) {
245 std::vector<Dictionary>& v = m_binary_pair_map[CASrev];
247 if (key ==
"name1") {
248 return v[0].get_string(
"name1");
249 }
else if (key ==
"name2") {
250 return v[0].get_string(
"name2");
251 }
else if (key ==
"BibTeX") {
252 return v[0].get_string(
"BibTeX");
253 }
else if (key ==
"kij") {
255 }
else if (key ==
"kijT") {
259 return format(
"%0.16g", 0.0);
267 throw ValueError(
format(
"Could not match the parameter [%s] for the binary pair [%s,%s] - for now this is an error.", key.c_str(),
268 CAS1.c_str(), CAS2.c_str()));
271 std::sort(CAS.begin(), CAS.end());
272 if (m_binary_pair_map.find(CAS) != m_binary_pair_map.end()) {
273 throw ValueError(
format(
"Could not match the binary pair [%s,%s] - order of CAS numbers is backwards; found the swapped CAS numbers.",
274 CAS1.c_str(), CAS2.c_str()));
276 throw ValueError(
format(
"Could not match the binary pair [%s,%s] - for now this is an error.", CAS1.c_str(), CAS2.c_str()));
283 std::vector<std::string> CAS;
287 std::vector<std::string> CASrev;
288 CASrev.push_back(CAS2);
289 CASrev.push_back(CAS1);
291 if (m_binary_pair_map.find(CAS) != m_binary_pair_map.end()) {
293 std::vector<Dictionary>& v = m_binary_pair_map[CAS];
294 if (v[0].has_number(key)) {
295 v[0].add_number(key, value);
297 throw ValueError(
format(
"Could not set the parameter [%s] for the binary pair [%s,%s] - for now this is an error", key.c_str(),
298 CAS1.c_str(), CAS2.c_str()));
302 format(
"CAS pair(%s,%s) already in binary interaction map; considering enabling configuration key OVERWRITE_BINARY_INTERACTION",
303 CAS1.c_str(), CAS2.c_str()));
305 }
else if (m_binary_pair_map.find(CASrev) != m_binary_pair_map.end()) {
307 std::vector<Dictionary>& v = m_binary_pair_map[CASrev];
308 if (v[0].has_number(key)) {
309 v[0].add_number(key, value);
311 throw ValueError(
format(
"Could not set the parameter [%s] for the binary pair [%s,%s] - for now this is an error", key.c_str(),
312 CAS1.c_str(), CAS2.c_str()));
316 format(
"CAS pair(%s,%s) already in binary interaction map; considering enabling configuration key OVERWRITE_BINARY_INTERACTION",
317 CAS1.c_str(), CAS2.c_str()));
321 std::vector<std::string> CAS;
326 m_binary_pair_map.insert(std::pair<std::vector<std::string>, std::vector<Dictionary>>(CAS, std::vector<Dictionary>(1, dict)));
330void PCSAFTLibraryClass::load_from_JSON(
const nlohmann::json& doc) {
331 for (
const auto& el : doc) {
336 std::vector<std::string> CAS;
343 std::sort(CAS.begin(), CAS.end());
349 std::swap(name1, name2);
356 if (el.contains(
"kij")) {
359 std::cout <<
"Loading error: binary pair of " << name1 <<
" & " << name2 <<
"does not provide kij" <<
'\n';
361 if (el.contains(
"kijT")) {
365 auto it = m_binary_pair_map.find(CAS);
366 if (it == m_binary_pair_map.end()) {
368 m_binary_pair_map.insert(std::pair<std::vector<std::string>, std::vector<Dictionary>>(CAS, std::vector<Dictionary>(1, dict)));
372 m_binary_pair_map.erase(it);
373 std::pair<std::map<std::vector<std::string>, std::vector<Dictionary>>::iterator,
bool> ret;
374 ret = m_binary_pair_map.insert(std::pair<std::vector<std::string>, std::vector<Dictionary>>(CAS, std::vector<Dictionary>(1, dict)));
375 assert(ret.second ==
true);
379 format(
"CAS pair(%s,%s) already in binary interaction map; considering enabling configuration key OVERWRITE_BINARY_INTERACTION",
380 CAS[0].c_str(), CAS[1].c_str()));
386void PCSAFTLibraryClass::load_from_string(
const std::string_view& str) {