teqp 0.19.1
Loading...
Searching...
No Matches
teqp_impl_factory.cpp
Go to the documentation of this file.
2#include "teqp/models/fwd.hpp"
4
5// This large block of schema definitions is populated by cmake
6// at cmake configuration time
7extern const nlohmann::json model_schema_library;
8
9namespace teqp {
10 namespace cppinterface {
11
12 std::unique_ptr<teqp::cppinterface::AbstractModel> make_SAFTVRMie(const nlohmann::json &j);
13
15 using namespace teqp::cppinterface::adapter;
16
17 nlohmann::json get_model_schema(const std::string& kind) { return model_schema_library.at(kind); }
18
19 // A list of factory functions that maps from EOS kind to factory function
20 // The factory function returns a pointer to an AbstractModel (but which is an instance of a derived class)
21 static std::unordered_map<std::string, makefunc> pointer_factory = {
22 {"vdW1", [](const nlohmann::json& spec){ return make_owned(vdWEOS1(spec.at("a"), spec.at("b"))); }},
23 {"vdW", [](const nlohmann::json& spec){ return make_owned(vdWEOS<double>(spec.at("Tcrit / K"), spec.at("pcrit / Pa"))); }},
24 {"PR", [](const nlohmann::json& spec){ return make_owned(make_canonicalPR(spec));}},
25 {"SRK", [](const nlohmann::json& spec){ return make_owned(make_canonicalSRK(spec));}},
26 {"cubic", [](const nlohmann::json& spec){ return make_owned(make_generalizedcubic(spec));}},
27 {"QCPRAasen", [](const nlohmann::json& spec){ return make_owned(QuantumCorrectedPR(spec));}},
28 {"advancedPRaEres", [](const nlohmann::json& spec){ return make_owned(make_AdvancedPRaEres(spec));}},
29 {"RKPRCismondi2005", [](const nlohmann::json& spec){ return make_owned(RKPRCismondi2005(spec));}},
30
31 {"CPA", [](const nlohmann::json& spec){ return make_owned(CPA::CPAfactory(spec));}},
32 {"PCSAFT", [](const nlohmann::json& spec){ return make_owned(PCSAFT::PCSAFTfactory(spec));}},
33
34 {"LKP", [](const nlohmann::json& spec){ return make_owned(LKP::make_LKPMix(spec));}},
35
36 {"multifluid", [](const nlohmann::json& spec){ return make_owned(multifluidfactory(spec));}},
37 {"multifluid-ECS-HuberEly1994", [](const nlohmann::json& spec){ return make_owned(ECSHuberEly::ECSHuberEly1994(spec));}},
38 {"SW_EspindolaHeredia2009", [](const nlohmann::json& spec){ return make_owned(squarewell::EspindolaHeredia2009(spec.at("lambda")));}},
39 {"EXP6_Kataoka1992", [](const nlohmann::json& spec){ return make_owned(exp6::Kataoka1992(spec.at("alpha")));}},
40 {"AmmoniaWaterTillnerRoth", [](const nlohmann::json& /*spec*/){ return make_owned(AmmoniaWaterTillnerRoth());}},
41 {"LJ126_TholJPCRD2016", [](const nlohmann::json& /*spec*/){ return make_owned(build_LJ126_TholJPCRD2016());}},
42 {"LJ126_KolafaNezbeda1994", [](const nlohmann::json& /*spec*/){ return make_owned(LJ126KolafaNezbeda1994());}},
43 {"LJ126_Johnson1993", [](const nlohmann::json& /*spec*/){ return make_owned(LJ126Johnson1993());}},
44 {"Mie_Pohl2023", [](const nlohmann::json& spec){ return make_owned(Mie::Mie6Pohl2023(spec.at("lambda_a")));}},
45 {"2CLJF-Dipole", [](const nlohmann::json& spec){ return make_owned(twocenterljf::build_two_center_model_dipole(spec.at("author"), spec.at("L^*"), spec.at("(mu^*)^2")));}},
46 {"2CLJF-Quadrupole", [](const nlohmann::json& spec){ return make_owned(twocenterljf::build_two_center_model_quadrupole(spec.at("author"), spec.at("L^*"), spec.at("(Q^*)^2")));}},
47 {"IdealHelmholtz", [](const nlohmann::json& spec){ return make_owned(IdealHelmholtz(spec));}},
48
49 {"GERG2004resid", [](const nlohmann::json& spec){ return make_owned(GERG2004::GERG2004ResidualModel(spec.at("names")));}},
50 {"GERG2008resid", [](const nlohmann::json& spec){ return make_owned(GERG2008::GERG2008ResidualModel(spec.at("names")));}},
51 {"GERG2004idealgas", [](const nlohmann::json& spec){ return make_owned(GERG2004::GERG2004IdealGasModel(spec.at("names")));}},
52 {"GERG2008idealgas", [](const nlohmann::json& spec){ return make_owned(GERG2008::GERG2008IdealGasModel(spec.at("names")));}},
53
54 // Implemented in its own compilation unit to help with compilation time
55 {"SAFT-VR-Mie", [](const nlohmann::json& spec){ return make_SAFTVRMie(spec); }}
56 };
57
58 std::unique_ptr<teqp::cppinterface::AbstractModel> build_model_ptr(const nlohmann::json& json, const bool validate) {
59
60 // Extract the name of the model and the model parameters
61 std::string kind = json.at("kind");
62 auto spec = json.at("model");
63
64 // Read in flag to enable/disable validation, if present
65 bool validate_in_json = json.value("validate", true);
66
67 auto itr = pointer_factory.find(kind);
68 if (itr != pointer_factory.end()){
69 if (validate || validate_in_json){
70 if (model_schema_library.contains(kind)){
71 // This block is not thread-safe, needs a mutex or something
72 JSONValidator validator(model_schema_library.at(kind));
73 if (!validator.is_valid(spec)){
75 }
76 }
77 }
78 return (itr->second)(spec);
79 }
80 else{
81 throw std::invalid_argument("Don't understand \"kind\" of: " + kind);
82 }
83 }
84
85 std::unique_ptr<AbstractModel> make_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) {
86 return make_owned(build_multifluid_model(components, coolprop_root, BIPcollectionpath, flags, departurepath));
87 }
88
89 std::unique_ptr<AbstractModel> make_model(const nlohmann::json& j, const bool validate) {
90 return build_model_ptr(j, validate);
91 }
92
94 if (pointer_factory.find(key) == pointer_factory.end()){
95 pointer_factory[key] = func;
96 }
97 else{
98 throw teqp::InvalidArgument("key is already present, overwriting is not currently allowed");
99 }
100 }
101 }
102}
Ideal-gas Helmholtz energy container.
Validation of a JSON schema failed.
bool is_valid(const nlohmann::json &j) const
std::vector< std::string > get_validation_errors(const nlohmann::json &j) const
A (very) simple implementation of the van der Waals EOS.
Definition vdW.hpp:10
A slightly more involved implementation of van der Waals, this time with mixture properties.
Definition vdW.hpp:45
auto CPAfactory(const nlohmann::json &j)
Definition CPA.hpp:301
auto make_LKPMix(const nlohmann::json &j)
Definition LKP.hpp:100
auto PCSAFTfactory(const nlohmann::json &spec)
A JSON-based factory function for the PC-SAFT model.
Definition pcsaft.hpp:426
auto make_owned(const TemplatedModel &tmodel)
std::unique_ptr< AbstractModel > make_model(const nlohmann::json &, bool validate=true)
std::function< std::unique_ptr< teqp::cppinterface::AbstractModel >(const nlohmann::json &j)> ModelPointerFactoryFunction
Definition teqpcpp.hpp:194
std::unique_ptr< teqp::cppinterface::AbstractModel > make_SAFTVRMie(const nlohmann::json &j)
std::unique_ptr< AbstractModel > make_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={})
void add_model_pointer_factory_function(const std::string &key, ModelPointerFactoryFunction &func)
This function allows you to inject your own model factory function into the set of factory functions ...
ModelPointerFactoryFunction makefunc
std::unique_ptr< AbstractModel > build_model_ptr(const nlohmann::json &json, bool validate=true)
nlohmann::json get_model_schema(const std::string &kind)
Return the schema for the given model kind.
auto build_two_center_model_dipole(const std::string &model_version, const double &L=0.0, const double &mu_sq=0.0)
auto build_two_center_model_quadrupole(const std::string &model_version, const double &L=0.0, const double &Q_sq=0.0)
auto make_generalizedcubic(const nlohmann::json &spec)
A JSON-based factory function for the generalized cubic + alpha.
Definition cubics.hpp:342
auto make_AdvancedPRaEres(const nlohmann::json &j)
Definition cubics.hpp:728
auto build_LJ126_TholJPCRD2016()
auto make_canonicalPR(const nlohmann::json &spec)
A JSON-based factory function for the canonical SRK model.
Definition cubics.hpp:332
auto multifluidfactory(const nlohmann::json &spec)
Load a model from a JSON data structure.
auto build_multifluid_model(const std::vector< std::string > &components, const std::string &root, const std::string &BIPcollectionpath={}, const nlohmann::json &flags={}, const std::string &departurepath={})
auto make_canonicalSRK(const nlohmann::json &spec)
A JSON-based factory function for the canonical SRK model.
Definition cubics.hpp:288
const nlohmann::json model_schema_library