7 void UNIFACParameterLibrary::jsonize(std::string& s, rapidjson::Document& d) {
9 if (d.HasParseError()) {
15 void UNIFACParameterLibrary::populate(rapidjson::Value& group_data, rapidjson::Value& interaction_data, rapidjson::Value& comp_data) {
18 interaction_parameters.clear();
22 for (rapidjson::Value::ValueIterator itr = group_data.Begin(); itr != group_data.End(); ++itr) {
24 g.
sgi = (*itr)[
"sgi"].GetInt();
25 g.
mgi = (*itr)[
"mgi"].GetInt();
26 g.
R_k = (*itr)[
"R_k"].GetDouble();
27 g.
Q_k = (*itr)[
"Q_k"].GetDouble();
30 for (rapidjson::Value::ValueIterator itr = interaction_data.Begin(); itr != interaction_data.End(); ++itr) {
32 ip.
mgi1 = (*itr)[
"mgi1"].GetInt();
33 ip.
mgi2 = (*itr)[
"mgi2"].GetInt();
34 ip.
a_ij = (*itr)[
"a_ij"].GetDouble();
35 ip.
a_ji = (*itr)[
"a_ji"].GetDouble();
36 ip.
b_ij = (*itr)[
"b_ij"].GetDouble();
37 ip.
b_ji = (*itr)[
"b_ji"].GetDouble();
38 ip.
c_ij = (*itr)[
"c_ij"].GetDouble();
39 ip.
c_ji = (*itr)[
"c_ji"].GetDouble();
40 interaction_parameters.push_back(ip);
42 for (rapidjson::Value::ValueIterator itr = comp_data.Begin(); itr != comp_data.End(); ++itr) {
44 c.
inchikey = (*itr)[
"inchikey"].GetString();
46 c.
name = (*itr)[
"name"].GetString();
47 c.
Tc = (*itr)[
"Tc"].GetDouble();
48 c.
pc = (*itr)[
"pc"].GetDouble();
49 c.
acentric = (*itr)[
"acentric"].GetDouble();
50 c.
molemass = (*itr)[
"molemass"].GetDouble();
52 if ((*itr).HasMember(
"userid")) {
53 c.
userid = (*itr)[
"userid"].GetString();
56 if ((*itr).HasMember(
"alpha") && (*itr)[
"alpha"].IsObject()) {
57 rapidjson::Value& alpha = (*itr)[
"alpha"];
63 if ((*itr).HasMember(
"alpha0") && (*itr)[
"alpha0"].IsArray()) {
66 rapidjson::Value& groups = (*itr)[
"groups"];
67 for (rapidjson::Value::ValueIterator itrg = groups.Begin(); itrg != groups.End(); ++itrg) {
68 int count = (*itrg)[
"count"].GetInt();
69 int sgi = (*itrg)[
"sgi"].GetInt();
75 components.push_back(c);
78 void UNIFACParameterLibrary::populate(std::string& group_data, std::string& interaction_data, std::string& decomp_data) {
79 rapidjson::Document group_JSON;
80 jsonize(group_data, group_JSON);
81 rapidjson::Document interaction_JSON;
82 jsonize(interaction_data, interaction_JSON);
83 rapidjson::Document decomp_JSON;
84 jsonize(decomp_data, decomp_JSON);
85 populate(group_JSON, interaction_JSON, decomp_JSON);
89 for (std::vector<Group>::const_iterator it = groups.begin(); it != groups.end(); ++it) {
97 for (std::vector<Group>::const_iterator it = groups.begin(); it != groups.end(); ++it) {
115 for (std::vector<InteractionParameters>::const_iterator it = interaction_parameters.begin(); it != interaction_parameters.end(); ++it) {
116 if (it->mgi1 == mgi1 && it->mgi2 == mgi2) {
120 if (it->mgi2 == mgi1 && it->mgi1 == mgi2) {
127 throw CoolProp::ValueError(
format(
"Could not find interaction between pair mgi[%d]-mgi[%d]",
static_cast<int>(mgi1),
static_cast<int>(mgi2)));
131 if (identifier ==
"name") {
132 for (std::vector<Component>::const_iterator it = components.begin(); it != components.end(); ++it) {
133 if (it->name == value) {
143 #if defined(ENABLE_CATCH)
144 # include <catch2/catch_all.hpp>
148 TEST_CASE(
"Check Poling example for UNIFAC",
"[UNIFAC]") {
149 std::string acetone_pentane_groups =
150 "[{ \"Tc\": 508.1, \"acentric\": 0.3071, \"groups\": [ { \"count\": 1, \"sgi\": 1 }, {\"count\": 1, \"sgi\": 18 } ], \"molemass\": 0.44, "
151 "\"inchikey\": \"?????????????\", \"name\": \"Acetone\", \"pc\": 4700000.0, \"registry_number\": \"67-64-1\", \"userid\": \"\" }, { \"Tc\": "
152 "469.7000000000001, \"acentric\": 0.251, \"molemass\": 0.44, \"groups\": [ { \"count\": 2, \"sgi\": 1 }, { \"count\": 3, \"sgi\": 2 } ], "
153 "\"inchikey\": \"?????????????\", \"name\": \"n-Pentane\", \"pc\": 3370000.0, \"registry_number\": \"109-66-0\", \"userid\": \"\" } ]";
154 std::string groups =
"[{\"Q_k\": 0.848, \"R_k\": 0.9011, \"maingroup_name\": \"CH2\", \"mgi\": 1, \"sgi\": 1, \"subgroup_name\": \"CH3\"},"
155 "{\"Q_k\": 0.540, \"R_k\": 0.6744, \"maingroup_name\": \"CH2\", \"mgi\": 1, \"sgi\": 2, \"subgroup_name\": \"CH2\"},"
156 "{\"Q_k\": 1.488, \"R_k\": 1.6724, \"maingroup_name\": \"CH2CO\", \"mgi\": 9, \"sgi\": 18, \"subgroup_name\": \"CH3CO\"}]";
157 std::string interactions =
158 "[{\"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}]";
160 SECTION(
"Validate AC for acetone + n-pentane") {
162 CHECK_NOTHROW(lib.populate(groups, interactions, acetone_pentane_groups));
164 std::vector<std::string> names;
165 names.push_back(
"Acetone");
166 names.push_back(
"n-Pentane");
167 mix.set_components(
"name", names);
168 mix.set_interaction_parameters();
170 std::vector<double> z(2, 0.047);
172 mix.set_mole_fractions(z);
173 CHECK_NOTHROW(mix.set_temperature(307));
175 double lngammaR0 = mix.ln_gamma_R(1.0 / 307, 0, 0);
176 double lngammaR1 = mix.ln_gamma_R(1.0 / 307, 1, 0);
179 CHECK(std::abs(lngammaR0 - 1.66) < 1e-2);
180 CHECK(std::abs(lngammaR1 - 5.68e-3) < 1e-3);
182 std::vector<double> gamma(2);
183 mix.activity_coefficients(1.0 / 307, z, gamma);
186 CHECK(std::abs(gamma[0] - 4.99) < 1e-2);
187 CHECK(std::abs(gamma[1] - 1.005) < 1e-3);