teqp 0.23.1
Loading...
Searching...
No Matches
teqp_impl_factory.cpp
Go to the documentation of this file.
2
3#include "teqp/models/vdW.hpp"
6
7// This large block of schema definitions is populated by cmake
8// at cmake configuration time
9extern const nlohmann::json model_schema_library;
10
11namespace teqp {
12 namespace cppinterface {
13
14 std::unique_ptr<teqp::cppinterface::AbstractModel> make_SAFTVRMie(const nlohmann::json &);
15 std::unique_ptr<teqp::cppinterface::AbstractModel> make_PCSAFT(const nlohmann::json &);
16 std::unique_ptr<teqp::cppinterface::AbstractModel> make_PCSAFTPureGrossSadowski2001(const nlohmann::json &);
17 std::unique_ptr<teqp::cppinterface::AbstractModel> make_SOFTSAFT(const nlohmann::json &);
18 std::unique_ptr<teqp::cppinterface::AbstractModel> make_genericSAFT(const nlohmann::json &);
19
20 std::unique_ptr<teqp::cppinterface::AbstractModel> make_GERG2004resid(const nlohmann::json &);
21 std::unique_ptr<teqp::cppinterface::AbstractModel> make_GERG2008resid(const nlohmann::json &);
22 std::unique_ptr<teqp::cppinterface::AbstractModel> make_GERG2004idealgas(const nlohmann::json &);
23 std::unique_ptr<teqp::cppinterface::AbstractModel> make_GERG2008idealgas(const nlohmann::json &);
24 std::unique_ptr<teqp::cppinterface::AbstractModel> make_LKP(const nlohmann::json &);
25 std::unique_ptr<teqp::cppinterface::AbstractModel> make_multifluid(const nlohmann::json &);
26 std::unique_ptr<teqp::cppinterface::AbstractModel> make_multifluid_association(const nlohmann::json &);
27 std::unique_ptr<teqp::cppinterface::AbstractModel> make_multifluid_activity(const nlohmann::json &);
28 std::unique_ptr<teqp::cppinterface::AbstractModel> make_multifluid_ECS_HuberEly1994(const nlohmann::json &);
29 std::unique_ptr<teqp::cppinterface::AbstractModel> make_AmmoniaWaterTillnerRoth();
30 std::unique_ptr<teqp::cppinterface::AbstractModel> make_LJ126_TholJPCRD2016();
31 std::unique_ptr<teqp::cppinterface::AbstractModel> make_LJ126_KolafaNezbeda1994();
32 std::unique_ptr<teqp::cppinterface::AbstractModel> make_LJ126_Johnson1993();
33
34 std::unique_ptr<teqp::cppinterface::AbstractModel> make_SW_EspindolaHeredia2009(const nlohmann::json &);
35 std::unique_ptr<teqp::cppinterface::AbstractModel> make_EXP6_Kataoka1992(const nlohmann::json &);
36 std::unique_ptr<teqp::cppinterface::AbstractModel> make_Mie_Pohl2023(const nlohmann::json &);
37 std::unique_ptr<teqp::cppinterface::AbstractModel> make_Mie_Chaparro2023(const nlohmann::json &);
38 std::unique_ptr<teqp::cppinterface::AbstractModel> make_2CLJF(const nlohmann::json &);
39 std::unique_ptr<teqp::cppinterface::AbstractModel> make_2CLJF_Dipole(const nlohmann::json &);
40 std::unique_ptr<teqp::cppinterface::AbstractModel> make_2CLJF_Quadrupole(const nlohmann::json &);
41
42 std::unique_ptr<teqp::cppinterface::AbstractModel> make_CPA(const nlohmann::json &);
43 std::unique_ptr<teqp::cppinterface::AbstractModel> make_advancedPRaEres(const nlohmann::json &);
44
45 std::unique_ptr<teqp::cppinterface::AbstractModel> make_IdealHelmholtz(const nlohmann::json &);
46
48 using namespace teqp::cppinterface::adapter;
49
50 nlohmann::json get_model_schema(const std::string& kind) { return model_schema_library.at(kind); }
51
52 // A list of factory functions that maps from EOS kind to factory function
53 // The factory function returns a pointer to an AbstractModel (but which is an instance of a derived class)
54 static std::unordered_map<std::string, makefunc> pointer_factory = {
55 {"vdW1", [](const nlohmann::json& spec){ return make_owned(vdWEOS1(spec.at("a"), spec.at("b"))); }},
56 {"vdW", [](const nlohmann::json& spec){ return make_owned(vdWEOS<double>(spec.at("Tcrit / K"), spec.at("pcrit / Pa"))); }},
57 {"PR", [](const nlohmann::json& spec){ return make_owned(make_canonicalPR(spec));}},
58 {"SRK", [](const nlohmann::json& spec){ return make_owned(make_canonicalSRK(spec));}},
59 {"cubic", [](const nlohmann::json& spec){ return make_owned(make_generalizedcubic(spec));}},
60 {"QCPRAasen", [](const nlohmann::json& spec){ return make_owned(QuantumCorrectedPR(spec));}},
61 {"RKPRCismondi2005", [](const nlohmann::json& spec){ return make_owned(RKPRCismondi2005(spec));}},
62
63 {"advancedPRaEres", [](const nlohmann::json& spec){ return make_advancedPRaEres(spec);}},
64
65 // Implemented in their own compilation units to help with compilation time and memory
66 // use. Having all the template instantations in one file is handy, but requires a huge amount of RAM
67 // ---------
68 {"SAFT-VR-Mie", [](const nlohmann::json& spec){ return make_SAFTVRMie(spec); }},
69
70 {"PCSAFT", [](const nlohmann::json& spec){ return make_PCSAFT(spec); }},
71 {"PCSAFTPureGrossSadowski2001", [](const nlohmann::json& spec){ return make_PCSAFTPureGrossSadowski2001(spec); }},
72 {"SoftSAFT", [](const nlohmann::json& spec){ return make_SOFTSAFT(spec); }},
73 {"genericSAFT", [](const nlohmann::json& spec){ return make_genericSAFT(spec); }},
74
75 {"GERG2004resid", [](const nlohmann::json& spec){ return make_GERG2004resid(spec);}},
76 {"GERG2008resid", [](const nlohmann::json& spec){ return make_GERG2008resid(spec);}},
77 {"GERG2004idealgas", [](const nlohmann::json& spec){ return make_GERG2004idealgas(spec);}},
78 {"GERG2008idealgas", [](const nlohmann::json& spec){ return make_GERG2008idealgas(spec);}},
79
80 {"LKP", [](const nlohmann::json& spec){ return make_LKP(spec);}},
81
82 {"multifluid", [](const nlohmann::json& spec){ return make_multifluid(spec);}},
83 {"multifluid-ECS-HuberEly1994", [](const nlohmann::json& spec){ return make_multifluid_ECS_HuberEly1994(spec);}},
84 {"multifluid-association", [](const nlohmann::json& spec){ return make_multifluid_association(spec);}},
85 {"multifluid-activity", [](const nlohmann::json& spec){ return make_multifluid_activity(spec);}},
86 {"AmmoniaWaterTillnerRoth", [](const nlohmann::json& /*spec*/){ return make_AmmoniaWaterTillnerRoth();}},
87
88 {"LJ126_TholJPCRD2016", [](const nlohmann::json& /*spec*/){ return make_LJ126_TholJPCRD2016();}},
89 {"LJ126_KolafaNezbeda1994", [](const nlohmann::json& /*spec*/){ return make_LJ126_KolafaNezbeda1994();}},
90 {"LJ126_Johnson1993", [](const nlohmann::json& /*spec*/){ return make_LJ126_Johnson1993();}},
91 {"SW_EspindolaHeredia2009", [](const nlohmann::json& spec){ return make_SW_EspindolaHeredia2009(spec);}},
92 {"EXP6_Kataoka1992", [](const nlohmann::json& spec){ return make_EXP6_Kataoka1992(spec); }},
93 {"Mie_Pohl2023", [](const nlohmann::json& spec){ return make_Mie_Pohl2023(spec); }},
94 {"Mie_Chaparro2023", [](const nlohmann::json& spec){ return make_Mie_Chaparro2023(spec); }},
95 {"2CLJF", [](const nlohmann::json& spec){ return make_2CLJF(spec); }},
96 {"2CLJF-Dipole", [](const nlohmann::json& spec){ return make_2CLJF_Dipole(spec); }},
97 {"2CLJF-Quadrupole", [](const nlohmann::json& spec){ return make_2CLJF_Quadrupole(spec); }},
98
99 {"CPA", [](const nlohmann::json& spec){ return make_CPA(spec); }},
100
101 {"IdealHelmholtz", [](const nlohmann::json& spec){ return make_IdealHelmholtz(spec); }},
102 };
103
104 std::unique_ptr<teqp::cppinterface::AbstractModel> build_model_ptr(const nlohmann::json& json, const bool validate) {
105
106 // Extract the name of the model and the model parameters
107 std::string kind = json.at("kind");
108 auto spec = json.at("model");
109
110 auto itr = pointer_factory.find(kind);
111 if (itr != pointer_factory.end()){
112 bool do_validation = validate;
113 if (json.contains("validate")){
114 do_validation = json["validate"];
115 }
116 if (do_validation){
117 if (model_schema_library.contains(kind)){
118 // This block is not thread-safe, needs a mutex or something
119 JSONValidator validator(model_schema_library.at(kind));
120 if (!validator.is_valid(spec)){
122 }
123 }
124 }
125 return (itr->second)(spec);
126 }
127 else{
128 throw std::invalid_argument("Don't understand \"kind\" of: " + kind);
129 }
130 }
131
132 std::unique_ptr<AbstractModel> make_multifluid_model(const std::vector<std::string>& components, const std::string& root, const std::string& BIP, const nlohmann::json& flags, const std::string& departurepath) {
133 return make_multifluid({{"components", components}, {"root",root}, {"BIP", BIP}, {"flags", flags}, {"departure", departurepath}});
134 }
135
136 std::unique_ptr<AbstractModel> make_model(const nlohmann::json& j, const bool validate) {
137 return build_model_ptr(j, validate);
138 }
139
141 if (pointer_factory.find(key) == pointer_factory.end()){
142 pointer_factory[key] = func;
143 }
144 else{
145 throw teqp::InvalidArgument("key is already present, overwriting is not currently allowed");
146 }
147 }
148 }
149}
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
auto make_owned(const TemplatedModel &tmodel)
std::unique_ptr< teqp::cppinterface::AbstractModel > make_Mie_Pohl2023(const nlohmann::json &spec)
std::unique_ptr< teqp::cppinterface::AbstractModel > make_SOFTSAFT(const nlohmann::json &spec)
std::unique_ptr< teqp::cppinterface::AbstractModel > make_2CLJF(const nlohmann::json &spec)
std::unique_ptr< AbstractModel > make_model(const nlohmann::json &, bool validate=true)
std::unique_ptr< teqp::cppinterface::AbstractModel > make_LJ126_TholJPCRD2016()
std::unique_ptr< teqp::cppinterface::AbstractModel > make_2CLJF_Dipole(const nlohmann::json &spec)
std::unique_ptr< teqp::cppinterface::AbstractModel > make_multifluid_activity(const nlohmann::json &j)
std::unique_ptr< teqp::cppinterface::AbstractModel > make_GERG2004idealgas(const nlohmann::json &spec)
std::unique_ptr< teqp::cppinterface::AbstractModel > make_advancedPRaEres(const nlohmann::json &j)
std::function< std::unique_ptr< teqp::cppinterface::AbstractModel >(const nlohmann::json &j)> ModelPointerFactoryFunction
Definition teqpcpp.hpp:214
std::unique_ptr< teqp::cppinterface::AbstractModel > make_multifluid_association(const nlohmann::json &j)
std::unique_ptr< teqp::cppinterface::AbstractModel > make_SW_EspindolaHeredia2009(const nlohmann::json &spec)
std::unique_ptr< teqp::cppinterface::AbstractModel > make_LJ126_Johnson1993()
std::unique_ptr< teqp::cppinterface::AbstractModel > make_SAFTVRMie(const nlohmann::json &j)
std::unique_ptr< teqp::cppinterface::AbstractModel > make_PCSAFTPureGrossSadowski2001(const nlohmann::json &spec)
std::unique_ptr< teqp::cppinterface::AbstractModel > make_multifluid_ECS_HuberEly1994(const nlohmann::json &j)
std::unique_ptr< teqp::cppinterface::AbstractModel > make_multifluid(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={})
std::unique_ptr< teqp::cppinterface::AbstractModel > make_EXP6_Kataoka1992(const nlohmann::json &spec)
std::unique_ptr< teqp::cppinterface::AbstractModel > make_2CLJF_Quadrupole(const nlohmann::json &spec)
std::unique_ptr< teqp::cppinterface::AbstractModel > make_GERG2004resid(const nlohmann::json &spec)
std::unique_ptr< teqp::cppinterface::AbstractModel > make_CPA(const nlohmann::json &spec)
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< teqp::cppinterface::AbstractModel > make_AmmoniaWaterTillnerRoth()
std::unique_ptr< AbstractModel > build_model_ptr(const nlohmann::json &json, bool validate=true)
std::unique_ptr< teqp::cppinterface::AbstractModel > make_LJ126_KolafaNezbeda1994()
std::unique_ptr< teqp::cppinterface::AbstractModel > make_IdealHelmholtz(const nlohmann::json &spec)
std::unique_ptr< teqp::cppinterface::AbstractModel > make_PCSAFT(const nlohmann::json &spec)
std::unique_ptr< teqp::cppinterface::AbstractModel > make_GERG2008resid(const nlohmann::json &spec)
std::unique_ptr< teqp::cppinterface::AbstractModel > make_LKP(const nlohmann::json &j)
std::unique_ptr< teqp::cppinterface::AbstractModel > make_genericSAFT(const nlohmann::json &spec)
std::unique_ptr< teqp::cppinterface::AbstractModel > make_Mie_Chaparro2023(const nlohmann::json &spec)
nlohmann::json get_model_schema(const std::string &kind)
Return the schema for the given model kind.
std::unique_ptr< teqp::cppinterface::AbstractModel > make_GERG2008idealgas(const nlohmann::json &spec)
auto make_generalizedcubic(const nlohmann::json &spec)
A JSON-based factory function for the generalized cubic + alpha.
auto make_canonicalPR(const nlohmann::json &spec)
A JSON-based factory function for the canonical SRK model.
auto make_canonicalSRK(const nlohmann::json &spec)
A JSON-based factory function for the canonical SRK model.
const nlohmann::json model_schema_library