24 namespace PCSAFTLibrary {
37 if (m_binary_pair_map.size() == 0) {
45 std::map<std::string, std::size_t>::iterator it = string_to_index_map.find(key);
47 if (it != string_to_index_map.end()) {
48 return get(it->second);
50 throw ValueError(
format(
"key [%s] was not found in string_to_index_map in PCSAFTLibraryClass", key.c_str()));
60 std::map<std::size_t, PCSAFTFluid>::iterator it = fluid_map.find(key);
62 if (it != fluid_map.end()) {
65 throw ValueError(
format(
"key [%d] was not found in PCSAFTLibraryClass", key));
76 rapidjson::Document dd;
78 dd.Parse<0>(JSON.c_str());
79 if (dd.HasParseError()) {
80 throw ValueError(
"Unable to load all_pcsaft_JSON.json");
84 }
catch (std::exception& e) {
85 std::cout << e.what() << std::endl;
90 throw ValueError(
format(
"Unable to load PC-SAFT library with error: %s", errstr.c_str()));
97 std::string fluid_name;
98 for (rapidjson::Value::ValueIterator itr = listing.Begin(); itr != listing.End(); ++itr) {
106 bool already_present =
false;
108 if (string_to_index_map.find(fluid.
getCAS()) != string_to_index_map.end()
109 || string_to_index_map.find(fluid_name) != string_to_index_map.end()
110 || string_to_index_map.find(
upper(fluid_name)) != string_to_index_map.end()) {
111 already_present =
true;
114 for (std::size_t i = 0; i < fluid.
getAliases().size(); ++i) {
115 if (string_to_index_map.find(fluid.
getAliases()[i]) != string_to_index_map.end()) {
116 already_present =
true;
119 if (string_to_index_map.find(
upper(fluid.
getAliases()[i])) != string_to_index_map.end()) {
120 already_present =
true;
126 if (already_present) {
129 "Cannot load fluid [%s:%s] because it is already in library; consider enabling the config boolean variable OVERWRITE_FLUIDS",
135 std::size_t index = string_to_index_map.find(fluid_name)->second;
137 if (fluid_map.find(index) != fluid_map.end()) {
138 fluid_map.erase(fluid_map.find(index));
141 if (string_to_index_map.find(fluid_name) != string_to_index_map.end()) {
142 fluid_map.erase(fluid_map.find(index));
146 if (string_to_index_map.find(fluid.
getCAS()) != string_to_index_map.end()) {
147 string_to_index_map.erase(string_to_index_map.find(fluid.
getCAS()));
149 if (string_to_index_map.find(fluid_name) != string_to_index_map.end()) {
150 string_to_index_map.erase(string_to_index_map.find(fluid_name));
153 for (std::size_t i = 0; i < fluid.
getAliases().size(); ++i) {
154 if (string_to_index_map.find(fluid.
getAliases()[i]) != string_to_index_map.end()) {
155 string_to_index_map.erase(string_to_index_map.find(fluid.
getAliases()[i]));
157 if (string_to_index_map.find(
upper(fluid.
getAliases()[i])) != string_to_index_map.end()) {
158 string_to_index_map.erase(string_to_index_map.find(
upper(fluid.
getAliases()[i])));
167 std::size_t index = fluid_map.size();
170 fluid_map[index] = fluid;
175 string_to_index_map[fluid.
getCAS()] = index;
178 string_to_index_map[fluid_name] = index;
181 for (std::size_t i = 0; i < fluid.
getAliases().size(); ++i) {
182 string_to_index_map[fluid.
getAliases()[i]] = index;
190 std::cout <<
format(
"Loaded.\n");
192 }
catch (
const std::exception& e) {
193 throw ValueError(
format(
"Unable to load fluid [%s] due to error: %s", fluid_name.c_str(), e.what()));
205 std::vector<std::string> CAS;
209 std::vector<std::string> CASrev;
210 CASrev.push_back(CAS2);
211 CASrev.push_back(CAS1);
213 if (m_binary_pair_map.find(CAS) != m_binary_pair_map.end()) {
214 std::vector<Dictionary>& v = m_binary_pair_map[CAS];
216 if (key ==
"name1") {
217 return v[0].get_string(
"name1");
218 }
else if (key ==
"name2") {
219 return v[0].get_string(
"name2");
220 }
else if (key ==
"BibTeX") {
221 return v[0].get_string(
"BibTeX");
222 }
else if (key ==
"kij") {
224 }
else if (key ==
"kijT") {
228 return format(
"%0.16g", 0.0);
234 throw ValueError(
format(
"Could not match the parameter [%s] for the binary pair [%s,%s] - for now this is an error.", key.c_str(),
235 CAS1.c_str(), CAS2.c_str()));
236 }
else if (m_binary_pair_map.find(CASrev) != m_binary_pair_map.end()) {
237 std::vector<Dictionary>& v = m_binary_pair_map[CASrev];
239 if (key ==
"name1") {
240 return v[0].get_string(
"name1");
241 }
else if (key ==
"name2") {
242 return v[0].get_string(
"name2");
243 }
else if (key ==
"BibTeX") {
244 return v[0].get_string(
"BibTeX");
245 }
else if (key ==
"kij") {
247 }
else if (key ==
"kijT") {
251 return format(
"%0.16g", 0.0);
257 throw ValueError(
format(
"Could not match the parameter [%s] for the binary pair [%s,%s] - for now this is an error.", key.c_str(),
258 CAS1.c_str(), CAS2.c_str()));
261 std::sort(CAS.begin(), CAS.end());
262 if (m_binary_pair_map.find(CAS) != m_binary_pair_map.end()) {
263 throw ValueError(
format(
"Could not match the binary pair [%s,%s] - order of CAS numbers is backwards; found the swapped CAS numbers.",
264 CAS1.c_str(), CAS2.c_str()));
266 throw ValueError(
format(
"Could not match the binary pair [%s,%s] - for now this is an error.", CAS1.c_str(), CAS2.c_str()));
273 std::vector<std::string> CAS;
277 std::vector<std::string> CASrev;
278 CASrev.push_back(CAS2);
279 CASrev.push_back(CAS1);
281 if (m_binary_pair_map.find(CAS) != m_binary_pair_map.end()) {
283 std::vector<Dictionary>& v = m_binary_pair_map[CAS];
284 if (v[0].has_number(key)) {
285 v[0].add_number(key, value);
287 throw ValueError(
format(
"Could not set the parameter [%s] for the binary pair [%s,%s] - for now this is an error", key.c_str(),
288 CAS1.c_str(), CAS2.c_str()));
292 format(
"CAS pair(%s,%s) already in binary interaction map; considering enabling configuration key OVERWRITE_BINARY_INTERACTION",
293 CAS1.c_str(), CAS2.c_str()));
295 }
else if (m_binary_pair_map.find(CASrev) != m_binary_pair_map.end()) {
297 std::vector<Dictionary>& v = m_binary_pair_map[CASrev];
298 if (v[0].has_number(key)) {
299 v[0].add_number(key, value);
301 throw ValueError(
format(
"Could not set the parameter [%s] for the binary pair [%s,%s] - for now this is an error", key.c_str(),
302 CAS1.c_str(), CAS2.c_str()));
306 format(
"CAS pair(%s,%s) already in binary interaction map; considering enabling configuration key OVERWRITE_BINARY_INTERACTION",
307 CAS1.c_str(), CAS2.c_str()));
311 std::vector<std::string> CAS;
316 m_binary_pair_map.insert(std::pair<std::vector<std::string>, std::vector<Dictionary>>(CAS, std::vector<Dictionary>(1, dict)));
320 void PCSAFTLibraryClass::load_from_JSON(rapidjson::Document& doc) {
321 for (rapidjson::Value::ValueIterator itr = doc.Begin(); itr != doc.End(); ++itr) {
326 std::vector<std::string> CAS;
333 std::sort(CAS.begin(), CAS.end());
339 std::swap(name1, name2);
346 if (itr->HasMember(
"kij")) {
349 std::cout <<
"Loading error: binary pair of " << name1 <<
" & " << name2 <<
"does not provide kij" << std::endl;
351 if (itr->HasMember(
"kijT")) {
355 std::map<std::vector<std::string>, std::vector<Dictionary>>::iterator it = m_binary_pair_map.find(CAS);
356 if (it == m_binary_pair_map.end()) {
358 m_binary_pair_map.insert(std::pair<std::vector<std::string>, std::vector<Dictionary>>(CAS, std::vector<Dictionary>(1, dict)));
362 m_binary_pair_map.erase(it);
363 std::pair<std::map<std::vector<std::string>, std::vector<Dictionary>>::iterator,
bool> ret;
364 ret = m_binary_pair_map.insert(std::pair<std::vector<std::string>, std::vector<Dictionary>>(CAS, std::vector<Dictionary>(1, dict)));
365 assert(ret.second ==
true);
369 format(
"CAS pair(%s,%s) already in binary interaction map; considering enabling configuration key OVERWRITE_BINARY_INTERACTION",
370 CAS[0].c_str(), CAS[1].c_str()));
376 void PCSAFTLibraryClass::load_from_string(
const std::string& str) {
377 rapidjson::Document doc;
378 doc.Parse<0>(str.c_str());
379 if (doc.HasParseError()) {
380 throw ValueError(
"Unable to parse PC-SAFT binary interaction parameter string");