Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
RiemannConfig.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
27
29#include "shambackends/vec.hpp"
31#include <nlohmann/json.hpp>
32#include <variant>
33
34namespace shammodels::gsph {
35
47 template<class Tvec>
48 struct RiemannConfig;
49
50} // namespace shammodels::gsph
51
52template<class Tvec>
54
55 using Tscal = shambase::VecComponent<Tvec>;
56 static constexpr u32 dim = shambase::VectorProperties<Tvec>::dimension;
57
65 struct Iterative {
66 Tscal tol = Tscal{1.0e-6};
68 };
69
77 struct Exact {
78 Tscal tol = Tscal{1.0e-8};
79 u32 max_iter = 100;
80 };
81
89 struct HLLC {};
90
98 struct Roe {
99 Tscal entropy_fix = Tscal{0.1};
100 };
101
102 using Variant = std::variant<Iterative, Exact, HLLC, Roe>;
103
104 Variant config = Iterative{};
105
106 void set(Variant v) { config = v; }
107
108 void set_iterative(Tscal tol = Tscal{1.0e-6}, u32 max_iter = 20) {
109 set(Iterative{tol, max_iter});
110 }
111
112 void set_exact(Tscal tol = Tscal{1.0e-8}, u32 max_iter = 100) { set(Exact{tol, max_iter}); }
113
114 void set_hllc() { set(HLLC{}); }
115
116 void set_roe(Tscal entropy_fix = Tscal{0.1}) { set(Roe{entropy_fix}); }
117
118 inline bool is_iterative() const { return std::holds_alternative<Iterative>(config); }
119 inline bool is_exact() const { return std::holds_alternative<Exact>(config); }
120 inline bool is_hllc() const { return std::holds_alternative<HLLC>(config); }
121 inline bool is_roe() const { return std::holds_alternative<Roe>(config); }
122
123 inline void print_status() const {
124 logger::raw_ln("--- Riemann solver config");
125
126 if (const Iterative *v = std::get_if<Iterative>(&config)) {
127 logger::raw_ln(" Type : Iterative (van Leer 1997)");
128 logger::raw_ln(" tol =", v->tol);
129 logger::raw_ln(" max_iter =", v->max_iter);
130 } else if (const Exact *v = std::get_if<Exact>(&config)) {
131 logger::raw_ln(" Type : Exact (Toro)");
132 logger::raw_ln(" tol =", v->tol);
133 logger::raw_ln(" max_iter =", v->max_iter);
134 } else if (std::get_if<HLLC>(&config)) {
135 logger::raw_ln(" Type : HLLC");
136 } else if (const Roe *v = std::get_if<Roe>(&config)) {
137 logger::raw_ln(" Type : Roe");
138 logger::raw_ln(" entropy_fix =", v->entropy_fix);
139 } else {
141 }
142
143 logger::raw_ln("-------------");
144 }
145};
146
147namespace shammodels::gsph {
148
149 template<class Tvec>
150 inline void to_json(nlohmann::json &j, const RiemannConfig<Tvec> &p) {
151 using T = RiemannConfig<Tvec>;
152 using Iterative = typename T::Iterative;
153 using Exact = typename T::Exact;
154 using HLLC = typename T::HLLC;
155 using Roe = typename T::Roe;
156
157 if (const Iterative *v = std::get_if<Iterative>(&p.config)) {
158 j = {
159 {"riemann_type", "iterative"},
160 {"tol", v->tol},
161 {"max_iter", v->max_iter},
162 };
163 } else if (const Exact *v = std::get_if<Exact>(&p.config)) {
164 j = {
165 {"riemann_type", "exact"},
166 {"tol", v->tol},
167 {"max_iter", v->max_iter},
168 };
169 } else if (std::get_if<HLLC>(&p.config)) {
170 j = {
171 {"riemann_type", "hllc"},
172 };
173 } else if (const Roe *v = std::get_if<Roe>(&p.config)) {
174 j = {
175 {"riemann_type", "roe"},
176 {"entropy_fix", v->entropy_fix},
177 };
178 } else {
180 }
181 }
182
183 template<class Tvec>
184 inline void from_json(const nlohmann::json &j, RiemannConfig<Tvec> &p) {
185 using T = RiemannConfig<Tvec>;
186 using Tscal = shambase::VecComponent<Tvec>;
187
188 if (!j.contains("riemann_type")) {
190 "no field riemann_type is found in this json");
191 }
192
193 std::string riemann_type;
194 j.at("riemann_type").get_to(riemann_type);
195
196 using Iterative = typename T::Iterative;
197 using Exact = typename T::Exact;
198 using HLLC = typename T::HLLC;
199 using Roe = typename T::Roe;
200
201 if (riemann_type == "iterative") {
202 p.set(Iterative{j.at("tol").get<Tscal>(), j.at("max_iter").get<u32>()});
203 } else if (riemann_type == "exact") {
204 // max_iter is read with a fallback default so configs saved before
205 // this field existed (tol-only) still load instead of throwing.
206 p.set(Exact{j.at("tol").get<Tscal>(), j.value("max_iter", Exact{}.max_iter)});
207 } else if (riemann_type == "hllc") {
208 p.set(HLLC{});
209 } else if (riemann_type == "roe") {
210 p.set(Roe{j.at("entropy_fix").get<Tscal>()});
211 } else {
212 shambase::throw_unimplemented("Unknown Riemann solver type: " + riemann_type);
213 }
214 }
215
216} // 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.
Contains traits and utilities for backend related types.
Tscal tol
Convergence tolerance.
u32 max_iter
Maximum bisection iterations.
HLLC approximate Riemann solver.
van Leer (1997) iterative Riemann solver
Roe linearized Riemann solver.
Tscal entropy_fix
Entropy fix parameter.
Configuration for Riemann solvers in GSPH.