Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
NodeEvolveDustCOALASourceTerm.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
18#include "shambase/memory.hpp"
20#include "shambase/string.hpp"
23#include "shambackends/math.hpp"
24#include "shambackends/vec.hpp"
25#include "shamcomm/logs.hpp"
28#include "shamrock/patch/PatchDataField.hpp" // IWYU pragma: keep
30#include <experimental/mdspan>
31#include <array>
32#include <vector>
33
35
36 template<class Tvec>
38 using Tscal = shambase::VecComponent<Tvec>;
39
40 using mdspan_rank_1 = std::mdspan<Tscal, std::dextents<u32, 1>>;
41 using mdspan_rank_1_strided = std::mdspan<Tscal, std::dextents<u32, 1>, std::layout_stride>;
42
43 using const_mdspan_rank_1 = std::mdspan<const Tscal, std::dextents<u32, 1>>;
44
45 u32 nbins;
46 Tscal rho_eps;
47 Tscal vfrag_threshold;
48 u32 corrected_len;
49 u32 group_size;
50 u32 true_size;
51
52 auto operator()(
53 u32 /**/,
54 // common to all kernel calls
55 const Tscal *__restrict massgrid_ptr,
56 const u32 *__restrict tabflux_pair_offset,
57 const u32 *__restrict tabflux_pair_jmin,
58 const Tscal *__restrict tabflux_values,
59 // field specific data
60 const Tscal *__restrict s_j,
61 const Tvec *__restrict delta_v_j,
62 Tscal *__restrict S_coag,
63 // scratch
64 Tscal *__restrict gij_scratch) const {
65
66 auto range = sycl::nd_range<1>{corrected_len, group_size};
67
68 auto local_acc_sz_nbins = sycl::range<1>{group_size * nbins};
69
70 auto true_size = this->true_size;
71 auto rho_eps = this->rho_eps;
72 auto vfrag_threshold = this->vfrag_threshold;
73 auto group_size = this->group_size;
74
75 return [=, nbins = this->nbins](sycl::handler &cgh) {
76 auto flux_acc = sycl::local_accessor<Tscal>{local_acc_sz_nbins, cgh};
77
78 cgh.parallel_for(range, [=](sycl::nd_item<1> tid) {
79 const u64 id_a = tid.get_global_linear_id();
80 const u64 lid = tid.get_local_linear_id();
81
82 if (id_a >= true_size) {
83 return;
84 }
85
86 u32 id_a_d = id_a * nbins;
87
88 /* inputs */
90 nbins, tabflux_pair_offset, tabflux_pair_jmin, tabflux_values};
91 const_mdspan_rank_1 massgrid(massgrid_ptr, nbins + 1);
92
93 /* internal */
94 // flux in local memory with a bin-major layout (stride group_size), such
95 // that the work-items of a sub-group, which access the same bin at the
96 // same time, hit distinct banks
97 mdspan_rank_1_strided flux(
98 &(flux_acc[lid]),
99 typename mdspan_rank_1_strided::mapping_type(
100 std::dextents<u32, 1>(nbins), std::array<u32, 1>{group_size}));
101
102 // gij in global memory with a particle-major layout, such that the
103 // work-items of a sub-group, which access the same bin at the same time,
104 // read contiguous addresses
105 mdspan_rank_1_strided gij(
106 gij_scratch + id_a,
107 typename mdspan_rank_1_strided::mapping_type(
108 std::dextents<u32, 1>(nbins), std::array<u32, 1>{true_size}));
109
110 /* output */
111 mdspan_rank_1 S_coag_span(S_coag + id_a_d, nbins);
112
113 /* lambda getters */
114 auto rho_dust = [&](int j) {
115 auto tmp = s_j[id_a_d + j];
116 return tmp * tmp;
117 };
118
119 auto dv = [&, delta_v = delta_v_j + id_a_d](int i, int j) {
120 // dv_ij = v_dust_j - v_dust_i = delta_v_j[j] - delta_v_j[i]
121 auto tmp = sycl::length(delta_v[j] - delta_v[i]);
122 // Coag-only kernel: "poor man" fragmentation, pairs faster than
123 // vfrag_threshold do not coagulate (dv_ij = 0).
124 return (tmp > vfrag_threshold) ? 0 : tmp;
125 };
126
127 // should implement the same content as
128 // src/pylib/shamrock/external/coala/interface_coala_shamrock.py
129
130 shamphys::coala_k0_source_term(
131 nbins,
132 dv,
133 rho_dust,
134 rho_eps,
135 massgrid,
136 tabflux_coag,
137 gij,
138 flux,
139 S_coag_span);
140 });
141 };
142 }
143 };
144
145 template<class Tvec>
147
149
150 auto edges = get_edges();
151
152 auto s_j_spans = edges.s_j.get_spans();
153 auto delta_v_j_spans = edges.delta_v_j.get_spans();
154
155 auto counts = edges.part_counts.indexes;
156
157 edges.S_coag.ensure_sizes(counts);
158 auto S_coag_spans = edges.S_coag.get_spans();
159
160 Tscal rho_eps = edges.rhodust_eps.data;
161 Tscal vfrag_threshold = edges.vfrag_threshold.data;
162 const std::vector<Tscal> &massgrid = edges.massgrid.data;
163 const std::vector<Tscal> &tensor_tabflux_coag = edges.tensor_tabflux_coag.data;
164
165 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
166 auto &q = shambase::get_check_ref(dev_sched).get_queue();
167
168 sham::DeviceBuffer<Tscal> massgrid_buf(nbins + 1, dev_sched);
169 massgrid_buf.copy_from_stdvec(massgrid);
170
171 // only the non-zero part of the tensor is used on device
173 nbins,
174 std::mdspan<const Tscal, std::dextents<u32, 3>>(
175 tensor_tabflux_coag.data(), nbins, nbins, nbins));
176
177 sham::DeviceBuffer<u32> tabflux_pair_offset_buf(
178 tabflux_sparse.pair_offset.size(), dev_sched);
179 tabflux_pair_offset_buf.copy_from_stdvec(tabflux_sparse.pair_offset);
180
181 sham::DeviceBuffer<u32> tabflux_pair_jmin_buf(tabflux_sparse.pair_jmin.size(), dev_sched);
182 tabflux_pair_jmin_buf.copy_from_stdvec(tabflux_sparse.pair_jmin);
183
184 sham::DeviceBuffer<Tscal> tabflux_values_buf(tabflux_sparse.values.size(), dev_sched);
185 tabflux_values_buf.copy_from_stdvec(tabflux_sparse.values);
186
187 // per thread local memory: flux, one per bin
188 usize local_mem_per_thread = nbins * sizeof(Tscal);
189 usize local_mem_size = q.get_device_prop().local_mem_size;
190
191 u32 group_size = 64;
192 while (group_size > 1 && group_size * local_mem_per_thread > local_mem_size) {
193 group_size /= 2;
194 }
195 if (group_size * local_mem_per_thread > local_mem_size) {
197 "COALA kernel: not enough local memory for nbins = {} ({} B per thread, {} B "
198 "available)",
199 nbins,
200 local_mem_per_thread,
201 local_mem_size));
202 }
203
204 counts.for_each([&](u64 id_patch, u64 count) {
205 u32 group_cnt = shambase::group_count(count, group_size);
206 u32 corrected_len = group_cnt * group_size;
207
208 sham::DeviceBuffer<Tscal> gij_scratch(count * nbins, dev_sched);
209
210 sham::kernel_call_hndl(
211 q,
213 massgrid_buf,
214 tabflux_pair_offset_buf,
215 tabflux_pair_jmin_buf,
216 tabflux_values_buf,
217 s_j_spans.get(id_patch),
218 delta_v_j_spans.get(id_patch)},
219 sham::MultiRef{S_coag_spans.get(id_patch), gij_scratch},
220 count,
222 .nbins = nbins,
223 .rho_eps = rho_eps,
224 .vfrag_threshold = vfrag_threshold,
225 .corrected_len = corrected_len,
226 .group_size = group_size,
227 .true_size = u32(count)});
228 });
229 }
230
231 template<class Tvec>
233 std::string tex = R"tex(
234 COALA dust coagulation source term, DG $k=0$ (Lombart et al., 2021)
235
236 Per gas particle $a$ and mass bin $j$ (monofluid: $\rho_{{\rm d},j,a} = {s_j}_{j,a}^2$):
237
238 \begin{align}
239 \rho_{{\rm d},j,a} &= {s_j}_{j,a}^2 \\
240 \Delta m_j &= {massgrid}_{j+1} - {massgrid}_j \\
241 g_{j,a} &= \begin{cases}
242 \rho_{{\rm d},j,a} / \Delta m_j & \rho_{{\rm d},j,a} > \rho_{\rm eps} \\
243 0 & \text{otherwise}
244 \end{cases} \\
245 \mathrm{dv}_{l,m,a} &= \left| {delta_v_j}_{m,a} - {delta_v_j}_{l,a} \right| \\
246 \mathrm{flux}_{j,a} &= \sum_{l,m}
247 {tensor_tabflux_coag}_{j,l,m}\,
248 \mathrm{dv}_{l,m,a}\, g_{l,a}\, g_{m,a} \\
249 {S_coag}_{0,a} &= -\mathrm{flux}_{0,a}, \quad
250 {S_coag}_{j,a} = \mathrm{flux}_{j-1,a} - \mathrm{flux}_{j,a}
251 \quad (j \ge 1) \\
252 a &\in [0, {part_counts}), \quad j,l,m \in [0, N_{\rm bins}) \\
253 \rho_{\rm eps} &= {rhodust_eps}, \quad N_{\rm bins} = {nbins}
254 \end{align}
255 )tex";
256
257 replace_edges_tex_symbols(tex);
258
259 shambase::replace_all(tex, "{nbins}", sham::format("{}", nbins));
260
261 return tex;
262 }
263} // namespace shammodels::sph::modules
264
Header file describing a Node Instance.
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
std::size_t usize
size_t alias
A buffer allocated in USM (Unified Shared Memory).
void copy_from_stdvec(const std::vector< T > &vec)
Copy the content of a std::vector into the buffer.
virtual std::string _impl_get_tex() const
get the tex of the node
COALA dust coagulation helpers for a DG (piecewise-constant) basis.
TabfluxCoagK0Sparse< T, Tidx > make_tabflux_coag_k0_sparse(int nbins, shambase::is_mdspan_rank< 3 > auto tensor_tabflux_coag)
Build the TabfluxCoagK0Sparse form of tensor_tabflux_coag.
This header file contains utility functions related to exception handling in the code.
constexpr u32 group_count(u32 len, u32 group_size)
Calculates the number of groups based on the length and group size.
Definition integer.hpp:125
void replace_all(std::string &inout, std::string_view what, std::string_view with)
replace all occurence of a search string with another
Definition string.hpp:106
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
namespace for the sph model modules
This file contains the definition for the stacktrace related functionality.
#define __shamrock_stack_entry()
Macro to create a stack entry.
A class that references multiple buffers or similar objects.
Definition MultiRef.hpp:33
Device view of a TabfluxCoagK0Sparse (see its documentation for the layout).