Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
NodeUpdateDerivsMonofluidTVA.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/string.hpp"
22#include "shamrock/patch/PatchDataField.hpp" // IWYU pragma: keep
23
36template<class Tscal>
37inline Tscal monofluid_tva_pair_gate(Tscal s_a, Tscal s_b, Tscal g_ab) {
38 Tscal s_up = (g_ab > 0) ? s_a : s_b; // donor
39 return (s_up < 0) ? Tscal{0} : Tscal{1};
40}
41
52template<class Tvec, template<class> class SPHKernel, bool pair_gate>
54 using Tscal = shambase::VecComponent<Tvec>;
55 using Kernel = SPHKernel<Tscal>;
56 static constexpr Tscal Rkern = Kernel::Rkern;
57 static constexpr Tscal Rker2 = Rkern * Rkern;
58
59 Tscal pmass;
60 u32 ndust;
61
62 inline void operator()(
63 u32 thread_id,
64 // input
65 const Tvec *__restrict xyz,
66 const Tscal *__restrict hpart,
67 const Tvec *__restrict vxyz,
68 const Tscal *__restrict omega,
69 const Tscal *__restrict pressure,
70 const Tscal *__restrict s_j,
71 const Tscal *__restrict Ttilde_sj,
73 // output
74 Tscal *__restrict ds_j_dt) const {
75
76 u32 id_a = thread_id / ndust;
77 u32 jdust = thread_id % ndust;
78
79 Tscal h_a = hpart[id_a];
80 Tvec xyz_a = xyz[id_a];
81 Tvec vxyz_a = vxyz[id_a];
82 Tscal P_a = pressure[id_a];
83 Tscal omega_a = omega[id_a];
84 Tscal s_j_a = s_j[thread_id];
85 Tscal Ttilde_sj_a = Ttilde_sj[thread_id];
86
87 using namespace shamrock::sph;
88 Tscal rho_a = rho_h(pmass, h_a, Kernel::hfactd);
89
90 Tscal term1 = 0;
91 Tscal term2 = 0;
92
93 shamrock::tree::ObjectCacheIterator particle_looper(ploop_ptrs);
94 particle_looper.for_each_object(id_a, [&](u32 id_b) {
95 Tvec dr = xyz_a - xyz[id_b];
96 Tscal rab2 = sycl::dot(dr, dr);
97 Tscal h_b = hpart[id_b];
98
99 if (rab2 > h_a * h_a * Rker2 && rab2 > h_b * h_b * Rker2) {
100 return;
101 }
102
103 Tvec vxyz_b = vxyz[id_b];
104 Tscal P_b = pressure[id_b];
105 Tscal s_j_b = s_j[id_b * ndust + jdust];
106 Tscal Ttilde_sj_b = Ttilde_sj[id_b * ndust + jdust];
107
108 Tscal rab = sycl::sqrt(rab2);
109 Tscal rab_inv_sat = sham::inv_sat_positive(rab);
110
111 Tscal rho_b = rho_h(pmass, h_b, Kernel::hfactd);
112
113 Tscal Fab_a = Kernel::dW_3d(rab, h_a);
114 Tscal Fab_b = Kernel::dW_3d(rab, h_b);
115
116 Tvec v_ab = vxyz_a - vxyz_b;
117
118 Tvec r_ab_unit = dr * rab_inv_sat;
119
120 Tscal F_ab_bar = (Fab_a + Fab_b) / 2;
121 Tscal delta_P = P_a - P_b;
122 Tscal Ts_weighted = (Ttilde_sj_a / rho_a + Ttilde_sj_b / rho_b);
123
124 Tscal g_ab = Ts_weighted * delta_P * F_ab_bar * rab_inv_sat;
125
126 if constexpr (pair_gate) {
127 Tscal gate = monofluid_tva_pair_gate(s_j_a, s_j_b, g_ab);
128 term1 += gate * (pmass * s_j_b / rho_b) * g_ab;
129 } else {
130 term1 += (pmass * s_j_b / rho_b) * g_ab;
131 }
132 term2 += pmass * sham::dot(v_ab, r_ab_unit * Fab_a);
133 });
134
135 // eq 51, Hutchison 2018
136 ds_j_dt[thread_id] = Tscal{-0.5} * term1 + (s_j_a / (2 * rho_a * omega_a)) * term2;
137 }
138};
139
140template<class Tvec, template<class> class SPHKernel>
143
145
146 auto edges = get_edges();
147
148 auto &part_counts_with_ghost = edges.part_counts_with_ghost.indexes;
149 auto &part_counts = edges.part_counts.indexes;
150
151 // check that all input edges have the particles with ghosts zones
152 edges.xyz.check_sizes(part_counts_with_ghost);
153 edges.hpart.check_sizes(part_counts_with_ghost);
154 edges.vxyz.check_sizes(part_counts_with_ghost);
155 edges.omega.check_sizes(part_counts_with_ghost);
156 edges.pressure.check_sizes(part_counts_with_ghost);
157 edges.s_j.check_sizes(part_counts_with_ghost);
158 edges.Ttilde_sj.check_sizes(part_counts_with_ghost);
159
160 // ensure that the output edges are of size part_counts (output without ghosts zones)
161 edges.ds_j_dt.ensure_sizes(part_counts);
162
163 const Tscal pmass = edges.gpart_mass.data;
164
165 auto total_specie_count = part_counts.template map<u32>([&](u64 id, u32 count) {
166 return count * ndust;
167 });
168
169 auto launch = [&](auto kernel) {
170 // call the kernel for each patches with part_counts.get(id_patch) threads of patch id_patch
172 shamsys::instance::get_compute_scheduler_ptr(),
174 edges.xyz.get_spans(),
175 edges.hpart.get_spans(),
176 edges.vxyz.get_spans(),
177 edges.omega.get_spans(),
178 edges.pressure.get_spans(),
179 edges.s_j.get_spans(),
180 edges.Ttilde_sj.get_spans(),
181 edges.neigh_cache},
182 sham::DDMultiRef{edges.ds_j_dt.get_spans()},
183 total_specie_count,
184 kernel);
185 };
186
187 if (pair_gate) {
189 } else {
191 }
192}
193
194template<class Tvec, template<class> class SPHKernel>
196 const {
197 const std::string ndust_str = std::to_string(ndust);
198 const std::string kernel_radius_str = std::to_string(kernel_radius);
199
200 std::string tex = R"tex(
201 NodeUpdateDerivsMonofluidTVA (eq. 51, Hutchison 2018)
202
203 For gas particle $a$ and dust bin $j$:
204
205 \begin{align}
206 \rho_a &= \rho_h({gpart_mass}, {hpart}_a) \\
207 \rho_b &= \rho_h({gpart_mass}, {hpart}_b) \\
208 \mathbf{v}_{a,b} &= {vxyz}_a - {vxyz}_b \\
209 \hat{\mathbf{r}}_{a,b} &= \frac{{xyz}_a - {xyz}_b}{\lvert {xyz}_a - {xyz}_b\rvert} \\
210 F_{a,b}^a &= \mathrm{d}W_{3d}(\lvert \mathbf{r}_{a,b}\rvert, {hpart}_a) \\
211 F_{a,b}^b &= \mathrm{d}W_{3d}(\lvert \mathbf{r}_{a,b}\rvert, {hpart}_b) \\
212 \bar{F}_{a,b} &= \frac{F_{a,b}^a + F_{a,b}^b}{2} \\
213 T^{\rm w}_{j,a,b} &= \frac{{Ttilde_sj}_{j,a}}{\rho_a} + \frac{{Ttilde_sj}_{j,b}}{\rho_b}
214 \\
215 {ds_j_dt}_{j,a} &=
216 -\frac{1}{2}\sum_{b \in \mathcal{N}(a)}
217 {gate_symbol}{gpart_mass}\;
218 \frac{{s_j}_{j,b}}{\rho_b}\;
219 T^{\rm w}_{j,a,b}\;
220 ({pressure}_a - {pressure}_b)\;
221 \bar{F}_{a,b}
222 \\
223 &\quad +
224 \frac{{s_j}_{j,a}}{2\rho_a\,{omega}_a}
225 \sum_{b \in \mathcal{N}(a)}
226 {gpart_mass}\;
227 \mathbf{v}_{a,b}\cdot\left(\hat{\mathbf{r}}_{a,b} F_{a,b}^a\right)
228 \end{align}
229
230 with the neighbor set $\mathcal{N}(a)$ defined by the kernel support:
231 $\lvert \mathbf{r}_{a,b}\rvert \le \max({hpart}_a,{hpart}_b)\,{Rkern}$.
232
233 {gate_def}
234
235 $a \in [0,{part_counts})$, $j \in [0,{ndust})$.
236 )tex";
237
238 const std::string gate_symbol = pair_gate ? R"tex(G_{j,a,b}\;)tex" : "";
239 const std::string gate_def = pair_gate ? R"tex(
240 Symmetrized per-pair (upwind) positivity gate, with
241 $g_{j,a,b} = T^{\rm w}_{j,a,b} ({pressure}_a - {pressure}_b) \bar{F}_{a,b} / \lvert \mathbf{r}_{a,b}\rvert$:
242 the pair carries ${s_j}$ from $a$ to $b$ if $g_{j,a,b} > 0$ (from $b$ to $a$ otherwise),
243 and is switched off when its upwind (donor) particle has ${s_j} < 0$:
244 \begin{align}
245 G_{j,a,b} &= \begin{cases}
246 0 & {s_j}_{j,\mathrm{up}} < 0 \\
247 1 & \text{otherwise}
248 \end{cases},
249 \qquad \mathrm{up} = \begin{cases} a & g_{j,a,b} > 0 \\ b & \text{otherwise} \end{cases}
250 \end{align}
251 )tex"
252 : "";
253
254 shambase::replace_all(tex, "{gate_symbol}", gate_symbol);
255 shambase::replace_all(tex, "{gate_def}", gate_def);
256
257 replace_edges_tex_symbols(tex);
258
259 shambase::replace_all(tex, "{ndust}", ndust_str);
260 shambase::replace_all(tex, "{Rkern}", kernel_radius_str);
261
262 return tex;
263}
264
265using namespace shammath;
269
Tscal monofluid_tva_pair_gate(Tscal s_a, Tscal s_b, Tscal g_ab)
Symmetrized per-pair (upwind) gate of the smooth positivity limiter.
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
virtual std::string _impl_get_tex() const
get the tex of the node
T inv_sat_positive(T v, T minvsat=T{1e-9}, T satval=T{0.}) noexcept
inverse saturated (positive numbers only)
Definition math.hpp:841
void distributed_data_kernel_call(sham::DeviceScheduler_ptr dev_sched, RefIn in, RefOut in_out, const shambase::DistributedData< index_t > &thread_counts, Functor &&func)
A variant of sham::kernel_call for distributed data.
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
namespace for math utility
Definition AABB.hpp:26
sph kernels
#define __shamrock_stack_entry()
Macro to create a stack entry.
TVA ds_j/dt kernel (eq. 51, Hutchison 2018), optionally with the pair gate.
A variant of sham::MultiRef for distributed data.