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
23#include "shambackends/vec.hpp"
25#include <nlohmann/json.hpp>
26#include <algorithm>
27#include <cmath>
28#include <stdexcept>
29#include <string>
30#include <variant>
31#include <vector>
32
34
41 template<class Tvec>
43 using Tscal = shambase::VecComponent<Tvec>;
44
47 Tvec g;
48 };
49
50 // Point mass gravity
52 Tscal mass = 1;
53 Tvec pos = Tvec(0.5, 0.5, 0.5);
54 Tscal epsilon_soft = 0.05;
55 };
56
57 using Variant = std::variant<ConstantGravity, PointMassGravity>;
58
59 std::vector<Variant> ext_forces;
60
63
64 inline void set_coupling(ExtForceCoupling mode) { coupling = mode; }
65
66 inline void add_constant_gravity(Tvec g) { ext_forces.push_back(ConstantGravity{g}); }
67 inline void add_point_mass_gravity(Tscal mass, Tvec pos, Tscal epsilon_soft) {
68 if (!(std::isfinite(epsilon_soft) && epsilon_soft > 0)) {
70 "point mass gravity: epsilon_soft must be finite and > 0");
71 }
72 ext_forces.push_back(PointMassGravity{mass, pos, epsilon_soft});
73 }
74
75 inline bool is_coordinates_needed() const {
76 return std::any_of(ext_forces.begin(), ext_forces.end(), [](const Variant &f) {
77 return std::holds_alternative<PointMassGravity>(f);
78 });
79 }
80
81 inline bool has_ext_forces() const { return !ext_forces.empty(); }
82 };
83
84 template<class Tvec>
85 inline void to_json(nlohmann::json &j, const ExtForceConfig<Tvec> &p) {
86 using EF = ExtForceConfig<Tvec>;
87
88 nlohmann::json force_list = nlohmann::json::array();
89 for (const auto &var_force : p.ext_forces) {
90 std::visit(
92 [&](const typename EF::ConstantGravity &cfg) {
93 force_list.push_back(
94 nlohmann::json{{"force_type", "constant_gravity"}, {"g", cfg.g}});
95 },
96 [&](const typename EF::PointMassGravity &cfg) {
97 force_list.push_back(
98 nlohmann::json{
99 {"force_type", "point_mass_gravity"},
100 {"mass", cfg.mass},
101 {"pos", cfg.pos},
102 {"epsilon_soft", cfg.epsilon_soft}});
103 },
104 },
105 var_force);
106 }
107
108 j = nlohmann::json{{"force_list", force_list}, {"coupling", p.coupling}};
109 }
110
111 template<class Tvec>
112 inline void from_json(const nlohmann::json &j, ExtForceConfig<Tvec> &p) {
113 p.ext_forces.clear();
114
115 // configs dumped before the coupling was configurable used the midpoint rule
116 p.coupling = ExtForceCoupling::Midpoint;
117 if (j.contains("coupling")) {
118 j.at("coupling").get_to(p.coupling);
119 }
120
121 for (const auto &jforce : j.at("force_list")) {
122 const std::string force_type = jforce.at("force_type").get<std::string>();
123 using Tscal = shambase::VecComponent<Tvec>;
124 if (force_type == "constant_gravity") {
125 p.add_constant_gravity(jforce.at("g").get<Tvec>());
126 } else if (force_type == "point_mass_gravity") {
127 p.add_point_mass_gravity(
128 jforce.at("mass").get<Tscal>(),
129 jforce.at("pos").get<Tvec>(),
130 jforce.at("epsilon_soft").get<Tscal>());
131 } else {
133 "Invalid external force type: " + force_type);
134 }
135 }
136 }
137
138} // namespace shammodels::basegodunov
External forces time coupling enum + json serialization/deserialization.
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.
overloaded(Ts...) -> overloaded< Ts... >
Deduction guide so overloaded{lambda1, lambda2, ...} deduces Ts... from the lambdas.
namespace for the basegodunov model
ExtForceCoupling
Time quadrature used to couple the external forces source term to the solver.
List of the external forces applied to the gas.
ExtForceCoupling coupling
Time quadrature of the external forces source term.
Contains functions for converting between SYCL vector types and C++ standard library array types.