From f30e466903048681cd4c392f304d88eb50555301 Mon Sep 17 00:00:00 2001
From: Ian Bell <ian.bell@nist.gov>
Date: Fri, 5 Nov 2021 09:30:45 -0400
Subject: [PATCH] Add CPA to json builder

---
 include/teqp/json_builder.hpp |  7 ++++++-
 interface/C/teqpc.cpp         | 24 ++++++++++++++++++++++++
 2 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/include/teqp/json_builder.hpp b/include/teqp/json_builder.hpp
index e578092..0e1f4bc 100644
--- a/include/teqp/json_builder.hpp
+++ b/include/teqp/json_builder.hpp
@@ -2,13 +2,15 @@
 
 #include "teqp/models/vdW.hpp"
 #include "teqp/models/cubics.hpp"
+#include "teqp/models/CPA.hpp"
 
 #include "nlohmann/json.hpp"
 
 using vad = std::valarray<double>;
 using cub = decltype(canonical_PR(vad{}, vad{}, vad{}));
+using cpatype = decltype(CPA::CPAfactory(nlohmann::json{})); // The type returned by the factory function
 
-using AllowedModels = std::variant<vdWEOS1, cub>;
+using AllowedModels = std::variant<vdWEOS1, cub, cpatype>;
 
 AllowedModels build_model(const nlohmann::json& json) {
 
@@ -27,6 +29,9 @@ AllowedModels build_model(const nlohmann::json& json) {
         std::valarray<double> Tc_K = spec.at("Tcrit / K"), pc_Pa = spec.at("pcrit / Pa"), acentric = spec.at("acentric");
         return canonical_SRK(Tc_K, pc_Pa, acentric);
     }
+    else if (kind == "CPA") {
+        return CPA::CPAfactory(spec);
+    }
     else {
         throw teqpcException(30, "Unknown kind:" + kind);
     }
diff --git a/interface/C/teqpc.cpp b/interface/C/teqpc.cpp
index e678f8f..d72f2ba 100644
--- a/interface/C/teqpc.cpp
+++ b/interface/C/teqpc.cpp
@@ -168,6 +168,30 @@ TEST_CASE("Use of C interface with simple models") {
         REQUIRE(e3 == 0);
         return val;
     };
+
+    BENCHMARK("CPA") {
+        nlohmann::json water = {
+            {"a0i / Pa m^6/mol^2",0.12277 }, {"bi / m^3/mol", 0.000014515}, {"c1", 0.67359}, {"Tc / K", 647.096},
+            {"epsABi / J/mol", 16655.0}, {"betaABi", 0.0692}, {"class", "4C"}
+        };
+        nlohmann::json jCPA = {
+            {"cubic","SRK"},
+            {"pures", {water}},
+            {"R_gas / J/mol/K", 8.3144598}
+        };
+        nlohmann::json j = {
+            {"kind", "CPA"},
+            {"model", jCPA}
+        };
+        std::string jstring = j.dump();
+        int e1 = build_model(jstring.c_str(), uuid, errmsg, errmsg_length);
+        int e2 = get_Arxy(uuid, 0, 1, 300, 3.0e-6, &(molefrac[0]), molefrac.size(), &val, errmsg, errmsg_length);
+        int e3 = free_model(uuid, errmsg, errmsg_length);
+        REQUIRE(e1 == 0);
+        REQUIRE(e2 == 0);
+        REQUIRE(e3 == 0);
+        return val;
+    };
 }
 #else 
 int main() {
-- 
GitLab