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
20#include "shambackends/math.hpp"
22#include "shambackends/vec.hpp"
24#include <nlohmann/json.hpp>
25#include <type_traits>
26#include <string>
27#include <variant>
28
29namespace shammodels {
30
31 template<class Tvec>
33 using Tscal = shambase::VecComponent<Tvec>;
35 struct PointMass {
36 Tscal central_mass;
37 Tscal Racc;
38 };
39
41 Tscal central_mass;
42 Tscal Racc;
43 Tscal a_spin;
44 Tvec dir_spin;
45 };
46
55 i32_3 shear_base = {1, 0, 0};
56 i32_3 shear_dir = {0, 1, 0};
57
58 Tscal Omega_0;
59 Tscal eta;
60 Tscal q;
61
62 inline Tscal shear_speed(Tscal box_length) { return q * Omega_0 * box_length; }
63
64 ShearingBoxForce() = default;
65 ShearingBoxForce(Tscal Omega_0, Tscal eta, Tscal q)
66 : Omega_0(Omega_0), eta(eta), q(q) {};
67 ShearingBoxForce(i32_3 shear_base, i32_3 shear_dir, Tscal Omega_0, Tscal eta, Tscal q)
68 : shear_base(shear_base), shear_dir(shear_dir), Omega_0(Omega_0), eta(eta), q(q) {};
69 };
70
73 Tscal central_mass;
74 Tscal R0;
75 };
76
79 Tscal eta;
80 };
81
82 using VariantForce = std::variant<
88 VariantForce val;
89 };
90
91 template<class Tvec>
93
94 using Tscal = shambase::VecComponent<Tvec>;
96
97 using PointMass = typename ExtForceVariant<Tvec>::PointMass;
98 using LenseThirring = typename ExtForceVariant<Tvec>::LenseThirring;
99 using ShearingBoxForce = typename ExtForceVariant<Tvec>::ShearingBoxForce;
100 using VerticalDiscPotential = typename ExtForceVariant<Tvec>::VerticalDiscPotential;
101 using VelocityDissipation = typename ExtForceVariant<Tvec>::VelocityDissipation;
102
103 std::vector<ExtForceVariant<Tvec>> ext_forces;
104
105 inline void add_point_mass(Tscal central_mass, Tscal Racc) {
106 ext_forces.push_back(ExtForceVariant<Tvec>{PointMass{central_mass, Racc}});
107 }
108
109 inline void add_lense_thirring(
110 Tscal central_mass, Tscal Racc, Tscal a_spin, Tvec dir_spin) {
111 if (sham::abs(sycl::length(dir_spin) - 1) > 1e-8) {
113 "the sping direction should be a unit vector");
114 }
115 ext_forces.push_back(
116 ExtForceVariant<Tvec>{LenseThirring{central_mass, Racc, a_spin, dir_spin}});
117 }
118
123 inline void add_shearing_box(Tscal Omega_0, Tscal eta, Tscal q) {
124
125 ext_forces.push_back(ExtForceVariant<Tvec>{ShearingBoxForce{Omega_0, eta, q}});
126 }
127
128 inline void add_vertical_disc_potential(Tscal central_mass, Tscal R0) {
129 ext_forces.push_back(ExtForceVariant<Tvec>{VerticalDiscPotential{central_mass, R0}});
130 }
131
132 inline void add_velocity_dissipation(Tscal eta) {
133 ext_forces.push_back(ExtForceVariant<Tvec>{VelocityDissipation{eta}});
134 }
135 };
136
137} // namespace shammodels
138
139namespace shammodels {
140 template<class Tvec>
141 inline void to_json(nlohmann::json &j, const ExtForceVariant<Tvec> &p) {
142 using T = ExtForceVariant<Tvec>;
143
144 using PointMass = typename T::PointMass;
145 using LenseThirring = typename T::LenseThirring;
146 using ShearingBoxForce = typename T::ShearingBoxForce;
147 using VerticalDiscPotential = typename T::VerticalDiscPotential;
148 using VelocityDissipation = typename T::VelocityDissipation;
149
150 if (const PointMass *v = std::get_if<PointMass>(&p.val)) {
151 j = {
152 {"force_type", "point_mass"}, {"central_mass", v->central_mass}, {"Racc", v->Racc}};
153 } else if (const LenseThirring *v = std::get_if<LenseThirring>(&p.val)) {
154 j = {
155 {"force_type", "lense_thirring"},
156 {"central_mass", v->central_mass},
157 {"Racc", v->Racc},
158 {"a_spin", v->a_spin},
159 {"dir_spin", v->dir_spin},
160 };
161 } else if (const ShearingBoxForce *v = std::get_if<ShearingBoxForce>(&p.val)) {
162 j = {
163 {"force_type", "shearing_box_force"},
164 {"shear_base", v->shear_base},
165 {"shear_dir", v->shear_dir},
166 {"Omega_0", v->Omega_0},
167 {"eta", v->eta},
168 {"q", v->q},
169 };
170 } else if (const VerticalDiscPotential *v = std::get_if<VerticalDiscPotential>(&p.val)) {
171 j
172 = {{"force_type", "vertical_disc_potential"},
173 {"central_mass", v->central_mass},
174 {"R0", v->R0}};
175 } else if (const VelocityDissipation *v = std::get_if<VelocityDissipation>(&p.val)) {
176 j = {{"force_type", "velocity_dissipation"}, {"eta", v->eta}};
177 } else {
179 }
180 }
181
182 template<class Tvec>
183 inline void from_json(const nlohmann::json &j, ExtForceVariant<Tvec> &p) {
184 using Tscal = shambase::VecComponent<Tvec>;
185 using T = ExtForceVariant<Tvec>;
186
187 if (!j.contains("force_type")) {
188 shambase::throw_with_loc<std::runtime_error>("no field eos_type is found in this json");
189 }
190
191 std::string force_type;
192 j.at("force_type").get_to(force_type);
193
194 using PointMass = typename T::PointMass;
195 using LenseThirring = typename T::LenseThirring;
196 using ShearingBoxForce = typename T::ShearingBoxForce;
197 using VerticalDiscPotential = typename T::VerticalDiscPotential;
198 using VelocityDissipation = typename T::VelocityDissipation;
199
200 if (force_type == "point_mass") {
201 p.val = PointMass{
202 j.at("central_mass").get<Tscal>(),
203 j.at("Racc").get<Tscal>(),
204 };
205 } else if (force_type == "lense_thirring") {
206 p.val = LenseThirring{
207 j.at("central_mass").get<Tscal>(),
208 j.at("Racc").get<Tscal>(),
209 j.at("a_spin").get<Tscal>(),
210 j.at("dir_spin").get<Tvec>(),
211 };
212 } else if (force_type == "shearing_box_force") {
213 p.val = ShearingBoxForce{
214 j.at("shear_base").get<i32_3>(),
215 j.at("shear_dir").get<i32_3>(),
216 j.at("Omega_0").get<Tscal>(),
217 j.at("eta").get<Tscal>(),
218 j.at("q").get<Tscal>(),
219 };
220 } else if (force_type == "vertical_disc_potential") {
221 p.val = VerticalDiscPotential{
222 j.at("central_mass").get<Tscal>(),
223 j.at("R0").get<Tscal>(),
224 };
225 } else if (force_type == "velocity_dissipation") {
226 p.val = VelocityDissipation{j.at("eta").get<Tscal>()};
227 } else {
229 }
230 }
231
232 template<class Tvec>
233 inline void to_json(nlohmann::json &j, const ExtForceConfig<Tvec> &p) {
234 using T = ExtForceConfig<Tvec>;
235
236 j = {{"force_list", p.ext_forces}};
237 }
238
239 template<class Tvec>
240 inline void from_json(const nlohmann::json &j, ExtForceConfig<Tvec> &p) {
241 using T = ExtForceConfig<Tvec>;
242
243 j.at("force_list").get_to(p.ext_forces);
244 }
245} // 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 .
Contains functions for converting between SYCL vector types and C++ standard library array types.