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
17#include "shambase/memory.hpp"
19#include "shambase/string.hpp"
22#include "shambackends/math.hpp"
23#include "shambackends/vec.hpp"
24#include "shamcomm/logs.hpp"
27#include "shamrock/patch/PatchDataField.hpp" // IWYU pragma: keep
29#include <experimental/mdspan>
30#include <vector>
31
33
34 template<class Tvec>
36 using Tscal = shambase::VecComponent<Tvec>;
37
38 using mdspan_rank_1 = std::mdspan<Tscal, std::dextents<u32, 1>>;
39 using mdspan_rank_3 = std::mdspan<Tscal, std::dextents<u32, 3>>;
40
41 using const_mdspan_rank_1 = std::mdspan<const Tscal, std::dextents<u32, 1>>;
42 using const_mdspan_rank_3 = std::mdspan<const Tscal, std::dextents<u32, 3>>;
43
44 u32 nbins;
45 Tscal rho_eps;
46 Tscal dv_max;
47 u32 corrected_len;
48 u32 group_size;
49 u32 true_size;
50
51 auto operator()(
52 u32 /**/,
53 // common to all kernel calls
54 const Tscal *__restrict massgrid_ptr,
55 const Tscal *__restrict tensor_tabflux_coag,
56 // field specific data
57 const Tscal *__restrict s_j,
58 const Tvec *__restrict delta_v_j,
59 Tscal *__restrict S_coag) const {
60
61 auto range = sycl::nd_range<1>{corrected_len, group_size};
62
63 auto local_acc_sz_nbins = sycl::range<1>{group_size * nbins};
64
65 auto true_size = this->true_size;
66 auto rho_eps = this->rho_eps;
67 auto dv_max = this->dv_max;
68
69 return [=, nbins = this->nbins](sycl::handler &cgh) {
70 auto gij_acc = sycl::local_accessor<Tscal>{local_acc_sz_nbins, cgh};
71 auto flux_acc = sycl::local_accessor<Tscal>{local_acc_sz_nbins, cgh};
72
73 cgh.parallel_for(range, [=](sycl::nd_item<1> tid) {
74 const u64 id_a = tid.get_global_linear_id();
75 const u64 lid = tid.get_local_linear_id();
76
77 if (id_a >= true_size) {
78 return;
79 }
80
81 u32 id_a_d = id_a * nbins;
82
83 /* inputs */
84 const_mdspan_rank_3 tabflux_coag(tensor_tabflux_coag, nbins, nbins, nbins);
85 const_mdspan_rank_1 massgrid(massgrid_ptr, nbins + 1);
86
87 /* internal */
88 auto gij_loc = &(gij_acc[nbins * lid]);
89 auto flux_loc = &(flux_acc[nbins * lid]);
90
91 mdspan_rank_1 gij(gij_loc, nbins);
92 mdspan_rank_1 flux(flux_loc, nbins);
93
94 /* output */
95 mdspan_rank_1 S_coag_span(S_coag + id_a_d, nbins);
96
97 /* lambda getters */
98 auto rho_dust = [&](int j) {
99 auto tmp = s_j[id_a_d + j];
100 return tmp * tmp;
101 };
102
103 auto dv = [&, delta_v = delta_v_j + id_a_d](int i, int j) {
104 // dv_ij = v_dust_j - v_dust_i = delta_v_j[j] - delta_v_j[i]
105 auto tmp = sycl::length(delta_v[j] - delta_v[i]);
106 return (tmp > dv_max) ? 0 : tmp;
107 };
108
109 // should implement the same content as
110 // src/pylib/shamrock/external/coala/interface_coala_shamrock.py
111
112 shamphys::coala_k0_source_term(
113 nbins,
114 dv,
115 rho_dust,
116 rho_eps,
117 massgrid,
118 tabflux_coag,
119 gij,
120 flux,
121 S_coag_span);
122 });
123 };
124 }
125 };
126
127 template<class Tvec>
129
131
132 auto edges = get_edges();
133
134 auto s_j_spans = edges.s_j.get_spans();
135 auto delta_v_j_spans = edges.delta_v_j.get_spans();
136
137 auto counts = edges.part_counts.indexes;
138
139 edges.S_coag.ensure_sizes(counts);
140 auto S_coag_spans = edges.S_coag.get_spans();
141
142 Tscal rho_eps = edges.rhodust_eps.value;
143 Tscal dv_max = edges.dv_max.value;
144 const std::vector<Tscal> &massgrid = edges.massgrid.value;
145 const std::vector<Tscal> &tensor_tabflux_coag = edges.tensor_tabflux_coag.value;
146
147 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
148 auto &q = shambase::get_check_ref(dev_sched).get_queue();
149
150 sham::DeviceBuffer<Tscal> massgrid_buf(nbins + 1, dev_sched);
151 massgrid_buf.copy_from_stdvec(massgrid);
152
153 sham::DeviceBuffer<Tscal> tensor_tabflux_coag_buf(nbins * nbins * nbins, dev_sched);
154 tensor_tabflux_coag_buf.copy_from_stdvec(tensor_tabflux_coag);
155
156 u32 group_size = 64;
157
158 counts.for_each([&](u64 id_patch, u64 count) {
159 u32 group_cnt = shambase::group_count(count, group_size);
160 u32 corrected_len = group_cnt * group_size;
161
162 sham::kernel_call_hndl(
163 q,
165 massgrid_buf,
166 tensor_tabflux_coag_buf,
167 s_j_spans.get(id_patch),
168 delta_v_j_spans.get(id_patch)},
169 sham::MultiRef{S_coag_spans.get(id_patch)},
170 count,
172 .nbins = nbins,
173 .rho_eps = rho_eps,
174 .dv_max = dv_max,
175 .corrected_len = corrected_len,
176 .group_size = group_size,
177 .true_size = u32(count)});
178 });
179 }
180
181 template<class Tvec>
183
184 auto rhodust_eps = get_ro_edge_base(0).get_tex_symbol();
185 auto massgrid = get_ro_edge_base(1).get_tex_symbol();
186 auto tensor_tabflux_coag = get_ro_edge_base(2).get_tex_symbol();
187 auto part_counts = get_ro_edge_base(3).get_tex_symbol();
188 auto s_j = get_ro_edge_base(5).get_tex_symbol();
189 auto delta_v_j = get_ro_edge_base(6).get_tex_symbol();
190 auto S_coag = get_rw_edge_base(0).get_tex_symbol();
191
192 std::string tex = R"tex(
193 COALA dust coagulation source term, DG $k=0$ (Lombart et al., 2021)
194
195 Per gas particle $a$ and mass bin $j$ (monofluid: $\rho_{{\rm d},j,a}} = {s_j}_{j,a}^2$):
196
197 \begin{align}
198 \rho_{{\rm d},j,a} &= {s_j}_{j,a}^2 \\
199 \Delta m_j &= {massgrid}_{j+1} - {massgrid}_j \\
200 g_{j,a} &= \begin{cases}
201 \rho_{{\rm d},j,a} / \Delta m_j & \rho_{{\rm d},j,a} > \rho_{\rm eps} \\
202 0 & \text{otherwise}
203 \end{cases} \\
204 \mathrm{dv}_{l,m,a} &= \left| {delta_v_j}_{m,a} - {delta_v_j}_{l,a} \right| \\
205 \mathrm{flux}_{j,a} &= \sum_{l,m}
206 {tensor_tabflux_coag}_{j,l,m}\,
207 \mathrm{dv}_{l,m,a}\, g_{l,a}\, g_{m,a} \\
208 {S_coag}_{0,a} &= -\mathrm{flux}_{0,a}, \quad
209 {S_coag}_{j,a} = \mathrm{flux}_{j-1,a} - \mathrm{flux}_{j,a}
210 \quad (j \ge 1) \\
211 a &\in [0, {part_counts}), \quad j,l,m \in [0, N_{\rm bins}) \\
212 \rho_{\rm eps} &= {rhodust_eps}, \quad N_{\rm bins} = {nbins}
213 \end{align}
214 )tex";
215
216 shambase::replace_all(tex, "{rhodust_eps}", rhodust_eps);
217 shambase::replace_all(tex, "{massgrid}", massgrid);
218 shambase::replace_all(tex, "{tensor_tabflux_coag}", tensor_tabflux_coag);
219 shambase::replace_all(tex, "{part_counts}", part_counts);
220 shambase::replace_all(tex, "{s_j}", s_j);
221 shambase::replace_all(tex, "{delta_v_j}", delta_v_j);
222 shambase::replace_all(tex, "{S_coag}", S_coag);
223 shambase::replace_all(tex, "{nbins}", shambase::format("{}", nbins));
224
225 return tex;
226 }
227} // namespace shammodels::sph::modules
228
Header file describing a Node Instance.
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 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
IEdge & get_rw_edge_base(int slot)
Get a reference to a read write edge and cast it to the type IEdge.
Definition INode.hpp:100
const IEdge & get_ro_edge_base(int slot)
Get a reference to a read only edge.
Definition INode.hpp:91
COALA dust coagulation helpers for a DG (piecewise-constant) basis.
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:110
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:110
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.