Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
MHDConfig.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
19
20#include "shambackends/vec.hpp"
23#include <nlohmann/json.hpp>
24#include <variant>
25
26namespace shammodels::sph {
27
28 template<class Tvec>
29 struct MHDConfig;
30}
31
32template<class Tvec>
34
35 using Tscal = shambase::VecComponent<Tvec>;
36 static constexpr u32 dim = shambase::VectorProperties<Tvec>::dimension;
37
38 struct None {};
39
41 Tscal sigma_mhd = 0.1;
42 Tscal alpha_B = 1.;
43 };
44
45 struct NonIdealMHD {
46 Tscal sigma_mhd = 0.1;
47 Tscal alpha_B = 1.;
48 Tscal etaO = 1.;
49 Tscal etaH = 1.;
50 Tscal etaAD = 1.;
51 };
52
53 // how to set a new state of a variant as a dummy:
54 // a) do everything right
55 // b) forget to add the state to the variant
56 //-> question your life choices
57 using Variant = std::variant<None, IdealMhdConstrainedHyperPara, NonIdealMHD>;
58
59 Variant configMHD = None{};
60
61 void set(Variant v) { configMHD = v; }
62
63 inline bool do_nimhd() {
64 bool is_NIMHD = bool(std::get_if<NonIdealMHD>(&configMHD));
65 return is_NIMHD;
66 }
67
68 inline bool has_b_field() {
69 bool is_B = bool(std::get_if<IdealMhdConstrainedHyperPara>(&configMHD))
70 || bool(std::get_if<NonIdealMHD>(&configMHD));
71 return is_B;
72 }
73
74 inline bool has_psi_field() {
75 bool is_psi = bool(std::get_if<IdealMhdConstrainedHyperPara>(&configMHD))
76 || bool(std::get_if<NonIdealMHD>(&configMHD));
77 return is_psi;
78 }
79
80 inline bool has_div_b_field() {
81 bool is_divB = bool(std::get_if<IdealMhdConstrainedHyperPara>(&configMHD));
82 return is_divB;
83 }
84
85 inline bool has_curl_b_field() {
86 bool is_curlB = bool(std::get_if<NonIdealMHD>(&configMHD));
87 return is_curlB;
88 }
89
90 inline bool has_dtdiv_b_field() {
91 bool is_dtdivB = bool(std::get_if<NonIdealMHD>(&configMHD));
92 return is_dtdivB;
93 }
94
95 inline void print_status() {
96 logger::raw_ln("--- MHD configMHD");
97
98 if (None *v = std::get_if<None>(&configMHD)) {
99 logger::raw_ln(" Config MHD Type : None (No MHD)");
100 } else if (
102 = std::get_if<IdealMhdConstrainedHyperPara>(&configMHD)) {
103 logger::raw_ln(" Config MHD : Ideal MHD, constrained hyperbolic/parabolic treatment");
104 logger::raw_ln(" sigma_mhd =", v->sigma_mhd);
105 logger::raw_ln(" alpha_B =", v->alpha_B);
106 } else if (NonIdealMHD *v = std::get_if<NonIdealMHD>(&configMHD)) {
107 logger::raw_ln(" Config MHD Type : Non Ideal MHD");
108 logger::raw_ln(" sigma_mhd =", v->sigma_mhd);
109 logger::raw_ln(" alpha_B =", v->alpha_B);
110 logger::raw_ln(" etaO =", v->etaO);
111 logger::raw_ln(" etaH =", v->etaH);
112 logger::raw_ln(" etaAD =", v->etaAD);
113 } else {
115 }
116
117 logger::raw_ln("--- MHD configMHD (deduced)");
118
119 logger::raw_ln("-------------");
120 }
121
122 inline void check_config() {
123
124 if (do_nimhd()) {
125
127 shambase::throw_with_loc<std::runtime_error>("Non Ideal MHD is experimental");
128 }
129 }
130 }
131};
132
133namespace shammodels::sph {
134
141 template<class Tvec>
142 inline void to_json(nlohmann::json &j, const MHDConfig<Tvec> &p) {
143 using T = MHDConfig<Tvec>;
144
145 using None = typename T::None;
146 using IMHD = typename T::IdealMhdConstrainedHyperPara;
147 using NonIdealMHD = typename T::NonIdealMHD;
148
149 // Write the configMHD type into the JSON object
150 if (const None *v = std::get_if<None>(&p.configMHD)) {
151 j = {
152 {"mhd_type", "none"},
153 };
154 } else if (const IMHD *v = std::get_if<IMHD>(&p.configMHD)) {
155 j = {
156 {"mhd_type", "ideal_mhd_constrained_hyper_para"},
157 {"sigma_mhd", v->sigma_mhd},
158 {"alpha_B", v->alpha_B},
159 };
160 } else if (const NonIdealMHD *v = std::get_if<NonIdealMHD>(&p.configMHD)) {
161 // Write the shear base, direction, and speed into the JSON object
162 j = {
163 {"mhd_type", "non_ideal_mhd"},
164 {"sigma_mhd", v->sigma_mhd},
165 {"alpha_B", v->alpha_B},
166 {"etaO", v->etaO},
167 {"etaH", v->etaH},
168 {"etaAD", v->etaAD},
169 };
170 } else {
172 }
173 }
174
181 template<class Tvec>
182 inline void from_json(const nlohmann::json &j, MHDConfig<Tvec> &p) {
183 using T = MHDConfig<Tvec>;
184
185 using Tscal = shambase::VecComponent<Tvec>;
186
187 // Check if the JSON object contains the "mhd_type" field
188 if (!j.contains("mhd_type")) {
189 shambase::throw_with_loc<std::runtime_error>("no field mhd_type is found in this json");
190 }
191
192 // Read the configMHD type from the JSON object
193 std::string mhd_type;
194 j.at("mhd_type").get_to(mhd_type);
195
196 using None = typename T::None;
197 using IMHD = typename T::IdealMhdConstrainedHyperPara;
198 using NonIdealMHD = typename T::NonIdealMHD;
199
200 // Set the BCConfig based on the configMHD type
201 if (mhd_type == "none") {
202 p.set(None{});
203 } else if (mhd_type == "ideal_mhd_constrained_hyper_para") {
204 // alpha_u, alpha_AV & beta_AV are now in the artificial viscosity config, they are
205 // ignored if present in older configs
206 IMHD def{};
207 p.set(
208 IMHD{
209 j.at("sigma_mhd").get<Tscal>(),
210 j.value("alpha_B", def.alpha_B),
211 });
212 } else if (mhd_type == "non_ideal_mhd") {
213 // alpha_u, alpha_AV & beta_AV are now in the artificial viscosity config, they are
214 // ignored if present in older configs
215 NonIdealMHD def{};
216 p.set(
217 NonIdealMHD{
218 j.at("sigma_mhd").get<Tscal>(),
219 j.value("alpha_B", def.alpha_B),
220 j.value("etaO", def.etaO),
221 j.value("etaH", def.etaH),
222 j.value("etaAD", def.etaAD),
223 });
224 } else {
226 }
227 }
228
229} // namespace shammodels::sph
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 the sph model
bool are_experimental_features_allowed()
Allow the use of experimental features.