Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
EOSConfig.cpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
18
21
22namespace shammodels {
23
44 template<class Tvec>
45 void to_json(nlohmann::json &j, const EOSConfig<Tvec> &p) {
46 // Serialize EOSConfig to a json object
47
48 using json = nlohmann::json;
49
50 std::string type_id = "";
51
52 if constexpr (std::is_same_v<f32_3, Tvec>) {
53 type_id = "f32_3"; // type of the vector quantities (e.g. position)
54 } else if constexpr (std::is_same_v<f64_3, Tvec>) {
55 type_id = "f64_3"; // type of the vector quantities (e.g. position)
56 } else {
57 static_assert(shambase::always_false_v<Tvec>, "This Tvec type is not handled");
58 }
59
60 using Isothermal = typename EOSConfig<Tvec>::Isothermal;
61 using Adiabatic = typename EOSConfig<Tvec>::Adiabatic;
62 using Polytropic = typename EOSConfig<Tvec>::Polytropic;
63 using LocIsoT = typename EOSConfig<Tvec>::LocallyIsothermal;
64 using LocIsoTLP07 = typename EOSConfig<Tvec>::LocallyIsothermalLP07;
65 using LocIsoTFA2014 = typename EOSConfig<Tvec>::LocallyIsothermalFA2014;
66 using LocIsoTFA2014Extended = typename EOSConfig<Tvec>::LocallyIsothermalFA2014Extended;
67 using Fermi = typename EOSConfig<Tvec>::Fermi;
68 using Barotropic = typename EOSConfig<Tvec>::Barotropic;
69
70 if (const Isothermal *eos_config = std::get_if<Isothermal>(&p.config)) {
71 j = json{{"Tvec", type_id}, {"eos_type", "isothermal"}, {"cs", eos_config->cs}};
72 } else if (const Adiabatic *eos_config = std::get_if<Adiabatic>(&p.config)) {
73 j = json{{"Tvec", type_id}, {"eos_type", "adiabatic"}, {"gamma", eos_config->gamma}};
74 } else if (const Polytropic *eos_config = std::get_if<Polytropic>(&p.config)) {
75 j = json{
76 {"Tvec", type_id},
77 {"eos_type", "polytropic"},
78 {"K", eos_config->K},
79 {"gamma", eos_config->gamma}};
80 } else if (const LocIsoT *eos_config = std::get_if<LocIsoT>(&p.config)) {
81 j = json{{"Tvec", type_id}, {"eos_type", "locally_isothermal"}};
82 } else if (const LocIsoTLP07 *eos_config = std::get_if<LocIsoTLP07>(&p.config)) {
83 j = json{
84 {"Tvec", type_id},
85 {"eos_type", "locally_isothermal_lp07"},
86 {"cs0", eos_config->cs0},
87 {"q", eos_config->q},
88 {"r0", eos_config->r0}};
89 } else if (const LocIsoTFA2014 *eos_config = std::get_if<LocIsoTFA2014>(&p.config)) {
90 j = json{
91 {"Tvec", type_id},
92 {"eos_type", "locally_isothermal_fa2014"},
93 {"h_over_r", eos_config->h_over_r}};
94 } else if (
95 const LocIsoTFA2014Extended *eos_config
96 = std::get_if<LocIsoTFA2014Extended>(&p.config)) {
97 j = json{
98 {"Tvec", type_id},
99 {"eos_type", "locally_isothermal_fa2014_extended"},
100 {"cs0", eos_config->cs0},
101 {"q", eos_config->q},
102 {"r0", eos_config->r0},
103 {"n_sinks", eos_config->n_sinks}};
104 } else if (const Fermi *eos_config = std::get_if<Fermi>(&p.config)) {
105 j = json{{"Tvec", type_id}, {"eos_type", "fermi"}, {"mu_e", eos_config->mu_e}};
106 } else if (const Barotropic *eos_config = std::get_if<Barotropic>(&p.config)) {
107 j = json{
108 {"Tvec", type_id},
109 {"eos_type", "barotropic"},
110 {"cs0", eos_config->cs0},
111 {"rho0", eos_config->rho0},
112 {"gamma0", eos_config->gamma0}};
113 } else {
114 shambase::throw_unimplemented(); // should never be reached
115 }
116 }
117
130 template<class Tvec>
131 void from_json(const nlohmann::json &j, EOSConfig<Tvec> &p) {
132
133 using Tscal = shambase::VecComponent<Tvec>;
134
135 std::string type_id;
136 j.at("Tvec").get_to(type_id);
137
138 if constexpr (std::is_same_v<f32_3, Tvec>) {
139 if (type_id != "f32_3") {
141 "You are trying to create a EOSConfig with the wrong vector type");
142 }
143 } else if constexpr (std::is_same_v<f64_3, Tvec>) {
144 if (type_id != "f64_3") {
146 "You are trying to create a EOSConfig with the wrong vector type");
147 }
148 } else {
149 static_assert(shambase::always_false_v<Tvec>, "This Tvec type is not handled");
150 }
151
152 if (!j.contains("eos_type")) {
153 shambase::throw_with_loc<std::runtime_error>("no field eos_type is found in this json");
154 }
155
156 std::string eos_type;
157 j.at("eos_type").get_to(eos_type);
158
159 using Isothermal = typename EOSConfig<Tvec>::Isothermal;
160 using Adiabatic = typename EOSConfig<Tvec>::Adiabatic;
161 using Polytropic = typename EOSConfig<Tvec>::Polytropic;
162 using LocIsoT = typename EOSConfig<Tvec>::LocallyIsothermal;
163 using LocIsoTLP07 = typename EOSConfig<Tvec>::LocallyIsothermalLP07;
164 using LocIsoTFA2014 = typename EOSConfig<Tvec>::LocallyIsothermalFA2014;
165 using LocIsoTFA2014Extended = typename EOSConfig<Tvec>::LocallyIsothermalFA2014Extended;
166 using Fermi = typename EOSConfig<Tvec>::Fermi;
167 using Barotropic = typename EOSConfig<Tvec>::Barotropic;
168
169 if (eos_type == "isothermal") {
170 p.config = Isothermal{j.at("cs").get<Tscal>()};
171 } else if (eos_type == "adiabatic") {
172 p.config = Adiabatic{j.at("gamma").get<Tscal>()};
173 } else if (eos_type == "polytropic") {
174 p.config = Polytropic{j.at("K").get<Tscal>(), j.at("gamma").get<Tscal>()};
175 } else if (eos_type == "locally_isothermal") {
176 p.config = LocIsoT{};
177 } else if (eos_type == "locally_isothermal_lp07") {
178 p.config = LocIsoTLP07{
179 j.at("cs0").get<Tscal>(), j.at("q").get<Tscal>(), j.at("r0").get<Tscal>()};
180 } else if (eos_type == "locally_isothermal_fa2014") {
181 p.config = LocIsoTFA2014{j.at("h_over_r").get<Tscal>()};
182 } else if (eos_type == "locally_isothermal_fa2014_extended") {
183 p.config = LocIsoTFA2014Extended{
184 j.at("cs0").get<Tscal>(),
185 j.at("q").get<Tscal>(),
186 j.at("r0").get<Tscal>(),
187 j.at("n_sinks").get<u32>()};
188 } else if (eos_type == "fermi") {
189 p.config = Fermi{j.at("mu_e").get<Tscal>()};
190 } else if (eos_type == "barotropic") {
191 p.config = Barotropic{
192 j.at("cs0").get<Tscal>(), j.at("rho0").get<Tscal>(), j.at("gamma0").get<Tscal>()};
193 } else {
194 shambase::throw_unimplemented("Unknown or unsupported eos_type found in json");
195 }
196 }
197
198 template void to_json<f64_3>(nlohmann::json &j, const EOSConfig<f64_3> &p);
199 template void from_json<f64_3>(const nlohmann::json &j, EOSConfig<f64_3> &p);
200
201} // namespace shammodels
void to_json(nlohmann::json &j, const PatchSchedulerConfig &p)
Converts a PatchSchedulerConfig object to a JSON object.
void from_json(const nlohmann::json &j, PatchSchedulerConfig &p)
Deserializes a PatchSchedulerConfig object from a JSON object.
std::uint32_t u32
32 bit unsigned integer
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
void throw_unimplemented(SourceLocation loc=SourceLocation{})
Throw a std::runtime_error saying that the function is unimplemented.
constexpr bool always_false_v
Helper variable template that is always false. Especially useful to perform static asserts based on t...
namespace for models
Definition AMRBlock.hpp:26
Locally isothermal equation of state configuration.
Definition EOSConfig.hpp:61
Configuration struct for the equation of state used in the hydrodynamic models.
Definition EOSConfig.hpp:42
Variant config
Current EOS configuration.
Definition EOSConfig.hpp:91
shamphys::EOS_Config_Polytropic< Tscal > Polytropic
Polytropic equation of state configuration.
Definition EOSConfig.hpp:55
shamphys::EOS_Config_Isothermal< Tscal > Isothermal
Isothermal equation of state configuration.
Definition EOSConfig.hpp:58
shamphys::EOS_Config_LocallyIsothermalDisc_Farris2014< Tscal > LocallyIsothermalFA2014
Locally isothermal equation of state configuration from Lodato Price 2007.
Definition EOSConfig.hpp:67
shamphys::EOS_Config_LocallyIsothermal_LP07< Tscal > LocallyIsothermalLP07
Locally isothermal equation of state configuration from Lodato Price 2007.
Definition EOSConfig.hpp:64
shamphys::EOS_Config_Fermi< Tscal > Fermi
Fermi equation of state configuration.
Definition EOSConfig.hpp:73
shamphys::EOS_Config_LocallyIsothermalDisc_ExtendedFarris2014< Tscal > LocallyIsothermalFA2014Extended
Locally isothermal equation of state configuration from Farris 2014 extended to q !...
Definition EOSConfig.hpp:70
shamphys::EOS_Config_Adiabatic< Tscal > Adiabatic
Adiabatic equation of state configuration.
Definition EOSConfig.hpp:52
shamphys::EOS_Config_Barotropic< Tscal > Barotropic
Barotropic equation of state configuration.
Definition EOSConfig.hpp:76