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
20#include "shambackends/vec.hpp"
22#include <nlohmann/json.hpp>
23#include <variant>
24
25namespace shammodels::sph {
26
27 template<class Tvec>
28 struct MHDConfig;
29}
30
31template<class Tvec>
33
34 using Tscal = shambase::VecComponent<Tvec>;
36
37 struct None {};
38
40 Tscal sigma_mhd = 0.1;
41 Tscal alpha_u = 1.;
42 };
43
44 struct NonIdealMHD {
45 Tscal sigma_mhd = 0.1;
46 Tscal alpha_u = 1.;
47 };
48
49 // how to set a new state of a variant as a dummy:
50 // a) do everything right
51 // b) forget to add the state to the variant
52 //-> question your life choices
53 using Variant = std::variant<None, IdealMHD_constrained_hyper_para, NonIdealMHD>;
54
55 Variant config = None{};
56
57 void set(Variant v) { config = v; }
58
59 inline bool has_B_field() {
60 bool is_B = bool(std::get_if<IdealMHD_constrained_hyper_para>(&config))
61 || bool(std::get_if<NonIdealMHD>(&config));
62 return is_B;
63 }
64
65 inline bool has_psi_field() {
66 bool is_psi = bool(std::get_if<IdealMHD_constrained_hyper_para>(&config))
67 || bool(std::get_if<NonIdealMHD>(&config));
68 return is_psi;
69 }
70
71 inline bool has_divB_field() {
72 bool is_divB = bool(std::get_if<IdealMHD_constrained_hyper_para>(&config));
73 return is_divB;
74 }
75
76 inline bool has_curlB_field() {
77 bool is_curlB = bool(std::get_if<NonIdealMHD>(&config));
78 return is_curlB;
79 }
80
81 inline bool has_dtdivB_field() {
82 bool is_dtdivB = bool(std::get_if<NonIdealMHD>(&config));
83 return is_dtdivB;
84 }
85
86 inline void print_status() {
87 logger::raw_ln("--- MHD config");
88
89 if (None *v = std::get_if<None>(&config)) {
90 logger::raw_ln(" Config MHD Type : None (No MHD)");
91 } else if (
92 IdealMHD_constrained_hyper_para *v
93 = std::get_if<IdealMHD_constrained_hyper_para>(&config)) {
94 logger::raw_ln(" Config MHD : Ideal MHD, constrained hyperbolic/parabolic treatment");
95 logger::raw_ln(" sigma_mhd =", v->sigma_mhd);
96 } else if (NonIdealMHD *v = std::get_if<NonIdealMHD>(&config)) {
97 logger::raw_ln(" Config MHD Type : Non Ideal MHD");
98 logger::raw_ln(" sigma_mhd =", v->sigma_mhd);
99 } else {
101 }
102
103 logger::raw_ln("--- MHD config (deduced)");
104
105 logger::raw_ln("-------------");
106 }
107};
108
109namespace shammodels::sph {
110
117 template<class Tvec>
118 inline void to_json(nlohmann::json &j, const MHDConfig<Tvec> &p) {
119 using T = MHDConfig<Tvec>;
120
121 using None = typename T::None;
122 using IMHD = typename T::IdealMHD_constrained_hyper_para;
123 using NonIdealMHD = typename T::NonIdealMHD;
124
125 // Write the config type into the JSON object
126 if (const None *v = std::get_if<None>(&p.config)) {
127 j = {
128 {"mhd_type", "none"},
129 };
130 } else if (const IMHD *v = std::get_if<IMHD>(&p.config)) {
131 j = {
132 {"mhd_type", "ideal_mhd_constrained_hyper_para"},
133 {"sigma_mhd", v->sigma_mhd},
134 {"alpha_u", v->alpha_u},
135 };
136 } else if (const NonIdealMHD *v = std::get_if<NonIdealMHD>(&p.config)) {
137 // Write the shear base, direction, and speed into the JSON object
138 j = {
139 {"mhd_type", "non_ideal_mhd"},
140 {"sigma_mhd", v->sigma_mhd},
141 {"alpha_u", v->alpha_u},
142 };
143 } else {
145 }
146 }
147
154 template<class Tvec>
155 inline void from_json(const nlohmann::json &j, MHDConfig<Tvec> &p) {
156 using T = MHDConfig<Tvec>;
157
158 using Tscal = shambase::VecComponent<Tvec>;
159
160 // Check if the JSON object contains the "mhd_type" field
161 if (!j.contains("mhd_type")) {
162 shambase::throw_with_loc<std::runtime_error>("no field mhd_type is found in this json");
163 }
164
165 // Read the config type from the JSON object
166 std::string mhd_type;
167 j.at("mhd_type").get_to(mhd_type);
168
169 using None = typename T::None;
170 using IMHD = typename T::IdealMHD_constrained_hyper_para;
171 using NonIdealMHD = typename T::NonIdealMHD;
172
173 // Set the BCConfig based on the config type
174 if (mhd_type == "none") {
175 p.set(None{});
176 } else if (mhd_type == "ideal_mhd_constrained_hyper_para") {
177 p.set(
178 IMHD{
179 j.at("sigma_mhd").get<Tscal>(),
180 j.at("alpha_u").get<Tscal>(),
181 });
182 } else if (mhd_type == "non_ideal_mhd") {
183 p.set(
184 NonIdealMHD{
185 j.at("sigma_mhd").get<Tscal>(),
186 j.at("alpha_u").get<Tscal>(),
187 });
188 } else {
190 }
191 }
192
193} // 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
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.