From 8ac5375b31f51b960f26e1fe5c6617af18447138 Mon Sep 17 00:00:00 2001
From: Ian Bell <ian.bell@nist.gov>
Date: Tue, 28 Mar 2023 15:18:32 -0400
Subject: [PATCH] Additional tests for non-provided kmat

Another bugfix release is needed
---
 include/teqp/json_builder.hpp   |  7 +++--
 src/tests/catch_test_cubics.cxx | 52 +++++++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+), 2 deletions(-)

diff --git a/include/teqp/json_builder.hpp b/include/teqp/json_builder.hpp
index 2c09ca1..a346a9f 100644
--- a/include/teqp/json_builder.hpp
+++ b/include/teqp/json_builder.hpp
@@ -13,6 +13,9 @@ namespace teqp {
     inline AllowedModels build_model(const nlohmann::json& json) {
         
         auto build_square_matrix = [](const nlohmann::json& j){
+            if (j.is_null() || (j.is_array() && j.size() == 0)){
+                return Eigen::ArrayXXd(0, 0);
+            }
             try{
                 const std::valarray<std::valarray<double>> m = j;
                 // First assume that the matrix is square, resize
@@ -51,7 +54,7 @@ namespace teqp {
             std::valarray<double> Tc_K = spec.at("Tcrit / K"), pc_Pa = spec.at("pcrit / Pa"), acentric = spec.at("acentric");
             Eigen::ArrayXXd kmat(0, 0);
             if (spec.contains("kmat")){
-                kmat = build_square_matrix(spec["kmat"]);
+                kmat = build_square_matrix(spec.at("kmat"));
             }
             return canonical_PR(Tc_K, pc_Pa, acentric, kmat);
         }
@@ -59,7 +62,7 @@ namespace teqp {
             std::valarray<double> Tc_K = spec.at("Tcrit / K"), pc_Pa = spec.at("pcrit / Pa"), acentric = spec.at("acentric");
             Eigen::ArrayXXd kmat(0, 0);
             if (spec.contains("kmat")){
-                kmat = build_square_matrix(spec["kmat"]);
+                kmat = build_square_matrix(spec.at("kmat"));
             }
             return canonical_SRK(Tc_K, pc_Pa, acentric, kmat);
         }
diff --git a/src/tests/catch_test_cubics.cxx b/src/tests/catch_test_cubics.cxx
index 4de15b4..1fb237d 100644
--- a/src/tests/catch_test_cubics.cxx
+++ b/src/tests/catch_test_cubics.cxx
@@ -9,6 +9,7 @@ using Catch::Approx;
 #include "teqp/models/cubics.hpp"
 #include "teqp/derivs.hpp"
 #include "teqp/algorithms/VLE.hpp"
+#include "teqp/cpp/teqpcpp.hpp"
 
 #include <boost/numeric/odeint/stepper/euler.hpp>
 #include <boost/numeric/odeint/stepper/runge_kutta_cash_karp54.hpp>
@@ -431,3 +432,54 @@ TEST_CASE("Check manual integration of subcritical VLE isobar for binary mixture
         std::ofstream file("isoP.json"); file << J;
     }
 }
+
+TEST_CASE("Bad kmat options", "[PRkmat]"){
+    SECTION("null; ok"){
+        auto j = nlohmann::json::parse(R"({
+            "kind": "PR",
+            "model": {
+                "Tcrit / K": [190],
+                "pcrit / Pa": [3.5e6],
+                "acentric": [0.11],
+                "kmat": null
+            }
+        })");
+        CHECK_NOTHROW(teqp::cppinterface::make_model(j));
+    }
+    SECTION("empty; ok"){
+        auto j = nlohmann::json::parse(R"({
+            "kind": "PR",
+            "model": {
+                "Tcrit / K": [190],
+                "pcrit / Pa": [3.5e6],
+                "acentric": [0.11],
+                "kmat": []
+            }
+        })");
+        CHECK_NOTHROW(teqp::cppinterface::make_model(j));
+    }
+    SECTION("empty for two components; ok"){
+        auto j = nlohmann::json::parse(R"({
+            "kind": "PR",
+            "model": {
+                "Tcrit / K": [190,200],
+                "pcrit / Pa": [3.5e6,4e6],
+                "acentric": [0.11,0.2],
+                "kmat": []
+            }
+        })");
+        CHECK_NOTHROW(teqp::cppinterface::make_model(j));
+    }
+    SECTION("wrong size for two components; fail"){
+        auto j = nlohmann::json::parse(R"({
+            "kind": "PR",
+            "model": {
+                "Tcrit / K": [190,200],
+                "pcrit / Pa": [3.5e6,4e6],
+                "acentric": [0.11,0.2],
+                "kmat": [0.001]
+            }
+        })");
+        CHECK_THROWS(teqp::cppinterface::make_model(j));
+    }
+}
-- 
GitLab