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 };
80
88 struct HLLC {};
89
97 struct Roe {
98 Tscal entropy_fix = Tscal{0.1};
99 };
100
101 using Variant = std::variant<Iterative, Exact, HLLC, Roe>;
102
103 Variant config = Iterative{};
104
105 void set(Variant v) { config = v; }
106
107 void set_iterative(Tscal tol = Tscal{1.0e-6}, u32 max_iter = 20) {
108 set(Iterative{tol, max_iter});
109 }
110
111 void set_exact(Tscal tol = Tscal{1.0e-8}) { set(Exact{tol}); }
112
113 void set_hllc() { set(HLLC{}); }
114
115 void set_roe(Tscal entropy_fix = Tscal{0.1}) { set(Roe{entropy_fix}); }
116
117 inline bool is_iterative() const { return std::holds_alternative<Iterative>(config); }
118 inline bool is_exact() const { return std::holds_alternative<Exact>(config); }
119 inline bool is_hllc() const { return std::holds_alternative<HLLC>(config); }
120 inline bool is_roe() const { return std::holds_alternative<Roe>(config); }
121
122 inline void print_status() const {
123 logger::raw_ln("--- Riemann solver config");
124
125 if (const Iterative *v = std::get_if<Iterative>(&config)) {
126 logger::raw_ln(" Type : Iterative (van Leer 1997)");
127 logger::raw_ln(" tol =", v->tol);
128 logger::raw_ln(" max_iter =", v->max_iter);
129 } else if (const Exact *v = std::get_if<Exact>(&config)) {
130 logger::raw_ln(" Type : Exact (Toro)");
131 logger::raw_ln(" tol =", v->tol);
132 } else if (std::get_if<HLLC>(&config)) {
133 logger::raw_ln(" Type : HLLC");
134 } else if (const Roe *v = std::get_if<Roe>(&config)) {
135 logger::raw_ln(" Type : Roe");
136 logger::raw_ln(" entropy_fix =", v->entropy_fix);
137 } else {
139 }
140
141 logger::raw_ln("-------------");
142 }
143};
144
145namespace shammodels::gsph {
146
147 template<class Tvec>
148 inline void to_json(nlohmann::json &j, const RiemannConfig<Tvec> &p) {
149 using T = RiemannConfig<Tvec>;
150 using Iterative = typename T::Iterative;
151 using Exact = typename T::Exact;
152 using HLLC = typename T::HLLC;
153 using Roe = typename T::Roe;
154
155 if (const Iterative *v = std::get_if<Iterative>(&p.config)) {
156 j = {
157 {"riemann_type", "iterative"},
158 {"tol", v->tol},
159 {"max_iter", v->max_iter},
160 };
161 } else if (const Exact *v = std::get_if<Exact>(&p.config)) {
162 j = {
163 {"riemann_type", "exact"},
164 {"tol", v->tol},
165 };
166 } else if (std::get_if<HLLC>(&p.config)) {
167 j = {
168 {"riemann_type", "hllc"},
169 };
170 } else if (const Roe *v = std::get_if<Roe>(&p.config)) {
171 j = {
172 {"riemann_type", "roe"},
173 {"entropy_fix", v->entropy_fix},
174 };
175 } else {
177 }
178 }
179
180 template<class Tvec>
181 inline void from_json(const nlohmann::json &j, RiemannConfig<Tvec> &p) {
182 using T = RiemannConfig<Tvec>;
183 using Tscal = shambase::VecComponent<Tvec>;
184
185 if (!j.contains("riemann_type")) {
187 "no field riemann_type is found in this json");
188 }
189
190 std::string riemann_type;
191 j.at("riemann_type").get_to(riemann_type);
192
193 using Iterative = typename T::Iterative;
194 using Exact = typename T::Exact;
195 using HLLC = typename T::HLLC;
196 using Roe = typename T::Roe;
197
198 if (riemann_type == "iterative") {
199 p.set(Iterative{j.at("tol").get<Tscal>(), j.at("max_iter").get<u32>()});
200 } else if (riemann_type == "exact") {
201 p.set(Exact{j.at("tol").get<Tscal>()});
202 } else if (riemann_type == "hllc") {
203 p.set(HLLC{});
204 } else if (riemann_type == "roe") {
205 p.set(Roe{j.at("entropy_fix").get<Tscal>()});
206 } else {
207 shambase::throw_unimplemented("Unknown Riemann solver type: " + riemann_type);
208 }
209 }
210
211} // 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.
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.