teqp 0.22.0
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_IdealHelmholtz(const nlohmann::json &);
44
46 using namespace teqp::cppinterface::adapter;
47
48 nlohmann::json get_model_schema(const std::string& kind) { return model_schema_library.at(kind); }
49
50 // A list of factory functions that maps from EOS kind to factory function
51 // The factory function returns a pointer to an AbstractModel (but which is an instance of a derived class)
52 static std::unordered_map<std::string, makefunc> pointer_factory = {
53 {"vdW1", [](const nlohmann::json& spec){ return make_owned(vdWEOS1(spec.at("a"), spec.at("b"))); }},
54 {"vdW", [](const nlohmann::json& spec){ return make_owned(vdWEOS<double>(spec.at("Tcrit / K"), spec.at("pcrit / Pa"))); }},
55 {"PR", [](const nlohmann::json& spec){ return make_owned(make_canonicalPR(spec));}},
56 {"SRK", [](const nlohmann::json& spec){ return make_owned(make_canonicalSRK(spec));}},
57 {"cubic", [](const nlohmann::json& spec){ return make_owned(make_generalizedcubic(spec));}},
58 {"QCPRAasen", [](const nlohmann::json& spec){ return make_owned(QuantumCorrectedPR(spec));}},
59 {"advancedPRaEres", [](const nlohmann::json& spec){ return make_owned(make_AdvancedPRaEres(spec));}},
60 {"RKPRCismondi2005", [](const nlohmann::json& spec){ return make_owned(RKPRCismondi2005(spec));}},
61
62 // Implemented in their own compilation units to help with compilation time and memory
63 // use. Having all the template instantations in one file is handy, but requires a huge amount of RAM
64 // ---------
65 {"SAFT-VR-Mie", [](const nlohmann::json& spec){ return make_SAFTVRMie(spec); }},
66
67 {"PCSAFT", [](const nlohmann::json& spec){ return make_PCSAFT(spec); }},
68 {"PCSAFTPureGrossSadowski2001", [](const nlohmann::json& spec){ return make_PCSAFTPureGrossSadowski2001(spec); }},
69 {"SoftSAFT", [](const nlohmann::json& spec){ return make_SOFTSAFT(spec); }},
70 {"genericSAFT", [](const nlohmann::json& spec){ return make_genericSAFT(spec); }},
71
72 {"GERG2004resid", [](const nlohmann::json& spec){ return make_GERG2004resid(spec);}},
73 {"GERG2008resid", [](const nlohmann::json& spec){ return make_GERG2008resid(spec);}},
74 {"GERG2004idealgas", [](const nlohmann::json& spec){ return make_GERG2004idealgas(spec);}},
75 {"GERG2008idealgas", [](const nlohmann::json& spec){ return make_GERG2008idealgas(spec);}},
76
77 {"LKP", [](const nlohmann::json& spec){ return make_LKP(spec);}},
78
79 {"multifluid", [](const nlohmann::json& spec){ return make_multifluid(spec);}},
80 {"multifluid-ECS-HuberEly1994", [](const nlohmann::json& spec){ return make_multifluid_ECS_HuberEly1994(spec);}},
81 {"multifluid-association", [](const nlohmann::json& spec){ return make_multifluid_association(spec);}},
82 {"multifluid-activity", [](const nlohmann::json& spec){ return make_multifluid_activity(spec);}},
83 {"AmmoniaWaterTillnerRoth", [](const nlohmann::json& /*spec*/){ return make_AmmoniaWaterTillnerRoth();}},
84
85 {"LJ126_TholJPCRD2016", [](const nlohmann::json& /*spec*/){ return make_LJ126_TholJPCRD2016();}},
86 {"LJ126_KolafaNezbeda1994", [](const nlohmann::json& /*spec*/){ return make_LJ126_KolafaNezbeda1994();}},
87 {"LJ126_Johnson1993", [](const nlohmann::json& /*spec*/){ return make_LJ126_Johnson1993();}},
88 {"SW_EspindolaHeredia2009", [](const nlohmann::json& spec){ return make_SW_EspindolaHeredia2009(spec);}},
89 {"EXP6_Kataoka1992", [](const nlohmann::json& spec){ return make_EXP6_Kataoka1992(spec); }},
90 {"Mie_Pohl2023", [](const nlohmann::json& spec){ return make_Mie_Pohl2023(spec); }},
91 {"Mie_Chaparro2023", [](const nlohmann::json& spec){ return make_Mie_Chaparro2023(spec); }},
92 {"2CLJF", [](const nlohmann::json& spec){ return make_2CLJF(spec); }},
93 {"2CLJF-Dipole", [](const nlohmann::json& spec){ return make_2CLJF_Dipole(spec); }},
94 {"2CLJF-Quadrupole", [](const nlohmann::json& spec){ return make_2CLJF_Quadrupole(spec); }},
95
96 {"CPA", [](const nlohmann::json& spec){ return make_CPA(spec); }},
97
98 {"IdealHelmholtz", [](const nlohmann::json& spec){ return make_IdealHelmholtz(spec); }},
99 };
100
101 std::unique_ptr<teqp::cppinterface::AbstractModel> build_model_ptr(const nlohmann::json& json, const bool validate) {
102
103 // Extract the name of the model and the model parameters
104 std::string kind = json.at("kind");
105 auto spec = json.at("model");
106
107 auto itr = pointer_factory.find(kind);
108 if (itr != pointer_factory.end()){
109 bool do_validation = validate;
110 if (json.contains("validate")){
111 do_validation = json["validate"];
112 }
113 if (do_validation){
114 if (model_schema_library.contains(kind)){
115 // This block is not thread-safe, needs a mutex or something
116 JSONValidator validator(model_schema_library.at(kind));
117 if (!validator.is_valid(spec)){
119 }
120 }
121 }
122 return (itr->second)(spec);
123 }
124 else{
125 throw std::invalid_argument("Don't understand \"kind\" of: " + kind);
126 }
127 }
128
129 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) {
130 return make_multifluid({{"components", components}, {"root",root}, {"BIP", BIP}, {"flags", flags}, {"departure", departurepath}});
131 }
132
133 std::unique_ptr<AbstractModel> make_model(const nlohmann::json& j, const bool validate) {
134 return build_model_ptr(j, validate);
135 }
136
138 if (pointer_factory.find(key) == pointer_factory.end()){
139 pointer_factory[key] = func;
140 }
141 else{
142 throw teqp::InvalidArgument("key is already present, overwriting is not currently allowed");
143 }
144 }
145 }
146}
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 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::function< std::unique_ptr< teqp::cppinterface::AbstractModel >(const nlohmann::json &j)> ModelPointerFactoryFunction
Definition teqpcpp.hpp:209
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.
Definition cubics.hpp:345
auto make_AdvancedPRaEres(const nlohmann::json &j)
Definition cubics.hpp:638
auto make_canonicalPR(const nlohmann::json &spec)
A JSON-based factory function for the canonical SRK model.
Definition cubics.hpp:335
auto make_canonicalSRK(const nlohmann::json &spec)
A JSON-based factory function for the canonical SRK model.
Definition cubics.hpp:290
const nlohmann::json model_schema_library