Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
ApplyDragExpo.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
18
20#include "shambase/string.hpp"
28
29namespace {
30
32 template<class Tvec>
33 struct ExpoDragCell {
34 using Tscal = shambase::VecComponent<Tvec>;
35 using mdspan_t
36 = std::mdspan<Tscal, std::extents<size_t, std::dynamic_extent, std::dynamic_extent>>;
37
38 Tscal dt;
39 u32 ndust;
40 u32 friction_control;
41 size_t mat_size;
42
43 const Tscal *rho_next;
44 const Tvec *rhov_next;
45 const Tscal *rhoe_next;
46 const Tscal *rho_d_next;
47 const Tvec *rhov_d_next;
48 const Tscal *alphas;
49
50 Tscal *rho;
51 Tvec *rhov;
52 Tscal *rhoe;
53 Tscal *rho_d;
54 Tvec *rhov_d;
55
56 inline void operator()(
57 u32 id_a, Tscal *ptr_A, Tscal *ptr_B, Tscal *ptr_F, Tscal *ptr_I, Tscal *ptr_Id) const {
58
59 // sparse jacobian matrix
60 auto get_jacobian = [&](u32 id, mdspan_t &jacobian) {
62 // fill first row
63 for (auto j = 1; j < jacobian.extent(1); j++)
64 jacobian(0, j) = alphas[j - 1];
65 // fil first column
66 for (auto i = 1; i < jacobian.extent(0); i++) {
67 jacobian(i, 0)
68 = alphas[i - 1] * (rho_d_next[id * ndust + (i - 1)] / rho_next[id]);
69 jacobian(0, 0) -= jacobian(i, 0);
70 }
71 // fill diagonal from (i,j)=(1,1)
72 for (auto i = 1; i < jacobian.extent(0); i++)
73 jacobian(i, i) = -alphas[i - 1];
74 // the rest of the buffer is set to zero
75 };
76
77 Tscal mu = 0;
78 for (auto i = 0; i < ndust; i++) {
79 mu += (1 + (rho_d_next[id_a * ndust + i] / rho_next[id_a])) * alphas[i];
80 }
81 mu *= (-dt / (ndust + 1));
82
83 // create mdspan(s)
84 mdspan_t mdspan_A(ptr_A, mat_size, mat_size);
85 mdspan_t mdspan_B(ptr_B, mat_size, mat_size);
86 mdspan_t mdspan_F(ptr_F, mat_size, mat_size);
87 mdspan_t mdspan_I(ptr_I, mat_size, mat_size);
88 mdspan_t mdspan_Id(ptr_Id, mat_size, mat_size);
89
90 get_jacobian(id_a, mdspan_A);
91
92 // pre-processing step
94 shammath::mat_axpy_beta<Tscal, Tscal>(-mu, mdspan_Id, dt, mdspan_A);
95
96 // compute matrix exponential
97 const i32 K_exp = 9;
99 K_exp, mdspan_A, mdspan_F, mdspan_B, mdspan_I, mdspan_Id, ndust + 1);
100
101 // post-processing step
102 shammath::mat_mul_scalar<Tscal>(mdspan_A, sycl::exp(mu));
103
104 // use the matrix exponential to for to updates momemtum
105 Tvec r = {0., 0., 0.}, dd = {0., 0., 0.};
106 r += mdspan_A(0, 0) * rhov_next[id_a];
107
108 for (auto j = 1; j < ndust + 1; j++) {
109 r += mdspan_A(0, j) * rhov_d_next[id_a * ndust + (j - 1)];
110 }
111
112 dd = r - rhov_next[id_a];
113
114 Tscal dissipation = 0, drag_work = 0;
115
116 // compute work of drag terms
117 Tscal inv_rho = 1.0 / (rho_next[id_a]);
118
119 Tvec v_bf = inv_rho * rhov_next[id_a];
120 Tvec v_af = inv_rho * r;
121
122 drag_work = 0.5
123 * (dd[0] * (v_bf[0] + v_af[0]) + dd[1] * (v_bf[1] + v_af[1])
124 + dd[2] * (v_bf[2] + v_af[2]));
125
126 // save gas momentum back
127 rhov[id_a] = r;
128 rho[id_a] = rho_next[id_a];
129
130 for (auto d_id = 1; d_id <= ndust; d_id++) {
131 r *= 0;
132 r += mdspan_A(d_id, 0) * rhov_next[id_a];
133
134 for (auto j = 1; j <= ndust; j++) {
135
136 r += mdspan_A(d_id, j) * rhov_d_next[id_a * ndust + (j - 1)];
137 }
138
139 dd = r - rhov_d_next[id_a * ndust + (d_id - 1)];
140
141 inv_rho = 1.0 / (rho_d_next[id_a * ndust + (d_id - 1)]);
142
143 v_bf = inv_rho * rhov_d_next[id_a * ndust + (d_id - 1)];
144
145 v_af = inv_rho * r;
146
147 // compute dissipaation by id-th dust
148 dissipation += 0.5
149 * (dd[0] * (v_bf[0] + v_af[0]) + dd[1] * (v_bf[1] + v_af[1])
150 + dd[2] * (v_bf[2] + v_af[2]));
151
152 // save dust momentum back
153 rhov_d[id_a * ndust + (d_id - 1)] = r;
154 rho_d[id_a * ndust + (d_id - 1)] = rho_d_next[id_a * ndust + (d_id - 1)];
155 }
156
157 // updates energy
158 rhoe[id_a] = rhoe_next[id_a] + (1 - friction_control) * drag_work
159 - friction_control * dissipation;
160 }
161 };
162
163} // namespace
164
166
167 template<class Tvec>
169 auto edges = get_edges();
170
171 edges.spans_rho_next.check_sizes(edges.sizes.indexes);
172 edges.spans_rhov_next.check_sizes(edges.sizes.indexes);
173 edges.spans_rhoe_next.check_sizes(edges.sizes.indexes);
174 edges.spans_rho_d_next.check_sizes(edges.sizes.indexes);
175 edges.spans_rhov_d_next.check_sizes(edges.sizes.indexes);
176 edges.spans_rho.check_sizes(edges.sizes.indexes);
177 edges.spans_rhov.check_sizes(edges.sizes.indexes);
178 edges.spans_rhoe.check_sizes(edges.sizes.indexes);
179 edges.spans_rho_d.check_sizes(edges.sizes.indexes);
180 edges.spans_rhov_d.check_sizes(edges.sizes.indexes);
181
182 Tscal dt = edges.dt.value;
183 u32 ndust = this->ndust;
184 u32 friction_control = (enable_frictional_heating == false) ? 1 : 0;
185
186 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
187 sham::DeviceQueue &q = dev_sched->get_queue();
188
189 sham::DeviceBuffer<Tscal> alphas_buf(ndust, dev_sched);
190 alphas_buf.copy_from_stdvec(alphas);
191
192 size_t mat_size = ndust + 1;
193 size_t mat_size_squared = mat_size * mat_size;
194 size_t group_size
195 = (q.get_device_prop().local_mem_size) / (5 * mat_size_squared * sizeof(Tscal));
196 size_t loc_acc_size = mat_size_squared * group_size;
197
198 size_t loc_mem_size = 5 * sizeof(Tscal) * loc_acc_size;
199
200 bool use_global_mem = group_size < 8;
201
202 if (!use_global_mem && loc_mem_size > q.get_device_prop().local_mem_size) {
204 "not enough local memory for expo drag integrator:\n"
205 "loc_mem_size: {} > max_local_mem: {}\n"
206 "loc_acc_size: {}\n"
207 "group_size: {}\n"
208 "ndust: {}\n",
209 loc_mem_size,
211 loc_acc_size,
212 group_size,
213 ndust));
214 }
215
216 edges.sizes.indexes.for_each([&](u64 id_patch, u32 block_count) {
217 u32 cell_count = block_count * block_size;
218
219 auto in = sham::MultiRef{
220 edges.spans_rho_next.get_spans().get(id_patch),
221 edges.spans_rhov_next.get_spans().get(id_patch),
222 edges.spans_rhoe_next.get_spans().get(id_patch),
223 edges.spans_rho_d_next.get_spans().get(id_patch),
224 edges.spans_rhov_d_next.get_spans().get(id_patch),
225 alphas_buf};
226
227 auto make_cell = [dt, ndust, friction_control, mat_size](
228 const Tscal *rho_next,
229 const Tvec *rhov_next,
230 const Tscal *rhoe_next,
231 const Tscal *rho_d_next,
232 const Tvec *rhov_d_next,
233 const Tscal *alphas,
234 Tscal *rho,
235 Tvec *rhov,
236 Tscal *rhoe,
237 Tscal *rho_d,
238 Tvec *rhov_d) {
239 return ExpoDragCell<Tvec>{
240 dt,
241 ndust,
242 friction_control,
243 mat_size,
244 rho_next,
245 rhov_next,
246 rhoe_next,
247 rho_d_next,
248 rhov_d_next,
249 alphas,
250 rho,
251 rhov,
252 rhoe,
253 rho_d,
254 rhov_d};
255 };
256
257 if (use_global_mem) {
258 sham::DeviceBuffer<Tscal> scratch_expo(
259 5 * mat_size_squared * cell_count, dev_sched);
260
262 q,
263 in,
265 edges.spans_rho.get_spans().get(id_patch),
266 edges.spans_rhov.get_spans().get(id_patch),
267 edges.spans_rhoe.get_spans().get(id_patch),
268 edges.spans_rho_d.get_spans().get(id_patch),
269 edges.spans_rhov_d.get_spans().get(id_patch),
270 scratch_expo},
271 cell_count,
272 [mat_size_squared, make_cell](
273 u32 id_a,
274 const Tscal *rho_next,
275 const Tvec *rhov_next,
276 const Tscal *rhoe_next,
277 const Tscal *rho_d_next,
278 const Tvec *rhov_d_next,
279 const Tscal *alphas,
280 Tscal *rho,
281 Tvec *rhov,
282 Tscal *rhoe,
283 Tscal *rho_d,
284 Tvec *rhov_d,
285 Tscal *exp_scratch_ptr_base) {
286 auto cell = make_cell(
287 rho_next,
288 rhov_next,
289 rhoe_next,
290 rho_d_next,
291 rhov_d_next,
292 alphas,
293 rho,
294 rhov,
295 rhoe,
296 rho_d,
297 rhov_d);
298
299 Tscal *ptr_base = exp_scratch_ptr_base + (id_a * 5 * mat_size_squared);
300
301 cell(
302 id_a,
303 ptr_base,
304 ptr_base + mat_size_squared,
305 ptr_base + 2 * mat_size_squared,
306 ptr_base + 3 * mat_size_squared,
307 ptr_base + 4 * mat_size_squared);
308 });
309
310 } else {
311
312 sham::kernel_call_hndl(
313 q,
314 in,
316 edges.spans_rho.get_spans().get(id_patch),
317 edges.spans_rhov.get_spans().get(id_patch),
318 edges.spans_rhoe.get_spans().get(id_patch),
319 edges.spans_rho_d.get_spans().get(id_patch),
320 edges.spans_rhov_d.get_spans().get(id_patch)},
321 cell_count,
322 [mat_size_squared, loc_acc_size, group_size, make_cell](
323 u32 n,
324 const Tscal *rho_next,
325 const Tvec *rhov_next,
326 const Tscal *rhoe_next,
327 const Tscal *rho_d_next,
328 const Tvec *rhov_d_next,
329 const Tscal *alphas,
330 Tscal *rho,
331 Tvec *rhov,
332 Tscal *rhoe,
333 Tscal *rho_d,
334 Tvec *rhov_d) {
335 auto cell = make_cell(
336 rho_next,
337 rhov_next,
338 rhoe_next,
339 rho_d_next,
340 rhov_d_next,
341 alphas,
342 rho,
343 rhov,
344 rhoe,
345 rho_d,
346 rhov_d);
347
348 return [=](sycl::handler &cgh) {
349 // local/shared memory alloc for each work-item
350 sycl::local_accessor<Tscal> local_A(loc_acc_size, cgh);
351 sycl::local_accessor<Tscal> local_B(loc_acc_size, cgh);
352 sycl::local_accessor<Tscal> local_F(loc_acc_size, cgh);
353 sycl::local_accessor<Tscal> local_I(loc_acc_size, cgh);
354 sycl::local_accessor<Tscal> local_Id(loc_acc_size, cgh);
355
356 cgh.parallel_for(
357 shambase::make_range(n, group_size), [=](sycl::nd_item<1> id) {
358 u32 loc_id = id.get_local_id();
359 u32 id_a = id.get_global_id();
360 if (id_a >= n)
361 return;
362
363 cell(
364 id_a,
365 &(local_A[0]) + mat_size_squared * loc_id,
366 &(local_B[0]) + mat_size_squared * loc_id,
367 &(local_F[0]) + mat_size_squared * loc_id,
368 &(local_I[0]) + mat_size_squared * loc_id,
369 &(local_Id[0]) + mat_size_squared * loc_id);
370 });
371 };
372 });
373 }
374 });
375 }
376
377 template<class Tvec>
379 std::string tex = R"tex(
380 Gas-dust drag, matrix exponential integrator
381
382 Update ${spans_rho}$, ${spans_rhov}$, ${spans_rhoe}$, ${spans_rho_d}$, ${spans_rhov_d}$
383 from the state without drag ${spans_rho_next}$, ${spans_rhov_next}$,
384 ${spans_rhoe_next}$, ${spans_rho_d_next}$, ${spans_rhov_d_next}$ over a time step
385 ${dt}$.
386 )tex";
387
388 replace_edges_tex_symbols(tex);
389
390 return tex;
391 }
392
393} // namespace shammodels::basegodunov::modules
394
Node applying the gas-dust drag with the matrix exponential integrator.
Header file describing a Node Instance.
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
std::int32_t i32
32 bit 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.
A SYCL queue associated with a device and a context.
DeviceProperties & get_device_prop()
Retrieves the properties of the associated device.
Apply the gas-dust drag with the matrix exponential integrator.
virtual std::string _impl_get_tex() const
get the tex of the node
This header file contains utility functions related to exception handling in the code.
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.
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
sycl::nd_range< 1 > make_range(u32 length, const u32 group_size=32)
Generate a sycl nd range out of a group size and length.
void mat_mul_scalar(const std::mdspan< T, Extents, Layout, Accessor > &input, const T &scalar)
Multiply a matrix by a scalar value.
void mat_set_nul(const std::mdspan< T, Extents, Layout, Accessor > &input)
Set the content of a matrix to zero.
void mat_axpy_beta(const U alpha, const std::mdspan< T, Extents1, Layout1, Accessor1 > &input, const U beta, const std::mdspan< T, Extents2, Layout2, Accessor2 > &output)
This function compute M = alpha*N + beta*M with M,N both matrices.
void mat_exp(const i32 K, const std::mdspan< T, Extents1, Layout1, Accessor1 > &A, const std::mdspan< T, Extents2, Layout2, Accessor2 > &F, const std::mdspan< T, Extents3, Layout3, Accessor3 > &B, const std::mdspan< T, Extents4, Layout4, Accessor4 > &I, const std::mdspan< T, Extents5, Layout5, Accessor5 > &Id, const size_t size_A)
matrix scaling-squaring Talylor-based matrix exponential
void mat_set_identity(const std::mdspan< T, Extents, Layout, Accessor > &input1)
Set the content of a matrix to the identity matrix.
namespace for the basegodunov model modules
usize local_mem_size
The amount of shared local memory on the device in bytes.
Definition Device.hpp:110
A class that references multiple buffers or similar objects.
Definition MultiRef.hpp:33