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
21
22namespace shammodels {
23
42 template<class Tvec>
43 void to_json(nlohmann::json &j, const EOSConfig<Tvec> &p) {
44 // Serialize EOSConfig to a json object
45
46 using json = nlohmann::json;
47
48 std::string type_id = "";
49
50 if constexpr (std::is_same_v<f32_3, Tvec>) {
51 type_id = "f32_3"; // type of the vector quantities (e.g. position)
52 } else if constexpr (std::is_same_v<f64_3, Tvec>) {
53 type_id = "f64_3"; // type of the vector quantities (e.g. position)
54 } else {
55 static_assert(shambase::always_false_v<Tvec>, "This Tvec type is not handled");
56 }
57
58 using Isothermal = typename EOSConfig<Tvec>::Isothermal;
59 using Adiabatic = typename EOSConfig<Tvec>::Adiabatic;
60 using Polytropic = typename EOSConfig<Tvec>::Polytropic;
61 using LocIsoT = typename EOSConfig<Tvec>::LocallyIsothermal;
62 using LocIsoTLP07 = typename EOSConfig<Tvec>::LocallyIsothermalLP07;
63 using LocIsoTFA2014 = typename EOSConfig<Tvec>::LocallyIsothermalFA2014;
64 using LocIsoTFA2014Extended = typename EOSConfig<Tvec>::LocallyIsothermalFA2014Extended;
65 using Fermi = typename EOSConfig<Tvec>::Fermi;
66
67 if (const Isothermal *eos_config = std::get_if<Isothermal>(&p.config)) {
68 j = json{{"Tvec", type_id}, {"eos_type", "isothermal"}, {"cs", eos_config->cs}};
69 } else if (const Adiabatic *eos_config = std::get_if<Adiabatic>(&p.config)) {
70 j = json{{"Tvec", type_id}, {"eos_type", "adiabatic"}, {"gamma", eos_config->gamma}};
71 } else if (const Polytropic *eos_config = std::get_if<Polytropic>(&p.config)) {
72 j = json{
73 {"Tvec", type_id},
74 {"eos_type", "polytropic"},
75 {"K", eos_config->K},
76 {"gamma", eos_config->gamma}};
77 } else if (const LocIsoT *eos_config = std::get_if<LocIsoT>(&p.config)) {
78 j = json{{"Tvec", type_id}, {"eos_type", "locally_isothermal"}};
79 } else if (const LocIsoTLP07 *eos_config = std::get_if<LocIsoTLP07>(&p.config)) {
80 j = json{
81 {"Tvec", type_id},
82 {"eos_type", "locally_isothermal_lp07"},
83 {"cs0", eos_config->cs0},
84 {"q", eos_config->q},
85 {"r0", eos_config->r0}};
86 } else if (const LocIsoTFA2014 *eos_config = std::get_if<LocIsoTFA2014>(&p.config)) {
87 j = json{
88 {"Tvec", type_id},
89 {"eos_type", "locally_isothermal_fa2014"},
90 {"h_over_r", eos_config->h_over_r}};
91 } else if (
92 const LocIsoTFA2014Extended *eos_config
93 = std::get_if<LocIsoTFA2014Extended>(&p.config)) {
94 j = json{
95 {"Tvec", type_id},
96 {"eos_type", "locally_isothermal_fa2014_extended"},
97 {"cs0", eos_config->cs0},
98 {"q", eos_config->q},
99 {"r0", eos_config->r0},
100 {"n_sinks", eos_config->n_sinks}};
101 } else if (const Fermi *eos_config = std::get_if<Fermi>(&p.config)) {
102 j = json{{"Tvec", type_id}, {"eos_type", "fermi"}, {"mu_e", eos_config->mu_e}};
103 } else {
104 shambase::throw_unimplemented(); // should never be reached
105 }
106 }
107
120 template<class Tvec>
121 void from_json(const nlohmann::json &j, EOSConfig<Tvec> &p) {
122
123 using Tscal = shambase::VecComponent<Tvec>;
124
125 std::string type_id;
126 j.at("Tvec").get_to(type_id);
127
128 if constexpr (std::is_same_v<f32_3, Tvec>) {
129 if (type_id != "f32_3") {
131 "You are trying to create a EOSConfig with the wrong vector type");
132 }
133 } else if constexpr (std::is_same_v<f64_3, Tvec>) {
134 if (type_id != "f64_3") {
136 "You are trying to create a EOSConfig with the wrong vector type");
137 }
138 } else {
139 static_assert(shambase::always_false_v<Tvec>, "This Tvec type is not handled");
140 }
141
142 if (!j.contains("eos_type")) {
143 shambase::throw_with_loc<std::runtime_error>("no field eos_type is found in this json");
144 }
145
146 std::string eos_type;
147 j.at("eos_type").get_to(eos_type);
148
149 using Isothermal = typename EOSConfig<Tvec>::Isothermal;
150 using Adiabatic = typename EOSConfig<Tvec>::Adiabatic;
151 using Polytropic = typename EOSConfig<Tvec>::Polytropic;
152 using LocIsoT = typename EOSConfig<Tvec>::LocallyIsothermal;
153 using LocIsoTLP07 = typename EOSConfig<Tvec>::LocallyIsothermalLP07;
154 using LocIsoTFA2014 = typename EOSConfig<Tvec>::LocallyIsothermalFA2014;
155 using LocIsoTFA2014Extended = typename EOSConfig<Tvec>::LocallyIsothermalFA2014Extended;
156 using Fermi = typename EOSConfig<Tvec>::Fermi;
157
158 if (eos_type == "isothermal") {
159 p.config = Isothermal{j.at("cs").get<Tscal>()};
160 } else if (eos_type == "adiabatic") {
161 p.config = Adiabatic{j.at("gamma").get<Tscal>()};
162 } else if (eos_type == "polytropic") {
163 p.config = Polytropic{j.at("K").get<Tscal>(), j.at("gamma").get<Tscal>()};
164 } else if (eos_type == "locally_isothermal") {
165 p.config = LocIsoT{};
166 } else if (eos_type == "locally_isothermal_lp07") {
167 p.config = LocIsoTLP07{
168 j.at("cs0").get<Tscal>(), j.at("q").get<Tscal>(), j.at("r0").get<Tscal>()};
169 } else if (eos_type == "locally_isothermal_fa2014") {
170 p.config = LocIsoTFA2014{j.at("h_over_r").get<Tscal>()};
171 } else if (eos_type == "locally_isothermal_fa2014_extended") {
172 p.config = LocIsoTFA2014Extended{
173 j.at("cs0").get<Tscal>(),
174 j.at("q").get<Tscal>(),
175 j.at("r0").get<Tscal>(),
176 j.at("n_sinks").get<u32>()};
177 } else if (eos_type == "fermi") {
178 p.config = Fermi{j.at("mu_e").get<Tscal>()};
179 } else {
180 shambase::throw_unimplemented("Unknown or unsupported eos_type found in json");
181 }
182 }
183
184 template void to_json<f64_3>(nlohmann::json &j, const EOSConfig<f64_3> &p);
185 template void from_json<f64_3>(const nlohmann::json &j, EOSConfig<f64_3> &p);
186
187} // namespace shammodels
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.
namespace for models
Definition AMRBlock.hpp:26
void to_json(nlohmann::json &j, const EOSConfig< Tvec > &p)
Serialize EOSConfig to json.
Definition EOSConfig.cpp:43
void from_json(const nlohmann::json &j, EOSConfig< Tvec > &p)
Deserializes an EOSConfig<Tvec> from a JSON object.
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
Configuration struct for adiabatic equation of state.
Configuration struct for Fermi equation of state.
Tscal mu_e
mu_e is the mean molecular weight
Configuration struct for isothermal equation of state.
Configuration struct for the locally isothermal equation of state extended from Farris 2014 to includ...
Configuration struct for the locally isothermal equation of state from Farris 2014.
Configuration struct for the locally isothermal equation of state from Lodato Price 2007.
Configuration struct for polytropic equation of state.