Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
SolverConfig.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
27
29
30 template<class Tvec, class TgridVec>
31 inline void SolverConfig<Tvec, TgridVec>::set_layout(
32 shamrock::patch::PatchDataLayerLayout &pdl) {
33 pdl.add_field<TgridVec>("cell_min", 1);
34 pdl.add_field<TgridVec>("cell_max", 1);
35 pdl.add_field<Tscal>("rho", AMRBlock::block_size);
36 pdl.add_field<Tvec>("rhovel", AMRBlock::block_size);
37 pdl.add_field<Tscal>("rhoetot", AMRBlock::block_size);
38
39 if (is_dust_on()) {
40 u32 ndust = dust_config.ndust;
41 pdl.add_field<Tscal>("rho_dust", (ndust * AMRBlock::block_size));
42 pdl.add_field<Tvec>("rhovel_dust", (ndust * AMRBlock::block_size));
43 }
44
45 if (is_gravity_on()) {
46 pdl.add_field<Tscal>("phi", AMRBlock::block_size);
47 pdl.add_field<Tscal>("phi_old", AMRBlock::block_size);
48 }
49
50 if (is_gas_passive_scalar_on()) {
51 u32 npscal_gas = npscal_gas_config.npscal_gas;
52 pdl.add_field<Tscal>("rho_gas_pscal", (npscal_gas * AMRBlock::block_size));
53 }
54 }
55
56 template<class Tvec, class TgridVec>
57 void to_json(nlohmann::json &j, const SolverConfig<Tvec, TgridVec> &p) {
58
59 j = nlohmann::json{
60 {"type_id", shambase::get_type_name<Tvec>()},
61 {"scheduler_config", p.scheduler_conf},
62 {"courant_safety_factor", p.Csafe},
63 {"dust_riemann_solver", p.dust_config.dust_riemann_config},
64 {"ndust", p.dust_config.ndust},
65 {"eos_config", p.eos_config},
66 {"cooling_config", p.cooling_config},
67 {"face_half_time_interpolation", p.face_half_time_interpolation},
68 {"gravity_solver", p.gravity_config.gravity_mode},
69 {"gravity_tol", p.gravity_config.tol},
70 {"gravity_tol_happy_breakdown", p.gravity_config.tol_hp_bk},
71 {"gravity_Niter_max", p.gravity_config.Niter_max},
72 {"grid_coord_to_pos_fact", p.grid_coord_to_pos_fact},
73 {"hydro_riemann_solver", p.riemann_config},
74 {"passive_scalar_mode", p.npscal_gas_config.npscal_gas},
75 {"slope_limiter", p.slope_config},
76 {"unit_sys", p.unit_sys},
77 {"drag_config", p.drag_config},
78 {"bc_config", p.bc_config},
79 {"amr_mode", p.amr_mode},
80 {"amr_interp_mode", p.amr_interp_mode},
81 {"ext_force_config", p.ext_force_config}};
82 }
83
84 template<class Tvec, class TgridVec>
85 void from_json(const nlohmann::json &j, SolverConfig<Tvec, TgridVec> &p) {
87
88 if (j.contains("type_id")) {
89
90 std::string type_id = j.at("type_id").get<std::string>();
91
92 if (type_id != shambase::get_type_name<Tvec>()) {
94 "Invalid type to deserialize, wanted " + shambase::get_type_name<Tvec>()
95 + " but got " + type_id);
96 }
97 }
98
99 bool has_used_defaults = false;
100 bool has_updated_config = false;
101
102 auto _get_to_if_contains = [&](const std::string &key, auto &value) {
103 shamrock::get_to_if_contains(j, key, value, has_used_defaults);
104 };
105
106 _get_to_if_contains("scheduler_config", p.scheduler_conf);
107
108 // actual data stored in the json
109 _get_to_if_contains("courant_safety_factor", p.Csafe);
110 _get_to_if_contains("dust_riemann_solver", p.dust_config.dust_riemann_config);
111 _get_to_if_contains("ndust", p.dust_config.ndust);
112 // configs dumped before the cooling existed stored the instantaneous barotropic cooling as
113 // the "reset_barotropic" EOS: the adiabatic EOS of index gamma with the barotrope
114 // (rho_crit, cs0, gamma_baro)
115 bool is_legacy_reset_barotropic = j.contains("eos_config")
116 && j.at("eos_config").contains("type")
117 && j.at("eos_config").at("type") == "reset_barotropic";
118
119 if (is_legacy_reset_barotropic) {
120 using Tscal = typename T::Tscal;
121 const nlohmann::json &j_eos = j.at("eos_config");
122 p.eos_config.set_adiabatic(j_eos.at("gamma").get<Tscal>());
123 p.cooling_config.set_instant_barotropic(
124 j_eos.at("rho_crit").get<Tscal>(),
125 j_eos.at("cs0").get<Tscal>(),
126 j_eos.at("gamma_baro").get<Tscal>());
127 has_updated_config = true;
128 if (shamcomm::world_rank() == 0) {
130 "Ramses::SolverConfig",
131 "Updating old [eos_config] of type [reset_barotropic] to an adiabatic "
132 "[eos_config] with an [instant_barotropic] [cooling_config] in from_json");
133 }
134 } else if (j.contains("eos_config")) {
135 j.at("eos_config").get_to(p.eos_config);
136 } else if (j.contains("eos_gamma")) {
137 // legacy configs only stored the adiabatic index
138 p.set_eos_gamma(j.at("eos_gamma").get<typename T::Tscal>());
139 has_updated_config = true;
140 if (shamcomm::world_rank() == 0) {
142 "Ramses::SolverConfig",
143 "Updating old key [eos_gamma] to new key [eos_config] in from_json");
144 }
145 } else {
146 has_used_defaults = true;
147 }
148 if (!is_legacy_reset_barotropic) {
149 _get_to_if_contains("cooling_config", p.cooling_config);
150 }
151 _get_to_if_contains("face_half_time_interpolation", p.face_half_time_interpolation);
152 _get_to_if_contains("gravity_solver", p.gravity_config.gravity_mode);
153 _get_to_if_contains("gravity_tol", p.gravity_config.tol);
154 _get_to_if_contains("gravity_tol_happy_breakdown", p.gravity_config.tol_hp_bk);
155 _get_to_if_contains("gravity_Niter_max", p.gravity_config.Niter_max);
156 _get_to_if_contains("grid_coord_to_pos_fact", p.grid_coord_to_pos_fact);
157 _get_to_if_contains("hydro_riemann_solver", p.riemann_config);
158 _get_to_if_contains("passive_scalar_mode", p.npscal_gas_config.npscal_gas);
159 _get_to_if_contains("slope_limiter", p.slope_config);
160 _get_to_if_contains("unit_sys", p.unit_sys);
161 _get_to_if_contains("drag_config", p.drag_config);
162 _get_to_if_contains("bc_config", p.bc_config);
163 _get_to_if_contains("amr_mode", p.amr_mode);
164 _get_to_if_contains("amr_interp_mode", p.amr_interp_mode);
165 _get_to_if_contains("ext_force_config", p.ext_force_config);
166
167 if (has_used_defaults || has_updated_config) {
168 if (shamcomm::world_rank() == 0) {
170 "Ramses::SolverConfig",
171 shamrock::log_json_changes(p, j, has_used_defaults, has_updated_config));
172 }
173 }
174 }
175
176 template void to_json<f64_3, i64_3>(nlohmann::json &j, const SolverConfig<f64_3, i64_3> &p);
177 template void from_json<f64_3, i64_3>(const nlohmann::json &j, SolverConfig<f64_3, i64_3> &p);
178
179} // namespace shammodels::basegodunov
180
std::uint32_t u32
32 bit unsigned integer
void add_field(const std::string &field_name, u32 nvar, SourceLocation loc=SourceLocation{})
add a field of type T to the layout
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
i32 world_rank()
Gives the rank of the current process in the MPI communicator.
Definition worldInfo.cpp:41
namespace for the basegodunov model
void get_to_if_contains(const nlohmann::json &j, const std::string &key, T &value, bool &has_used_defaults)
std::string log_json_changes(const nlohmann::json &j_current, const nlohmann::json &j, bool has_used_defaults, bool has_updated_config)
Shown the changes between two JSON objects to log config changes.
void info_ln(std::string module_name, Types... var2)
Prints a log message with multiple arguments followed by a newline.
Definition logs.hpp:132
void warn_ln(std::string module_name, Types... var2)
Prints a log message with multiple arguments followed by a newline.
Definition logs.hpp:132
void set_eos_gamma(Tscal gamma)
Set an adiabatic equation of state (kept for backward compatibility).
ExtForceConfig ext_force_config
External forces applied to the gas.
AMRMode< Tvec, TgridVec > amr_mode
AMR refinement mode.
std::optional< shamunits::UnitSystem< Tscal > > unit_sys
The unit system of the simulation.
EOSConfig< Tvec > eos_config
Equation of state of the gas.
CoolingConfig< Tvec > cooling_config
Cooling of the gas.