OpenImpala
Image-based parallel transport property solver
Loading...
Searching...
No Matches
TortuosityKernels.H
Go to the documentation of this file.
1
14#ifndef OPENIMPALA_TORTUOSITY_KERNELS_H_
15#define OPENIMPALA_TORTUOSITY_KERNELS_H_
16
17#include <AMReX_Array4.H>
18#include <AMReX_Box.H>
19#include <AMReX_GpuLaunch.H>
20#include <AMReX_GpuQualifiers.H>
21#include <AMReX_REAL.H>
22
23#include <cmath>
24
25namespace OpenImpala {
26
28constexpr int CELL_TYPE_BLOCKED = 0;
29constexpr int CELL_TYPE_FREE = 1;
30
32constexpr int NEIGHBOR_OUTSIDE = -1;
33
34// =========================================================================
35// removeIsolatedCells — replaces tortuosity_remspot
36// =========================================================================
52inline void removeIsolatedCells(int* q, const int* q_lo, const int* q_hi, int /*ncomp*/,
53 const int* bxlo, const int* bxhi, const int* domlo,
54 const int* domhi) {
55 // 3-D column-major indexing for component 0
56 const int nx = q_hi[0] - q_lo[0] + 1;
57 const int ny = q_hi[1] - q_lo[1] + 1;
58 auto idx = [&](int i, int j, int k) -> int& {
59 return q[(i - q_lo[0]) + (j - q_lo[1]) * nx + (k - q_lo[2]) * nx * ny];
60 };
61
62 for (int k = bxlo[2]; k <= bxhi[2]; ++k) {
63 for (int j = bxlo[1]; j <= bxhi[1]; ++j) {
64 for (int i = bxlo[0]; i <= bxhi[0]; ++i) {
65 const int center = idx(i, j, k);
66
67 // Gather 6 neighbour phases; use NEIGHBOR_OUTSIDE at domain edges
68 int nbr[6];
69 nbr[0] = (i == domlo[0]) ? NEIGHBOR_OUTSIDE : idx(i - 1, j, k);
70 nbr[1] = (i == domhi[0]) ? NEIGHBOR_OUTSIDE : idx(i + 1, j, k);
71 nbr[2] = (j == domlo[1]) ? NEIGHBOR_OUTSIDE : idx(i, j - 1, k);
72 nbr[3] = (j == domhi[1]) ? NEIGHBOR_OUTSIDE : idx(i, j + 1, k);
73 nbr[4] = (k == domlo[2]) ? NEIGHBOR_OUTSIDE : idx(i, j, k - 1);
74 nbr[5] = (k == domhi[2]) ? NEIGHBOR_OUTSIDE : idx(i, j, k + 1);
75
76 bool connected = false;
77 for (int n = 0; n < 6; ++n) {
78 if (center == nbr[n]) {
79 connected = true;
80 break;
81 }
82 }
83
84 if (!connected) {
85 idx(i, j, k) = (center == 0) ? 1 : 0;
86 }
87 }
88 }
89 }
90}
91
92// =========================================================================
93// computeFaceFluxes — replaces tortuosity_poisson_flux
94// =========================================================================
108inline void computeFaceFluxes(const amrex::Box& bx, const amrex::Array4<amrex::Real>& fx,
109 const amrex::Array4<amrex::Real>& fy,
110 const amrex::Array4<amrex::Real>& fz,
111 const amrex::Array4<const amrex::Real>& sol,
112 const amrex::Real* dxinv) {
113 const auto lo = bx.loVect3d();
114 const auto hi = bx.hiVect3d();
115 const amrex::Real dhx = dxinv[0];
116 const amrex::Real dhy = dxinv[1];
117 const amrex::Real dhz = dxinv[2];
118
119 constexpr int comp_phi = 0;
120 constexpr int comp_ct = 1;
121
122 // X-fluxes on faces i = lo.x .. hi.x+1
123 {
124 amrex::Box xbx(bx);
125 xbx.surroundingNodes(0); // face-centred in X
126 amrex::ParallelFor(xbx, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept {
127 if (static_cast<int>(std::round(sol(i, j, k, comp_ct))) == CELL_TYPE_BLOCKED ||
128 static_cast<int>(std::round(sol(i - 1, j, k, comp_ct))) == CELL_TYPE_BLOCKED) {
129 fx(i, j, k) = 0.0;
130 } else {
131 fx(i, j, k) = dhx * (sol(i, j, k, comp_phi) - sol(i - 1, j, k, comp_phi));
132 }
133 });
134 }
135
136 // Y-fluxes on faces j = lo.y .. hi.y+1
137 {
138 amrex::Box ybx(bx);
139 ybx.surroundingNodes(1);
140 amrex::ParallelFor(ybx, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept {
141 if (static_cast<int>(std::round(sol(i, j, k, comp_ct))) == CELL_TYPE_BLOCKED ||
142 static_cast<int>(std::round(sol(i, j - 1, k, comp_ct))) == CELL_TYPE_BLOCKED) {
143 fy(i, j, k) = 0.0;
144 } else {
145 fy(i, j, k) = dhy * (sol(i, j, k, comp_phi) - sol(i, j - 1, k, comp_phi));
146 }
147 });
148 }
149
150 // Z-fluxes on faces k = lo.z .. hi.z+1
151 {
152 amrex::Box zbx(bx);
153 zbx.surroundingNodes(2);
154 amrex::ParallelFor(zbx, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept {
155 if (static_cast<int>(std::round(sol(i, j, k, comp_ct))) == CELL_TYPE_BLOCKED ||
156 static_cast<int>(std::round(sol(i, j, k - 1, comp_ct))) == CELL_TYPE_BLOCKED) {
157 fz(i, j, k) = 0.0;
158 } else {
159 fz(i, j, k) = dhz * (sol(i, j, k, comp_phi) - sol(i, j, k - 1, comp_phi));
160 }
161 });
162 }
163}
164
165// =========================================================================
166// forwardEulerUpdate — replaces tortuosity_poisson_update
167// =========================================================================
182inline void forwardEulerUpdate(const amrex::Box& bx, const amrex::Array4<const amrex::Real>& p_arr,
183 const amrex::Array4<amrex::Real>& n_arr,
184 const amrex::Array4<const amrex::Real>& fx,
185 const amrex::Array4<const amrex::Real>& fy,
186 const amrex::Array4<const amrex::Real>& fz, const amrex::Real* dxinv,
187 amrex::Real dt) {
188 const amrex::Real dtdx = dt * dxinv[0];
189 const amrex::Real dtdy = dt * dxinv[1];
190 const amrex::Real dtdz = dt * dxinv[2];
191
192 constexpr int comp_phi = 0;
193
194 amrex::ParallelFor(bx, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept {
195 n_arr(i, j, k, comp_phi) =
196 p_arr(i, j, k, comp_phi) + dtdx * (fx(i + 1, j, k) - fx(i, j, k)) +
197 dtdy * (fy(i, j + 1, k) - fy(i, j, k)) + dtdz * (fz(i, j, k + 1) - fz(i, j, k));
198 });
199}
200
201// =========================================================================
202// integrateBoundaryFluxes — replaces tortuosity_poisson_fio
203// =========================================================================
218inline void integrateBoundaryFluxes(const amrex::Box& cell_bx,
219 const amrex::Array4<const amrex::Real>& fx,
220 const amrex::Array4<const amrex::Real>& fy,
221 const amrex::Array4<const amrex::Real>& fz,
222 const amrex::Box& fxbox, const amrex::Box& fybox,
223 const amrex::Box& fzbox, int dir, amrex::Real& flux_in,
224 amrex::Real& flux_out) {
225 const auto lo = cell_bx.loVect3d();
226 const auto hi = cell_bx.hiVect3d();
227
228 flux_in = 0.0;
229 flux_out = 0.0;
230
231 if (dir == 0) {
232 // X-direction: sum fx on low-X and high-X faces
233 if (fxbox.smallEnd(0) <= lo[0]) {
234 for (int k = lo[2]; k <= hi[2]; ++k)
235 for (int j = lo[1]; j <= hi[1]; ++j)
236 flux_in += fx(lo[0], j, k);
237 }
238 if (fxbox.bigEnd(0) >= hi[0] + 1) {
239 for (int k = lo[2]; k <= hi[2]; ++k)
240 for (int j = lo[1]; j <= hi[1]; ++j)
241 flux_out += fx(hi[0] + 1, j, k);
242 }
243 } else if (dir == 1) {
244 // Y-direction
245 if (fybox.smallEnd(1) <= lo[1]) {
246 for (int k = lo[2]; k <= hi[2]; ++k)
247 for (int i = lo[0]; i <= hi[0]; ++i)
248 flux_in += fy(i, lo[1], k);
249 }
250 if (fybox.bigEnd(1) >= hi[1] + 1) {
251 for (int k = lo[2]; k <= hi[2]; ++k)
252 for (int i = lo[0]; i <= hi[0]; ++i)
253 flux_out += fy(i, hi[1] + 1, k);
254 }
255 } else {
256 // Z-direction
257 if (fzbox.smallEnd(2) <= lo[2]) {
258 for (int j = lo[1]; j <= hi[1]; ++j)
259 for (int i = lo[0]; i <= hi[0]; ++i)
260 flux_in += fz(i, j, lo[2]);
261 }
262 if (fzbox.bigEnd(2) >= hi[2] + 1) {
263 for (int j = lo[1]; j <= hi[1]; ++j)
264 for (int i = lo[0]; i <= hi[0]; ++i)
265 flux_out += fz(i, j, hi[2] + 1);
266 }
267 }
268}
269
270} // namespace OpenImpala
271
272#endif // OPENIMPALA_TORTUOSITY_KERNELS_H_
Definition CathodeWrite.cpp:12
constexpr int CELL_TYPE_FREE
Definition TortuosityKernels.H:29
constexpr int CELL_TYPE_BLOCKED
Cell-type constants (matching Tortuosity.H conventions).
Definition TortuosityKernels.H:28
void integrateBoundaryFluxes(const amrex::Box &cell_bx, const amrex::Array4< const amrex::Real > &fx, const amrex::Array4< const amrex::Real > &fy, const amrex::Array4< const amrex::Real > &fz, const amrex::Box &fxbox, const amrex::Box &fybox, const amrex::Box &fzbox, int dir, amrex::Real &flux_in, amrex::Real &flux_out)
Sum fluxes on the inlet (low) and outlet (high) domain faces.
Definition TortuosityKernels.H:218
constexpr int NEIGHBOR_OUTSIDE
Sentinel value for neighbors outside the physical domain.
Definition TortuosityKernels.H:32
void removeIsolatedCells(int *q, const int *q_lo, const int *q_hi, int, const int *bxlo, const int *bxhi, const int *domlo, const int *domhi)
Remove isolated single cells by checking 6-connectivity.
Definition TortuosityKernels.H:52
void forwardEulerUpdate(const amrex::Box &bx, const amrex::Array4< const amrex::Real > &p_arr, const amrex::Array4< amrex::Real > &n_arr, const amrex::Array4< const amrex::Real > &fx, const amrex::Array4< const amrex::Real > &fy, const amrex::Array4< const amrex::Real > &fz, const amrex::Real *dxinv, amrex::Real dt)
Forward Euler update: phi_new = phi_old + dt * div(flux).
Definition TortuosityKernels.H:182
void computeFaceFluxes(const amrex::Box &bx, const amrex::Array4< amrex::Real > &fx, const amrex::Array4< amrex::Real > &fy, const amrex::Array4< amrex::Real > &fz, const amrex::Array4< const amrex::Real > &sol, const amrex::Real *dxinv)
Compute face fluxes using finite differences on a cell-centred solution.
Definition TortuosityKernels.H:108