3#include "nlohmann/json.hpp"
17#include "RPinterop/interop.hpp"
19#if defined(TEQP_MULTICOMPLEX_ENABLED)
20#include "MultiComplex/MultiComplex.hpp"
27#include <boost/algorithm/string/join.hpp>
29#if defined(TEQP_MULTICOMPLEX_ENABLED)
32 template<
typename TN>
struct NumTraits<mcx::MultiComplex<TN>> : NumTraits<double>
38 RequireInitialization = 1,
49template<
typename EOSCollection>
53 const EOSCollection EOSs;
57 auto size()
const {
return EOSs.size(); }
59 template<
typename TauType,
typename DeltaType,
typename MoleFractions>
60 auto alphar(
const TauType& tau,
const DeltaType& delta,
const MoleFractions& molefracs)
const {
61 using resulttype = std::common_type_t<
decltype(tau),
decltype(molefracs[0]),
decltype(delta)>;
63 auto N = molefracs.size();
64 for (
auto i = 0U; i < N; ++i) {
65 alphar += molefracs[i] * EOSs[i].alphar(tau, delta);
70 template<
typename TauType,
typename DeltaType>
71 auto alphari(
const TauType& tau,
const DeltaType& delta, std::size_t i)
const {
72 return EOSs[i].alphar(tau, delta);
80template<
typename FCollection,
typename DepartureFunctionCollection>
85 const DepartureFunctionCollection funcs;
89 const auto&
get_F()
const {
return F; }
91 template<
typename TauType,
typename DeltaType,
typename MoleFractions>
92 auto alphar(
const TauType& tau,
const DeltaType& delta,
const MoleFractions& molefracs)
const {
93 using resulttype = std::decay_t<std::common_type_t<
decltype(tau),
decltype(molefracs[0]),
decltype(delta)>>;
95 std::size_t N = molefracs.size();
96 for (
auto i = 0U; i < N; ++i) {
97 for (
auto j = i+1; j < N; ++j) {
98 alphar += molefracs[i] * molefracs[j] * F(i, j) * funcs[i][j].alphar(tau, delta);
105 template<
typename TauType,
typename DeltaType>
106 auto get_alpharij(
const std::size_t i,
const std::size_t j,
const TauType& tau,
const DeltaType& delta)
const {
107 std::size_t N = funcs.size();
111 if (i >= N || j >= N){
118template<
typename CorrespondingTerm,
typename DepartureTerm>
122 std::string meta =
"";
130 template<
class VecType>
131 auto R(
const VecType& molefracs)
const {
132 return std::visit([&molefracs](
const auto& el){
return el.get_R(molefracs); },
Rcalc);
140 const std::variant<double, std::string>
get_BIP(
const std::size_t &i,
const std::size_t &j,
const std::string& key)
const{
141 if (key ==
"F" || key ==
"Fij"){
142 auto F =
dep.get_F();
143 if (0 <= i && i < F.rows() && 0 <= j && j < F.cols()){
152 template<
typename TType,
typename RhoType>
154 const RhoType& rhovec,
155 const std::optional<typename RhoType::value_type> rhotot = std::nullopt)
const
157 typename RhoType::value_type rhotot_ = (rhotot.has_value()) ? rhotot.value() : std::accumulate(std::begin(rhovec), std::end(rhovec), (
decltype(rhovec[0]))0.0);
158 auto molefrac = rhovec / rhotot_;
159 return alphar(T, rhotot_, molefrac);
162 template<
typename TType,
typename RhoType,
typename MoleFracType>
165 const MoleFracType& molefrac)
const
167 if (
static_cast<std::size_t
>(molefrac.size()) !=
corr.size()){
168 throw teqp::InvalidArgument(
"Wrong size of mole fractions; "+std::to_string(
corr.size()) +
" are loaded but "+std::to_string(molefrac.size()) +
" were provided");
172 if (molefrac.size() == 1){
174 return corr.alphari(tau, delta, 0);
176 return forceeval(
corr.alphar(tau, delta, molefrac) +
dep.alphar(tau, delta, molefrac));
179 template<
typename TType,
typename RhoType,
typename MoleFracType>
181 const RhoType &delta,
182 const MoleFracType& molefrac)
const
184 if (
static_cast<std::size_t
>(molefrac.size()) !=
corr.size()){
185 throw teqp::InvalidArgument(
"Wrong size of mole fractions; "+std::to_string(
corr.size()) +
" are loaded but "+std::to_string(molefrac.size()) +
" were provided");
187 if (molefrac.size() == 1){
188 return corr.alphari(tau, delta, 0U);
190 return forceeval(
corr.alphar(tau, delta, molefrac) +
dep.alphar(tau, delta, molefrac));
193 template<
typename TType,
typename RhoType>
194 inline auto alphar_taudeltai(
const TType &tau,
const RhoType &delta,
const std::size_t i)
const
196 return corr.alphari(tau, delta, i);
207 std::string filepath = std::filesystem::is_regular_file(path) ? path : path +
"/dev/mixtures/mixture_departure_functions.json";
209 std::string js = j.dump(2);
212 if (el.at(
"Name") == name) {
218 for (
auto &alias : el.at(
"aliases")) {
224 throw std::invalid_argument(
"Could not match the name: " + name +
"when looking up departure function");
228 auto build_power = [&](
auto term,
auto& dep) {
229 std::size_t N = term[
"n"].size();
238 auto eigorzero = [&term, &N](
const std::string& name) -> Eigen::ArrayXd {
239 if (!term[name].empty()) {
240 return toeig(term[name]);
243 return Eigen::ArrayXd::Zero(N);
248 eos.
n = eigorzero(
"n");
249 eos.
t = eigorzero(
"t");
250 eos.
d = eigorzero(
"d");
252 Eigen::ArrayXd c(N), l(N); c.setZero();
254 int Nlzero = 0, Nlnonzero = 0;
255 bool contiguous_lzero =
false;
257 if (term[
"l"].empty()) {
261 throw std::invalid_argument(
"Lengths are not all identical in polynomial-like term");
266 throw std::invalid_argument(
"Lengths are not all identical in exponential term");
268 l =
toeig(term[
"l"]);
270 for (
auto i = 0; i < c.size(); ++i) {
277 contiguous_lzero = (l[0] == 0);
278 for (
auto i = 0; i < c.size(); ++i) {
284 Nlnonzero =
static_cast<int>(l.size()) - Nlzero;
286 if (contiguous_lzero && (l.tail(Nlnonzero) == 0).any()) {
287 throw std::invalid_argument(
"If l_i has zero and non-zero values, the zero values need to come first");
293 eos.
l_i = eos.
l.cast<
int>();
295 if (Nlzero + Nlnonzero != l.size()) {
296 throw std::invalid_argument(
"Somehow the l lengths don't add up");
300 if (((eos.
l_i.cast<
double>() - eos.
l).cwiseAbs() > 0.0).any()) {
301 throw std::invalid_argument(
"Non-integer entry in l found");
316 else if (l.sum() > 0 && contiguous_lzero){
318 poly.
n = eos.
n.head(Nlzero);
319 poly.
t = eos.
t.head(Nlzero);
320 poly.
d = eos.
d.head(Nlzero);
324 e.
n = eos.
n.tail(Nlnonzero);
325 e.
t = eos.
t.tail(Nlnonzero);
326 e.
d = eos.
d.tail(Nlnonzero);
327 e.
c = eos.
c.tail(Nlnonzero);
328 e.
l = eos.
l.tail(Nlnonzero);
329 e.
l_i = eos.
l_i.tail(Nlnonzero);
338 auto build_doubleexponential = [&](
auto& term,
auto& dep) {
340 throw std::invalid_argument(
"Lengths are not all identical in double exponential term");
343 eos.
n =
toeig(term.at(
"n"));
344 eos.
t =
toeig(term.at(
"t"));
345 eos.
d =
toeig(term.at(
"d"));
350 eos.
ld_i = eos.
ld.cast<
int>();
353 auto build_Chebyshev2D = [&](
auto& term,
auto& dep) {
355 int Ntau = term.at(
"Ntau");
356 int Ndelta = term.at(
"Ndelta");
357 Eigen::ArrayXd c =
toeig(term.at(
"a"));
358 if ((Ntau + 1)*(Ndelta + 1) != c.size()){
359 throw std::invalid_argument(
"Provided length [" + std::to_string(c.size()) +
"] is not equal to (Ntau+1)*(Ndelta+1)");
361 eos.
a = c.reshaped(Ntau+1, Ndelta+1).eval();
362 eos.
taumin = term.at(
"taumin");
363 eos.
taumax = term.at(
"taumax");
382 auto build_GERG2004 = [&](
const auto& term,
auto& dep) {
383 if (!
all_same_length(term, {
"n",
"t",
"d",
"eta",
"beta",
"gamma",
"epsilon" })) {
384 throw std::invalid_argument(
"Lengths are not all identical in GERG term");
386 int Npower = term[
"Npower"];
387 auto NGERG =
static_cast<int>(term[
"n"].size()) - Npower;
390 eos.
n =
toeig(term[
"n"]).head(Npower);
391 eos.
t =
toeig(term[
"t"]).head(Npower);
392 eos.
d =
toeig(term[
"d"]).head(Npower);
393 if (term.contains(
"l")) {
394 eos.
l =
toeig(term[
"l"]).head(Npower);
399 eos.
c = (eos.
l > 0).cast<int>().cast<
double>();
400 eos.
l_i = eos.
l.cast<
int>();
404 e.
n =
toeig(term[
"n"]).tail(NGERG);
405 e.
t =
toeig(term[
"t"]).tail(NGERG);
406 e.
d =
toeig(term[
"d"]).tail(NGERG);
407 e.
eta =
toeig(term[
"eta"]).tail(NGERG);
408 e.
beta =
toeig(term[
"beta"]).tail(NGERG);
413 auto build_GaussianExponential = [&](
const auto& term,
auto& dep) {
414 if (!
all_same_length(term, {
"n",
"t",
"d",
"eta",
"beta",
"gamma",
"epsilon" })) {
415 throw std::invalid_argument(
"Lengths are not all identical in Gaussian+Exponential term");
417 int Npower = term[
"Npower"];
418 auto NGauss =
static_cast<int>(term[
"n"].size()) - Npower;
421 eos.
n =
toeig(term[
"n"]).head(Npower);
422 eos.
t =
toeig(term[
"t"]).head(Npower);
423 eos.
d =
toeig(term[
"d"]).head(Npower);
424 if (term.contains(
"l")) {
425 eos.
l =
toeig(term[
"l"]).head(Npower);
430 eos.
c = (eos.
l > 0).cast<int>().cast<
double>();
431 eos.
l_i = eos.
l.cast<
int>();
435 e.
n =
toeig(term[
"n"]).tail(NGauss);
436 e.
t =
toeig(term[
"t"]).tail(NGauss);
437 e.
d =
toeig(term[
"d"]).tail(NGauss);
438 e.
eta =
toeig(term[
"eta"]).tail(NGauss);
439 e.
beta =
toeig(term[
"beta"]).tail(NGauss);
445 std::string type = j.at(
"type");
447 if (type ==
"Exponential") {
450 else if (type ==
"DoubleExponential") {
451 build_doubleexponential(j, dep);
453 else if (type ==
"GERG-2004" || type ==
"GERG-2008") {
454 build_GERG2004(j, dep);
456 else if (type ==
"Gaussian+Exponential") {
457 build_GaussianExponential(j, dep);
459 else if (type ==
"Chebyshev2D") {
460 build_Chebyshev2D(j, dep);
462 else if (type ==
"none") {
467 std::vector<std::string> options = {
"Exponential",
"GERG-2004",
"GERG-2008",
"Gaussian+Exponential",
"none",
"DoubleExponential",
"Chebyshev2D"};
468 throw std::invalid_argument(
"Bad departure term type: " + type +
". Options are {" + boost::algorithm::join(options,
",") +
"}");
473inline auto get_departure_function_matrix(
const nlohmann::json& depcollection,
const nlohmann::json& BIPcollection,
const std::vector<std::string>& components,
const nlohmann::json& flags) {
476 std::vector<std::vector<DepartureTerms>> funcs(components.size());
for (
auto i = 0U; i < funcs.size(); ++i) { funcs[i].resize(funcs.size()); }
481 for (
auto& el : depcollection) {
482 if (el[
"Name"] == Name) {
return el; }
484 throw std::invalid_argument(
"Bad departure function name: "+Name);
487 auto funcsmeta = nlohmann::json::object();
489 for (
auto i = 0U; i < funcs.size(); ++i) {
490 std::string istr = std::to_string(i);
491 if (funcsmeta.contains(istr)) { funcsmeta[istr] = {}; }
492 for (
auto j = i + 1; j < funcs.size(); ++j) {
493 std::string jstr = std::to_string(j);
494 auto [BIP, swap_needed] =
reducing::get_BIPdep(BIPcollection, { components[i], components[j] }, flags);
495 std::string funcname = BIP.contains(
"function") ? BIP[
"function"] :
"";
497 if (!funcname.empty()) {
498 if (depcollection.empty()){
499 throw teqp::InvalidArgument(
"No departure functions were loaded, unable to select requested function: " + funcname);
509 funcsmeta[istr][jstr] = { {
"departure", jj}, {
"BIP", BIP} };
510 funcsmeta[istr][jstr][
"BIP"][
"swap_needed"] = swap_needed;
513 return std::make_tuple(funcs, funcsmeta);
518 auto alphar = j[
"EOS"][0][
"alphar"];
525 const std::vector<std::string> allowed_types = {
"ResidualHelmholtzPower",
"ResidualHelmholtzGaussian",
"ResidualHelmholtzNonAnalytic",
"ResidualHelmholtzGaoB",
"ResidualHelmholtzLemmon2005",
"ResidualHelmholtzExponential",
"ResidualHelmholtzDoubleExponential",
"ResidualHelmholtzGenericCubic",
"ResidualHelmholtzPCSAFTGrossSadowski2001" };
527 auto isallowed = [&](
const auto& conventional_types,
const std::string& name) {
528 for (
auto& a : conventional_types) {
if (name == a) {
return true; }; }
return false;
531 for (
auto& term : alphar) {
532 std::string type = term[
"type"];
533 if (!isallowed(allowed_types, type)) {
534 std::string a = allowed_types[0];
for (
auto i = 1U; i < allowed_types.size(); ++i) { a +=
"," + allowed_types[i]; }
535 throw std::invalid_argument(
"Bad type:" + type +
"; allowed types are: {" + a +
"}");
541 auto build_power = [&](
auto term,
auto & container) {
542 std::size_t N = term[
"n"].
size();
546 auto eigorzero = [&term, &N](
const std::string& name) -> Eigen::ArrayXd {
547 if (!term[name].empty()) {
548 return toeig(term[name]);
551 return Eigen::ArrayXd::Zero(N);
556 eos.
n = eigorzero(
"n");
557 eos.
t = eigorzero(
"t");
558 eos.
d = eigorzero(
"d");
560 Eigen::ArrayXd c(N), l(N); c.setZero();
561 int Nlzero = 0, Nlnonzero = 0;
562 bool contiguous_lzero;
563 if (term[
"l"].empty()) {
568 l =
toeig(term[
"l"]);
570 for (
auto i = 0; i < c.size(); ++i) {
577 contiguous_lzero = (l[0] == 0);
578 for (
auto i = 0; i < c.size(); ++i) {
584 Nlnonzero =
static_cast<int>(l.size()) - Nlzero;
589 eos.
l_i = eos.
l.cast<
int>();
591 if (Nlzero + Nlnonzero != l.size()) {
592 throw std::invalid_argument(
"Somehow the l lengths don't add up");
595 if (((eos.
l_i.cast<
double>() - eos.
l).cwiseAbs() > 0.0).any()) {
596 throw std::invalid_argument(
"Non-integer entry in l found");
611 else if (l.sum() > 0 && contiguous_lzero) {
613 poly.
n = eos.
n.head(Nlzero);
614 poly.
t = eos.
t.head(Nlzero);
615 poly.
d = eos.
d.head(Nlzero);
619 e.
n = eos.
n.tail(Nlnonzero);
620 e.
t = eos.
t.tail(Nlnonzero);
621 e.
d = eos.
d.tail(Nlnonzero);
622 e.
c = eos.
c.tail(Nlnonzero);
623 e.
l = eos.
l.tail(Nlnonzero);
624 e.
l_i = eos.
l_i.tail(Nlnonzero);
633 auto build_Lemmon2005 = [&](
auto term) {
640 eos.
l_i = eos.
l.cast<
int>();
642 throw std::invalid_argument(
"Lengths are not all identical in Lemmon2005 term");
644 if (((eos.
l_i.cast<
double>() - eos.
l).cwiseAbs() > 0.0).any()) {
645 throw std::invalid_argument(
"Non-integer entry in l found");
650 auto build_gaussian = [&](
auto term) {
659 if (!
all_same_length(term, {
"n",
"t",
"d",
"eta",
"beta",
"gamma",
"epsilon" })) {
660 throw std::invalid_argument(
"Lengths are not all identical in Gaussian term");
665 auto build_exponential = [&](
auto term) {
672 eos.
l_i = eos.
l.cast<
int>();
674 throw std::invalid_argument(
"Lengths are not all identical in exponential term");
679 auto build_doubleexponential = [&](
auto& term) {
681 throw std::invalid_argument(
"Lengths are not all identical in double exponential term");
684 eos.
n =
toeig(term.at(
"n"));
685 eos.
t =
toeig(term.at(
"t"));
686 eos.
d =
toeig(term.at(
"d"));
691 eos.
ld_i = eos.
ld.cast<
int>();
695 auto build_GaoB = [&](
auto term) {
705 if (!
all_same_length(term, {
"n",
"t",
"d",
"eta",
"beta",
"gamma",
"epsilon",
"b" })) {
706 throw std::invalid_argument(
"Lengths are not all identical in GaoB term");
712 auto build_na = [&](
auto& term) {
723 throw std::invalid_argument(
"Lengths are not all identical in nonanalytic term");
728 for (
auto& term : alphar) {
729 std::string type = term.at(
"type");
730 if (type ==
"ResidualHelmholtzPower") {
731 build_power(term, container);
733 else if (type ==
"ResidualHelmholtzGaussian") {
734 container.
add_term(build_gaussian(term));
736 else if (type ==
"ResidualHelmholtzNonAnalytic") {
739 else if (type ==
"ResidualHelmholtzLemmon2005") {
740 container.
add_term(build_Lemmon2005(term));
742 else if (type ==
"ResidualHelmholtzGaoB") {
743 container.
add_term(build_GaoB(term));
745 else if (type ==
"ResidualHelmholtzExponential") {
746 container.
add_term(build_exponential(term));
748 else if (type ==
"ResidualHelmholtzDoubleExponential") {
749 container.
add_term(build_doubleexponential(term));
751 else if (type ==
"ResidualHelmholtzGenericCubic") {
754 else if (type ==
"ResidualHelmholtzPCSAFTGrossSadowski2001") {
758 throw std::invalid_argument(
"Bad term type: "+type);
764inline auto get_EOSs(
const std::vector<nlohmann::json>& pureJSON) {
765 std::vector<EOSTerms> EOSs;
766 for (
auto& j : pureJSON) {
768 EOSs.emplace_back(term);
775 std::vector<nlohmann::json> out;
776 for (
auto c : components) {
778 std::vector<std::filesystem::path> candidates = { c, root +
"/dev/fluids/" + c +
".json" };
779 std::filesystem::path selected_path =
"";
780 for (
auto candidate : candidates) {
781 if (std::filesystem::is_regular_file(candidate)) {
782 selected_path = candidate;
786 if (selected_path !=
"") {
790 throw std::invalid_argument(
"Could not load any of the candidates:" + c);
798 std::vector<std::string> CAS, Name, REFPROP, hash;
799 for (
auto j : pureJSON) {
800 auto INFO = j.at(
"INFO");
801 Name.push_back(INFO.at(
"NAME"));
802 CAS.push_back(INFO.at(
"CAS"));
803 REFPROP.push_back(INFO.at(
"REFPROP_NAME"));
804 if (INFO.contains(
"HASH")){
805 hash.push_back(INFO.at(
"HASH"));
808 std::map<std::string, std::vector<std::string>> result{
813 if (hash.size() == result[
"CAS"].size()){
814 result[
"hash"] = hash;
820template<
typename mapvec
string>
821inline auto select_identifier(
const nlohmann::json& BIPcollection,
const mapvecstring& identifierset,
const nlohmann::json& flags){
822 for (
const auto &ident: identifierset){
823 std::string key; std::vector<std::string> identifiers;
824 std::tie(key, identifiers) = ident;
826 for (
auto i = 0U; i < identifiers.size(); ++i){
827 for (
auto j = i+1; j < identifiers.size(); ++j){
828 const std::vector<std::string> pair = {identifiers[i], identifiers[j]};
839 for (
const auto& [k,v] : identifierset){
846 throw std::invalid_argument(
"Unable to match any of the identifier options: " + errmsg);
851 std::map<std::string, std::string> aliasmap;
852 for (
auto path : get_files_in_folder(root +
"/dev/fluids",
".json")) {
854 std::string REFPROP_name = j.at(
"INFO").at(
"REFPROP_NAME");
855 std::string name = j.at(
"INFO").at(
"NAME");
856 for (std::string k : {
"NAME",
"CAS",
"REFPROP_NAME"}) {
857 std::string val = j.at(
"INFO").at(k);
859 if (k ==
"REFPROP_NAME" && val == name) {
863 if (k ==
"REFPROP_NAME" && val ==
"N/A") {
866 if (aliasmap.count(val) > 0) {
867 throw std::invalid_argument(
"Duplicated reverse lookup identifier ["+k+
"] found in file:" + path.string());
870 aliasmap[val] = std::filesystem::absolute(path).string();
873 std::vector<std::string> aliases = j.at(
"INFO").at(
"ALIASES");
875 for (std::string alias : aliases) {
876 if (alias != REFPROP_name && alias != name) {
877 if (aliasmap.count(alias) > 0) {
878 throw std::invalid_argument(
"Duplicated alias [" + alias +
"] found in file:" + path.string());
881 aliasmap[alias] = std::filesystem::absolute(path).string();
891inline auto _build_multifluid_model(
const std::vector<nlohmann::json> &pureJSON,
const nlohmann::json& BIPcollection,
const nlohmann::json& depcollection,
const nlohmann::json& flags = {}) {
893 auto get_Rvals = [](
const std::vector<nlohmann::json> &pureJSON) -> std::vector<double>{
894 std::vector<double> o;
895 for (
auto pure : pureJSON){
896 o.push_back(pure.at(
"EOS")[0].at(
"gas_constant"));
904 auto Rvals = get_Rvals(pureJSON);
909 auto identifiers = identifierset[
select_identifier(BIPcollection, identifierset, flags)];
918 if (flags.contains(
"Rmodel") && flags.at(
"Rmodel") ==
"CODATA"){
919 Rcalc = multifluid::gasconstant::CODATA();
923 nlohmann::json meta = {
928 auto redfunc =
ReducingFunctions(std::move(MultiFluidReducingFunction(betaT, gammaT, betaV, gammaV, Tc, vc)));
930 auto model = MultiFluid(
932 CorrespondingStatesContribution(std::move(EOSs)),
933 DepartureContribution(std::move(F), std::move(funcs)),
936 model.set_meta(meta.dump(1));
941inline auto build_multifluid_JSONstr(
const std::vector<std::string>& componentJSON,
const std::string& BIPJSON,
const std::string& departureJSON,
const nlohmann::json& flags = {}) {
944 const auto BIPcollection = nlohmann::json::parse(BIPJSON);
945 const auto depcollection = nlohmann::json::parse(departureJSON);
948 std::vector<nlohmann::json> pureJSON;
949 for (
auto& c : componentJSON) {
950 pureJSON.emplace_back(nlohmann::json::parse(c));
965 std::vector<nlohmann::json> pureJSON;
966 if (!components.is_array()){
967 throw std::invalid_argument(
"Must be an array");
970 for (
const nlohmann::json& comp : components){
971 auto get_or_aliasmap = [&](){
977 if (!optaliasmap && root){
979 if (optaliasmap.value().count(comp) != 1){
980 std::string scomp = comp.get<std::string>();
981 std::string errname = (scomp.size() > 200) ? scomp.substr(0, 200)+
"..." : scomp;
982 throw teqp::InvalidArgument(
"Alias map constructed, but component name is not found in alias map: " + errname);
986 std::string scomp = comp.get<std::string>();
987 std::string errname = (scomp.size() > 200) ? scomp.substr(0, 200)+
"..." : scomp;
988 teqp::InvalidArgument(
"It was not possible to load the alias map because no path was provided. Failure to load: " + errname);
993 if (comp.is_string()){
994 std::string contents = comp;
996 if (contents.find(
"PATH::") == 0){
999 else if (contents.find(
"FLDPATH::") == 0){
1000 pureJSON.push_back(RPinterop::FLDfile(contents.substr(9)).make_json(
""));
1002 else if (contents.find(
"FLD::") == 0){
1003 pureJSON.push_back(RPinterop::FLDfile(contents.substr(5)).make_json(
""));
1006 pureJSON.push_back(get_or_aliasmap());
1010 pureJSON.push_back(get_or_aliasmap());
1016inline 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 = {}) {
1019 nlohmann::json BIPcollection = nlohmann::json::array();
1020 nlohmann::json depcollection = nlohmann::json::array();
1021 if (components.size() > 1){
1022 nlohmann::json B = BIPcollectionpath, D = departurepath;
1024 depcollection =
multilevel_JSON_load(D, root +
"/dev/mixtures/mixture_departure_functions.json");
1040 nlohmann::json flags = (spec.contains(
"flags")) ? spec.at(
"flags") : nlohmann::json();
1043 if (spec.contains(
"HMX.BNC")){
1044 std::vector<nlohmann::json> componentJSON;
1045 for (
auto comp : spec.at(
"components")){
1046 componentJSON.push_back(RPinterop::FLDfile(comp).make_json(
""));
1048 auto [BIPcollection, depcollection] = RPinterop::HMXBNCfile(spec.at(
"HMX.BNC")).make_jsons();
1053 std::string root = (spec.contains(
"root")) ? spec.at(
"root") :
"";
1055 auto components = spec.at(
"components");
1057 nlohmann::json BIPcollection = nlohmann::json::array();
1058 nlohmann::json depcollection = nlohmann::json::array();
1059 if (components.size() > 1){
1060 BIPcollection =
multilevel_JSON_load(spec.at(
"BIP"), root +
"/dev/mixtures/mixture_binary_pairs.json");
1062 if (spec.contains(
"departure")){
1063 std::string msg =
"departure was provided but is invalid; options are non-empty array, path to file as string, or JSON data encoded as string";
1064 auto load_departure = [&msg](
const nlohmann::json& j){
1065 if (j.is_array() && j.size() > 0){
1068 else if (j.is_string()){
1069 const std::string& s = j;
1070 if (s.find(
"PATH::") == 0){
1079 return nlohmann::json::parse(s);
1093 depcollection = load_departure(spec.at(
"departure"));
1096 depcollection =
multilevel_JSON_load(spec.at(
"departure"), root +
"/dev/mixtures/mixture_departure_functions.json");
CorrespondingStatesContribution(EOSCollection &&EOSs)
auto alphari(const TauType &tau, const DeltaType &delta, std::size_t i) const
auto alphar(const TauType &tau, const DeltaType &delta, const MoleFractions &molefracs) const
auto get_EOS(std::size_t i) const
DepartureContribution(FCollection &&F, DepartureFunctionCollection &&funcs)
auto alphar(const TauType &tau, const DeltaType &delta, const MoleFractions &molefracs) const
auto get_alpharij(const std::size_t i, const std::size_t j, const TauType &tau, const DeltaType &delta) const
Call a single departure term at i,j.
const auto & get_F() const
auto add_term(Instance &&instance)
auto alphar(const TType &T, const RhoType &rho, const MoleFracType &molefrac) const
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 CorrespondingTerm corr
auto get_meta() const
Get the metadata stored in string form.
void set_meta(const std::string &m)
Store some sort of metadata in string form (perhaps a JSON representation of the model?...
MultiFluid(ReducingFunctions &&redfunc, CorrespondingTerm &&corr, DepartureTerm &&dep, GasConstantCalculator &&Rcalc)
multifluid::gasconstant::GasConstantCalculator GasConstantCalculator
const ReducingFunctions redfunc
const GasConstantCalculator Rcalc
auto alphar(TType T, const RhoType &rhovec, const std::optional< typename RhoType::value_type > rhotot=std::nullopt) const
auto alphar_taudelta(const TType &tau, const RhoType &delta, const MoleFracType &molefrac) const
auto alphar_taudeltai(const TType &tau, const RhoType &delta, const std::size_t i) const
auto R(const VecType &molefracs) const
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
std::variant< MoleFractionWeighted, CODATA > GasConstantCalculator
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.
auto _build_multifluid_model(const std::vector< nlohmann::json > &pureJSON, const nlohmann::json &BIPcollection, const nlohmann::json &depcollection, const nlohmann::json &flags={})
Internal method for actually constructing the model with the provided JSON data structures.
auto get_EOS_terms(const nlohmann::json &j)
auto get_EOSs(const std::vector< nlohmann::json > &pureJSON)
auto build_alias_map(const std::string &root)
Build a reverse-lookup map for finding a fluid JSON structure given a backup identifier.
auto toeig(const std::vector< double > &v) -> Eigen::ArrayXd
nlohmann::json load_a_JSON_file(const std::string &path)
Load a JSON file from a specified file.
auto collect_identifiers(const std::vector< nlohmann::json > &pureJSON)
auto build_multifluid_JSONstr(const std::vector< std::string > &componentJSON, const std::string &BIPJSON, const std::string &departureJSON, const nlohmann::json &flags={})
A builder function where the JSON-formatted strings are provided explicitly rather than file paths.
auto get_departure_json(const std::string &name, const std::string &path)
auto get_departure_function_matrix(const nlohmann::json &depcollection, const nlohmann::json &BIPcollection, const std::vector< std::string > &components, const nlohmann::json &flags)
auto build_departure_function(const nlohmann::json &j)
auto all_same_length(const nlohmann::json &j, const std::vector< std::string > &ks)
auto select_identifier(const nlohmann::json &BIPcollection, const mapvecstring &identifierset, const nlohmann::json &flags)
Iterate over the possible options for identifiers to determine which one will satisfy all the binary ...
auto multifluidfactory(const nlohmann::json &spec)
Load a model from a JSON data structure.
auto make_pure_components_JSON(const nlohmann::json &components, const std::optional< std::string > &root=std::nullopt)
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={})
ReducingTermContainer< MultiFluidReducingFunction, MultiFluidInvariantReducingFunction > ReducingFunctions
auto collect_component_json(const std::vector< std::string > &components, const std::string &root)
auto multilevel_JSON_load(const nlohmann::json &j, const std::optional< std::string > &default_path=std::nullopt)