From ce0a20f238ceb736631c77a9fd7e5097ef621fbb Mon Sep 17 00:00:00 2001
From: Ian Bell <ian.bell@nist.gov>
Date: Mon, 8 Nov 2021 18:32:59 -0500
Subject: [PATCH] Fix mixing of absolute paths and identifiers requiring
 backwards lookup

---
 include/teqp/models/multifluid.hpp  | 33 +++++++++++++++++++++++------
 src/tests/catch_test_multifluid.cxx | 12 +++++++++++
 2 files changed, 38 insertions(+), 7 deletions(-)

diff --git a/include/teqp/models/multifluid.hpp b/include/teqp/models/multifluid.hpp
index 1397390..0735d97 100644
--- a/include/teqp/models/multifluid.hpp
+++ b/include/teqp/models/multifluid.hpp
@@ -901,8 +901,18 @@ inline auto build_alias_map(const std::string& root) {
     std::map<std::string, std::string> aliasmap;
     for (auto path : get_files_in_folder(root + "/dev/fluids", ".json")) {
         auto j = load_a_JSON_file(path.string());
+        std::string REFPROP_name = j.at("INFO").at("REFPROP_NAME"); 
+        std::string name = j.at("INFO").at("NAME");
         for (std::string k : {"NAME", "CAS", "REFPROP_NAME"}) {
-            std::string val = j.at("INFO").at(k); 
+            std::string val = j.at("INFO").at(k);
+            // Skip REFPROP names that match the fluid itself
+            if (k == "REFPROP_NAME" && val == name) {
+                continue;
+            }
+            // Skip invalid REFPROP names
+            if (k == "REFPROP_NAME" && val == "N/A") {
+                continue;
+            }
             if (aliasmap.count(val) > 0) {
                 throw std::invalid_argument("Duplicated reverse lookup identifier ["+k+"] found in file:" + path.string());
             }
@@ -911,12 +921,15 @@ inline auto build_alias_map(const std::string& root) {
             }
         }
         std::vector<std::string> aliases = j.at("INFO").at("ALIASES");
+        
         for (std::string alias : aliases) {
-            if (aliasmap.count(alias) > 0) {
-                throw std::invalid_argument("Duplicated alias [" + alias + "] found in file:" + path.string());
-            }
-            else {
-                aliasmap[alias] = std::filesystem::absolute(path).string();
+            if (alias != REFPROP_name && alias != name) { // Don't add REFPROP name or base name, were already above to list of aliases
+                if (aliasmap.count(alias) > 0) {
+                    throw std::invalid_argument("Duplicated alias [" + alias + "] found in file:" + path.string());
+                }
+                else {
+                    aliasmap[alias] = std::filesystem::absolute(path).string();
+                }
             }
         }
     }
@@ -942,7 +955,13 @@ inline auto build_multifluid_model(const std::vector<std::string>& components, c
         auto aliasmap = build_alias_map(coolprop_root);
         std::vector<std::string> abspaths;
         for (auto c : components) {
-            abspaths.push_back(aliasmap[c]);
+            // Allow matching of absolute paths first
+            if (std::filesystem::exists(c)) {
+                abspaths.push_back(c);
+            }
+            else {
+                abspaths.push_back(aliasmap[c]);
+            }
         }
         // Backup lookup with absolute paths resolved for each component
         pureJSON = collect_component_json(abspaths, coolprop_root);
diff --git a/src/tests/catch_test_multifluid.cxx b/src/tests/catch_test_multifluid.cxx
index 634e898..c878ea4 100644
--- a/src/tests/catch_test_multifluid.cxx
+++ b/src/tests/catch_test_multifluid.cxx
@@ -82,6 +82,18 @@ TEST_CASE("Check that mixtures can also do absolute paths", "[multifluid],[abspa
     }
 }
 
+TEST_CASE("Check mixing absolute and relative paths and fluid names", "[multifluid],[abspath]") {
+    std::string root = "../mycp";
+    SECTION("With correct name of fluid") {
+        std::vector<std::string> paths = { std::filesystem::absolute(root + "/dev/fluids/Methane.json").string(), "Ethane" };
+        auto model = build_multifluid_model(paths, root, root + "/dev/mixtures/mixture_binary_pairs.json");
+    }
+    SECTION("Needing a reverse lookup for one fluid") {
+        std::vector<std::string> paths = { std::filesystem::absolute(root + "/dev/fluids/Methane.json").string(), "PROPANE" };
+        auto model = build_multifluid_model(paths, root, root + "/dev/mixtures/mixture_binary_pairs.json");
+    }
+}
+
 TEST_CASE("Check that all binary pairs specified in the binary pair file can be instantiated", "[multifluid],[binaries]") {
     std::string root = "../mycp";
     REQUIRE_NOTHROW(build_alias_map(root));
-- 
GitLab