Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
CoolingConfig.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
17
20#include "shambase/string.hpp"
21#include "shambackends/vec.hpp"
23#include <nlohmann/json.hpp>
24#include <stdexcept>
25#include <string>
26#include <variant>
27
29
33 template<class Tvec>
35 using Tscal = shambase::VecComponent<Tvec>;
36
38 struct None {};
39
50 Tscal rho0;
51 Tscal cs0;
52 Tscal gamma0 = 5. / 3.;
53
54 inline shammath::FluidStateBarotropic<Tvec> get_barotrope() const {
55 return shammath::FluidStateBarotropic<Tvec>(rho0, cs0, gamma0);
56 }
57 };
58
59 using Variant = std::variant<None, InstantBarotropic>;
60
61 Variant config = None{};
62
63 inline void set_none() { config = None{}; }
64 inline void set_instant_barotropic(Tscal rho0, Tscal cs0, Tscal gamma0) {
65 config = InstantBarotropic{rho0, cs0, gamma0};
66 }
67
68 inline bool is_cooling_on() const { return !std::holds_alternative<None>(config); }
69
71 inline void check_config() const {
72 if (const InstantBarotropic *cfg = std::get_if<InstantBarotropic>(&config)) {
73 if (!(cfg->rho0 > 0.0) || !(cfg->cs0 > 0.0)) {
75 "InstantBarotropic cooling: rho0 and cs0 must be > 0, currently rho0 = "
76 "{}, cs0 = {}",
77 cfg->rho0,
78 cfg->cs0));
79 }
80 }
81 }
82 };
83
84 template<class Tvec>
85 inline void to_json(nlohmann::json &j, const CoolingConfig<Tvec> &p) {
86 using Cooling = CoolingConfig<Tvec>;
87
88 std::visit(
90 [&](const typename Cooling::None &) {
91 j = {{"type", "none"}};
92 },
93 [&](const typename Cooling::InstantBarotropic &cfg) {
94 j
95 = {{"type", "instant_barotropic"},
96 {"rho0", cfg.rho0},
97 {"cs0", cfg.cs0},
98 {"gamma0", cfg.gamma0}};
99 },
100 },
101 p.config);
102 }
103
104 template<class Tvec>
105 inline void from_json(const nlohmann::json &j, CoolingConfig<Tvec> &p) {
106 using Tscal = shambase::VecComponent<Tvec>;
107
108 const std::string type = j.at("type").get<std::string>();
109 if (type == "none") {
110 p.set_none();
111 } else if (type == "instant_barotropic") {
112 p.set_instant_barotropic(
113 j.at("rho0").get<Tscal>(), j.at("cs0").get<Tscal>(), j.at("gamma0").get<Tscal>());
114 } else {
115 shambase::throw_with_loc<std::runtime_error>("Invalid cooling type: " + type);
116 }
117 }
118
119} // namespace shammodels::basegodunov
FluidStateSpec implementation for a barotropic equation of state that is isothermal below a threshold...
This header file contains utility functions related to exception handling in the code.
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
namespace for the basegodunov model
Gas and dust conservative/primitive states and axis-transform helpers shared by every gas and dust Ri...
Build an overload set out of several callables, for use with std::visit.
Instantaneous cooling onto a barotrope (RAMSES-like).
Cooling of the gas, applied on top of the (adiabatic) equation of state.
void check_config() const
Throws std::invalid_argument if the parameters of the cooling are invalid.