Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
SlopeLimitedGradientUtilities.hpp
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
10#pragma once
11
19
20#include "shamcomm/logs.hpp"
21#include "shammath/riemann.hpp"
25#include <type_traits>
26
27namespace {
29 using Direction = shammodels::basegodunov::modules::Direction;
30
31 template<class T>
32 inline T slope_function_van_leer_f_form(T sL, T sR) {
33 T st = sL + sR;
34
35 auto vanleer = [](T f) {
36 return 4. * f * (1. - f);
37 };
38
39 auto slopelim = [&](T f) {
40 if constexpr (std::is_same_v<T, f64_3>) {
41 f.x() = (f.x() >= 0 && f.x() <= 1) ? f.x() : 0;
42 f.y() = (f.y() >= 0 && f.y() <= 1) ? f.y() : 0;
43 f.z() = (f.z() >= 0 && f.z() <= 1) ? f.z() : 0;
44 } else {
45 f = (f >= 0 && f <= 1) ? f : 0;
46 }
47 return vanleer(f);
48 };
49
50 return slopelim(sL / st) * st * 0.5;
51 }
52
53 template<class T>
54 inline T slope_function_van_leer_symetric(T sL, T sR) {
55
56 if constexpr (std::is_same_v<T, f64_3>) {
57 return {
58 shammath::van_leer_slope_symetric(sL[0], sR[0]),
59 shammath::van_leer_slope_symetric(sL[1], sR[1]),
60 shammath::van_leer_slope_symetric(sL[2], sR[2])};
61 } else {
62 return shammath::van_leer_slope_symetric(sL, sR);
63 }
64 }
65
66 template<class T>
67 inline T slope_function_van_leer_standard(T sL, T sR) {
68
69 if constexpr (std::is_same_v<T, f64_3>) {
70 return {
71 shammath::van_leer_slope(sL[0], sR[0]),
72 shammath::van_leer_slope(sL[1], sR[1]),
73 shammath::van_leer_slope(sL[2], sR[2])};
74 } else {
75 return shammath::van_leer_slope(sL, sR);
76 }
77 }
78
79 template<class T>
80 inline T slope_function_minmod(T sL, T sR) {
81
82 if constexpr (std::is_same_v<T, f64_3>) {
83 return {
84 shammath::minmod(sL[0], sR[0]),
85 shammath::minmod(sL[1], sR[1]),
86 shammath::minmod(sL[2], sR[2])};
87 } else {
88 return shammath::minmod(sL, sR);
89 }
90 }
91
93
94 template<class T, SlopeMode mode>
95 inline T slope_function(T sL, T sR) {
96 if constexpr (mode == SlopeMode::None) {
97 return sham::VectorProperties<T>::get_zero();
98 }
99
100 if constexpr (mode == SlopeMode::VanLeer_f) {
101 return slope_function_van_leer_f_form(sL, sR);
102 }
103
104 if constexpr (mode == SlopeMode::VanLeer_std) {
105 return slope_function_van_leer_standard(sL, sR);
106 }
107
108 if constexpr (mode == SlopeMode::VanLeer_sym) {
109 return slope_function_van_leer_symetric(sL, sR);
110 }
111
112 if constexpr (mode == SlopeMode::Minmod) {
113 return slope_function_minmod(sL, sR);
114 }
115 }
116
135 template<class Tfield, class Tvec, SlopeMode mode, class ACCField>
136 inline std::array<Tfield, 3> get_3d_grad(
137 const f64 *cell_sizes,
138 const u32 block_size,
139 const u32 cell_global_id,
140 const AMRGraphLinkiterator &graph_iter_xp,
141 const AMRGraphLinkiterator &graph_iter_xm,
142 const AMRGraphLinkiterator &graph_iter_yp,
143 const AMRGraphLinkiterator &graph_iter_ym,
144 const AMRGraphLinkiterator &graph_iter_zp,
145 const AMRGraphLinkiterator &graph_iter_zm,
146 ACCField &&field_access) {
147
148 auto cur_cell_block_id = cell_global_id / block_size;
149
150 auto get_gradient_dir = [&](auto &graph_links, Direction dir) -> Tfield {
151 Tfield acc = shambase::VectorProperties<Tfield>::get_zero();
152 auto cell_center_dist = cell_sizes[cur_cell_block_id];
153
154 u32 cnt = graph_links.for_each_object_link_cnt(cell_global_id, [&](u32 id_b) {
155 auto neigh_block_id = id_b / block_size;
156 auto fac = 1.;
157 if (cell_sizes[neigh_block_id] > cell_sizes[cur_cell_block_id]) {
158 fac = (3. / 2.);
159 }
160 // This logic suppose that the last (4-th) cell at interface have same size with the
161 // other three cells. This is also consitent with 2:1 refinement.
162 // TODO: extended to anisotropic mesh
163 if (cell_sizes[neigh_block_id] < cell_sizes[cur_cell_block_id]) {
164 fac = (3. / 4.);
165 }
166 const auto inv_dist = 1. / (fac * cell_center_dist);
167
168 int sign = 1 - 2 * (dir % 2);
169 acc += sign * inv_dist * (field_access(id_b) - field_access(cell_global_id));
170 });
171 return (cnt > 0) ? acc / cnt : shambase::VectorProperties<Tfield>::get_zero();
172 };
173
174 return {
175 slope_function<Tfield, mode>(
176 get_gradient_dir(graph_iter_xm, Direction::xm),
177 get_gradient_dir(graph_iter_xp, Direction::xp)),
178 slope_function<Tfield, mode>(
179 get_gradient_dir(graph_iter_ym, Direction::ym),
180 get_gradient_dir(graph_iter_yp, Direction::yp)),
181 slope_function<Tfield, mode>(
182 get_gradient_dir(graph_iter_zm, Direction::zm),
183 get_gradient_dir(graph_iter_zp, Direction::zp))};
184 }
185
209 template<class Tvec, SlopeMode mode, class ACCField1, class ACCField2, class ACCField3>
210 inline std::array<shammath::ConsState<Tvec>, 3> get_3d_grad_cons(
211 const f64 *cell_sizes,
212 const u32 block_size,
213 const u32 cell_global_id,
214 const AMRGraphLinkiterator &graph_iter_xp,
215 const AMRGraphLinkiterator &graph_iter_xm,
216 const AMRGraphLinkiterator &graph_iter_yp,
217 const AMRGraphLinkiterator &graph_iter_ym,
218 const AMRGraphLinkiterator &graph_iter_zp,
219 const AMRGraphLinkiterator &graph_iter_zm,
220 ACCField1 &&field_access_rho,
221 ACCField2 &&field_access_rho_vel,
222 ACCField3 &&field_access_rhoe) {
223
224 using Tscal = shambase::VecComponent<Tvec>;
225 auto cur_cell_block_id = cell_global_id / block_size;
226 auto get_gradient_dir = [&](auto &graph_links, Direction dir) -> shammath::ConsState<Tvec> {
227 Tscal acc_rho = shambase::VectorProperties<Tscal>::get_zero();
228 Tscal acc_rhoe = shambase::VectorProperties<Tscal>::get_zero();
229 Tvec acc_rho_vel = shambase::VectorProperties<Tvec>::get_zero();
230 auto cell_center_dist = cell_sizes[cur_cell_block_id];
231
232 auto cnt = graph_links.for_each_object_link_cnt(cell_global_id, [&](u32 id_b) {
233 auto neigh_block_id = id_b / block_size;
234 auto fac = 1.;
235 if (cell_sizes[neigh_block_id] > cell_sizes[cur_cell_block_id]) {
236 fac = (3. / 2.);
237 }
238 // This logic suppose that the last (4-th) cell at interface have same size with the
239 // other three cells. This is also consitent with 2:1 refinement.
240 // TODO: extended to anisotropic mesh
241 if (cell_sizes[neigh_block_id] < cell_sizes[cur_cell_block_id]) {
242 fac = (3. / 4.);
243 }
244 const auto inv_dist = 1. / (fac * cell_center_dist);
245
246 int sign = 1 - 2 * (dir % 2);
247 acc_rho += sign * inv_dist
248 * (field_access_rho(id_b) - field_access_rho(cell_global_id));
249 acc_rhoe += sign * inv_dist
250 * (field_access_rhoe(id_b) - field_access_rhoe(cell_global_id));
251 acc_rho_vel
252 += sign * inv_dist
253 * (field_access_rho_vel(id_b) - field_access_rho_vel(cell_global_id));
254 });
255
257 = {shambase::VectorProperties<Tscal>::get_zero(),
258 shambase::VectorProperties<Tscal>::get_zero(),
259
260 {shambase::VectorProperties<Tscal>::get_zero(),
261 shambase::VectorProperties<Tscal>::get_zero(),
262 shambase::VectorProperties<Tscal>::get_zero()}};
263 if (cnt > 0) {
264 res = {acc_rho, acc_rhoe, acc_rho_vel};
265 res *= 1. / cnt;
266 }
267 return res;
268 };
269 shammath::ConsState<Tvec> delta_xp = get_gradient_dir(graph_iter_xp, Direction::xp);
270 shammath::ConsState<Tvec> delta_xm = get_gradient_dir(graph_iter_xm, Direction::xm);
271 shammath::ConsState<Tvec> delta_yp = get_gradient_dir(graph_iter_yp, Direction::yp);
272 shammath::ConsState<Tvec> delta_ym = get_gradient_dir(graph_iter_ym, Direction::ym);
273 shammath::ConsState<Tvec> delta_zp = get_gradient_dir(graph_iter_zp, Direction::zp);
274 shammath::ConsState<Tvec> delta_zm = get_gradient_dir(graph_iter_zm, Direction::zm);
275
276 shammath::ConsState<Tvec> lim_slope_x
277 = {slope_function<Tscal, mode>(delta_xm.rho, delta_xp.rho),
278 slope_function<Tscal, mode>(delta_xm.rhoe, delta_xp.rhoe),
279 slope_function<Tvec, mode>(delta_xm.rhovel, delta_xp.rhovel)};
280
281 shammath::ConsState<Tvec> lim_slope_y
282 = {slope_function<Tscal, mode>(delta_ym.rho, delta_yp.rho),
283 slope_function<Tscal, mode>(delta_ym.rhoe, delta_yp.rhoe),
284 slope_function<Tvec, mode>(delta_ym.rhovel, delta_yp.rhovel)};
285
286 shammath::ConsState<Tvec> lim_slope_z
287 = {slope_function<Tscal, mode>(delta_zm.rho, delta_zp.rho),
288 slope_function<Tscal, mode>(delta_zm.rhoe, delta_zp.rhoe),
289 slope_function<Tvec, mode>(delta_zm.rhovel, delta_zp.rhovel)};
290
291 return {lim_slope_x, lim_slope_y, lim_slope_z};
292 }
293
297 template<class T, class Tvec, class ACCField>
298 inline T get_pseudo_grad(
299 const u32 cell_global_id,
300 const AMRGraphLinkiterator &graph_iter_xp,
301 const AMRGraphLinkiterator &graph_iter_xm,
302 const AMRGraphLinkiterator &graph_iter_yp,
303 const AMRGraphLinkiterator &graph_iter_ym,
304 const AMRGraphLinkiterator &graph_iter_zp,
305 const AMRGraphLinkiterator &graph_iter_zm,
306 ACCField &&field_access)
307
308 {
309
310 using namespace sham;
311 using namespace sham::details;
312
313 auto get_avg_neigh = [&](auto &graph_links, u32 dir) -> T {
314 T acc = shambase::VectorProperties<T>::get_zero();
315 u32 cnt = graph_links.for_each_object_link_cnt(cell_global_id, [&](u32 id_b) {
316 acc += field_access(id_b);
317 });
318
319 return (cnt > 0) ? acc / cnt : shambase::VectorProperties<T>::get_zero();
320 };
321
322 auto epsilon = shambase::get_epsilon<T>();
323 T u_cur = field_access(cell_global_id);
324 T u_xp = get_avg_neigh(graph_iter_xp, 0);
325 T u_xm = get_avg_neigh(graph_iter_xm, 1);
326 T u_yp = get_avg_neigh(graph_iter_yp, 2);
327 T u_ym = get_avg_neigh(graph_iter_ym, 3);
328 T u_zp = get_avg_neigh(graph_iter_zp, 4);
329 T u_zm = get_avg_neigh(graph_iter_zm, 5);
330
331 // RAMSES LIKE
332
333 T x_scal = 2
334 * g_sycl_max(
335 g_sycl_abs((u_cur - u_xm) / (epsilon + u_cur + u_xm)),
336 g_sycl_abs((u_cur - u_xp) / (epsilon + u_cur + u_xp)));
337
338 T y_scal = 2
339 * g_sycl_max(
340 g_sycl_abs((u_cur - u_ym) / (epsilon + u_cur + u_ym)),
341 g_sycl_abs((u_cur - u_yp) / (epsilon + u_cur + u_yp)));
342 T z_scal = 2
343 * g_sycl_max(
344 g_sycl_abs((u_cur - u_zm) / (epsilon + u_cur + u_zm)),
345 g_sycl_abs((u_cur - u_zp) / (epsilon + u_cur + u_zp)));
346
347 T res = g_sycl_max(x_scal, g_sycl_max(y_scal, z_scal));
348 return res;
349 }
350
353 template<class T, class ACCField>
354 inline T baryonic_normalized_slope_criterion(
355 const u32 cell_global_id,
356 const AMRGraphLinkiterator &graph_iter_xp,
357 const AMRGraphLinkiterator &graph_iter_xm,
358 const AMRGraphLinkiterator &graph_iter_yp,
359 const AMRGraphLinkiterator &graph_iter_ym,
360 const AMRGraphLinkiterator &graph_iter_zp,
361 const AMRGraphLinkiterator &graph_iter_zm,
362 ACCField &&field_access)
363
364 {
365
366 using namespace sham;
367 using namespace sham::details;
368
369 auto get_avg_neigh = [&](auto &graph_links, u32 dir) -> T {
370 T acc = shambase::VectorProperties<T>::get_zero();
371 u32 cnt = graph_links.for_each_object_link_cnt(cell_global_id, [&](u32 id_b) {
372 acc += field_access(id_b);
373 });
374
375 return (cnt > 0) ? acc / cnt : shambase::VectorProperties<T>::get_zero();
376 };
377
378 auto epsilon = shambase::get_epsilon<T>();
379 T u_cur = field_access(cell_global_id);
380 T u_xp = get_avg_neigh(graph_iter_xp, 0);
381 T u_xm = get_avg_neigh(graph_iter_xm, 1);
382 T u_yp = get_avg_neigh(graph_iter_yp, 2);
383 T u_ym = get_avg_neigh(graph_iter_ym, 3);
384 T u_zp = get_avg_neigh(graph_iter_zp, 4);
385 T u_zm = get_avg_neigh(graph_iter_zm, 5);
386
387 T norm_slope_x = g_sycl_abs((u_xm - u_xp) / (2 * u_cur + epsilon));
388 T norm_slope_y = g_sycl_abs((u_ym - u_yp) / (2 * u_cur + epsilon));
389 T norm_slope_z = g_sycl_abs((u_zm - u_zp) / (2 * u_cur + epsilon));
390
391 T res = g_sycl_max(norm_slope_x, g_sycl_max(norm_slope_y, norm_slope_z));
392
393 return res;
394 }
395
396 /***
397 * Lohner second order criterion
398 */
399 template<class T, class Tvec, class ACCField>
400 inline T modif_second_derivative(
401 const u32 cell_global_id,
402 const AMRGraphLinkiterator &graph_iter_xp,
403 const AMRGraphLinkiterator &graph_iter_xm,
404 const AMRGraphLinkiterator &graph_iter_yp,
405 const AMRGraphLinkiterator &graph_iter_ym,
406 const AMRGraphLinkiterator &graph_iter_zp,
407 const AMRGraphLinkiterator &graph_iter_zm,
408 ACCField &&field_access) {
409 using namespace sham;
410 using namespace sham::details;
411
412 auto get_avg_neigh = [&](auto &graph_links) -> T {
413 T acc = shambase::VectorProperties<T>::get_zero();
414 u32 cnt = graph_links.for_each_object_link_cnt(cell_global_id, [&](u32 id_b) {
415 acc += field_access(id_b);
416 });
417 return (cnt > 0) ? acc / cnt : shambase::VectorProperties<T>::get_zero();
418 };
419
420 auto eps_ref = 0.01;
421 auto epsilon = shambase::get_epsilon<T>();
422 T u_cur = field_access(cell_global_id);
423 T u_xp = get_avg_neigh(graph_iter_xp);
424 T u_xm = get_avg_neigh(graph_iter_xm);
425 T u_yp = get_avg_neigh(graph_iter_yp);
426 T u_ym = get_avg_neigh(graph_iter_ym);
427 T u_zp = get_avg_neigh(graph_iter_zp);
428 T u_zm = get_avg_neigh(graph_iter_zm);
429
430 T delta_u_xp = u_xp - u_cur;
431 T delta_u_xm = u_xm - u_cur;
432 T delta_u_yp = u_yp - u_cur;
433 T delta_u_ym = u_ym - u_cur;
434 T delta_u_zp = u_zp - u_cur;
435 T delta_u_zm = u_zm - u_cur;
436
437 T scalar_x = g_sycl_abs(u_xp) + g_sycl_abs(u_xm) + 2 * g_sycl_abs(u_cur);
438 T scalar_y = g_sycl_abs(u_yp) + g_sycl_abs(u_ym) + 2 * g_sycl_abs(u_cur);
439 T scalar_z = g_sycl_abs(u_zp) + g_sycl_abs(u_zm) + 2 * g_sycl_abs(u_cur);
440
441 T res_x
442 = g_sycl_abs(delta_u_xm + delta_u_xp)
443 / (g_sycl_abs(delta_u_xm) + g_sycl_abs(delta_u_xp) + eps_ref * scalar_x + epsilon);
444 T res_y
445 = g_sycl_abs(delta_u_ym + delta_u_yp)
446 / (g_sycl_abs(delta_u_ym) + g_sycl_abs(delta_u_yp) + eps_ref * scalar_y + epsilon);
447 T res_z
448 = g_sycl_abs(delta_u_zm + delta_u_zp)
449 / (g_sycl_abs(delta_u_zm) + g_sycl_abs(delta_u_zp) + eps_ref * scalar_z + epsilon);
450
451 return (res_x + res_y + res_z);
452 }
453
457 template<class Tvec, class ACCField>
458 inline shambase::VecComponent<Tvec> normalized_shear(
459 const u32 cell_global_id,
460 const f32 sound_speed,
461 const Tvec delta_cells,
462 const AMRGraphLinkiterator &graph_iter_xp,
463 const AMRGraphLinkiterator &graph_iter_xm,
464 const AMRGraphLinkiterator &graph_iter_yp,
465 const AMRGraphLinkiterator &graph_iter_ym,
466 const AMRGraphLinkiterator &graph_iter_zp,
467 const AMRGraphLinkiterator &graph_iter_zm,
468 ACCField &&field_access)
469
470 {
471
472 using namespace sham;
473 using namespace sham::details;
474
475 auto get_avg_neigh = [&](auto &graph_links, u32 dir) -> Tvec {
476 Tvec acc = shambase::VectorProperties<Tvec>::get_zero();
477 u32 cnt = graph_links.for_each_object_link_cnt(cell_global_id, [&](u32 id_b) {
478 acc += field_access(id_b);
479 });
480
481 return (cnt > 0) ? acc / cnt : shambase::VectorProperties<Tvec>::get_zero();
482 };
483
484 Tvec u_xp = get_avg_neigh(graph_iter_xp, 0);
485 Tvec u_xm = get_avg_neigh(graph_iter_xm, 1);
486 Tvec u_yp = get_avg_neigh(graph_iter_yp, 2);
487 Tvec u_ym = get_avg_neigh(graph_iter_ym, 3);
488 Tvec u_zp = get_avg_neigh(graph_iter_zp, 4);
489 Tvec u_zm = get_avg_neigh(graph_iter_zm, 5);
490
491 auto vgy = 0.25 * (u_xp[1] - u_xm[1]) * (u_xp[1] - u_xm[1]);
492 auto vgx = 0.25 * (u_yp[0] - u_ym[0]) * (u_yp[0] - u_ym[0]);
493
494 return vgy + vgx;
495
499 // auto dv_xdir = 0.5 * sycl::abs(u_xp - u_xm);
500 // auto dv_ydir = 0.5 * sycl::abs(u_yp - u_ym);
501 // auto dv_zdir = 0.5 * sycl::abs(u_zp - u_zm) ;
502 // auto shear_1 = (dv_ydir[0] + dv_xdir[1])*(dv_ydir[0] + dv_xdir[1]);
503 // auto shear_2 = (dv_ydir[2] + dv_zdir[1]) * (dv_ydir[2] + dv_zdir[1]);
504 // auto shear_3 = (dv_zdir[0] + dv_xdir[2]) * (dv_zdir[0] + dv_xdir[2]);
505 // return (shear_1 + shear_2 + shear_3) * (delta_cells.x() * delta_cells.x())/(sound_speed
506 // * sound_speed);
507 }
508
509} // namespace
double f64
Alias for double.
float f32
Alias for float.
std::uint32_t u32
32 bit unsigned integer
namespace for backends this one is named only sham since shambackends is too long to write
T van_leer_slope(T f, T g)
Van leer slope limiter.
SlopeMode
Slope limiter modes.
Umbrella header pulling in the gas states and all gas Riemann solvers (Rusanov, HLL,...
Math header to compute slope limiters.