flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
mac_pressure.hpp
Go to the documentation of this file.
1
10#ifndef PECLET_FLOW_MAC_PRESSURE_HPP
11#define PECLET_FLOW_MAC_PRESSURE_HPP
12
13#include <Kokkos_Core.hpp>
14#include <type_traits>
15
16#include "mac_cutcell.hpp"
17#include "policy.hpp"
18
19namespace peclet::flow {
20
21// --- A0: the per-cell bodies (doc/vof_step_performance_design.md §4.5 rule H, §5.2) -------------
22// Every launch form of these kernels -- the device MDRange, the host pencil, and the single-team
23// kernels to come -- calls the SAME inline body, so the per-cell arithmetic cannot drift between
24// them. Each expression is a verbatim copy of the kernel it came from, in the same order: a
25// rewritten expression can contract into a different FMA chain on the device (design note R1).
26// The neighbour INDICES of the field being smoothed or applied are arguments (xp = i + sx,
27// xm = i - sx, ...), so a caller can hand a periodically wrapped index instead of a ghost one; the
28// coefficients are always read at their own (unwrapped) indices.
29
30// A = -div(open grad): AC = sum of the 6 face terms (openness*gf), off-diagonal across each face =
31// -term. ox[i] is the -x face openness of cell i (== +x face of cell i-1). (mg_build_op_k port.)
32template <class OpV, class OV>
33KOKKOS_INLINE_FUNCTION void cutcellBuildOpCell(const OpV& AC, const OpV& AW, const OpV& AE,
34 const OpV& AS, const OpV& AN, const OpV& AB,
35 const OpV& AT, const OV& ox, const OV& oy,
36 const OV& oz, long i, long sx, long sy, long sz,
37 double gfx, double gfy, double gfz) {
38 const double tw = ox(i) * gfx, te = ox(i + sx) * gfx;
39 const double ts = oy(i) * gfy, tn = oy(i + sy) * gfy;
40 const double tb = oz(i) * gfz, tt = oz(i + sz) * gfz;
41 AW(i) = -tw;
42 AE(i) = -te;
43 AS(i) = -ts;
44 AN(i) = -tn;
45 AB(i) = -tb;
46 AT(i) = -tt;
47 AC(i) = te + tw + tn + ts + tt + tb;
48}
49// One red/black Gauss-Seidel update of cell i: phi = (b - offdiag)/AC; AC~0 (fully solid) cells are
50// decoupled and keep phi.
51template <class PV, class BV, class OpV>
52KOKKOS_INLINE_FUNCTION void cutcellSmoothCell(const PV& phi, const BV& b, const OpV& AC,
53 const OpV& AW, const OpV& AE, const OpV& AS,
54 const OpV& AN, const OpV& AB, const OpV& AT, long i,
55 long xp, long xm, long yp, long ym, long zp,
56 long zm) {
57 const double ac = AC(i);
58 if (ac < 1e-30)
59 return; // fully closed (solid) cell: decoupled, phi stays 0
60 const double s = AE(i) * phi(xp) + AW(i) * phi(xm) + AN(i) * phi(yp) + AS(i) * phi(ym) +
61 AT(i) * phi(zp) + AB(i) * phi(zm);
62 phi(i) = (b(i) - s) / ac;
63}
64// (A x)_i from the bands (applyCutcellOp).
65template <class XV, class OpV>
66KOKKOS_INLINE_FUNCTION double cutcellApplyCell(const XV& x, const OpV& AC, const OpV& AW,
67 const OpV& AE, const OpV& AS, const OpV& AN,
68 const OpV& AB, const OpV& AT, long i, long xp,
69 long xm, long yp, long ym, long zp, long zm) {
70 return AC(i) * x(i) + AE(i) * x(xp) + AW(i) * x(xm) + AN(i) * x(yp) + AS(i) * x(ym) +
71 AT(i) * x(zp) + AB(i) * x(zm);
72}
73// (A x)_i in the exact flux form from the double face openness (applyCutcellOpExact).
74template <class XV, class OV>
75KOKKOS_INLINE_FUNCTION double cutcellApplyExactCell(const XV& x, const OV& ox, const OV& oy,
76 const OV& oz, long i, long sx, long sy, long sz,
77 long xp, long xm, long yp, long ym, long zp,
78 long zm, double gfx, double gfy, double gfz) {
79 const double xi = x(i);
80 return ox(i) * gfx * (xi - x(xm)) + ox(i + sx) * gfx * (xi - x(xp)) + oy(i) * gfy * (xi - x(ym)) +
81 oy(i + sy) * gfy * (xi - x(yp)) + oz(i) * gfz * (xi - x(zm)) +
82 oz(i + sz) * gfz * (xi - x(zp));
83}
84
85template <class OpV>
86inline void buildCutcellOp(OpV AC, OpV AW, OpV AE, OpV AS, OpV AN, OpV AB, OpV AT, CCConst ox,
87 CCConst oy, CCConst oz, C3 e, int g, double gfx, double gfy,
88 double gfz) {
89 ccFor3(
90 "peclet::flow::cc_build_op", C3{g, g, g}, C3{e.x - g, e.y - g, e.z - g},
91 KOKKOS_LAMBDA(int lx, int ly, int lz) {
92 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
93 const long i = (long)lx + (long)ly * sy + (long)lz * sz;
94 cutcellBuildOpCell(AC, AW, AE, AS, AN, AB, AT, ox, oy, oz, i, sx, sy, sz, gfx, gfy, gfz);
95 });
96}
97
98// --- A2: the FACE form of the pressure operator (doc/vof_step_performance_design.md §4.2, §5.3) --
99// The pressure hierarchy stores the diagonal AC and ONE band coefficient per face instead of seven
100// bands: AFX(i) is the off-diagonal coefficient of cell i's LOW x face, -open_x(i) * gfx (MReal) --
101// exactly the old AW(i) -- and the same face is cell i-1's high face, so
102// AW(i) = AFX(i) AE(i) = AFX(i+sx) AS(i) = AFY(i) AN(i) = AFY(i+sy)
103// AB(i) = AFZ(i) AT(i) = AFZ(i+sz).
104// Bit-identical to the bands: AFX holds the same MReal-rounded value AW held, and every consumer
105// reads it into the identical band expression -- the order AE, AW, AN, AS, AT, AB, each term a
106// (coefficient) * (neighbour) product. Storing the band SIGN matters on the device: the first form
107// of this change stored +open*gf and read (-TX(i+1))*phi + ..., and nvcc then contracted a
108// different product into each FMA than for the band expression -- the bubble column drifted by
109// 8.8e-15 (u, relative) in 50 steps; host builds do not contract and were bitwise either way
110// (doc/vof_step_performance_design.md §4.2 note, WO-3). AC is built by the identical expression.
111// 32 instead of 56 bytes of operator per cell (double), and three arrays per level instead of six.
112// The seven-band kernels above stay for the scalar-transport smoother and the standalone kernel
113// tests.
114//
115// AFX/AFY/AFZ are written over the FULL extent [0, ext) -- every index a band consumer ever reads,
116// including the i+sx of the last ring cell of a communication-avoiding (g=2) level -- from the
117// ghost-filled, boundary-re-imposed openness; AC over the build box [gb, ext-gb) as before.
118template <class OpV, class OV>
119KOKKOS_INLINE_FUNCTION void cutcellBuildFaceOpCell(const OpV& AC, const OpV& AFX, const OpV& AFY,
120 const OpV& AFZ, const OV& ox, const OV& oy,
121 const OV& oz, long i, long sx, long sy, long sz,
122 double gfx, double gfy, double gfz, bool diag) {
123 const double tw = ox(i) * gfx;
124 const double ts = oy(i) * gfy;
125 const double tb = oz(i) * gfz;
126 AFX(i) = -tw;
127 AFY(i) = -ts;
128 AFZ(i) = -tb;
129 if (diag) {
130 const double te = ox(i + sx) * gfx;
131 const double tn = oy(i + sy) * gfy;
132 const double tt = oz(i + sz) * gfz;
133 AC(i) = te + tw + tn + ts + tt + tb;
134 }
135}
136template <class PV, class BV, class OpV>
137KOKKOS_INLINE_FUNCTION void cutcellSmoothFaceCell(const PV& phi, const BV& b, const OpV& AC,
138 const OpV& AFX, const OpV& AFY, const OpV& AFZ,
139 long i, long sx, long sy, long sz, long xp,
140 long xm, long yp, long ym, long zp, long zm) {
141 const double ac = AC(i);
142 if (ac < 1e-30)
143 return; // fully closed (solid) cell: decoupled, phi stays 0
144 const double s = AFX(i + sx) * phi(xp) + AFX(i) * phi(xm) + AFY(i + sy) * phi(yp) +
145 AFY(i) * phi(ym) + AFZ(i + sz) * phi(zp) + AFZ(i) * phi(zm);
146 phi(i) = (b(i) - s) / ac;
147}
148template <class XV, class OpV>
149KOKKOS_INLINE_FUNCTION double cutcellApplyFaceCell(const XV& x, const OpV& AC, const OpV& AFX,
150 const OpV& AFY, const OpV& AFZ, long i, long sx,
151 long sy, long sz, long xp, long xm, long yp,
152 long ym, long zp, long zm) {
153 return AC(i) * x(i) + AFX(i + sx) * x(xp) + AFX(i) * x(xm) + AFY(i + sy) * x(yp) +
154 AFY(i) * x(ym) + AFZ(i + sz) * x(zp) + AFZ(i) * x(zm);
155}
156template <class RV, class XV, class BV, class OpV>
157KOKKOS_INLINE_FUNCTION void cutcellResidualFaceCell(const RV& r, const XV& x, const BV& b,
158 const OpV& AC, const OpV& AFX, const OpV& AFY,
159 const OpV& AFZ, long i, long sx, long sy,
160 long sz, long xp, long xm, long yp, long ym,
161 long zp, long zm) {
162 const double Ax = (double)AC(i) * x(i) + (double)AFX(i + sx) * x(xp) + (double)AFX(i) * x(xm) +
163 (double)AFY(i + sy) * x(yp) + (double)AFY(i) * x(ym) +
164 (double)AFZ(i + sz) * x(zp) + (double)AFZ(i) * x(zm);
165 r(i) = b(i) - Ax;
166}
167
168// Face-form build: AFX/AFY/AFZ over [0, ext), AC over the box [gb, ext-gb) (gb = the level's build
169// width, as buildCutcellOp's g).
170template <class OpV>
171inline void buildCutcellOpFace(OpV AC, OpV AFX, OpV AFY, OpV AFZ, CCConst ox, CCConst oy,
172 CCConst oz, C3 e, int gb, double gfx, double gfy, double gfz) {
173 ccFor3(
174 "peclet::flow::cc_build_op", C3{0, 0, 0}, C3{e.x, e.y, e.z},
175 KOKKOS_LAMBDA(int lx, int ly, int lz) {
176 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
177 const long i = (long)lx + (long)ly * sy + (long)lz * sz;
178 const bool diag =
179 lx >= gb && lx < e.x - gb && ly >= gb && ly < e.y - gb && lz >= gb && lz < e.z - gb;
180 cutcellBuildFaceOpCell(AC, AFX, AFY, AFZ, ox, oy, oz, i, sx, sy, sz, gfx, gfy, gfz, diag);
181 });
182}
183// Face-form siblings of cutcellSmoothColor / cutcellSmoothColorBox (same launch forms).
184template <class OpV>
185inline void cutcellSmoothColorFace(CCField phi, CCConst b, OpV AC, OpV AFX, OpV AFY, OpV AFZ, C3 e,
186 C3 og, int g, int color) {
187 CCExec space;
188 if constexpr (std::is_same_v<typename CCExec::memory_space, Kokkos::HostSpace>) {
189 const int nyi = e.y - 2 * g, nzi = e.z - 2 * g;
190 const long cells = (long)nyi * nzi * (e.x - 2 * g);
191 auto pencil = KOKKOS_LAMBDA(long t) {
192 const int ly = g + (int)(t % nyi), lz = g + (int)(t / nyi);
193 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
194 const int P = (color + og.x + og.y + ly + og.z + lz) & 1;
195 PECLET_FLOW_OMP_SIMD // same-colour cells are independent (ccFor3's contract)
196 for (int lx = g + ((P ^ (g & 1)) & 1); lx < e.x - g; lx += 2) {
197 const long i = (long)lx + (long)ly * sy + (long)lz * sz;
198 cutcellSmoothFaceCell(phi, b, AC, AFX, AFY, AFZ, i, sx, sy, sz, i + sx, i - sx, i + sy,
199 i - sy, i + sz, i - sz);
200 }
201 };
202 if (hostRunSerial(cells)) { // coarse MG level: the fork/join costs more than the sweep
203 const HostSerialRegion serialRegion("peclet::flow::cc_smooth@serial"); // WO-T
204 for (long t = 0; t < (long)nyi * nzi; ++t)
205 pencil(t);
206 return;
207 }
208 Kokkos::parallel_for("peclet::flow::cc_smooth",
209 Kokkos::RangePolicy<CCExec>(space, 0, (long)nyi * nzi), pencil);
210 return;
211 }
212 using MD = MDRange3<CCExec>;
213 Kokkos::parallel_for(
214 "peclet::flow::cc_smooth", MD(space, {g, g, g}, {e.x - g, e.y - g, e.z - g}),
215 KOKKOS_LAMBDA(int lx, int ly, int lz) {
216 if (((og.x + lx + og.y + ly + og.z + lz) & 1) != color)
217 return;
218 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
219 const long i = (long)lx + (long)ly * sy + (long)lz * sz;
220 cutcellSmoothFaceCell(phi, b, AC, AFX, AFY, AFZ, i, sx, sy, sz, i + sx, i - sx, i + sy,
221 i - sy, i + sz, i - sz);
222 });
223}
224// K3 (doc/vof_distributed_host_comm_design.md §5.2): `wrapMask` != 0 reads the field operand
225// across the level's rank-local periodic axes in-kernel (ccMaskNbrs; g = the level's ghost width);
226// the coefficients are read as today. wrapMask = 0 (the default) is today's code.
227template <class OpV>
228inline void cutcellSmoothColorBoxFace(CCField phi, CCConst b, OpV AC, OpV AFX, OpV AFY, OpV AFZ,
229 C3 e, C3 og, int color, C3 rlo, C3 rhi, C3 slo, C3 shi,
230 int wrapMask = 0, int g = 1) {
231 if (rhi.x <= rlo.x || rhi.y <= rlo.y || rhi.z <= rlo.z)
232 return;
233 CCExec space;
234 if (wrapMask != 0) {
235 const C3 n{e.x - 2 * g, e.y - 2 * g, e.z - 2 * g};
236 if constexpr (std::is_same_v<typename CCExec::memory_space, Kokkos::HostSpace>) {
237 auto run = [=](int ly, int lz, int x0, int x1) {
238 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
239 const int P = (color + og.x + og.y + ly + og.z + lz) & 1;
240 ccMaskRow(ly, lz, x0 + ((P ^ (x0 & 1)) & 1), x1, 2, e, n, g, wrapMask,
241 [&](long i, long xp, long xm, long yp, long ym, long zp, long zm) {
242 cutcellSmoothFaceCell(phi, b, AC, AFX, AFY, AFZ, i, sx, sy, sz, xp, xm, yp, ym,
243 zp, zm);
244 });
245 };
246 ccBoxShellRuns("peclet::flow::cc_smooth_box", rlo, rhi, slo, shi, run);
247 return;
248 }
249 using MD = MDRange3<CCExec>;
250 Kokkos::parallel_for(
251 "peclet::flow::cc_smooth_box", MD(space, {rlo.x, rlo.y, rlo.z}, {rhi.x, rhi.y, rhi.z}),
252 KOKKOS_LAMBDA(int lx, int ly, int lz) {
253 if (lx >= slo.x && lx < shi.x && ly >= slo.y && ly < shi.y && lz >= slo.z && lz < shi.z)
254 return;
255 if (((og.x + lx + og.y + ly + og.z + lz) & 1) != color)
256 return;
257 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
258 const long i = (long)lx + (long)ly * sy + (long)lz * sz;
259 const CcNbrs w = ccMaskNbrs(lx, ly, lz, n, g, i, sy, sz, wrapMask);
260 cutcellSmoothFaceCell(phi, b, AC, AFX, AFY, AFZ, i, sx, sy, sz, w.xp, w.xm, w.yp, w.ym,
261 w.zp, w.zm);
262 });
263 return;
264 }
265 if constexpr (std::is_same_v<typename CCExec::memory_space, Kokkos::HostSpace>) {
266 // H-7 (§14.3): the shell launch visits only the shell, the interior launch (empty skip box)
267 // whole rows with no skip test; each x-run takes the colour's parity at its own start, `omp
268 // simd` (same-colour cells are independent, ccFor3's contract). Same cells, same body.
269 auto run = [=](int ly, int lz, int x0, int x1) {
270 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
271 const int P = (color + og.x + og.y + ly + og.z + lz) & 1;
273 for (int lx = x0 + ((P ^ (x0 & 1)) & 1); lx < x1; lx += 2) {
274 const long i = (long)lx + (long)ly * sy + (long)lz * sz;
275 cutcellSmoothFaceCell(phi, b, AC, AFX, AFY, AFZ, i, sx, sy, sz, i + sx, i - sx, i + sy,
276 i - sy, i + sz, i - sz);
277 }
278 };
279 ccBoxShellRuns("peclet::flow::cc_smooth_box", rlo, rhi, slo, shi, run);
280 return;
281 }
282 using MD = MDRange3<CCExec>;
283 Kokkos::parallel_for(
284 "peclet::flow::cc_smooth_box", MD(space, {rlo.x, rlo.y, rlo.z}, {rhi.x, rhi.y, rhi.z}),
285 KOKKOS_LAMBDA(int lx, int ly, int lz) {
286 if (lx >= slo.x && lx < shi.x && ly >= slo.y && ly < shi.y && lz >= slo.z && lz < shi.z)
287 return; // inside the skip box (already swept by the interior pass)
288 if (((og.x + lx + og.y + ly + og.z + lz) & 1) != color)
289 return;
290 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
291 const long i = (long)lx + (long)ly * sy + (long)lz * sz;
292 cutcellSmoothFaceCell(phi, b, AC, AFX, AFY, AFZ, i, sx, sy, sz, i + sx, i - sx, i + sy,
293 i - sy, i + sz, i - sz);
294 });
295}
296// Face-form siblings of applyCutcellOp / applyCutcellOpBox.
297template <class OpV>
298inline void applyCutcellOpFace(CCField y, CCConst x, OpV AC, OpV AFX, OpV AFY, OpV AFZ, C3 e,
299 int g) {
300 ccFor3(
301 "peclet::flow::cc_apply", C3{g, g, g}, C3{e.x - g, e.y - g, e.z - g},
302 KOKKOS_LAMBDA(int lx, int ly, int lz) {
303 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
304 const long i = (long)lx + (long)ly * sy + (long)lz * sz;
305 y(i) = cutcellApplyFaceCell(x, AC, AFX, AFY, AFZ, i, sx, sy, sz, i + sx, i - sx, i + sy,
306 i - sy, i + sz, i - sz);
307 });
308}
309template <class OpV>
310inline void applyCutcellOpBoxFace(CCField y, CCConst x, OpV AC, OpV AFX, OpV AFY, OpV AFZ, C3 e,
311 C3 rlo, C3 rhi, C3 slo, C3 shi, int wrapMask = 0, int g = 1) {
312 if (rhi.x <= rlo.x || rhi.y <= rlo.y || rhi.z <= rlo.z)
313 return;
314 if (wrapMask != 0) { // K3 (§5.2): see cutcellSmoothColorBoxFace
315 const C3 n{e.x - 2 * g, e.y - 2 * g, e.z - 2 * g};
316 if constexpr (std::is_same_v<typename CCExec::memory_space, Kokkos::HostSpace>) {
317 auto run = [=](int ly, int lz, int x0, int x1) {
318 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
319 ccMaskRow(ly, lz, x0, x1, 1, e, n, g, wrapMask,
320 [&](long i, long xp, long xm, long yp, long ym, long zp, long zm) {
321 y(i) = cutcellApplyFaceCell(x, AC, AFX, AFY, AFZ, i, sx, sy, sz, xp, xm, yp, ym,
322 zp, zm);
323 });
324 };
325 ccBoxShellRuns("peclet::flow::cc_apply_box", rlo, rhi, slo, shi, run);
326 return;
327 }
328 ccFor3(
329 "peclet::flow::cc_apply_box", C3{rlo.x, rlo.y, rlo.z}, C3{rhi.x, rhi.y, rhi.z},
330 KOKKOS_LAMBDA(int lx, int ly, int lz) {
331 if (lx >= slo.x && lx < shi.x && ly >= slo.y && ly < shi.y && lz >= slo.z && lz < shi.z)
332 return;
333 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
334 const long i = (long)lx + (long)ly * sy + (long)lz * sz;
335 const CcNbrs w = ccMaskNbrs(lx, ly, lz, n, g, i, sy, sz, wrapMask);
336 y(i) = cutcellApplyFaceCell(x, AC, AFX, AFY, AFZ, i, sx, sy, sz, w.xp, w.xm, w.yp, w.ym,
337 w.zp, w.zm);
338 });
339 return;
340 }
341 if constexpr (std::is_same_v<typename CCExec::memory_space, Kokkos::HostSpace>) {
342 // H-7 (§14.3): shell-only shell launch, skip-test-free interior launch (ccBoxShellRuns).
343 auto run = [=](int ly, int lz, int x0, int x1) {
344 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
346 for (int lx = x0; lx < x1; ++lx) {
347 const long i = (long)lx + (long)ly * sy + (long)lz * sz;
348 y(i) = cutcellApplyFaceCell(x, AC, AFX, AFY, AFZ, i, sx, sy, sz, i + sx, i - sx, i + sy,
349 i - sy, i + sz, i - sz);
350 }
351 };
352 ccBoxShellRuns("peclet::flow::cc_apply_box", rlo, rhi, slo, shi, run);
353 return;
354 }
355 ccFor3(
356 "peclet::flow::cc_apply_box", C3{rlo.x, rlo.y, rlo.z}, C3{rhi.x, rhi.y, rhi.z},
357 KOKKOS_LAMBDA(int lx, int ly, int lz) {
358 if (lx >= slo.x && lx < shi.x && ly >= slo.y && ly < shi.y && lz >= slo.z && lz < shi.z)
359 return;
360 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
361 const long i = (long)lx + (long)ly * sy + (long)lz * sz;
362 y(i) = cutcellApplyFaceCell(x, AC, AFX, AFY, AFZ, i, sx, sy, sz, i + sx, i - sx, i + sy,
363 i - sy, i + sz, i - sz);
364 });
365}
366
367// Open-weighted flux divergence d_i = sum_f signed(o_f * face-velocity), consistent with A
368// (diverg_open_k).
369inline void divergOpen(CCConst u, CCConst v, CCConst w, CCConst ox, CCConst oy, CCConst oz,
370 CCField d, C3 e, int g) {
371 ccFor3(
372 "peclet::flow::diverg_open", C3{g, g, g}, C3{e.x - g, e.y - g, e.z - g},
373 KOKKOS_LAMBDA(int x, int y, int z) {
374 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
375 const long i = (long)x + (long)y * sy + (long)z * sz;
376 d(i) = (ox(i + sx) * u(i + sx) - ox(i) * u(i)) + (oy(i + sy) * v(i + sy) - oy(i) * v(i)) +
377 (oz(i + sz) * w(i + sz) - oz(i) * w(i));
378 });
379}
380
381// One red/black sweep of the variable operator: phi=(b - offdiag)/AC; AC~0 (fully solid) cells
382// decoupled. b carries the negated divergence so the system is A phi = -div(u*) (matches the
383// validated const-coeff sign).
384template <class OpV>
385inline void cutcellSmoothColor(CCField phi, CCConst b, OpV AC, OpV AW, OpV AE, OpV AS, OpV AN,
386 OpV AB, OpV AT, C3 e, C3 og, int g, int color) {
387 CCExec space;
388 // Host backends: line-sweep form (one (y,z) pencil per task, stride-2 x-loop at the colour's
389 // parity) — bit-identical (same-colour cells are independent); device keeps MDRange untouched.
390 if constexpr (std::is_same_v<typename CCExec::memory_space, Kokkos::HostSpace>) {
391 const int nyi = e.y - 2 * g, nzi = e.z - 2 * g;
392 const long cells = (long)nyi * nzi * (e.x - 2 * g);
393 auto pencil = KOKKOS_LAMBDA(long t) {
394 const int ly = g + (int)(t % nyi), lz = g + (int)(t / nyi);
395 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
396 const int P = (color + og.x + og.y + ly + og.z + lz) & 1;
397 PECLET_FLOW_OMP_SIMD // same-colour cells are independent (ccFor3's contract)
398 for (int lx = g + ((P ^ (g & 1)) & 1); lx < e.x - g; lx += 2) {
399 const long i = (long)lx + (long)ly * sy + (long)lz * sz;
400 cutcellSmoothCell(phi, b, AC, AW, AE, AS, AN, AB, AT, i, i + sx, i - sx, i + sy, i - sy,
401 i + sz, i - sz);
402 }
403 };
404 if (hostRunSerial(cells)) { // coarse MG level: the fork/join costs more than the sweep
405 const HostSerialRegion serialRegion("peclet::flow::cc_smooth@serial"); // WO-T
406 for (long t = 0; t < (long)nyi * nzi; ++t)
407 pencil(t);
408 return;
409 }
410 Kokkos::parallel_for("peclet::flow::cc_smooth",
411 Kokkos::RangePolicy<CCExec>(space, 0, (long)nyi * nzi), pencil);
412 return;
413 }
414 using MD = MDRange3<CCExec>;
415 Kokkos::parallel_for(
416 "peclet::flow::cc_smooth", MD(space, {g, g, g}, {e.x - g, e.y - g, e.z - g}),
417 KOKKOS_LAMBDA(int lx, int ly, int lz) {
418 if (((og.x + lx + og.y + ly + og.z + lz) & 1) != color)
419 return;
420 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
421 const long i = (long)lx + (long)ly * sy + (long)lz * sz;
422 cutcellSmoothCell(phi, b, AC, AW, AE, AS, AN, AB, AT, i, i + sx, i - sx, i + sy, i - sy,
423 i + sz, i - sz);
424 });
425}
426
427// Box-restricted red/black sweep for the distributed overlap smoother: sweeps [rlo,rhi) only,
428// skipping the box [slo,shi) (pass slo==shi to skip nothing). Used to split one color's sweep into
429// an INTERIOR pass (cells whose 7-point stencil reads no ghost cell — runs while the halo exchange
430// is in flight) and a boundary-SHELL pass (after the exchange lands). A color's cells never read
431// same-color cells, so interior-then-shell ordering is bit-identical to the full blocking sweep
432// (and a cell swept twice recomputes the identical value — overlapping shell slabs are safe).
433template <class OpV>
434inline void cutcellSmoothColorBox(CCField phi, CCConst b, OpV AC, OpV AW, OpV AE, OpV AS, OpV AN,
435 OpV AB, OpV AT, C3 e, C3 og, int color, C3 rlo, C3 rhi, C3 slo,
436 C3 shi) {
437 if (rhi.x <= rlo.x || rhi.y <= rlo.y || rhi.z <= rlo.z)
438 return;
439 CCExec space;
440 if constexpr (std::is_same_v<typename CCExec::memory_space, Kokkos::HostSpace>) {
441 // Host line-sweep form of the box sweep (see cutcellSmoothColor); the (ly,lz)-level skip test
442 // hoists out of the x-loop, the x-range skip stays per cell.
443 const int nyi = rhi.y - rlo.y, nzi = rhi.z - rlo.z;
444 const long cells = (long)nyi * nzi * (rhi.x - rlo.x);
445 auto pencil = KOKKOS_LAMBDA(long t) {
446 const int ly = rlo.y + (int)(t % nyi), lz = rlo.z + (int)(t / nyi);
447 const bool yzSkip = ly >= slo.y && ly < shi.y && lz >= slo.z && lz < shi.z;
448 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
449 const int P = (color + og.x + og.y + ly + og.z + lz) & 1;
450 for (int lx = rlo.x + ((P ^ (rlo.x & 1)) & 1); lx < rhi.x; lx += 2) {
451 if (yzSkip && lx >= slo.x && lx < shi.x)
452 continue; // inside the skip box (already swept by the interior pass)
453 const long i = (long)lx + (long)ly * sy + (long)lz * sz;
454 cutcellSmoothCell(phi, b, AC, AW, AE, AS, AN, AB, AT, i, i + sx, i - sx, i + sy, i - sy,
455 i + sz, i - sz);
456 }
457 };
458 if (hostRunSerial(cells)) { // coarse MG level: the fork/join costs more than the sweep
459 const HostSerialRegion serialRegion("peclet::flow::cc_smooth_box@serial"); // WO-T
460 for (long t = 0; t < (long)nyi * nzi; ++t)
461 pencil(t);
462 return;
463 }
464 Kokkos::parallel_for("peclet::flow::cc_smooth_box",
465 Kokkos::RangePolicy<CCExec>(space, 0, (long)nyi * nzi), pencil);
466 return;
467 }
468 using MD = MDRange3<CCExec>;
469 Kokkos::parallel_for(
470 "peclet::flow::cc_smooth_box", MD(space, {rlo.x, rlo.y, rlo.z}, {rhi.x, rhi.y, rhi.z}),
471 KOKKOS_LAMBDA(int lx, int ly, int lz) {
472 if (lx >= slo.x && lx < shi.x && ly >= slo.y && ly < shi.y && lz >= slo.z && lz < shi.z)
473 return; // inside the skip box (already swept by the interior pass)
474 if (((og.x + lx + og.y + ly + og.z + lz) & 1) != color)
475 return;
476 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
477 const long i = (long)lx + (long)ly * sy + (long)lz * sz;
478 cutcellSmoothCell(phi, b, AC, AW, AE, AS, AN, AB, AT, i, i + sx, i - sx, i + sy, i - sy,
479 i + sz, i - sz);
480 });
481}
482
483// y = A x for the cut-cell operator over inner cells (matvec for PCG; mg_apply_var_k port).
484template <class OpV>
485inline void applyCutcellOp(CCField y, CCConst x, OpV AC, OpV AW, OpV AE, OpV AS, OpV AN, OpV AB,
486 OpV AT, C3 e, int g) {
487 ccFor3(
488 "peclet::flow::cc_apply", C3{g, g, g}, C3{e.x - g, e.y - g, e.z - g},
489 KOKKOS_LAMBDA(int lx, int ly, int lz) {
490 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
491 const long i = (long)lx + (long)ly * sy + (long)lz * sz;
492 y(i) = cutcellApplyCell(x, AC, AW, AE, AS, AN, AB, AT, i, i + sx, i - sx, i + sy, i - sy,
493 i + sz, i - sz);
494 });
495}
496
497// Box-restricted matvec for the distributed overlap: rows in [rlo,rhi) skipping [slo,shi). Reads
498// x, writes y (no aliasing), so the interior rows can apply while x's ghost exchange is in flight
499// and the shell rows apply after it lands — trivially bit-identical to the blocking order.
500template <class OpV>
501inline void applyCutcellOpBox(CCField y, CCConst x, OpV AC, OpV AW, OpV AE, OpV AS, OpV AN, OpV AB,
502 OpV AT, C3 e, C3 rlo, C3 rhi, C3 slo, C3 shi) {
503 if (rhi.x <= rlo.x || rhi.y <= rlo.y || rhi.z <= rlo.z)
504 return;
505 ccFor3(
506 "peclet::flow::cc_apply_box", C3{rlo.x, rlo.y, rlo.z}, C3{rhi.x, rhi.y, rhi.z},
507 KOKKOS_LAMBDA(int lx, int ly, int lz) {
508 if (lx >= slo.x && lx < shi.x && ly >= slo.y && ly < shi.y && lz >= slo.z && lz < shi.z)
509 return;
510 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
511 const long i = (long)lx + (long)ly * sy + (long)lz * sz;
512 y(i) = cutcellApplyCell(x, AC, AW, AE, AS, AN, AB, AT, i, i + sx, i - sx, i + sy, i - sy,
513 i + sz, i - sz);
514 });
515}
516
517// --- Exact (matrix-free, double) level-0 apply — the defect-correction residual/matvec ---------
518//
519// P1 of the suite-wide defect-correction campaign (docs/DEFECT_CORRECTION_PLAN.md). The band form
520// above reads the STORED coefficients, which are float by default (MReal), so the operator the
521// Krylov method actually inverts is the fp32-rounded one and its singular row-sum identity
522// A*1 = 0 holds only to eps_f32 (WO-M measured the consequence: a resolution-independent residual
523// floor at ~5e-9 followed by a 1e4-1e5 rebound). These two kernels apply the SAME operator
524// matrix-free from the double face openness that buildCutcellOp assembles the bands from
525// (Level::ox/oy/oz, CCField = View<double*>, ghost-filled and boundary-re-imposed by
526// setOpenness) — so they need no new storage, and they read 24 B/cell of coefficient instead of
527// 28.
528//
529// Written in FLUX (difference) form, y_i = sum_f t_f (x_i - x_nbr) with t_f = open_f * gf:
530// * the diagonal is never formed, so it can never disagree with the faces;
531// * a constant vector is annihilated BITWISE whatever the precision of t_f, because every
532// difference vanishes identically. A stored double diagonal only gets A*1 = 0 to eps_f64.
533// * a fully-closed (solid) cell has every t_f = 0, so y_i = 0 exactly, as with the bands
534// (AC = 0 there). Where the openness is tiny-but-nonzero the two DISAGREE, and that is the
535// point: the band AC is the float-rounded sum of the six faces, this is the exact one.
536// Sign convention matches buildCutcellOp exactly: AC = sum(t_f), A<face> = -t_f, hence
537// AC*x_i + sum(-t_f*x_nbr) == sum_f t_f (x_i - x_nbr).
538//
539// The hierarchy below level 0 is untouched — it is the preconditioner and may stay float.
541 int g, double gfx, double gfy, double gfz) {
542 ccFor3(
543 "peclet::flow::cc_apply_exact", C3{g, g, g}, C3{e.x - g, e.y - g, e.z - g},
544 KOKKOS_LAMBDA(int lx, int ly, int lz) {
545 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
546 const long i = (long)lx + (long)ly * sy + (long)lz * sz;
547 y(i) = cutcellApplyExactCell(x, ox, oy, oz, i, sx, sy, sz, i + sx, i - sx, i + sy, i - sy,
548 i + sz, i - sz, gfx, gfy, gfz);
549 });
550}
551
552// Box-restricted sibling of applyCutcellOpExact, for the distributed overlap matvec: rows in
553// [rlo,rhi) skipping [slo,shi). Same skip-box signature as applyCutcellOpBox, and the same
554// argument for bit-identity with the blocking form — it reads x and the openness, writes y, no
555// aliasing — so interior-then-shell ordering reproduces the blocking apply exactly.
557 C3 rlo, C3 rhi, C3 slo, C3 shi, double gfx, double gfy,
558 double gfz, int wrapMask = 0, int g = 1) {
559 if (rhi.x <= rlo.x || rhi.y <= rlo.y || rhi.z <= rlo.z)
560 return;
561 if (wrapMask != 0) { // K3 (§5.2): see cutcellSmoothColorBoxFace
562 const C3 n{e.x - 2 * g, e.y - 2 * g, e.z - 2 * g};
563 if constexpr (std::is_same_v<typename CCExec::memory_space, Kokkos::HostSpace>) {
564 auto run = [=](int ly, int lz, int x0, int x1) {
565 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
566 ccMaskRow(ly, lz, x0, x1, 1, e, n, g, wrapMask,
567 [&](long i, long xp, long xm, long yp, long ym, long zp, long zm) {
568 y(i) = cutcellApplyExactCell(x, ox, oy, oz, i, sx, sy, sz, xp, xm, yp, ym, zp,
569 zm, gfx, gfy, gfz);
570 });
571 };
572 ccBoxShellRuns("peclet::flow::cc_apply_exact_box", rlo, rhi, slo, shi, run);
573 return;
574 }
575 ccFor3(
576 "peclet::flow::cc_apply_exact_box", C3{rlo.x, rlo.y, rlo.z}, C3{rhi.x, rhi.y, rhi.z},
577 KOKKOS_LAMBDA(int lx, int ly, int lz) {
578 if (lx >= slo.x && lx < shi.x && ly >= slo.y && ly < shi.y && lz >= slo.z && lz < shi.z)
579 return;
580 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
581 const long i = (long)lx + (long)ly * sy + (long)lz * sz;
582 const CcNbrs w = ccMaskNbrs(lx, ly, lz, n, g, i, sy, sz, wrapMask);
583 y(i) = cutcellApplyExactCell(x, ox, oy, oz, i, sx, sy, sz, w.xp, w.xm, w.yp, w.ym, w.zp,
584 w.zm, gfx, gfy, gfz);
585 });
586 return;
587 }
588 ccFor3(
589 "peclet::flow::cc_apply_exact_box", C3{rlo.x, rlo.y, rlo.z}, C3{rhi.x, rhi.y, rhi.z},
590 KOKKOS_LAMBDA(int lx, int ly, int lz) {
591 if (lx >= slo.x && lx < shi.x && ly >= slo.y && ly < shi.y && lz >= slo.z && lz < shi.z)
592 return;
593 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
594 const long i = (long)lx + (long)ly * sy + (long)lz * sz;
595 y(i) = cutcellApplyExactCell(x, ox, oy, oz, i, sx, sy, sz, i + sx, i - sx, i + sy, i - sy,
596 i + sz, i - sz, gfx, gfy, gfz);
597 });
598}
599
600// PHASE 2 (anisotropic cells), doc/anisotropic_metric.md §1.3/§3: every correction kernel below
601// takes the per-axis pressure weight `w_a = 1/h_a'^2` and applies it OUTSIDE the whole existing
602// expression (`u -= w_x * (today's term)`) — the same weight `CutcellMG::setOpenness` puts on the
603// operator row of that axis, which is what makes the corrected open flux telescope. On the
604// isotropic path `w == 1.0` exactly and `1.0 * x == x` in IEEE-754, so these are bit-identical;
605// the defaulted arguments keep the kernel-level tests calling the historical signature.
606//
607// Projection correction u -= grad(phi) on the staggered faces (correct_k port). No openness here —
608// the openness lives in the operator + divergence; closed faces carry phi~0 on both sides so stay
609// unchanged.
610inline void projectCorrect(CCField u, CCField v, CCField w, CCConst phi, C3 e, int g,
611 double wx = 1.0, double wy = 1.0, double wz = 1.0) {
612 ccFor3(
613 "peclet::flow::correct", C3{g, g, g}, C3{e.x - g, e.y - g, e.z - g},
614 KOKKOS_LAMBDA(int x, int y, int z) {
615 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
616 const long i = (long)x + (long)y * sy + (long)z * sz;
617 u(i) -= wx * (phi(i) - phi(i - sx));
618 v(i) -= wy * (phi(i) - phi(i - sy));
619 w(i) -= wz * (phi(i) - phi(i - sz));
620 });
621}
622
623// Variable-density projection correction (sibling of projectCorrect): u_f -= (rho0/rho_f) grad(phi)
624// with rho_f the arithmetic face mean — the SAME face density that scaled the Poisson coefficient
625// c_f = open_f*rho0/rho_f, so the corrected open flux telescopes to A*phi exactly (discrete
626// consistency; constant rho == rho0 reduces to projectCorrect identically, ratio 1.0 exact in FP).
628 double rho0, C3 e, int g, double wx = 1.0, double wy = 1.0,
629 double wz = 1.0) {
630 ccFor3(
631 "peclet::flow::correct_var", C3{g, g, g}, C3{e.x - g, e.y - g, e.z - g},
632 KOKKOS_LAMBDA(int x, int y, int z) {
633 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
634 const long i = (long)x + (long)y * sy + (long)z * sz;
635 u(i) -= wx * (rho0 / (0.5 * (rho(i) + rho(i - sx))) * (phi(i) - phi(i - sx)));
636 v(i) -= wy * (rho0 / (0.5 * (rho(i) + rho(i - sy))) * (phi(i) - phi(i - sy)));
637 w(i) -= wz * (rho0 / (0.5 * (rho(i) + rho(i - sz))) * (phi(i) - phi(i - sz)));
638 });
639}
640
641// Variable-density Poisson face coefficients on the MG (g=1) block: c_f = open_f * rho0 / rho_f,
642// rho_f = arithmetic face mean. Computed over the inner cells only — CutcellMG::setOpenness runs
643// its own periodic/halo ghost fill + non-periodic boundary re-imposition on whatever level-0 fields
644// it receives, exactly as for the raw openness (the coefficient "rides the openness rails"). The
645// rho ghost ring of the g=1 block must be valid (bridged from the filled G=2 field).
646inline void buildRhoCoeff(CCField cx, CCField cy, CCField cz, CCConst ox, CCConst oy, CCConst oz,
647 CCConst rho, double rho0, C3 e, int g) {
648 ccFor3(
649 "peclet::flow::rho_coeff", C3{g, g, g}, C3{e.x - g, e.y - g, e.z - g},
650 KOKKOS_LAMBDA(int x, int y, int z) {
651 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
652 const long i = (long)x + (long)y * sy + (long)z * sz;
653 cx(i) = ox(i) * rho0 / (0.5 * (rho(i) + rho(i - sx)));
654 cy(i) = oy(i) * rho0 / (0.5 * (rho(i) + rho(i - sy)));
655 cz(i) = oz(i) * rho0 / (0.5 * (rho(i) + rho(i - sz)));
656 });
657}
658
659// --- Harmonic rho_f face mean (VoF rung V2a / WO-J item 5; the flagged coarsening trap of
660// MULTIPHYSICS_PLAN.md:474) -------------------------------------------------------------------
661//
662// SIBLINGS of buildRhoCoeff / projectCorrectVar with rho_f = 2*rho_a*rho_b/(rho_a+rho_b) instead of
663// the arithmetic mean. Default OFF (set_rho_face_harmonic); the validated arithmetic kernels above
664// are untouched and stay the default.
665//
666// Read this before turning it on. The two means are NOT interchangeable, and the arithmetic one is
667// the correct fine-level choice for THIS discretization, for two independent reasons:
668// 1. The face coefficient is c_f = open_f * rho0/rho_f, i.e. proportional to the mobility
669// beta = 1/rho. The ARITHMETIC mean of rho is exactly the HARMONIC mean of beta — the
670// series-resistance / homogenization-correct choice for a flux crossing two half-cells
671// (VOF_PLAN.md §5). Taking rho harmonic makes beta arithmetic, i.e. the parallel rule, which
672// is the wrong one for a normal flux.
673// 2. Discrete hydrostatic balance is EXACT only because the momentum time term, the
674// face-interpolated body force, and the projection coefficient use the SAME rho_f
675// (doc/variable_density_projection.md §1/§3). The momentum side interpolates rho
676// arithmetically (mass is volume-additive on the staggered control volume) and is not part of
677// this switch, so switching only the projection breaks that three-way consistency and leaves
678// a permanent spurious velocity at the interface. This is measured, not asserted — see the
679// WO-J findings entry.
680// The knob exists because arithmetic COARSENING of the coefficient is what makes the V-cycle
681// preconditioner indefinite past ratio ~1e3 (doc/variable_density_projection.md §2), so the
682// question "does a harmonic face mean help the solver" needs to be answerable by measurement
683// rather than by argument. It ships measured.
684//
685// The projection stays an EXACT projection either way: the operator coefficient and the velocity
686// correction must use the same rho_f or the corrected open flux no longer telescopes to A*phi, so
687// the flag switches BOTH kernels together (IbmSolver::project / projectVelocities).
689 CCConst oz, CCConst rho, double rho0, C3 e, int g) {
690 ccFor3(
691 "peclet::flow::rho_coeff_harm", C3{g, g, g}, C3{e.x - g, e.y - g, e.z - g},
692 KOKKOS_LAMBDA(int x, int y, int z) {
693 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
694 const long i = (long)x + (long)y * sy + (long)z * sz;
695 // 2 a b/(a+b) written as rho0*(a+b)/(2ab) for the coefficient rho0/rho_f.
696 cx(i) = ox(i) * rho0 * (rho(i) + rho(i - sx)) / (2.0 * rho(i) * rho(i - sx));
697 cy(i) = oy(i) * rho0 * (rho(i) + rho(i - sy)) / (2.0 * rho(i) * rho(i - sy));
698 cz(i) = oz(i) * rho0 * (rho(i) + rho(i - sz)) / (2.0 * rho(i) * rho(i - sz));
699 });
700}
701
702// WO-R2 item 1 — the variable-density coefficient of the HIGH-side outflow domain face.
703//
704// `buildRhoCoeff` runs over the INNER cells, and the staggered face index of a cell is its LOW
705// face, so the low domain face of an axis (inner index `g`) is covered and the high one
706// (`dims-g`, a ghost index) is not. `CutcellMG::applyBoundaryOpenness` used to fill that gap with
707// the literal openness 1.0, which is right for the raw-openness operator and wrong by the density
708// ratio for the coefficient one. This writes the missing plane, `open_f * rho0/rho_f` with:
709// * open_f = 1 — the same fully-open value the raw path imposed there (the g=1 coefficient
710// block's ghost ring carries no geometric openness of its own; a solid cutting an OUTFLOW
711// plane is a separate, pre-existing gap that the raw path has too);
712// * rho_f = the arithmetic (or harmonic) face mean of the LAST INNER cell and the domain ghost,
713// literally the expression `bcCorrectOutflowVar` applies at that face — which is what makes
714// the projection there exact. Under the Neumann property ghost (`fillPropGhosts`) both
715// evaluate to the last inner cell's own rho.
716// The whole transverse plane is written (ghost ring included) so that a distributed CA level's
717// ring rows coarsen from a valid fine plane; `rho`'s ghost ring must be filled first, which is
718// what the bridge in IbmSolver::project guarantees.
719inline void buildRhoCoeffOutflowFace(CCField ca, CCConst oa, CCConst rho, double rho0, C3 e, int g,
720 int a, bool harmonic) {
721 CCExec space;
722 int dims[3] = {e.x, e.y, e.z};
723 long st[3] = {1, (long)e.x, (long)e.x * e.y};
724 const int b = (a + 1) % 3, c = (a + 2) % 3;
725 const long sa = st[a], sb = st[b], sc = st[c];
726 const int bf = dims[a] - g;
727 using MD = MDRange2<CCExec>;
728 Kokkos::parallel_for(
729 "peclet::flow::rho_coeff_outflow", MD(space, {0, 0}, {dims[b], dims[c]}),
730 KOKKOS_LAMBDA(int p0, int p1) {
731 const long i = (long)p0 * sb + (long)p1 * sc + (long)bf * sa;
732 const double ra = rho(i), rb = rho(i - sa);
733 const double rf = harmonic ? (2.0 * ra * rb / (ra + rb)) : (0.5 * (ra + rb));
734 // SCALING_ISSUES #3: `oa(i)` is this face's cut-cell aperture, which every OTHER face of
735 // the coefficient carries (buildRhoCoeff multiplies by it) and this one used to drop --
736 // invisible while the outlet is clear of solid (aperture 1), an operator that disagrees
737 // with the divergence constraint by (1 - aperture) once a solid cuts the outlet.
738 ca(i) = oa(i) * rho0 / rf;
739 });
740}
741
743 double rho0, C3 e, int g, double wx = 1.0, double wy = 1.0,
744 double wz = 1.0) {
745 ccFor3(
746 "peclet::flow::correct_var_harm", C3{g, g, g}, C3{e.x - g, e.y - g, e.z - g},
747 KOKKOS_LAMBDA(int x, int y, int z) {
748 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
749 const long i = (long)x + (long)y * sy + (long)z * sz;
750 u(i) -= wx * (rho0 * (rho(i) + rho(i - sx)) / (2.0 * rho(i) * rho(i - sx)) *
751 (phi(i) - phi(i - sx)));
752 v(i) -= wy * (rho0 * (rho(i) + rho(i - sy)) / (2.0 * rho(i) * rho(i - sy)) *
753 (phi(i) - phi(i - sy)));
754 w(i) -= wz * (rho0 * (rho(i) + rho(i - sz)) / (2.0 * rho(i) * rho(i - sz)) *
755 (phi(i) - phi(i - sz)));
756 });
757}
758
759// --- Volume-averaged (porous) continuity for unresolved CFD-DEM
760// ----------------------------------- The proper continuity is d(eps)/dt + div(eps u) = 0 (eps =
761// void fraction from the particles), so the velocity is NOT solenoidal: div(eps u) = -d(eps)/dt.
762// These size the projection to that constraint.
763
764// eps-weighted open-face divergence: d = div(open * eps_f * u), eps_f = arithmetic face mean.
765// Reduces to divergOpen when eps == 1 everywhere (no particles).
767 CCConst eps, CCField d, C3 e, int g) {
768 ccFor3(
769 "peclet::flow::diverg_open_eps", C3{g, g, g}, C3{e.x - g, e.y - g, e.z - g},
770 KOKKOS_LAMBDA(int x, int y, int z) {
771 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
772 const long i = (long)x + (long)y * sy + (long)z * sz;
773 const double exp = 0.5 * (eps(i) + eps(i + sx)), exm = 0.5 * (eps(i) + eps(i - sx));
774 const double eyp = 0.5 * (eps(i) + eps(i + sy)), eym = 0.5 * (eps(i) + eps(i - sy));
775 const double ezp = 0.5 * (eps(i) + eps(i + sz)), ezm = 0.5 * (eps(i) + eps(i - sz));
776 d(i) = (ox(i + sx) * exp * u(i + sx) - ox(i) * exm * u(i)) +
777 (oy(i + sy) * eyp * v(i + sy) - oy(i) * eym * v(i)) +
778 (oz(i + sz) * ezp * w(i + sz) - oz(i) * ezm * w(i));
779 });
780}
781
782// Porous Poisson face coefficient c_f = open_f * eps_f (eps_f = arithmetic face mean).
783// Constant-density gas: the correction stays u -= grad(phi) (projectCorrect) so the open*eps flux
784// telescopes to A*phi. (Combining with variable rho — c_f *= rho0/rho_f, projectCorrectVar — is a
785// later composition.)
786// SCALING_ISSUES #3: the three porous builders run ONE index past the inner block along each axis,
787// so the HIGH domain face of each axis -- a ghost index no other kernel writes, and the face the
788// MG's `applyBoundaryOpenness` carries at a Dirichlet outlet -- is built by the same formula as
789// every other face, cut-cell aperture `o(i)` included. The extra plane is read-only elsewhere (the
790// per-level ghost fill overwrites it) and every operand it reads is a ghost-filled index.
791inline void buildPorousCoeff(CCField cx, CCField cy, CCField cz, CCConst ox, CCConst oy, CCConst oz,
792 CCConst eps, C3 e, int g) {
793 ccFor3(
794 "peclet::flow::porous_coeff", C3{g, g, g}, C3{e.x - g + 1, e.y - g + 1, e.z - g + 1},
795 KOKKOS_LAMBDA(int x, int y, int z) {
796 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
797 const long i = (long)x + (long)y * sy + (long)z * sz;
798 cx(i) = ox(i) * 0.5 * (eps(i) + eps(i - sx));
799 cy(i) = oy(i) * 0.5 * (eps(i) + eps(i - sy));
800 cz(i) = oz(i) * 0.5 * (eps(i) + eps(i - sz));
801 });
802}
803
804// Semi-implicit-drag porous coefficient: c_f = open_f * eps_f * w_f, with the face drag-relaxation
805// w_f = idt/(idt + beta_f) (idt = rho/dt, beta = the momentum-diagonal drag coefficient). This
806// makes the pressure correction CONSISTENT with the drag-loaded momentum diagonal A_P = idt + beta:
807// where the drag is stiff (dense bed) w_f -> 0 and the pressure barely moves the velocity (the drag
808// holds it) — the SIMPLE/PISO-with-implicit-drag scheme (OpenFOAM rAU, MFIX). Reduces to
809// buildPorousCoeff when beta==0 (w==1). The correction MUST use the same w_f
810// (projectCorrectPorousDrag) so the open*eps*w flux telescopes to A*phi.
812 CCConst oz, CCConst eps, CCConst beta, double idt, C3 e, int g) {
813 ccFor3(
814 "peclet::flow::porous_coeff_drag", C3{g, g, g}, C3{e.x - g + 1, e.y - g + 1, e.z - g + 1},
815 KOKKOS_LAMBDA(int x, int y, int z) {
816 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
817 const long i = (long)x + (long)y * sy + (long)z * sz;
818 cx(i) = ox(i) * 0.5 * (eps(i) + eps(i - sx)) * idt / (idt + 0.5 * (beta(i) + beta(i - sx)));
819 cy(i) = oy(i) * 0.5 * (eps(i) + eps(i - sy)) * idt / (idt + 0.5 * (beta(i) + beta(i - sy)));
820 cz(i) = oz(i) * 0.5 * (eps(i) + eps(i - sz)) * idt / (idt + 0.5 * (beta(i) + beta(i - sz)));
821 });
822}
823
824// --- eps-CONSERVATIVE porous projection pair -------------------------------------------------
825// For the conservative volume-averaged momentum (time term (eps_f rho/dt) u, Model-B full -grad p)
826// the face momentum diagonal is D_f = eps_f*rho*idt + beta_f, the velocity correction is
827// u_f -= (rho*idt / D_f) * grad(phi) (beta=0, eps=1 -> plain projectCorrect exactly)
828// and the flux constraint div(eps u) = rhs makes the Poisson coefficient
829// c_f = open_f * eps_f * (rho*idt/D_f) = open_f * (eps_f*rho*idt) / D_f
830// — the eps of the flux cancels against the eps of the inertia, unlike the plain-u pair above
831// (buildPorousCoeff*/projectCorrectPorousDrag), which is consistent only for the non-conservative
832// momentum form and kinematically drags gas along with the moving porosity (energy injection).
834 CCConst oz, CCConst eps, CCConst beta, bool useBeta, double rhoidt,
835 C3 e, int g) {
836 ccFor3(
837 "peclet::flow::porous_coeff_cons", C3{g, g, g}, C3{e.x - g + 1, e.y - g + 1, e.z - g + 1},
838 KOKKOS_LAMBDA(int x, int y, int z) {
839 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
840 const long i = (long)x + (long)y * sy + (long)z * sz;
841 auto cf = [&](long s, CCConst o) {
842 const double epsF = 0.5 * (eps(i) + eps(i - s));
843 const double bF = useBeta ? 0.5 * (beta(i) + beta(i - s)) : 0.0;
844 const double inert = epsF * rhoidt;
845 return o(i) * inert / (inert + bF) * 1.0;
846 };
847 cx(i) = cf(sx, ox);
848 cy(i) = cf(sy, oy);
849 cz(i) = cf(sz, oz);
850 });
851}
853 CCConst beta, bool useBeta, double rhoidt, C3 e, int g,
854 double wx = 1.0, double wy = 1.0, double wz = 1.0) {
855 ccFor3(
856 "peclet::flow::correct_porous_cons", C3{g, g, g}, C3{e.x - g, e.y - g, e.z - g},
857 KOKKOS_LAMBDA(int x, int y, int z) {
858 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
859 const long i = (long)x + (long)y * sy + (long)z * sz;
860 auto wf = [&](long s) {
861 const double epsF = 0.5 * (eps(i) + eps(i - s));
862 const double bF = useBeta ? 0.5 * (beta(i) + beta(i - s)) : 0.0;
863 return rhoidt / (epsF * rhoidt + bF);
864 };
865 u(i) -= wx * (wf(sx) * (phi(i) - phi(i - sx)));
866 v(i) -= wy * (wf(sy) * (phi(i) - phi(i - sy)));
867 w(i) -= wz * (wf(sz) * (phi(i) - phi(i - sz)));
868 });
869}
870
871// Drag-relaxed velocity correction (sibling of projectCorrect): u_f -= w_f * grad(phi), w_f the
872// SAME face drag relaxation as buildPorousCoeffDrag. beta==0 -> w==1 -> projectCorrect exactly.
874 double idt, C3 e, int g, double wx = 1.0, double wy = 1.0,
875 double wz = 1.0) {
876 ccFor3(
877 "peclet::flow::correct_porous_drag", C3{g, g, g}, C3{e.x - g, e.y - g, e.z - g},
878 KOKKOS_LAMBDA(int x, int y, int z) {
879 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
880 const long i = (long)x + (long)y * sy + (long)z * sz;
881 u(i) -= wx * (idt / (idt + 0.5 * (beta(i) + beta(i - sx))) * (phi(i) - phi(i - sx)));
882 v(i) -= wy * (idt / (idt + 0.5 * (beta(i) + beta(i - sy))) * (phi(i) - phi(i - sy)));
883 w(i) -= wz * (idt / (idt + 0.5 * (beta(i) + beta(i - sz))) * (phi(i) - phi(i - sz)));
884 });
885}
886
887} // namespace peclet::flow
888
889#endif // PECLET_FLOW_MAC_PRESSURE_HPP
flow — portable (Kokkos) cut-cell pressure-operator face openness from an SDF.
#define PECLET_FLOW_OMP_SIMD
double cutcellApplyExactCell(const XV &x, const OV &ox, const OV &oy, const OV &oz, long i, long sx, long sy, long sz, long xp, long xm, long yp, long ym, long zp, long zm, double gfx, double gfy, double gfz)
Kokkos::DefaultExecutionSpace CCExec
CcNbrs ccMaskNbrs(int lx, int ly, int lz, C3 n, int g, long i, long sy, long sz, int m)
void buildRhoCoeffOutflowFace(CCField ca, CCConst oa, CCConst rho, double rho0, C3 e, int g, int a, bool harmonic)
void buildPorousCoeffDrag(CCField cx, CCField cy, CCField cz, CCConst ox, CCConst oy, CCConst oz, CCConst eps, CCConst beta, double idt, C3 e, int g)
void applyCutcellOpFace(CCField y, CCConst x, OpV AC, OpV AFX, OpV AFY, OpV AFZ, C3 e, int g)
Kokkos::MDRangePolicy< Exec, Kokkos::Rank< 3, Kokkos::Iterate::Left, Kokkos::Iterate::Left > > MDRange3
3-D range, x (the first index) fastest (both the tile order and the order within a tile).
Definition policy.hpp:36
void cutcellBuildFaceOpCell(const OpV &AC, const OpV &AFX, const OpV &AFY, const OpV &AFZ, const OV &ox, const OV &oy, const OV &oz, long i, long sx, long sy, long sz, double gfx, double gfy, double gfz, bool diag)
void projectCorrectPorousDrag(CCField u, CCField v, CCField w, CCConst phi, CCConst beta, double idt, C3 e, int g, double wx=1.0, double wy=1.0, double wz=1.0)
void buildRhoCoeff(CCField cx, CCField cy, CCField cz, CCConst ox, CCConst oy, CCConst oz, CCConst rho, double rho0, C3 e, int g)
void cutcellResidualFaceCell(const RV &r, const XV &x, const BV &b, const OpV &AC, const OpV &AFX, const OpV &AFY, const OpV &AFZ, long i, long sx, long sy, long sz, long xp, long xm, long yp, long ym, long zp, long zm)
void buildPorousCoeffCons(CCField cx, CCField cy, CCField cz, CCConst ox, CCConst oy, CCConst oz, CCConst eps, CCConst beta, bool useBeta, double rhoidt, C3 e, int g)
void cutcellSmoothFaceCell(const PV &phi, const BV &b, const OpV &AC, const OpV &AFX, const OpV &AFY, const OpV &AFZ, long i, long sx, long sy, long sz, long xp, long xm, long yp, long ym, long zp, long zm)
Kokkos::View< const double *, CCMem > CCConst
void cutcellSmoothColorBoxFace(CCField phi, CCConst b, OpV AC, OpV AFX, OpV AFY, OpV AFZ, C3 e, C3 og, int color, C3 rlo, C3 rhi, C3 slo, C3 shi, int wrapMask=0, int g=1)
void divergOpen(CCConst u, CCConst v, CCConst w, CCConst ox, CCConst oy, CCConst oz, CCField d, C3 e, int g)
void applyCutcellOpBoxFace(CCField y, CCConst x, OpV AC, OpV AFX, OpV AFY, OpV AFZ, C3 e, C3 rlo, C3 rhi, C3 slo, C3 shi, int wrapMask=0, int g=1)
void projectCorrect(CCField u, CCField v, CCField w, CCConst phi, C3 e, int g, double wx=1.0, double wy=1.0, double wz=1.0)
void cutcellSmoothColorBox(CCField phi, CCConst b, OpV AC, OpV AW, OpV AE, OpV AS, OpV AN, OpV AB, OpV AT, C3 e, C3 og, int color, C3 rlo, C3 rhi, C3 slo, C3 shi)
void applyCutcellOpExact(CCField y, CCConst x, CCConst ox, CCConst oy, CCConst oz, C3 e, int g, double gfx, double gfy, double gfz)
void applyCutcellOp(CCField y, CCConst x, OpV AC, OpV AW, OpV AE, OpV AS, OpV AN, OpV AB, OpV AT, C3 e, int g)
void projectCorrectPorousCons(CCField u, CCField v, CCField w, CCConst phi, CCConst eps, CCConst beta, bool useBeta, double rhoidt, C3 e, int g, double wx=1.0, double wy=1.0, double wz=1.0)
void ccBoxShellRuns(const char *name, C3 rlo, C3 rhi, C3 slo, C3 shi, Run run)
void buildCutcellOpFace(OpV AC, OpV AFX, OpV AFY, OpV AFZ, CCConst ox, CCConst oy, CCConst oz, C3 e, int gb, double gfx, double gfy, double gfz)
void cutcellBuildOpCell(const OpV &AC, const OpV &AW, const OpV &AE, const OpV &AS, const OpV &AN, const OpV &AB, const OpV &AT, const OV &ox, const OV &oy, const OV &oz, long i, long sx, long sy, long sz, double gfx, double gfy, double gfz)
void applyCutcellOpExactBox(CCField y, CCConst x, CCConst ox, CCConst oy, CCConst oz, C3 e, C3 rlo, C3 rhi, C3 slo, C3 shi, double gfx, double gfy, double gfz, int wrapMask=0, int g=1)
void cutcellSmoothCell(const PV &phi, const BV &b, const OpV &AC, const OpV &AW, const OpV &AE, const OpV &AS, const OpV &AN, const OpV &AB, const OpV &AT, long i, long xp, long xm, long yp, long ym, long zp, long zm)
double cutcellApplyCell(const XV &x, const OpV &AC, const OpV &AW, const OpV &AE, const OpV &AS, const OpV &AN, const OpV &AB, const OpV &AT, long i, long xp, long xm, long yp, long ym, long zp, long zm)
void cutcellSmoothColorFace(CCField phi, CCConst b, OpV AC, OpV AFX, OpV AFY, OpV AFZ, C3 e, C3 og, int g, int color)
void projectCorrectVar(CCField u, CCField v, CCField w, CCConst phi, CCConst rho, double rho0, C3 e, int g, double wx=1.0, double wy=1.0, double wz=1.0)
Kokkos::View< double *, CCMem > CCField
void ccFor3(const char *name, C3 lo, C3 hi, F f)
void buildCutcellOp(OpV AC, OpV AW, OpV AE, OpV AS, OpV AN, OpV AB, OpV AT, CCConst ox, CCConst oy, CCConst oz, C3 e, int g, double gfx, double gfy, double gfz)
void ccMaskRow(int ly, int lz, int lx0, int x1, int step, C3 e, C3 n, int g, int m, const Cell &cell)
void cutcellSmoothColor(CCField phi, CCConst b, OpV AC, OpV AW, OpV AE, OpV AS, OpV AN, OpV AB, OpV AT, C3 e, C3 og, int g, int color)
void buildPorousCoeff(CCField cx, CCField cy, CCField cz, CCConst ox, CCConst oy, CCConst oz, CCConst eps, C3 e, int g)
void buildRhoCoeffHarm(CCField cx, CCField cy, CCField cz, CCConst ox, CCConst oy, CCConst oz, CCConst rho, double rho0, C3 e, int g)
void divergOpenEps(CCConst u, CCConst v, CCConst w, CCConst ox, CCConst oy, CCConst oz, CCConst eps, CCField d, C3 e, int g)
void projectCorrectVarHarm(CCField u, CCField v, CCField w, CCConst phi, CCConst rho, double rho0, C3 e, int g, double wx=1.0, double wy=1.0, double wz=1.0)
bool hostRunSerial(long cells)
double cutcellApplyFaceCell(const XV &x, const OpV &AC, const OpV &AFX, const OpV &AFY, const OpV &AFZ, long i, long sx, long sy, long sz, long xp, long xm, long yp, long ym, long zp, long zm)
Kokkos::MDRangePolicy< Exec, Kokkos::Rank< 2, Kokkos::Iterate::Left, Kokkos::Iterate::Left > > MDRange2
2-D range, first index fastest (both the tile order and the order within a tile).
Definition policy.hpp:31
void applyCutcellOpBox(CCField y, CCConst x, OpV AC, OpV AW, OpV AE, OpV AS, OpV AN, OpV AB, OpV AT, C3 e, C3 rlo, C3 rhi, C3 slo, C3 shi)
flow — the multidimensional iteration policies: iteration order follows storage.
static constexpr double AC
Kokkos::View< double *, peclet::flow::CCMem > OpV
static int run(int bc)