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
27
29
30 template<class Tvec, class TgridVec>
31 inline void SolverConfig<Tvec, TgridVec>::set_layout(
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 }
48
49 if (is_gas_passive_scalar_on()) {
50 u32 npscal_gas = npscal_gas_config.npscal_gas;
51 pdl.add_field<Tscal>("rho_gas_pscal", (npscal_gas * AMRBlock::block_size));
52 }
53 }
54
55 template<class Tvec, class TgridVec>
56 void to_json(nlohmann::json &j, const SolverConfig<Tvec, TgridVec> &p) {
57
58 j = nlohmann::json{
59 {"type_id", shambase::get_type_name<Tvec>()},
60 {"scheduler_config", p.scheduler_conf},
61 {"courant_safety_factor", p.Csafe},
62 {"dust_riemann_solver", p.dust_config.dust_riemann_config},
63 {"eos_gamma", p.eos_gamma},
64 {"face_half_time_interpolation", p.face_half_time_interpolation},
65 {"gravity_solver", p.gravity_config.gravity_mode},
66 {"grid_coord_to_pos_fact", p.grid_coord_to_pos_fact},
67 {"hydro_riemann_solver", p.riemann_config},
68 {"passive_scalar_mode", p.npscal_gas_config.npscal_gas},
69 {"slope_limiter", p.slope_config},
70 {"time_state", p.time_state},
71 {"unit_sys", p.unit_sys}};
72 }
73
74 template<class Tvec, class TgridVec>
75 void from_json(const nlohmann::json &j, SolverConfig<Tvec, TgridVec> &p) {
77
78 if (j.contains("type_id")) {
79
80 std::string type_id = j.at("type_id").get<std::string>();
81
82 if (type_id != shambase::get_type_name<Tvec>()) {
84 "Invalid type to deserialize, wanted " + shambase::get_type_name<Tvec>()
85 + " but got " + type_id);
86 }
87 }
88
89 bool has_used_defaults = false;
90 bool has_updated_config = false;
91
92 auto _get_to_if_contains = [&](const std::string &key, auto &value) {
93 shamrock::get_to_if_contains(j, key, value, has_used_defaults);
94 };
95
96 _get_to_if_contains("scheduler_config", p.scheduler_conf);
97
98 // actual data stored in the json
99 _get_to_if_contains("courant_safety_factor", p.Csafe);
100 _get_to_if_contains("dust_riemann_solver", p.dust_config.dust_riemann_config);
101 _get_to_if_contains("eos_gamma", p.eos_gamma);
102 _get_to_if_contains("face_half_time_interpolation", p.face_half_time_interpolation);
103 _get_to_if_contains("gravity_solver", p.gravity_config.gravity_mode);
104 _get_to_if_contains("grid_coord_to_pos_fact", p.grid_coord_to_pos_fact);
105 _get_to_if_contains("hydro_riemann_solver", p.riemann_config);
106 _get_to_if_contains("passive_scalar_mode", p.npscal_gas_config.npscal_gas);
107 _get_to_if_contains("slope_limiter", p.slope_config);
108 _get_to_if_contains("time_state", p.time_state);
109 _get_to_if_contains("unit_sys", p.unit_sys);
110
111 if (has_used_defaults || has_updated_config) {
112 if (shamcomm::world_rank() == 0) {
113 logger::info_ln(
114 "Ramses::SolverConfig",
115 shamrock::log_json_changes(p, j, has_used_defaults, has_updated_config));
116 }
117 }
118 }
119
120 template void to_json<f64_3, i64_3>(nlohmann::json &j, const SolverConfig<f64_3, i64_3> &p);
121 template void from_json<f64_3, i64_3>(const nlohmann::json &j, SolverConfig<f64_3, i64_3> &p);
122
123} // namespace shammodels::basegodunov
124
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:40
namespace for the basegodunov model
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 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.