diff --git a/include/teqp/exceptions.hpp b/include/teqp/exceptions.hpp new file mode 100644 index 0000000000000000000000000000000000000000..1b015083cc07c032f641dd37f1c117fe5e0990c9 --- /dev/null +++ b/include/teqp/exceptions.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include <exception> + +class teqpcException : public std::exception { +public: + const int code; + const std::string msg; + teqpcException(int code, const std::string& msg) : code(code), msg(msg) {}; + const char *what() const override { + return msg.c_str(); + } +}; \ No newline at end of file diff --git a/include/teqp/json_builder.hpp b/include/teqp/json_builder.hpp new file mode 100644 index 0000000000000000000000000000000000000000..e57809249d8e174b906bf4ce7ef593433659c91c --- /dev/null +++ b/include/teqp/json_builder.hpp @@ -0,0 +1,33 @@ +#pragma once + +#include "teqp/models/vdW.hpp" +#include "teqp/models/cubics.hpp" + +#include "nlohmann/json.hpp" + +using vad = std::valarray<double>; +using cub = decltype(canonical_PR(vad{}, vad{}, vad{})); + +using AllowedModels = std::variant<vdWEOS1, cub>; + +AllowedModels build_model(const nlohmann::json& json) { + + // Extract the name of the model and the model parameters + std::string kind = json.at("kind"); + auto spec = json.at("model"); + + if (kind == "vdW1") { + return vdWEOS1(spec.at("a"), spec.at("b")); + } + else if (kind == "PR") { + std::valarray<double> Tc_K = spec.at("Tcrit / K"), pc_Pa = spec.at("pcrit / Pa"), acentric = spec.at("acentric"); + return canonical_PR(Tc_K, pc_Pa, acentric); + } + else if (kind == "SRK") { + std::valarray<double> Tc_K = spec.at("Tcrit / K"), pc_Pa = spec.at("pcrit / Pa"), acentric = spec.at("acentric"); + return canonical_SRK(Tc_K, pc_Pa, acentric); + } + else { + throw teqpcException(30, "Unknown kind:" + kind); + } +} diff --git a/interface/C/teqpc.cpp b/interface/C/teqpc.cpp index 4bb290c3e98a73029541fbbedd794440a1dabe6a..e678f8fc65829738a4ad0b5e04567d2469db86d2 100644 --- a/interface/C/teqpc.cpp +++ b/interface/C/teqpc.cpp @@ -7,15 +7,9 @@ #include <variant> #include <atomic> -#include "teqp/models/vdW.hpp" -#include "teqp/models/cubics.hpp" #include "teqp/derivs.hpp" - -using vad = std::valarray<double>; -using cub = decltype(canonical_PR(vad{}, vad{}, vad{})); - -using AllowedModels = std::variant<vdWEOS1, cub>; -std::unordered_map<std::string, AllowedModels> library; +#include "teqp/exceptions.hpp" +#include "teqp/json_builder.hpp" // An atomic is used here for thread safety // The max possible index is 18,446,744,073,709,551,615 @@ -28,12 +22,7 @@ std::string get_uid(int N) { return std::string(N - s.size(), '0') + s; } -class teqpcException : public std::exception { -public: - const int code; - const std::string msg; - teqpcException(int code, const std::string& msg) : code(code), msg(msg) {} -}; +std::unordered_map<std::string, AllowedModels> library; void exception_handler(int& errcode, char* message_buffer, const int buffer_length) { @@ -54,26 +43,12 @@ int build_model(const char* j, char* uuid, char* errmsg, int errmsg_length){ int errcode = 0; try{ nlohmann::json json = nlohmann::json::parse(j); - - // Extract the name of the model and the model parameters - std::string kind = json.at("kind"); - auto spec = json.at("model"); - std::string uid = get_uid(32); - - if (kind == "vdW1") { - library.emplace(std::make_pair(uid, vdWEOS1(spec.at("a"), spec.at("b")))); - } - else if (kind == "PR") { - std::valarray<double> Tc_K = spec.at("Tcrit / K"), pc_Pa = spec.at("pcrit / Pa"), acentric = spec.at("acentric"); - library.emplace(std::make_pair(uid, canonical_PR(Tc_K, pc_Pa, acentric))); - } - else if (kind == "SRK") { - std::valarray<double> Tc_K = spec.at("Tcrit / K"), pc_Pa = spec.at("pcrit / Pa"), acentric = spec.at("acentric"); - library.emplace(std::make_pair(uid, canonical_SRK(Tc_K, pc_Pa, acentric))); + try { + library.emplace(std::make_pair(uid, build_model(json))); } - else { - throw teqpcException(30, "Unknown kind:" + kind); + catch (std::exception &e) { + throw teqpcException(30, "Unable to load with error:" + std::string(e.what())); } strcpy(uuid, uid.c_str()); }