34 using Tscal = shambase::VecComponent<Tvec>;
36 = std::mdspan<Tscal, std::extents<size_t, std::dynamic_extent, std::dynamic_extent>>;
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;
56 inline void operator()(
57 u32 id_a, Tscal *ptr_A, Tscal *ptr_B, Tscal *ptr_F, Tscal *ptr_I, Tscal *ptr_Id)
const {
60 auto get_jacobian = [&](
u32 id, mdspan_t &jacobian) {
63 for (
auto j = 1; j < jacobian.extent(1); j++)
64 jacobian(0, j) = alphas[j - 1];
66 for (
auto i = 1; i < jacobian.extent(0); i++) {
68 = alphas[i - 1] * (rho_d_next[
id * ndust + (i - 1)] / rho_next[id]);
69 jacobian(0, 0) -= jacobian(i, 0);
72 for (
auto i = 1; i < jacobian.extent(0); i++)
73 jacobian(i, i) = -alphas[i - 1];
78 for (
auto i = 0; i < ndust; i++) {
79 mu += (1 + (rho_d_next[id_a * ndust + i] / rho_next[id_a])) * alphas[i];
81 mu *= (-dt / (ndust + 1));
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);
90 get_jacobian(id_a, mdspan_A);
99 K_exp, mdspan_A, mdspan_F, mdspan_B, mdspan_I, mdspan_Id, ndust + 1);
105 Tvec r = {0., 0., 0.}, dd = {0., 0., 0.};
106 r += mdspan_A(0, 0) * rhov_next[id_a];
108 for (
auto j = 1; j < ndust + 1; j++) {
109 r += mdspan_A(0, j) * rhov_d_next[id_a * ndust + (j - 1)];
112 dd = r - rhov_next[id_a];
114 Tscal dissipation = 0, drag_work = 0;
117 Tscal inv_rho = 1.0 / (rho_next[id_a]);
119 Tvec v_bf = inv_rho * rhov_next[id_a];
120 Tvec v_af = inv_rho * r;
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]));
128 rho[id_a] = rho_next[id_a];
130 for (
auto d_id = 1; d_id <= ndust; d_id++) {
132 r += mdspan_A(d_id, 0) * rhov_next[id_a];
134 for (
auto j = 1; j <= ndust; j++) {
136 r += mdspan_A(d_id, j) * rhov_d_next[id_a * ndust + (j - 1)];
139 dd = r - rhov_d_next[id_a * ndust + (d_id - 1)];
141 inv_rho = 1.0 / (rho_d_next[id_a * ndust + (d_id - 1)]);
143 v_bf = inv_rho * rhov_d_next[id_a * ndust + (d_id - 1)];
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]));
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)];
158 rhoe[id_a] = rhoe_next[id_a] + (1 - friction_control) * drag_work
159 - friction_control * dissipation;
169 auto edges = get_edges();
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);
182 Tscal dt = edges.dt.value;
183 u32 ndust = this->ndust;
184 u32 friction_control = (enable_frictional_heating ==
false) ? 1 : 0;
186 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
192 size_t mat_size = ndust + 1;
193 size_t mat_size_squared = mat_size * mat_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;
198 size_t loc_mem_size = 5 *
sizeof(Tscal) * loc_acc_size;
200 bool use_global_mem = group_size < 8;
204 "not enough local memory for expo drag integrator:\n"
205 "loc_mem_size: {} > max_local_mem: {}\n"
216 edges.sizes.indexes.for_each([&](
u64 id_patch,
u32 block_count) {
217 u32 cell_count = block_count * block_size;
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),
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,
239 return ExpoDragCell<Tvec>{
257 if (use_global_mem) {
259 5 * mat_size_squared * cell_count, dev_sched);
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),
272 [mat_size_squared, make_cell](
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,
285 Tscal *exp_scratch_ptr_base) {
286 auto cell = make_cell(
299 Tscal *ptr_base = exp_scratch_ptr_base + (id_a * 5 * mat_size_squared);
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);
312 sham::kernel_call_hndl(
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)},
322 [mat_size_squared, loc_acc_size, group_size, make_cell](
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,
335 auto cell = make_cell(
348 return [=](sycl::handler &cgh) {
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);
358 u32 loc_id =
id.get_local_id();
359 u32 id_a =
id.get_global_id();
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);
379 std::string tex = R
"tex(
380 Gas-dust drag, matrix exponential integrator
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
388 replace_edges_tex_symbols(tex);
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.
void _impl_evaluate_internal()
evaluate the node
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.
A class that references multiple buffers or similar objects.