From 5957a05b62f88f041cb0dec831fe62ade55d8393 Mon Sep 17 00:00:00 2001 From: Ian Bell <ian.bell@nist.gov> Date: Thu, 2 Feb 2023 14:08:33 -0500 Subject: [PATCH] Improve the setting of departure and BIP in build_multifluid_model Can also specify them as JSON-formatted string, to make fitting a bit easier, for instance --- include/teqp/models/multifluid.hpp | 45 +++++++++++++++++++++++++---- src/tests/catch_test_multifluid.cxx | 17 +++++++++++ 2 files changed, 57 insertions(+), 5 deletions(-) diff --git a/include/teqp/models/multifluid.hpp b/include/teqp/models/multifluid.hpp index be804d1..950067d 100644 --- a/include/teqp/models/multifluid.hpp +++ b/include/teqp/models/multifluid.hpp @@ -914,11 +914,46 @@ inline auto make_pure_components_JSON(const nlohmann::json& components, const st inline auto build_multifluid_model(const std::vector<std::string>& components, const std::string& coolprop_root, const std::string& BIPcollectionpath = {}, const nlohmann::json& flags = {}, const std::string& departurepath = {}) { - std::string BIPpath = (BIPcollectionpath.empty()) ? coolprop_root + "/dev/mixtures/mixture_binary_pairs.json" : BIPcollectionpath; - const auto BIPcollection = load_a_JSON_file(BIPpath); - - std::string deppath = (departurepath.empty()) ? coolprop_root + "/dev/mixtures/mixture_departure_functions.json" : departurepath; - const auto depcollection = load_a_JSON_file(deppath); + + auto is_valid_path = [](const std::string & s){ + try{ + std::filesystem::is_regular_file(s); + return true; + } + catch(...){ + return false; + } + }; + + nlohmann::json BIPcollection = nlohmann::json::array(); + // If not provided, default values + if (BIPcollectionpath.empty()){ + auto BIPpath = coolprop_root + "/dev/mixtures/mixture_binary_pairs.json"; + BIPcollection = load_a_JSON_file(BIPpath); + } + // If path to existing file, use it + else if (is_valid_path(BIPcollectionpath) && std::filesystem::is_regular_file(BIPcollectionpath)){ + BIPcollection = load_a_JSON_file(BIPcollectionpath); + } + // Or assume it is a string in JSON format + else{ + BIPcollection = nlohmann::json::parse(BIPcollectionpath); + } + + nlohmann::json depcollection = nlohmann::json::array(); + // If not provided, default values + if (departurepath.empty()){ + std::string deppath = coolprop_root + "/dev/mixtures/mixture_departure_functions.json"; + depcollection = load_a_JSON_file(deppath); + } + // If path to existing file, use it + else if (is_valid_path(departurepath) && std::filesystem::is_regular_file(departurepath)){ + depcollection = load_a_JSON_file(departurepath); + } + // Or assume it is a string in JSON format + else{ + depcollection = nlohmann::json::parse(departurepath); + } return _build_multifluid_model(make_pure_components_JSON(components, coolprop_root), BIPcollection, depcollection, flags); } diff --git a/src/tests/catch_test_multifluid.cxx b/src/tests/catch_test_multifluid.cxx index b277f4a..9a72a7f 100644 --- a/src/tests/catch_test_multifluid.cxx +++ b/src/tests/catch_test_multifluid.cxx @@ -191,6 +191,23 @@ TEST_CASE("Check mixing absolute and relative paths and fluid names", "[multiflu } } +TEST_CASE("Check specifying some different kinds of sources of BIP", "[multifluidBIP]") { + std::string root = "../mycp"; + SECTION("Not JSON, should throw") { + std::vector<std::string> paths = { std::filesystem::absolute(root + "/dev/fluids/Nitrogen.json").string(), "Ethane" }; + CHECK_THROWS(build_multifluid_model(paths, root, "I am not a JSON formatted string")); + } + SECTION("The normal approach") { + std::vector<std::string> paths = { std::filesystem::absolute(root + "/dev/fluids/Nitrogen.json").string(), "Ethane" }; + auto model = build_multifluid_model(paths, root, root + "/dev/mixtures/mixture_binary_pairs.json"); + } + SECTION("Sending the contents in JSON format") { + std::vector<std::string> paths = { std::filesystem::absolute(root + "/dev/fluids/Nitrogen.json").string(), "PROPANE" }; + auto BIP = load_a_JSON_file(root + "/dev/mixtures/mixture_binary_pairs.json"); + auto model = build_multifluid_model(paths, root, BIP.dump()); + } +} + 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