OpenImpala
Image-based parallel transport property solver
Loading...
Searching...
No Matches
EffDiffFillMtx.H
Go to the documentation of this file.
1
11#ifndef OPENIMPALA_EFFDIFF_FILL_MTX_H_
12#define OPENIMPALA_EFFDIFF_FILL_MTX_H_
13
14#include "TortuosityHypreFill.H" // Shared constants: StencilIdx, CELL_ACTIVE/INACTIVE, SMALL_REAL
15
16#include <AMReX_BLassert.H>
17#include <AMReX_REAL.H>
18#include <AMReX_Print.H>
19#include <AMReX_Array4.H>
20#include <AMReX_GpuLaunch.H>
21#include <AMReX_GpuQualifiers.H>
22#include <AMReX_Math.H>
23
24#include <cmath>
25#include <cstdio>
26
27namespace OpenImpala {
28
30constexpr int CELL_PINNED = 2;
31
54inline void effDiffFillMatrix(amrex::Real* a_out, amrex::Real* rhs_out, amrex::Real* xinit_out,
55 int npts, const int* active_mask, const int* mask_lo,
56 const int* mask_hi, const amrex::Real* diff_coeff, const int* dc_lo,
57 const int* dc_hi, const int* bxlo, const int* bxhi, const int* domlo,
58 const int* domhi, const amrex::Real* cell_sizes, int dir_k,
59 int verbose, amrex::Real pin_scale = 1.0) {
60 (void)domlo;
61 (void)domhi;
62 (void)verbose;
63
64 if (npts <= 0)
65 return;
66
67 const amrex::Real dx = cell_sizes[0];
68 const amrex::Real dy = cell_sizes[1];
69 const amrex::Real dz = cell_sizes[2];
70
71 const amrex::Real inv_dx = 1.0 / dx;
72 const amrex::Real inv_dy = 1.0 / dy;
73 const amrex::Real inv_dz = 1.0 / dz;
74 const amrex::Real inv_dx2 = inv_dx * inv_dx;
75 const amrex::Real inv_dy2 = inv_dy * inv_dy;
76 const amrex::Real inv_dz2 = inv_dz * inv_dz;
77
78 // Array indexing helpers (column-major / Fortran order)
79 const int mask_nx = mask_hi[0] - mask_lo[0] + 1;
80 const int mask_ny = mask_hi[1] - mask_lo[1] + 1;
81 auto mask = [&](int i, int j, int k) -> int {
82 return active_mask[(i - mask_lo[0]) + (j - mask_lo[1]) * mask_nx +
83 (k - mask_lo[2]) * mask_nx * mask_ny];
84 };
85
86 const int dc_nx = dc_hi[0] - dc_lo[0] + 1;
87 const int dc_ny = dc_hi[1] - dc_lo[1] + 1;
88 auto dc = [&](int i, int j, int k) -> amrex::Real {
89 return diff_coeff[(i - dc_lo[0]) + (j - dc_lo[1]) * dc_nx + (k - dc_lo[2]) * dc_nx * dc_ny];
90 };
91
92 constexpr int NSTEN = 7;
93 int m_idx = 0;
94
95 for (int k = bxlo[2]; k <= bxhi[2]; ++k) {
96 for (int j = bxlo[1]; j <= bxhi[1]; ++j) {
97 for (int i = bxlo[0]; i <= bxhi[0]; ++i) {
98 const int stn = NSTEN * m_idx;
99
100 // Initialize
101 for (int s = 0; s < NSTEN; ++s)
102 a_out[stn + s] = 0.0;
103 rhs_out[m_idx] = 0.0;
104 xinit_out[m_idx] = 0.0;
105
106 const int cell_state = mask(i, j, k);
107 // Inactive cell or pinned active cell (chi = 0): decouple symmetrically
108 if (cell_state == CELL_INACTIVE || cell_state == CELL_PINNED) {
109 a_out[stn + STN_C] = pin_scale;
110 ++m_idx;
111 continue;
112 }
113
114 // --- Active cell: conservative finite-volume -div(D (e_k + grad chi_k)) = 0 ---
115 const amrex::Real D_c = dc(i, j, k);
116 amrex::Real diag_val = 0.0;
117
118 const int mx_state = mask(i - 1, j, k);
119 const int px_state = mask(i + 1, j, k);
120 const int my_state = mask(i, j - 1, k);
121 const int py_state = mask(i, j + 1, k);
122
123 amrex::Real D_mx = 0.0, D_px = 0.0;
124 amrex::Real D_my = 0.0, D_py = 0.0;
125 amrex::Real D_mz = 0.0, D_pz = 0.0;
126
127 // -X / +X faces (insulating faces contribute zero conductance;
128 // faces into CELL_PINNED contribute to diagonal and RHS with chi_pinned = 0)
129 if (mx_state != CELL_INACTIVE) {
130 amrex::Real D_nbr = dc(i - 1, j, k);
131 if (D_c + D_nbr > 0.0)
132 D_mx = 2.0 * D_c * D_nbr / (D_c + D_nbr);
133 if (mx_state == CELL_ACTIVE)
134 a_out[stn + STN_MX] = -D_mx * inv_dx2;
135 diag_val += D_mx * inv_dx2;
136 }
137 if (px_state != CELL_INACTIVE) {
138 amrex::Real D_nbr = dc(i + 1, j, k);
139 if (D_c + D_nbr > 0.0)
140 D_px = 2.0 * D_c * D_nbr / (D_c + D_nbr);
141 if (px_state == CELL_ACTIVE)
142 a_out[stn + STN_PX] = -D_px * inv_dx2;
143 diag_val += D_px * inv_dx2;
144 }
145
146 // -Y / +Y faces
147 if (my_state != CELL_INACTIVE) {
148 amrex::Real D_nbr = dc(i, j - 1, k);
149 if (D_c + D_nbr > 0.0)
150 D_my = 2.0 * D_c * D_nbr / (D_c + D_nbr);
151 if (my_state == CELL_ACTIVE)
152 a_out[stn + STN_MY] = -D_my * inv_dy2;
153 diag_val += D_my * inv_dy2;
154 }
155 if (py_state != CELL_INACTIVE) {
156 amrex::Real D_nbr = dc(i, j + 1, k);
157 if (D_c + D_nbr > 0.0)
158 D_py = 2.0 * D_c * D_nbr / (D_c + D_nbr);
159 if (py_state == CELL_ACTIVE)
160 a_out[stn + STN_PY] = -D_py * inv_dy2;
161 diag_val += D_py * inv_dy2;
162 }
163
164#if (AMREX_SPACEDIM == 3)
165 const int mz_state = mask(i, j, k - 1);
166 const int pz_state = mask(i, j, k + 1);
167
168 // -Z / +Z faces
169 if (mz_state != CELL_INACTIVE) {
170 amrex::Real D_nbr = dc(i, j, k - 1);
171 if (D_c + D_nbr > 0.0)
172 D_mz = 2.0 * D_c * D_nbr / (D_c + D_nbr);
173 if (mz_state == CELL_ACTIVE)
174 a_out[stn + STN_MZ] = -D_mz * inv_dz2;
175 diag_val += D_mz * inv_dz2;
176 }
177 if (pz_state != CELL_INACTIVE) {
178 amrex::Real D_nbr = dc(i, j, k + 1);
179 if (D_c + D_nbr > 0.0)
180 D_pz = 2.0 * D_c * D_nbr / (D_c + D_nbr);
181 if (pz_state == CELL_ACTIVE)
182 a_out[stn + STN_PZ] = -D_pz * inv_dz2;
183 diag_val += D_pz * inv_dz2;
184 }
185#endif
186
187 a_out[stn + STN_C] = diag_val;
188
189 // Conservative face-flux RHS: (D_{i+1/2}^{(k)} - D_{i-1/2}^{(k)}) / h_k
190 amrex::Real rhs_val = 0.0;
191 if (dir_k == 0) {
192 rhs_val = (D_px - D_mx) * inv_dx;
193 } else if (dir_k == 1) {
194 rhs_val = (D_py - D_my) * inv_dy;
195 }
196#if (AMREX_SPACEDIM == 3)
197 else if (dir_k == 2) {
198 rhs_val = (D_pz - D_mz) * inv_dz;
199 }
200#endif
201 rhs_out[m_idx] = rhs_val;
202
203 // Safety: decouple cells with near-zero diagonal
204 if (std::abs(diag_val) < SMALL_REAL) {
205 for (int s = 0; s < NSTEN; ++s)
206 a_out[stn + s] = 0.0;
207 a_out[stn + STN_C] = pin_scale;
208 rhs_out[m_idx] = 0.0;
209 xinit_out[m_idx] = 0.0;
210 }
211
212 ++m_idx;
213 }
214 }
215 }
216
217 AMREX_ALWAYS_ASSERT_WITH_MESSAGE(m_idx == npts, "effDiffFillMatrix: m_idx != npts.");
218}
219
220// =============================================================================
221// GPU-accelerated effective diffusivity matrix fill kernel
222// =============================================================================
223#ifdef OPENIMPALA_USE_GPU
224
241inline void effDiffFillMatrixGpu(const amrex::Box& bx, amrex::Real* a_out, amrex::Real* rhs_out,
242 amrex::Real* xinit_out, const amrex::Array4<const int>& mask_arr,
243 const amrex::Array4<const amrex::Real>& dc_arr,
244 const amrex::Real* cell_sizes, int dir_k,
245 amrex::Real pin_scale = 1.0) {
246 const int bx_lo0 = bx.smallEnd(0);
247 const int bx_lo1 = bx.smallEnd(1);
248 const int bx_lo2 = bx.smallEnd(2);
249 const int bx_nx = bx.length(0);
250 const int bx_ny = bx.length(1);
251
252 const amrex::Real dx = cell_sizes[0];
253 const amrex::Real dy = cell_sizes[1];
254 const amrex::Real dz = cell_sizes[2];
255
256 const amrex::Real inv_dx = 1.0 / dx;
257 const amrex::Real inv_dy = 1.0 / dy;
258 const amrex::Real inv_dz = 1.0 / dz;
259 const amrex::Real inv_dx2 = inv_dx * inv_dx;
260 const amrex::Real inv_dy2 = inv_dy * inv_dy;
261 const amrex::Real inv_dz2 = inv_dz * inv_dz;
262
263 constexpr int NSTEN = 7;
264
265 amrex::ParallelFor(bx, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept {
266 const int m_idx = (i - bx_lo0) + (j - bx_lo1) * bx_nx + (k - bx_lo2) * bx_nx * bx_ny;
267 const int stn = NSTEN * m_idx;
268
269 // Initialize
270 for (int s = 0; s < NSTEN; ++s)
271 a_out[stn + s] = 0.0;
272 rhs_out[m_idx] = 0.0;
273 xinit_out[m_idx] = 0.0;
274
275 const int cell_state = mask_arr(i, j, k);
276 // Inactive cell or pinned active cell (chi = 0): decouple symmetrically
277 if (cell_state == CELL_INACTIVE || cell_state == CELL_PINNED) {
278 a_out[stn + STN_C] = pin_scale;
279 return;
280 }
281
282 const amrex::Real D_c = dc_arr(i, j, k);
283 amrex::Real diag_val = 0.0;
284
285 const int mx_state = mask_arr(i - 1, j, k);
286 const int px_state = mask_arr(i + 1, j, k);
287 const int my_state = mask_arr(i, j - 1, k);
288 const int py_state = mask_arr(i, j + 1, k);
289
290 amrex::Real D_mx = 0.0, D_px = 0.0;
291 amrex::Real D_my = 0.0, D_py = 0.0;
292 amrex::Real D_mz = 0.0, D_pz = 0.0;
293
294 if (mx_state != CELL_INACTIVE) {
295 amrex::Real D_nbr = dc_arr(i - 1, j, k);
296 if (D_c + D_nbr > 0.0)
297 D_mx = 2.0 * D_c * D_nbr / (D_c + D_nbr);
298 if (mx_state == CELL_ACTIVE)
299 a_out[stn + STN_MX] = -D_mx * inv_dx2;
300 diag_val += D_mx * inv_dx2;
301 }
302 if (px_state != CELL_INACTIVE) {
303 amrex::Real D_nbr = dc_arr(i + 1, j, k);
304 if (D_c + D_nbr > 0.0)
305 D_px = 2.0 * D_c * D_nbr / (D_c + D_nbr);
306 if (px_state == CELL_ACTIVE)
307 a_out[stn + STN_PX] = -D_px * inv_dx2;
308 diag_val += D_px * inv_dx2;
309 }
310
311 if (my_state != CELL_INACTIVE) {
312 amrex::Real D_nbr = dc_arr(i, j - 1, k);
313 if (D_c + D_nbr > 0.0)
314 D_my = 2.0 * D_c * D_nbr / (D_c + D_nbr);
315 if (my_state == CELL_ACTIVE)
316 a_out[stn + STN_MY] = -D_my * inv_dy2;
317 diag_val += D_my * inv_dy2;
318 }
319 if (py_state != CELL_INACTIVE) {
320 amrex::Real D_nbr = dc_arr(i, j + 1, k);
321 if (D_c + D_nbr > 0.0)
322 D_py = 2.0 * D_c * D_nbr / (D_c + D_nbr);
323 if (py_state == CELL_ACTIVE)
324 a_out[stn + STN_PY] = -D_py * inv_dy2;
325 diag_val += D_py * inv_dy2;
326 }
327
328#if (AMREX_SPACEDIM == 3)
329 const int mz_state = mask_arr(i, j, k - 1);
330 const int pz_state = mask_arr(i, j, k + 1);
331
332 if (mz_state != CELL_INACTIVE) {
333 amrex::Real D_nbr = dc_arr(i, j, k - 1);
334 if (D_c + D_nbr > 0.0)
335 D_mz = 2.0 * D_c * D_nbr / (D_c + D_nbr);
336 if (mz_state == CELL_ACTIVE)
337 a_out[stn + STN_MZ] = -D_mz * inv_dz2;
338 diag_val += D_mz * inv_dz2;
339 }
340 if (pz_state != CELL_INACTIVE) {
341 amrex::Real D_nbr = dc_arr(i, j, k + 1);
342 if (D_c + D_nbr > 0.0)
343 D_pz = 2.0 * D_c * D_nbr / (D_c + D_nbr);
344 if (pz_state == CELL_ACTIVE)
345 a_out[stn + STN_PZ] = -D_pz * inv_dz2;
346 diag_val += D_pz * inv_dz2;
347 }
348#endif
349
350 a_out[stn + STN_C] = diag_val;
351
352 amrex::Real rhs_val = 0.0;
353 if (dir_k == 0) {
354 rhs_val = (D_px - D_mx) * inv_dx;
355 } else if (dir_k == 1) {
356 rhs_val = (D_py - D_my) * inv_dy;
357 }
358#if (AMREX_SPACEDIM == 3)
359 else if (dir_k == 2) {
360 rhs_val = (D_pz - D_mz) * inv_dz;
361 }
362#endif
363 rhs_out[m_idx] = rhs_val;
364
365 if (amrex::Math::abs(diag_val) < SMALL_REAL) {
366 for (int s = 0; s < NSTEN; ++s)
367 a_out[stn + s] = 0.0;
368 a_out[stn + STN_C] = pin_scale;
369 rhs_out[m_idx] = 0.0;
370 xinit_out[m_idx] = 0.0;
371 }
372 });
373}
374
375#endif // OPENIMPALA_USE_GPU
376
377} // namespace OpenImpala
378
379#endif // OPENIMPALA_EFFDIFF_FILL_MTX_H_
C++ kernel for filling HYPRE structured-grid matrix coefficients for the tortuosity (flow-through) so...
Definition CathodeWrite.cpp:12
void effDiffFillMatrix(amrex::Real *a_out, amrex::Real *rhs_out, amrex::Real *xinit_out, int npts, const int *active_mask, const int *mask_lo, const int *mask_hi, const amrex::Real *diff_coeff, const int *dc_lo, const int *dc_hi, const int *bxlo, const int *bxhi, const int *domlo, const int *domhi, const amrex::Real *cell_sizes, int dir_k, int verbose, amrex::Real pin_scale=1.0)
Fill HYPRE matrix for the effective-diffusivity cell problem.
Definition EffDiffFillMtx.H:54
@ STN_MZ
-Z (Bottom)
Definition TortuosityHypreFill.H:34
@ STN_PY
+Y (North)
Definition TortuosityHypreFill.H:33
@ STN_PX
+X (East)
Definition TortuosityHypreFill.H:31
@ STN_MX
-X (West)
Definition TortuosityHypreFill.H:30
@ STN_MY
-Y (South)
Definition TortuosityHypreFill.H:32
@ STN_C
Center.
Definition TortuosityHypreFill.H:29
@ STN_PZ
+Z (Top)
Definition TortuosityHypreFill.H:35
constexpr int CELL_ACTIVE
Definition TortuosityHypreFill.H:41
constexpr amrex::Real SMALL_REAL
Small tolerance for floating-point comparisons.
Definition TortuosityHypreFill.H:44
constexpr int CELL_PINNED
Pinned active cell marker (chi = 0 Dirichlet pin for per-component nullspace removal).
Definition EffDiffFillMtx.H:30
constexpr int CELL_INACTIVE
Mask values.
Definition TortuosityHypreFill.H:40