Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
CGLaplacianStencil.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
20
21#include "shambackends/vec.hpp"
22#include "shamcomm/logs.hpp"
24#include <shambackends/sycl.hpp>
25
28 using Direction = shammodels::basegodunov::modules::Direction;
29
68 template<class T, class Tvec, class ACCField>
70 const shambase::VecComponent<Tvec> *cell_sizes,
71 const u32 block_size,
72 const u32 cell_global_id,
73 const AMRGraphLinkiterator &graph_iter_xp,
74 const AMRGraphLinkiterator &graph_iter_xm,
75 const AMRGraphLinkiterator &graph_iter_yp,
76 const AMRGraphLinkiterator &graph_iter_ym,
77 const AMRGraphLinkiterator &graph_iter_zp,
78 const AMRGraphLinkiterator &graph_iter_zm,
79 ACCField &&field_access,
80 T &Aii) {
81
82 auto cur_cell_block_id = cell_global_id / block_size;
83 Aii = shambase::VectorProperties<T>::get_zero();
84
85 auto get_gradient_dir_by_flux_formulation = [&](auto &graph_links, Direction dir) -> T {
86 T acc = shambase::VectorProperties<T>::get_zero();
87 auto cell_center_dist = cell_sizes[cur_cell_block_id];
88
89 auto fac = 1.;
90 u32 cnt = graph_links.for_each_object_link_cnt(cell_global_id, [&](u32 id_b) {
91 auto neigh_block_id = id_b / block_size;
92
93 int sign = 1 - 2 * (dir % 2);
94 acc += sign * (field_access(id_b) - field_access(cell_global_id));
95
96 if (cell_sizes[neigh_block_id] > cell_sizes[cur_cell_block_id]) {
97 fac = (3. / 2.);
98 }
99 // This logic supposes that the last (4-th) cell at the interface has the same size
100 // as the other three cells. This is consistent with 2:1 refinement.
101 // TODO: extend to anisotropic mesh
102 if (cell_sizes[neigh_block_id] < cell_sizes[cur_cell_block_id]) {
103 fac = (3. / 4.);
104 }
105 });
106
107 // TODO: (kept from #1942) this is added even when cnt == 0, whereas the flux of a
108 // direction without neighbours is zero, so Aii is then larger than the actual diagonal
109 // coefficient of the matrix (only matters for non periodic boundaries).
110 Aii += 1. / (cell_center_dist * fac);
111 return (cnt > 0) ? acc / (cell_center_dist * fac * cnt)
112 : shambase::VectorProperties<T>::get_zero();
113 };
114
115 T flux_xp = get_gradient_dir_by_flux_formulation(graph_iter_xp, Direction::xp);
116 T flux_xm = get_gradient_dir_by_flux_formulation(graph_iter_xm, Direction::xm);
117 T flux_yp = get_gradient_dir_by_flux_formulation(graph_iter_yp, Direction::yp);
118 T flux_ym = get_gradient_dir_by_flux_formulation(graph_iter_ym, Direction::ym);
119 T flux_zp = get_gradient_dir_by_flux_formulation(graph_iter_zp, Direction::zp);
120 T flux_zm = get_gradient_dir_by_flux_formulation(graph_iter_zm, Direction::zm);
121
122 auto S = cell_sizes[cur_cell_block_id] * cell_sizes[cur_cell_block_id];
123
124 auto result = S * ((flux_xp + flux_yp + flux_zp) - (flux_xm + flux_ym + flux_zm));
125 Aii *= S;
126
127 return -result;
128 }
129
151 template<class T, class Tvec, class ACCField>
153 const shambase::VecComponent<Tvec> *cell_sizes,
154 const u32 block_size,
155 const u32 cell_global_id,
156 const AMRGraphLinkiterator &graph_iter_xp,
157 const AMRGraphLinkiterator &graph_iter_xm,
158 const AMRGraphLinkiterator &graph_iter_yp,
159 const AMRGraphLinkiterator &graph_iter_ym,
160 const AMRGraphLinkiterator &graph_iter_zp,
161 const AMRGraphLinkiterator &graph_iter_zm,
162 ACCField &&field_access) {
163 T Aii_unused;
165 cell_sizes,
166 block_size,
167 cell_global_id,
168 graph_iter_xp,
169 graph_iter_xm,
170 graph_iter_yp,
171 graph_iter_ym,
172 graph_iter_zp,
173 graph_iter_zm,
174 field_access,
175 Aii_unused);
176 }
177
202 template<class T, class Tvec>
204 const shambase::VecComponent<Tvec> *cell_sizes,
205 const u32 block_size,
206 const u32 cell_global_id,
207 const AMRGraphLinkiterator &graph_iter_xp,
208 const AMRGraphLinkiterator &graph_iter_xm,
209 const AMRGraphLinkiterator &graph_iter_yp,
210 const AMRGraphLinkiterator &graph_iter_ym,
211 const AMRGraphLinkiterator &graph_iter_zp,
212 const AMRGraphLinkiterator &graph_iter_zm) {
213
214 auto cur_cell_block_id = cell_global_id / block_size;
215
216 auto get_face_weight = [&](auto &graph_links) -> T {
217 auto cell_center_dist = cell_sizes[cur_cell_block_id];
218
219 auto fac = 1.;
220 u32 cnt = graph_links.for_each_object_link_cnt(cell_global_id, [&](u32 id_b) {
221 auto neigh_block_id = id_b / block_size;
222
223 if (cell_sizes[neigh_block_id] > cell_sizes[cur_cell_block_id]) {
224 fac = 3. / 2.;
225 }
226 if (cell_sizes[neigh_block_id] < cell_sizes[cur_cell_block_id]) {
227 fac = 3. / 4.;
228 }
229 });
230
231 return (cnt > 0) ? 1.0 / (cell_center_dist * fac)
232 : shambase::VectorProperties<T>::get_zero();
233 };
234
235 T res = shambase::VectorProperties<T>::get_zero();
236 res += get_face_weight(graph_iter_xp);
237 res += get_face_weight(graph_iter_xm);
238 res += get_face_weight(graph_iter_yp);
239 res += get_face_weight(graph_iter_ym);
240 res += get_face_weight(graph_iter_zp);
241 res += get_face_weight(graph_iter_zm);
242
243 auto S = cell_sizes[cur_cell_block_id] * cell_sizes[cur_cell_block_id];
244 return S * res;
245 }
246} // namespace shammodels::basegodunov
std::uint32_t u32
32 bit unsigned integer
namespace for the basegodunov model
T laplacian_7pt(const shambase::VecComponent< Tvec > *cell_sizes, const u32 block_size, const u32 cell_global_id, const AMRGraphLinkiterator &graph_iter_xp, const AMRGraphLinkiterator &graph_iter_xm, const AMRGraphLinkiterator &graph_iter_yp, const AMRGraphLinkiterator &graph_iter_ym, const AMRGraphLinkiterator &graph_iter_zp, const AMRGraphLinkiterator &graph_iter_zm, ACCField &&field_access)
Get the discretized (finite volume) Laplacian.
T jacobi_weight_7pt(const shambase::VecComponent< Tvec > *cell_sizes, const u32 block_size, const u32 cell_global_id, const AMRGraphLinkiterator &graph_iter_xp, const AMRGraphLinkiterator &graph_iter_xm, const AMRGraphLinkiterator &graph_iter_yp, const AMRGraphLinkiterator &graph_iter_ym, const AMRGraphLinkiterator &graph_iter_zp, const AMRGraphLinkiterator &graph_iter_zm)
Get the weight of the Jacobi preconditioner of the 7-point Laplacian.
T laplacian_7pt_with_Aii(const shambase::VecComponent< Tvec > *cell_sizes, const u32 block_size, const u32 cell_global_id, const AMRGraphLinkiterator &graph_iter_xp, const AMRGraphLinkiterator &graph_iter_xm, const AMRGraphLinkiterator &graph_iter_yp, const AMRGraphLinkiterator &graph_iter_ym, const AMRGraphLinkiterator &graph_iter_zp, const AMRGraphLinkiterator &graph_iter_zm, ACCField &&field_access, T &Aii)
Get the discretized (finite volume) Laplacian, and the diagonal coefficient of the associated matrix.