Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
sink_edges_helper.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
18
21#include "shambackends/vec.hpp"
25#include <vector>
26
27namespace shambase {
28 template<class T>
29 struct TypeNameInfo<std::vector<T>> {
30 inline static const std::string name = "std::vector<" + get_type_name<T>() + ">";
31 };
32} // namespace shambase
33
34namespace shammodels::sph {
35
36 template<class Tvec>
37 struct SinkEdges {
38 using Tscal = shambase::VecComponent<Tvec>;
39
40 std::vector<Tvec> &pos;
41 std::vector<Tvec> &vel;
42 std::vector<Tvec> &acc_sph;
43 std::vector<Tvec> &acc_ext;
44 std::vector<Tscal> &mass;
45 std::vector<Tvec> &angular_momentum;
46 std::vector<Tscal> &accretion_radius;
47
48 inline bool has_sinks() const { return !pos.empty(); }
49 inline size_t size() const { return pos.size(); }
50 };
51
52 namespace details {
53
54 template<class T>
55 inline void ensure_sync_data_edge(
57 const std::string &name,
58 const std::string &tex_symbol,
59 T init_value) {
60
61 if (!sync_data.has_edge(name)) {
62 auto edge = sync_data.register_edge(
64 edge->data = std::move(init_value);
65 }
66 }
67
68 template<class T>
69 inline T &get_sync_data(
70 shamrock::solvergraph::SolverGraphSerializable &sync, const std::string &name) {
71 return sync.template get_edge_ref<shamrock::solvergraph::IDataEdgeSerializable<T>>(name)
72 .data;
73 }
74
75 } // namespace details
76
80 template<class Tvec>
82 using Tscal = shambase::VecComponent<Tvec>;
83
84 details::ensure_sync_data_edge<std::vector<Tvec>>(
85 sync, "sink_pos", "\\bf{r}_{\\mathrm{sink}}", {});
86 details::ensure_sync_data_edge<std::vector<Tvec>>(
87 sync, "sink_vel", "\\bf{v}_{\\mathrm{sink}}", {});
88 details::ensure_sync_data_edge<std::vector<Tvec>>(
89 sync, "sink_acc_sph", "\\bf{a}_{\\mathrm{sink, sph}}", {});
90 details::ensure_sync_data_edge<std::vector<Tvec>>(
91 sync, "sink_acc_ext", "\\bf{a}_{\\mathrm{sink, ext}}", {});
92 details::ensure_sync_data_edge<std::vector<Tscal>>(
93 sync, "sink_mass", "m_{\\mathrm{sink}}", {});
94 details::ensure_sync_data_edge<std::vector<Tvec>>(
95 sync, "sink_angular_momentum", "\\bf{L}_{\\mathrm{sink}}", {});
96 details::ensure_sync_data_edge<std::vector<Tscal>>(
97 sync, "sink_accretion_radius", "r_{\\mathrm{accretion}}", {});
98 }
99
104 template<class Tvec>
106 return details::get_sync_data<std::vector<Tvec>>(sync, "sink_pos");
107 }
108
109 template<class Tvec>
110 inline std::vector<Tvec> &get_sink_vel(shamrock::solvergraph::SolverGraphSerializable &sync) {
111 return details::get_sync_data<std::vector<Tvec>>(sync, "sink_vel");
112 }
113
114 template<class Tvec>
115 inline std::vector<Tvec> &get_sink_acc_sph(
117 return details::get_sync_data<std::vector<Tvec>>(sync, "sink_acc_sph");
118 }
119
120 template<class Tvec>
121 inline std::vector<Tvec> &get_sink_acc_ext(
122 shamrock::solvergraph::SolverGraphSerializable &sync) {
123 return details::get_sync_data<std::vector<Tvec>>(sync, "sink_acc_ext");
124 }
125
126 template<class Tvec>
127 inline std::vector<shambase::VecComponent<Tvec>> &get_sink_mass(
128 shamrock::solvergraph::SolverGraphSerializable &sync) {
129 return details::get_sync_data<std::vector<shambase::VecComponent<Tvec>>>(sync, "sink_mass");
130 }
131
132 template<class Tvec>
133 inline std::vector<Tvec> &get_sink_angular_momentum(
134 shamrock::solvergraph::SolverGraphSerializable &sync) {
135 return details::get_sync_data<std::vector<Tvec>>(sync, "sink_angular_momentum");
136 }
137
138 template<class Tvec>
139 inline std::vector<shambase::VecComponent<Tvec>> &get_sink_accretion_radius(
140 shamrock::solvergraph::SolverGraphSerializable &sync) {
141 return details::get_sync_data<std::vector<shambase::VecComponent<Tvec>>>(
142 sync, "sink_accretion_radius");
143 }
144
151 template<class Tvec>
153 return !get_sink_pos<Tvec>(sync).empty();
154 }
155
162 template<class Tvec>
164 return SinkEdges<Tvec>{
165 get_sink_pos<Tvec>(sync),
166 get_sink_vel<Tvec>(sync),
167 get_sink_acc_sph<Tvec>(sync),
168 get_sink_acc_ext<Tvec>(sync),
169 get_sink_mass<Tvec>(sync),
170 get_sink_angular_momentum<Tvec>(sync),
171 get_sink_accretion_radius<Tvec>(sync),
172 };
173 }
174
178 template<class Tvec>
179 inline std::vector<SinkParticle<Tvec>> to_sink_particles(const SinkEdges<Tvec> &e) {
180 std::vector<SinkParticle<Tvec>> out;
181 out.reserve(e.size());
182 for (size_t i = 0; i < e.size(); i++) {
183 out.push_back(
185 e.pos[i],
186 e.vel[i],
187 e.acc_sph[i],
188 e.acc_ext[i],
189 e.mass[i],
190 e.angular_momentum[i],
191 e.accretion_radius[i],
192 });
193 }
194 return out;
195 }
196
200 template<class Tvec>
202 SinkEdges<Tvec> &e, const std::vector<SinkParticle<Tvec>> &sinks) {
203 e.pos.clear();
204 e.vel.clear();
205 e.acc_sph.clear();
206 e.acc_ext.clear();
207 e.mass.clear();
208 e.angular_momentum.clear();
209 e.accretion_radius.clear();
210
211 e.pos.reserve(sinks.size());
212 e.vel.reserve(sinks.size());
213 e.acc_sph.reserve(sinks.size());
214 e.acc_ext.reserve(sinks.size());
215 e.mass.reserve(sinks.size());
216 e.angular_momentum.reserve(sinks.size());
217 e.accretion_radius.reserve(sinks.size());
218
219 for (const auto &s : sinks) {
220 e.pos.push_back(s.pos);
221 e.vel.push_back(s.velocity);
222 e.acc_sph.push_back(s.sph_acceleration);
223 e.acc_ext.push_back(s.ext_acceleration);
224 e.mass.push_back(s.mass);
225 e.angular_momentum.push_back(s.angular_momentum);
226 e.accretion_radius.push_back(s.accretion_radius);
227 }
228 }
229
233 template<class Tvec>
234 inline void add_sink(
236 typename SinkEdges<Tvec>::Tscal mass,
237 Tvec pos,
238 Tvec velocity,
239 typename SinkEdges<Tvec>::Tscal accretion_radius) {
240
241 e.pos.push_back(pos);
242 e.vel.push_back(velocity);
243 e.acc_sph.push_back({});
244 e.acc_ext.push_back({});
245 e.mass.push_back(mass);
246 e.angular_momentum.push_back({});
247 e.accretion_radius.push_back(accretion_radius);
248 }
249
250} // namespace shammodels::sph
Declare a class to register and retrieve nodes and edges from a unique container.
bool has_edge(const std::string &name) const
Check whether an edge with the given name exists.
std::shared_ptr< T > register_edge(const std::string &name, T &&edge)
Register an edge with automatic type deduction and shared pointer creation.
Namespace for internal details of the logs module.
namespace for basic c++ utilities
namespace for the sph model
bool has_sinks(shamrock::solvergraph::SolverGraphSerializable &sync)
Check whether any sinks are present by inspecting sink_pos only.
std::vector< SinkParticle< Tvec > > to_sink_particles(const SinkEdges< Tvec > &e)
Build an AoS sink list from the current SoA edges (Python API / dump helpers).
std::vector< Tvec > & get_sink_pos(shamrock::solvergraph::SolverGraphSerializable &sync)
Named SoA getters (edges must already exist; call ensure_sink_edges first). Prefer these when a funct...
void ensure_sink_edges(shamrock::solvergraph::SolverGraphSerializable &sync)
Register sink SoA synchronized edges if missing (idempotent).
SinkEdges< Tvec > get_sink_edges(shamrock::solvergraph::SolverGraphSerializable &sync)
Fetch mutable references to the sink SoA synchronized edges.
void add_sink(SinkEdges< Tvec > &e, typename SinkEdges< Tvec >::Tscal mass, Tvec pos, Tvec velocity, typename SinkEdges< Tvec >::Tscal accretion_radius)
Append one sink to the SoA edges.
void set_sink_particles(SinkEdges< Tvec > &e, const std::vector< SinkParticle< Tvec > > &sinks)
Replace SoA sink edge contents from an AoS sink list (legacy dump migration).
Contains traits and utilities for backend related types.