7void UNIFACParameterLibrary::jsonize(std::string& s, nlohmann::json& d) {
10void UNIFACParameterLibrary::populate(
const nlohmann::json& group_data,
const nlohmann::json& interaction_data,
const nlohmann::json& comp_data) {
13 interaction_parameters.clear();
17 for (
const auto& el : group_data) {
25 for (
const auto& el : interaction_data) {
35 interaction_parameters.push_back(ip);
37 for (
const auto& el : comp_data) {
47 if (el.contains(
"userid")) {
51 if (el.contains(
"alpha") && el.at(
"alpha").is_object()) {
52 const nlohmann::json& alpha = el.at(
"alpha");
58 if (el.contains(
"alpha0") && el.at(
"alpha0").is_array()) {
61 const nlohmann::json& comp_groups = el.at(
"groups");
62 for (
const auto& g : comp_groups) {
70 components.push_back(c);
73void UNIFACParameterLibrary::populate(std::string& group_data, std::string& interaction_data, std::string& decomp_data) {
74 nlohmann::json group_JSON, interaction_JSON, decomp_JSON;
75 jsonize(group_data, group_JSON);
76 jsonize(interaction_data, interaction_JSON);
77 jsonize(decomp_data, decomp_JSON);
78 populate(group_JSON, interaction_JSON, decomp_JSON);
82 for (
const auto& group : groups) {
83 if (group.sgi == sgi) {
90 for (
const auto& group : groups) {
91 if (group.sgi == sgi) {
108 for (
const auto& interaction_parameter : interaction_parameters) {
109 if (interaction_parameter.mgi1 == mgi1 && interaction_parameter.mgi2 == mgi2) {
111 return interaction_parameter;
113 if (interaction_parameter.mgi2 == mgi1 && interaction_parameter.mgi1 == mgi2) {
120 throw CoolProp::ValueError(
format(
"Could not find interaction between pair mgi[%d]-mgi[%d]",
static_cast<int>(mgi1),
static_cast<int>(mgi2)));
124 if (identifier ==
"name") {
125 for (
const auto& component : components) {
126 if (component.name == value) {
136#if defined(ENABLE_CATCH)
137# include <catch2/catch_all.hpp>
141TEST_CASE(
"Check Poling example for UNIFAC",
"[UNIFAC]") {
142 std::string acetone_pentane_groups =
143 "[{ \"Tc\": 508.1, \"acentric\": 0.3071, \"groups\": [ { \"count\": 1, \"sgi\": 1 }, {\"count\": 1, \"sgi\": 18 } ], \"molemass\": 0.44, "
144 "\"inchikey\": \"?????????????\", \"name\": \"Acetone\", \"pc\": 4700000.0, \"registry_number\": \"67-64-1\", \"userid\": \"\" }, { \"Tc\": "
145 "469.7000000000001, \"acentric\": 0.251, \"molemass\": 0.44, \"groups\": [ { \"count\": 2, \"sgi\": 1 }, { \"count\": 3, \"sgi\": 2 } ], "
146 "\"inchikey\": \"?????????????\", \"name\": \"n-Pentane\", \"pc\": 3370000.0, \"registry_number\": \"109-66-0\", \"userid\": \"\" } ]";
147 std::string groups =
"[{\"Q_k\": 0.848, \"R_k\": 0.9011, \"maingroup_name\": \"CH2\", \"mgi\": 1, \"sgi\": 1, \"subgroup_name\": \"CH3\"},"
148 "{\"Q_k\": 0.540, \"R_k\": 0.6744, \"maingroup_name\": \"CH2\", \"mgi\": 1, \"sgi\": 2, \"subgroup_name\": \"CH2\"},"
149 "{\"Q_k\": 1.488, \"R_k\": 1.6724, \"maingroup_name\": \"CH2CO\", \"mgi\": 9, \"sgi\": 18, \"subgroup_name\": \"CH3CO\"}]";
150 std::string interactions = R
"([{"a_ij": 476.4, "a_ji": 26.76, "b_ij": 0.0, "b_ji": 0.0, "c_ij": 0.0, "c_ji": 0.0, "mgi1": 1, "mgi2": 9}])";
152 SECTION("Validate AC for acetone + n-pentane") {
154 CHECK_NOTHROW(lib.populate(groups, interactions, acetone_pentane_groups));
156 std::vector<std::string> names;
157 names.emplace_back(
"Acetone");
158 names.emplace_back(
"n-Pentane");
159 mix.set_components(
"name", names);
160 mix.set_interaction_parameters();
162 std::vector<double> z(2, 0.047);
164 mix.set_mole_fractions(z);
165 CHECK_NOTHROW(mix.set_temperature(307));
167 double lngammaR0 = mix.ln_gamma_R(1.0 / 307, 0, 0);
168 double lngammaR1 = mix.ln_gamma_R(1.0 / 307, 1, 0);
171 CHECK(std::abs(lngammaR0 - 1.66) < 1e-2);
172 CHECK(std::abs(lngammaR1 - 5.68e-3) < 1e-3);
174 std::vector<double> gamma(2);
175 mix.activity_coefficients(1.0 / 307, z, gamma);
178 CHECK(std::abs(gamma[0] - 4.99) < 1e-2);
179 CHECK(std::abs(gamma[1] - 1.005) < 1e-3);