teqp 0.22.0
Loading...
Searching...
No Matches
multifluid_reducing.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "teqp/types.hpp"
4
5namespace teqp {
6
7 namespace reducing {
8
9 inline auto get_BIPdep(const nlohmann::json& collection, const std::vector<std::string>& identifiers, const nlohmann::json& flags) {
10 if (!collection.is_array()){
11 throw teqp::InvalidArgument("collection provided to get_BIPdep must be an array");
12 }
13 if (collection.size() > 0 && !collection[0].is_object()){
14 throw teqp::InvalidArgument("entries in collection provided to get_BIPdep must be objects");
15 }
16
17 // If force-estimate is provided in flags, the estimation will over-ride the provided model(s)
18 if (flags.contains("force-estimate")) {
19 std::string scheme = flags.at("estimate");
20 if (scheme == "Lorentz-Berthelot") {
21 return std::make_tuple(nlohmann::json({
22 {"betaT", 1.0}, {"gammaT", 1.0}, {"betaV", 1.0}, {"gammaV", 1.0}, {"F", 0.0}
23 }), false);
24 }
25 else {
26 throw std::invalid_argument("estimation scheme is not understood:" + scheme);
27 }
28 }
29
30 // convert string to upper case
31 auto toupper = [](const std::string s) { auto data = s; std::for_each(data.begin(), data.end(), [](char& c) { c = ::toupper(c); }); return data; };
32
33 std::string comp0 = toupper(identifiers[0]);
34 std::string comp1 = toupper(identifiers[1]);
35 // O-th pass, check the hashes
36 for (auto& el : collection) {
37 if (!el.contains("hash1")){ continue; }
38 std::string name1 = toupper(el.at("hash1"));
39 std::string name2 = toupper(el.at("hash2"));
40 if (comp0 == name1 && comp1 == name2) {
41 return std::make_tuple(el, false);
42 }
43 if (comp0 == name2 && comp1 == name1) {
44 return std::make_tuple(el, true);
45 }
46 }
47 // First pass, check names
48 for (auto& el : collection) {
49 std::string name1 = toupper(el.at("Name1"));
50 std::string name2 = toupper(el.at("Name2"));
51 if (comp0 == name1 && comp1 == name2) {
52 return std::make_tuple(el, false);
53 }
54 if (comp0 == name2 && comp1 == name1) {
55 return std::make_tuple(el, true);
56 }
57 }
58 // Second pass, check CAS#
59 for (auto& el : collection) {
60 std::string CAS1 = el.at("CAS1");
61 std::string CAS2 = el.at("CAS2");
62 if (identifiers[0] == CAS1 && identifiers[1] == CAS2) {
63 return std::make_tuple(el, false);
64 }
65 if (identifiers[0] == CAS2 && identifiers[1] == CAS1) {
66 return std::make_tuple(el, true);
67 }
68 }
69
70 // If estimate is provided in flags, it will be the fallback solution for filling in interaction parameters
71 if (flags.contains("estimate")) {
72 std::string scheme = flags.at("estimate");
73 if (scheme == "Lorentz-Berthelot") {
74 return std::make_tuple(nlohmann::json({
75 {"betaT", 1.0}, {"gammaT", 1.0}, {"betaV", 1.0}, {"gammaV", 1.0}, {"F", 0.0}
76 }), false);
77 }
78 else {
79 throw std::invalid_argument("estimation scheme is not understood:" + scheme);
80 }
81 }
82 else {
83 throw std::invalid_argument("Can't match the binary pair for: " + identifiers[0] + "/" + identifiers[1]);
84 }
85 }
86
88 inline auto get_binary_interaction_double(const nlohmann::json& collection, const std::vector<std::string>& identifiers, const nlohmann::json& flags, const std::vector<double>& Tc, const std::vector<double>& vc) {
89 auto [el, swap_needed] = get_BIPdep(collection, identifiers, flags);
90
91 double betaT, gammaT, betaV, gammaV;
92 if (el.contains("betaT") && el.contains("gammaT") && el.contains("betaV") && el.contains("gammaV")) {
93 betaT = el["betaT"]; gammaT = el["gammaT"]; betaV = el["betaV"]; gammaV = el["gammaV"];
94 // Backwards order of components, flip beta values
95 if (swap_needed) {
96 betaT = 1.0 / betaT;
97 betaV = 1.0 / betaV;
98 }
99 }
100 else if (el.contains("xi") && el.contains("zeta")) {
101 double xi = el["xi"], zeta = el["zeta"];
102 gammaT = 0.5 * (Tc[0] + Tc[1] + xi) / (2 * sqrt(Tc[0] * Tc[1]));
103 gammaV = 4.0 * (vc[0] + vc[1] + zeta) / (0.25 * pow(1 / pow(1 / vc[0], 1.0 / 3.0) + 1 / pow(1 / vc[1], 1.0 / 3.0), 3));
104 betaT = 1.0;
105 betaV = 1.0;
106 }
107 else {
108 throw std::invalid_argument("Could not understand what to do with this binary model specification: " + el.dump());
109 }
110 return std::make_tuple(betaT, gammaT, betaV, gammaV);
111 }
112
114 template <typename Tcvec, typename vcvec>
115 inline auto get_BIP_matrices(const nlohmann::json& collection, const std::vector<std::string>& components, const nlohmann::json& flags, const Tcvec& Tc, const vcvec& vc) {
116 Eigen::MatrixXd betaT, gammaT, betaV, gammaV, YT, Yv;
117 auto N = components.size();
118 betaT.resize(N, N); betaT.setZero();
119 gammaT.resize(N, N); gammaT.setZero();
120 betaV.resize(N, N); betaV.setZero();
121 gammaV.resize(N, N); gammaV.setZero();
122 for (auto i = 0U; i < N; ++i) {
123 for (auto j = i + 1; j < N; ++j) {
124 auto [betaT_, gammaT_, betaV_, gammaV_] = get_binary_interaction_double(collection, { components[i], components[j] }, flags, { Tc[i], Tc[j] }, { vc[i], vc[j] });
125 betaT(i, j) = betaT_; betaT(j, i) = 1.0 / betaT(i, j);
126 gammaT(i, j) = gammaT_; gammaT(j, i) = gammaT(i, j);
127 betaV(i, j) = betaV_; betaV(j, i) = 1.0 / betaV(i, j);
128 gammaV(i, j) = gammaV_; gammaV(j, i) = gammaV(i, j);
129 }
130 }
131 return std::make_tuple(betaT, gammaT, betaV, gammaV);
132 }
133
138 inline auto get_Tcvc(const std::vector<nlohmann::json>& pureJSON) {
139 Eigen::ArrayXd Tc(pureJSON.size()), vc(pureJSON.size());
140 auto i = 0;
141 for (auto& j : pureJSON) {
142 auto red = j["EOS"][0]["STATES"]["reducing"];
143 double Tc_ = red.at("T");
144 double rhoc_ = red.at("rhomolar");
145 Tc[i] = Tc_;
146 vc[i] = 1.0 / rhoc_;
147 i++;
148 }
149 return std::make_tuple(Tc, vc);
150 }
151
153 inline auto get_F_matrix(const nlohmann::json& collection, const std::vector<std::string>& identifiers, const nlohmann::json& flags) {
154 auto N = identifiers.size();
155 Eigen::MatrixXd F(N, N);
156 for (auto i = 0U; i < N; ++i) {
157 F(i, i) = 0.0;
158 for (auto j = i + 1; j < N; ++j) {
159 auto [el, swap_needed] = get_BIPdep(collection, { identifiers[i], identifiers[j] }, flags);
160 if (el.empty()) {
161 F(i, j) = 0.0;
162 F(j, i) = 0.0;
163 }
164 else {
165 F(i, j) = el.at("F");
166 F(j, i) = el.at("F");
167 }
168 }
169 }
170 return F;
171 }
172 }
173
175 private:
176 Eigen::MatrixXd YT, Yv;
177
178 public:
179 const Eigen::MatrixXd betaT, gammaT, betaV, gammaV;
180 const Eigen::ArrayXd Tc, vc;
181
182 template<typename ArrayLike>
184 const Eigen::MatrixXd& betaT, const Eigen::MatrixXd& gammaT,
185 const Eigen::MatrixXd& betaV, const Eigen::MatrixXd& gammaV,
186 const ArrayLike& Tc, const ArrayLike& vc)
188
189 auto N = Tc.size();
190
191 YT.resize(N, N); YT.setZero();
192 Yv.resize(N, N); Yv.setZero();
193 for (auto i = 0; i < N; ++i) {
194 for (auto j = i + 1; j < N; ++j) {
195 YT(i, j) = 2.0 * betaT(i, j) * gammaT(i, j) * sqrt(Tc[i] * Tc[j]);
196 YT(j, i) = 2.0 * betaT(j, i) * gammaT(j, i) * sqrt(Tc[i] * Tc[j]);
197 Yv(i, j) = 2.0 * 1.0 / 8.0 * betaV(i, j) * gammaV(i, j) * pow3(cbrt(vc[i]) + cbrt(vc[j]));
198 Yv(j, i) = 2.0 * 1.0 / 8.0 * betaV(j, i) * gammaV(j, i) * pow3(cbrt(vc[i]) + cbrt(vc[j]));
199 }
200 }
201 }
202
203 template <typename MoleFractions>
204 auto Y(const MoleFractions& z, const Eigen::ArrayXd& Yc, const Eigen::MatrixXd& beta, const Eigen::MatrixXd& Yij) const {
205
206 auto N = z.size();
207 typename MoleFractions::value_type sum1 = 0.0;
208 for (auto i = 0U; i < N; ++i) {
209 sum1 = sum1 + pow2(z[i]) * Yc[i];
210 }
211
212 typename MoleFractions::value_type sum2 = 0.0;
213 for (auto i = 0U; i < N - 1; ++i) {
214 for (auto j = i + 1; j < N; ++j) {
215 auto den = beta(i, j)*beta(i, j) * z[i] + z[j];
216 if (getbaseval(den) != 0){
217 sum2 = sum2 + z[i] * z[j] * (z[i] + z[j]) / den * Yij(i, j);
218 }
219 else{
220 // constexpr check to abort if trying to do second derivatives
221 // and at least two compositions are zero. This should incur
222 // zero runtime overhead. First derivatives are ok.
224 using namespace autodiff::detail;
225 constexpr auto isDual_ = isDual<typename MoleFractions::Scalar>;
226 constexpr auto order = NumberTraits<typename MoleFractions::Scalar>::Order;
227 if constexpr (isDual_ && order > 1){
228 throw teqp::InvalidArgument("The multifluid reducing term of GERG does not permit more than one zero composition when taking second composition derivatives with autodiff");
229 }
230 }
231 double beta2 = beta(i,j)*beta(i,j);
232 sum2 = sum2 + Yij(i, j)*(
233 z[i]*z[j] + z[i]*z[i]*(1.0-beta2)
234 );
235 }
236 }
237 }
238
239 return forceeval(sum1 + sum2);
240 }
241
242 template<typename MoleFractions> auto get_Tr(const MoleFractions& molefracs) const { return Y(molefracs, Tc, betaT, YT); }
243 template<typename MoleFractions> auto get_rhor(const MoleFractions& molefracs) const { return 1.0 / Y(molefracs, vc, betaV, Yv); }
244
245 const auto& get_mat(const std::string& key) const {
246 if (key == "betaT"){ return betaT; }
247 if (key == "gammaT"){ return gammaT; }
248 if (key == "betaV"){ return betaV; }
249 if (key == "gammaV"){ return gammaV; }
250 throw std::invalid_argument("variable is not understood: " + key);
251 }
252 auto get_BIP(const std::size_t& i, const std::size_t& j, const std::string& key) const {
253 const auto& mat = get_mat(key);
254 if (i < static_cast<std::size_t>(mat.rows()) && j < static_cast<std::size_t>(mat.cols())){
255 return mat(i,j);
256 }
257 else{
258 throw std::invalid_argument("Indices are out of bounds");
259 }
260 }
261
262 };
263
265 private:
266 Eigen::MatrixXd YT, Yv;
267
268 public:
269 const Eigen::MatrixXd phiT, lambdaT, phiV, lambdaV;
270 const Eigen::ArrayXd Tc, vc;
271
272 template<typename ArrayLike>
274 const Eigen::MatrixXd& phiT, const Eigen::MatrixXd& lambdaT,
275 const Eigen::MatrixXd& phiV, const Eigen::MatrixXd& lambdaV,
276 const ArrayLike& Tc, const ArrayLike& vc)
278
279 auto N = Tc.size();
280
281 YT.resize(N, N); YT.setZero();
282 Yv.resize(N, N); Yv.setZero();
283 for (auto i = 0; i < N; ++i) {
284 for (auto j = 0; j < N; ++j) {
285 YT(i, j) = sqrt(Tc[i] * Tc[j]);
286 YT(j, i) = sqrt(Tc[i] * Tc[j]);
287 Yv(i, j) = 1.0 / 8.0 * pow3(cbrt(vc[i]) + cbrt(vc[j]));
288 Yv(j, i) = 1.0 / 8.0 * pow3(cbrt(vc[i]) + cbrt(vc[j]));
289 }
290 }
291 }
293 template <typename MoleFractions>
294 auto Y(const MoleFractions& z, const Eigen::MatrixXd& phi, const Eigen::MatrixXd& lambda, const Eigen::MatrixXd& Yij) const {
295 auto N = z.size();
296 typename MoleFractions::value_type sum = 0.0;
297 for (auto i = 0U; i < N; ++i) {
298 typename MoleFractions::value_type sumj1 = 0.0, sumj2 = 0.0;
299 for (auto j = 0U; j < N; ++j) {
300 sumj1 += z[j] * phi(i, j) * Yij(i, j);
301 sumj2 += z[j] * cbrt(lambda(i,j)) * cbrt(Yij(i,j));
302 }
303 sum += z[i]*(sumj1 + sumj2*sumj2*sumj2);
304 }
305 return sum;
306 }
307 template<typename MoleFractions> auto get_Tr(const MoleFractions& molefracs) const { return Y(molefracs, phiT, lambdaT, YT); }
308 template<typename MoleFractions> auto get_rhor(const MoleFractions& molefracs) const { return 1.0 / Y(molefracs, phiV, lambdaV, Yv); }
309
310 const auto& get_mat(const std::string& key) const {
311 if (key == "phiT"){ return phiT; }
312 if (key == "lambdaT"){ return lambdaT; }
313 if (key == "phiV"){ return phiV; }
314 if (key == "lambdaV"){ return lambdaV; }
315 throw std::invalid_argument("variable is not understood: " + key);
316 }
317 auto get_BIP(const std::size_t& i, const std::size_t& j, const std::string& key) const {
318 const auto& mat = get_mat(key);
319 if (i < static_cast<std::size_t>(mat.rows()) && j < static_cast<std::size_t>(mat.cols())){
320 return mat(i,j);
321 }
322 else{
323 throw std::invalid_argument("Indices are out of bounds");
324 }
325 }
326 };
327
328
329 template<typename... Args>
331 private:
332 const std::variant<Args...> term;
333 auto get_Tc() const { return std::visit([](const auto& t) { return std::cref(t.Tc); }, term); }
334 auto get_vc() const { return std::visit([](const auto& t) { return std::cref(t.vc); }, term); }
335 public:
336 const Eigen::ArrayXd Tc, vc;
337
338 template<typename Instance>
339 ReducingTermContainer(const Instance& instance) : term(instance), Tc(get_Tc()), vc(get_vc()) {}
340
341 template <typename MoleFractions>
342 auto get_Tr(const MoleFractions& molefracs) const {
343 return std::visit([&](auto& t) { return t.get_Tr(molefracs); }, term);
344 }
345
346 template <typename MoleFractions>
347 auto get_rhor(const MoleFractions& molefracs) const {
348 return std::visit([&](auto& t) { return t.get_rhor(molefracs); }, term);
349 }
350
351 auto get_BIP(const std::size_t& i, const std::size_t& j, const std::string& key) const {
352 return std::visit([&](auto& t) { return t.get_BIP(i, j, key); }, term);
353 }
354 };
355
357
358}; // namespace teqp
MultiFluidInvariantReducingFunction(const Eigen::MatrixXd &phiT, const Eigen::MatrixXd &lambdaT, const Eigen::MatrixXd &phiV, const Eigen::MatrixXd &lambdaV, const ArrayLike &Tc, const ArrayLike &vc)
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
const auto & get_mat(const std::string &key) const
auto Y(const MoleFractions &z, const Eigen::MatrixXd &phi, const Eigen::MatrixXd &lambda, const Eigen::MatrixXd &Yij) const
As implemented in Table 7.18 from GERG-2004.
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 Y(const MoleFractions &z, const Eigen::ArrayXd &Yc, const Eigen::MatrixXd &beta, const Eigen::MatrixXd &Yij) const
auto get_rhor(const MoleFractions &molefracs) const
MultiFluidReducingFunction(const Eigen::MatrixXd &betaT, const Eigen::MatrixXd &gammaT, const Eigen::MatrixXd &betaV, const Eigen::MatrixXd &gammaV, const ArrayLike &Tc, const ArrayLike &vc)
const auto & get_mat(const std::string &key) const
auto get_rhor(const MoleFractions &molefracs) const
ReducingTermContainer(const Instance &instance)
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 get_binary_interaction_double(const nlohmann::json &collection, const std::vector< std::string > &identifiers, const nlohmann::json &flags, const std::vector< double > &Tc, const std::vector< double > &vc)
Get the binary interaction parameters for a given binary pair.
auto get_Tcvc(const std::vector< nlohmann::json > &pureJSON)
auto get_F_matrix(const nlohmann::json &collection, const std::vector< std::string > &identifiers, const nlohmann::json &flags)
Get the matrix F of Fij factors multiplying the departure functions.
auto get_BIPdep(const nlohmann::json &collection, const std::vector< std::string > &identifiers, const nlohmann::json &flags)
auto get_BIP_matrices(const nlohmann::json &collection, const std::vector< std::string > &components, const nlohmann::json &flags, const Tcvec &Tc, const vcvec &vc)
Build the matrices of betaT, gammaT, betaV, gammaV for the multi-fluid model.
T pow3(const T &x)
Definition types.hpp:8
auto getbaseval(const T &expr)
Definition types.hpp:90
T pow2(const T &x)
Definition types.hpp:7
auto pow(const double &x, const double &e)
Definition types.hpp:195
auto forceeval(T &&expr)
Definition types.hpp:52