OpenImpala
Image-based parallel transport property solver
Loading...
Searching...
No Matches
TortuosityHypreFill.H
Go to the documentation of this file.
1
9#ifndef OPENIMPALA_TORTUOSITY_HYPRE_FILL_H_
10#define OPENIMPALA_TORTUOSITY_HYPRE_FILL_H_
11
12#include <AMReX_BLassert.H>
13#include <AMReX_REAL.H>
14#include <AMReX_Print.H>
15#include <AMReX_Array4.H>
16#include <AMReX_GpuLaunch.H>
17#include <AMReX_GpuQualifiers.H>
18#include <AMReX_Math.H>
19
20#include "BoundaryCondition.H"
21
22#include <cmath>
23#include <cstdio>
24
25namespace OpenImpala {
26
28enum StencilIdx : int {
29 STN_C = 0,
30 STN_MX = 1,
31 STN_PX = 2,
32 STN_MY = 3,
33 STN_PY = 4,
34 STN_MZ = 5,
35 STN_PZ = 6,
37};
38
40constexpr int CELL_INACTIVE = 0;
41constexpr int CELL_ACTIVE = 1;
42
44constexpr amrex::Real SMALL_REAL = 1.0e-15;
45
77inline void tortuosityFillMatrix(amrex::Real* a, amrex::Real* rhs, amrex::Real* xinit, int nval,
78 const int* active_mask, const int* mask_lo, const int* mask_hi,
79 const amrex::Real* diff_coeff, const int* dc_lo, const int* dc_hi,
80 const int* bxlo, const int* bxhi, const int* domlo,
81 const int* domhi, const amrex::Real* dxinv, amrex::Real vlo,
82 amrex::Real vhi, int dir, int verbose) {
83
84 if (nval <= 0)
85 return;
86
87 // Consistency check: nval must match box dimensions
88 {
89 const int expected =
90 (bxhi[0] - bxlo[0] + 1) * (bxhi[1] - bxlo[1] + 1) * (bxhi[2] - bxlo[2] + 1);
91 AMREX_ALWAYS_ASSERT_WITH_MESSAGE(expected == nval, "tortuosityFillMatrix: nval mismatch.");
92 }
93
94 const amrex::Real coeff_x = dxinv[0];
95 const amrex::Real coeff_y = dxinv[1];
96 const amrex::Real coeff_z = dxinv[2];
97
98 // Helper lambdas to index into 3-D arrays stored in column-major (Fortran) order
99 // active_mask dimensions
100 const int mask_nx = mask_hi[0] - mask_lo[0] + 1;
101 const int mask_ny = mask_hi[1] - mask_lo[1] + 1;
102 auto mask = [&](int i, int j, int k) -> int {
103 return active_mask[(i - mask_lo[0]) + (j - mask_lo[1]) * mask_nx +
104 (k - mask_lo[2]) * mask_nx * mask_ny];
105 };
106
107 // diff_coeff dimensions
108 const int dc_nx = dc_hi[0] - dc_lo[0] + 1;
109 const int dc_ny = dc_hi[1] - dc_lo[1] + 1;
110 auto dc = [&](int i, int j, int k) -> amrex::Real {
111 return diff_coeff[(i - dc_lo[0]) + (j - dc_lo[1]) * dc_nx + (k - dc_lo[2]) * dc_nx * dc_ny];
112 };
113
114 int m_idx = 0;
115
116 for (int k = bxlo[2]; k <= bxhi[2]; ++k) {
117 for (int j = bxlo[1]; j <= bxhi[1]; ++j) {
118 for (int i = bxlo[0]; i <= bxhi[0]; ++i) {
119 const int stn_start = NSTENCIL * m_idx;
120
121 // --- Inactive cell: decouple ---
122 if (mask(i, j, k) == CELL_INACTIVE) {
123 for (int s = 0; s < NSTENCIL; ++s)
124 a[stn_start + s] = 0.0;
125 a[stn_start + STN_C] = 1.0;
126 rhs[m_idx] = 0.0;
127 xinit[m_idx] = 0.0;
128 ++m_idx;
129 continue;
130 }
131
132 // --- Active cell ---
133 const amrex::Real D_c = dc(i, j, k);
134 amrex::Real diag_val = 0.0;
135 for (int s = 0; s < NSTENCIL; ++s)
136 a[stn_start + s] = 0.0;
137 bool has_inactive_neighbor = false;
138
139 // -X face
140 if (mask(i - 1, j, k) == CELL_ACTIVE) {
141 amrex::Real D_nbr = dc(i - 1, j, k);
142 amrex::Real D_face = 2.0 * D_c * D_nbr / (D_c + D_nbr);
143 a[stn_start + STN_MX] = -D_face * coeff_x;
144 diag_val += D_face * coeff_x;
145 } else {
146 has_inactive_neighbor = true;
147 }
148
149 // +X face
150 if (mask(i + 1, j, k) == CELL_ACTIVE) {
151 amrex::Real D_nbr = dc(i + 1, j, k);
152 amrex::Real D_face = 2.0 * D_c * D_nbr / (D_c + D_nbr);
153 a[stn_start + STN_PX] = -D_face * coeff_x;
154 diag_val += D_face * coeff_x;
155 } else {
156 has_inactive_neighbor = true;
157 }
158
159 // -Y face
160 if (mask(i, j - 1, k) == CELL_ACTIVE) {
161 amrex::Real D_nbr = dc(i, j - 1, k);
162 amrex::Real D_face = 2.0 * D_c * D_nbr / (D_c + D_nbr);
163 a[stn_start + STN_MY] = -D_face * coeff_y;
164 diag_val += D_face * coeff_y;
165 } else {
166 has_inactive_neighbor = true;
167 }
168
169 // +Y face
170 if (mask(i, j + 1, k) == CELL_ACTIVE) {
171 amrex::Real D_nbr = dc(i, j + 1, k);
172 amrex::Real D_face = 2.0 * D_c * D_nbr / (D_c + D_nbr);
173 a[stn_start + STN_PY] = -D_face * coeff_y;
174 diag_val += D_face * coeff_y;
175 } else {
176 has_inactive_neighbor = true;
177 }
178
179 // -Z face
180 if (mask(i, j, k - 1) == CELL_ACTIVE) {
181 amrex::Real D_nbr = dc(i, j, k - 1);
182 amrex::Real D_face = 2.0 * D_c * D_nbr / (D_c + D_nbr);
183 a[stn_start + STN_MZ] = -D_face * coeff_z;
184 diag_val += D_face * coeff_z;
185 } else {
186 has_inactive_neighbor = true;
187 }
188
189 // +Z face
190 if (mask(i, j, k + 1) == CELL_ACTIVE) {
191 amrex::Real D_nbr = dc(i, j, k + 1);
192 amrex::Real D_face = 2.0 * D_c * D_nbr / (D_c + D_nbr);
193 a[stn_start + STN_PZ] = -D_face * coeff_z;
194 diag_val += D_face * coeff_z;
195 } else {
196 has_inactive_neighbor = true;
197 }
198
199 a[stn_start + STN_C] = diag_val;
200
201 // Check for zero diagonal in an active cell
202 if (std::abs(diag_val) < SMALL_REAL) {
203 std::printf("WARNING: Zero diagonal in ACTIVE cell at (i,j,k)=%d %d %d\n", i, j,
204 k);
205 for (int s = 0; s < NSTENCIL; ++s)
206 a[stn_start + s] = 0.0;
207 a[stn_start + STN_C] = 1.0;
208 rhs[m_idx] = 0.0;
209 xinit[m_idx] = 0.0;
210 ++m_idx;
211 continue;
212 }
213
214 rhs[m_idx] = 0.0;
215
216 // --- Initial guess: linear interpolation along flow direction ---
217 {
218 amrex::Real domain_extent = 0.0;
219 amrex::Real coord = 0.0;
220 if (dir == 0) {
221 domain_extent = static_cast<amrex::Real>(domhi[0] - domlo[0]);
222 coord = static_cast<amrex::Real>(i - domlo[0]);
223 } else if (dir == 1) {
224 domain_extent = static_cast<amrex::Real>(domhi[1] - domlo[1]);
225 coord = static_cast<amrex::Real>(j - domlo[1]);
226 } else {
227 domain_extent = static_cast<amrex::Real>(domhi[2] - domlo[2]);
228 coord = static_cast<amrex::Real>(k - domlo[2]);
229 }
230 amrex::Real factor =
231 (std::abs(domain_extent) < SMALL_REAL) ? 0.0 : 1.0 / domain_extent;
232 xinit[m_idx] = vlo + (vhi - vlo) * coord * factor;
233 }
234
235 // --- Debug printing ---
236 if (verbose >= 3) {
237 bool near_boundary =
238 (i <= domlo[0] + 1 || i >= domhi[0] - 1 || j <= domlo[1] + 1 ||
239 j >= domhi[1] - 1 || k <= domlo[2] + 1 || k >= domhi[2] - 1);
240
241 if (near_boundary || has_inactive_neighbor) {
242 amrex::Real off_diag_sum = 0.0;
243 for (int s = 1; s < NSTENCIL; ++s)
244 off_diag_sum += std::abs(a[stn_start + s]);
245 amrex::Real dv = a[stn_start + STN_C];
246 amrex::Real diag_ratio;
247 if (std::abs(off_diag_sum) < SMALL_REAL)
248 diag_ratio = (std::abs(dv) < SMALL_REAL) ? 1.0 : 1.0e+30;
249 else
250 diag_ratio = std::abs(dv) / off_diag_sum;
251
252 std::printf("DEBUG Stencil at (%d,%d,%d) Active=1 "
253 "Interface=%d D_c=%.4e\n",
254 i, j, k, static_cast<int>(has_inactive_neighbor), D_c);
255 std::printf(" RHS =%.4e\n", rhs[m_idx]);
256 std::printf(" Stencil (C, -X,+X, -Y,+Y, -Z,+Z) = %.4e %.4e %.4e %.4e "
257 "%.4e %.4e %.4e\n",
258 a[stn_start + STN_C], a[stn_start + STN_MX],
259 a[stn_start + STN_PX], a[stn_start + STN_MY],
260 a[stn_start + STN_PY], a[stn_start + STN_MZ],
261 a[stn_start + STN_PZ]);
262 std::printf(" Diag Dominance Ratio = %.4e (OffDiagSum = %.4e)\n",
263 diag_ratio, off_diag_sum);
264 }
265 }
266
267 ++m_idx;
268 }
269 }
270 }
271}
272
284inline void tortuosityFillMatrix(amrex::Real* a, amrex::Real* rhs, amrex::Real* xinit, int nval,
285 const int* active_mask, const int* mask_lo, const int* mask_hi,
286 const amrex::Real* diff_coeff, const int* dc_lo, const int* dc_hi,
287 const int* bxlo, const int* bxhi, const int* domlo,
288 const int* domhi, const amrex::Real* dxinv, amrex::Real vlo,
289 amrex::Real vhi, int dir, int verbose,
290 const BoundaryCondition* bc_inlet_outlet,
291 const BoundaryCondition* bc_sides) {
292
293 // Build interior stencil (no BCs applied)
294 tortuosityFillMatrix(a, rhs, xinit, nval, active_mask, mask_lo, mask_hi, diff_coeff, dc_lo,
295 dc_hi, bxlo, bxhi, domlo, domhi, dxinv, vlo, vhi, dir, verbose);
296
297 // Apply inlet/outlet BCs (perpendicular to flow direction)
298 if (bc_inlet_outlet != nullptr) {
299 bc_inlet_outlet->applyBC(a, rhs, xinit, nval, active_mask, mask_lo, mask_hi, diff_coeff,
300 dc_lo, dc_hi, bxlo, bxhi, domlo, domhi, dxinv, vlo, vhi, dir);
301 }
302
303 // Apply lateral BCs (parallel to flow direction)
304 if (bc_sides != nullptr) {
305 bc_sides->applyBC(a, rhs, xinit, nval, active_mask, mask_lo, mask_hi, diff_coeff, dc_lo,
306 dc_hi, bxlo, bxhi, domlo, domhi, dxinv, vlo, vhi, dir);
307 }
308}
309
310// =============================================================================
311// GPU-accelerated matrix fill kernel
312// =============================================================================
313#ifdef OPENIMPALA_USE_GPU
314
335inline void tortuosityFillMatrixGpu(const amrex::Box& bx, amrex::Real* a, amrex::Real* rhs,
336 amrex::Real* xinit, const amrex::Array4<const int>& mask_arr,
337 const amrex::Array4<const amrex::Real>& dc_arr,
338 const int* domlo, const int* domhi, const amrex::Real* dxinv,
339 amrex::Real vlo, amrex::Real vhi, int dir) {
340 const int bx_lo0 = bx.smallEnd(0);
341 const int bx_lo1 = bx.smallEnd(1);
342 const int bx_lo2 = bx.smallEnd(2);
343 const int bx_nx = bx.length(0);
344 const int bx_ny = bx.length(1);
345
346 const amrex::Real coeff_x = dxinv[0];
347 const amrex::Real coeff_y = dxinv[1];
348 const amrex::Real coeff_z = dxinv[2];
349
350 const int dom_lo_dir = domlo[dir];
351 const int dom_hi_dir = domhi[dir];
352 const amrex::Real domain_extent = static_cast<amrex::Real>(dom_hi_dir - dom_lo_dir);
353 const amrex::Real inv_extent =
354 (std::abs(domain_extent) < SMALL_REAL) ? 0.0 : 1.0 / domain_extent;
355
356 amrex::ParallelFor(bx, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept {
357 // Linear index into output arrays (column-major: i varies fastest)
358 const int m_idx = (i - bx_lo0) + (j - bx_lo1) * bx_nx + (k - bx_lo2) * bx_nx * bx_ny;
359 const int stn_start = NSTENCIL * m_idx;
360
361 // Initialize stencil to zero
362 for (int s = 0; s < NSTENCIL; ++s)
363 a[stn_start + s] = 0.0;
364
365 // Inactive cell: decouple
366 if (mask_arr(i, j, k) == CELL_INACTIVE) {
367 a[stn_start + STN_C] = 1.0;
368 rhs[m_idx] = 0.0;
369 xinit[m_idx] = 0.0;
370 return;
371 }
372
373 // Active cell: build stencil with harmonic-mean face coefficients
374 const amrex::Real D_c = dc_arr(i, j, k);
375 amrex::Real diag_val = 0.0;
376
377 // -X face
378 if (mask_arr(i - 1, j, k) == CELL_ACTIVE) {
379 amrex::Real D_nbr = dc_arr(i - 1, j, k);
380 amrex::Real D_face = 2.0 * D_c * D_nbr / (D_c + D_nbr);
381 a[stn_start + STN_MX] = -D_face * coeff_x;
382 diag_val += D_face * coeff_x;
383 }
384
385 // +X face
386 if (mask_arr(i + 1, j, k) == CELL_ACTIVE) {
387 amrex::Real D_nbr = dc_arr(i + 1, j, k);
388 amrex::Real D_face = 2.0 * D_c * D_nbr / (D_c + D_nbr);
389 a[stn_start + STN_PX] = -D_face * coeff_x;
390 diag_val += D_face * coeff_x;
391 }
392
393 // -Y face
394 if (mask_arr(i, j - 1, k) == CELL_ACTIVE) {
395 amrex::Real D_nbr = dc_arr(i, j - 1, k);
396 amrex::Real D_face = 2.0 * D_c * D_nbr / (D_c + D_nbr);
397 a[stn_start + STN_MY] = -D_face * coeff_y;
398 diag_val += D_face * coeff_y;
399 }
400
401 // +Y face
402 if (mask_arr(i, j + 1, k) == CELL_ACTIVE) {
403 amrex::Real D_nbr = dc_arr(i, j + 1, k);
404 amrex::Real D_face = 2.0 * D_c * D_nbr / (D_c + D_nbr);
405 a[stn_start + STN_PY] = -D_face * coeff_y;
406 diag_val += D_face * coeff_y;
407 }
408
409 // -Z face
410 if (mask_arr(i, j, k - 1) == CELL_ACTIVE) {
411 amrex::Real D_nbr = dc_arr(i, j, k - 1);
412 amrex::Real D_face = 2.0 * D_c * D_nbr / (D_c + D_nbr);
413 a[stn_start + STN_MZ] = -D_face * coeff_z;
414 diag_val += D_face * coeff_z;
415 }
416
417 // +Z face
418 if (mask_arr(i, j, k + 1) == CELL_ACTIVE) {
419 amrex::Real D_nbr = dc_arr(i, j, k + 1);
420 amrex::Real D_face = 2.0 * D_c * D_nbr / (D_c + D_nbr);
421 a[stn_start + STN_PZ] = -D_face * coeff_z;
422 diag_val += D_face * coeff_z;
423 }
424
425 a[stn_start + STN_C] = diag_val;
426
427 // Zero diagonal safety: decouple
428 if (amrex::Math::abs(diag_val) < SMALL_REAL) {
429 for (int s = 0; s < NSTENCIL; ++s)
430 a[stn_start + s] = 0.0;
431 a[stn_start + STN_C] = 1.0;
432 rhs[m_idx] = 0.0;
433 xinit[m_idx] = 0.0;
434 return;
435 }
436
437 rhs[m_idx] = 0.0;
438
439 // Initial guess: linear interpolation along flow direction
440 amrex::Real coord = 0.0;
441 if (dir == 0)
442 coord = static_cast<amrex::Real>(i - dom_lo_dir);
443 else if (dir == 1)
444 coord = static_cast<amrex::Real>(j - dom_lo_dir);
445 else
446 coord = static_cast<amrex::Real>(k - dom_lo_dir);
447 xinit[m_idx] = vlo + (vhi - vlo) * coord * inv_extent;
448 });
449}
450
451#endif // OPENIMPALA_USE_GPU
452
453} // namespace OpenImpala
454
455#endif // OPENIMPALA_TORTUOSITY_HYPRE_FILL_H_
Boundary condition abstraction for tortuosity and transport solvers.
Abstract base class for boundary conditions.
Definition BoundaryCondition.H:79
virtual void applyBC(amrex::Real *a, amrex::Real *rhs, amrex::Real *xinit, int nval, 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 *dxinv, amrex::Real vlo, amrex::Real vhi, int dir) const =0
Apply BC modifications to stencil/rhs/xinit for one box.
Definition CathodeWrite.cpp:12
StencilIdx
Stencil indices (0-based, matching HYPRE_StructStencilSetElement order).
Definition TortuosityHypreFill.H:28
@ STN_MZ
-Z (Bottom)
Definition TortuosityHypreFill.H:34
@ STN_PY
+Y (North)
Definition TortuosityHypreFill.H:33
@ STN_PX
+X (East)
Definition TortuosityHypreFill.H:31
@ NSTENCIL
Definition TortuosityHypreFill.H:36
@ 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
void tortuosityFillMatrix(amrex::Real *a, amrex::Real *rhs, amrex::Real *xinit, int nval, 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 *dxinv, amrex::Real vlo, amrex::Real vhi, int dir, int verbose)
Fill HYPRE matrix coefficients for the tortuosity solve (interior stencil only).
Definition TortuosityHypreFill.H:77
constexpr int CELL_INACTIVE
Mask values.
Definition TortuosityHypreFill.H:40