flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_vof_collocated.cpp
Go to the documentation of this file.
1// VoF rung V8 — the COLLOCATED path: variable density and surface tension in the ABC approximate
2// projection through the mass-adjoint pair (doc/collocated_varrho_forces.md), colour advection from
3// the projected face field.
4//
5// The collocated solver's pressure coupling is the approximate projection: map the cell velocities
6// onto a MAC face field, project THAT exactly, correct the cell field. On this path
7//
8// * the TRANSPORT half: `uf_/vf_/wf_` is exactly discretely divergence-free, which is precisely
9// what Weymouth-Yue's conservation proof needs;
10// * the FORCE half: the pressure, the CSF and `set_body_force` (a mean pressure gradient) enter
11// the IMPLICIT momentum predictor as the finite-volume face integral
12// rho_c * avg_faces(o (f - (P(i)-P(i-s)))/rho_f); a per-cell force enters at the cell value
13// times the reconstruction's weight sum; the constraint reads the MOMENTUM-weighted face
14// velocity (rho_L u_L + rho_R u_R)/(rho_L + rho_R). Then M Gamma = -C^T: stable at every dt and
15// ratio, dt-independent steady state (tests/python/test_collocated_stability_guard.py).
16// The retired WO-T form -- every force a face acceleration added AFTER the viscous solve
17// (Basilisk centered.h) -- was balanced per step but non-incremental and unstable above
18// mu dt/(rho h^2) = 1/12; do not reintroduce it.
19//
20// Balance at rest. With the balanced-force projection (doc §4.6; the DEFAULT on this path) a static
21// balance -- hydrostatic column, constant-kappa drop -- is exact from the first step. With it
22// explicitly OFF the balance is still an exact FIXED POINT, but the pressure absorbs a gradient
23// force only through the step, and the transient from the initial P decays. Both are gated: the
24// T1/T1b/T2 blocks below run explicitly OFF; "ON" re-runs them at the default.
25//
26// Gates, in the order they run:
27//
28// T1 HYDROSTATIC, through a hand-set rho and through C, at ratio 1000, in a triply periodic box
29// (the uniform offset <rho> g as set_body_force) and in a walled column. Staggered and
30// collocated side by side. Collocated: the FACE field at round-off; the cell field and
31// dP/dz are the decaying transient -- reported, with decay required (400 steps < 100 steps).
32//
33// T1b THE INVISIBLE SUBSPACE, measured. A cell-field checkerboard is annihilated by the uniform
34// centre-to-face average (`½(U(i)+U(i-1))` kills the odd-even mode), so the approximate
35// projection cannot see it and cannot remove it -- a PRE-EXISTING property of the collocated
36// grid, measured on the constant-density control beside the V8 column. The transient of the
37// column lives largely in that mode, which is why it decays slowly.
38//
39// T2 STATIONARY DROPLET with a CONSTANT curvature: the CSF is exactly a face gradient, so the
40// balance is a fixed point; ratio 1..1000 and the ratio-1000 mu sweep must not throw, and the
41// face field must decay (90 steps < 30 steps).
42//
43// T3 CONSTANT-DENSITY EQUIVALENCE. A uniform-rho V8 run reproduces the constant-density
44// collocated run to round-off: the face integral of a uniform rho IS the central difference.
45//
46// T4 THE BRIDGE (G6). The colour transport on the collocated grid is the SAME kernel on the SAME
47// faces as on the staggered grid: `uf_(i)` sits at i-1/2, exactly where the staggered `u(i)`
48// sits, so a staggered solver handed the collocated projected face field reproduces the
49// collocated colour BITWISE.
50//
51// T5 SCOPE. Variable density / VoF on the collocated grid with an immersed solid, with
52// `enable_vof_momentum`, with `set_rho_face_harmonic` or with the non-incremental pressure,
53// must throw.
54#include <algorithm>
55#include <cmath>
56#include <cstdio>
57#include <Kokkos_Core.hpp>
58#include <memory>
59#include <stdexcept>
60#include <string>
61#include <vector>
62
63#include "flow_ibm.hpp"
64
65namespace {
72
73int failures = 0;
74#define CHECK(cond) \
75 do { \
76 if (!(cond)) { \
77 std::fprintf(stderr, "CHECK failed: %s\n at %s:%d\n", #cond, __FILE__, __LINE__); \
78 ++failures; \
79 } \
80 } while (0)
81
82std::size_t idx(int x, int y, int z, int nx, int ny) {
83 return (std::size_t)x + (std::size_t)y * nx + (std::size_t)z * (std::size_t)nx * ny;
84}
85double maxAbs(const std::vector<double>& v) {
86 double m = 0;
87 for (double x : v)
88 m = std::fmax(m, std::fabs(x));
89 return m;
90}
91double maxAbsDiff(const std::vector<double>& a, const std::vector<double>& b) {
92 // WO-R2: NaN-PROPAGATING. `std::fmax(m, NaN) == m`, so the obvious loop returns 0.000e+00 for a
93 // field that has gone entirely NaN and every bitwise gate built on it passes (WO-R found this on
94 // a drained open-boundary run). A non-finite difference must fail, so return it.
95 double m = 0;
96 for (std::size_t i = 0; i < a.size(); ++i) {
97 const double d = std::fabs(a[i] - b[i]);
98 if (!(d == d))
99 return d; // NaN
100 m = std::fmax(m, d);
101 }
102 return m;
103}
104template <class S>
105double maxVel(S& s) {
106 return std::fmax(std::fmax(maxAbs(s.getVelocity(0)), maxAbs(s.getVelocity(1))),
107 maxAbs(s.getVelocity(2)));
108}
109template <class S>
110double maxFaceVel(S& s) {
111 return std::fmax(std::fmax(maxAbs(s.getFaceVelocity(0)), maxAbs(s.getFaceVelocity(1))),
112 maxAbs(s.getFaceVelocity(2)));
113}
114// Odd-even (checkerboard) amplitude of a z-column of a cell field: max |f(k) - ½(f(k-1)+f(k+1))|,
115// which reads 2A for a pure checkerboard of amplitude A and 0 for anything smooth-and-linear.
116double checkerboardZ(const std::vector<double>& f, int nx, int ny, int nz) {
117 double m = 0;
118 const int xc = nx / 2, yc = ny / 2;
119 for (int z = 1; z + 1 < nz; ++z)
120 m = std::fmax(m, std::fabs(f[idx(xc, yc, z, nx, ny)] - 0.5 * (f[idx(xc, yc, z - 1, nx, ny)] +
121 f[idx(xc, yc, z + 1, nx, ny)])));
122 return m;
123}
124
125// ---------------------------------------------------------------- T1: hydrostatic
126//
127// A two-layer stratified column at rest. `periodic` runs it in a triply periodic box with the
128// ZERO-MEAN force f = -(rho - <rho>) g (the only hydrostatic problem a periodic box admits; the
129// reported dP/dz is then -(rho_f - <rho>) g). `walled` reproduces the staggered acid test of
130// `test_vardensity_projection.cpp` verbatim (walls +-z), which is the direct staggered column.
131struct HydroResult {
132 double cellU = 0, faceU = 0, pErr = 0, cb = 0;
133 long iters = 0;
134};
135// The Chebyshev cap of a solve at an explicit rtol (hydrostatic's chebRtol > 0). A gate at rtol
136// 1e-14 also checks iters < kChebMaxit: a capped solve's face error is not the scheme's.
137constexpr int kChebMaxit = 4000;
138
139template <class S>
140// `bfp`: -1 = the solver's default (balanced-force projection ON on the collocated V8 path, OFF on
141// the staggered grid), 0 = explicitly OFF, 1 = explicitly ON.
142HydroResult hydrostatic(double ratio, double mu, int steps, bool periodic, bool throughColour,
143 int bfp = -1, double chebRtol = -1.0) {
144 const int N = 8, NZ = 24;
145 const double g = 0.1;
146 S s(N, N, NZ);
147 s.setAdvection(false);
148 s.setRho(1.0);
149 s.setMu(mu);
150 s.setDt(1.0);
151 s.setVelocityResidualTolerance(0.0); // machine-precision gates: legacy fixed-sweep momentum loop
152 if (!periodic) {
153 s.setDomainBc(4, 1, 0, 0, 0);
154 s.setDomainBc(5, 1, 0, 0, 0);
155 }
156 s.setPressureGeometry(std::vector<double>((std::size_t)N * N * NZ, 10.0));
157 // Heavy BELOW in the walled column (the stable stratification of the staggered acid test); a
158 // heavy slab in the middle quarter in the periodic box, where the density is a FROZEN hand-set
159 // field so no Rayleigh-Taylor mode exists (a periodic two-layer column always has one unstably
160 // stratified interface — with a frozen rho it is simply not a degree of freedom).
161 auto heavy = [&](int z) { return periodic ? (z >= NZ / 4 && z < 3 * NZ / 4) : (z < NZ / 2); };
162 std::vector<double> fld((std::size_t)N * N * NZ);
163 for (int z = 0; z < NZ; ++z)
164 for (int y = 0; y < N; ++y)
165 for (int x = 0; x < N; ++x)
166 fld[idx(x, y, z, N, N)] =
167 heavy(z) ? (throughColour ? 1.0 : ratio) : (throughColour ? 0.0 : 1.0);
168 double rbar = 0;
169 for (int z = 0; z < NZ; ++z)
170 rbar += (heavy(z) ? ratio : 1.0) / NZ;
171 if (throughColour) {
172 // FROZEN interface (the `freeze` route of test_vof_twophase.cpp gate B2): register "C" as a
173 // plain field and let the closure read it, without enable_vof, so C supplies rho and nothing
174 // else. With the interface free the rest state is not a fixed point of the discrete system at
175 // all (an interfacial gravity-wave mode with loop gain g*drho*dt/rho_g), which is a property of
176 // the physics and not of this rung.
177 s.addField("C");
178 s.setField("C", fld);
179 s.exchangeField("C");
180 s.setPropertyModel("rho", ClosureKind::LinearMix, "C", "", {1.0, ratio - 1.0});
181 } else {
182 s.addField("rho");
183 s.setField("rho", fld);
184 s.setDensityMode(true);
185 }
186 // gravity f_z = -rho g as a per-cell field (volumetric: the cell value on the collocated grid);
187 // in the periodic box the offset +<rho> g that makes the pressure periodic is a UNIFORM drive,
188 // i.e. a mean pressure gradient, so it is set_body_force (a surface force on the collocated
189 // variable-density path, doc/collocated_varrho_forces.md U1 / §5) and not part of the field.
190 s.setPropertyModel("force_z", ClosureKind::LinearMix, "rho", "", std::vector<double>{0.0, -g});
191 if (periodic)
192 s.setBodyForce(0.0, 0.0, rbar * g);
193 if (bfp >= 0)
194 s.setBalancedForceProjection(bfp == 1);
195 if (chebRtol > 0.0) // the driver last (set_density_mode re-selects Chebyshev)
196 s.setPressureChebyshev(true, kChebMaxit, chebRtol);
197 for (int k = 0; k < steps; ++k)
198 s.step();
199 HydroResult r;
200 r.cellU = maxVel(s);
201 r.faceU = maxFaceVel(s);
202 r.iters = s.lastPressureIterations();
203 r.cb = checkerboardZ(s.getVelocity(2), N, N, NZ);
204 const auto p = s.getPressure();
205 const double off = periodic ? rbar * g : 0.0;
206 for (int z = 1; z < NZ; ++z) {
207 const double dp = p[idx(N / 2, N / 2, z, N, N)] - p[idx(N / 2, N / 2, z - 1, N, N)];
208 const double rf = 0.5 * ((heavy(z) ? ratio : 1.0) + (heavy(z - 1) ? ratio : 1.0));
209 r.pErr = std::fmax(r.pErr, std::fabs(dp + g * rf - off) / (g * ratio));
210 }
211 return r;
212}
213
214void gateHydrostatic() {
215 std::printf(
216 "\n=== T1 hydrostatic at ratio 1000, mu = 0: dP/dz == -rho_f g. Staggered: exact. "
217 "Collocated:\n"
218 " the FACE field at round-off; the balance is an exact fixed point, so the cell "
219 "field\n"
220 " and dP/dz are the transient from P = 0 -- reported, and required to DECAY.\n");
221 struct Case {
222 const char* name;
223 bool periodic, colour;
224 };
225 const Case cases[3] = {{"periodic, hand-set rho, frozen", true, false},
226 {"walled, hand-set rho ", false, false},
227 {"walled, through C (frozen) ", false, true}};
228 for (const Case& c : cases) {
229 const auto st = hydrostatic<Stag>(1000.0, 0.0, 100, c.periodic, c.colour);
230 // Walled: solved at Chebyshev rtol 1e-14 (doc §9 G4 protocol, as gateBalancedOn): at the
231 // default 1e-9 the face error is a stopping draw of the symmetric V-cycle
232 // (doc/mg_depth_policy.md A3), 1.99e-12 in 10 iterations (trilinear 2.25e-13 in 13).
233 const double rt = c.periodic ? -1.0 : 1e-14;
234 const auto co = hydrostatic<Colo>(1000.0, 0.0, 100, c.periodic, c.colour, 0, rt);
235 const auto co4 = hydrostatic<Colo>(1000.0, 0.0, 400, c.periodic, c.colour, 0, rt);
236 std::printf(" %s staggered cell %.3e face %.3e dP/dz %.3e cb %.3e it %ld\n", c.name,
237 st.cellU, st.faceU, st.pErr, st.cb, st.iters);
238 std::printf(
239 " %s COLLOCATED cell %.3e face %.3e dP/dz %.3e cb %.3e it %ld (400 steps: "
240 "cell %.3e dP/dz %.3e)\n",
241 c.name, co.cellU, co.faceU, co.pErr, co.cb, co.iters, co4.cellU, co4.pErr);
242 CHECK(st.faceU < 1e-12);
243 CHECK(st.pErr < 1e-11);
244 // Collocated: the FACE field is the projected field of a velocity that is itself the decaying
245 // transient, so it sits at the pressure solve's round-off of that transient. Walled: the
246 // note's 1e-12 holds (8.2e-14 measured at rtol 1e-14, double operator storage). Periodic:
247 // FROZEN at WO-V1 (2026-09-25) at 10x the measured 1.79e-10 (doc §9 G4 measure-then-freeze; the
248 // note's 1e-10 missed by 1.8x; the periodic box carries a 2.3e-1 cell checkerboard transient at
249 // mu = 0).
250 CHECK(co.faceU < (c.periodic ? 1.8e-9 : 1e-12));
251 CHECK(co4.cellU < co.cellU); // the transient decays ...
252 CHECK(co4.pErr < co.pErr); // ... in the velocity and in the pressure gradient
253 CHECK(co.iters < 200); // rule 3b: no capped solve
254 if (rt > 0.0)
255 CHECK(co4.iters < kChebMaxit);
256 }
257 std::printf(
258 "\n The mu sweep (walled, hand-set rho, ratio 1000, 100 steps). The staggered balance is\n"
259 " only approached at mu > 0 (A = rho_f/dt - mu*Lap does not commute with the discrete\n"
260 " gradient at variable rho: WO-P's mu*dt^2 residue).\n");
261 for (double mu : {0.0, 1e-3, 1e-2, 1e-1}) {
262 const auto st = hydrostatic<Stag>(1000.0, mu, 100, false, false);
263 const auto co = hydrostatic<Colo>(1000.0, mu, 100, false, false, 0, 1e-14); // as T1 walled
264 std::printf(
265 " mu = %-6g staggered face %.3e dP/dz %.3e | COLLOCATED face %.3e "
266 "dP/dz %.3e\n",
267 mu, st.faceU, st.pErr, co.faceU, co.pErr);
268 CHECK(co.faceU < 1e-12); // measured 2.0e-14 .. 1.1e-13 at rtol 1e-14 (double storage)
269 CHECK(co.iters < kChebMaxit);
270 }
271}
272
273void gateInvisibleSubspace() {
274 std::printf(
275 "\n=== T1b the invisible subspace: a CELL checkerboard is annihilated by centerToFace\n"
276 " (1/2(U(i)+U(i-1)) kills the odd-even mode), so the approximate projection cannot\n"
277 " see it and cannot remove it. CONTROL = the VALIDATED constant-density collocated\n"
278 " path with a plain body force, where rung V8 is completely inert. The V8 column's\n"
279 " transient lives largely in that mode, and must decay.\n");
280 const int N = 8, NZ = 24;
281 const double g = 0.1;
282 for (double mu : {0.0, 0.01}) {
283 Colo s(N, N, NZ);
284 s.setAdvection(false);
285 s.setRho(1.0);
286 s.setMu(mu);
287 s.setDt(1.0);
288 s.setVelocityResidualTolerance(0.0);
289 s.setDomainBc(4, 1, 0, 0, 0);
290 s.setDomainBc(5, 1, 0, 0, 0);
291 s.setPressureGeometry(std::vector<double>((std::size_t)N * N * NZ, 10.0));
292 s.setBodyForce(0.0, 0.0, -g);
293 for (int k = 0; k < 100; ++k)
294 s.step();
295 std::printf(
296 " CONTROL constant-rho collocated, body force, mu = %-5g cell |u| %.3e face |uf| %.3e"
297 " cb %.3e\n",
298 mu, maxVel(s), maxFaceVel(s), checkerboardZ(s.getVelocity(2), N, N, NZ));
299 }
300 const auto a = hydrostatic<Colo>(1000.0, 0.0, 100, false, false, 0);
301 const auto b = hydrostatic<Colo>(1000.0, 0.0, 400, false, false, 0);
302 std::printf(
303 " V8 walled ratio 1000 mu = 0: 100 steps cell %.3e (cb %.3e), 400 steps cell %.3e "
304 "(cb %.3e) -> decayed %.2fx\n",
305 a.cellU, a.cb, b.cellU, b.cb, a.cellU / std::fmax(b.cellU, 1e-300));
306 CHECK(b.cellU < a.cellU); // the mode DECAYS; it is a transient remnant, not an instability
307}
308
309// ---------------------------------------------------------------- T2: the exactness gate
310//
311// Volume fractions of a sphere: exact in z, sub x sub sampling in (x,y) (the sampler of
312// test_vof_surface_tension.cpp, kept identical so the two gates are on the same scene).
313std::vector<double> sphereC(int n, double R, double cx, double cy, double cz, int sub = 24) {
314 std::vector<double> C((std::size_t)n * n * n, 0.0);
315 const double w = 1.0 / sub;
316 for (int k = 0; k < n; ++k)
317 for (int j = 0; j < n; ++j)
318 for (int i = 0; i < n; ++i) {
319 double acc = 0.0;
320 for (int b = 0; b < sub; ++b)
321 for (int a = 0; a < sub; ++a) {
322 const double px = i + (a + 0.5) * w, py = j + (b + 0.5) * w;
323 const double r2 = R * R - (px - cx) * (px - cx) - (py - cy) * (py - cy);
324 if (r2 <= 0.0)
325 continue;
326 const double h = std::sqrt(r2);
327 const double lo = std::fmax(cz - h, (double)k), hi = std::fmin(cz + h, (double)k + 1);
328 if (hi > lo)
329 acc += hi - lo;
330 }
331 C[idx(i, j, k, n, n)] = acc / (sub * sub);
332 }
333 return C;
334}
335
336template <class S>
337std::unique_ptr<S> makeDroplet(int n, double R, double sigma, double mu, double rhoG, double rhoL,
338 double kappa, double dtFac, int bfp = -1) {
339 auto s = std::make_unique<S>(n, n, n);
340 s->setAdvection(false);
341 s->setRho(rhoL);
342 s->setMu(mu);
343 s->setDt(1.0);
344 s->setVelocityResidualTolerance(0.0);
345 s->setPressureGeometry(std::vector<double>((std::size_t)n * n * n, 10.0));
346 s->setPressureChebyshev(true, 500, 1e-14);
347 s->enableVof();
348 s->setVof(sphereC(n, R, n / 2 + 0.13, n / 2 + 0.27, n / 2 + 0.11));
349 s->setPropertyModel("rho", ClosureKind::LinearMix, "C", "", {rhoG, rhoL - rhoG});
350 s->setSurfaceTension(sigma);
351 if (kappa >= 0.0)
352 s->setVofKappaConstant(kappa);
353 s->setDt(dtFac * s->capillaryDt());
354 if (bfp >= 0)
355 s->setBalancedForceProjection(bfp == 1);
356 return s;
357}
358
359struct DropResult {
360 double cellU = 0, faceU = 0, face30 = 0;
361 long iters = 0;
362 bool threw = false;
363 std::string what;
364};
365
366template <class S>
367DropResult runDroplet(int n, double R, double sigma, double mu, double ratio, double kappa,
368 double dtFac, int steps, int bfp = -1) {
369 DropResult r;
370 try {
371 auto s = makeDroplet<S>(n, R, sigma, mu, 1.0, ratio, kappa, dtFac, bfp);
372 for (int k = 0; k < steps; ++k) {
373 s->step();
374 if (k + 1 == 30)
375 r.face30 = maxFaceVel(*s);
376 }
377 r.cellU = maxVel(*s);
378 r.faceU = maxFaceVel(*s);
379 r.iters = s->lastPressureIterations();
380 } catch (const std::exception& e) {
381 r.threw = true;
382 r.what = e.what();
383 }
384 return r;
385}
386
387void gateStaticDroplet() {
388 std::printf(
389 "\n=== T2 static droplet, CONSTANT kappa: the face force is exactly grad(sigma*kappa*C), "
390 "so\n"
391 " the balance is an exact fixed point. On the STAGGERED grid the force goes through\n"
392 " A = rho_f/dt - mu*Lap, which does not commute with the discrete gradient at "
393 "variable\n"
394 " rho or near walls, so the balance is only approached (WO-P's mu*dt^2 residue). The\n"
395 " collocated step (balanced-force projection OFF) no longer annihilates "
396 "constant-kappa\n"
397 " CSF per step: a decaying transient residue remains (doc §4.6.5 model: 2.2e-4 at\n"
398 " ratio 1 after 30 steps, 1.6e-5 at ratio 1000, decaying to 2.4e-17 / 2.1e-7 in 300\n"
399 " steps; 1-2 decades above staggered at ratio 1000). Exact from step 1 needs the\n"
400 " balanced-force projection. Gated here: no throw, decay 30 -> 90 steps, and the\n"
401 " 30-step face frozen at 10x its WO-V1 value.\n");
402 // FROZEN at WO-V1 (2026-09-25, host-openmp): the collocated face |uf| after 30 steps, measured
403 // 9.99e-5 / 2.40e-4 / 1.46e-4 / 6.22e-5 for ratio 1 / 10 / 100 / 1000 (mu = 0.1); the gate is
404 // 10x that (doc §9 G4-OFF).
405 const double ratios[4] = {1.0, 10.0, 100.0, 1000.0};
406 const double frozen30[4] = {9.99e-4, 2.40e-3, 1.46e-3, 6.22e-4};
407 for (int q = 0; q < 4; ++q) {
408 const double ratio = ratios[q];
409 const auto st = runDroplet<Stag>(32, 8.0, 1.0, 0.1, ratio, 0.25, 0.5, 30);
410 const auto co = runDroplet<Colo>(32, 8.0, 1.0, 0.1, ratio, 0.25, 0.5, 90, 0);
411 if (st.threw)
412 std::printf(" ratio %6g staggered THREW: %.90s\n", ratio, st.what.c_str());
413 else
414 std::printf(" ratio %6g staggered cell %.4e face %.4e it %ld\n", ratio, st.cellU,
415 st.faceU, st.iters);
416 if (co.threw)
417 std::printf(" ratio %6g COLLOCATED THREW: %.90s\n", ratio, co.what.c_str());
418 else
419 std::printf(
420 " ratio %6g COLLOCATED face %.4e (30 steps) -> %.4e (90 steps), cell %.4e "
421 "it %ld\n",
422 ratio, co.face30, co.faceU, co.cellU, co.iters);
423 if (ratio == 1.0) // the exactness statement of the staggered grid
424 CHECK(!st.threw && st.faceU < 1e-14);
425 CHECK(!co.threw);
426 CHECK(co.faceU < co.face30);
427 CHECK(co.face30 <= frozen30[q]);
428 CHECK(co.iters < 500);
429 }
430 std::printf(
431 "\n The ratio-1000 mu sweep (the WO-T face-acceleration form went unstable at mu = 0.1,\n"
432 " ~4x per step; the predictor form is stable at every mu dt). Decay 30 -> 90 steps.\n");
433 // FROZEN at WO-V1: 30-step collocated face for mu = 0 / 0.01 / 0.1, measured 6.74e-5 / 6.50e-5 /
434 // 6.22e-5; the gate is 10x that.
435 const double mus[3] = {0.0, 0.01, 0.1};
436 const double frozenMu[3] = {6.74e-4, 6.50e-4, 6.22e-4};
437 for (int q = 0; q < 3; ++q) {
438 const double mu = mus[q];
439 const auto st = runDroplet<Stag>(32, 8.0, 1.0, mu, 1000.0, 0.25, 0.5, 30);
440 const auto co = runDroplet<Colo>(32, 8.0, 1.0, mu, 1000.0, 0.25, 0.5, 90, 0);
441 std::printf(
442 " ratio 1000, mu = %-5g staggered face %.4e%s | COLLOCATED face %.4e -> "
443 "%.4e%s\n",
444 mu, st.faceU, st.threw ? " (THREW)" : "", co.face30, co.faceU, co.threw ? " (THREW)" : "");
445 CHECK(!co.threw);
446 CHECK(co.faceU < co.face30);
447 CHECK(co.face30 <= frozenMu[q]);
448 }
449}
450
451// ---------------------------------------------------------------- T1/T1b/T2 with the option ON
452//
453// The balanced-force projection (doc/collocated_varrho_forces.md §4.6) is the DEFAULT on this path
454// (U2): once per step it moves the gradient part of the forces into P with the projection's own
455// operator, so a static balance is exact from the FIRST step. These are the settled V8 exactness
456// statements (constant-kappa CSF annihilated; hydrostatic dP/dz exact) at the solver default.
457void gateBalancedOn() {
458 std::printf("\n=== T1/T1b/T2 ON (the V8 default): static balances exact from step 1\n");
459 struct Case {
460 const char* name;
461 bool periodic, colour;
462 };
463 const Case cases[3] = {{"periodic, hand-set rho, frozen", true, false},
464 {"walled, hand-set rho ", false, false},
465 {"walled, through C (frozen) ", false, true}};
466 // The PERIODIC box at the default Chebyshev rtol 1e-9 misses by more than 10x (after 1 step: face
467 // 5.6e-10, cell 2.0e-9, dP/dz 4.3e-11; after 100: face 9.9e-12) -- and passes at 1e-14 (face
468 // 3.0e-14, cell 8.9e-14, dP/dz 1.2e-14), so it is the solve tolerance, not the scheme (doc §9 G4:
469 // re-run at Chebyshev 1e-14; only a miss there is conceptual). It is gated at 1e-14. The walled
470 // cases are too since the symmetric V-cycle became the default (doc/mg_depth_policy.md A3): at
471 // 1e-9 their step-1 face error is a stopping draw, 6.6e-13 (trilinear) and 1.5e-12 (symmetric),
472 // at 1e-14 5.1e-14, the same protocol. WO-P5 (the increment solve, full-RHS stop) does not change
473 // the first step -- one solve at rtol 1e-9 relative to the full right-hand side -- and after it
474 // the pre-projection skips (1e-9: 100 steps face 2.2e-10; 1e-14: 13 iterations at step 1, then 0,
475 // never the cap).
476 for (const Case& c : cases)
477 for (int steps : {1, 100}) {
478 const auto co = hydrostatic<Colo>(1000.0, 0.0, steps, c.periodic, c.colour, -1, 1e-14);
479 std::printf(" T1 %s %3d step(s) cell %.3e face %.3e dP/dz %.3e it %ld\n", c.name, steps,
480 co.cellU, co.faceU, co.pErr, co.iters);
481 CHECK(co.faceU < 1e-12);
482 CHECK(co.cellU < 1e-10);
483 CHECK(co.pErr < 1e-10);
484 CHECK(co.iters < kChebMaxit);
485 }
486 { // T1 periodic at the DEFAULT Chebyshev rtol (1e-9), 100 steps: the frozen step-1 error.
487 // Why 1e-9 and not the 1e-12 above: the pre-projection solves once at step 1 to rtol * |b|
488 // relative to the FULL right-hand side (WO-P5), and |b| = |D(O c beta)| is large here (the
489 // ratio-1000 hydrostatic force), so rtol * |b| leaves ~1e-10 in X; from step 2 the previous
490 // split already meets that test and the solve is SKIPPED, which freezes that step-1 error in
491 // the face field instead of letting later solves shave it. Measured 2.2e-10 (WO-P5); gated
492 // at 1e-9 so a real regression (a second copy of the balanced pressure, a lost skip) shows.
493 // The pre-projection tolerance stays EQUAL to the main solve's (DECIDED 2026-09-25): do not
494 // tighten it to pass a tighter gate; the 1e-14 cases above are the exactness statement.
495 const auto d = hydrostatic<Colo>(1000.0, 0.0, 100, true, false, -1, -1.0);
496 std::printf(" T1 periodic at the default rtol, 100 steps: face %.3e cell %.3e dP/dz %.3e\n",
497 d.faceU, d.cellU, d.pErr);
498 CHECK(d.faceU < 1e-9);
499 }
500 { // T1b: the walled column's cell field is exact too (the transient never exists)
501 const auto a = hydrostatic<Colo>(1000.0, 0.0, 100, false, false);
502 std::printf(" T1b walled ratio 1000, 100 steps: cell %.3e (cb %.3e)\n", a.cellU, a.cb);
503 CHECK(a.cellU <= 1e-10);
504 }
505 const double ratios[4] = {1.0, 10.0, 100.0, 1000.0};
506 for (double ratio : ratios) {
507 const auto co = runDroplet<Colo>(32, 8.0, 1.0, 0.1, ratio, 0.25, 0.5, 30);
508 std::printf(" T2 ratio %6g cell %.4e face %.4e it %ld%s\n", ratio, co.cellU, co.faceU,
509 co.iters, co.threw ? " THREW" : "");
510 const double tol = (ratio == 1.0) ? 1e-14 : 1e-13;
511 CHECK(!co.threw && co.faceU < tol && co.cellU < tol);
512 }
513 for (double mu : {0.0, 0.01, 0.1}) {
514 const auto st = runDroplet<Stag>(32, 8.0, 1.0, mu, 1000.0, 0.25, 0.5, 30);
515 const auto co = runDroplet<Colo>(32, 8.0, 1.0, mu, 1000.0, 0.25, 0.5, 30);
516 std::printf(
517 " T2 ratio 1000 mu %-5g staggered (OFF) face %.4e | COLLOCATED face %.4e "
518 "cell %.4e\n",
519 mu, st.faceU, co.faceU, co.cellU);
520 CHECK(!co.threw && co.faceU < 1e-13);
521 // No worse than staggered. At mu = 0 both faces are round-off (staggered 7e-18 / 1e-17,
522 // collocated 5e-18 / 5e-17 across V-cycle builds, doc/mg_depth_policy_log.md 2026-10-30
523 // "A3 finish"), and the order of two round-off values is a draw: floored at 1e-16.
524 CHECK(co.faceU < std::max(st.faceU, 1e-16));
525 }
526}
527
528// ---------------------------------------------------------------- T3: constant-density equivalence
529void gateUniformReduction() {
530 std::printf(
531 "\n=== T3 uniform rho, mu = 0: the V8 face-force predictor IS the validated cell-force one\n"
532 " (avg_f(P(i)-P(i-s)) is the central difference) -> agreement at round-off\n");
533 const int N = 16, NZ = 8;
534 std::vector<std::vector<double>> uu, pp;
535 for (int var = 0; var < 2; ++var) {
536 Colo s(N, N, NZ);
537 s.setAdvection(false);
538 s.setRho(2.0);
539 s.setMu(0.0);
540 s.setDt(4.0);
541 s.setPressureGeometry(std::vector<double>((std::size_t)N * N * NZ, 10.0));
542 s.setBodyForce(1e-2, 3e-3, 0.0);
543 if (var) {
544 s.addField("rho");
545 s.setField("rho", std::vector<double>((std::size_t)N * N * NZ, 2.0));
546 s.setDensityMode(true);
547 s.setPressurePcg(true, 400, 1e-14);
548 } else {
549 s.setPressurePcg(true, 400, 1e-14);
550 }
551 for (int k = 0; k < 30; ++k)
552 s.step();
553 uu.push_back(s.getVelocity(0));
554 pp.push_back(s.getPressure());
555 }
556 const double du = maxAbsDiff(uu[0], uu[1]), dp = maxAbsDiff(pp[0], pp[1]);
557 std::printf(" max|du| %.3e (|u| %.3e) max|dP| %.3e (|P| %.3e)\n", du, maxAbs(uu[0]), dp,
558 maxAbs(pp[0]));
559 CHECK(du < 1e-13 * std::fmax(maxAbs(uu[0]), 1e-30));
560 CHECK(dp < 1e-11 * std::fmax(maxAbs(pp[0]), 1e-30));
561}
562
563// ---------------------------------------------------------------- T4: the bridge (G6)
564void gateBridge() {
565 std::printf(
566 "\n=== T4 the bridge (G6): the collocated colour transport is the SAME kernel on the SAME\n"
567 " faces. uf_(i) sits at i-1/2 = flow's staggered u(i), so a staggered solver handed\n"
568 " the collocated projected face field must reproduce the colour BITWISE.\n");
569 const int N = 24;
570 const int steps = 20;
571 // A collocated run whose cell velocity is a prescribed smooth solenoidal field; one step()
572 // projects it onto uf_/vf_/wf_, and every advect_vof afterwards reuses that frozen face field.
573 Colo co(N, N, N);
574 co.setAdvection(false);
575 co.setRho(1.0);
576 co.setMu(0.0);
577 co.setDt(0.1); // the ABC field peaks at ~1.7, so the interface CFL is ~0.17 < the WY cap 0.25
578 co.setPressureGeometry(std::vector<double>((std::size_t)N * N * N, 10.0));
579 co.setPressurePcg(true, 400, 1e-14);
580 co.enableVof();
581 {
582 std::vector<double> c0((std::size_t)N * N * N, 0.0);
583 for (int z = 0; z < N; ++z)
584 for (int y = 0; y < N; ++y)
585 for (int x = 0; x < N; ++x) {
586 const double dx = x + 0.5 - (N / 2 + 0.13), dy = y + 0.5 - (N / 2 + 0.27),
587 dz = z + 0.5 - (N / 2 + 0.11);
588 c0[idx(x, y, z, N, N)] = (dx * dx + dy * dy + dz * dz < 36.0) ? 1.0 : 0.0;
589 }
590 co.setVof(c0);
591 }
592 { // an ABC-flow cell velocity: every component varies along all three axes (a uniform or a
593 // solid-body field would not detect the axial half of the index shift)
594 std::vector<double> u((std::size_t)N * N * N), v(u.size()), w(u.size());
595 const double k = 2.0 * M_PI / N;
596 for (int z = 0; z < N; ++z)
597 for (int y = 0; y < N; ++y)
598 for (int x = 0; x < N; ++x) {
599 const double X = x + 0.5, Y = y + 0.5, Z = z + 0.5;
600 u[idx(x, y, z, N, N)] = std::sin(k * Z) + 0.7 * std::cos(k * Y);
601 v[idx(x, y, z, N, N)] = std::sin(k * X) + 0.7 * std::cos(k * Z);
602 w[idx(x, y, z, N, N)] = std::sin(k * Y) + 0.7 * std::cos(k * X);
603 }
604 co.setField("u", u);
605 co.setField("v", v);
606 co.setField("w", w);
607 }
608 co.step(); // populates + projects uf_/vf_/wf_ (and advances C once)
609 const std::vector<double> uf = co.getFaceVelocity(0), vf = co.getFaceVelocity(1),
610 wf = co.getFaceVelocity(2);
611 const std::vector<double> c1 = co.getVof();
612 std::printf(" collocated max|div(open uf)| after the seeding step = %.3e\n",
613 co.maxOpenDivergence());
614 CHECK(co.maxOpenDivergence() < 1e-10);
615
616 // --- (a) the same faces on the STAGGERED solver, whose u(i) IS the low face -------------------
617 Stag st(N, N, N);
618 st.setAdvection(false);
619 st.setRho(1.0);
620 st.setMu(0.0);
621 st.setDt(0.1);
622 st.setPressureGeometry(std::vector<double>((std::size_t)N * N * N, 10.0));
623 st.enableVof();
624 st.setVof(c1);
625 st.setField("u", uf);
626 st.setField("v", vf);
627 st.setField("w", wf);
628 st.setVofStepParity(co.vofStepParity()); // the sweep permutation must start at the same index
629
630 for (int k = 0; k < steps; ++k) {
631 co.advectVofKinematic(0.1);
632 st.advectVofKinematic(0.1);
633 }
634 const double dC = maxAbsDiff(co.getVof(), st.getVof());
635 std::printf(
636 " collocated vs staggered on the SAME face field, %d kinematic steps: max|dC| = "
637 "%.3e\n",
638 steps, dC);
639 CHECK(dC == 0.0);
640 // and the colour actually moved (a bitwise match on a frozen field would be vacuous)
641 std::printf(" max|C - C0| over the run = %.3e (sum C: collocated %.15e, staggered %.15e)\n",
642 maxAbsDiff(co.getVof(), c1), co.vofDiagnostics().sumC, st.vofDiagnostics().sumC);
643 CHECK(maxAbsDiff(co.getVof(), c1) > 0.1);
644}
645
646// ---------------------------------------------------------------- T5: scope
647void gateScope() {
648 std::printf("\n=== T5 scope: what the rung refuses, loudly\n");
649 const int N = 16;
650 std::vector<double> solid((std::size_t)N * N * N);
651 for (int z = 0; z < N; ++z)
652 for (int y = 0; y < N; ++y)
653 for (int x = 0; x < N; ++x) {
654 const double dx = x + 0.5 - N / 2.0, dy = y + 0.5 - N / 2.0, dz = z + 0.5 - N / 2.0;
655 solid[idx(x, y, z, N, N)] = std::sqrt(dx * dx + dy * dy + dz * dz) - 4.0;
656 }
657 { // enable_vof with an immersed solid
658 Colo s(N, N, N);
659 s.setAdvection(false);
660 s.setRho(1.0);
661 s.setMu(0.01);
662 s.setDt(1.0);
663 s.setSolid(solid, true);
664 bool threw = false;
665 try {
666 s.enableVof();
667 } catch (const std::exception&) {
668 threw = true;
669 }
670 std::printf(" enable_vof + immersed solid (collocated) -> throws: %s\n",
671 threw ? "yes" : "NO");
672 CHECK(threw);
673 }
674 { // variable density with an immersed solid: caught at the first project()
675 Colo s(N, N, N);
676 s.setAdvection(false);
677 s.setRho(1.0);
678 s.setMu(0.01);
679 s.setDt(1.0);
680 s.setSolid(solid, true);
681 s.addField("rho");
682 s.setField("rho", std::vector<double>((std::size_t)N * N * N, 1.0));
683 s.setDensityMode(true);
684 bool threw = false;
685 try {
686 s.step();
687 } catch (const std::exception&) {
688 threw = true;
689 }
690 std::printf(" set_density_mode + immersed solid -> throws: %s\n",
691 threw ? "yes" : "NO");
692 CHECK(threw);
693 }
694 { // momentum consistency stays staggered-only
695 Colo s(N, N, N);
696 s.setAdvection(false);
697 s.setRho(1.0);
698 s.setMu(0.01);
699 s.setDt(1.0);
700 s.setPressureGeometry(std::vector<double>((std::size_t)N * N * N, 10.0));
701 bool threw = false;
702 try {
703 s.enableVofMomentum(1.0, 10.0);
704 } catch (const std::exception&) {
705 threw = true;
706 }
707 std::printf(" enable_vof_momentum (collocated) -> throws: %s\n",
708 threw ? "yes" : "NO");
709 CHECK(threw);
710 }
711 { // the harmonic rho_f knob is not wired into the face acceleration
712 Colo s(N, N, N);
713 s.setAdvection(false);
714 s.setRho(1.0);
715 s.setMu(0.01);
716 s.setDt(1.0);
717 s.setPressureGeometry(std::vector<double>((std::size_t)N * N * N, 10.0));
718 s.addField("rho");
719 s.setField("rho", std::vector<double>((std::size_t)N * N * N, 1.0));
720 s.setDensityMode(true);
721 s.setRhoFaceHarmonic(true);
722 bool threw = false;
723 try {
724 s.step();
725 } catch (const std::exception&) {
726 threw = true;
727 }
728 std::printf(" set_rho_face_harmonic (collocated) -> throws: %s\n",
729 threw ? "yes" : "NO");
730 CHECK(threw);
731 }
732 { // the pressure force lives in the implicit predictor: the non-incremental step has none
733 Colo s(N, N, N);
734 s.setAdvection(false);
735 s.setRho(1.0);
736 s.setMu(0.01);
737 s.setDt(1.0);
738 s.setPressureGeometry(std::vector<double>((std::size_t)N * N * N, 10.0));
739 s.addField("rho");
740 s.setField("rho", std::vector<double>((std::size_t)N * N * N, 1.0));
741 s.setDensityMode(true);
742 s.setIncrementalPressure(false);
743 bool threw = false;
744 try {
745 s.step();
746 } catch (const std::exception&) {
747 threw = true;
748 }
749 std::printf(" non-incremental pressure (collocated V8) -> throws: %s\n",
750 threw ? "yes" : "NO");
751 CHECK(threw);
752 }
753}
754
755} // namespace
756
757int main(int argc, char** argv) {
758 Kokkos::initialize(argc, argv);
759 {
760 gateHydrostatic();
761 gateInvisibleSubspace();
762 gateStaticDroplet();
763 gateBalancedOn();
764 gateUniformReduction();
765 gateBridge();
766 gateScope();
767 }
768 Kokkos::finalize();
769 if (failures) {
770 std::fprintf(stderr, "\n%d CHECK(s) failed\n", failures);
771 return 1;
772 }
773 std::printf("\nAll rung V8 collocated gates passed.\n");
774 return 0;
775}
Weymouth-Yue split advection of a colour field on an extended (inner + ghost) block.
flow — host-facing Kokkos IBM Navier-Stokes solver (drop-in flow-style API).
y
Definition cmp.py:24
list ratios
Definition mgd_case.py:448
float solid
Definition owner_gate.py:49
long L3(int x, int y, int z, I3 e)
int m
Definition post.py:35
xc
Definition post.py:21
hydrostatic(ratio, mu=0.0, steps=100, g=0.1, dt=1.0, N=8, NZ=24)
static constexpr int NZ
static constexpr int N
static double maxAbs(const std::vector< double > &a)
static double maxAbsDiff(const std::vector< double > &a, const std::vector< double > &b)
peclet::flow::Solver< peclet::flow::Staggered > Stag
peclet::flow::Solver< peclet::flow::Colocated > Colo
static constexpr double R
static std::vector< double > colour(int m)
#define CHECK(cond)