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
20#include "shambase/string.hpp"
23#include "shambackends/vec.hpp"
27#include <vector>
28
29namespace shambase {
30 template<class T>
31 struct TypeNameInfo<std::vector<T>> {
32 inline static const std::string name = "std::vector<" + get_type_name<T>() + ">";
33 };
34} // namespace shambase
35
36namespace shammodels::sph {
37
38 template<class Tvec>
39 struct SinkEdges {
40 using Tscal = shambase::VecComponent<Tvec>;
41
42 std::vector<Tvec> &pos;
43 std::vector<Tvec> &vel;
44 std::vector<Tvec> &acc_sph;
45 std::vector<Tvec> &acc_ext;
46 std::vector<Tscal> &mass;
47 std::vector<Tvec> &angular_momentum;
48 std::vector<Tscal> &accretion_radius;
49
50 inline bool has_sinks() const { return !pos.empty(); }
51 inline size_t size() const { return pos.size(); }
52 };
53
54 namespace details {
55
56 template<class T>
57 inline T &get_sync_data(
58 shamrock::solvergraph::SolverGraphSerializable &sync, const std::string &name) {
59 return sync.template get_edge_ref<shamrock::solvergraph::IDataEdgeSerializable<T>>(name)
60 .data;
61 }
62
63 } // namespace details
64
69 template<class Tvec>
71 return details::get_sync_data<std::vector<Tvec>>(sync, "sink_pos");
72 }
73
74 template<class Tvec>
75 inline std::vector<Tvec> &get_sink_vel(shamrock::solvergraph::SolverGraphSerializable &sync) {
76 return details::get_sync_data<std::vector<Tvec>>(sync, "sink_vel");
77 }
78
79 template<class Tvec>
80 inline std::vector<Tvec> &get_sink_acc_sph(
82 return details::get_sync_data<std::vector<Tvec>>(sync, "sink_acc_sph");
83 }
84
85 template<class Tvec>
86 inline std::vector<Tvec> &get_sink_acc_ext(
87 shamrock::solvergraph::SolverGraphSerializable &sync) {
88 return details::get_sync_data<std::vector<Tvec>>(sync, "sink_acc_ext");
89 }
90
91 template<class Tvec>
92 inline std::vector<shambase::VecComponent<Tvec>> &get_sink_mass(
93 shamrock::solvergraph::SolverGraphSerializable &sync) {
94 return details::get_sync_data<std::vector<shambase::VecComponent<Tvec>>>(sync, "sink_mass");
95 }
96
97 template<class Tvec>
98 inline std::vector<Tvec> &get_sink_angular_momentum(
99 shamrock::solvergraph::SolverGraphSerializable &sync) {
100 return details::get_sync_data<std::vector<Tvec>>(sync, "sink_angular_momentum");
101 }
102
103 template<class Tvec>
104 inline std::vector<shambase::VecComponent<Tvec>> &get_sink_accretion_radius(
105 shamrock::solvergraph::SolverGraphSerializable &sync) {
106 return details::get_sync_data<std::vector<shambase::VecComponent<Tvec>>>(
107 sync, "sink_accretion_radius");
108 }
109
116 return sync.has_edge("sink_accreted_dust_mass");
117 }
118
119 template<class Tvec>
120 inline std::vector<shambase::VecComponent<Tvec>> &get_sink_accreted_dust_mass(
122 return details::get_sync_data<std::vector<shambase::VecComponent<Tvec>>>(
123 sync, "sink_accreted_dust_mass");
124 }
125
133 template<class Tscal>
135 std::vector<Tscal> &accreted_dust_mass, size_t sink_count, u32 ndust) {
136 size_t target_size = sink_count * ndust;
137 if (accreted_dust_mass.size() == target_size) {
138 return;
139 }
140 if (ndust == 0 || accreted_dust_mass.size() > target_size
141 || accreted_dust_mass.size() % ndust != 0) {
143 "sink accreted dust mass layout mismatch: size={}, sink_count={}, ndust={}",
144 accreted_dust_mass.size(),
145 sink_count,
146 ndust));
147 }
148 accreted_dust_mass.resize(target_size, Tscal{0});
149 }
150
157 template<class Tvec>
159 return !get_sink_pos<Tvec>(sync).empty();
160 }
161
168 template<class Tvec>
170 return SinkEdges<Tvec>{
171 get_sink_pos<Tvec>(sync),
172 get_sink_vel<Tvec>(sync),
173 get_sink_acc_sph<Tvec>(sync),
174 get_sink_acc_ext<Tvec>(sync),
175 get_sink_mass<Tvec>(sync),
176 get_sink_angular_momentum<Tvec>(sync),
177 get_sink_accretion_radius<Tvec>(sync),
178 };
179 }
180
184 template<class Tvec>
185 inline std::vector<SinkParticle<Tvec>> to_sink_particles(const SinkEdges<Tvec> &e) {
186 std::vector<SinkParticle<Tvec>> out;
187 out.reserve(e.size());
188 for (size_t i = 0; i < e.size(); i++) {
189 out.push_back(
191 e.pos[i],
192 e.vel[i],
193 e.acc_sph[i],
194 e.acc_ext[i],
195 e.mass[i],
196 e.angular_momentum[i],
197 e.accretion_radius[i],
198 });
199 }
200 return out;
201 }
202
206 template<class Tvec>
208 SinkEdges<Tvec> &e, const std::vector<SinkParticle<Tvec>> &sinks) {
209 e.pos.clear();
210 e.vel.clear();
211 e.acc_sph.clear();
212 e.acc_ext.clear();
213 e.mass.clear();
214 e.angular_momentum.clear();
215 e.accretion_radius.clear();
216
217 e.pos.reserve(sinks.size());
218 e.vel.reserve(sinks.size());
219 e.acc_sph.reserve(sinks.size());
220 e.acc_ext.reserve(sinks.size());
221 e.mass.reserve(sinks.size());
222 e.angular_momentum.reserve(sinks.size());
223 e.accretion_radius.reserve(sinks.size());
224
225 for (const auto &s : sinks) {
226 e.pos.push_back(s.pos);
227 e.vel.push_back(s.velocity);
228 e.acc_sph.push_back(s.sph_acceleration);
229 e.acc_ext.push_back(s.ext_acceleration);
230 e.mass.push_back(s.mass);
231 e.angular_momentum.push_back(s.angular_momentum);
232 e.accretion_radius.push_back(s.accretion_radius);
233 }
234 }
235
239 template<class Tvec>
240 inline void add_sink(
242 typename SinkEdges<Tvec>::Tscal mass,
243 Tvec pos,
244 Tvec velocity,
245 typename SinkEdges<Tvec>::Tscal accretion_radius) {
246
247 e.pos.push_back(pos);
248 e.vel.push_back(velocity);
249 e.acc_sph.push_back({});
250 e.acc_ext.push_back({});
251 e.mass.push_back(mass);
252 e.angular_momentum.push_back({});
253 e.accretion_radius.push_back(accretion_radius);
254 }
255
256} // namespace shammodels::sph
Declare a class to register and retrieve nodes and edges from a unique container.
std::uint32_t u32
32 bit unsigned integer
bool has_edge(const std::string &name) const
Check whether an edge with the given name exists.
This header file contains utility functions related to exception handling in the code.
Namespace for internal details of the logs module.
namespace for basic c++ utilities
ExcptTypes make_except_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Create an exception with a message and a location.
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; registered by Solver::init_solver_graph)....
SinkEdges< Tvec > get_sink_edges(shamrock::solvergraph::SolverGraphSerializable &sync)
Fetch mutable references to the sink SoA synchronized edges.
void resize_sink_accreted_dust_mass(std::vector< Tscal > &accreted_dust_mass, size_t sink_count, u32 ndust)
Resize the flattened per species accreted dust mass to sink_count * ndust.
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.
bool has_sink_accreted_dust_mass(shamrock::solvergraph::SolverGraphSerializable &sync)
Whether the per species accreted dust mass sink edge is registered.
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.