Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
ExtForceConfig.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
19
21#include "shambackends/math.hpp"
23#include "shambackends/vec.hpp"
25#include <nlohmann/json.hpp>
26#include <type_traits>
27#include <string>
28#include <variant>
29
30namespace shammodels {
31
32 template<class Tvec>
34 using Tscal = shambase::VecComponent<Tvec>;
35 static constexpr u32 dim = shambase::VectorProperties<Tvec>::dimension;
36 struct PointMass {
37 Tscal central_mass;
38 Tscal Racc;
39 };
40
41 struct PN_PW {
42 Tscal central_mass;
43 Tvec central_pos;
44 Tscal Racc;
45 };
46
48 Tscal central_mass;
49 Tscal Racc;
50 Tscal a_spin;
51 Tvec dir_spin;
52 };
53
61 struct ShearingBoxForce {
62 i32_3 shear_base = {1, 0, 0};
63 i32_3 shear_dir = {0, 1, 0};
64
65 Tscal Omega_0;
66 Tscal eta;
67 Tscal q;
68
69 inline Tscal shear_speed(Tscal box_length) { return q * Omega_0 * box_length; }
70
71 ShearingBoxForce() = default;
72 ShearingBoxForce(Tscal Omega_0, Tscal eta, Tscal q)
73 : Omega_0(Omega_0), eta(eta), q(q) {};
74 ShearingBoxForce(i32_3 shear_base, i32_3 shear_dir, Tscal Omega_0, Tscal eta, Tscal q)
75 : shear_base(shear_base), shear_dir(shear_dir), Omega_0(Omega_0), eta(eta), q(q) {};
76 };
77
80 Tscal central_mass;
81 Tscal R0;
82 };
83
86 Tscal eta;
87 };
88
89 using VariantForce = std::variant<
91 PN_PW,
96 VariantForce val;
97 };
98
99 template<class Tvec>
101
102 using Tscal = shambase::VecComponent<Tvec>;
103 static constexpr u32 dim = shambase::VectorProperties<Tvec>::dimension;
104
105 using PointMass = typename ExtForceVariant<Tvec>::PointMass;
106 using PN_PW = typename ExtForceVariant<Tvec>::PN_PW;
107 using LenseThirring = typename ExtForceVariant<Tvec>::LenseThirring;
108 using ShearingBoxForce = typename ExtForceVariant<Tvec>::ShearingBoxForce;
109 using VerticalDiscPotential = typename ExtForceVariant<Tvec>::VerticalDiscPotential;
110 using VelocityDissipation = typename ExtForceVariant<Tvec>::VelocityDissipation;
111
112 std::vector<ExtForceVariant<Tvec>> ext_forces;
113
114 inline void add_point_mass(Tscal central_mass, Tscal Racc) {
115 ext_forces.push_back(ExtForceVariant<Tvec>{PointMass{central_mass, Racc}});
116 }
117
118 inline void add_paczynski_wiita(Tscal central_mass, Tvec central_pos, Tscal Racc) {
119 ext_forces.push_back(ExtForceVariant<Tvec>{PN_PW{central_mass, central_pos, Racc}});
120 }
121
122 inline void add_lense_thirring(
123 Tscal central_mass, Tscal Racc, Tscal a_spin, Tvec dir_spin) {
124 if (sham::abs(sycl::length(dir_spin) - 1) > 1e-8) {
126 "the sping direction should be a unit vector");
127 }
128 ext_forces.push_back(
129 ExtForceVariant<Tvec>{LenseThirring{central_mass, Racc, a_spin, dir_spin}});
130 }
131
136 inline void add_shearing_box(Tscal Omega_0, Tscal eta, Tscal q) {
137
138 ext_forces.push_back(ExtForceVariant<Tvec>{ShearingBoxForce{Omega_0, eta, q}});
139 }
140
141 inline void add_vertical_disc_potential(Tscal central_mass, Tscal R0) {
142 ext_forces.push_back(ExtForceVariant<Tvec>{VerticalDiscPotential{central_mass, R0}});
143 }
144
145 inline void add_velocity_dissipation(Tscal eta) {
146 ext_forces.push_back(ExtForceVariant<Tvec>{VelocityDissipation{eta}});
147 }
148 };
149
150} // namespace shammodels
151
152namespace shammodels {
153 template<class Tvec>
154 inline void to_json(nlohmann::json &j, const ExtForceVariant<Tvec> &p) {
155 using T = ExtForceVariant<Tvec>;
156
157 using PointMass = typename T::PointMass;
158 using PN_PW = typename T::PN_PW;
159 using LenseThirring = typename T::LenseThirring;
160 using ShearingBoxForce = typename T::ShearingBoxForce;
161 using VerticalDiscPotential = typename T::VerticalDiscPotential;
162 using VelocityDissipation = typename T::VelocityDissipation;
163
164 if (const PointMass *v = std::get_if<PointMass>(&p.val)) {
165 j = {
166 {"force_type", "point_mass"}, {"central_mass", v->central_mass}, {"Racc", v->Racc}};
167
168 } else if (const PN_PW *v = std::get_if<PN_PW>(&p.val)) {
169 j
170 = {{"force_type", "paczynski_wiita"},
171 {"central_mass", v->central_mass},
172 {"central_pos", v->central_pos},
173 {"Racc", v->Racc}};
174 } else if (const LenseThirring *v = std::get_if<LenseThirring>(&p.val)) {
175 j = {
176 {"force_type", "lense_thirring"},
177 {"central_mass", v->central_mass},
178 {"Racc", v->Racc},
179 {"a_spin", v->a_spin},
180 {"dir_spin", v->dir_spin},
181 };
182 } else if (const ShearingBoxForce *v = std::get_if<ShearingBoxForce>(&p.val)) {
183 j = {
184 {"force_type", "shearing_box_force"},
185 {"shear_base", v->shear_base},
186 {"shear_dir", v->shear_dir},
187 {"Omega_0", v->Omega_0},
188 {"eta", v->eta},
189 {"q", v->q},
190 };
191 } else if (const VerticalDiscPotential *v = std::get_if<VerticalDiscPotential>(&p.val)) {
192 j
193 = {{"force_type", "vertical_disc_potential"},
194 {"central_mass", v->central_mass},
195 {"R0", v->R0}};
196 } else if (const VelocityDissipation *v = std::get_if<VelocityDissipation>(&p.val)) {
197 j = {{"force_type", "velocity_dissipation"}, {"eta", v->eta}};
198 } else {
200 }
201 }
202
203 template<class Tvec>
204 inline void from_json(const nlohmann::json &j, ExtForceVariant<Tvec> &p) {
205 using Tscal = shambase::VecComponent<Tvec>;
206 using T = ExtForceVariant<Tvec>;
207
208 if (!j.contains("force_type")) {
209 shambase::throw_with_loc<std::runtime_error>("no field eos_type is found in this json");
210 }
211
212 std::string force_type;
213 j.at("force_type").get_to(force_type);
214
215 using PointMass = typename T::PointMass;
216 using PN_PW = typename T::PN_PW;
217 using LenseThirring = typename T::LenseThirring;
218 using ShearingBoxForce = typename T::ShearingBoxForce;
219 using VerticalDiscPotential = typename T::VerticalDiscPotential;
220 using VelocityDissipation = typename T::VelocityDissipation;
221
222 if (force_type == "point_mass") {
223 p.val = PointMass{
224 j.at("central_mass").get<Tscal>(),
225 j.at("Racc").get<Tscal>(),
226 };
227 } else if (force_type == "paczynski_wiita") {
228 p.val = PN_PW{
229 j.at("central_mass").get<Tscal>(),
230 j.at("central_pos").get<Tvec>(),
231 j.at("Racc").get<Tscal>(),
232 };
233 } else if (force_type == "lense_thirring") {
234 p.val = LenseThirring{
235 j.at("central_mass").get<Tscal>(),
236 j.at("Racc").get<Tscal>(),
237 j.at("a_spin").get<Tscal>(),
238 j.at("dir_spin").get<Tvec>(),
239 };
240 } else if (force_type == "shearing_box_force") {
241 p.val = ShearingBoxForce{
242 j.at("shear_base").get<i32_3>(),
243 j.at("shear_dir").get<i32_3>(),
244 j.at("Omega_0").get<Tscal>(),
245 j.at("eta").get<Tscal>(),
246 j.at("q").get<Tscal>(),
247 };
248 } else if (force_type == "vertical_disc_potential") {
249 p.val = VerticalDiscPotential{
250 j.at("central_mass").get<Tscal>(),
251 j.at("R0").get<Tscal>(),
252 };
253 } else if (force_type == "velocity_dissipation") {
254 p.val = VelocityDissipation{j.at("eta").get<Tscal>()};
255 } else {
257 }
258 }
259
260 template<class Tvec>
261 inline void to_json(nlohmann::json &j, const ExtForceConfig<Tvec> &p) {
262 using T = ExtForceConfig<Tvec>;
263
264 j = {{"force_list", p.ext_forces}};
265 }
266
267 template<class Tvec>
268 inline void from_json(const nlohmann::json &j, ExtForceConfig<Tvec> &p) {
269 using T = ExtForceConfig<Tvec>;
270
271 j.at("force_list").get_to(p.ext_forces);
272 }
273} // namespace shammodels
std::uint32_t u32
32 bit unsigned integer
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.
void throw_unimplemented(SourceLocation loc=SourceLocation{})
Throw a std::runtime_error saying that the function is unimplemented.
namespace for models
Definition AMRBlock.hpp:26
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.
void add_shearing_box(Tscal Omega_0, Tscal eta, Tscal q)
Shearing box forces as in athena stone2010_shear_box.
Contains functions for converting between SYCL vector types and C++ standard library array types.