Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
SinkParticlesAccreteQuantities.cpp
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
16
23#include "shamcomm/logs.hpp"
29#include <shambackends/sycl.hpp>
30#include <string>
31#include <vector>
32
34
35 template<class Tvec>
37
39
40 auto edges = get_edges();
41
42 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
43 auto &q = shambase::get_check_ref(dev_sched).get_queue();
44
45 Tscal gpart_mass = edges.gpart_mass.data;
46 Tscal dt = edges.dt.data;
47
48 sham::DeviceBuffer<u32> acc_flag(0, dev_sched);
49
50 bool had_accretion = false;
51 std::string log = "sink accretion :";
52
53 auto &sink_positions = edges.sink_positions.data;
54 auto &sink_velocities = edges.sink_velocities.data;
55 auto &sink_accelerations = edges.sink_accelerations.data;
56 auto &sink_angmom = edges.sink_angmom.data;
57 auto &sink_mass = edges.sink_mass.data;
58
59 u32 sink_count = shambase::narrow_or_throw<u32>(sink_positions.size());
60
61 // per species dust accretion tracking (monofluid TVA), the dust mass of a particle is
62 // m * epsilon_j with epsilon_j = S_j^2 / rho
63 bool track_dust = ndust > 0 && edges.sink_accreted_dust_mass.has_value();
64 if (track_dust && !(edges.s_j.has_value() && edges.hpart.has_value())) {
66 "accreted dust mass tracking requires s_j and hpart");
67 }
68
69 if (track_dust) {
71 edges.sink_accreted_dust_mass.value().get().data, sink_count, ndust);
72 }
73
74 for (u32 i_sink = 0; i_sink < sink_count; i_sink++) {
75
76 Tvec r_sink = sink_positions[i_sink];
77 Tvec v_sink = sink_velocities[i_sink];
78
79 // compute the accreted mass, position moment and linear momentum
80 Tscal s_acc_mass = 0;
81 Tvec s_acc_mxyz = {0, 0, 0};
82 Tvec s_acc_pxyz = {0, 0, 0};
83 Tvec s_acc_maxyz = {0, 0, 0};
84 Tvec s_acc_lxyz = {0, 0, 0};
85 std::vector<Tscal> s_acc_dust_mass(track_dust ? ndust : 0, 0);
86
87 edges.part_counts.indexes.for_each([&](u64 id_patch, u32 Nobj) {
88 acc_flag.resize(Nobj);
89
90 auto &acc_table = edges.sink_accretion_table.get_spans().get(id_patch);
91
93 q,
94 sham::MultiRef{acc_table},
95 sham::MultiRef{acc_flag},
96 Nobj,
97 [i_sink](u32 id_a, const u32 *__restrict acc_table, u32 *__restrict acc_flag) {
98 acc_flag[id_a] = (acc_table[id_a] == i_sink) ? 1 : 0;
99 });
100
101 auto id_list_accrete = shamalgs::stream_compact(dev_sched, acc_flag, Nobj);
102
103 auto &pos_data = edges.positions.get_spans().get(id_patch);
104 auto &vel_data = edges.velocities.get_spans().get(id_patch);
105 auto &acc_data = edges.accelerations.get_spans().get(id_patch);
106
107 // sum accreted values onto sink
108 if (id_list_accrete.get_size() > 0) {
109 u32 Naccrete = shambase::narrow_or_throw<u32>(id_list_accrete.get_size());
110
111 Tscal acc_mass = gpart_mass * Naccrete;
112
113 sham::DeviceBuffer<Tvec> pxyz_acc(Naccrete, dev_sched);
114 sham::DeviceBuffer<Tvec> maxyz_acc(Naccrete, dev_sched);
115 sham::DeviceBuffer<Tvec> mxyz_acc(Naccrete, dev_sched);
116 sham::DeviceBuffer<Tvec> lxyz_acc(Naccrete, dev_sched);
117
119 q,
120 sham::MultiRef{pos_data, vel_data, acc_data, id_list_accrete},
121 sham::MultiRef{pxyz_acc, mxyz_acc, maxyz_acc, lxyz_acc},
122 Naccrete,
123 [r_sink, v_sink, gpart_mass, dt](
124 u32 id_a,
125 const Tvec *__restrict xyz,
126 const Tvec *__restrict vxyz,
127 const Tvec *__restrict axyz,
128 const u32 *__restrict id_acc,
129 Tvec *__restrict accretion_p,
130 Tvec *__restrict accretion_mr,
131 Tvec *__restrict accretion_ma,
132 Tvec *__restrict accretion_l) {
133 u32 i_a = id_acc[id_a];
134 Tvec r = xyz[i_a];
135 Tvec v = vxyz[i_a];
136 Tvec a = axyz[i_a];
137 accretion_p[id_a] = gpart_mass * v;
138 accretion_mr[id_a] = gpart_mass * r;
139 accretion_ma[id_a] = gpart_mass * a;
140
141 // dirty trick to account for the residual acceleration in the spin.
142 // This allows us to maitain a much better angular momentum
143 // conservation.
144 v += a * dt / 2;
145 accretion_l[id_a] = gpart_mass * sycl::cross(r - r_sink, v - v_sink);
146 });
147
148 Tvec acc_pxyz = shamalgs::primitives::sum(dev_sched, pxyz_acc, 0, Naccrete);
149 Tvec acc_mxyz = shamalgs::primitives::sum(dev_sched, mxyz_acc, 0, Naccrete);
150 Tvec acc_maxyz = shamalgs::primitives::sum(dev_sched, maxyz_acc, 0, Naccrete);
151 Tvec acc_lxyz = shamalgs::primitives::sum(dev_sched, lxyz_acc, 0, Naccrete);
152
153 s_acc_mass += acc_mass;
154 s_acc_pxyz += acc_pxyz;
155 s_acc_mxyz += acc_mxyz;
156 s_acc_maxyz += acc_maxyz;
157 s_acc_lxyz += acc_lxyz;
158
159 if (track_dust) {
160 // accreted dust mass laid out as [j_dust * Naccrete + id_a] so that each
161 // species can be summed over a contiguous range
162 sham::DeviceBuffer<Tscal> dust_mass_acc(Naccrete * ndust, dev_sched);
163
164 auto &hpart_data = edges.hpart.value().get().get_spans().get(id_patch);
165 auto &s_j_data = edges.s_j.value().get().get_spans().get(id_patch);
166
168 q,
169 sham::MultiRef{hpart_data, s_j_data, id_list_accrete},
170 sham::MultiRef{dust_mass_acc},
171 Naccrete,
172 [gpart_mass, hfactd = this->hfactd, ndust = this->ndust, Naccrete](
173 u32 id_a,
174 const Tscal *__restrict hpart,
175 const Tscal *__restrict s_j,
176 const u32 *__restrict id_acc,
177 Tscal *__restrict dust_mass) {
178 u32 i_a = id_acc[id_a];
179 Tscal rho_a = shamrock::sph::rho_h(gpart_mass, hpart[i_a], hfactd);
180 for (u32 j = 0; j < ndust; j++) {
181 Tscal sj = s_j[i_a * ndust + j];
182 dust_mass[j * Naccrete + id_a] = gpart_mass * sj * sj / rho_a;
183 }
184 });
185
186 for (u32 j = 0; j < ndust; j++) {
187 s_acc_dust_mass[j] += shamalgs::primitives::sum(
188 dev_sched, dust_mass_acc, j * Naccrete, (j + 1) * Naccrete);
189 }
190 }
191 }
192 });
193
194 Tscal sum_acc_mass = shamalgs::collective::allreduce_sum(s_acc_mass);
195
196 // if there is accretion continue otherwise skip that part
197 if (sum_acc_mass <= 0) {
198 continue;
199 }
200
201 Tvec sum_acc_pxyz = shamalgs::collective::allreduce_sum(s_acc_pxyz);
202 Tvec sum_acc_mxyz = shamalgs::collective::allreduce_sum(s_acc_mxyz);
203 Tvec sum_acc_maxyz = shamalgs::collective::allreduce_sum(s_acc_maxyz);
204 Tvec sum_acc_lxyz = shamalgs::collective::allreduce_sum(s_acc_lxyz);
205
206 std::vector<Tscal> sum_acc_dust_mass(s_acc_dust_mass.size());
207 for (u32 j = 0; j < s_acc_dust_mass.size(); j++) {
208 sum_acc_dust_mass[j] = shamalgs::collective::allreduce_sum(s_acc_dust_mass[j]);
209 }
210
211 Tscal old_mass = sink_mass[i_sink];
212 Tvec old_pos = sink_positions[i_sink];
213 Tvec old_vel = sink_velocities[i_sink];
214 Tvec old_acc = sink_accelerations[i_sink];
215 Tvec old_ang = sink_angmom[i_sink];
216
217 // compute the new sink values
218 Tscal new_mass = old_mass + sum_acc_mass;
219 Tvec new_pos = (sum_acc_mxyz + old_pos * old_mass) / (old_mass + sum_acc_mass);
220 Tvec new_vel = (sum_acc_pxyz + old_vel * old_mass) / (old_mass + sum_acc_mass);
221 Tvec new_acc = (sum_acc_maxyz + old_acc * old_mass) / (old_mass + sum_acc_mass);
222 Tvec new_ang_mom = old_ang + sum_acc_lxyz
223 - new_mass * sycl::cross(new_pos - old_pos, new_vel - old_vel);
224
225 // write back the update sink state
226 sink_mass[i_sink] = new_mass;
227 sink_positions[i_sink] = new_pos;
228 sink_velocities[i_sink] = new_vel;
229 sink_angmom[i_sink] = new_ang_mom;
230 sink_accelerations[i_sink] = new_acc;
231
232 if (track_dust) {
233 auto &sink_dust_mass = edges.sink_accreted_dust_mass.value().get().data;
234 for (u32 j = 0; j < ndust; j++) {
235 sink_dust_mass[i_sink * ndust + j] += sum_acc_dust_mass[j];
236 }
237 }
238
239 had_accretion = true;
240 log += sham::format(
241 "\n id {} deltas : mass={} r={} v={} l={}",
242 i_sink,
243 new_mass - old_mass,
244 new_pos - old_pos,
245 new_vel - old_vel,
246 new_ang_mom - old_ang);
247 if (track_dust) {
248 log += sham::format(" dust={}", sum_acc_dust_mass);
249 }
250 }
251
252 if (shamcomm::world_rank() == 0 && had_accretion) {
253 logger::info_ln("sph::Sink", log);
254 }
255 }
256
257 template<class Tvec>
259 return "TODO";
260 }
261
262} // namespace shammodels::sph::modules
263
Header file describing a Node Instance.
Accrete flagged SPH particles onto sinks (mass, CoM, spin, etc.).
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
A buffer allocated in USM (Unified Shared Memory).
void resize(size_t new_size, bool keep_data=true)
Resizes the buffer to a given size.
virtual std::string _impl_get_tex() const
get the tex of the node
void kernel_call(sham::DeviceQueue &q, RefIn in, RefOut in_out, u32 n, Functor &&func, SourceLocation &&callsite=SourceLocation{})
Submit a kernel to a SYCL queue.
std::tuple< std::optional< sycl::buffer< u32 > >, u32 > stream_compact(sycl::queue &q, sycl::buffer< u32 > &buf_flags, u32 len)
Stream compaction algorithm.
Definition numeric.cpp:65
T sum(const sham::DeviceScheduler_ptr &sched, const sham::DeviceBuffer< T > &buf1, u32 start_id, u32 end_id)
Compute the sum of elements in a device buffer within a specified range.
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
T & get_check_ref(const std::unique_ptr< T > &ptr, SourceLocation loc=SourceLocation())
Takes a std::unique_ptr and returns a reference to the object it holds. It throws a std::runtime_erro...
Definition memory.hpp:112
i32 world_rank()
Gives the rank of the current process in the MPI communicator.
Definition worldInfo.cpp:41
namespace for the sph model modules
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.
Utilities for safe type narrowing conversions.
void info_ln(std::string module_name, Types... var2)
Prints a log message with multiple arguments followed by a newline.
Definition logs.hpp:132
Helpers to access SPH sink particles stored as SoA synchronized data edges.
#define __shamrock_stack_entry()
Macro to create a stack entry.
A class that references multiple buffers or similar objects.
Definition MultiRef.hpp:33
Functions related to the MPI communicator.