teqp 0.22.0
Loading...
Searching...
No Matches
multifluid_mutant.hpp
Go to the documentation of this file.
1#pragma once
2
3
4namespace teqp {
5
11 template<typename DepartureFunction, typename BaseClass>
13
14 private:
15 std::string meta = "";
16
17 public:
18 const BaseClass& base;
20 const DepartureFunction dep;
21
22 template<class VecType>
23 auto R(const VecType& molefrac) const { return base.R(molefrac); }
24
25 MultiFluidAdapter(const BaseClass& base, ReducingFunctions&& redfunc, DepartureFunction&& depfunc) : base(base), redfunc(redfunc), dep(depfunc) {};
26
28 void set_meta(const std::string& m) { meta = m; }
30 auto get_meta() const { return meta; }
32 const std::variant<double, std::string> get_BIP(const std::size_t &i, const std::size_t &j, const std::string& key) const{
33 if (key == "F" || key == "Fij"){
34 auto F = dep.get_F();
35 if (0 <= i && i < F.rows() && 0 <= j && j < F.cols()){
36 return F(i,j);
37 }
38 }
39 return redfunc.get_BIP(i, j, key);
40 }
41
42 template<typename TauType, typename DeltaType, typename MoleFracType>
43 auto alphar_taudelta(const TauType& tau,
44 const DeltaType& delta,
45 const MoleFracType& molefrac) const
46 {
47 auto val = base.corr.alphar(tau, delta, molefrac) + dep.alphar(tau, delta, molefrac);
48 return forceeval(val);
49 }
50
51 template<typename TType, typename RhoType, typename MoleFracType>
52 auto alphar(const TType& T,
53 const RhoType& rho,
54 const MoleFracType& molefrac) const
55 {
56 auto Tred = forceeval(redfunc.get_Tr(molefrac));
57 auto rhored = forceeval(redfunc.get_rhor(molefrac));
58 auto delta = forceeval(rho / rhored);
59 auto tau = forceeval(Tred / T);
60 auto val = base.corr.alphar(tau, delta, molefrac) + dep.alphar(tau, delta, molefrac);
61 return forceeval(val);
62 }
63 };
64
65 template<class Model>
66 auto build_multifluid_mutant(const Model& model, const nlohmann::json& jj) {
67
68 auto N = model.redfunc.Tc.size();
69
70 // Allocate the matrices of default models and F factors
71 Eigen::MatrixXd F(N, N); F.setZero();
72 std::vector<std::vector<DepartureTerms>> funcs(N);
73 for (auto i = 0; i < N; ++i) { funcs[i].resize(N); }
74
75 // Build the F and departure function matrix
76 for (auto i = 0; i < N; ++i) {
77 for (auto j = i; j < N; ++j) {
78 if (i == j) {
79 funcs[i][i].add_term(NullEOSTerm());
80 }
81 else {
82 // Extract the given entry
83 auto entry = jj[std::to_string(i)][std::to_string(j)];
84
85 // Set the entry in the matrix of F and departure functions
86 auto dep = entry.at("departure");
87 auto BIP = entry.at("BIP");
88 F(i, j) = BIP.at("Fij");
89 F(j, i) = F(i, j);
90 funcs[i][j] = build_departure_function(dep);
91 funcs[j][i] = build_departure_function(dep);
92 }
93 }
94 }
95
96 // Determine what sort of reducing function is to be used
97 auto get_reducing = [&](const auto& deptype) {
98 auto red = model.redfunc;
99 auto Tc = red.Tc, vc = red.vc;
100 if (deptype == "invariant") {
101 using mat = std::decay_t<decltype(MultiFluidInvariantReducingFunction::phiT)>;
102 mat phiT = mat::Zero(N, N), lambdaT = mat::Zero(N, N), phiV = mat::Zero(N, N), lambdaV = mat::Zero(N, N);
103
104 for (auto i = 0; i < N; ++i) {
105 for (auto j = i+1; j < N; ++j) {
106 // Extract the given entry
107 auto entry = jj.at(std::to_string(i)).at(std::to_string(j));
108
109 auto BIP = entry.at("BIP");
110 // Set the reducing function parameters in the copy
111 phiT(i, j) = BIP.at("phiT");
112 phiT(j, i) = phiT(i, j);
113 lambdaT(i, j) = BIP.at("lambdaT");
114 lambdaT(j, i) = -lambdaT(i, j);
115
116 phiV(i, j) = BIP.at("phiV");
117 phiV(j, i) = phiV(i, j);
118 lambdaV(i, j) = BIP.at("lambdaV");
119 lambdaV(j, i) = -lambdaV(i, j);
120 }
121 }
122 return ReducingFunctions(MultiFluidInvariantReducingFunction(phiT, lambdaT, phiV, lambdaV, Tc, vc));
123 }
124 else {
125 using mat = std::decay_t<decltype(MultiFluidReducingFunction::betaT)>;
126 mat betaT = mat::Zero(N, N), gammaT = mat::Zero(N, N), betaV = mat::Zero(N, N), gammaV = mat::Zero(N, N);
127
128 for (auto i = 0; i < N; ++i) {
129 for (auto j = i+1; j < N; ++j) {
130 // Extract the given entry
131 auto entry = jj.at(std::to_string(i)).at(std::to_string(j));
132 auto BIP = entry.at("BIP");
133 // Set the reducing function parameters in the copy
134 betaT(i, j) = BIP.at("betaT");
135 betaT(j, i) = 1 / betaT(i, j);
136 betaV(i, j) = BIP.at("betaV");
137 betaV(j, i) = 1 / betaV(i, j);
138 gammaT(i, j) = BIP.at("gammaT"); gammaT(j, i) = gammaT(i, j);
139 gammaV(i, j) = BIP.at("gammaV"); gammaV(j, i) = gammaV(i, j);
140 }
141 }
142 return ReducingFunctions(MultiFluidReducingFunction(betaT, gammaT, betaV, gammaV, Tc, vc));
143 }
144 };
145 std::string deptype = (jj.at("0").at("1").at("BIP").contains("type")) ? jj.at("0").at("1").at("BIP")["type"] : "";
146 ReducingFunctions newred = get_reducing(deptype);
147
148 auto newdep = DepartureContribution(std::move(F), std::move(funcs));
149 auto mfa = MultiFluidAdapter(model, std::move(newred), std::move(newdep));
151 mfa.set_meta(jj.dump());
152 return mfa;
153 }
154
155}
MultiFluidAdapter(const BaseClass &base, ReducingFunctions &&redfunc, DepartureFunction &&depfunc)
auto R(const VecType &molefrac) const
auto alphar(const TType &T, const RhoType &rho, const MoleFracType &molefrac) const
auto get_meta() const
Get the metadata stored in string form.
const std::variant< double, std::string > get_BIP(const std::size_t &i, const std::size_t &j, const std::string &key) const
Return a binary interaction parameter.
const ReducingFunctions redfunc
const DepartureFunction dep
auto alphar_taudelta(const TauType &tau, const DeltaType &delta, const MoleFracType &molefrac) const
void set_meta(const std::string &m)
Store some sort of metadata in string form (perhaps a JSON representation of the model?...
auto get_rhor(const MoleFractions &molefracs) const
auto get_Tr(const MoleFractions &molefracs) const
auto get_BIP(const std::size_t &i, const std::size_t &j, const std::string &key) const
auto build_multifluid_mutant(const Model &model, const nlohmann::json &jj)
auto build_departure_function(const nlohmann::json &j)
ReducingTermContainer< MultiFluidReducingFunction, MultiFluidInvariantReducingFunction > ReducingFunctions
auto forceeval(T &&expr)
Definition types.hpp:52