diff --git a/include/teqp/models/multifluid.hpp b/include/teqp/models/multifluid.hpp index be804d1f37d24c9e056a9bdf8c0d57efe616b9ec..950067d5af376eba5a363864142fbdf796eb4aa6 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 b277f4af48e71e4cfd63ff2ca5505ec14a33bd70..9a72a7f96f554907d5a5c6cb26da9f2f9caffc01 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));