OpenImpala
Image-based parallel transport property solver
Loading...
Searching...
No Matches
BoundaryCondition.H
Go to the documentation of this file.
1
12#ifndef OPENIMPALA_BOUNDARY_CONDITION_H_
13#define OPENIMPALA_BOUNDARY_CONDITION_H_
14
15#include <AMReX_BLassert.H>
16#include <AMReX_Print.H>
17#include <AMReX_REAL.H>
18
19#include <algorithm>
20#include <cmath>
21#include <cstdio>
22#include <memory>
23#include <string>
24
25namespace OpenImpala {
26
27// Forward-reference stencil constants from TortuosityHypreFill.H
28// (redeclared here to keep this header self-contained)
29namespace bc_detail {
30constexpr int STN_C = 0;
31constexpr int STN_MX = 1;
32constexpr int STN_PX = 2;
33constexpr int STN_MY = 3;
34constexpr int STN_PY = 4;
35constexpr int STN_MZ = 5;
36constexpr int STN_PZ = 6;
37constexpr int NSTENCIL = 7;
38constexpr int CELL_INACTIVE = 0;
39constexpr int CELL_ACTIVE = 1;
40constexpr amrex::Real SMALL_REAL = 1.0e-15;
41} // namespace bc_detail
42
51
57inline BCType parseBCType(const std::string& bc_str) {
58 std::string s = bc_str;
59 std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return std::tolower(c); });
60 if (s == "dirichletexternal" || s == "dirichlet_external" || s == "dirichlet")
62 if (s == "dirichletphaseboundary" || s == "dirichlet_phase_boundary" || s == "phaseboundary")
64 if (s == "neumann" || s == "zeroflux" || s == "zero_flux")
65 return BCType::Neumann;
66 if (s == "periodic")
67 return BCType::Periodic;
68 amrex::Abort("Invalid boundary condition string: '" + bc_str +
69 "'. Valid options: DirichletExternal, DirichletPhaseBoundary, Neumann, Periodic.");
71}
72
80public:
81 virtual ~BoundaryCondition() = default;
82
107 virtual void applyBC(amrex::Real* a, amrex::Real* rhs, amrex::Real* xinit, int nval,
108 const int* active_mask, const int* mask_lo, const int* mask_hi,
109 const amrex::Real* diff_coeff, const int* dc_lo, const int* dc_hi,
110 const int* bxlo, const int* bxhi, const int* domlo, const int* domhi,
111 const amrex::Real* dxinv, amrex::Real vlo, amrex::Real vhi,
112 int dir) const = 0;
113
117 virtual bool needsPeriodicGrid() const {
118 return false;
119 }
120};
121
122// ---------------------------------------------------------------------------
123// DirichletExternalBC — Dirichlet at domain faces (current v1.0.0 default)
124// ---------------------------------------------------------------------------
125
131public:
132 void applyBC(amrex::Real* a, amrex::Real* rhs, amrex::Real* /*xinit*/, int nval,
133 const int* active_mask, const int* mask_lo, const int* mask_hi,
134 const amrex::Real* diff_coeff, const int* dc_lo, const int* dc_hi, const int* bxlo,
135 const int* bxhi, const int* domlo, const int* domhi, const amrex::Real* dxinv,
136 amrex::Real vlo, amrex::Real vhi, int dir) const override {
137 using namespace bc_detail;
138
139 const int mask_nx = mask_hi[0] - mask_lo[0] + 1;
140 const int mask_ny = mask_hi[1] - mask_lo[1] + 1;
141 auto mask = [&](int i, int j, int k) -> int {
142 return active_mask[(i - mask_lo[0]) + (j - mask_lo[1]) * mask_nx +
143 (k - mask_lo[2]) * mask_nx * mask_ny];
144 };
145
146 int m_idx = 0;
147 for (int k = bxlo[2]; k <= bxhi[2]; ++k) {
148 for (int j = bxlo[1]; j <= bxhi[1]; ++j) {
149 for (int i = bxlo[0]; i <= bxhi[0]; ++i) {
150 if (mask(i, j, k) == CELL_INACTIVE) {
151 ++m_idx;
152 continue;
153 }
154
155 const int stn_start = NSTENCIL * m_idx;
156
157 const int coords[] = {i, j, k};
158 const int pos = coords[dir];
159 if (pos == domlo[dir] || pos == domhi[dir]) {
160 const int nx = dc_hi[0] - dc_lo[0] + 1;
161 const int ny = dc_hi[1] - dc_lo[1] + 1;
162 const int idx = i - dc_lo[0] + nx * (j - dc_lo[1] + ny * (k - dc_lo[2]));
163 const amrex::Real weight = 2.0 * diff_coeff[idx] * dxinv[dir];
164 // The exterior face is half a cell from the unknown.
165 for (int side = 0; side < 2; ++side) {
166 if (pos == (side == 0 ? domlo[dir] : domhi[dir])) {
167 a[stn_start + STN_C] += weight;
168 rhs[m_idx] += weight * (side == 0 ? vlo : vhi);
169 }
170 }
171 }
172 ++m_idx;
173 }
174 }
175 }
176 }
177};
178
179// ---------------------------------------------------------------------------
180// DirichletPhaseBoundaryBC — Dirichlet at pore-solid interface near boundary
181// ---------------------------------------------------------------------------
182
191public:
192 void applyBC(amrex::Real* a, amrex::Real* rhs, amrex::Real* xinit, int nval,
193 const int* active_mask, const int* mask_lo, const int* mask_hi,
194 const amrex::Real* /*diff_coeff*/, const int* /*dc_lo*/, const int* /*dc_hi*/,
195 const int* bxlo, const int* bxhi, const int* domlo, const int* domhi,
196 const amrex::Real* /*dxinv*/, amrex::Real vlo, amrex::Real vhi,
197 int dir) const override {
198 using namespace bc_detail;
199
200 const int mask_nx = mask_hi[0] - mask_lo[0] + 1;
201 const int mask_ny = mask_hi[1] - mask_lo[1] + 1;
202 auto mask = [&](int i, int j, int k) -> int {
203 return active_mask[(i - mask_lo[0]) + (j - mask_lo[1]) * mask_nx +
204 (k - mask_lo[2]) * mask_nx * mask_ny];
205 };
206
207 // Determine which neighbor offset to check for inactive cells
208 // For low face: check the -dir neighbor; for high face: check the +dir neighbor
209 int di_lo[3] = {0, 0, 0};
210 int di_hi[3] = {0, 0, 0};
211 di_lo[dir] = -1; // neighbor toward inlet
212 di_hi[dir] = 1; // neighbor toward outlet
213
214 int m_idx = 0;
215 for (int k = bxlo[2]; k <= bxhi[2]; ++k) {
216 for (int j = bxlo[1]; j <= bxhi[1]; ++j) {
217 for (int i = bxlo[0]; i <= bxhi[0]; ++i) {
218 if (mask(i, j, k) == CELL_INACTIVE) {
219 ++m_idx;
220 continue;
221 }
222
223 const int stn_start = NSTENCIL * m_idx;
224 bool apply_lo = false;
225 bool apply_hi = false;
226
227 // Near low face: active cell with inactive neighbor toward inlet
228 int coords[3] = {i, j, k};
229 if (coords[dir] == domlo[dir]) {
230 // On the domain face itself — always apply Dirichlet
231 apply_lo = true;
232 } else {
233 // Check if neighbor toward low face is inactive
234 int ni = i + di_lo[0];
235 int nj = j + di_lo[1];
236 int nk = k + di_lo[2];
237 if (mask(ni, nj, nk) == CELL_INACTIVE) {
238 // Only apply at the immediate boundary layer (first cell
239 // adjacent to the domain face), not deep into the interior
240 if (coords[dir] <= domlo[dir] + 1) {
241 apply_lo = true;
242 }
243 }
244 }
245
246 // Near high face: active cell with inactive neighbor toward outlet
247 if (coords[dir] == domhi[dir]) {
248 apply_hi = true;
249 } else {
250 int ni = i + di_hi[0];
251 int nj = j + di_hi[1];
252 int nk = k + di_hi[2];
253 if (mask(ni, nj, nk) == CELL_INACTIVE) {
254 if (coords[dir] >= domhi[dir] - 1) {
255 apply_hi = true;
256 }
257 }
258 }
259
260 // If both flags are set (very small domain), skip — ambiguous
261 if (apply_lo && apply_hi) {
262 apply_lo = false;
263 apply_hi = false;
264 }
265
266 if (apply_lo || apply_hi) {
267 amrex::Real bc_val = apply_lo ? vlo : vhi;
268 for (int s = 0; s < NSTENCIL; ++s)
269 a[stn_start + s] = 0.0;
270 a[stn_start + STN_C] = 1.0;
271 rhs[m_idx] = bc_val;
272 xinit[m_idx] = bc_val;
273 }
274
275 ++m_idx;
276 }
277 }
278 }
279 }
280};
281
282// ---------------------------------------------------------------------------
283// NeumannBC — Zero-flux (no-op, already implicit in the stencil)
284// ---------------------------------------------------------------------------
285
292public:
293 void applyBC(amrex::Real* /*a*/, amrex::Real* /*rhs*/, amrex::Real* /*xinit*/, int /*nval*/,
294 const int* /*active_mask*/, const int* /*mask_lo*/, const int* /*mask_hi*/,
295 const amrex::Real* /*diff_coeff*/, const int* /*dc_lo*/, const int* /*dc_hi*/,
296 const int* /*bxlo*/, const int* /*bxhi*/, const int* /*domlo*/,
297 const int* /*domhi*/, const amrex::Real* /*dxinv*/, amrex::Real /*vlo*/,
298 amrex::Real /*vhi*/, int /*dir*/) const override {
299 // No-op: zero-flux is implicit in the stencil (inactive neighbors have zero coupling)
300 }
301};
302
303// ---------------------------------------------------------------------------
304// PeriodicBC — Periodic wrapping (HYPRE handles the stencil connectivity)
305// ---------------------------------------------------------------------------
306
314public:
315 void applyBC(amrex::Real* /*a*/, amrex::Real* /*rhs*/, amrex::Real* /*xinit*/, int /*nval*/,
316 const int* /*active_mask*/, const int* /*mask_lo*/, const int* /*mask_hi*/,
317 const amrex::Real* /*diff_coeff*/, const int* /*dc_lo*/, const int* /*dc_hi*/,
318 const int* /*bxlo*/, const int* /*bxhi*/, const int* /*domlo*/,
319 const int* /*domhi*/, const amrex::Real* /*dxinv*/, amrex::Real /*vlo*/,
320 amrex::Real /*vhi*/, int /*dir*/) const override {
321 // No-op: HYPRE handles periodic connectivity at the grid level
322 }
323
324 bool needsPeriodicGrid() const override {
325 return true;
326 }
327};
328
329// ---------------------------------------------------------------------------
330// Factory function
331// ---------------------------------------------------------------------------
332
337inline std::unique_ptr<BoundaryCondition> createBC(BCType type) {
338 switch (type) {
340 return std::make_unique<DirichletExternalBC>();
342 return std::make_unique<DirichletPhaseBoundaryBC>();
343 case BCType::Neumann:
344 return std::make_unique<NeumannBC>();
345 case BCType::Periodic:
346 return std::make_unique<PeriodicBC>();
347 default:
348 amrex::Abort("Unknown BCType in createBC");
349 return nullptr;
350 }
351}
352
353} // namespace OpenImpala
354
355#endif // OPENIMPALA_BOUNDARY_CONDITION_H_
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.
virtual ~BoundaryCondition()=default
virtual bool needsPeriodicGrid() const
Whether this BC requires HYPRE periodic grid setup.
Definition BoundaryCondition.H:117
Applies Dirichlet BC at domain boundary faces perpendicular to the flow direction.
Definition BoundaryCondition.H:130
void applyBC(amrex::Real *a, amrex::Real *rhs, amrex::Real *, 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 override
Apply BC modifications to stencil/rhs/xinit for one box.
Definition BoundaryCondition.H:132
Applies Dirichlet BC at active cells adjacent to inactive cells near the domain boundary.
Definition BoundaryCondition.H:190
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 *, const int *, const int *, const int *bxlo, const int *bxhi, const int *domlo, const int *domhi, const amrex::Real *, amrex::Real vlo, amrex::Real vhi, int dir) const override
Apply BC modifications to stencil/rhs/xinit for one box.
Definition BoundaryCondition.H:192
Zero-flux Neumann BC — no stencil modification needed.
Definition BoundaryCondition.H:291
void applyBC(amrex::Real *, amrex::Real *, amrex::Real *, int, const int *, const int *, const int *, const amrex::Real *, const int *, const int *, const int *, const int *, const int *, const int *, const amrex::Real *, amrex::Real, amrex::Real, int) const override
Apply BC modifications to stencil/rhs/xinit for one box.
Definition BoundaryCondition.H:293
Periodic BC — requires HYPRE periodic grid setup.
Definition BoundaryCondition.H:313
bool needsPeriodicGrid() const override
Whether this BC requires HYPRE periodic grid setup.
Definition BoundaryCondition.H:324
void applyBC(amrex::Real *, amrex::Real *, amrex::Real *, int, const int *, const int *, const int *, const amrex::Real *, const int *, const int *, const int *, const int *, const int *, const int *, const amrex::Real *, amrex::Real, amrex::Real, int) const override
Apply BC modifications to stencil/rhs/xinit for one box.
Definition BoundaryCondition.H:315
Definition BoundaryCondition.H:29
constexpr int STN_PX
Definition BoundaryCondition.H:32
constexpr int CELL_INACTIVE
Definition BoundaryCondition.H:38
constexpr int STN_MX
Definition BoundaryCondition.H:31
constexpr amrex::Real SMALL_REAL
Definition BoundaryCondition.H:40
constexpr int NSTENCIL
Definition BoundaryCondition.H:37
constexpr int STN_PY
Definition BoundaryCondition.H:34
constexpr int STN_C
Definition BoundaryCondition.H:30
constexpr int CELL_ACTIVE
Definition BoundaryCondition.H:39
constexpr int STN_MZ
Definition BoundaryCondition.H:35
constexpr int STN_MY
Definition BoundaryCondition.H:33
constexpr int STN_PZ
Definition BoundaryCondition.H:36
Definition CathodeWrite.cpp:12
BCType parseBCType(const std::string &bc_str)
Convert a case-insensitive string to BCType.
Definition BoundaryCondition.H:57
@ NSTENCIL
Definition TortuosityHypreFill.H:36
@ STN_C
Center.
Definition TortuosityHypreFill.H:29
BCType
Boundary condition types for transport solvers.
Definition BoundaryCondition.H:44
@ DirichletPhaseBoundary
Definition BoundaryCondition.H:46
@ DirichletExternal
Definition BoundaryCondition.H:45
@ Neumann
Definition BoundaryCondition.H:48
@ Periodic
Definition BoundaryCondition.H:49
std::unique_ptr< BoundaryCondition > createBC(BCType type)
Create a BoundaryCondition object from a BCType enum.
Definition BoundaryCondition.H:337
constexpr int CELL_INACTIVE
Mask values.
Definition TortuosityHypreFill.H:40