Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
ApplyDragIRK1.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
24
26
27 template<class Tvec>
29 auto edges = get_edges();
30
31 edges.spans_rho_next.check_sizes(edges.sizes.indexes);
32 edges.spans_rhov_next.check_sizes(edges.sizes.indexes);
33 edges.spans_rhoe_next.check_sizes(edges.sizes.indexes);
34 edges.spans_rho_d_next.check_sizes(edges.sizes.indexes);
35 edges.spans_rhov_d_next.check_sizes(edges.sizes.indexes);
36 edges.spans_rho.check_sizes(edges.sizes.indexes);
37 edges.spans_rhov.check_sizes(edges.sizes.indexes);
38 edges.spans_rhoe.check_sizes(edges.sizes.indexes);
39 edges.spans_rho_d.check_sizes(edges.sizes.indexes);
40 edges.spans_rhov_d.check_sizes(edges.sizes.indexes);
41
42 Tscal dt = edges.dt.value;
43 u32 ndust = this->ndust;
44 u32 friction_control = (enable_frictional_heating == false) ? 1 : 0;
45
46 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
47
48 sham::DeviceBuffer<Tscal> alphas_buf(ndust, dev_sched);
49 alphas_buf.copy_from_stdvec(alphas);
50
51 edges.sizes.indexes.for_each([&](u64 id_patch, u32 block_count) {
52 u32 cell_count = block_count * block_size;
53
55 dev_sched->get_queue(),
57 edges.spans_rho_next.get_spans().get(id_patch),
58 edges.spans_rhov_next.get_spans().get(id_patch),
59 edges.spans_rhoe_next.get_spans().get(id_patch),
60 edges.spans_rho_d_next.get_spans().get(id_patch),
61 edges.spans_rhov_d_next.get_spans().get(id_patch),
62 alphas_buf},
64 edges.spans_rho.get_spans().get(id_patch),
65 edges.spans_rhov.get_spans().get(id_patch),
66 edges.spans_rhoe.get_spans().get(id_patch),
67 edges.spans_rho_d.get_spans().get(id_patch),
68 edges.spans_rhov_d.get_spans().get(id_patch)},
69 cell_count,
70 [dt, ndust, friction_control](
71 u32 id_a,
72 const Tscal *__restrict rho_next,
73 const Tvec *__restrict rhov_next,
74 const Tscal *__restrict rhoe_next,
75 const Tscal *__restrict rho_d_next,
76 const Tvec *__restrict rhov_d_next,
77 const Tscal *__restrict alphas,
78 Tscal *__restrict rho,
79 Tvec *__restrict rhov,
80 Tscal *__restrict rhoe,
81 Tscal *__restrict rho_d,
82 Tvec *__restrict rhov_d) {
83 Tvec tmp_mom_1 = rhov_next[id_a];
84 Tscal tmp_rho = rho[id_a];
85
86 for (u32 i = 0; i < ndust; i++) {
87 const Tscal inv_dt_alphas = 1.0 / (1.0 + alphas[i] * dt);
88 const Tscal dt_alphas = dt * alphas[i];
89
90 tmp_mom_1
91 = tmp_mom_1 + dt_alphas * inv_dt_alphas * rhov_d_next[id_a * ndust + i];
92 tmp_rho = tmp_rho + dt_alphas * inv_dt_alphas * rho_d[id_a * ndust + i];
93 }
94
95 Tscal tmp_inv_rho = 1.0 / tmp_rho;
96 Tvec tmp_vel = tmp_inv_rho * tmp_mom_1;
97 Tscal Eg = 0.0;
98
99 Tscal inv_rho_g = 1.0 / rho_next[id_a];
100 Tvec vg_bf = inv_rho_g * rhov_next[id_a];
101 Tvec vg_af = inv_rho_g * rho[id_a] * tmp_vel;
102
103 Tscal work_drag
104 = 0.5
105 * ((rho[id_a] * tmp_vel[0] - rhov_next[id_a][0]) * (vg_bf[0] + vg_af[0])
106 + (rho[id_a] * tmp_vel[1] - rhov_next[id_a][1]) * (vg_bf[1] + vg_af[1])
107 + (rho[id_a] * tmp_vel[2] - rhov_next[id_a][2])
108 * (vg_bf[2] + vg_af[2]));
109 Tscal dissipation = 0.0;
110 for (u32 i = 0; i < ndust; i++) {
111 const Tscal inv_dt_alphas = 1.0 / (1.0 + alphas[i] * dt);
112 const Tscal dt_alphas = dt * alphas[i];
113 Tscal inv_rho_d = 1.0 / rho_d_next[id_a * ndust + i];
114 Tvec vd_bf = inv_rho_d * rhov_d_next[id_a * ndust + i];
115 Tvec vd_af = inv_rho_d * inv_dt_alphas
116 * (rhov_d_next[id_a * ndust + i]
117 + dt_alphas * rho_d[id_a * ndust + i] * tmp_vel);
118 dissipation += 0.5 * dt_alphas * inv_dt_alphas
119 * ((rho_d[id_a * ndust + i] * tmp_vel[0]
120 - rhov_d_next[id_a * ndust + i][0])
121 * (vd_af[0] + vd_bf[0])
122 + (rho_d[id_a * ndust + i] * tmp_vel[1]
123 - rhov_d_next[id_a * ndust + i][1])
124 * (vd_af[1] + vd_bf[1])
125 + (rho_d[id_a * ndust + i] * tmp_vel[2]
126 - rhov_d_next[id_a * ndust + i][2])
127 * (vd_af[2] + vd_bf[2]));
128 }
129
130 Eg += rhoe_next[id_a] + (1 - friction_control) * work_drag
131 - friction_control * dissipation;
132 rhov[id_a] = tmp_vel * rho[id_a];
133 rhoe[id_a] = Eg;
134 rho[id_a] = rho_next[id_a];
135 for (u32 i = 0; i < ndust; i++) {
136 const Tscal inv_dt_alphas = 1.0 / (1.0 + alphas[i] * dt);
137 const Tscal dt_alphas = dt * alphas[i];
138 rhov_d[id_a * ndust + i]
139 = inv_dt_alphas
140 * (rhov_d_next[id_a * ndust + i]
141 + dt_alphas * rho_d[id_a * ndust + i] * tmp_vel);
142 rho_d[id_a * ndust + i] = rho_d_next[id_a * ndust + i];
143 }
144 });
145 });
146 }
147
148 template<class Tvec>
150 std::string tex = R"tex(
151 Gas-dust drag, first order implicit Runge-Kutta (IRK1)
152
153 Update ${spans_rho}$, ${spans_rhov}$, ${spans_rhoe}$, ${spans_rho_d}$, ${spans_rhov_d}$ from
154 the state without drag ${spans_rho_next}$, ${spans_rhov_next}$, ${spans_rhoe_next}$,
155 ${spans_rho_d_next}$, ${spans_rhov_d_next}$ over a time step ${dt}$.
156 )tex";
157
158 replace_edges_tex_symbols(tex);
159
160 return tex;
161 }
162
163} // namespace shammodels::basegodunov::modules
164
Node applying the gas-dust drag with the first order implicit Runge-Kutta scheme (IRK1).
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.
Apply the gas-dust drag with the first order implicit Runge-Kutta scheme (IRK1).
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.
namespace for the basegodunov model modules
A class that references multiple buffers or similar objects.
Definition MultiRef.hpp:33