Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
ReconstructConfig.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 "shambackends/vec.hpp"
27#include <nlohmann/json.hpp>
28#include <variant>
29
30namespace shammodels::gsph {
31
41 template<class Tvec>
42 struct ReconstructConfig;
43
44} // namespace shammodels::gsph
45
46template<class Tvec>
48
49 using Tscal = shambase::VecComponent<Tvec>;
51
55 enum class Limiter {
56 VanLeer,
57 Minmod,
58 Superbee,
59 MC
60 };
61
69
80
81 using Variant = std::variant<PiecewiseConstant, MUSCL>;
82
83 Variant config = PiecewiseConstant{};
84
85 void set(Variant v) { config = v; }
86
87 void set_piecewise_constant() { set(PiecewiseConstant{}); }
88
89 void set_muscl(Limiter limiter = Limiter::VanLeer) { set(MUSCL{limiter}); }
90
91 inline bool is_piecewise_constant() const {
92 return std::holds_alternative<PiecewiseConstant>(config);
93 }
94
95 inline bool is_muscl() const { return std::holds_alternative<MUSCL>(config); }
96
97 inline bool requires_gradients() const { return is_muscl(); }
98
99 inline void print_status() const {
100 logger::raw_ln("--- Reconstruction config");
101
102 if (std::get_if<PiecewiseConstant>(&config)) {
103 logger::raw_ln(" Type : PiecewiseConstant (1st order)");
104 } else if (const MUSCL *v = std::get_if<MUSCL>(&config)) {
105 logger::raw_ln(" Type : MUSCL (2nd order)");
106 switch (v->limiter) {
107 case Limiter::VanLeer : logger::raw_ln(" Limiter : VanLeer"); break;
108 case Limiter::Minmod : logger::raw_ln(" Limiter : Minmod"); break;
109 case Limiter::Superbee: logger::raw_ln(" Limiter : Superbee"); break;
110 case Limiter::MC : logger::raw_ln(" Limiter : MC"); break;
111 }
112 } else {
114 }
115
116 logger::raw_ln("-------------");
117 }
118};
119
120namespace shammodels::gsph {
121
122 template<class Tvec>
123 inline void to_json(nlohmann::json &j, const ReconstructConfig<Tvec> &p) {
124 using T = ReconstructConfig<Tvec>;
125 using PiecewiseConstant = typename T::PiecewiseConstant;
126 using MUSCL = typename T::MUSCL;
127 using Limiter = typename T::Limiter;
128
129 if (std::get_if<PiecewiseConstant>(&p.config)) {
130 j = {
131 {"reconstruct_type", "piecewise_constant"},
132 };
133 } else if (const MUSCL *v = std::get_if<MUSCL>(&p.config)) {
134 std::string limiter_str;
135 switch (v->limiter) {
136 case Limiter::VanLeer : limiter_str = "vanleer"; break;
137 case Limiter::Minmod : limiter_str = "minmod"; break;
138 case Limiter::Superbee: limiter_str = "superbee"; break;
139 case Limiter::MC : limiter_str = "mc"; break;
140 }
141 j = {
142 {"reconstruct_type", "muscl"},
143 {"limiter", limiter_str},
144 };
145 } else {
147 }
148 }
149
150 template<class Tvec>
151 inline void from_json(const nlohmann::json &j, ReconstructConfig<Tvec> &p) {
152 using T = ReconstructConfig<Tvec>;
153 using PiecewiseConstant = typename T::PiecewiseConstant;
154 using MUSCL = typename T::MUSCL;
155 using Limiter = typename T::Limiter;
156
157 if (!j.contains("reconstruct_type")) {
159 "no field reconstruct_type is found in this json");
160 }
161
162 std::string reconstruct_type;
163 j.at("reconstruct_type").get_to(reconstruct_type);
164
165 if (reconstruct_type == "piecewise_constant") {
166 p.set(PiecewiseConstant{});
167 } else if (reconstruct_type == "muscl") {
168 std::string limiter_str;
169 j.at("limiter").get_to(limiter_str);
170
171 Limiter limiter;
172 if (limiter_str == "vanleer") {
173 limiter = Limiter::VanLeer;
174 } else if (limiter_str == "minmod") {
175 limiter = Limiter::Minmod;
176 } else if (limiter_str == "superbee") {
177 limiter = Limiter::Superbee;
178 } else if (limiter_str == "mc") {
179 limiter = Limiter::MC;
180 } else {
181 shambase::throw_unimplemented("Unknown limiter type: " + limiter_str);
182 }
183
184 p.set(MUSCL{limiter});
185 } else {
186 shambase::throw_unimplemented("Unknown reconstruction type: " + reconstruct_type);
187 }
188 }
189
190} // namespace shammodels::gsph
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.
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.
Contains traits and utilities for backend related types.
Configuration for reconstruction methods in GSPH.
Limiter
Slope limiter types for MUSCL reconstruction.
@ Superbee
Superbee limiter (least diffusive)
@ Minmod
Minmod limiter (most diffusive)
@ MC
Monotonized Central limiter.