9#ifndef OPENIMPALA_TORTUOSITY_HYPRE_FILL_H_
10#define OPENIMPALA_TORTUOSITY_HYPRE_FILL_H_
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>
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) {
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.");
94 const amrex::Real coeff_x = dxinv[0];
95 const amrex::Real coeff_y = dxinv[1];
96 const amrex::Real coeff_z = dxinv[2];
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];
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];
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;
124 a[stn_start + s] = 0.0;
125 a[stn_start +
STN_C] = 1.0;
133 const amrex::Real D_c = dc(i, j, k);
134 amrex::Real diag_val = 0.0;
136 a[stn_start + s] = 0.0;
137 bool has_inactive_neighbor =
false;
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;
146 has_inactive_neighbor =
true;
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;
156 has_inactive_neighbor =
true;
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;
166 has_inactive_neighbor =
true;
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;
176 has_inactive_neighbor =
true;
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;
186 has_inactive_neighbor =
true;
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;
196 has_inactive_neighbor =
true;
199 a[stn_start +
STN_C] = diag_val;
203 std::printf(
"WARNING: Zero diagonal in ACTIVE cell at (i,j,k)=%d %d %d\n", i, j,
206 a[stn_start + s] = 0.0;
207 a[stn_start +
STN_C] = 1.0;
218 amrex::Real domain_extent = 0.0;
219 amrex::Real coord = 0.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]);
227 domain_extent =
static_cast<amrex::Real
>(domhi[2] - domlo[2]);
228 coord =
static_cast<amrex::Real
>(k - domlo[2]);
231 (std::abs(domain_extent) <
SMALL_REAL) ? 0.0 : 1.0 / domain_extent;
232 xinit[m_idx] = vlo + (vhi - vlo) * coord * factor;
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);
241 if (near_boundary || has_inactive_neighbor) {
242 amrex::Real off_diag_sum = 0.0;
244 off_diag_sum += std::abs(a[stn_start + s]);
245 amrex::Real dv = a[stn_start +
STN_C];
246 amrex::Real diag_ratio;
248 diag_ratio = (std::abs(dv) <
SMALL_REAL) ? 1.0 : 1.0e+30;
250 diag_ratio = std::abs(dv) / off_diag_sum;
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 "
262 std::printf(
" Diag Dominance Ratio = %.4e (OffDiagSum = %.4e)\n",
263 diag_ratio, off_diag_sum);
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,
295 dc_hi, bxlo, bxhi, domlo, domhi, dxinv, vlo, vhi, dir, verbose);
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);
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);
313#ifdef OPENIMPALA_USE_GPU
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);
346 const amrex::Real coeff_x = dxinv[0];
347 const amrex::Real coeff_y = dxinv[1];
348 const amrex::Real coeff_z = dxinv[2];
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;
356 amrex::ParallelFor(bx, [=] AMREX_GPU_DEVICE(
int i,
int j,
int k)
noexcept {
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;
363 a[stn_start + s] = 0.0;
367 a[stn_start +
STN_C] = 1.0;
374 const amrex::Real D_c = dc_arr(i, j, k);
375 amrex::Real diag_val = 0.0;
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;
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;
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;
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;
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;
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;
425 a[stn_start +
STN_C] = diag_val;
428 if (amrex::Math::abs(diag_val) <
SMALL_REAL) {
430 a[stn_start + s] = 0.0;
431 a[stn_start +
STN_C] = 1.0;
440 amrex::Real coord = 0.0;
442 coord =
static_cast<amrex::Real
>(i - dom_lo_dir);
444 coord =
static_cast<amrex::Real
>(j - dom_lo_dir);
446 coord =
static_cast<amrex::Real
>(k - dom_lo_dir);
447 xinit[m_idx] = vlo + (vhi - vlo) * coord * inv_extent;
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