flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
scalar_mg.hpp
Go to the documentation of this file.
1// ScalarMG: the multigrid preconditioner of the cut-cell scalar path (doc/scalar_ibm_design.md
2// §5.2, D10; WO-4 single phase). One instance per cut-cell scalar; it preconditions the SPD
3// lumped-probe SURROGATE S of §4.3 — the BiCGStab of `scalar_krylov.hpp` runs on the true probe
4// operator, and one V-cycle of this class is its z = M^-1 r.
5//
6// Level table (§5.2): VelocityMG::init / initMpi's rule verbatim — in-place coarsening per axis
7// while the global dimension is even and >= 4 and (distributed) every rank's block origin and size
8// are even on that axis, then the shared aspect rule `CutcellMG::mgChooseRatio` (theta = 2, read
9// from setMetric's h'). No depth cap (the "full table"), no telescoping (§13 Q3). Level 0 is the
10// scalar's own G = 2 block (its ghost exchange is the solver's, injected as `Fill`); coarse levels
11// carry g = 1 and their own `GridHalo<double>`.
12//
13// Per level the surrogate is stored in FACE form, double: AC (diagonal) and AFX/AFY/AFZ, AFX(i) the
14// off-diagonal coefficient of cell i's LOW x face (mac_pressure.hpp's face form). Level 0 (§4.3):
15// AC = the WO-3 surrogate diagonal SAC (bands' diagonal + the lumped wall term), AF = -Lam w_a a,
16// guarded toward a cell that is not an unknown exactly as `sco::buildBands` (so the level-0 sweeps
17// are bitwise the WO-3 band sweeps). Coarse level L, coarse cell C (REDISCRETIZED, §5.2):
18// * face: t_a = w_a(L) <Lam a>, w_a(L) = w_a / cfac_a^2, <Lam a> the plain average of the fine
19// sub-faces' products, coarsened recursively by `coarsenOpenAvg`'s cell body;
20// * mass: m_C = restrictAvg of the children's m (kappa idt, + the lumped implicit outflow
21// under advection, WO-5);
22// * wall: W_C = (1/N_L) sum over the level-0 facets under C of alpha G(s_phi) — the plain
23// average
24// of the level-0 wall terms cw, at the FINE probe distance (design Amendment A1; the
25// level's own s_L overshoots the coarse correction by ~2^L and diverged, log WO-4).
26// Robin and Dirichlet alike (Neumann: 0); in WO-7 the conjugate coupling the same way.
27// A direct gather, one thread per coarse cell over its level-0 descendants in fixed
28// order;
29// * pins: C is pinned iff every child is (the unknown flags restricted with max); pinned rows
30// are
31// identity rows with zero correction;
32// * Dirichlet domain faces: 2 w_a(L) <Lam a_bf>, the boundary plane averaged level by level.
33// Ghosts beyond a non-periodic global face are pinned on every level (the record clears the level-0
34// ghost unknown flags; a coarse ghost's children are those ghosts), so a coarse correction is 0
35// there before the trilinear prolongation reads it, and every face coefficient toward them is 0.
36//
37// Smoother: red-black Gauss-Seidel, colour = (gx + gy + gz) mod 2 from GLOBAL indices, an exchange
38// before each colour — on a single rank on a device backend (WO-9b; flow's A3) the colour passes
39// and the residual of a level with even inner dimensions read the periodic neighbour through the
40// wrapped index instead (`wrapReads`); a coarse level's fill is ONE launch (`smg::fillShell`), no
41// fence. V-cycle: pre 2 sweeps R -> B, residual (after a fresh exchange), restrictAvg, (singular:
42// the coarse rhs mean removed), recurse, coarse ghosts filled, prolongAdd (trilinear), pinned cells
43// re-zeroed, post 2 sweeps B -> R. Bottom: 16 sweeps (8 R -> B then 8 B -> R, so the cycle stays
44// symmetric) plus the mean removal when singular. The level rule (§5.2): transient with kappa_A = 1
45// + 4 dt' D' sum_a w_a < kFullTableKappa = 25 uses level 0 alone (2 + 2 sweeps, the WO-3
46// preconditioner); otherwise, and always when steady, the full table. The cycle is a fixed linear
47// operator.
48//
49// Steady mode WITH advection (design Amendment A2, §6.7; `Inputs::advective`, the same on every
50// rank): the cycle runs on the ADVECTIVE surrogate S_adv, in band form (AC, AW..AT). Level 0 is
51// the operator's 7 bands with SAC as the diagonal (no new storage). A coarse level keeps everything
52// above (faces, wall, Dirichlet planes, pins; its mass carries the open-face outflow omega_open,
53// not the interior outflow) and adds the summed positive parts of the sub-face fluxes, Qp/Qm per
54// low face per unit level volume (the piecewise-constant Galerkin FOU): AC += the cell's outgoing
55// Q, AW = AFX - Qp_x, AE = AFX(i+e_x) - Qm_x(i+e_x), ... The sweep (unknown cells of the colour),
56// the residual, applySurrogate and the contraction instrument use the bands; the cycle structure,
57// transfers, colours and exchanges are unchanged. Transient mode and steady mode at rest take the
58// face-form path above, bitwise unchanged.
59#ifndef PECLET_FLOW_SCALAR_MG_HPP
60#define PECLET_FLOW_SCALAR_MG_HPP
61
62#include <algorithm>
63#include <cmath>
64#include <cstdint>
65#include <functional>
66#include <Kokkos_Core.hpp>
67#include <limits>
68#include <memory>
69#include <string>
70#include <type_traits>
71#include <utility>
72#include <vector>
73
74#include "mac_cutcell_mg.hpp" // coarsenOpenAvgCell, restrictAvg, prolongAdd, CutcellMG::mgChooseRatio
75#include "mac_pressure.hpp" // cutcellSmoothFaceCell, cutcellResidualFaceCell, cutcellApplyFaceCell
76#include "policy.hpp"
77#include "scalar_cutcell_geometry.hpp" // ScalarFacetOverlay
78
79namespace peclet::flow {
80
81namespace smg {
82
88 double lam = 0.0;
89 long st = 1;
90 KOKKOS_INLINE_FUNCTION double operator()(long i) const {
91 return (unk(i) > 0.5 && unk(i - st) > 0.5) ? lam * ap(i) : 0.0;
92 }
93};
94
99struct MassField {
101 double idt = 0.0;
102 bool hasOut = false;
103 KOKKOS_INLINE_FUNCTION double operator()(long i) const {
104 if (!(unk(i) > 0.5))
105 return 0.0;
106 return hasOut ? kappa(i) * idt + out(i) : kappa(i) * idt;
107 }
108};
109
111template <class FV>
112inline void restrictMax(CCField coarse, const FV& fine, C3 cext, C3 fext, int gc, int gf, C3 cinner,
113 C3 ratio) {
114 ccFor3(
115 "peclet::flow::smg_restrict_max", C3{0, 0, 0}, cinner,
116 KOKKOS_LAMBDA(int icx, int icy, int icz) {
117 const long fsy = fext.x, fsz = (long)fext.x * fext.y;
118 double m = 0.0;
119 for (int dz = 0; dz < ratio.z; ++dz)
120 for (int dy = 0; dy < ratio.y; ++dy)
121 for (int dx = 0; dx < ratio.x; ++dx) {
122 const long fi = (long)(ratio.x * icx + dx + gf) +
123 (long)(ratio.y * icy + dy + gf) * fsy +
124 (long)(ratio.z * icz + dz + gf) * fsz;
125 m = fine(fi) > m ? fine(fi) : m;
126 }
127 coarse((long)(icx + gc) + (long)(icy + gc) * cext.x +
128 (long)(icz + gc) * (long)cext.x * cext.y) = m > 0.5 ? 1.0 : 0.0;
129 });
130}
131
133inline void zeroPinned(CCField x, CCConst unk, C3 e, int g) {
134 ccFor3(
135 "peclet::flow::smg_zero_pinned", C3{g, g, g}, C3{e.x - g, e.y - g, e.z - g},
136 KOKKOS_LAMBDA(int lx, int ly, int lz) {
137 const long i = (long)lx + (long)ly * e.x + (long)lz * (long)e.x * e.y;
138 if (!(unk(i) > 0.5))
139 x(i) = 0.0;
140 });
141}
142
145inline void zeroPlanes(CCField f, C3 e, int a, int lo, int hi) {
146 if (hi <= lo)
147 return;
148 const int ext[3] = {e.x, e.y, e.z};
149 const int b = (a + 1) % 3, c = (a + 2) % 3;
150 const long st[3] = {1, (long)e.x, (long)e.x * e.y};
151 const long sa = st[a], sb = st[b], sc = st[c];
152 const int nb = ext[b], nc = ext[c];
153 CCExec space;
154 auto row = KOKKOS_LAMBDA(long t) { // one transverse row (pb, pc) through the planes
155 const int pb = (int)(t % nb), pc = (int)(t / nb);
156 for (int k = lo; k < hi; ++k)
157 f((long)k * sa + (long)pb * sb + (long)pc * sc) = 0.0;
158 };
159 if constexpr (std::is_same_v<typename CCExec::memory_space, Kokkos::HostSpace>) {
160 if (hostRunSerial((long)nb * nc * (hi - lo))) {
161 const HostSerialRegion serialRegion("peclet::flow::smg_zero_planes@serial"); // WO-T
162 for (long t = 0; t < (long)nb * nc; ++t)
163 row(t);
164 return;
165 }
166 }
167 Kokkos::parallel_for("peclet::flow::smg_zero_planes",
168 Kokkos::RangePolicy<CCExec>(space, 0, (long)nb * nc), row);
169}
170
180inline void fillShell(CCField f, C3 e, C3 inner, int g, const bool per[3]) {
181 CCExec space;
182 const int nx = inner.x, ny = inner.y, nz = inner.z;
183 const long exy = (long)e.x * e.y;
184 const bool px = per[0], py = per[1], pz = per[2];
185 CCField ff = f;
186 if constexpr (std::is_same_v<typename CCExec::memory_space, Kokkos::HostSpace>) {
187 // host launch rule: rows (one index computation per row, a contiguous x loop inside)
188 const long rZ = 2L * g * e.y, rY = (long)nz * 2 * g, rX = (long)nz * ny;
189 auto row = [=](long t) {
190 long dst, src;
191 bool zero;
192 if (t < rZ) { // z-ghost plane row (y, zg), all x
193 const int l = (int)(t / e.y), y = (int)(t - (long)l * e.y);
194 const int z = l < g ? l : nz + l;
195 const bool yg = y < g || y >= g + ny;
196 const int sy = y < g ? y + ny : (y >= g + ny ? y - ny : y);
197 const int sz = z < g ? z + nz : z - nz;
198 zero = !pz || (yg && !py);
199 dst = (long)y * e.x + (long)z * exy;
200 src = (long)sy * e.x + (long)sz * exy;
201 } else if (t < rZ + rY) { // y-ghost row (yg, z) of the inner z range, all x
202 const long t2 = t - rZ;
203 const int zi = (int)(t2 / (2 * g)), l = (int)(t2 - (long)zi * 2 * g);
204 const int y = l < g ? l : ny + l, z = g + zi;
205 const int sy = y < g ? y + ny : y - ny;
206 zero = !py;
207 dst = (long)y * e.x + (long)z * exy;
208 src = (long)sy * e.x + (long)z * exy;
209 } else { // the x-ghost cells of the inner row (y, z)
210 const long t3 = t - rZ - rY;
211 const int zi = (int)(t3 / ny), yi = (int)(t3 - (long)zi * ny);
212 const long base = (long)(g + yi) * e.x + (long)(g + zi) * exy;
213 for (int x = 0; x < g; ++x)
214 ff(base + x) = px ? ff(base + x + nx) : 0.0;
215 for (int x = g + nx; x < e.x; ++x)
216 ff(base + x) = px ? ff(base + x - nx) : 0.0;
217 return;
218 }
219 if (zero) {
220 for (int x = 0; x < e.x; ++x)
221 ff(dst + x) = 0.0;
222 return;
223 }
224 for (int x = 0; x < g; ++x)
225 ff(dst + x) = px ? ff(src + x + nx) : 0.0;
226 for (int x = g; x < g + nx; ++x)
227 ff(dst + x) = ff(src + x);
228 for (int x = g + nx; x < e.x; ++x)
229 ff(dst + x) = px ? ff(src + x - nx) : 0.0;
230 };
231 const long cells = 2L * g * exy + (long)nz * 2 * g * e.x + (long)nz * ny * 2 * g;
232 if (hostRunSerial(cells)) {
233 const HostSerialRegion serialRegion("peclet::flow::smg_fill_shell@serial"); // WO-T
234 for (long t = 0; t < rZ + rY + rX; ++t)
235 row(t);
236 return;
237 }
238 Kokkos::parallel_for("peclet::flow::smg_fill_shell",
239 Kokkos::RangePolicy<CCExec>(space, 0, rZ + rY + rX), row);
240 return;
241 }
242 const long nZ = 2L * g * exy, nY = (long)nz * 2 * g * e.x, nX = (long)nz * ny * 2 * g;
243 Kokkos::parallel_for(
244 "peclet::flow::smg_fill_shell", Kokkos::RangePolicy<CCExec>(space, 0, nZ + nY + nX),
245 KOKKOS_LAMBDA(long t) {
246 int x, y, z;
247 if (t < nZ) {
248 const int l = (int)(t / exy);
249 const long r = t - (long)l * exy;
250 x = (int)(r % e.x);
251 y = (int)(r / e.x);
252 z = l < g ? l : nz + l;
253 } else if (t < nZ + nY) {
254 const long t2 = t - nZ, rw = 2L * g * e.x;
255 const long zi = t2 / rw, r = t2 - zi * rw;
256 const int l = (int)(r / e.x);
257 x = (int)(r % e.x);
258 y = l < g ? l : ny + l;
259 z = g + (int)zi;
260 } else {
261 const long t3 = t - nZ - nY, rw = (long)ny * 2 * g;
262 const long zi = t3 / rw, r = t3 - zi * rw;
263 const int l = (int)(r % (2 * g));
264 x = l < g ? l : nx + l;
265 y = g + (int)(r / (2 * g));
266 z = g + (int)zi;
267 }
268 const bool gx = x < g || x >= g + nx, gy = y < g || y >= g + ny, gz = z < g || z >= g + nz;
269 const long d = (long)x + (long)y * e.x + (long)z * exy;
270 if ((gx && !px) || (gy && !py) || (gz && !pz)) {
271 ff(d) = 0.0;
272 return;
273 }
274 const int sx = x < g ? x + nx : (x >= g + nx ? x - nx : x);
275 const int sy = y < g ? y + ny : (y >= g + ny ? y - ny : y);
276 const int sz = z < g ? z + nz : (z >= g + nz ? z - nz : z);
277 ff(d) = ff((long)sx + (long)sy * e.x + (long)sz * exy);
278 });
279}
280
285struct Nb {
286 long xp, xm, yp, ym, zp, zm;
287};
288KOKKOS_INLINE_FUNCTION Nb nbrs(int lx, int ly, int lz, C3 n, int g, long i, long sy, long sz,
289 int wrap) {
290 Nb w;
291 w.xm = ((wrap & 1) && lx == g) ? i + (long)(n.x - 1) : i - 1;
292 w.xp = ((wrap & 1) && lx == g + n.x - 1) ? i - (long)(n.x - 1) : i + 1;
293 w.ym = ((wrap & 2) && ly == g) ? i + (long)(n.y - 1) * sy : i - sy;
294 w.yp = ((wrap & 2) && ly == g + n.y - 1) ? i - (long)(n.y - 1) * sy : i + sy;
295 w.zm = ((wrap & 4) && lz == g) ? i + (long)(n.z - 1) * sz : i - sz;
296 w.zp = ((wrap & 4) && lz == g + n.z - 1) ? i - (long)(n.z - 1) * sz : i + sz;
297 return w;
298}
299
303KOKKOS_INLINE_FUNCTION Nb nbrsOn(int lx, int ly, int lz, C3 n, int g, long i, long sy, long sz,
304 int wrap) {
305 if constexpr (std::is_same_v<typename CCExec::memory_space, Kokkos::HostSpace>) {
306 (void)lx, (void)ly, (void)lz, (void)n, (void)g, (void)wrap;
307 return Nb{i + 1, i - 1, i + sy, i - sy, i + sz, i - sz};
308 } else {
309 return nbrs(lx, ly, lz, n, g, i, sy, sz, wrap);
310 }
311}
312
315template <class OpV>
316inline void sweepColorFace(CCField x, CCConst rhs, OpV AC, OpV AFX, OpV AFY, OpV AFZ, C3 e, C3 og,
317 int g, int color, C3 n, int wrap) {
318 CCExec space;
319 if constexpr (std::is_same_v<typename CCExec::memory_space, Kokkos::HostSpace>) {
320 const int nyi = e.y - 2 * g, nzi = e.z - 2 * g;
321 const long cells = (long)nyi * nzi * (e.x - 2 * g);
322 auto pencil = KOKKOS_LAMBDA(long t) {
323 const int ly = g + (int)(t % nyi), lz = g + (int)(t / nyi);
324 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
325 const int P = (color + og.x + og.y + ly + og.z + lz) & 1;
326 PECLET_FLOW_OMP_SIMD // same-colour cells are independent (ccFor3's contract)
327 for (int lx = g + ((P ^ (g & 1)) & 1); lx < e.x - g; lx += 2) {
328 const long i = (long)lx + (long)ly * sy + (long)lz * sz;
329 const Nb w = nbrsOn(lx, ly, lz, n, g, i, sy, sz, wrap);
330 cutcellSmoothFaceCell(x, rhs, AC, AFX, AFY, AFZ, i, sx, sy, sz, w.xp, w.xm, w.yp, w.ym,
331 w.zp, w.zm);
332 }
333 };
334 if (hostRunSerial(cells)) { // coarse MG level: the fork/join costs more than the sweep
335 const HostSerialRegion serialRegion("peclet::flow::smg_smooth@serial"); // WO-T
336 for (long t = 0; t < (long)nyi * nzi; ++t)
337 pencil(t);
338 return;
339 }
340 Kokkos::parallel_for("peclet::flow::smg_smooth",
341 Kokkos::RangePolicy<CCExec>(space, 0, (long)nyi * nzi), pencil);
342 return;
343 }
344 using MD = MDRange3<CCExec>;
345 Kokkos::parallel_for(
346 "peclet::flow::smg_smooth", MD(space, {g, g, g}, {e.x - g, e.y - g, e.z - g}),
347 KOKKOS_LAMBDA(int lx, int ly, int lz) {
348 if (((og.x + lx + og.y + ly + og.z + lz) & 1) != color)
349 return;
350 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
351 const long i = (long)lx + (long)ly * sy + (long)lz * sz;
352 const Nb w = nbrsOn(lx, ly, lz, n, g, i, sy, sz, wrap);
353 cutcellSmoothFaceCell(x, rhs, AC, AFX, AFY, AFZ, i, sx, sy, sz, w.xp, w.xm, w.yp, w.ym,
354 w.zp, w.zm);
355 });
356}
357
361 CCField AFZ, C3 e, int g, C3 n, int wrap) {
362 ccFor3(
363 "peclet::flow::smg_residual", C3{g, g, g}, C3{e.x - g, e.y - g, e.z - g},
364 KOKKOS_LAMBDA(int lx, int ly, int lz) {
365 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
366 const long i = (long)lx + (long)ly * sy + (long)lz * sz;
367 const Nb w = nbrsOn(lx, ly, lz, n, g, i, sy, sz, wrap);
368 cutcellResidualFaceCell(r, x, b, AC, AFX, AFY, AFZ, i, sx, sy, sz, w.xp, w.xm, w.yp, w.ym,
369 w.zp, w.zm);
370 });
371}
372
373// ---- the advective path (design Amendment A2, §6.7): band form ----------------------------------
374
381 long st = 1;
382 double sgn = 1.0;
383 KOKKOS_INLINE_FUNCTION double operator()(long i) const {
384 return (unk(i) > 0.5 && unk(i - st) > 0.5) ? Kokkos::fmax(sgn * phi(i), 0.0) : 0.0;
385 }
386};
387
390template <class XV, class BV>
391KOKKOS_INLINE_FUNCTION double bandOff(const XV& x, const BV& AW, const BV& AE, const BV& AS,
392 const BV& AN, const BV& AB, const BV& AT, long i,
393 const Nb& w) {
394 return ((AW(i) * x(w.xm) + AE(i) * x(w.xp)) + (AS(i) * x(w.ym) + AN(i) * x(w.yp))) +
395 (AB(i) * x(w.zm) + AT(i) * x(w.zp));
396}
397
401template <class BV, class UV>
402inline void sweepColorBand(CCField x, CCConst rhs, BV AC, BV AW, BV AE, BV AS, BV AN, BV AB, BV AT,
403 UV unk, C3 e, C3 og, int g, int color, C3 n, int wrap) {
404 CCExec space;
405 if constexpr (std::is_same_v<typename CCExec::memory_space, Kokkos::HostSpace>) {
406 const int nyi = e.y - 2 * g, nzi = e.z - 2 * g;
407 const long cells = (long)nyi * nzi * (e.x - 2 * g);
408 auto pencil = KOKKOS_LAMBDA(long t) {
409 const int ly = g + (int)(t % nyi), lz = g + (int)(t / nyi);
410 const long sy = e.x, sz = (long)e.x * e.y;
411 const int P = (color + og.x + og.y + ly + og.z + lz) & 1;
412 for (int lx = g + ((P ^ (g & 1)) & 1); lx < e.x - g; lx += 2) {
413 const long i = (long)lx + (long)ly * sy + (long)lz * sz;
414 if (!(unk(i) > 0.5))
415 continue;
416 const Nb w = nbrsOn(lx, ly, lz, n, g, i, sy, sz, wrap);
417 x(i) = (rhs(i) - bandOff(x, AW, AE, AS, AN, AB, AT, i, w)) / AC(i);
418 }
419 };
420 if (hostRunSerial(cells)) { // coarse MG level: the fork/join costs more than the sweep
421 const HostSerialRegion serialRegion("peclet::flow::smg_smooth_band@serial"); // WO-T
422 for (long t = 0; t < (long)nyi * nzi; ++t)
423 pencil(t);
424 return;
425 }
426 Kokkos::parallel_for("peclet::flow::smg_smooth_band",
427 Kokkos::RangePolicy<CCExec>(space, 0, (long)nyi * nzi), pencil);
428 return;
429 }
430 using MD = MDRange3<CCExec>;
431 Kokkos::parallel_for(
432 "peclet::flow::smg_smooth_band", MD(space, {g, g, g}, {e.x - g, e.y - g, e.z - g}),
433 KOKKOS_LAMBDA(int lx, int ly, int lz) {
434 if (((og.x + lx + og.y + ly + og.z + lz) & 1) != color)
435 return;
436 const long sy = e.x, sz = (long)e.x * e.y;
437 const long i = (long)lx + (long)ly * sy + (long)lz * sz;
438 if (!(unk(i) > 0.5))
439 return;
440 const Nb w = nbrsOn(lx, ly, lz, n, g, i, sy, sz, wrap);
441 x(i) = (rhs(i) - bandOff(x, AW, AE, AS, AN, AB, AT, i, w)) / AC(i);
442 });
443}
444
447template <class XV, class OV>
448KOKKOS_INLINE_FUNCTION double faceOff(const XV& x, const OV& AFX, const OV& AFY, const OV& AFZ,
449 long i, long sx, long sy, long sz, const Nb& w) {
450 return AFX(i + sx) * x(w.xp) + AFX(i) * x(w.xm) + AFY(i + sy) * x(w.yp) + AFY(i) * x(w.ym) +
451 AFZ(i + sz) * x(w.zp) + AFZ(i) * x(w.zm);
452}
453
462template <bool Band, class XV, class BV>
463KOKKOS_INLINE_FUNCTION void twoPhaseCell(const XV& x, const XV& rhs, const BV& AC, const BV& AFX,
464 const BV& AFY, const BV& AFZ, const BV& AW, const BV& AE,
465 const BV& AS, const BV& AN, const BV& AB, const BV& AT,
466 const XV& xs, const XV& rhss, const BV& ACs,
467 const BV& AFXs, const BV& AFYs, const BV& AFZs,
468 const BV& W, const CCConst& unkF, long i, long sx, long sy,
469 long sz, const Nb& w) {
470 const double wc = W(i);
471 const double af = AC(i), as = ACs(i);
472 if (wc > 0.0) {
473 const double nf = Band ? bandOff(x, AW, AE, AS, AN, AB, AT, i, w)
474 : faceOff(x, AFX, AFY, AFZ, i, sx, sy, sz, w);
475 const double rf = rhs(i) - nf;
476 const double rs = rhss(i) - faceOff(xs, AFXs, AFYs, AFZs, i, sx, sy, sz, w);
477 const double det = af * as - wc * wc;
478 if (det > 1e-300 * af * as) {
479 x(i) = (as * rf + wc * rs) / det;
480 xs(i) = (wc * rf + af * rs) / det;
481 } else {
482 x(i) = (rf + wc * xs(i)) / af;
483 xs(i) = (rs + wc * x(i)) / as;
484 }
485 return;
486 }
487 if constexpr (Band) {
488 if (unkF(i) > 0.5)
489 x(i) = (rhs(i) - bandOff(x, AW, AE, AS, AN, AB, AT, i, w)) / af;
490 } else {
491 if (!(af < 1e-30))
492 x(i) = (rhs(i) - faceOff(x, AFX, AFY, AFZ, i, sx, sy, sz, w)) / af;
493 }
494 if (!(as < 1e-30))
495 xs(i) = (rhss(i) - faceOff(xs, AFXs, AFYs, AFZs, i, sx, sy, sz, w)) / as;
496}
497
499template <bool Band>
500inline void sweepColorTwo(CCField x, CCField rhs, CCField AC, CCField AFX, CCField AFY, CCField AFZ,
501 CCField AW, CCField AE, CCField AS, CCField AN, CCField AB, CCField AT,
502 CCField xs, CCField rhss, CCField ACs, CCField AFXs, CCField AFYs,
503 CCField AFZs, CCField W, CCConst unkF, C3 e, C3 og, int g, int color,
504 C3 n, int wrap) {
505 CCExec space;
506 if constexpr (std::is_same_v<typename CCExec::memory_space, Kokkos::HostSpace>) {
507 const int nyi = e.y - 2 * g, nzi = e.z - 2 * g;
508 const long cells = (long)nyi * nzi * (e.x - 2 * g);
509 auto pencil = KOKKOS_LAMBDA(long t) {
510 const int ly = g + (int)(t % nyi), lz = g + (int)(t / nyi);
511 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
512 const int P = (color + og.x + og.y + ly + og.z + lz) & 1;
513 for (int lx = g + ((P ^ (g & 1)) & 1); lx < e.x - g; lx += 2) {
514 const long i = (long)lx + (long)ly * sy + (long)lz * sz;
515 const Nb w = nbrsOn(lx, ly, lz, n, g, i, sy, sz, wrap);
516 twoPhaseCell<Band>(x, rhs, AC, AFX, AFY, AFZ, AW, AE, AS, AN, AB, AT, xs, rhss, ACs, AFXs,
517 AFYs, AFZs, W, unkF, i, sx, sy, sz, w);
518 }
519 };
520 if (hostRunSerial(cells)) {
521 const HostSerialRegion serialRegion("peclet::flow::smg_smooth_two@serial"); // WO-T
522 for (long t = 0; t < (long)nyi * nzi; ++t)
523 pencil(t);
524 return;
525 }
526 Kokkos::parallel_for("peclet::flow::smg_smooth_two",
527 Kokkos::RangePolicy<CCExec>(space, 0, (long)nyi * nzi), pencil);
528 return;
529 }
530 using MD = MDRange3<CCExec>;
531 Kokkos::parallel_for(
532 "peclet::flow::smg_smooth_two", MD(space, {g, g, g}, {e.x - g, e.y - g, e.z - g}),
533 KOKKOS_LAMBDA(int lx, int ly, int lz) {
534 if (((og.x + lx + og.y + ly + og.z + lz) & 1) != color)
535 return;
536 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
537 const long i = (long)lx + (long)ly * sy + (long)lz * sz;
538 const Nb w = nbrsOn(lx, ly, lz, n, g, i, sy, sz, wrap);
539 twoPhaseCell<Band>(x, rhs, AC, AFX, AFY, AFZ, AW, AE, AS, AN, AB, AT, xs, rhss, ACs, AFXs,
540 AFYs, AFZs, W, unkF, i, sx, sy, sz, w);
541 });
542}
543
546template <bool Band>
548 CCField AFZ, CCField AW, CCField AE, CCField AS, CCField AN, CCField AB,
549 CCField AT, CCField rs, CCConst xs, CCConst bs, CCField ACs, CCField AFXs,
550 CCField AFYs, CCField AFZs, CCField W, C3 e, int g, C3 n, int wrap) {
551 ccFor3(
552 "peclet::flow::smg_residual_two", C3{g, g, g}, C3{e.x - g, e.y - g, e.z - g},
553 KOKKOS_LAMBDA(int lx, int ly, int lz) {
554 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
555 const long i = (long)lx + (long)ly * sy + (long)lz * sz;
556 const Nb w = nbrsOn(lx, ly, lz, n, g, i, sy, sz, wrap);
557 const double ax = Band ? AC(i) * x(i) + bandOff(x, AW, AE, AS, AN, AB, AT, i, w)
558 : AC(i) * x(i) + faceOff(x, AFX, AFY, AFZ, i, sx, sy, sz, w);
559 const double axs = ACs(i) * xs(i) + faceOff(xs, AFXs, AFYs, AFZs, i, sx, sy, sz, w);
560 const double wc = W(i);
561 r(i) = (b(i) - ax) + wc * xs(i);
562 rs(i) = (bs(i) - axs) + wc * x(i);
563 });
564}
565
568 CCField AS, CCField AN, CCField AB, CCField AT, C3 e, int g, C3 n,
569 int wrap) {
570 ccFor3(
571 "peclet::flow::smg_residual_band", C3{g, g, g}, C3{e.x - g, e.y - g, e.z - g},
572 KOKKOS_LAMBDA(int lx, int ly, int lz) {
573 const long sy = e.x, sz = (long)e.x * e.y;
574 const long i = (long)lx + (long)ly * sy + (long)lz * sz;
575 const Nb w = nbrsOn(lx, ly, lz, n, g, i, sy, sz, wrap);
576 r(i) = b(i) - (AC(i) * x(i) + bandOff(x, AW, AE, AS, AN, AB, AT, i, w));
577 });
578}
579
580} // namespace smg
581
582class ScalarMG {
583 public:
584 static constexpr int G0 = 2;
585 static constexpr int GC = 1;
594 static constexpr double kFullTableKappa = 25.0;
595 using Fill = std::function<void(CCField)>;
596
597 struct Level {
598 C3 ext{0, 0, 0}, inner{0, 0, 0}, ratio{1, 1, 1}, cfac{1, 1, 1};
599 C3 og{0, 0, 0};
600 C3 gdim{0, 0, 0};
601 int g = GC;
602 std::size_t n = 0;
607 Kokkos::View<double*, CCMem> plane[6];
608 double nUnk = 0.0;
614 CCField qp[3], qm[3];
622 double nUnkS = 0.0;
623#ifdef PECLET_FLOW_MPI
624 std::shared_ptr<GridHaloTopology<3>> halo;
625 std::shared_ptr<GridHalo<double>> dev;
626#endif
627 };
628
630 void setMetric(const double w[3], const double hp[3]) {
631 for (int a = 0; a < 3; ++a) {
632 w_[a] = w[a];
633 hp_[a] = hp[a];
634 }
635 aniso_ = !(hp_[0] == 1.0 && hp_[1] == 1.0 && hp_[2] == 1.0);
636 }
637
638 void setPeriodic(const bool per[3]) {
639 for (int a = 0; a < 3; ++a)
640 per_[a] = per[a];
641 }
642
646 void setLevel0Wrap(bool on) { fill0Wraps_ = on; }
647
649 void init(int nx, int ny, int nz, Fill fill0) {
650 lv_.clear();
651 distributed_ = false;
652 fill0_ = std::move(fill0);
653 C3 inner{nx, ny, nz}, cf{1, 1, 1};
654 for (int L = 0; L < kMaxLevels; ++L) {
655 Level v;
656 v.g = L == 0 ? G0 : GC;
657 v.inner = inner;
658 v.gdim = inner;
659 v.ext = C3{inner.x + 2 * v.g, inner.y + 2 * v.g, inner.z + 2 * v.g};
660 v.cfac = cf;
661 v.n = (std::size_t)v.ext.x * v.ext.y * v.ext.z;
662 const bool canA[3] = {can(inner.x), can(inner.y), can(inner.z)};
663 const C3 ratio = chooseRatio(canA, cf);
664 const C3 next{ratio.x == 2 ? inner.x / 2 : inner.x, ratio.y == 2 ? inner.y / 2 : inner.y,
665 ratio.z == 2 ? inner.z / 2 : inner.z};
666 v.ratio = ratio;
667 allocate(v, L);
668 lv_.push_back(v);
669 if (next.x == inner.x && next.y == inner.y && next.z == inner.z)
670 break;
671 inner = next;
672 cf = C3{cf.x * ratio.x, cf.y * ratio.y, cf.z * ratio.z};
673 }
674 }
675#ifdef PECLET_FLOW_MPI
679 void initMpi(const peclet::core::decomp::BlockDecomposer<3>& dec0, MPI_Comm comm, C3 og0,
680 Fill fill0) {
681 lv_.clear();
682 distributed_ = true;
683 comm_ = comm;
684 fill0_ = std::move(fill0);
685 int rank = 0;
686 MPI_Comm_rank(comm, &rank);
687 std::array<bool, 3> per{true, true, true}; // periodic everywhere; non-periodic ghosts zeroed
688 const auto& g0 = dec0.globalSize();
689 C3 gs{(int)g0[0], (int)g0[1], (int)g0[2]}, cf{1, 1, 1};
690 auto evenOn = [](const peclet::core::decomp::BlockDecomposer<3>& d, int ax) {
691 for (std::size_t b = 0; b < d.numBlocks(); ++b) {
692 const auto blk = d.block(b);
693 if (blk.origin[ax] % 2 != 0 || blk.size[ax] % 2 != 0)
694 return false;
695 }
696 return true;
697 };
698 peclet::core::decomp::BlockDecomposer<3> dec = dec0;
699 for (int L = 0; L < kMaxLevels; ++L) {
700 Level v;
701 v.g = L == 0 ? G0 : GC;
702 if (L == 0) {
703 const auto blk = dec.block((std::size_t)rank);
704 v.inner = C3{(int)blk.size[0], (int)blk.size[1], (int)blk.size[2]};
705 v.ext = C3{v.inner.x + 2 * G0, v.inner.y + 2 * G0, v.inner.z + 2 * G0};
706 v.og = og0;
707 } else {
708 v.halo = std::make_shared<GridHaloTopology<3>>();
709 v.halo->buildTopology(dec, rank, v.g, per, comm);
710 v.dev = std::make_shared<GridHalo<double>>();
711 v.dev->init(*v.halo);
712 v.dev->setLabel("smg L" + std::to_string(L));
713 const auto& idx = v.halo->indexer();
714 const auto eg = idx.sizeInclGhost(), ino = idx.sizeInner(), oig = idx.originInclGhost();
715 v.ext = {(int)eg[0], (int)eg[1], (int)eg[2]};
716 v.inner = {(int)ino[0], (int)ino[1], (int)ino[2]};
717 v.og = {(int)oig[0] + v.g, (int)oig[1] + v.g, (int)oig[2] + v.g};
718 }
719 v.gdim = gs;
720 v.cfac = cf;
721 v.n = (std::size_t)v.ext.x * v.ext.y * v.ext.z;
722 const bool canA[3] = {can(gs.x) && evenOn(dec, 0), can(gs.y) && evenOn(dec, 1),
723 can(gs.z) && evenOn(dec, 2)};
724 const C3 ratio = chooseRatio(canA, cf);
725 const C3 next{ratio.x == 2 ? gs.x / 2 : gs.x, ratio.y == 2 ? gs.y / 2 : gs.y,
726 ratio.z == 2 ? gs.z / 2 : gs.z};
727 v.ratio = ratio;
728 allocate(v, L);
729 lv_.push_back(v);
730 if (next.x == gs.x && next.y == gs.y && next.z == gs.z)
731 break;
732 gs = next;
733 cf = C3{cf.x * ratio.x, cf.y * ratio.y, cf.z * ratio.z};
734 dec = dec.coarsened(peclet::core::IVec<3>{ratio.x, ratio.y, ratio.z});
735 }
736 }
737#endif
738
739 int levels() const { return (int)lv_.size(); }
741 int levelsUsed() const { return nUse_; }
742 const Level& level(int L) const { return lv_[L]; }
744 std::vector<C3> levelRatios() const {
745 std::vector<C3> r;
746 for (const auto& v : lv_)
747 r.push_back(v.ratio);
748 return r;
749 }
750
752 struct Inputs {
753 double lam = 0.0;
754 double idt = 0.0;
755 bool fullTable = true;
756 bool singular = false;
759 const scg::ScalarFacetOverlay* fac = nullptr;
760 Kokkos::View<const double*, CCMem> facetW;
761 bool dirFace[6] = {false, false, false, false, false, false};
766 bool hasOutflow = false;
772 bool advective = false;
780 bool twoPhase = false;
783 Kokkos::View<const double*, CCMem> facetC;
784 };
785
787 void build(const Inputs& in) {
788 singular_ = in.singular;
789 adv_ = in.advective;
790 nUse_ = in.fullTable ? (int)lv_.size() : 1;
791 Level& l0 = lv_[0];
792 l0.AC = in.SAC;
793 if (adv_) { // A2: the operator's bands, no new storage
794 l0.AW = in.AW;
795 l0.AE = in.AE;
796 l0.AS = in.AS;
797 l0.AN = in.AN;
798 l0.AB = in.AB;
799 l0.AT = in.AT;
800 }
801 l0.unk = CCField(); // level 0's flag is the geometry's (const) view
802 unk0_ = in.unknown;
804 l0.nUnk = countUnknown(0);
805 two_ = in.twoPhase;
806 if (two_) { // WO-7: the solid phase and the coupling on level 0
807 l0.ACs = in.SACs;
808 l0.W = in.W0;
809 unkS0_ = in.sunk;
811 l0.nUnkS = countUnknownS(0);
812 }
813 for (int f = 0; f < 6; ++f)
814 dirFace_[f] = in.dirFace[f];
815 if (nUse_ > 1) {
816 const bool newLists = buildCutLists(in);
817 wallReuse_ = !newLists && wallKeep(in);
818 for (int f = 0; f < 6; ++f)
819 if (dirFace_[f] && touches(l0, f))
820 buildPlane0(in, f);
821 for (int L = 1; L < nUse_; ++L)
822 buildCoarse(in, L);
823 }
824 }
825
829 void apply(CCField z, CCField r) {
830 Level& l0 = lv_[0];
831 l0.x = z;
832 l0.rhs = r;
833 Kokkos::deep_copy(CCExec(), z, 0.0);
834 if (nUse_ == 1) {
835 for (int k = 0; k < 2; ++k) {
836 sweepColor(0, 0);
837 sweepColor(0, 1);
838 }
839 for (int k = 0; k < 2; ++k) {
840 sweepColor(0, 1);
841 sweepColor(0, 0);
842 }
843 return;
844 }
845 vcycle(0);
846 }
847
851 void applyTwo(CCField zf, CCField zs, CCField rf, CCField rs) {
852 Level& l0 = lv_[0];
853 l0.x = zf;
854 l0.rhs = rf;
855 l0.xs = zs;
856 l0.rhss = rs;
857 Kokkos::deep_copy(CCExec(), zf, 0.0);
858 Kokkos::deep_copy(CCExec(), zs, 0.0);
859 if (nUse_ == 1) {
860 for (int k = 0; k < 2; ++k) {
861 sweepColor(0, 0);
862 sweepColor(0, 1);
863 }
864 for (int k = 0; k < 2; ++k) {
865 sweepColor(0, 1);
866 sweepColor(0, 0);
867 }
868 return;
869 }
870 vcycleTwo(0);
871 }
872
876 Level& l0 = lv_[0];
877 fill(0, xf);
878 fill(0, xs);
879 const C3 e = l0.ext;
880 const int g = l0.g;
881 CCField AC = l0.AC, AFX = l0.AFX, AFY = l0.AFY, AFZ = l0.AFZ, ACs = l0.ACs, AFXs = l0.AFXs,
882 AFYs = l0.AFYs, AFZs = l0.AFZs, W = l0.W, AW = l0.AW, AE = l0.AE, AS = l0.AS,
883 AN = l0.AN, AB = l0.AB, AT = l0.AT;
884 const bool band = adv_;
885 CCConst x = xf, s = xs;
886 ccFor3(
887 "peclet::flow::smg_apply_two", C3{g, g, g}, C3{e.x - g, e.y - g, e.z - g},
888 KOKKOS_LAMBDA(int lx, int ly, int lz) {
889 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
890 const long i = (long)lx + (long)ly * sy + (long)lz * sz;
891 const smg::Nb w{i + sx, i - sx, i + sy, i - sy, i + sz, i - sz};
892 const double ax = band ? AC(i) * x(i) + smg::bandOff(x, AW, AE, AS, AN, AB, AT, i, w)
893 : AC(i) * x(i) + smg::faceOff(x, AFX, AFY, AFZ, i, sx, sy, sz, w);
894 const double axs = ACs(i) * s(i) + smg::faceOff(s, AFXs, AFYs, AFZs, i, sx, sy, sz, w);
895 yf(i) = ax - W(i) * s(i);
896 ys(i) = axs - W(i) * x(i);
897 });
898 }
899
902 Level& l0 = lv_[0];
903 fill(0, x);
904 const C3 e = l0.ext;
905 const int g = l0.g;
906 if (adv_) { // A2: S_adv at level 0 = the operator's band matvec with SAC as the diagonal
907 applyCutcellOp(y, CCConst(x), l0.AC, l0.AW, l0.AE, l0.AS, l0.AN, l0.AB, l0.AT, e, g);
908 return;
909 }
910 CCField AC = l0.AC, AFX = l0.AFX, AFY = l0.AFY, AFZ = l0.AFZ;
911 CCConst xx = x;
912 ccFor3(
913 "peclet::flow::smg_apply", C3{g, g, g}, C3{e.x - g, e.y - g, e.z - g},
914 KOKKOS_LAMBDA(int lx, int ly, int lz) {
915 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
916 const long i = (long)lx + (long)ly * sy + (long)lz * sz;
917 y(i) = cutcellApplyFaceCell(xx, AC, AFX, AFY, AFZ, i, sx, sy, sz, i + sx, i - sx, i + sy,
918 i - sy, i + sz, i - sz);
919 });
920 }
921
926 std::vector<double> contraction(int iters, CCField e, CCField t, CCField z) {
927 Level& l0 = lv_[0];
928 const C3 ext = l0.ext, og = l0.og;
929 const int g = l0.g;
930 CCConst unk = unk0_;
931 ccFor3(
932 "peclet::flow::smg_seed", C3{g, g, g}, C3{ext.x - g, ext.y - g, ext.z - g},
933 KOKKOS_LAMBDA(int lx, int ly, int lz) {
934 const long i = (long)lx + (long)ly * ext.x + (long)lz * (long)ext.x * ext.y;
935 std::uint64_t h = (std::uint64_t)(og.x + lx - g) * 0x9E3779B97F4A7C15ull ^
936 (std::uint64_t)(og.y + ly - g) * 0xC2B2AE3D27D4EB4Full ^
937 (std::uint64_t)(og.z + lz - g) * 0x165667B19E3779F9ull;
938 h ^= h >> 29;
939 h *= 0xBF58476D1CE4E5B9ull;
940 h ^= h >> 32;
941 e(i) = unk(i) > 0.5 ? (double)(h >> 11) * (1.0 / 9007199254740992.0) - 0.5 : 0.0;
942 });
943 if (singular_)
944 removeMean(0, e);
945 std::vector<double> ratios;
946 double prev = norm2(e);
947 for (int k = 0; k < iters; ++k) {
948 applySurrogate(t, e);
949 apply(z, t);
950 if (singular_)
951 removeMean(0, z);
952 ccFor3(
953 "peclet::flow::smg_err_update", C3{g, g, g}, C3{ext.x - g, ext.y - g, ext.z - g},
954 KOKKOS_LAMBDA(int lx, int ly, int lz) {
955 const long i = (long)lx + (long)ly * ext.x + (long)lz * (long)ext.x * ext.y;
956 e(i) -= z(i);
957 });
958 if (singular_) // keep the iterate off the null space (round-off would otherwise grow there)
959 removeMean(0, e);
960 const double cur = norm2(e);
961 ratios.push_back(prev > 0.0 ? cur / prev : 0.0);
962 if (!(cur > 0.0))
963 break;
964 // renormalize (keeps the iterate O(1) over many iterations)
965 const double s = 1.0 / cur;
966 ccFor3(
967 "peclet::flow::smg_err_scale", C3{g, g, g}, C3{ext.x - g, ext.y - g, ext.z - g},
968 KOKKOS_LAMBDA(int lx, int ly, int lz) {
969 const long i = (long)lx + (long)ly * ext.x + (long)lz * (long)ext.x * ext.y;
970 e(i) *= s;
971 });
972 prev = 1.0;
973 }
974 return ratios;
975 }
976
977 // ---- (public for nvcc extended lambdas) ----
978 void vcycle(int L) {
979 Level& lv = lv_[L];
980 if (L + 1 == nUse_) { // bottom: 16 sweeps, symmetric order
981 for (int k = 0; k < kBottom / 2; ++k)
982 sweep(L, true);
983 for (int k = 0; k < kBottom / 2; ++k)
984 sweep(L, false);
985 if (singular_)
986 removeMean(L, lv.x);
987 return;
988 }
989 for (int k = 0; k < kPre; ++k)
990 sweep(L, true);
991 // the smoother leaves the ghosts one colour stale: a fresh fill, or the fused reads
992 const int wr = wrapReads(L);
993 if (wr < 0)
994 fill(L, lv.x);
995 if (adv_)
996 smg::residualBand(lv.res, CCConst(lv.x), CCConst(lv.rhs), lv.AC, lv.AW, lv.AE, lv.AS, lv.AN,
997 lv.AB, lv.AT, lv.ext, lv.g, lv.inner, wr < 0 ? 0 : wr);
998 else
999 smg::residualFace(lv.res, CCConst(lv.x), CCConst(lv.rhs), lv.AC, lv.AFX, lv.AFY, lv.AFZ,
1000 lv.ext, lv.g, lv.inner, wr < 0 ? 0 : wr);
1001 Level& cs = lv_[L + 1];
1002 restrictZeroX(cs, lv, cs.rhs, lv.res, cs.x);
1003 if (singular_)
1004 removeMean(L + 1, cs.rhs);
1005 vcycle(L + 1);
1006 fill(L + 1, cs.x); // coarse ghosts: exchanged, 0 beyond a non-periodic global face
1007 prolongAdd(lv.x, CCConst(cs.x), lv.ext, cs.ext, lv.g, cs.g, lv.inner, lv.ratio);
1008 smg::zeroPinned(lv.x, unkOf(L), lv.ext, lv.g);
1009 for (int k = 0; k < kPost; ++k)
1010 sweep(L, false);
1011 }
1012
1016 void vcycleTwo(int L) {
1017 Level& lv = lv_[L];
1018 if (L + 1 == nUse_) {
1019 for (int k = 0; k < kBottom / 2; ++k)
1020 sweep(L, true);
1021 for (int k = 0; k < kBottom / 2; ++k)
1022 sweep(L, false);
1023 if (singular_)
1024 removeMeanTwo(L, lv.x, lv.xs);
1025 return;
1026 }
1027 for (int k = 0; k < kPre; ++k)
1028 sweep(L, true);
1029 const int wr = wrapReads(L);
1030 if (wr < 0) {
1031 fill(L, lv.x);
1032 fill(L, lv.xs);
1033 }
1034 const int w = wr < 0 ? 0 : wr;
1035 if (adv_)
1036 smg::residualTwo<true>(lv.res, CCConst(lv.x), CCConst(lv.rhs), lv.AC, lv.AFX, lv.AFY, lv.AFZ,
1037 lv.AW, lv.AE, lv.AS, lv.AN, lv.AB, lv.AT, lv.ress, CCConst(lv.xs),
1038 CCConst(lv.rhss), lv.ACs, lv.AFXs, lv.AFYs, lv.AFZs, lv.W, lv.ext,
1039 lv.g, lv.inner, w);
1040 else
1041 smg::residualTwo<false>(lv.res, CCConst(lv.x), CCConst(lv.rhs), lv.AC, lv.AFX, lv.AFY, lv.AFZ,
1042 lv.AW, lv.AE, lv.AS, lv.AN, lv.AB, lv.AT, lv.ress, CCConst(lv.xs),
1043 CCConst(lv.rhss), lv.ACs, lv.AFXs, lv.AFYs, lv.AFZs, lv.W, lv.ext,
1044 lv.g, lv.inner, w);
1045 Level& cs = lv_[L + 1];
1046 restrictZeroX(cs, lv, cs.rhs, lv.res, cs.x);
1047 restrictZeroX(cs, lv, cs.rhss, lv.ress, cs.xs);
1048 if (singular_)
1049 removeMeanTwo(L + 1, cs.rhs, cs.rhss);
1050 vcycleTwo(L + 1);
1051 fill(L + 1, cs.x);
1052 fill(L + 1, cs.xs);
1053 prolongAdd(lv.x, CCConst(cs.x), lv.ext, cs.ext, lv.g, cs.g, lv.inner, lv.ratio);
1054 prolongAdd(lv.xs, CCConst(cs.xs), lv.ext, cs.ext, lv.g, cs.g, lv.inner, lv.ratio);
1055 smg::zeroPinned(lv.x, unkOf(L), lv.ext, lv.g);
1056 smg::zeroPinned(lv.xs, unkSOf(L), lv.ext, lv.g);
1057 for (int k = 0; k < kPost; ++k)
1058 sweep(L, false);
1059 }
1060
1066 void restrictZeroX(Level& cs, Level& lv, CCField crhs, CCField fres, CCField cx) {
1067 if (!distributed_) {
1068 restrictAvgZeroX(crhs, cx, CCConst(fres), cs.ext, lv.ext, cs.g, lv.g, cs.inner, lv.ratio);
1069 return;
1070 }
1071 restrictAvg(crhs, CCConst(fres), cs.ext, lv.ext, cs.g, lv.g, cs.inner, lv.ratio);
1072 Kokkos::deep_copy(CCExec(), cx, 0.0);
1073 }
1074
1077 void sweepColor(int L, int rb) {
1078 Level& lv = lv_[L];
1079 // WO-9b: on a single-rank level with every inner dimension even the pass reads the periodic
1080 // neighbours directly (wrapSmooth), so no fill; otherwise the fill before each colour
1081 const int ws = wrapSmooth(L);
1082 const int w = ws < 0 ? 0 : ws;
1083 if (ws < 0)
1084 fill(L, lv.x);
1085 const int kc = (rb + 3 * lv.g) & 1;
1086 if (two_) { // WO-7: the coupled two-field update (one more exchange: the solid's)
1087 if (ws < 0)
1088 fill(L, lv.xs);
1089 if (adv_)
1090 smg::sweepColorTwo<true>(lv.x, lv.rhs, lv.AC, lv.AFX, lv.AFY, lv.AFZ, lv.AW, lv.AE, lv.AS,
1091 lv.AN, lv.AB, lv.AT, lv.xs, lv.rhss, lv.ACs, lv.AFXs, lv.AFYs,
1092 lv.AFZs, lv.W, unkOf(L), lv.ext, lv.og, lv.g, kc, lv.inner, w);
1093 else
1094 smg::sweepColorTwo<false>(lv.x, lv.rhs, lv.AC, lv.AFX, lv.AFY, lv.AFZ, lv.AW, lv.AE, lv.AS,
1095 lv.AN, lv.AB, lv.AT, lv.xs, lv.rhss, lv.ACs, lv.AFXs, lv.AFYs,
1096 lv.AFZs, lv.W, unkOf(L), lv.ext, lv.og, lv.g, kc, lv.inner, w);
1097 return;
1098 }
1099 if (adv_) {
1100 smg::sweepColorBand(lv.x, CCConst(lv.rhs), lv.AC, lv.AW, lv.AE, lv.AS, lv.AN, lv.AB, lv.AT,
1101 unkOf(L), lv.ext, lv.og, lv.g, kc, lv.inner, w);
1102 return;
1103 }
1104 smg::sweepColorFace(lv.x, CCConst(lv.rhs), lv.AC, lv.AFX, lv.AFY, lv.AFZ, lv.ext, lv.og, lv.g,
1105 kc, lv.inner, w);
1106 }
1107
1109 void sweep(int L, bool fwd) {
1110 sweepColor(L, fwd ? 0 : 1);
1111 sweepColor(L, fwd ? 1 : 0);
1112 }
1113
1116 void fill(int L, CCField f) {
1117 if (L == 0) {
1118 fill0_(f);
1119 return;
1120 }
1121 Level& lv = lv_[L];
1122#ifdef PECLET_FLOW_MPI
1123 if (distributed_) {
1124 lv.dev->exchange(f);
1125 zeroNonPeriodicGhosts(lv, f);
1126 return;
1127 }
1128#endif
1129 smg::fillShell(f, lv.ext, lv.inner, lv.g, per_); // WO-9b: one launch, no fence
1130 }
1131
1139 int wrapReads(int L) const {
1140 // Device backends only: on a host backend the per-cell wrapped-index arithmetic costs more than
1141 // the fill it saves — it keeps the x-row loop from vectorizing (128^3 bed advance, 1 thread:
1142 // the smoother 700 -> 1131 ms and the residual 80 -> 124 ms per advance, against 44 ms of
1143 // level-0 fills), so host passes fill first (`fill` is one launch per coarse level anyway).
1144 if constexpr (std::is_same_v<typename CCExec::memory_space, Kokkos::HostSpace>)
1145 return -1;
1146 if (distributed_)
1147 return -1;
1148 if (L == 0)
1149 return fill0Wraps_ ? 7 : -1;
1150 return (per_[0] ? 1 : 0) | (per_[1] ? 2 : 0) | (per_[2] ? 4 : 0);
1151 }
1152
1154 int wrapSmooth(int L) const {
1155 const Level& lv = lv_[L];
1156 if (lv.inner.x % 2 != 0 || lv.inner.y % 2 != 0 || lv.inner.z % 2 != 0)
1157 return -1;
1158 return wrapReads(L);
1159 }
1160
1162 bool touches(const Level& lv, int f) const {
1163 const int a = f / 2;
1164 const int o = (a == 0) ? lv.og.x : (a == 1) ? lv.og.y : lv.og.z;
1165 const int n = (a == 0) ? lv.inner.x : (a == 1) ? lv.inner.y : lv.inner.z;
1166 const int gn = (a == 0) ? lv.gdim.x : (a == 1) ? lv.gdim.y : lv.gdim.z;
1167 return (f % 2 == 0) ? (o == 0) : (o + n == gn);
1168 }
1169
1170 public: // nvcc: a member enclosing an extended device lambda must be public (data stays private)
1171 static constexpr int kMaxLevels = 64;
1172 static constexpr int kPre = 2, kPost = 2, kBottom = 16;
1173
1174 static bool can(int d) { return (d % 2 == 0) && (d / 2 >= 2); }
1175 C3 chooseRatio(const bool canA[3], C3 cf) const {
1176 const double H[3] = {hp_[0] * (double)cf.x, hp_[1] * (double)cf.y, hp_[2] * (double)cf.z};
1177 return CutcellMG::mgChooseRatio(H, canA, aniso_, kAspectTheta);
1178 }
1179 static constexpr double kAspectTheta = 2.0;
1180
1181 void allocate(Level& v, int L) {
1182 if (L == 0) {
1183 v.res = CCField("smg_res0", v.n);
1184 v.AFX = CCField("smg_afx0", v.n);
1185 v.AFY = CCField("smg_afy0", v.n);
1186 v.AFZ = CCField("smg_afz0", v.n);
1187 return;
1188 }
1189 v.x = CCField("smg_x", v.n);
1190 v.rhs = CCField("smg_rhs", v.n);
1191 v.res = CCField("smg_res", v.n);
1192 v.AC = CCField("smg_ac", v.n);
1193 v.AFX = CCField("smg_afx", v.n);
1194 v.AFY = CCField("smg_afy", v.n);
1195 v.AFZ = CCField("smg_afz", v.n);
1196 v.unk = CCField("smg_unk", v.n);
1197 v.mass = CCField("smg_mass", v.n);
1198 v.px = CCField("smg_px", v.n);
1199 v.py = CCField("smg_py", v.n);
1200 v.pz = CCField("smg_pz", v.n);
1201 }
1202
1203 CCConst unkOf(int L) const { return L == 0 ? unk0_ : CCConst(lv_[L].unk); }
1204 CCConst unkSOf(int L) const { return L == 0 ? unkS0_ : CCConst(lv_[L].unks); }
1205
1208 void allocateSolid(Level& v, int L) {
1209 if (v.ress.extent(0) == v.n)
1210 return;
1211 v.ress = CCField("smg_ress", v.n);
1212 v.AFXs = CCField("smg_afxs", v.n);
1213 v.AFYs = CCField("smg_afys", v.n);
1214 v.AFZs = CCField("smg_afzs", v.n);
1215 if (L == 0)
1216 return;
1217 v.xs = CCField("smg_xs", v.n);
1218 v.rhss = CCField("smg_rhss", v.n);
1219 v.ACs = CCField("smg_acs", v.n);
1220 v.unks = CCField("smg_unks", v.n);
1221 v.masss = CCField("smg_masss", v.n);
1222 v.pxs = CCField("smg_pxs", v.n);
1223 v.pys = CCField("smg_pys", v.n);
1224 v.pzs = CCField("smg_pzs", v.n);
1225 v.W = CCField("smg_w", v.n);
1226 }
1227
1228 double countUnknownS(int L) const {
1229 const Level& lv = lv_[L];
1230 const C3 e = lv.ext;
1231 const int g = lv.g;
1232 CCConst unk = unkSOf(L);
1233 double s = 0.0;
1234 ccReduce3(
1235 "peclet::flow::smg_count_s", C3{g, g, g}, C3{e.x - g, e.y - g, e.z - g},
1236 KOKKOS_LAMBDA(int lx, int ly, int lz, double& acc) {
1237 const long i = (long)lx + (long)ly * e.x + (long)lz * (long)e.x * e.y;
1238 acc += unk(i) > 0.5 ? 1.0 : 0.0;
1239 },
1240 Kokkos::Sum<double>(s));
1241 allSum(&s, 1);
1242 return s;
1243 }
1244
1247 void removeMeanTwo(int L, CCField f, CCField s) {
1248 const Level& lv = lv_[L];
1249 const double nTot = lv.nUnk + lv.nUnkS;
1250 if (!(nTot > 0.0))
1251 return;
1252 const C3 e = lv.ext;
1253 const int g = lv.g;
1254 CCConst uf = unkOf(L), us = unkSOf(L);
1255 double sum = 0.0;
1256 ccReduce3(
1257 "peclet::flow::smg_mean_two", C3{g, g, g}, C3{e.x - g, e.y - g, e.z - g},
1258 KOKKOS_LAMBDA(int lx, int ly, int lz, double& acc) {
1259 const long i = (long)lx + (long)ly * e.x + (long)lz * (long)e.x * e.y;
1260 acc += (uf(i) > 0.5 ? f(i) : 0.0) + (us(i) > 0.5 ? s(i) : 0.0);
1261 },
1262 Kokkos::Sum<double>(sum));
1263 allSum(&sum, 1);
1264 const double mean = sum / nTot;
1265 ccFor3(
1266 "peclet::flow::smg_mean_two_sub", C3{g, g, g}, C3{e.x - g, e.y - g, e.z - g},
1267 KOKKOS_LAMBDA(int lx, int ly, int lz) {
1268 const long i = (long)lx + (long)ly * e.x + (long)lz * (long)e.x * e.y;
1269 if (uf(i) > 0.5)
1270 f(i) -= mean;
1271 if (us(i) > 0.5)
1272 s(i) -= mean;
1273 });
1274 }
1275
1279 Level& l0 = lv_[0];
1280 allocateSolid(l0, 0);
1281 const C3 e = l0.ext;
1282 const double wx = w_[0], wy = w_[1], wz = w_[2];
1283 CCConst px = in.spx, py = in.spy, pz = in.spz;
1284 CCField AFX = l0.AFXs, AFY = l0.AFYs, AFZ = l0.AFZs;
1285 ccFor3(
1286 "peclet::flow::smg_faces0_solid", C3{0, 0, 0}, e, KOKKOS_LAMBDA(int x, int y, int z) {
1287 const long i = (long)x + (long)y * e.x + (long)z * (long)e.x * e.y;
1288 AFX(i) = -(wx * px(i));
1289 AFY(i) = -(wy * py(i));
1290 AFZ(i) = -(wz * pz(i));
1291 });
1292 }
1293
1297 void buildCoarseSolid(const Inputs& in, int L) {
1298 Level& c = lv_[L];
1299 Level& fin = lv_[L - 1];
1300 const C3 ratio = fin.ratio;
1301 if (L == 1)
1302 smg::restrictMax(c.unks, in.sunk, c.ext, fin.ext, c.g, fin.g, c.inner, ratio);
1303 else
1304 smg::restrictMax(c.unks, CCConst(fin.unks), c.ext, fin.ext, c.g, fin.g, c.inner, ratio);
1305 c.nUnkS = countUnknownS(L);
1306 {
1307 CCField px = c.pxs, py = c.pys, pz = c.pzs;
1308 const C3 ce = c.ext, fe = fin.ext;
1309 const int gc = c.g, gf = fin.g;
1310 const CCConst fx = L == 1 ? in.spx : CCConst(fin.pxs),
1311 fy = L == 1 ? in.spy : CCConst(fin.pys),
1312 fz = L == 1 ? in.spz : CCConst(fin.pzs);
1313 ccFor3(
1314 "peclet::flow::smg_coarsen_faces_solid", C3{0, 0, 0}, c.inner,
1315 KOKKOS_LAMBDA(int icx, int icy, int icz) {
1316 coarsenOpenAvgCell(px, py, pz, fx, fy, fz, ce, fe, gc, gf, ratio, icx, icy, icz);
1317 });
1318 fill(L, c.pxs);
1319 fill(L, c.pys);
1320 fill(L, c.pzs);
1321 const CCField P[3] = {c.pxs, c.pys, c.pzs};
1322 for (int a = 0; a < 3; ++a)
1323 if (!per_[a] && touches(c, 2 * a))
1324 smg::zeroPlanes(P[a], c.ext, a, 0, c.g + 1);
1325 }
1326 restrictAvg(c.masss, L == 1 ? in.ms0 : CCConst(fin.masss), c.ext, fin.ext, c.g, fin.g, c.inner,
1327 ratio);
1328 const C3 e = c.ext, ci = c.inner, cf = c.cfac;
1329 const int g = c.g;
1330 const double wx = w_[0] / ((double)cf.x * cf.x), wy = w_[1] / ((double)cf.y * cf.y),
1331 wz = w_[2] / ((double)cf.z * cf.z);
1332 CCField AC = c.ACs, AFX = c.AFXs, AFY = c.AFYs, AFZ = c.AFZs, W = c.W;
1333 CCConst px = c.pxs, py = c.pys, pz = c.pzs, unk = c.unks, mass = c.masss;
1334 ccFor3(
1335 "peclet::flow::smg_coarse_faces_solid", C3{0, 0, 0}, e, KOKKOS_LAMBDA(int x, int y, int z) {
1336 const long i = (long)x + (long)y * e.x + (long)z * (long)e.x * e.y;
1337 AFX(i) = -(wx * px(i));
1338 AFY(i) = -(wy * py(i));
1339 AFZ(i) = -(wz * pz(i));
1340 });
1341 ccFor3(
1342 "peclet::flow::smg_coarse_diag_solid", C3{0, 0, 0}, ci,
1343 KOKKOS_LAMBDA(int icx, int icy, int icz) {
1344 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
1345 const long i = (long)(icx + g) + (long)(icy + g) * sy + (long)(icz + g) * sz;
1346 if (!(unk(i) > 0.5)) {
1347 AC(i) = 1.0;
1348 AFX(i) = AFY(i) = AFZ(i) = 0.0;
1349 W(i) = 0.0;
1350 return;
1351 }
1352 const double tw = wx * px(i), te = wx * px(i + sx);
1353 const double ts = wy * py(i), tn = wy * py(i + sy);
1354 const double tb = wz * pz(i), tt = wz * pz(i + sz);
1355 AC(i) = (mass(i) + (((tw + te) + (ts + tn)) + (tb + tt))) + W(i);
1356 });
1357 }
1358
1360 const int ext[3] = {lv.ext.x, lv.ext.y, lv.ext.z};
1361 for (int a = 0; a < 3; ++a) {
1362 if (per_[a])
1363 continue;
1364 if (touches(lv, 2 * a))
1365 smg::zeroPlanes(f, lv.ext, a, 0, lv.g);
1366 if (touches(lv, 2 * a + 1))
1367 smg::zeroPlanes(f, lv.ext, a, ext[a] - lv.g, ext[a]);
1368 }
1369 }
1370
1371 // ---- reductions ----
1372 void allSum(double* v, int k) const {
1373#ifdef PECLET_FLOW_MPI
1374 if (distributed_) {
1375 std::vector<double> o((std::size_t)k);
1376 MPI_Allreduce(v, o.data(), k, MPI_DOUBLE, MPI_SUM, comm_);
1377 for (int j = 0; j < k; ++j)
1378 v[j] = o[(std::size_t)j];
1379 }
1380#endif
1381 (void)v;
1382 (void)k;
1383 }
1384 double countUnknown(int L) const {
1385 const Level& lv = lv_[L];
1386 const C3 e = lv.ext;
1387 const int g = lv.g;
1388 CCConst unk = unkOf(L);
1389 double s = 0.0;
1390 ccReduce3(
1391 "peclet::flow::smg_count", C3{g, g, g}, C3{e.x - g, e.y - g, e.z - g},
1392 KOKKOS_LAMBDA(int lx, int ly, int lz, double& acc) {
1393 const long i = (long)lx + (long)ly * e.x + (long)lz * (long)e.x * e.y;
1394 acc += unk(i) > 0.5 ? 1.0 : 0.0;
1395 },
1396 Kokkos::Sum<double>(s));
1397 allSum(&s, 1);
1398 return s;
1399 }
1400
1401 void removeMean(int L, CCField f) {
1402 const Level& lv = lv_[L];
1403 if (!(lv.nUnk > 0.0))
1404 return;
1405 const C3 e = lv.ext;
1406 const int g = lv.g;
1407 CCConst unk = unkOf(L);
1408 double s = 0.0;
1409 ccReduce3(
1410 "peclet::flow::smg_mean", C3{g, g, g}, C3{e.x - g, e.y - g, e.z - g},
1411 KOKKOS_LAMBDA(int lx, int ly, int lz, double& acc) {
1412 const long i = (long)lx + (long)ly * e.x + (long)lz * (long)e.x * e.y;
1413 if (unk(i) > 0.5)
1414 acc += f(i);
1415 },
1416 Kokkos::Sum<double>(s));
1417 allSum(&s, 1);
1418 const double mean = s / lv.nUnk;
1419 ccFor3(
1420 "peclet::flow::smg_mean_sub", C3{g, g, g}, C3{e.x - g, e.y - g, e.z - g},
1421 KOKKOS_LAMBDA(int lx, int ly, int lz) {
1422 const long i = (long)lx + (long)ly * e.x + (long)lz * (long)e.x * e.y;
1423 if (unk(i) > 0.5)
1424 f(i) -= mean;
1425 });
1426 }
1427 double norm2(CCField f) const {
1428 const Level& lv = lv_[0];
1429 const C3 e = lv.ext;
1430 const int g = lv.g;
1431 double s = 0.0;
1432 ccReduce3(
1433 "peclet::flow::smg_norm", C3{g, g, g}, C3{e.x - g, e.y - g, e.z - g},
1434 KOKKOS_LAMBDA(int lx, int ly, int lz, double& acc) {
1435 const long i = (long)lx + (long)ly * e.x + (long)lz * (long)e.x * e.y;
1436 acc += f(i) * f(i);
1437 },
1438 Kokkos::Sum<double>(s));
1439 allSum(&s, 1);
1440 return std::sqrt(s);
1441 }
1442
1443 // ---- build ----
1447 void buildLevel0Faces(const Inputs& in) {
1448 Level& l0 = lv_[0];
1449 const C3 e = l0.ext;
1450 const double lx = in.lam * w_[0], ly = in.lam * w_[1], lz = in.lam * w_[2];
1451 CCConst unk = in.unknown, sax = in.sax, say = in.say, saz = in.saz;
1452 CCField AFX = l0.AFX, AFY = l0.AFY, AFZ = l0.AFZ;
1453 ccFor3(
1454 "peclet::flow::smg_faces0", C3{0, 0, 0}, e, KOKKOS_LAMBDA(int x, int y, int z) {
1455 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
1456 const long i = (long)x + (long)y * sy + (long)z * sz;
1457 const bool u = unk(i) > 0.5;
1458 const double tw = (x > 0 && u && unk(i - sx) > 0.5) ? lx * sax(i) : 0.0;
1459 const double ts = (y > 0 && u && unk(i - sy) > 0.5) ? ly * say(i) : 0.0;
1460 const double tb = (z > 0 && u && unk(i - sz) > 0.5) ? lz * saz(i) : 0.0;
1461 AFX(i) = -tw;
1462 AFY(i) = -ts;
1463 AFZ(i) = -tb;
1464 });
1465 }
1466
1475 bool buildCutLists(const Inputs& in) {
1476 const auto& fo = *in.fac;
1477 if (cutKey_ == fo.cutCell.data() && cutKeyN_ == fo.nCut && (int)cutStart_.size() == nUse_)
1478 return false;
1479 const Level& l0 = lv_[0];
1480 const C3 e0 = l0.ext, n0 = l0.inner;
1481 const int g0 = l0.g;
1482 auto hc = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), fo.cutCell);
1483 // the inner cut rows by level-0 linear index (the rows of one cell are unique)
1484 std::vector<std::pair<long, int>> rows;
1485 rows.reserve((std::size_t)fo.nCut);
1486 for (long r = 0; r < fo.nCut; ++r) {
1487 const long i0 = hc(r);
1488 const int x = (int)(i0 % e0.x) - g0, y = (int)((i0 / e0.x) % e0.y) - g0,
1489 z = (int)(i0 / ((long)e0.x * e0.y)) - g0;
1490 if (x >= 0 && x < n0.x && y >= 0 && y < n0.y && z >= 0 && z < n0.z)
1491 rows.emplace_back(i0, (int)r);
1492 }
1493 std::sort(rows.begin(), rows.end());
1494 cutStart_.assign((std::size_t)nUse_, Kokkos::View<int*, CCMem>());
1495 cutRows_.assign((std::size_t)nUse_, Kokkos::View<int*, CCMem>());
1496 for (int L = 1; L < nUse_; ++L) {
1497 const Level& c = lv_[L];
1498 const C3 ci = c.inner, cf = c.cfac;
1499 const long nc = (long)ci.x * ci.y * ci.z;
1500 std::vector<int> start((std::size_t)nc + 1, 0), list(rows.size());
1501 auto coarseOf = [&](long i0) {
1502 const int x = (int)(i0 % e0.x) - g0, y = (int)((i0 / e0.x) % e0.y) - g0,
1503 z = (int)(i0 / ((long)e0.x * e0.y)) - g0;
1504 return (long)(x / cf.x) + (long)(y / cf.y) * ci.x + (long)(z / cf.z) * ci.x * ci.y;
1505 };
1506 for (const auto& q : rows)
1507 ++start[(std::size_t)coarseOf(q.first) + 1];
1508 for (long k = 0; k < nc; ++k)
1509 start[(std::size_t)k + 1] += start[(std::size_t)k];
1510 std::vector<int> pos(start.begin(), start.end() - 1);
1511 for (const auto& q : rows) // stable: increasing level-0 index within each coarse cell
1512 list[(std::size_t)pos[(std::size_t)coarseOf(q.first)]++] = q.second;
1513 Kokkos::View<int*, CCMem> ds("smg_cut_start", (std::size_t)nc + 1),
1514 dl("smg_cut_rows", std::max<std::size_t>(list.size(), 1));
1515 Kokkos::deep_copy(
1516 ds, Kokkos::View<const int*, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged>>(
1517 start.data(), start.size()));
1518 if (!list.empty())
1519 Kokkos::deep_copy(
1520 Kokkos::subview(dl, std::make_pair((std::size_t)0, list.size())),
1521 Kokkos::View<const int*, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged>>(
1522 list.data(), list.size()));
1523 cutStart_[(std::size_t)L] = ds;
1524 cutRows_[(std::size_t)L] = dl;
1525 }
1526 cutKey_ = fo.cutCell.data();
1527 cutKeyN_ = fo.nCut;
1528 return true;
1529 }
1530
1539 bool wallKeep(const Inputs& in) {
1540 const auto& fo = *in.fac;
1541 const bool two = in.twoPhase;
1542 bool keep = wallValid_ && wallTwo_ == two && wallUse_ == nUse_ &&
1543 wallUnk_ == in.unknown.data() && cwSnap_.extent(0) == (std::size_t)fo.n &&
1544 (!two || ccSnap_.extent(0) == (std::size_t)fo.n);
1545 if (keep && fo.n > 0) {
1546 auto a = in.facetW, as = Kokkos::View<const double*, CCMem>(cwSnap_);
1547 auto c = two ? in.facetC : in.facetW,
1548 cs = two ? Kokkos::View<const double*, CCMem>(ccSnap_) : as;
1549 long diff = 0;
1550 Kokkos::parallel_reduce(
1551 "peclet::flow::smg_wall_same", Kokkos::RangePolicy<CCExec>(CCExec(), 0, fo.n),
1552 KOKKOS_LAMBDA(const long f, long& d) {
1553 auto same = [](double u, double w) {
1554 return u == w && Kokkos::signbit(u) == Kokkos::signbit(w);
1555 };
1556 if (!same(a(f), as(f)) || (two && !same(c(f), cs(f))))
1557 ++d;
1558 },
1559 diff);
1560 keep = diff == 0;
1561 }
1562 if (!keep) { // the gather below runs: snapshot its inputs
1563 if (cwSnap_.extent(0) != (std::size_t)fo.n)
1564 cwSnap_ = Kokkos::View<double*, CCMem>("smg_cw_snap", (std::size_t)fo.n);
1565 Kokkos::deep_copy(CCExec(), cwSnap_, in.facetW);
1566 if (two) {
1567 if (ccSnap_.extent(0) != (std::size_t)fo.n)
1568 ccSnap_ = Kokkos::View<double*, CCMem>("smg_cc_snap", (std::size_t)fo.n);
1569 Kokkos::deep_copy(CCExec(), ccSnap_, in.facetC);
1570 }
1571 wallValid_ = true;
1572 wallTwo_ = two;
1573 wallUse_ = nUse_;
1574 wallUnk_ = in.unknown.data();
1575 }
1576 return keep;
1577 }
1578
1581 void buildPlane0(const Inputs& in, int f) {
1582 Level& l0 = lv_[0];
1583 const int a = f / 2, side = f % 2;
1584 int t1, t2;
1585 tangents(a, t1, t2);
1586 const int ext[3] = {l0.ext.x, l0.ext.y, l0.ext.z};
1587 const long st[3] = {1, (long)l0.ext.x, (long)l0.ext.x * l0.ext.y};
1588 const int g = l0.g;
1589 const int n1 = ext[t1] - 2 * g, n2 = ext[t2] - 2 * g;
1590 const long sa = st[a], s1 = st[t1], s2 = st[t2];
1591 const int aInner = side == 0 ? g : ext[a] - g - 1;
1592 if (l0.plane[f].extent(0) != (std::size_t)n1 * n2)
1593 l0.plane[f] = Kokkos::View<double*, CCMem>("smg_plane0", (long)n1 * n2);
1594 auto pl = l0.plane[f];
1595 CCConst unk = in.unknown;
1596 CCConst saA = a == 0 ? in.sax : (a == 1 ? in.say : in.saz);
1597 const double lam = in.lam;
1598 CCExec space;
1599 Kokkos::parallel_for(
1600 "peclet::flow::smg_plane0", MDRange2<CCExec>(space, {0, 0}, {n1, n2}),
1601 KOKKOS_LAMBDA(int j1, int j2) {
1602 const long i = (long)aInner * sa + (long)(j1 + g) * s1 + (long)(j2 + g) * s2;
1603 const double abf = side == 0 ? saA(i) : saA(i + sa);
1604 pl((long)j1 + (long)j2 * n1) = unk(i) > 0.5 ? lam * abf : 0.0;
1605 });
1606 }
1607
1608 static void tangents(int a, int& t1, int& t2) {
1609 t1 = (a == 0) ? 1 : 0;
1610 t2 = (a == 2) ? 1 : 2;
1611 }
1612
1613 void buildCoarse(const Inputs& in, int L) {
1614 Level& c = lv_[L];
1615 Level& fin = lv_[L - 1];
1616 const C3 ratio = fin.ratio;
1617 // pins: max of the children's unknown flags
1618 if (L == 1)
1619 smg::restrictMax(c.unk, in.unknown, c.ext, fin.ext, c.g, fin.g, c.inner, ratio);
1620 else
1621 smg::restrictMax(c.unk, CCConst(fin.unk), c.ext, fin.ext, c.g, fin.g, c.inner, ratio);
1622 c.nUnk = countUnknown(L);
1623 // face products <Lam a>: coarsenOpenAvg's cell body on the fine products
1624 {
1625 CCField px = c.px, py = c.py, pz = c.pz;
1626 const C3 ce = c.ext, fe = fin.ext;
1627 const int gc = c.g, gf = fin.g;
1628 if (L == 1) {
1629 const long sy0 = fe.x, sz0 = (long)fe.x * fe.y;
1630 const smg::GuardedProduct fx{in.sax, in.unknown, in.lam, 1};
1631 const smg::GuardedProduct fy{in.say, in.unknown, in.lam, sy0};
1632 const smg::GuardedProduct fz{in.saz, in.unknown, in.lam, sz0};
1633 ccFor3(
1634 "peclet::flow::smg_coarsen_faces", C3{0, 0, 0}, c.inner,
1635 KOKKOS_LAMBDA(int icx, int icy, int icz) {
1636 coarsenOpenAvgCell(px, py, pz, fx, fy, fz, ce, fe, gc, gf, ratio, icx, icy, icz);
1637 });
1638 } else {
1639 CCConst fx = fin.px, fy = fin.py, fz = fin.pz;
1640 ccFor3(
1641 "peclet::flow::smg_coarsen_faces", C3{0, 0, 0}, c.inner,
1642 KOKKOS_LAMBDA(int icx, int icy, int icz) {
1643 coarsenOpenAvgCell(px, py, pz, fx, fy, fz, ce, fe, gc, gf, ratio, icx, icy, icz);
1644 });
1645 }
1646 fill(L, c.px);
1647 fill(L, c.py);
1648 fill(L, c.pz);
1649 // the low domain face of a non-periodic axis: no coupling toward the pinned ghost
1650 const CCField P[3] = {c.px, c.py, c.pz};
1651 for (int a = 0; a < 3; ++a)
1652 if (!per_[a] && touches(c, 2 * a))
1653 smg::zeroPlanes(P[a], c.ext, a, 0, c.g + 1);
1654 }
1655 // mass
1656 if (L == 1) {
1657 // A2: on the advective path the mass carries the open-face outflow omega_open (the interior
1658 // outflow is Q's); otherwise WO-5's lumped outflow, or nothing (WO-4) — those two verbatim
1659 const bool hasOut = in.advective || in.hasOutflow;
1660 const CCConst out = in.advective ? in.omegaOpen : (in.hasOutflow ? in.outflow : in.kappa);
1661 const smg::MassField m0{in.kappa, in.unknown, out, in.idt, hasOut};
1662 CCField cm = c.mass;
1663 const C3 ce = c.ext, fe = fin.ext, ci = c.inner;
1664 const int gc = c.g, gf = fin.g;
1665 ccFor3(
1666 "peclet::flow::smg_restrict_mass", C3{0, 0, 0}, ci,
1667 KOKKOS_LAMBDA(int icx, int icy, int icz) {
1668 restrictAvgCell(cm, m0, ce, fe, gc, gf, ratio, icx, icy, icz);
1669 });
1670 } else {
1671 restrictAvg(c.mass, CCConst(fin.mass), c.ext, fin.ext, c.g, fin.g, c.inner, ratio);
1672 }
1673 // Dirichlet boundary planes, averaged from the finer level's
1674 for (int f = 0; f < 6; ++f)
1675 if (dirFace_[f] && touches(c, f))
1676 coarsenPlane(L, f);
1677 // the operator
1678 if (two_) {
1679 allocateSolid(c, L);
1680 Kokkos::deep_copy(CCExec(), c.W, 0.0);
1681 }
1682 assembleCoarse(in, L);
1683 if (two_) // WO-7: the solid phase, W gathered by assembleCoarse
1684 buildCoarseSolid(in, L);
1685 if (adv_) { // A2: the coarse advection, then the 7 bands
1686 coarsenAdvection(in, L);
1687 assembleBands(L);
1688 }
1689 }
1690
1697 void coarsenAdvection(const Inputs& in, int L) {
1698 Level& c = lv_[L];
1699 Level& fin = lv_[L - 1];
1700 if (c.qp[0].extent(0) != c.n)
1701 for (int a = 0; a < 3; ++a) {
1702 c.qp[a] = CCField("smg_qp", c.n);
1703 c.qm[a] = CCField("smg_qm", c.n);
1704 }
1705 const C3 ratio = fin.ratio, ce = c.ext, fe = fin.ext;
1706 const int gc = c.g, gf = fin.g;
1707 const double ir[3] = {1.0 / (double)ratio.x, 1.0 / (double)ratio.y, 1.0 / (double)ratio.z};
1708 for (int sgn = 0; sgn < 2; ++sgn) {
1709 CCField qx = sgn == 0 ? c.qp[0] : c.qm[0], qy = sgn == 0 ? c.qp[1] : c.qm[1],
1710 qz = sgn == 0 ? c.qp[2] : c.qm[2];
1711 const double irx = ir[0], iry = ir[1], irz = ir[2];
1712 if (L == 1) {
1713 const long sy0 = fe.x, sz0 = (long)fe.x * fe.y;
1714 const double sg = sgn == 0 ? 1.0 : -1.0;
1715 const smg::GuardedPart fx{in.phi[0], in.unknown, 1, sg};
1716 const smg::GuardedPart fy{in.phi[1], in.unknown, sy0, sg};
1717 const smg::GuardedPart fz{in.phi[2], in.unknown, sz0, sg};
1718 ccFor3(
1719 "peclet::flow::smg_coarsen_adv", C3{0, 0, 0}, c.inner,
1720 KOKKOS_LAMBDA(int icx, int icy, int icz) {
1721 coarsenOpenAvgCell(qx, qy, qz, fx, fy, fz, ce, fe, gc, gf, ratio, icx, icy, icz);
1722 const long ci =
1723 (long)(icx + gc) + (long)(icy + gc) * ce.x + (long)(icz + gc) * (long)ce.x * ce.y;
1724 qx(ci) *= irx;
1725 qy(ci) *= iry;
1726 qz(ci) *= irz;
1727 });
1728 } else {
1729 const CCConst fx = sgn == 0 ? fin.qp[0] : fin.qm[0], fy = sgn == 0 ? fin.qp[1] : fin.qm[1],
1730 fz = sgn == 0 ? fin.qp[2] : fin.qm[2];
1731 ccFor3(
1732 "peclet::flow::smg_coarsen_adv", C3{0, 0, 0}, c.inner,
1733 KOKKOS_LAMBDA(int icx, int icy, int icz) {
1734 coarsenOpenAvgCell(qx, qy, qz, fx, fy, fz, ce, fe, gc, gf, ratio, icx, icy, icz);
1735 const long ci =
1736 (long)(icx + gc) + (long)(icy + gc) * ce.x + (long)(icz + gc) * (long)ce.x * ce.y;
1737 qx(ci) *= irx;
1738 qy(ci) *= iry;
1739 qz(ci) *= irz;
1740 });
1741 }
1742 }
1743 for (int a = 0; a < 3; ++a) {
1744 fill(L, c.qp[a]);
1745 fill(L, c.qm[a]);
1746 }
1747 for (int a = 0; a < 3; ++a)
1748 if (!per_[a] && touches(c, 2 * a)) {
1749 smg::zeroPlanes(c.qp[a], c.ext, a, 0, c.g + 1);
1750 smg::zeroPlanes(c.qm[a], c.ext, a, 0, c.g + 1);
1751 }
1752 }
1753
1758 void assembleBands(int L) {
1759 Level& c = lv_[L];
1760 if (c.AW.extent(0) != c.n) {
1761 c.AW = CCField("smg_aw", c.n);
1762 c.AE = CCField("smg_ae", c.n);
1763 c.AS = CCField("smg_as", c.n);
1764 c.AN = CCField("smg_an", c.n);
1765 c.AB = CCField("smg_ab", c.n);
1766 c.AT = CCField("smg_at", c.n);
1767 }
1768 const C3 e = c.ext;
1769 const int g = c.g;
1770 CCField AC = c.AC, AW = c.AW, AE = c.AE, AS = c.AS, AN = c.AN, AB = c.AB, AT = c.AT;
1771 CCConst AFX = c.AFX, AFY = c.AFY, AFZ = c.AFZ, unk = c.unk;
1772 CCConst qpx = c.qp[0], qpy = c.qp[1], qpz = c.qp[2], qmx = c.qm[0], qmy = c.qm[1],
1773 qmz = c.qm[2];
1774 Kokkos::deep_copy(CCExec(), AW, 0.0);
1775 Kokkos::deep_copy(CCExec(), AE, 0.0);
1776 Kokkos::deep_copy(CCExec(), AS, 0.0);
1777 Kokkos::deep_copy(CCExec(), AN, 0.0);
1778 Kokkos::deep_copy(CCExec(), AB, 0.0);
1779 Kokkos::deep_copy(CCExec(), AT, 0.0);
1780 ccFor3(
1781 "peclet::flow::smg_coarse_bands", C3{g, g, g}, C3{e.x - g, e.y - g, e.z - g},
1782 KOKKOS_LAMBDA(int lx, int ly, int lz) {
1783 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
1784 const long i = (long)lx + (long)ly * sy + (long)lz * sz;
1785 if (!(unk(i) > 0.5))
1786 return;
1787 AC(i) =
1788 AC(i) + ((qmx(i) + qpx(i + sx)) + (qmy(i) + qpy(i + sy))) + (qmz(i) + qpz(i + sz));
1789 AW(i) = AFX(i) - qpx(i);
1790 AE(i) = AFX(i + sx) - qmx(i + sx);
1791 AS(i) = AFY(i) - qpy(i);
1792 AN(i) = AFY(i + sy) - qmy(i + sy);
1793 AB(i) = AFZ(i) - qpz(i);
1794 AT(i) = AFZ(i + sz) - qmz(i + sz);
1795 });
1796 }
1797
1798 void coarsenPlane(int L, int f) {
1799 Level& c = lv_[L];
1800 Level& fin = lv_[L - 1];
1801 const int a = f / 2;
1802 int t1, t2;
1803 tangents(a, t1, t2);
1804 const int cin[3] = {c.inner.x, c.inner.y, c.inner.z};
1805 const int fin3[3] = {fin.inner.x, fin.inner.y, fin.inner.z};
1806 const int rr[3] = {fin.ratio.x, fin.ratio.y, fin.ratio.z};
1807 const int n1 = cin[t1], n2 = cin[t2], f1 = fin3[t1];
1808 const int r1 = rr[t1], r2 = rr[t2];
1809 if (c.plane[f].extent(0) != (std::size_t)n1 * n2)
1810 c.plane[f] = Kokkos::View<double*, CCMem>("smg_plane", (long)n1 * n2);
1811 auto pc = c.plane[f];
1812 auto pf = fin.plane[f];
1813 CCExec space;
1814 Kokkos::parallel_for(
1815 "peclet::flow::smg_coarsen_plane", MDRange2<CCExec>(space, {0, 0}, {n1, n2}),
1816 KOKKOS_LAMBDA(int j1, int j2) {
1817 double s = 0.0;
1818 for (int p = 0; p < r1; ++p)
1819 for (int q = 0; q < r2; ++q)
1820 s += pf((long)(r1 * j1 + p) + (long)(r2 * j2 + q) * f1);
1821 pc((long)j1 + (long)j2 * n1) = s / (double)(r1 * r2);
1822 });
1823 }
1824
1828 void assembleCoarse(const Inputs& in, int L) {
1829 Level& c = lv_[L];
1830 const C3 e = c.ext, ci = c.inner, cf = c.cfac;
1831 const int g = c.g;
1832 const double wx = w_[0] / ((double)cf.x * cf.x), wy = w_[1] / ((double)cf.y * cf.y),
1833 wz = w_[2] / ((double)cf.z * cf.z);
1834 CCField AC = c.AC, AFX = c.AFX, AFY = c.AFY, AFZ = c.AFZ;
1835 CCConst px = c.px, py = c.py, pz = c.pz;
1836 ccFor3(
1837 "peclet::flow::smg_coarse_faces", C3{0, 0, 0}, e, KOKKOS_LAMBDA(int x, int y, int z) {
1838 const long i = (long)x + (long)y * e.x + (long)z * (long)e.x * e.y;
1839 AFX(i) = -(wx * px(i));
1840 AFY(i) = -(wy * py(i));
1841 AFZ(i) = -(wz * pz(i));
1842 });
1843 // the wall term: the plain average of the level-0 wall terms (fine probe distance, A1)
1844 const double invN = 1.0 / ((double)cf.x * cf.y * cf.z);
1845 auto cstart = cutStart_[(std::size_t)L];
1846 auto crows = cutRows_[(std::size_t)L];
1847 auto cutCell = in.fac->cutCell;
1848 CCConst unk0 = in.unknown;
1849 CCConst unk = c.unk, mass = c.mass;
1850 auto cfs = in.fac->cellFacetStart;
1851 auto cw = in.facetW;
1852 // WO-7 (A1 for the interface): the coupling W_c is the plain average of the level-0 cc at the
1853 // fine probe distances, gathered in the same order; it enters the fluid diagonal and is stored
1854 // as the level's W (the solid diagonal and the off-diagonal -W follow in buildCoarseSolid).
1855 const bool two = two_;
1856 auto ccv = in.facetC;
1857 CCField Wl = c.W;
1858 if (c.wallG.extent(0) != c.n) // the gathered W (and W_c) of this level, kept across builds
1859 c.wallG = CCField("smg_wall_gathered", c.n);
1860 if (two && c.wallCG.extent(0) != c.n)
1861 c.wallCG = CCField("smg_wallc_gathered", c.n);
1862 CCField wg = c.wallG, wcg = c.wallCG;
1863 const bool reuse = wallReuse_;
1864 ccFor3(
1865 "peclet::flow::smg_coarse_diag", C3{0, 0, 0}, ci, KOKKOS_LAMBDA(int icx, int icy, int icz) {
1866 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
1867 const long i = (long)(icx + g) + (long)(icy + g) * sy + (long)(icz + g) * sz;
1868 if (!(unk(i) > 0.5)) {
1869 AC(i) = 1.0;
1870 AFX(i) = AFY(i) = AFZ(i) = 0.0;
1871 return;
1872 }
1873 double W = 0.0, Wc = 0.0;
1874 if (reuse) { // wallKeep: the inputs of the last gather are unchanged
1875 W = wg(i);
1876 if (two)
1877 Wc = wcg(i);
1878 } else {
1879 // the cut descendants in increasing level-0 index = the (dz, dy, dx) order
1880 const long ic = (long)icx + (long)icy * ci.x + (long)icz * ci.x * ci.y;
1881 for (int k = cstart(ic); k < cstart(ic + 1); ++k) {
1882 const int row = crows(k);
1883 if (!(unk0(cutCell(row)) > 0.5))
1884 continue;
1885 for (int f = cfs(row); f < cfs(row + 1); ++f) {
1886 W += cw(f);
1887 if (two)
1888 Wc += ccv(f);
1889 }
1890 }
1891 W *= invN;
1892 if (two)
1893 Wc *= invN;
1894 wg(i) = W;
1895 if (two)
1896 wcg(i) = Wc;
1897 }
1898 const double tw = wx * px(i), te = wx * px(i + sx);
1899 const double ts = wy * py(i), tn = wy * py(i + sy);
1900 const double tb = wz * pz(i), tt = wz * pz(i + sz);
1901 if (two) {
1902 AC(i) = ((mass(i) + (((tw + te) + (ts + tn)) + (tb + tt))) + W) + Wc;
1903 Wl(i) = Wc;
1904 } else {
1905 AC(i) = (mass(i) + (((tw + te) + (ts + tn)) + (tb + tt))) + W;
1906 }
1907 });
1908 // Dirichlet domain faces: AC += 2 w_a(L) <Lam a_bf> on the unknown boundary cells
1909 const double wa[3] = {wx, wy, wz};
1910 for (int f = 0; f < 6; ++f) {
1911 if (!(dirFace_[f] && touches(c, f)))
1912 continue;
1913 const int a = f / 2, side = f % 2;
1914 int t1, t2;
1915 tangents(a, t1, t2);
1916 const int ext[3] = {e.x, e.y, e.z};
1917 const long st[3] = {1, (long)e.x, (long)e.x * e.y};
1918 const int n1 = ext[t1] - 2 * g, n2 = ext[t2] - 2 * g;
1919 const long sa = st[a], s1 = st[t1], s2 = st[t2];
1920 const int aInner = side == 0 ? g : ext[a] - g - 1;
1921 const double tw2 = 2.0 * wa[a];
1922 auto pl = c.plane[f];
1923 CCExec space;
1924 Kokkos::parallel_for(
1925 "peclet::flow::smg_coarse_dirichlet", MDRange2<CCExec>(space, {0, 0}, {n1, n2}),
1926 KOKKOS_LAMBDA(int j1, int j2) {
1927 const long i = (long)aInner * sa + (long)(j1 + g) * s1 + (long)(j2 + g) * s2;
1928 if (!(unk(i) > 0.5))
1929 return;
1930 AC(i) += tw2 * pl((long)j1 + (long)j2 * n1);
1931 });
1932 }
1933 }
1934
1935 private:
1936 std::vector<Level> lv_;
1937 Fill fill0_;
1938 bool fill0Wraps_ = false;
1939 CCConst unk0_;
1940 CCConst unkS0_;
1941 bool two_ = false;
1944 std::vector<Kokkos::View<int*, CCMem>> cutStart_, cutRows_;
1945 const void* cutKey_ = nullptr;
1946 long cutKeyN_ = -1;
1949 Kokkos::View<double*, CCMem> cwSnap_, ccSnap_;
1950 bool wallValid_ = false, wallTwo_ = false, wallReuse_ = false;
1951 int wallUse_ = 0;
1952 const void* wallUnk_ = nullptr;
1953 double w_[3] = {1.0, 1.0, 1.0};
1954 double hp_[3] = {1.0, 1.0, 1.0};
1955 bool aniso_ = false;
1956 bool per_[3] = {true, true, true};
1957 bool dirFace_[6] = {false, false, false, false, false, false};
1958 bool singular_ = false;
1959 bool adv_ = false;
1960 int nUse_ = 1;
1961 bool distributed_ = false;
1962#ifdef PECLET_FLOW_MPI
1963 MPI_Comm comm_ = MPI_COMM_NULL;
1964#endif
1965};
1966
1967} // namespace peclet::flow
1968
1969#endif // PECLET_FLOW_SCALAR_MG_HPP
static C3 mgChooseRatio(const double H[3], const bool canA[3], bool aniso, double theta)
static constexpr int kPre
std::function< void(CCField)> Fill
void assembleCoarse(const Inputs &in, int L)
Coarse face form + diagonal: AF_a = -w_a(L) <Lam a> over the full extent; on unknown inner cells AC =...
static constexpr int GC
coarse levels
void buildLevel0SolidFaces(const Inputs &in)
WO-7, level 0: the solid faces AF_a(i) = -(w_a P_a(i)) from the operator's guarded products — the exp...
void sweep(int L, bool fwd)
One full red-black sweep: R -> B forward, B -> R backward.
void sweepColor(int L, int rb)
One colour of red-black Gauss-Seidel on level L: rb = 0 red (global (gx+gy+gz) even),...
static bool can(int d)
void vcycleTwo(int L)
The two-field V-cycle (WO-7): vcycle's structure on both phases — the same sweeps (the 2x2 block upda...
static void tangents(int a, int &t1, int &t2)
void setMetric(const double w[3], const double hp[3])
Per-axis metric (VelocityMG::setMetric): w_a = 1/h_a'^2 and h_a'. Call before init.
void applySurrogateTwo(CCField yf, CCField ys, CCField xf, CCField xs)
y = S x for the two-field surrogate on level 0 (exchanges x first): y_f = S_ff x_f - W x_s,...
bool wallKeep(const Inputs &in)
The gathered coarse wall terms W (and the conjugate W_c) of the last build are reused when their inpu...
bool touches(const Level &lv, int f) const
Does this rank's block on level lv touch global face f (0..5 = -x,+x,-y,+y,-z,+z)?
void setPeriodic(const bool per[3])
Periodicity per axis (the flow's domain faces); a non-periodic axis' ghosts are pinned.
void assembleBands(int L)
A2: the coarse bands from the face form (assembleCoarse) and Q.
int wrapSmooth(int L) const
A red-black pass reads the wrapped neighbour safely only when it has the OTHER colour (so it still ho...
static constexpr int G0
level 0: the scalar's own block
void init(int nx, int ny, int nz, Fill fill0)
Single-rank level table (VelocityMG::init's rule, no depth cap).
void applySurrogate(CCField y, CCField x)
y = S x on level 0 (exchanges x first). For the contraction instrument and tests.
static constexpr int kMaxLevels
no depth cap in practice (the full table)
int levelsUsed() const
Levels the last build set up for the V-cycle (1: level 0 alone, the transient rule).
static constexpr int kBottom
const Level & level(int L) const
void fill(int L, CCField f)
Ghost exchange of a level-L field: level 0 the solver's own; coarse levels the per-level halo (or the...
void zeroNonPeriodicGhosts(Level &lv, CCField f)
C3 chooseRatio(const bool canA[3], C3 cf) const
std::vector< C3 > levelRatios() const
The per-level coarsening ratios (the level table; equal to VelocityMG's on the same grid).
double countUnknownS(int L) const
void coarsenAdvection(const Inputs &in, int L)
A2: Qp/Qm of level L from level L - 1 — level 1 from the guarded positive parts of the level-0 flux,...
CCConst unkSOf(int L) const
static constexpr int kPost
static constexpr double kFullTableKappa
The transient level rule (design §5.2, Amendment A3 + ruling D-WO9-3): kappa_A = 1 + 4 dt' D' sum_a w...
void build(const Inputs &in)
Rebuild the surrogate on every level from the current operator (the level table is kept).
static constexpr double kAspectTheta
void applyTwo(CCField zf, CCField zs, CCField rf, CCField rs)
The two-field z = M^-1 r (WO-7): one V-cycle on the coupled surrogate from z = 0 (level 0 alone: 2 + ...
void apply(CCField z, CCField r)
z = M^-1 r: one V-cycle on the surrogate (level 0 alone: 2 + 2 sweeps) from z = 0.
void restrictZeroX(Level &cs, Level &lv, CCField crhs, CCField fres, CCField cx)
The coarse rhs = restrictAvg of the fine residual, and the coarse iterate x = 0.
void allocate(Level &v, int L)
void removeMeanTwo(int L, CCField f, CCField s)
f, s -= their joint mean over both phases' unknown cells of level L (the singular two-field problem: ...
void buildPlane0(const Inputs &in, int f)
Level-0 Dirichlet boundary plane of face f: <Lam a_bf> = Lam a_bf on unknown boundary cells (sco::dir...
void coarsenPlane(int L, int f)
std::vector< double > contraction(int iters, CCField e, CCField t, CCField z)
Power estimate of the V-cycle's error contraction rho(I - M^-1 S) on the current surrogate (§11 G-ite...
bool buildCutLists(const Inputs &in)
Per coarse level L the overlay rows (cut cells) under each coarse inner cell, in increasing level-0 l...
double countUnknown(int L) const
void setLevel0Wrap(bool on)
Declare that the injected level-0 fill is the single-rank periodic wrap of ALL three axes (Solver::fi...
void allocateSolid(Level &v, int L)
WO-7: the solid-phase arrays of level L (and W on coarse levels), on the first two-field build.
int wrapReads(int L) const
WO-9b, the fused periodic-wrap reads (flow's A3): the axes (bit a) a level-L pass may read across the...
void buildLevel0Faces(const Inputs &in)
Level-0 faces: AF_a(i) = -(Lam w_a a_a(i)) when cells i - st_a and i are both unknowns — the same exp...
void allSum(double *v, int k) const
void buildCoarse(const Inputs &in, int L)
void removeMean(int L, CCField f)
f -= its mean over the unknown cells of level L (CutcellMG::removeMean's form, unweighted).
void buildCoarseSolid(const Inputs &in, int L)
WO-7: the coarse solid phase of level L (after assembleCoarse, which gathered W): pins = max of the c...
CCConst unkOf(int L) const
double norm2(CCField f) const
#define PECLET_FLOW_OMP_SIMD
flow — portable (Kokkos) geometric multigrid for the cut-cell (variable-openness) pressure Poisson.
flow — portable (Kokkos) cut-cell pressure operator + Chorin projection.
Nb nbrsOn(int lx, int ly, int lz, C3 n, int g, long i, long sy, long sz, int wrap)
nbrs in the kernels: on a host backend always the plain i +- s (ScalarMG::wrapReads never asks a host...
void sweepColorFace(CCField x, CCConst rhs, OpV AC, OpV AFX, OpV AFY, OpV AFZ, C3 e, C3 og, int g, int color, C3 n, int wrap)
One colour of the face-form red-black Gauss-Seidel: cutcellSmoothColorFace (mac_pressure....
void residualFace(CCField r, CCConst x, CCConst b, CCField AC, CCField AFX, CCField AFY, CCField AFZ, C3 e, int g, C3 n, int wrap)
r = b - A x (face form, double), inner cells.
void fillShell(CCField f, C3 e, C3 inner, int g, const bool per[3])
The single-rank ghost fill of a level in ONE launch (WO-9b; CutcellMG::fillWrap's form): a ghost cell...
void sweepColorTwo(CCField x, CCField rhs, CCField AC, CCField AFX, CCField AFY, CCField AFZ, CCField AW, CCField AE, CCField AS, CCField AN, CCField AB, CCField AT, CCField xs, CCField rhss, CCField ACs, CCField AFXs, CCField AFYs, CCField AFZs, CCField W, CCConst unkF, C3 e, C3 og, int g, int color, C3 n, int wrap)
One colour of the two-field red-black sweep (same launch forms as cutcellSmoothColorFace).
void zeroPlanes(CCField f, C3 e, int a, int lo, int hi)
f = 0 on the planes [lo, hi) of axis a (full transverse extent).
Nb nbrs(int lx, int ly, int lz, C3 n, int g, long i, long sy, long sz, int wrap)
void residualTwo(CCField r, CCConst x, CCConst b, CCField AC, CCField AFX, CCField AFY, CCField AFZ, CCField AW, CCField AE, CCField AS, CCField AN, CCField AB, CCField AT, CCField rs, CCConst xs, CCConst bs, CCField ACs, CCField AFXs, CCField AFYs, CCField AFZs, CCField W, C3 e, int g, C3 n, int wrap)
The two-field residual: r_f = rhs_f - (S_ff x_f + N_f) + W x_s (N_f from the bands or the faces),...
double bandOff(const XV &x, const BV &AW, const BV &AE, const BV &AS, const BV &AN, const BV &AB, const BV &AT, long i, const Nb &w)
The band-form off-diagonal sum of A2, in its fixed order: ((AW x_W + AE x_E) + (AS x_S + AN x_N)) + (...
double faceOff(const XV &x, const OV &AFX, const OV &AFY, const OV &AFZ, long i, long sx, long sy, long sz, const Nb &w)
The face-form neighbour sum of cutcellSmoothFaceCell, in its order; x's neighbours from nbrs (the coe...
void zeroPinned(CCField x, CCConst unk, C3 e, int g)
x(i) = 0 on the inner cells that are not unknowns (re-zero the pinned cells).
void residualBand(CCField r, CCConst x, CCConst b, CCField AC, CCField AW, CCField AE, CCField AS, CCField AN, CCField AB, CCField AT, C3 e, int g, C3 n, int wrap)
r = b - (AC x + bandOff) over the inner cells (A2's band-form residual).
void twoPhaseCell(const XV &x, const XV &rhs, const BV &AC, const BV &AFX, const BV &AFY, const BV &AFZ, const BV &AW, const BV &AE, const BV &AS, const BV &AN, const BV &AB, const BV &AT, const XV &xs, const XV &rhss, const BV &ACs, const BV &AFXs, const BV &AFYs, const BV &AFZs, const BV &W, const CCConst &unkF, long i, long sx, long sy, long sz, const Nb &w)
The two-field cell update of the conjugate V-cycle (§5.2, WO-7; design A2's WO-7 note): where both ph...
void sweepColorBand(CCField x, CCConst rhs, BV AC, BV AW, BV AE, BV AS, BV AN, BV AB, BV AT, UV unk, C3 e, C3 og, int g, int color, C3 n, int wrap)
One colour of the band-form Gauss-Seidel (A2), on the unknown inner cells of the colour: x_i <- (rhs_...
void restrictMax(CCField coarse, const FV &fine, C3 cext, C3 fext, int gc, int gf, C3 cinner, C3 ratio)
coarse flag = max of the children's flags (1 unknown, 0 pinned), over the coarse inner cells.
Kokkos::DefaultExecutionSpace CCExec
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 restrictAvgZeroX(CCField coarse, CCField coarseX, CCConst fine, C3 cext, C3 fext, int gc, int gf, C3 cinner, C3 ratio)
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 prolongAdd(CCField fine, CCConst coarse, C3 fext, C3 cext, int gf, int gc, C3 finner, C3 ratio)
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 ccReduce3(const char *name, C3 lo, C3 hi, F f, R &&... reducers)
void applyCutcellOp(CCField y, CCConst x, OpV AC, OpV AW, OpV AE, OpV AS, OpV AN, OpV AB, OpV AT, C3 e, int g)
Kokkos::View< double *, CCMem > CCField
void ccFor3(const char *name, C3 lo, C3 hi, F f)
void restrictAvg(CCField coarse, CCConst fine, C3 cext, C3 fext, int gc, int gf, C3 cinner, C3 ratio)
void coarsenOpenAvgCell(const CV &oxc, const CV &oyc, const CV &ozc, const FV &oxf, const FV &oyf, const FV &ozf, C3 cext, C3 fext, int gc, int gf, C3 ratio, int icx, int icy, int icz)
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 restrictAvgCell(const CV &coarse, const FV &fine, C3 cext, C3 fext, int gc, int gf, C3 ratio, int icx, int icy, int icz)
Definition sweep.py:1
flow — the multidimensional iteration policies: iteration order follows storage.
int y
int x
int z
Everything build reads from the scalar operator and the geometry record.
bool fullTable
the level rule (false: level 0 alone)
bool advective
A2 (§6.7): steady with advection -> the V-cycle runs on the ADVECTIVE surrogate S_adv on every level ...
CCConst phi[3]
F/V through the low a-face (st.phi, guarded).
double idt
1/dt' (0 steady)
bool twoPhase
Conjugate (WO-7, §5.2): the coupled two-field levels.
Kokkos::View< const double *, CCMem > facetC
bool singular
steady pure-Neumann: the mean is removed on every level
CCField AT
the operator's level-0 bands
const scg::ScalarFacetOverlay * fac
CCConst outflow
WO-5: the lumped implicit-FOU outflow per level-0 cell (§4.3; already inside SAC),...
CCField SAC
level-0 surrogate diagonal (§4.3)
Kokkos::View< const double *, CCMem > facetW
level-0 wall term per facet (cw = alpha G)
CCConst omegaOpen
the open faces' implicit outflow per level-0 cell
C3 gdim
GLOBAL dims of this level.
double nUnkS
global count of solid unknowns
CCField pz
coarse levels: m and the face products <Lam a>
CCField xs
Conjugate (WO-7): the solid phase in face form (level 0: x/rhs the caller's, AC = SAC_s,...
CCField AFZ
the surrogate, face form (level 0: AC = the caller's SAC)
CCField wallG
WO-9b: the gathered wall term W (and the conjugate W_c) of this coarse level, per cell,...
CCField unk
1 unknown / 0 pinned (level 0: the geometry's flag)
CCField AW
Advective path (A2): the 7 bands (level 0: the operator's AW..AT, AC = SAC; coarse: owned,...
CCField res
level 0: x / rhs are the caller's (z, r); res owned
C3 og
block inner origin (global red-black parity); {0,0,0} single-rank
Kokkos::View< double *, CCMem > plane[6]
Dirichlet domain faces: <Lam a_bf> (owning rank).
double nUnk
global count of unknown cells (the singular mean)
The facet overlay (§3.5): SoA, facet index fastest. Offsets are LINEAR offsets on the block.
Kokkos::View< int *, CCMem > cellFacetStart
CSR, nCut + 1 entries.
Kokkos::View< long *, CCMem > cutCell
their extended linear index
Level-0 guarded positive part of the signed low-face flux: P(i) = max(sgn phi_a(i),...
double operator()(long i) const
Level-0 guarded face product Lam a (no metric weight): the face between cell i - st and cell i carrie...
Definition scalar_mg.hpp:86
double operator()(long i) const
Definition scalar_mg.hpp:90
Level-0 mass m = kappa idt on unknowns, 0 elsewhere (read by restrictAvgCell).
Definition scalar_mg.hpp:99
double operator()(long i) const
Neighbour indices of an inner cell for the fused periodic-wrap reads (WO-9b; flow's A3,...
static int wrap(int a, int n)
static constexpr double AC
Kokkos::View< double *, peclet::flow::CCMem > OpV
Kokkos::View< float *, IMem > FV
static bool same(double a, double b)
static double mean(const std::vector< double > &a)