From 4c09c323f58b6aea4dc4b12ff58da341a7cd2a13 Mon Sep 17 00:00:00 2001
From: Ian Bell <ian.bell@nist.gov>
Date: Sun, 24 Oct 2021 16:13:32 -0400
Subject: [PATCH] Throw if temperatures are out of range to superancillaries

---
 include/teqp/models/cubicsuperancillary.hpp | 7 +++++++
 src/tests/catch_test_cubics.cxx             | 8 ++++++++
 2 files changed, 15 insertions(+)

diff --git a/include/teqp/models/cubicsuperancillary.hpp b/include/teqp/models/cubicsuperancillary.hpp
index 6c6ecbf..d4c556d 100644
--- a/include/teqp/models/cubicsuperancillary.hpp
+++ b/include/teqp/models/cubicsuperancillary.hpp
@@ -50,6 +50,13 @@ public:
 
     /// Evaluate the SuperAncillary
     double y(double x) const{
+        // First check whether the input is possible
+        if (x < exps[0].xmin) {
+            throw std::invalid_argument("Ttilde (" + std::to_string(x) + ") is below the minimum of " + std::to_string(exps[0].xmin));
+        }
+        if (x > exps.back().xmax) {
+            throw std::invalid_argument("Ttilde (" + std::to_string(x) + ") is above the maximum of " + std::to_string(exps.back().xmax));
+        }
         // Bisection to find the expansion
         // we need
         auto i = get_index(x);
diff --git a/src/tests/catch_test_cubics.cxx b/src/tests/catch_test_cubics.cxx
index 737f920..6d10711 100644
--- a/src/tests/catch_test_cubics.cxx
+++ b/src/tests/catch_test_cubics.cxx
@@ -40,6 +40,14 @@ TEST_CASE("Check calling superancillary curves", "[cubic][superanc]")
         auto [rhoL, rhoV] = model.superanc_rhoLV(130.0);
         CHECK(rhoL > rhoV);
     }
+    SECTION("PR super large temp") {
+        auto model = canonical_PR(Tc_K, pc_Pa, acentric);
+        CHECK_THROWS(model.superanc_rhoLV(1.3e6));
+    }
+    SECTION("PR super small temp") {
+        auto model = canonical_PR(Tc_K, pc_Pa, acentric);
+        CHECK_THROWS(model.superanc_rhoLV(1.3e-10));
+    }
     SECTION("SRK") {
         auto model = canonical_SRK(Tc_K, pc_Pa, acentric);
         auto [rhoL, rhoV] = model.superanc_rhoLV(130.0);
-- 
GitLab