flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_vof_bc_mpi.cpp
Go to the documentation of this file.
1// flow — multi-rank validation of the two-phase OPEN BOUNDARIES (VoF rung V-BC, WO-R): the inflow
2// colour ghost, the inletOutlet backflow rule, the out-of-domain flux mask and the per-face
3// boundary liquid ledger, on a decomposition that CUTS the inflow and the outflow face.
4//
5// Three things can only break here:
6//
7// 1. **the mask must be built on GLOBAL indices.** `vof::buildOutsideMask` marks a ghost cell
8// only when its GLOBAL index is outside the global grid, exactly as `clampFill` clamps
9// globally. A per-block test would mark every rank's own block boundary and the interior rank
10// boundaries would take the algebraic flux instead of the geometric one — a silent
11// decomposition-dependent advection.
12// 2. **the boundary colour must be applied only by the rank that OWNS the face**
13// (`touchesGlobalFace`, the WO-F rule). Without it every rank would inject the inflow colour
14// into its own block's -z ghost band, i.e. into the middle of the domain.
15// 3. **the ledger must be a per-rank partial sum.** Each rank accumulates the boundary fluxes of
16// the faces it owns; the caller MPI_SUMs them. The budget identity then closes globally.
17//
18// Four configurations on 16x16x32 (the aligned ORB cuts the long z axis at np = 2 and 4). Two are
19// KINEMATIC and gated BITWISE — `slug-kin` and `packing-kin`, which is where the decomposition
20// exactness of this path is actually asserted; two are COUPLED and gated on conservation and
21// cancellation rather than pointwise agreement, because the colour is not a Lipschitz function of
22// the velocity (the long comment on case C, and docs/wo_vof_mpi_parity_gates.md):
23//
24// * `slug-kin` — the WO-R gate G1 budget, KINEMATIC: uniform inflow at -z, outflow at +z, walls
25// elsewhere, the advecting field the exactly-divergence-free uniform w. A liquid slug is
26// injected and then flushed. No pressure solve and no reduction anywhere in the update, so the
27// colour field and the ledger must be BITWISE identical to the single-rank reference at every
28// np, and the global budget must close to round-off.
29//
30// * `jet-coupled` — a liquid inflow into a gas domain at density ratio 100 with the full
31// coupled step (rho(C) closure, variable-density projection). What is gated BITWISE here is
32// that the inflow ghost DENSITY is the inlet fluid's on every rank that owns the face and
33// untouched on every rank that does not; the colour gets the coupled pair of gates (see below).
34//
35// * `packing-kin` — V5a x V-BC COMPOSED and kinematic: the sphere packing of the case below,
36// one sphere cutting the outlet plane, driven by a prescribed velocity that varies in all
37// three directions. BITWISE at every np and thread count. This is the gate on the composed
38// cut-cell x open-boundary colour transport, and the one the coupled case cannot be.
39//
40// * `packing` — the same scene under the full coupled step. Gated on the CONSERVATIVE content:
41// the WO-R budget identity, the solid-colour rule, and a signed-sum gate saying the colour
42// difference CANCELS. Its pointwise colour gate is a bound on the field going wrong, not a
43// reduction floor — the colour is not a Lipschitz function of the velocity, and the long
44// comment there carries the trace that shows why.
45#include <mpi.h>
46
47#include <cmath>
48#include <cstdio>
49#include <cstdlib>
50#include <cstring>
51#include <Kokkos_Core.hpp>
52#include <vector>
53
54#include "flow_ibm.hpp"
55#include "peclet/core/common/types.hpp"
56#include "peclet/core/decomp/block_decomposer.hpp"
57
60
61static constexpr int NX = 16, NY = 16, NZ = 32;
62static constexpr std::size_t GCELLS = (std::size_t)NX * NY * NZ;
63static constexpr double WIN = 1.0; // inflow speed (cells/s)
64static constexpr double DT = 0.2; // WY Courant 0.2
65
66static std::vector<double> gatherGlobal(const std::vector<double>& local, int ox, int oy, int oz,
67 int lnx, int lny, int lnz, int rank, int size) {
68 std::vector<double> global;
69 if (rank == 0)
70 global.assign(GCELLS, 0.0);
71 for (int r = 0; r < size; ++r) {
72 int meta[6] = {ox, oy, oz, lnx, lny, lnz};
73 if (r == 0) {
74 if (rank == 0)
75 for (int z = 0; z < lnz; ++z)
76 for (int y = 0; y < lny; ++y)
77 std::memcpy(&global[(std::size_t)ox + (std::size_t)(y + oy) * NX +
78 (std::size_t)(z + oz) * NX * NY],
79 &local[(std::size_t)y * lnx + (std::size_t)z * lnx * lny],
80 (std::size_t)lnx * sizeof(double));
81 continue;
82 }
83 if (rank == r) {
84 MPI_Send(meta, 6, MPI_INT, 0, 100 + r, MPI_COMM_WORLD);
85 MPI_Send(local.data(), (int)local.size(), MPI_DOUBLE, 0, 200 + r, MPI_COMM_WORLD);
86 } else if (rank == 0) {
87 MPI_Recv(meta, 6, MPI_INT, r, 100 + r, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
88 std::vector<double> buf((std::size_t)meta[3] * meta[4] * meta[5]);
89 MPI_Recv(buf.data(), (int)buf.size(), MPI_DOUBLE, r, 200 + r, MPI_COMM_WORLD,
90 MPI_STATUS_IGNORE);
91 for (int z = 0; z < meta[5]; ++z)
92 for (int y = 0; y < meta[4]; ++y)
93 std::memcpy(&global[(std::size_t)meta[0] + (std::size_t)(y + meta[1]) * NX +
94 (std::size_t)(z + meta[2]) * NX * NY],
95 &buf[(std::size_t)y * meta[3] + (std::size_t)z * meta[3] * meta[4]],
96 (std::size_t)meta[3] * sizeof(double));
97 }
98 }
99 return global;
100}
101
102// NaN-AWARE. `std::fmax(m, NaN)` returns m, so the obvious loop silently reports 0.000e+00 for a
103// field that has gone NaN — a gate that cannot fail. Propagate instead.
104static double maxAbsDiff(const std::vector<double>& a, const std::vector<double>& b) {
105 double m = 0;
106 for (std::size_t i = 0; i < b.size(); ++i) {
107 const double d = std::fabs(a[i] - b[i]);
108 if (!(d == d))
109 return d; // NaN
110 m = std::fmax(m, d);
111 }
112 return m;
113}
114// The SIGNED sum of the difference. `maxAbsDiff` asks whether the two fields agree; this asks
115// whether whatever they disagree about was CONSERVED — a difference that cancels is colour moved
116// between cells (a flux that landed differently), a difference that does not is colour created or
117// destroyed, which is what a decomposition defect does. See the tolerance comment on case C.
118static double signedSumDiff(const std::vector<double>& a, const std::vector<double>& b) {
119 double s = 0;
120 for (std::size_t i = 0; i < b.size(); ++i)
121 s += a[i] - b[i];
122 return s;
123}
124static long countNonFinite(const std::vector<double>& a) {
125 long n = 0;
126 for (double v : a)
127 if (!(v - v == 0.0))
128 ++n;
129 return n;
130}
131static double sumOf(const std::vector<double>& a) {
132 double s = 0;
133 for (double v : a)
134 s += v;
135 return s;
136}
137
138// ------------------------------------------------------------------- config A: the kinematic slug
139static void configureSlug(IbmSolver& s, int lnx, int lny, int lnz) {
140 s.setAdvection(false);
141 s.setRho(1.0);
142 s.setMu(0.0);
143 s.setDt(DT);
144 for (int f = 0; f < 4; ++f)
145 s.setDomainBc(f, 1, 0, 0, 0);
146 s.setDomainBc(4, 2, 0.0, 0.0, WIN);
147 s.setDomainBc(5, 3, 0, 0, 0);
148 s.setPressureGeometry(std::vector<double>((std::size_t)lnx * lny * lnz, 10.0));
149 s.enableVof();
150 s.setVof(std::vector<double>((std::size_t)lnx * lny * lnz, 0.0));
151 s.setField("w", std::vector<double>((std::size_t)lnx * lny * lnz, WIN));
152 s.setVofInflow(4, 1.0);
153 s.setVofBackflow(5, 0.0);
154}
155
156// ------------------------------------------------------------------- config B: the coupled jet
157static void configureJet(IbmSolver& s, int lnx, int lny, int lnz) {
158 s.setAdvection(false);
159 const double rhoG = 1.0, rhoL = 100.0;
160 s.setRho(rhoL);
161 s.setMu(0.2);
162 s.setDt(0.05);
163 for (int f = 0; f < 4; ++f)
164 s.setDomainBc(f, 1, 0, 0, 0);
165 s.setDomainBc(4, 2, 0.0, 0.0, 0.5);
166 s.setDomainBc(5, 3, 0, 0, 0);
167 // the validated open-boundary duct settings (scripts/verify_channel_sdflow.py)
168 s.setVelocityIterations(60);
169 s.setPressureLevels(6);
170 s.setPressureGeometry(std::vector<double>((std::size_t)lnx * lny * lnz, 1e30));
171 s.enableVof();
172 s.setVof(std::vector<double>((std::size_t)lnx * lny * lnz, 0.0)); // a gas domain
173 s.setPropertyModel("rho", peclet::flow::ClosureKind::LinearMix, "C", "",
174 {rhoG, rhoL - rhoG, 0.0, 0.0});
175 // THE DRIVER IS SELECTED LAST: setPropertyModel("rho", ...) fires setDensityMode, which
176 // reselects Chebyshev and silently discards a driver chosen earlier (the WO-H "capped at 120"
177 // tell). Without this the distributed run falls back to `presIters_` standalone V-cycles.
178 s.setPressureFcg(true, 400, 1e-11);
179 s.setVofInflow(4, 1.0); // fed by liquid
180 s.setVofBackflow(5, 0.0);
181}
182
183// ------------------------------ config D: the COLLOCATED bridge through an open boundary
184// All-fluid duct, -z inflow fed by liquid, +z outflow, colour initially filling the upper half.
185// Density ratio 1 on purpose: the colour rides as a passive tracer, so the CONSERVATION BUDGET is
186// a statement about the transport and the boundary ledger alone, with no two-phase pressure
187// coupling in the way.
188static void configureColoJet(Colo& s, int oz, int lnx, int lny, int lnz) {
189 s.setAdvection(false);
190 s.setRho(1.0);
191 s.setMu(0.05);
192 s.setDt(0.05);
193 for (int f = 0; f < 4; ++f)
194 s.setDomainBc(f, 1, 0, 0, 0);
195 s.setDomainBc(4, 2, 0.0, 0.0, 0.5); // -z inflow
196 s.setDomainBc(5, 3, 0, 0, 0); // +z outflow
197 s.setVelocityIterations(60);
198 s.setPressureLevels(4);
199 s.setPressureIterations(400);
200 s.setPressureGeometry(std::vector<double>((std::size_t)lnx * lny * lnz, 1e30));
201 s.enableVof();
202 std::vector<double> c0((std::size_t)lnx * lny * lnz, 0.0);
203 for (int z = 0; z < lnz; ++z)
204 if (z + oz >= NZ / 2)
205 for (int y = 0; y < lny; ++y)
206 for (int x = 0; x < lnx; ++x)
207 c0[(std::size_t)x + (std::size_t)y * lnx + (std::size_t)z * lnx * lny] = 1.0;
208 s.setVof(c0);
209 s.setVofInflow(4, 1.0);
210 s.setVofBackflow(5, 0.0);
211}
212
213// ---------------------------------------------- config C: V5a x V-BC composed (WO-R2 item 2)
214// A sphere array inside the open-boundary duct, one sphere CUTTING the outlet plane, so the
215// cut-cell flux path and the out-of-domain donor rule act on the same faces. The SDF is built from
216// GLOBAL cell centres, so every rank derives its own block's geometry from the same scene.
217static std::vector<double> packingSdf(int ox, int oy, int oz, int lnx, int lny, int lnz) {
218 const double sp[5][4] = {{4.5, 4.5, 11, 3.4},
219 {11.5, 11.5, 11, 3.4},
220 {4.5, 11.5, 20, 3.4},
221 {11.5, 4.5, 20, 3.4},
222 {8.0, 8.0, (double)NZ, 3.6}}; // the last one cuts the +z outlet plane
223 std::vector<double> f((std::size_t)lnx * lny * lnz, 1e30);
224 for (int z = 0; z < lnz; ++z)
225 for (int y = 0; y < lny; ++y)
226 for (int x = 0; x < lnx; ++x) {
227 double d = 1e30;
228 for (const auto& q : sp) {
229 const double dx = x + ox + 0.5 - q[0], dy = y + oy + 0.5 - q[1], dz = z + oz + 0.5 - q[2];
230 d = std::fmin(d, std::sqrt(dx * dx + dy * dy + dz * dz) - q[3]);
231 }
232 f[(std::size_t)x + (std::size_t)y * lnx + (std::size_t)z * lnx * lny] = d;
233 }
234 return f;
235}
236// The KINEMATIC driver for the composed scene: a smooth velocity built from GLOBAL cell centres,
237// so every decomposition prescribes the SAME field and any decomposition dependence left in the
238// colour transport is the transport's own. A UNIFORM field would not do: with every value equal, a
239// ghost band filled from the wrong side is indistinguishable from one filled from the right side,
240// and the whole halo/bridge path would go untested. It is NOT discretely solenoidal — see the
241// budget note on case C0, which is why that case gates reproducibility and not conservation.
242static void packingVelocity(IbmSolver& s, int ox, int oy, int oz, int lnx, int lny, int lnz) {
243 const double pi = 3.14159265358979323846;
244 std::vector<double> fu((std::size_t)lnx * lny * lnz, 0.0), fv(fu), fw(fu);
245 for (int z = 0; z < lnz; ++z)
246 for (int y = 0; y < lny; ++y)
247 for (int x = 0; x < lnx; ++x) {
248 const std::size_t k = (std::size_t)x + (std::size_t)y * lnx + (std::size_t)z * lnx * lny;
249 const double gx = x + ox + 0.5, gy = y + oy + 0.5, gz = z + oz + 0.5;
250 fu[k] =
251 0.10 * std::sin(2 * pi * gx / NX) * std::cos(2 * pi * gy / NY) * std::cos(pi * gz / NZ);
252 fv[k] =
253 0.10 * std::cos(2 * pi * gx / NX) * std::sin(2 * pi * gy / NY) * std::cos(pi * gz / NZ);
254 fw[k] = 0.50 + 0.10 * std::cos(2 * pi * gx / NX) * std::cos(2 * pi * gy / NY) *
255 std::sin(pi * gz / NZ);
256 }
257 s.setField("u", fu);
258 s.setField("v", fv);
259 s.setField("w", fw);
260}
261
262static void configurePacking(IbmSolver& s, int ox, int oy, int oz, int lnx, int lny, int lnz) {
263 s.setAdvection(false);
264 s.setRho(1.0);
265 s.setMu(0.5);
266 s.setDt(0.1);
267 for (int f = 0; f < 4; ++f)
268 s.setDomainBc(f, 1, 0, 0, 0);
269 s.setDomainBc(4, 2, 0.0, 0.0, 0.5); // inflow at -z
270 s.setDomainBc(5, 3, 0, 0, 0); // outflow at +z
271 s.setVelocityIterations(60);
272 s.setPressureLevels(4);
273 s.setPressureIterations(400);
274 s.setSolid(packingSdf(ox, oy, oz, lnx, lny, lnz), true);
275 s.enableVof();
276 std::vector<double> c0((std::size_t)lnx * lny * lnz, 0.0);
277 for (int z = 0; z < lnz; ++z)
278 if (z + oz >= NZ / 2)
279 for (int y = 0; y < lny; ++y)
280 for (int x = 0; x < lnx; ++x)
281 c0[(std::size_t)x + (std::size_t)y * lnx + (std::size_t)z * lnx * lny] = 1.0;
282 s.setVof(c0);
283 s.setVofInflow(4, 1.0);
284 s.setVofBackflow(5, 0.0);
285}
286
287int main(int argc, char** argv) {
288 MPI_Init(&argc, &argv);
289 Kokkos::initialize(argc, argv);
290 int fail = 0;
291 {
292 int rank = 0, size = 1;
293 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
294 MPI_Comm_size(MPI_COMM_WORLD, &size);
295 auto dec = peclet::flow::CutcellMG::decomposition(static_cast<std::size_t>(size), NX, NY, NZ);
296 auto blk = dec.block(rank);
297 const int ox = (int)blk.origin[0], oy = (int)blk.origin[1], oz = (int)blk.origin[2];
298 const int lnx = (int)blk.size[0], lny = (int)blk.size[1], lnz = (int)blk.size[2];
299 const int gn[3] = {NX, NY, NZ};
300 bool cut[3] = {false, false, false};
301 for (const auto& sz : dec.sizes())
302 for (int a = 0; a < 3; ++a)
303 if ((int)sz[a] != gn[a])
304 cut[a] = true;
305 if (rank == 0)
306 std::printf("VOF BC MPI np=%d grid %dx%dx%d block %dx%dx%d cut axes: %s%s%s\n", size, NX,
307 NY, NZ, lnx, lny, lnz, cut[0] ? "x" : "", cut[1] ? "y" : "", cut[2] ? "z" : "");
308 if (size > 1 && !cut[2]) {
309 if (rank == 0)
310 std::printf(" FAIL — the decomposition does NOT cut z (the inflow/outflow axis)\n");
311 fail = 1;
312 }
313
314 // ---------------------------------------------------------------- A: kinematic slug budget
315 {
316 // 20 slug steps then 480 more: at W = 1 and dt = 0.2 the slug's tail clears the 32-cell
317 // domain by step ~182 (so the budget is fully exercised, in == out == 1024) and the run then
318 // continues 300 steps INTO THE EMPTIED-DOMAIN REGIME, which is the gate on WO-R2 item 4.
319 // WO-R stopped at 185 because it had to: once C is nothing but +-1e-18 round-off,
320 // `wyIsMixed` called those cells mixed, the MYC normal of a ~1e-18 stencil is degenerate and
321 // `plicAlpha` divided by it — measured on nvidia-cuda at np = 1 (`PECLET_VOF_BC_TRACE=1`,
322 // this file): C went -inf at step 186 and NaN at 187. With the wisp guard
323 // (`WyAdvector::wispEps`, 1e-8 once `enable_vof` is called) those cells are pure for
324 // reconstruction and fluxed algebraically, and the drain runs indefinitely.
325 const int nSlug = 20, nAfter = 480;
326 IbmSolver sd(lnx, lny, lnz);
327 sd.initMpi(dec, MPI_COMM_WORLD);
328 configureSlug(sd, lnx, lny, lnz);
329 double ledger = 0.0;
330 for (int i = 0; i < nSlug + nAfter; ++i) {
331 if (i == nSlug)
332 sd.setVofInflow(4, 0.0);
333 sd.advectVof();
334 const auto v = sd.vofBcVolumes();
335 if (std::getenv("PECLET_VOF_BC_TRACE") && rank == 0) {
336 const auto cc = sd.getVof();
337 double sm = 0;
338 for (double q : cc)
339 sm += q;
340 std::printf(" trace step %3d: face %.6g %.6g | C sum %.6g nonfinite %ld\n", i, v[4],
341 v[5], sm, countNonFinite(cc));
342 }
343 for (int f = 0; f < 6; ++f)
344 ledger += v[f];
345 }
346 const double locSum = sumOf(sd.getVof());
347 double gSum = 0.0, gLedger = 0.0;
348 MPI_Allreduce(&locSum, &gSum, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
349 MPI_Allreduce(&ledger, &gLedger, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
350 const auto tot = sd.vofBcVolumesTotal();
351 double gIn = 0.0, gOut = 0.0;
352 double lIn = tot[4], lOut = -tot[5];
353 MPI_Allreduce(&lIn, &gIn, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
354 MPI_Allreduce(&lOut, &gOut, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
355 const std::vector<double> gc =
356 gatherGlobal(sd.getVof(), ox, oy, oz, lnx, lny, lnz, rank, size);
357 if (rank == 0) {
358 IbmSolver ref(NX, NY, NZ);
359 configureSlug(ref, NX, NY, NZ);
360 double rLedger = 0.0;
361 for (int i = 0; i < nSlug + nAfter; ++i) {
362 if (i == nSlug)
363 ref.setVofInflow(4, 0.0);
364 ref.advectVof();
365 const auto v = ref.vofBcVolumes();
366 for (int f = 0; f < 6; ++f)
367 rLedger += v[f];
368 }
369 const double dc = maxAbsDiff(gc, ref.getVof());
370 const long nbad = countNonFinite(gc) + countNonFinite(ref.getVof());
371 const double dl = std::fabs(gLedger - rLedger);
372 if (nbad != 0) {
373 std::printf(" [slug-kin np=%d] FAIL — %ld non-finite colour cells\n", size, nbad);
374 fail = 1;
375 }
376 if (!(gLedger == gLedger) || !(gIn == gIn) || !(gOut == gOut)) {
377 std::printf(" [slug-kin np=%d] FAIL — the boundary ledger is not finite\n", size);
378 fail = 1;
379 }
380 const double budget = std::fabs(gSum - gLedger);
381 std::printf(" [slug-kin np=%d] colour vs single-rank %.3e (BITWISE required)\n", size,
382 dc);
383 std::printf(
384 " [slug-kin np=%d] ledger vs single-rank %.3e; global budget "
385 "|sum(C) - ledger| %.3e; in %.10g out %.10g\n",
386 size, dl, budget, gIn, gOut);
387 if (!(dc == 0.0)) {
388 std::printf(" [slug-kin np=%d] FAIL — colour is not bitwise\n", size);
389 fail = 1;
390 }
391 if (!(budget < 1e-8)) {
392 std::printf(" [slug-kin np=%d] FAIL — the global budget does not close\n", size);
393 fail = 1;
394 }
395 if (!(std::fabs(gIn - gOut) < 1e-9 * gIn) || !(gIn > 0.0)) {
396 std::printf(" [slug-kin np=%d] FAIL — in/out do not balance\n", size);
397 fail = 1;
398 }
399 }
400 }
401
402 // ------------------------------------------------------- B: the coupled jet + the rho ghost
403 {
404 const int steps = 30;
405 IbmSolver sd(lnx, lny, lnz);
406 sd.initMpi(dec, MPI_COMM_WORLD);
407 configureJet(sd, lnx, lny, lnz);
408 long itmax = 0;
409 double divmax = 0.0;
410 for (int i = 0; i < steps; ++i) {
411 sd.step();
412 itmax = std::max<long>(itmax, sd.lastPressureIterations());
413 divmax = std::fmax(divmax, sd.maxOpenDivergence());
414 }
415 // the inflow ghost density: rho_liquid on a rank that OWNS the -z face, untouched elsewhere
416 auto rv = sd.fieldView("rho");
417 auto hr = Kokkos::create_mirror_view(rv);
418 Kokkos::deep_copy(hr, rv);
419 const int G = 2, ex = lnx + 2 * G, ey = lny + 2 * G;
420 const double ghost = hr((std::size_t)(G + lnx / 2) + (std::size_t)(G + lny / 2) * ex +
421 (std::size_t)(G - 1) * ex * ey);
422 const bool owns = (oz == 0);
423 int bad = (owns && std::fabs(ghost - 100.0) > 1e-9) ? 1 : 0;
424 int badAll = 0;
425 MPI_Allreduce(&bad, &badAll, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
426 const std::vector<double> gc =
427 gatherGlobal(sd.getVof(), ox, oy, oz, lnx, lny, lnz, rank, size);
428 long gItmax = 0;
429 MPI_Allreduce(&itmax, &gItmax, 1, MPI_LONG, MPI_MAX, MPI_COMM_WORLD);
430 if (rank == 0) {
431 IbmSolver ref(NX, NY, NZ);
432 configureJet(ref, NX, NY, NZ);
433 for (int i = 0; i < steps; ++i)
434 ref.step();
435 const double dc = maxAbsDiff(gc, ref.getVof());
436 const double dsum = signedSumDiff(gc, ref.getVof());
437 const double csum = sumOf(ref.getVof());
438 std::printf(
439 " [jet-coupl np=%d] colour vs single-rank %.3e (signed sum %.3e); inflow rho ghost "
440 "owners wrong on %d rank(s); pressure %ld/400, max|div| %.3e\n",
441 size, dc, dsum, badAll, gItmax, divmax);
442 // The SAME pair of gates as case C, for the same reason and with the same derivation — see
443 // the long comment there. This case happens to sit at 1.3e-15 today, but it is a coupled
444 // run through an inflow face whose colour front is axis-aligned, i.e. exactly the
445 // configuration in which `mycNormal`'s estimator selection ties; nothing about it earns a
446 // tighter pointwise gate than case C, and a tighter one here would only mean this file
447 // fails on a different machine instead of the same one.
448 const double tol = (size == 1) ? 0.0 : 1e-6;
449 if (!(dc <= tol)) {
450 std::printf(" [jet-coupl np=%d] FAIL — the colour field is wrong (tol %.1e)\n", size,
451 tol);
452 fail = 1;
453 }
454 const double tolSum = (size == 1) ? 0.0 : 1e-11 * std::fabs(csum);
455 if (!(std::fabs(dsum) <= tolSum)) {
456 std::printf(
457 " [jet-coupl np=%d] FAIL — the colour difference does not CANCEL: signed sum %.3e "
458 "> %.3e\n",
459 size, dsum, tolSum);
460 fail = 1;
461 }
462 if (badAll != 0) {
463 std::printf(" [jet-coupl np=%d] FAIL — the inflow rho ghost is not the inlet fluid's\n",
464 size);
465 fail = 1;
466 }
467 if (gItmax >= 400) {
468 std::printf(" [jet-coupl np=%d] FAIL — the pressure solve CAPPED (run invalid)\n", size);
469 fail = 1;
470 }
471 }
472 }
473 // ------------------------- C0: V5a x V-BC composed, KINEMATIC (the bitwise gate on this path)
474 //
475 // The SAME composed scene as case C — the same packing SDF, the same inflow/outflow faces, the
476 // same initial colour — driven by a PRESCRIBED velocity instead of the coupled step. Nothing
477 // in the update then goes through a reduction, so the colour must be BITWISE identical to the
478 // single-rank reference at every np, exactly as `slug-kin` is.
479 //
480 // WHY THIS CASE EXISTS. Case C below cannot be gated bitwise, and (WO 2026-09-14) it cannot be
481 // gated at a reduction floor either — see the long comment there. That left the composed
482 // cut-cell x open-boundary path with no exact gate at all, which is the coverage this restores:
483 // the out-of-domain mask on GLOBAL indices, the owner-only boundary colour, the openness-
484 // weighted domain-face flux and the per-rank ledger are all exercised here, against an
485 // absolute standard, with a velocity that VARIES in all three directions so a mis-filled ghost
486 // cannot hide behind a uniform field.
487 //
488 // What it does NOT cover, and case C still must: `setField` clears `outflowCorrValid_`, so the
489 // bridge takes its `doOutflow = true` branch here. The outflow-face correction the projection
490 // makes lives only on the coupled path.
491 {
492 const int steps = 20;
493 IbmSolver sd(lnx, lny, lnz);
494 sd.initMpi(dec, MPI_COMM_WORLD);
495 configurePacking(sd, ox, oy, oz, lnx, lny, lnz);
496 packingVelocity(sd, ox, oy, oz, lnx, lny, lnz);
497 double ledger = 0.0, solidSum = 0.0;
498 const double vol0loc = sd.vofDiagnostics().volume;
499 for (int i = 0; i < steps; ++i) {
500 sd.advectVof();
501 const auto v = sd.vofBcVolumes();
502 for (int f = 0; f < 6; ++f)
503 ledger += v[f];
504 solidSum = std::fmax(solidSum, std::fabs(sd.vofDiagnostics().solidSumC));
505 }
506 const double vol1loc = sd.vofDiagnostics().volume;
507 double g0 = 0, g1 = 0, gLed = 0, gSolid = 0;
508 MPI_Allreduce(&vol0loc, &g0, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
509 MPI_Allreduce(&vol1loc, &g1, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
510 MPI_Allreduce(&ledger, &gLed, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
511 MPI_Allreduce(&solidSum, &gSolid, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD);
512 const std::vector<double> gc =
513 gatherGlobal(sd.getVof(), ox, oy, oz, lnx, lny, lnz, rank, size);
514 if (rank == 0) {
515 IbmSolver ref(NX, NY, NZ);
516 configurePacking(ref, 0, 0, 0, NX, NY, NZ);
517 packingVelocity(ref, 0, 0, 0, NX, NY, NZ);
518 for (int i = 0; i < steps; ++i)
519 ref.advectVof();
520 const double dc = maxAbsDiff(gc, ref.getVof());
521 const double budget = std::fabs((g1 - g0) - gLed);
522 const long nbad = countNonFinite(gc) + countNonFinite(ref.getVof());
523 // The budget is REPORTED, not gated, and this is the one thing the case gives up: the
524 // Weymouth-Yue conservation identity rests on the advecting field being discretely
525 // divergence-free in the OPENNESS-WEIGHTED sense, and a prescribed analytic field is not
526 // (2.6e-03 relative over these 20 steps). The identity is gated where its hypothesis holds
527 // — on `slug-kin`, whose uniform w is exactly solenoidal, and on case C below, whose field
528 // comes out of the projection. What this case gates is the one thing those two cannot:
529 // that the composed cut-cell x open-boundary colour transport is decomposition-EXACT.
530 std::printf(
531 " [packing-kin np=%d] colour vs single-rank %.3e (BITWISE required); budget "
532 "|d sum(eps_eff C) - ledger| %.3e (rel %.3e, NOT gated — the prescribed field is not "
533 "discretely solenoidal); solid colour %.3e\n",
534 size, dc, budget, budget / g0, gSolid);
535 if (!(dc == 0.0)) {
536 std::printf(" [packing-kin np=%d] FAIL — the composed colour is not bitwise\n", size);
537 fail = 1;
538 }
539 if (nbad != 0) {
540 std::printf(" [packing-kin np=%d] FAIL — %ld non-finite colour cells\n", size, nbad);
541 fail = 1;
542 }
543 if (!(gSolid == 0.0)) {
544 std::printf(" [packing-kin np=%d] FAIL — colour leaked into solid cells\n", size);
545 fail = 1;
546 }
547 }
548 }
549
550 // ------------------------------------------- C: V5a x V-BC composed (WO-R2 item 2), coupled
551 //
552 // WHY THIS ONE IS AT THE REDUCTION FLOOR AND NOT BITWISE. The kinematic pattern the two rungs
553 // use separately (`vof_cutcell_mpi`: run the reference on the full grid, slice its velocity
554 // into every decomposition with `setField`) cannot be used through an OPEN boundary: the
555 // outflow-face correction lives on a ghost face index that `setField` does not carry, and the
556 // ghost fill `setField` triggers is exactly the one that erases it (`outflowCorrValid_`,
557 // WO-R). So the composed scene is driven by `step()` on both sides and gated at the
558 // allreduce-order floor, like the coupled jet above. The BUDGET identity is gated absolutely.
559 {
560 const int steps = 40;
561 IbmSolver sd(lnx, lny, lnz);
562 sd.initMpi(dec, MPI_COMM_WORLD);
563 configurePacking(sd, ox, oy, oz, lnx, lny, lnz);
564 long itmax = 0;
565 double ledger = 0.0, solidSum = 0.0;
566 const double vol0loc = sd.vofDiagnostics().volume;
567 for (int i = 0; i < steps; ++i) {
568 sd.step();
569 itmax = std::max<long>(itmax, sd.lastPressureIterations());
570 const auto v = sd.vofBcVolumes();
571 for (int f = 0; f < 6; ++f)
572 ledger += v[f];
573 solidSum = std::fmax(solidSum, std::fabs(sd.vofDiagnostics().solidSumC));
574 }
575 const double vol1loc = sd.vofDiagnostics().volume;
576 double g0 = 0, g1 = 0, gLed = 0, gSolid = 0;
577 long gIt = 0;
578 MPI_Allreduce(&vol0loc, &g0, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
579 MPI_Allreduce(&vol1loc, &g1, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
580 MPI_Allreduce(&ledger, &gLed, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
581 MPI_Allreduce(&solidSum, &gSolid, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD);
582 MPI_Allreduce(&itmax, &gIt, 1, MPI_LONG, MPI_MAX, MPI_COMM_WORLD);
583 const std::vector<double> gc =
584 gatherGlobal(sd.getVof(), ox, oy, oz, lnx, lny, lnz, rank, size);
585 if (rank == 0) {
586 IbmSolver ref(NX, NY, NZ);
587 configurePacking(ref, 0, 0, 0, NX, NY, NZ);
588 for (int i = 0; i < steps; ++i)
589 ref.step();
590 const double dc = maxAbsDiff(gc, ref.getVof());
591 const double budget = std::fabs((g1 - g0) - gLed);
592 const double dsum = signedSumDiff(gc, ref.getVof());
593 const double csum = sumOf(ref.getVof());
594 std::printf(
595 " [packing np=%d] colour vs single-rank %.3e (signed sum %.3e, rel %.3e); budget "
596 "|d sum(eps_eff C) - ledger| %.3e (rel %.3e); solid colour %.3e; "
597 "pressure %ld/400\n",
598 size, dc, dsum, dsum / csum, budget, budget / g0, gSolid, gIt);
599 // THE COLOUR IS NOT A LIPSCHITZ FUNCTION OF THE VELOCITY, so at np > 1 there is no
600 // reduction floor to gate the POINTWISE field against. Measured, on this configuration
601 // (WO docs/wo_vof_mpi_parity_gates.md, 2026-09-14):
602 //
603 // * the composed VoF path itself is decomposition-EXACT — `packing-kin` above drives
604 // this very scene with a prescribed 3-D velocity and is bitwise at np = 2 and 4 and at
605 // 1..16 OpenMP threads;
606 // * under `step()` every field the solver carries agrees at the allreduce floor: u, v, w
607 // and their ghosts to 1.4e-14, p to 1.4e-13 on |p| = 1.9e2;
608 // * and yet the colour parts company by 4.795e-09 in exactly TWO adjacent cells, equal
609 // and opposite, i.e. one x-face flux.
610 //
611 // The mechanism is `mycNormal`'s estimator selection (core/vof/plic.hpp), which is a STRICT
612 // comparison — `if (fabs(mm[cn][cn]) > t0) cn = 3;` — between the centred and the Youngs
613 // candidate. At a near-axis-aligned interface the two candidates carry the same dominant
614 // component, so that comparison is an exact tie: traced here, the centred candidate scored
615 // 0.99986893026188128 against Youngs' 0.99986893026188106 at np = 2 (ONE ULP apart, Youngs
616 // loses) and 0.99986893026188106 against 0.99986893026188106 single-rank (equal, so `>` is
617 // false and the centred one wins). The two winners are the same plane to 1e-6 in their
618 // dominant component and differ by 2.9e-06 in the transverse ones; re-tilting the plane by
619 // that much moves the liquid volume in the |a| = 3.4e-03 slab of that face by ~a^2 s
620 // = 1.5e-09, and the sweep by 4.795e-09. ONE ULP of colour in, 4.8e-09 out — and which way
621 // the tie breaks is decided by bits that a reduction order is entitled to change.
622 //
623 // The 1e-11 this gate used to ask for was therefore never a property of the code: it was a
624 // coin toss. It came up heads in CI and on 1 thread, and tails everywhere else — measured
625 // over 24 (ranks, threads) pairs, np = 1 is bitwise at 1..16 threads, np = 2 flips at 2..16
626 // threads and not at 1, and np = 4 flips at 2, 3, 4, 6 and 12 threads but NOT at 1, 8 and
627 // 16, which is the only reason `vof_bc_mpi_np4` passes on the 48-core box at all.
628 //
629 // So the pointwise gate is no longer a reduction-floor gate and does not pretend to be. The
630 // reduction-floor content moves to the SIGNED SUM below, which is Lipschitz and is what a
631 // real defect breaks; the pointwise number becomes a bound on the field going WRONG. It is
632 // set at 1e-6: three orders above the largest flip this scene can produce (a tie needs the
633 // two candidates to agree in the dominant component, which bounds their transverse mass
634 // s = 1 - max|m| and hence the flux jump at ~cfl^2 s), and far below what a real defect
635 // does here. That second half is MEASURED, not asserted — the WO-F owner rule was deleted
636 // (`vofApplyColourBc`'s `touchesGlobalFace` test, so every rank injects the inflow colour
637 // into its own block's -z ghost band) and re-run at np = 2: this gate reports 1.000e+00
638 // against its 1e-06, the signed sum 5.141e+02 against its 3.886e-08, and `packing-kin`
639 // 1.000e+00 against bitwise. Nine orders of margin, not one.
640 //
641 // The OTHER defect named in this file's header — the out-of-domain mask built on per-block
642 // instead of global indices — was injected too, and is worth recording precisely because
643 // this case does NOT see it: `packing` stayed at 3.174e-09 and `packing-kin` at exactly
644 // zero, while `slug-kin`'s bitwise gate caught it at 2.769e-37. It is caught by the
645 // bitwise cases, which is where that coverage lives; the 1e-11 this case used to carry
646 // never had it either.
647 const double tol = (size == 1) ? 0.0 : 1e-6;
648 if (!(dc <= tol)) {
649 std::printf(" [packing np=%d] FAIL — the colour field is wrong (tol %.1e)\n", size,
650 tol);
651 fail = 1;
652 }
653 // The reduction floor, on the quantity that HAS one. An 8192-term naive sum of O(1) values
654 // drifts by O(N eps) ~ 2e-12 relative; 1e-11 is five times that, and a flux that a
655 // decomposition lost or double-counted does not cancel at all.
656 const double tolSum = (size == 1) ? 0.0 : 1e-11 * std::fabs(csum);
657 if (!(std::fabs(dsum) <= tolSum)) {
658 std::printf(
659 " [packing np=%d] FAIL — the colour difference does not CANCEL: signed sum %.3e "
660 "> %.3e (colour created or destroyed, not moved)\n",
661 size, dsum, tolSum);
662 fail = 1;
663 }
664 if (!(budget / g0 < 1e-10)) {
665 std::printf(" [packing np=%d] FAIL — the composed budget does not close\n", size);
666 fail = 1;
667 }
668 if (!(gSolid == 0.0)) {
669 std::printf(" [packing np=%d] FAIL — colour leaked into solid cells\n", size);
670 fail = 1;
671 }
672 if (gIt >= 400) {
673 std::printf(" [packing np=%d] FAIL — the pressure solve CAPPED (run invalid)\n", size);
674 fail = 1;
675 }
676 }
677 }
678
679 // ------------------------------------ D: the COLLOCATED bridge through an open boundary
680 //
681 // The staggered composed case above gates this identity on `IbmSolver`; nothing gated it on
682 // `SolverColocated`, whose VoF bridge takes a DIFFERENT path -- it hands the advector the FACE
683 // field `uf_/vf_/wf_` and re-fills its ghosts first. `fillGhosts` is BC-unaware and wraps every
684 // axis, and the outflow face is a GHOST index, so that fill overwrites the mass-conserving
685 // value `bcCorrectOutflow` had just written with the opposite boundary's -- single-rank as
686 // well as distributed, since the single-rank fill wraps periodically too.
687 //
688 // The conserved functional is summed from the CANONICAL colour field, not from the advector
689 // census: `volume` (sum eps_eff*C) is populated only with cut-cell geometry attached, and
690 // `diagnostics()` returns what the last `advect()` stored, so neither is meaningful BEFORE the
691 // first step. This duct is all-fluid, so sum C over the inner block is the right quantity.
692 {
693 const int steps = 30;
694 Colo sd(lnx, lny, lnz);
695 sd.initMpi(dec, MPI_COMM_WORLD);
696 configureColoJet(sd, oz, lnx, lny, lnz);
697 double colLedger = 0.0;
698 const double colC0 = sumOf(sd.getVof());
699 long colIt = 0;
700 for (int i = 0; i < steps; ++i) {
701 sd.step();
702 colIt = std::max<long>(colIt, sd.lastPressureIterations());
703 const auto v = sd.vofBcVolumes();
704 for (int f = 0; f < 6; ++f)
705 colLedger += v[f];
706 }
707 const double colC1 = sumOf(sd.getVof());
708 double gC0 = 0, gC1 = 0, gColLed = 0;
709 long gColIt = 0;
710 MPI_Allreduce(&colC0, &gC0, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
711 MPI_Allreduce(&colC1, &gC1, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
712 MPI_Allreduce(&colLedger, &gColLed, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
713 MPI_Allreduce(&colIt, &gColIt, 1, MPI_LONG, MPI_MAX, MPI_COMM_WORLD);
714 const std::vector<double> gcol =
715 gatherGlobal(sd.getVof(), ox, oy, oz, lnx, lny, lnz, rank, size);
716 if (rank == 0) {
717 Colo ref(NX, NY, NZ);
718 configureColoJet(ref, 0, NX, NY, NZ);
719 for (int i = 0; i < steps; ++i)
720 ref.step();
721 const double dcol = maxAbsDiff(gcol, ref.getVof());
722 const double budget = std::fabs((gC1 - gC0) - gColLed);
723 const double csum = sumOf(ref.getVof());
724 std::printf(
725 " [colo-jet np=%d] colour vs single-rank %.3e; budget |d sum(C) - ledger| %.3e "
726 "(rel %.3e); pressure %ld/400\n",
727 size, dcol, budget, budget / gC0, gColIt);
728 const double tolCol = (size == 1) ? 0.0 : 1e-9 * std::fabs(csum);
729 if (!(dcol <= tolCol)) {
730 std::printf(" [colo-jet np=%d] FAIL — the colour field is wrong (tol %.1e)\n", size,
731 tolCol);
732 fail = 1;
733 }
734 if (!(budget / gC0 < 1e-10)) {
735 std::printf(" [colo-jet np=%d] FAIL — the boundary budget does not close\n", size);
736 fail = 1;
737 }
738 if (gColIt >= 400) {
739 std::printf(" [colo-jet np=%d] FAIL — the pressure solve CAPPED (run invalid)\n", size);
740 fail = 1;
741 }
742 }
743 }
744 int gfail = 0;
745 MPI_Allreduce(&fail, &gfail, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD);
746 fail = gfail;
747 if (rank == 0)
748 std::printf(fail ? "FAILED\n" : "OK\n");
749 }
750 Kokkos::finalize();
751 MPI_Finalize();
752 return fail;
753}
std::vector< double > vofBcVolumes() const
Signed liquid volume that crossed each of the six domain faces during the LAST colour advection,...
std::vector< double > getVof()
std::vector< double > vofBcVolumesTotal() const
The same, accumulated since enable_vof() (or the last resetVofBcVolumes()).
void setVofInflow(int f, double value)
Colour of the fluid entering through inflow face f (0..5 = -x,+x,-y,+y,-z,+z), in [0,...
CCField fieldView(const std::string &name)
vof::WyAdvector::Diagnostics vofDiagnostics()
flow — host-facing Kokkos IBM Navier-Stokes solver (drop-in flow-style API).
Solver< Staggered > IbmSolver
static constexpr int NY
static constexpr int NX
static constexpr std::size_t GCELLS
static constexpr int NZ
double solidSumC
filled by the SOLVER from the canonical G=2 colour field
double volume
sum of eps_eff*C over inner FLUID cells: the EXACTLY conserved functional of the cut-cell scheme (cut...
static constexpr double DT
static constexpr int G
peclet::flow::Solver< peclet::flow::Colocated > Colo
static constexpr double WIN
static std::vector< double > gatherGlobal(const std::vector< double > &local, int ox, int oy, int oz, int lnx, int lny, int lnz, int rank, int size)
static void configureSlug(IbmSolver &s, int lnx, int lny, int lnz)
static void configureJet(IbmSolver &s, int lnx, int lny, int lnz)
static double signedSumDiff(const std::vector< double > &a, const std::vector< double > &b)
static long countNonFinite(const std::vector< double > &a)
static double maxAbsDiff(const std::vector< double > &a, const std::vector< double > &b)
static void packingVelocity(IbmSolver &s, int ox, int oy, int oz, int lnx, int lny, int lnz)
static double sumOf(const std::vector< double > &a)
static std::vector< double > packingSdf(int ox, int oy, int oz, int lnx, int lny, int lnz)
static void configurePacking(IbmSolver &s, int ox, int oy, int oz, int lnx, int lny, int lnz)
static void configureColoJet(Colo &s, int oz, int lnx, int lny, int lnz)
static std::vector< double > ledger(IbmSolver &s)