Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
SolverConfig.hpp
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
10#pragma once
11
25#include "shambase/string.hpp"
26#include "shambackends/vec.hpp"
27#include "shamcomm/logs.hpp"
40#include <stdexcept>
41#include <variant>
42
44
48 struct DragConfig {
49 DragSolverMode drag_solver_config = NoDrag;
50 std::vector<f64> alphas;
51 bool enable_frictional_heating
52 = false; // 0 to turn off and 1 when all dissipation is deposited to the gas
53 };
54
55 struct DustConfig {
56 DustRiemannSolverMode dust_riemann_config = NoDust;
57 u32 ndust = 0;
58
59 inline bool is_dust_on() {
60 if (dust_riemann_config != NoDust) {
61
62 if (ndust == 0) {
64 "Dust is on with ndust == 0");
65 }
66 return true;
67 }
68 return false;
69 }
70 };
75 u32 npscal_gas = 0;
76
77 inline bool is_gas_passive_scalar_on() { return npscal_gas > 0; }
78 };
79
80 template<class Tvec>
82 using Tscal = shambase::VecComponent<Tvec>;
83 GravityMode gravity_mode = NoGravity;
84 bool analytical_gravity = false; // whether to use an external analytical gravity
85 Tscal tol = 1e-6;
86 inline Tscal get_tolerance() { return tol; }
87 inline bool is_gravity_on() { return gravity_mode != NoGravity; }
88 };
89
90 template<class Tvec>
91 struct SolverStatusVar;
92
93 template<class Tvec, class TgridVec>
94 struct AMRMode {
95
96 using Tscal = shambase::VecComponent<Tvec>;
97
98 struct None {};
99 struct DensityBased {
100 Tscal crit_mass;
101 };
102
103 using mode = std::variant<None, DensityBased>;
104
105 mode config = None{};
106 void set_refine_none() { config = None{}; }
107 void set_refine_density_based(Tscal crit_mass) { config = DensityBased{crit_mass}; }
108
109 bool need_level_zero_compute() { return false; }
110 bool need_amr_level_compute() { return false; }
111 };
112
113 struct BCConfig {
114 enum class GhostType { Periodic = 0, Reflective = 1, Outflow = 2 };
115
116 GhostType ghost_type_x = GhostType::Periodic;
117 GhostType ghost_type_y = GhostType::Periodic;
118 GhostType ghost_type_z = GhostType::Periodic;
119
120 GhostType get_x() const { return ghost_type_x; }
121 GhostType get_y() const { return ghost_type_y; }
122 GhostType get_z() const { return ghost_type_z; }
123
124 void set_x(GhostType ghost_type) { ghost_type_x = ghost_type; }
125 void set_y(GhostType ghost_type) { ghost_type_y = ghost_type; }
126 void set_z(GhostType ghost_type) { ghost_type_z = ghost_type; }
127 };
128
129 template<class Tvec, class TgridVec>
130 struct SolverConfig;
131
132}; // namespace shammodels::basegodunov
133
134template<class Tvec>
136
138 using Tscal = shambase::VecComponent<Tvec>;
139
141 Tscal dt = 0;
142};
143
144template<class Tvec, class TgridVec>
146
147 using Tscal = shambase::VecComponent<Tvec>;
148
149 Tscal eos_gamma = 5. / 3.;
150
151 Tscal grid_coord_to_pos_fact = 1;
152
153 static constexpr u32 NsideBlockPow = 1;
155
156 inline void set_eos_gamma(Tscal gamma) { eos_gamma = gamma; }
157
158 RiemannSolverMode riemann_config = HLL;
159 SlopeMode slope_config = VanLeer_sym;
160 bool face_half_time_interpolation = true;
161
162 inline bool should_compute_rho_mean() { return is_gravity_on() && is_boundary_periodic(); }
163
165 // Dust config
167
168 DustConfig dust_config{};
169 DragConfig drag_config{};
170
171 inline bool is_dust_on() { return dust_config.is_dust_on(); }
172 // get alpha values from user
173 // alphas is the dust collision rate (the inverse of the stopping time)
174 inline void set_alphas_static(f32 alpha_values) {
175 StackEntry stack_lock{};
176 drag_config.alphas.push_back(alpha_values);
177 }
178
180 // Dust config (END)
182
183 BCConfig bc_config{};
184
186 // Gas passive scalars config
188
189 PassiveScalarGasConfig npscal_gas_config{};
190
191 inline bool is_gas_passive_scalar_on() { return npscal_gas_config.is_gas_passive_scalar_on(); }
193 // Gas passive scalars config (END)
195
197 // Gravity config
199 inline Tscal get_constant_G() {
200 if (!unit_sys) {
201 ON_RANK_0(logger::warn_ln("amr::Config", "the unit system is not set"));
203 return ctes.G();
204 } else {
206 }
207 }
208 inline bool is_boundary_periodic() { return true; }
209 GravityConfig<Tvec> gravity_config{};
210 inline Tscal get_constant_4piG() {
211 auto scal_G = get_constant_G();
212 return 4 * M_PI * scal_G;
213 }
214 inline Tscal get_grav_tol() { return gravity_config.get_tolerance(); }
215 inline bool is_gravity_on() { return gravity_config.is_gravity_on(); }
216 inline bool is_coordinate_field_required() { return gravity_config.analytical_gravity; }
217
219 // Gravity config (END)
221
224
226 // Units Config
228
230 std::optional<shamunits::UnitSystem<Tscal>> unit_sys = {};
231
233 inline void set_units(shamunits::UnitSystem<Tscal> new_sys) { unit_sys = new_sys; }
235 // Units Config (END)
237
238 PatchSchedulerConfig scheduler_conf = {};
239
241 // Solver status variables
243
249 inline void set_time(Tscal t) { time_state.time = t; }
251 inline void set_next_dt(Tscal dt) { time_state.dt = dt; }
253 inline Tscal get_time() { return time_state.time; }
255 inline Tscal get_dt() { return time_state.dt; }
256
257 Tscal Csafe = 0.9;
259 // Solver status variables (END)
261
262 inline void check_config() {
263 if (grid_coord_to_pos_fact <= 0) {
265 "grid_coord_to_pos_fact must be > 0, got {}", grid_coord_to_pos_fact));
266 }
267
268 if (is_dust_on()) {
269 ON_RANK_0(logger::warn_ln("Ramses::SolverConfig", "Dust is experimental"));
270 }
271
272 if (is_gravity_on()) {
273 ON_RANK_0(logger::warn_ln("Ramses::SolverConfig", "Self gravity is experimental"));
274 u32 mode = gravity_config.gravity_mode;
275
277 shambase::format(
278 "self gravity mode is not enabled but gravity mode is set to {} (> 0 whith 0 "
279 "== "
280 "NoGravity mode)",
281 mode));
282 }
283
284 if (!(eos_gamma > 1.0)) {
286 shambase::format("Gamma must be > 1, currently Gamma = {}", eos_gamma));
287 }
288
289 if (is_gas_passive_scalar_on()) {
290 ON_RANK_0(logger::warn_ln("Ramses::SolverConfig", "Passive scalars are experimental"));
292 shambase::format(
293 "gas passive scalars mode is not enabled but gas passive scalars mode is set "
294 "to {}"
295 "> 0",
296 npscal_gas_config.npscal_gas));
297 }
298 }
299
300 void set_layout(shamrock::patch::PatchDataLayerLayout &pdl);
301};
302
303namespace shammodels::basegodunov {
304
305 template<class Tvec>
306 inline void to_json(nlohmann::json &j, const SolverStatusVar<Tvec> &p) {
307 j = nlohmann::json{{"time", p.time}, {"dt", p.dt}};
308 }
309
310 template<class Tvec>
311 inline void from_json(const nlohmann::json &j, SolverStatusVar<Tvec> &p) {
312 using Tscal = typename SolverStatusVar<Tvec>::Tscal;
313 j.at("time").get_to<Tscal>(p.time);
314 j.at("dt").get_to<Tscal>(p.dt);
315 }
316
323 template<class Tvec, class TgridVec>
324 void to_json(nlohmann::json &j, const SolverConfig<Tvec, TgridVec> &p);
331 template<class Tvec, class TgridVec>
332 void from_json(const nlohmann::json &j, SolverConfig<Tvec, TgridVec> &p);
333
334} // namespace shammodels::basegodunov
utility to manipulate AMR blocks
MPI scheduler.
float f32
Alias for float.
std::uint32_t u32
32 bit unsigned integer
Defines a unit system.
Drag solver mode enum + json serialization/deserialization.
Dust Riemann solver mode enum + json serialization/deserialization.
Gravity mode enum + json serialization/deserialization.
Riemann solver mode enum + json serialization/deserialization.
Slope mode enum + json serialization/deserialization.
This header file contains utility functions related to exception handling in the code.
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
namespace for the basegodunov model
DustRiemannSolverMode
Dust Riemann solver mode enum.
@ NoDust
No dust, so no Riemann solver is used.
SlopeMode
Slope limiter modes.
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.
void experimental_feature_check(const std::string &message, SourceLocation loc=SourceLocation{})
Check if experimental features are enabled, if not throw with the given message.
utility class to handle AMR blocks
Definition AMRBlock.hpp:35
alphas is the dust collision rate (the inverse of the stopping time)
Npscal_gas is the number of gas passive scalars.
SolverStatusVar< Tvec > SolverStatusVar
Alias to SolverStatusVar type.
Tscal get_time()
Get the current time.
AMRMode< Tvec, TgridVec > amr_mode
AMR refinement mode.
void set_next_dt(Tscal dt)
Set the time step for the next iteration.
std::optional< shamunits::UnitSystem< Tscal > > unit_sys
The unit system of the simulation.
Tscal get_dt()
Get the time step for the next iteration.
void set_time(Tscal t)
Set the current time.
void set_units(shamunits::UnitSystem< Tscal > new_sys)
Set the unit system of the simulation.
SolverStatusVar time_state
The time sate of the simulation.
shambase::VecComponent< Tvec > Tscal
The type of the scalar used to represent the quantities.
Physical constants.
constexpr T G()
get the value of G in the current unit system units
#define ON_RANK_0(x)
Macro to execute code only on rank 0.
Definition worldInfo.hpp:73