flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_units.cpp
Go to the documentation of this file.
1// PHYSICAL DOMAINS — the two gates of suite/docs/PHYSICAL_UNITS_PLAN.md Phase 1.
2//
3// Run as `test_units identity` and `test_units scale` (two ctests off one binary).
4//
5// GATE 1 — units_identity. The physical constructor at `extent == cells` reproduces the cell-unit
6// solver exactly: every accessor reports the historical numbers, and with rho = 1 and dt = 1 (where
7// all three reference scales are 1) the fields come back BITWISE equal to an extent=None run. This
8// is the anchor of the whole refactor: an armed solver that happens to sit on the unit lattice must
9// be the unit-lattice solver.
10//
11// GATE 2 — units_scale_invariance. The SAME physical problem, written in two unit systems whose
12// length unit differs by a factor 1000, must give the same physical answer. Under a pure change of
13// length unit (time and mass units fixed, numerical lengths divided by lam):
14//
15// L -> L/lam u -> u/lam rho -> rho*lam^3 mu -> mu*lam F -> F*lam^2 p -> p*lam
16// dt unchanged sdf -> sdf/lam
17//
18// Substituting those into the boundary conversions of `Solver::UnitScales` leaves EVERY internal
19// quantity unchanged (mu' = mu*tRef/(rhoRef*hRef^2) is invariant, and so are dt', rho', F', the
20// index velocity and the index SDF), so the two runs solve the identical discrete system and can
21// only differ by the round-off of forming the conversion factors themselves. Any surviving `h` in
22// a kernel or at the API boundary breaks that invariance immediately — which is exactly what this
23// gate is for (plan §5.2, target 1e-13 relative).
24//
25// Both a SAMPLED SDF (set_solid) and an ANALYTIC SCENE (set_scene + set_solid_from_scene) are
26// carried through the scale change, on Poiseuille and on a periodic sphere in Stokes flow.
27//
28// GATE 4 — units_anisotropic_poiseuille (`test_units aniso`). Phase 2 (anisotropic cells),
29// flow/doc/anisotropic_metric.md §8.2. Plane Poiseuille on cells (16, 40, 8), with the cut-cell
30// walls exactly on two y cell CENTRES — where the second difference of a quadratic is exact, so
31// the discrete solution IS the analytic parabola u(y) = F (y - ylo)(yhi - y)/(2 mu) and the only
32// thing the comparison can measure is the operator. TWO stretched configurations, making two
33// DIFFERENT statements:
34//
35// (a) EXACTNESS, extent (16, 10, 16) -> spacing (1, 0.25, 2), hRef = 0.25 on `y` (a non-trivial
36// check that hRef is min_a h_a and not h_x), h' = (4, 1, 8), w = (1/16, 1, 1/64) and
37// mu' = mu*tRef/hRef^2 = 80 — every one of them exactly representable in float, so the
38// stored operator carries no rounding of its own and the metric is on trial alone. Bound
39// 1e-9 of u_max = F H^2/(8 mu) = 0.2 (measured 1.388e-15).
40//
41// (b) THE FLOAT-STORAGE TRIPWIRE, extent (16, 12, 16) -> spacing (1, 0.3, 2), h' = (10/3, 1,
42// 20/3), w = (0.09, 1, 0.0225) — the §5.3 configuration, and a production-SHAPED one: with
43// hRef = 0.3 neither mu' = 55.5555… nor AC = 1 + 2((5 + 55.5555…) + 1.25) = 124.6111… is
44// representable, so the six off-diagonals no longer sum to AC - idiag exactly and the whole
45// profile is scaled by 1 - 1.06e-07 (that ratio is CONSTANT across all fifteen fluid rows,
46// which is what identifies it). Bound 1e-7, measured 3.052e-08 — and 8.674e-15 when the
47// identical source is built with -DPECLET_FLOW_OPERATOR_DOUBLE. The discretisation is
48// pointwise exact here too; this row watches the WO-M float operator-storage floor
49// (docs/SCALING_ISSUES.md #1), it is NOT an exactness statement and NOT a metric defect.
50//
51// Both configurations also require max|v|, max|w| <= 1e-12 u_max and the wall/solid u rows exactly
52// 0. The isotropic control on (16, 16, 16) at extent == cells meets the same bounds and reports
53// the EXACT identity metric (hp = w = (1,1,1), vol = 1, aniso false) from the §1.4 snap. The gate
54// also asserts the §7 refusals: the collocated policy, `enable_vof` and the hydro force integrals
55// each throw on an anisotropic domain with the three spacings in the message.
56//
57// GATE 3 — units_vof_sigma (`test_units vof`). The V4 surface-tension gates with a PHYSICAL sigma
58// at extent = 1e-2 * cells (plan §9.3 U5, §5.6). Surface tension is a force per unit LENGTH, i.e.
59// mass per time squared, so under a pure change of LENGTH unit sigma is numerically INVARIANT while
60// mu -> mu*lam, rho -> rho*lam^3, kappa -> kappa*lam, p -> p*lam and u -> u/lam. Two checks, the
61// two the V4 rung rests on: the exactness identity (a CONSTANT curvature makes the CSF the exact
62// discrete gradient of sigma*kappa*C, which the projection annihilates, so a static droplet stays
63// at machine zero) and Young-Laplace (the pressure jump equals sigma*kappa). Both must give the
64// same PHYSICS in the two unit systems.
65#include <algorithm>
66#include <cmath>
67#include <cstdio>
68#include <cstring>
69#include <Kokkos_Core.hpp>
70#include <string>
71#include <vector>
72
73#include "flow_ibm.hpp"
74
75namespace {
76int failures = 0;
77#define CHECK(cond) \
78 do { \
79 if (!(cond)) { \
80 std::fprintf(stderr, "CHECK failed: %s\n at %s:%d\n", #cond, __FILE__, __LINE__); \
81 ++failures; \
82 } \
83 } while (0)
84
85void checkClose(double a, double b, double tol, const char* what) {
86 if (!(std::fabs(a - b) <= tol)) {
87 std::fprintf(stderr, "CHECK_CLOSE failed (%s): %.17g vs %.17g (|d| = %.3e > %.3e)\n", what, a,
88 b, std::fabs(a - b), tol);
89 ++failures;
90 }
91}
92
93bool bitwiseEqual(const std::vector<double>& a, const std::vector<double>& b) {
94 if (a.size() != b.size())
95 return false;
96 return std::memcmp(a.data(), b.data(), a.size() * sizeof(double)) == 0;
97}
98
99double maxAbs(const std::vector<double>& a) {
100 double m = 0.0;
101 for (double v : a)
102 if (!(std::fabs(v) <= m))
103 m = std::fabs(v);
104 return m;
105}
106
109double relDiffScaled(const std::vector<double>& a, const std::vector<double>& b, double k) {
110 const double den = maxAbs(a);
111 double m = 0.0;
112 for (std::size_t i = 0; i < a.size(); ++i) {
113 const double d = std::fabs(a[i] - k * b[i]);
114 if (!(d <= m))
115 m = d; // NaN-propagating
116 }
117 return den > 0.0 ? m / den : m;
118}
119
120// --------------------------------------------------------------------------------------------
121// The two problems, both written in ONE unit system parameterised by `lam` (lam = 1 is the
122// cell-unit system: side N, spacing 1). Everything below is stated physically; nothing computes
123// with a cell size.
124// --------------------------------------------------------------------------------------------
125struct Fields {
126 std::vector<double> u, v, w, p;
127 std::array<double, 3> spacing{};
128 double div = 0.0;
129};
130
132template <class Grid>
133Fields runChannel(int nx, int ny, int nz, double lam, bool arm) {
134 const double RHO = 1.0 * lam * lam * lam, MU = 0.1 * lam, F = 0.01 * lam * lam, DT = 50.0;
135 const double H = 1.0 / lam; // one cell, physically
136 const double ylo = (std::round(0.30 * ny) + 0.5) * H, yhi = (std::round(0.70 * ny) + 0.5) * H;
137 peclet::flow::Solver<Grid> s(nx, ny, nz);
138 s.setAdvection(false);
139 if (arm)
140 s.setPhysicalDomain({nx * H, ny * H, nz * H}, {0.0, 0.0, 0.0}, {nx, ny, nz});
141 s.setRho(RHO);
142 s.setMu(MU);
143 s.setDt(DT);
144 s.setBodyForce(F, 0.0, 0.0);
145 s.setVelocityIterations(400);
146 s.setVelocityResidualTolerance(1e-14);
147 s.setPressurePcg(true, 80, 1e-13);
148 // WO-F (doc/vof_projection_distributed_design.md §9, R-8): this gate is about units, so it pins
149 // the FP64 V-cycle -- the FP32 weight rounding is not covariant under a non-power-of-two unit
150 // change (p 2.0e-11 / 1.05e-10 vs 1e-13 under 'auto' on a device); P§13 G-D2(a) gates D's
151 // power-of-two covariance.
152 s.setPressureVcyclePrecision(peclet::flow::CutcellMG::kVcycleFp64);
153 // The SDF is a PHYSICAL signed distance sampled at the physical cell centres.
154 const std::vector<double> cx = s.cellCentres(0), cy = s.cellCentres(1), cz = s.cellCentres(2);
155 std::vector<double> sdf((std::size_t)nx * ny * nz);
156 for (int z = 0; z < nz; ++z)
157 for (int y = 0; y < ny; ++y)
158 for (int x = 0; x < nx; ++x)
159 sdf[(std::size_t)x + (std::size_t)y * nx + (std::size_t)z * nx * ny] =
160 std::min(cy[y] - ylo, yhi - cy[y]);
161 (void)cx;
162 (void)cz;
163 s.setSolid(sdf, /*cutcellPressure=*/false);
164 for (int it = 0; it < 300; ++it)
165 s.step();
166 Fields f;
167 f.u = s.getVelocity(0);
168 f.v = s.getVelocity(1);
169 f.w = s.getVelocity(2);
170 f.p = s.getPressure();
171 f.spacing = s.spacing();
172 return f;
173}
174
177template <class Grid>
178Fields runSphere(int N, double lam, bool arm, bool scene) {
179 const double RHO = 1.0 * lam * lam * lam, MU = 0.1 * lam, F = 1e-3 * lam * lam, DT = 60.0;
180 const double H = 1.0 / lam;
181 const double R = 0.30 * N * H;
183 if (arm)
184 s.setPhysicalDomain({N * H, N * H, N * H}, {0.0, 0.0, 0.0}, {N, N, N});
185 s.setRho(RHO);
186 s.setMu(MU);
187 s.setDt(DT);
188 s.setBodyForce(F, 0.0, 0.0);
189 s.setAdvection(false);
190 s.setVelocityIterations(200);
191 s.setVelocityResidualTolerance(1e-14);
192 s.setPressurePcg(true, 120, 1e-13);
193 // WO-F (doc/vof_projection_distributed_design.md §9, R-8): this gate is about units, so it pins
194 // the FP64 V-cycle -- the FP32 weight rounding is not covariant under a non-power-of-two unit
195 // change (p 2.0e-11 / 1.05e-10 vs 1e-13 under 'auto' on a device); P§13 G-D2(a) gates D's
196 // power-of-two covariance.
197 s.setPressureVcyclePrecision(peclet::flow::CutcellMG::kVcycleFp64);
198 if (scene) {
199 namespace g = peclet::core::geom;
200 g::SceneBuilder<double> b;
201 b.addLeaf(g::kSphere, {R});
202 g::Transform<double> tr;
203 // Centre of the box, in PHYSICAL coordinates.
204 tr.translation = peclet::core::Vec3<double>{0.5 * N * H, 0.5 * N * H, 0.5 * N * H};
205 b.addInstance(0, tr);
206 std::vector<int> ni, ii;
207 std::vector<double> nr, ir;
208 b.encode(ni, nr, ii, ir);
209 s.setScene(ni, nr, ii, ir, /*periodic=*/true);
210 s.setSolidFromScene(/*cutcellPressure=*/true);
211 } else {
212 const std::vector<double> cx = s.cellCentres(0), cy = s.cellCentres(1), cz = s.cellCentres(2);
213 const double c = 0.5 * N * H;
214 std::vector<double> sdf((std::size_t)N * N * N);
215 for (int z = 0; z < N; ++z)
216 for (int y = 0; y < N; ++y)
217 for (int x = 0; x < N; ++x)
218 sdf[(std::size_t)x + (std::size_t)y * N + (std::size_t)z * N * N] =
219 std::sqrt((cx[x] - c) * (cx[x] - c) + (cy[y] - c) * (cy[y] - c) +
220 (cz[z] - c) * (cz[z] - c)) -
221 R;
222 s.setSolid(sdf, /*cutcellPressure=*/true);
223 }
224 for (int it = 0; it < 60; ++it)
225 s.step();
226 Fields f;
227 f.u = s.getVelocity(0);
228 f.v = s.getVelocity(1);
229 f.w = s.getVelocity(2);
230 f.p = s.getPressure();
231 f.spacing = s.spacing();
232 f.div = s.maxOpenDivergence();
233 return f;
234}
235
240std::vector<double> sphereColour(int n, double R, double cx, double cy, double cz, int sub = 24) {
241 std::vector<double> C((std::size_t)n * n * n, 0.0);
242 const double w = 1.0 / sub;
243 for (int k = 0; k < n; ++k)
244 for (int j = 0; j < n; ++j)
245 for (int i = 0; i < n; ++i) {
246 double acc = 0.0;
247 for (int b = 0; b < sub; ++b)
248 for (int a = 0; a < sub; ++a) {
249 const double px = i + (a + 0.5) * w, py = j + (b + 0.5) * w;
250 const double r2 = R * R - (px - cx) * (px - cx) - (py - cy) * (py - cy);
251 if (r2 <= 0.0)
252 continue;
253 const double hh = std::sqrt(r2);
254 const double lo = std::fmax(cz - hh, (double)k), hi = std::fmin(cz + hh, (double)k + 1);
255 if (hi > lo)
256 acc += hi - lo;
257 }
258 C[(std::size_t)i + (std::size_t)j * n + (std::size_t)k * n * n] = acc / (sub * sub);
259 }
260 return C;
261}
262
263struct Drop {
264 double maxU = 0.0;
265 double dp = 0.0;
266 double kappa = 0.0;
267 double dtCap = 0.0;
268};
269
272Drop runDroplet(int n, double Rcells, double lam, bool constantKappa, int steps) {
273 const double H = 1.0 / lam; // one cell, physically
274 const double SIGMA = 1.0; // invariant under a change of LENGTH unit
275 const double MU = 0.1 * lam, RHO = 1.0 * lam * lam * lam;
277 s.setAdvection(false);
278 s.setPhysicalDomain({n * H, n * H, n * H}, {0.0, 0.0, 0.0}, {n, n, n});
279 s.setRho(RHO);
280 s.setMu(MU);
281 s.setDt(1.0);
282 s.setVelocityResidualTolerance(0.0); // machine-precision gate: keep the fixed-sweep loop
283 s.setPressureGeometry(std::vector<double>((std::size_t)n * n * n, 10.0 * H));
284 s.setPressureChebyshev(true, 500, 1e-14);
285 s.enableVof();
286 const std::vector<double> C = sphereColour(n, Rcells, n / 2 + 0.13, n / 2 + 0.27, n / 2 + 0.11);
287 s.setVof(C);
288 s.setPropertyModel("rho", peclet::flow::ClosureKind::LinearMix, "C", "", {RHO, 0.0});
289 s.setSurfaceTension(SIGMA);
290 Drop d;
291 d.kappa = 2.0 / (Rcells * H); // physical 1/length
292 if (constantKappa)
293 s.setVofKappaConstant(d.kappa);
294 else
295 s.computeVofCurvature();
296 d.dtCap = s.capillaryDt();
297 s.setDt(0.5 * d.dtCap);
298 for (int k = 0; k < steps; ++k)
299 s.step();
300 for (int c = 0; c < 3; ++c)
301 d.maxU = std::fmax(d.maxU, maxAbs(s.getVelocity(c)));
302 // Young-Laplace: the pressure inside the drop minus the pressure well outside it.
303 const std::vector<double> p = s.getPressure();
304 const std::size_t inside =
305 (std::size_t)(n / 2) + (std::size_t)(n / 2) * n + (std::size_t)(n / 2) * n * n;
306 const std::size_t outside = 1 + (std::size_t)1 * n + (std::size_t)1 * n * n;
307 d.dp = p[inside] - p[outside];
308 return d;
309}
310
311// --------------------------------------------------------------------------------------------
312void gateVofSigma() {
313 std::printf("=== units_vof_sigma ===\n");
314 const int N = 32;
315 const double Rc = 8.0;
316 const double lam = 1e2; // extent = 1e-2 * cells
317
318 // (a) the exactness identity, in both unit systems.
319 const Drop a = runDroplet(N, Rc, 1.0, true, 20);
320 const Drop b = runDroplet(N, Rc, lam, true, 20);
321 std::printf(" constant kappa: max|u| %.3e (lam=1) vs %.3e (lam=%g) -> %.3e rescaled\n", a.maxU,
322 b.maxU, lam, b.maxU * lam);
323 // A velocity of exactly zero would also pass, so pin the SCALE the currents are measured against:
324 // sigma*kappa/mu is the capillary velocity the naive CSF would produce.
325 const double uScale = 1.0 * a.kappa / 0.1;
326 CHECK(a.maxU < 1e-14 * uScale);
327 CHECK(b.maxU * lam < 1e-14 * uScale);
328 checkClose(a.dp, 1.0 * a.kappa, 1e-9 * a.kappa, "Young-Laplace at lam=1");
329 checkClose(b.dp / lam, a.dp, 1e-9 * a.kappa, "Young-Laplace at lam=1e2 (rescaled)");
330 std::printf(" Young-Laplace: dp %.9e vs sigma*kappa %.9e (lam=%g: %.9e rescaled)\n", a.dp,
331 a.kappa, lam, b.dp / lam);
332 checkClose(b.kappa / lam, a.kappa, 1e-12 * a.kappa, "kappa scales as 1/length");
333 checkClose(b.dtCap, a.dtCap, 1e-12 * a.dtCap,
334 "capillary_dt invariant under a length-unit change");
335 std::printf(" capillary_dt %.9e vs %.9e (invariant: sigma and the time unit are)\n", a.dtCap,
336 b.dtCap);
337
338 // (b) the COMPUTED curvature: the physical spurious currents and the jump must agree.
339 const Drop ca = runDroplet(N, Rc, 1.0, false, 20);
340 const Drop cb = runDroplet(N, Rc, lam, false, 20);
341 const double ru = std::fabs(ca.maxU - cb.maxU * lam) / std::fabs(ca.maxU);
342 const double rp = std::fabs(ca.dp - cb.dp / lam) / std::fabs(ca.dp);
343 std::printf(
344 " computed kappa: max|u| %.6e vs %.6e rescaled (rel %.3e); dp %.6e vs %.6e "
345 "(rel %.3e)\n",
346 ca.maxU, cb.maxU * lam, ru, ca.dp, cb.dp / lam, rp);
347 CHECK(ru <= 1e-12);
348 CHECK(rp <= 1e-12);
349}
350
351// --------------------------------------------------------------------------------------------
352// Phase 2 gate G1 (doc/anisotropic_metric.md §8.2).
353// --------------------------------------------------------------------------------------------
354
358struct Channel {
359 Fields f;
360 std::vector<double> yc;
361 double ylo = 0.0, yhi = 0.0;
362 std::array<double, 3> w{1.0, 1.0, 1.0};
363 bool aniso = false;
364};
365
366Channel runChannelBox(int nx, int ny, int nz, const std::array<double, 3>& extent, int jlo, int jhi,
367 bool arm) {
368 const double RHO = 1.0, MU = 0.1, F = 0.01, DT = 50.0;
370 s.setAdvection(false);
371 if (arm)
372 s.setPhysicalDomain(extent, {0.0, 0.0, 0.0}, {nx, ny, nz});
373 s.setRho(RHO);
374 s.setMu(MU);
375 s.setDt(DT);
376 s.setBodyForce(F, 0.0, 0.0);
377 s.setVelocityIterations(400);
378 s.setVelocityResidualTolerance(1e-14);
379 s.setPressurePcg(true, 80, 1e-13);
380 Channel ch;
381 ch.yc = s.cellCentres(1);
382 ch.ylo = ch.yc[jlo];
383 ch.yhi = ch.yc[jhi];
384 std::vector<double> sdf((std::size_t)nx * ny * nz);
385 for (int z = 0; z < nz; ++z)
386 for (int y = 0; y < ny; ++y)
387 for (int x = 0; x < nx; ++x)
388 sdf[(std::size_t)x + (std::size_t)y * nx + (std::size_t)z * nx * ny] =
389 std::min(ch.yc[y] - ch.ylo, ch.yhi - ch.yc[y]);
390 s.setSolid(sdf, /*cutcellPressure=*/false);
391 for (int it = 0; it < 300; ++it)
392 s.step();
393 ch.f.u = s.getVelocity(0);
394 ch.f.v = s.getVelocity(1);
395 ch.f.w = s.getVelocity(2);
396 ch.f.p = s.getPressure();
397 ch.f.spacing = s.spacing();
398 const auto& u = s.unitScales();
399 ch.w = {u.w[0], u.w[1], u.w[2]};
400 ch.aniso = u.aniso;
401 return ch;
402}
403
407void checkChannel(const char* what, const Channel& ch, int nx, int ny, int nz, int jlo, int jhi,
408 double bound) {
409 const double MU = 0.1, F = 0.01;
410 const double H = ch.yhi - ch.ylo;
411 const double umax = F * H * H / (8.0 * MU);
412 std::printf(" %s: spacing (%.17g, %.17g, %.17g) aniso %d w (%.17g, %.17g, %.17g)\n", what,
413 ch.f.spacing[0], ch.f.spacing[1], ch.f.spacing[2], (int)ch.aniso, ch.w[0], ch.w[1],
414 ch.w[2]);
415 double relErr = 0.0, wallMax = 0.0;
416 for (int z = 0; z < nz; ++z)
417 for (int y = 0; y < ny; ++y)
418 for (int x = 0; x < nx; ++x) {
419 const std::size_t i = (std::size_t)x + (std::size_t)y * nx + (std::size_t)z * nx * ny;
420 const double uu = ch.f.u[i];
421 if (y <= jlo || y >= jhi) { // wall and solid rows
422 if (!(std::fabs(uu) <= wallMax))
423 wallMax = std::fabs(uu);
424 continue;
425 }
426 const double ex = F * (ch.yc[y] - ch.ylo) * (ch.yhi - ch.yc[y]) / (2.0 * MU);
427 const double d = std::fabs(uu - ex) / umax;
428 if (!(d <= relErr))
429 relErr = d;
430 }
431 const double mv = maxAbs(ch.f.v), mw = maxAbs(ch.f.w);
432 std::printf(
433 " H %.17g u_max %.17g max rel |u - parabola| %.3e (bound %.0e) max|v| %.3e "
434 "max|w| %.3e max|u| on the wall/solid rows %.3e\n",
435 H, umax, relErr, bound, mv, mw, wallMax);
436 if (!(relErr <= bound))
437 std::printf(
438 " NOTE: a miss that is a SINGLE one-signed multiplicative factor across the "
439 "whole profile (rel err proportional to u, one constant ratio on every fluid row) "
440 "is the FLOAT momentum-operator storage (`IbmSolver::FV`, MReal; WO-M / "
441 "docs/SCALING_ISSUES.md #1), not the metric -- rebuild with "
442 "-DPECLET_FLOW_OPERATOR_DOUBLE to separate the two.\n");
443 CHECK(relErr <= bound);
444 CHECK(wallMax == 0.0);
445 CHECK(mv <= 1e-12 * umax);
446 CHECK(mw <= 1e-12 * umax);
447}
448
449void gateAnisoPoiseuille() {
450 std::printf("=== units_anisotropic_poiseuille ===\n");
451 // (a) EXACTNESS. cells (16, 40, 8) over (16, 10, 16) -> spacing (1, 0.25, 2), hRef = 0.25 on the
452 // y axis, h' = (4, 1, 8), w = (1/16, 1, 1/64), mu' = 80 -- all float-representable, so the
453 // stored operator adds no rounding and the metric is on trial alone. Walls on the y cell
454 // centres 12.5*0.25 = 3.125 and 28.5*0.25 = 7.125 (H = 4).
455 {
456 const Channel ch = runChannelBox(16, 40, 8, {16.0, 10.0, 16.0}, 12, 28, /*arm=*/true);
457 CHECK(ch.aniso);
458 CHECK(ch.f.spacing[0] == 1.0 && ch.f.spacing[1] == 0.25 && ch.f.spacing[2] == 2.0);
459 CHECK(ch.w[0] == 0.0625 && ch.w[1] == 1.0 && ch.w[2] == 0.015625);
460 CHECK(ch.ylo == 3.125 && ch.yhi == 7.125);
461 checkChannel("stretched EXACTNESS (16, 40, 8) on (16, 10, 16)", ch, 16, 40, 8, 12, 28, 1e-9);
462 }
463 // (b) THE FLOAT-STORAGE TRIPWIRE. The same grid over (16, 12, 16) -> spacing (1, 0.3, 2),
464 // hRef = 0.3, h' = (10/3, 1, 20/3), w = (0.09, 1, 0.0225); walls at 12.5*0.3 = 3.75 and
465 // 28.5*0.3 = 8.55 (H = 4.8). mu' = 55.5555... and AC = 124.6111... are not representable in
466 // the float operator storage, which scales the whole profile by 1 - 1.06e-07: measured
467 // 3.052e-08 here and 8.674e-15 with -DPECLET_FLOW_OPERATOR_DOUBLE. Bound 1e-7 (see the
468 // header).
469 {
470 const Channel ch = runChannelBox(16, 40, 8, {16.0, 12.0, 16.0}, 12, 28, /*arm=*/true);
471 CHECK(ch.aniso);
472 checkClose(ch.f.spacing[0], 1.0, 0.0, "aniso spacing x");
473 checkClose(ch.f.spacing[1], 0.3, 1e-17, "aniso spacing y");
474 checkClose(ch.f.spacing[2], 2.0, 0.0, "aniso spacing z");
475 checkClose(ch.ylo, 3.75, 1e-14, "aniso wall ylo");
476 checkClose(ch.yhi, 8.55, 1e-14, "aniso wall yhi");
477 checkChannel("stretched FLOAT-FLOOR (16, 40, 8) on (16, 12, 16)", ch, 16, 40, 8, 12, 28, 1e-7);
478 }
479 // (c) The isotropic control on (16, 16, 16) at extent == cells, with `runChannel`'s own wall
480 // convention (round(0.30*ny) + 0.5, round(0.70*ny) + 0.5 in cells): the same bound, and the
481 // §1.4 snap gives it EXACTLY the identity metric. (It is not BITWISE the extent=None run of
482 // the same problem, and must not be asserted to be: dt = 50 pins tRef = 50, so the armed run
483 // computes with dt' = 1 and mu' = 50 mu while the cell-unit one computes with dt' = 50 and
484 // mu' = mu -- the same physics, a different internal scaling. `units_identity` is the
485 // bitwise statement, and it fixes rho = dt = 1 for exactly this reason.)
486 {
487 const Channel a = runChannelBox(16, 16, 16, {16.0, 16.0, 16.0}, 5, 11, /*arm=*/true);
488 CHECK(!a.aniso);
489 CHECK(a.w[0] == 1.0 && a.w[1] == 1.0 && a.w[2] == 1.0);
490 CHECK(a.f.spacing[0] == 1.0 && a.f.spacing[1] == 1.0 && a.f.spacing[2] == 1.0);
491 checkChannel("isotropic control (16, 16, 16) at extent == cells", a, 16, 16, 16, 5, 11, 1e-9);
492 }
493 // (d) The §7 table, as commit C4 leaves it. ADMITTED (C4 lifted them): the collocated policy
494 // and the hydro force integrals. STILL REFUSED, with the three spacings in the message:
495 // enable_vof (Phase 3), and hydro_force_torque_reaction only on the v3 moving-wall torque
496 // path (doc/units_escalation.md E3), which needs a moving scene and is not reachable here.
497 {
498 bool threw = false;
499 try {
501 sc.setAdvection(false);
502 sc.setPhysicalDomain({16.0, 12.0, 16.0}, {0.0, 0.0, 0.0}, {16, 40, 8});
503 const auto sp = sc.spacing();
504 std::printf(" ADMITTED (collocated): spacing (%.17g, %.17g, %.17g), aniso %d\n", sp[0],
505 sp[1], sp[2], (int)sc.unitScales().aniso);
506 CHECK(sc.unitScales().aniso);
507 } catch (const std::exception& ex) {
508 threw = true;
509 std::printf(" UNEXPECTED refusal (collocated): %s\n", ex.what());
510 }
511 CHECK(!threw);
512 threw = false;
513 try {
514 // PHASE 3 INVERTED THIS ASSERTION. Phase 2 wrote it as `CHECK(threw)` — `enable_vof` was
515 // the one entry point its §7 still refused on an anisotropic domain, and the gate pinned
516 // that refusal so it could not be lifted by accident. Phase 3 (flow/doc/anisotropic_vof.md)
517 // is what lifts it: the colour transport is metric-free because a stretched cell IS the unit
518 // cube of the index coordinates, and everything that reads a direction, a length or an area
519 // — the height-function curvature, the paraboloid frames, the CSF face force's per-axis
520 // weight `w_a`, the wetting rotation, the phase-change normals/areas/V_cell — now carries
521 // the metric. `units_vof_aniso` is the gate that holds it: the balanced-force exactness
522 // identity is at machine zero (2.3e-17 at aspect 2, 1.9e-17 at aspect 4) and Young-Laplace
523 // is exact, which is only true if the CSF weight is EXACTLY this file's `w_a`.
525 ss.setAdvection(false);
526 ss.setPhysicalDomain({16.0, 12.0, 16.0}, {0.0, 0.0, 0.0}, {16, 40, 8});
527 ss.enableVof();
528 std::printf(" ADMITTED (enable_vof — Phase 3; see units_vof_aniso)\n");
529 } catch (const std::exception& ex) {
530 threw = true;
531 std::printf(" UNEXPECTED refusal (enable_vof): %s\n", ex.what());
532 }
533 CHECK(!threw);
534 threw = false;
535 try {
537 ss.setAdvection(false);
538 ss.setPhysicalDomain({16.0, 12.0, 16.0}, {0.0, 0.0, 0.0}, {16, 40, 8});
539 ss.hydroForceTorque(); // C4: admitted (no scene here, so it returns an empty result)
540 std::printf(" ADMITTED (hydro_force_torque)\n");
541 } catch (const std::exception& ex) {
542 threw = true;
543 std::printf(" UNEXPECTED refusal (hydro_force_torque): %s\n", ex.what());
544 }
545 CHECK(!threw);
546 }
547}
548
549// --------------------------------------------------------------------------------------------
550void gateIdentity() {
551 std::printf("=== units_identity ===\n");
552
553 // (a) No domain: the historical cell-unit reports.
554 {
556 s.setAdvection(false);
557 CHECK(!s.hasPhysicalDomain());
558 const auto h = s.spacing();
559 CHECK(h[0] == 1.0 && h[1] == 1.0 && h[2] == 1.0);
560 const auto e = s.domainExtent();
561 CHECK(e[0] == 6.0 && e[1] == 8.0 && e[2] == 10.0);
562 const auto o = s.domainOrigin();
563 CHECK(o[0] == 0.0 && o[1] == 0.0 && o[2] == 0.0);
564 const auto c = s.cellCentres(1);
565 CHECK(c.size() == 8u);
566 CHECK(c[0] == 0.5 && c[7] == 7.5);
567 }
568
569 // (b) extent == cells with a shifted origin: spacing 1, centres shifted, scales all 1.
570 {
572 s.setAdvection(false);
573 s.setPhysicalDomain({6.0, 8.0, 10.0}, {-2.0, 0.5, 100.0}, {6, 8, 10});
574 CHECK(s.hasPhysicalDomain());
575 const auto h = s.spacing();
576 CHECK(h[0] == 1.0 && h[1] == 1.0 && h[2] == 1.0);
577 const auto o = s.domainOrigin();
578 CHECK(o[0] == -2.0 && o[1] == 0.5 && o[2] == 100.0);
579 const auto cx = s.cellCentres(0);
580 CHECK(cx[0] == -1.5 && cx[5] == 3.5);
581 const auto cz = s.cellCentres(2);
582 CHECK(cz[0] == 100.5 && cz[9] == 109.5);
583 const auto& u = s.unitScales();
584 CHECK(u.hRef == 1.0);
585 s.setRho(1.0);
586 s.setDt(1.0);
587 CHECK(u.rhoRef == 1.0 && u.tRef == 1.0);
588 CHECK(u.muToInt() == 1.0 && u.pToInt() == 1.0 && u.forceToInt(0) == 1.0);
589 CHECK(u.velToInt(2) == 1.0 && u.lenToInt() == 1.0 && u.sigmaToInt() == 1.0);
590 }
591
592 // (c) BITWISE: an armed run at extent == cells with rho = dt = 1 against extent=None.
593 {
594 auto run = [](bool arm) {
595 const int nx = 8, ny = 24, nz = 8;
597 s.setAdvection(false);
598 if (arm)
599 s.setPhysicalDomain({(double)nx, (double)ny, (double)nz}, {0.0, 0.0, 0.0}, {nx, ny, nz});
600 s.setRho(1.0);
601 s.setMu(0.1);
602 s.setDt(1.0);
603 s.setBodyForce(0.01, 0.0, 0.0);
604 s.setVelocityIterations(60);
605 s.setPressureIterations(4);
606 const double ylo = 7.5, yhi = ny - 7.5;
607 std::vector<double> sdf((std::size_t)nx * ny * nz);
608 for (int z = 0; z < nz; ++z)
609 for (int y = 0; y < ny; ++y)
610 for (int x = 0; x < nx; ++x)
611 sdf[(std::size_t)x + (std::size_t)y * nx + (std::size_t)z * nx * ny] =
612 std::min((y + 0.5) - ylo, yhi - (y + 0.5));
613 s.setSolid(sdf, /*cutcellPressure=*/true);
614 for (int it = 0; it < 30; ++it)
615 s.step();
616 Fields f;
617 f.u = s.getVelocity(0);
618 f.v = s.getVelocity(1);
619 f.w = s.getVelocity(2);
620 f.p = s.getPressure();
621 return f;
622 };
623 const Fields a = run(false), b = run(true);
624 CHECK(maxAbs(a.u) > 0.0);
625 CHECK(bitwiseEqual(a.u, b.u));
626 CHECK(bitwiseEqual(a.v, b.v));
627 CHECK(bitwiseEqual(a.w, b.w));
628 CHECK(bitwiseEqual(a.p, b.p));
629 std::printf(" extent == cells, rho = dt = 1: bitwise identical to the cell-unit run\n");
630 }
631}
632
633// --------------------------------------------------------------------------------------------
634void gateScale() {
635 std::printf("=== units_scale_invariance ===\n");
636 const double lam = 1e3; // the second unit system's length unit is 1000x larger
637 const double TOL = 1e-13;
638
639 {
640 const int nx = 8, ny = 24, nz = 8;
641 const Fields a = runChannel<peclet::flow::Staggered>(nx, ny, nz, 1.0, true);
642 const Fields b = runChannel<peclet::flow::Staggered>(nx, ny, nz, lam, true);
643 checkClose(a.spacing[0], 1.0, 0.0, "channel spacing (system A)");
644 checkClose(b.spacing[0], 1.0 / lam, 1e-18, "channel spacing (system B)");
645 // u_B = u_A / lam and p_B = p_A * lam, exactly.
646 const double ru = relDiffScaled(a.u, b.u, lam);
647 const double rp = relDiffScaled(a.p, b.p, 1.0 / lam);
648 std::printf(" Poiseuille (sampled SDF): u %.3e p %.3e\n", ru, rp);
649 CHECK(maxAbs(a.u) > 0.0);
650 CHECK(ru <= TOL);
651 CHECK(rp <= TOL);
652 }
653
654 for (int scene = 0; scene < 2; ++scene) {
655 const int N = 24;
656 const Fields a = runSphere<peclet::flow::Staggered>(N, 1.0, true, scene != 0);
657 const Fields b = runSphere<peclet::flow::Staggered>(N, lam, true, scene != 0);
658 const double ru = relDiffScaled(a.u, b.u, lam);
659 const double rv = relDiffScaled(a.v, b.v, lam);
660 const double rp = relDiffScaled(a.p, b.p, 1.0 / lam);
661 // The divergence diagnostic is 1/time and the time unit did not change, so it is invariant —
662 // but it is a solver RESIDUAL sitting at ~1e-12, not a component of the solution, so the
663 // meaningful statement is that both runs are converged and that the two residuals agree to
664 // round-off OF THAT RESIDUAL (measured 1.8e-18 absolute), not to 1e-13 of themselves.
665 const double rd = std::fabs(a.div - b.div);
666 std::printf(" sphere Stokes (%s): u %.3e v %.3e p %.3e div %.3e vs %.3e (d %.3e)\n",
667 scene ? "analytic scene" : "sampled SDF", ru, rv, rp, a.div, b.div, rd);
668 CHECK(maxAbs(a.u) > 0.0);
669 CHECK(ru <= TOL);
670 CHECK(rv <= TOL);
671 CHECK(rp <= TOL);
672 CHECK(a.div < 1e-9 && b.div < 1e-9); // both converged
673 CHECK(rd <= 1e-15); // and the residuals agree to their own round-off
674 }
675}
676// --------------------------------------------------------------------------------------------
677// GATE 5 - units_anisotropic_sphere (`test_units sphere`). Phase 2 gate G2,
678// doc/anisotropic_metric.md §8.3. The regression suite's Zick & Homsy sphere (phi = 0.216,
679// K_ref = 7.442) with `tests/regression/perf_baseline.json`'s own configuration - rho 1, mu 0.1,
680// dt 60, F 1e-3, 80 velocity sweeps, MG-PCG 300 / 1e-8, cut-cell pressure, advection off, the same
681// convergence rule on mean(u) - on the CUBE L^3 resolved by cells (N, 2N, N/2), i.e.
682// h = (dx, dx/2, 2dx), with the SDF sampled at the PHYSICAL cell centres. Four requirements:
683// (i) the stretched errors |K_s(N) - K_ref|/K_ref are STRICTLY DECREASING in N;
684// (ii) err_s(N) <= 4 err_c(N) + 0.2 % at every N (the z axis is twice as coarse, and second
685// order allows 4x);
686// (iii) the least-squares order of the stretched ERROR sequence, p_s >= 1.5;
687// (iv) the isotropic control (N, N, N) in CELL UNITS reproduces the recorded baseline: the step
688// counts 60 / 80 / 75 and the pressure iterations per step 6 / 7 / 7 EXACTLY, and the drag
689// factors 7.299689474225098 / 7.389063051924422 / 7.416167928926183 to 1e-6 relative.
690// Plus the §4.4 HYDRO-FORCE gate that C4 also lifts: on a periodic Stokes cell the total force on
691// the body equals the body force integrated over the fluid volume the momentum rows cover,
692// F_x * (h_x h_y h_z) * (# fluid u-DOFs) - the periodic Stokes identity. Measured on the cubic and
693// on the stretched grid; the stretched grid must reach the tolerance the cubic one does.
694//
695// Two notes on (iii) and (iv). `fit_order` (the regression's f(N) = f_inf + C N^-p grid search) is
696// DEGENERATE on three grids - two free linear parameters fit three points exactly for every p - so
697// the ctest gates the log-log least-squares slope of the errors instead, and
698// `scripts/verify_anisotropic_spheres.py` runs the five-grid ladder where `fit_order` is the same
699// estimator the regression uses.
700//
701// And "bitwise" in §8.3 (iv) is not attainable against `perf_baseline.json`, for two reasons that
702// have nothing to do with the metric: the baseline is recorded on the CUDA build (the regression's
703// documented tree) while this ctest also runs on host-openmp, and its `u.mean()` is numpy's
704// PAIRWISE summation against a sequential C++ sum here. Both perturb the drag factor at the
705// PRESSURE SOLVE'S OWN STOPPING TOLERANCE - the baseline configuration is MG-PCG rtol 1e-8 - and
706// the measured gap is 1.4e-08 / 9.7e-10 / 1.8e-09, i.e. exactly there. What IS exactly
707// reproducible is the discrete part of the record, so the gate asserts the step counts and the
708// pressure iterations per step EXACTLY (60 / 80 / 75 at 6 / 7 / 7) and the drag factor to 1e-6,
709// which still pins every digit `perf_baseline.json` prints, and it prints all 17 either way.
710//
711// GATE 6 - units_anisotropic_tgv (`test_units tgv`). Phase 2 gate G3, §8.4. Stokes Taylor-Green
712// on a periodic box L x L x L_z with cells (N, 2N, 4), rho = dt = 1 (so every reference scale but
713// hRef is 1 and the isotropic control can be compared BITWISE to the cell-unit run), mu = 0.1. The
714// mu = 0.25 is chosen so that BOTH assembled operators are exactly representable in the shipped
715// FLOAT operator storage (`IbmSolver::FV`, MReal; WO-M / docs/SCALING_ISSUES.md #1) and the metric
716// is therefore on trial alone -- the same move E1 of doc/units_escalation.md made for G1. Isotropic
717// control: hRef = 1, mu' = 0.25, b = (1/4, 1/4, 1/4), AC = 1 + 2((b+b)+b) = 5/2. Stretched:
718// hRef = 0.5, hp = (2, 1, 2), w = (1/4, 1, 1/4), mu' = 1, b = (1/4, 1, 1/4), AC = 4. Every one of
719// them dyadic. At mu = 0.1 the same gate reads 1.5e-07 (isotropic control) and 2.9e-07 (stretched)
720// -- the float floor, in the isotropic control too, so not a metric statement.
721// The plain TG field is NOT discretely divergence-free on a stretched staggered grid; the initial
722// field is the one that is,
723// u = a_x cos(kx) sin(ky), v = -a_y sin(kx) cos(ky),
724// a_x sin(k h_x/2)/h_x == a_y sin(k h_y/2)/h_y,
725// which is also an eigenvector of the anisotropic 7-point operator, so the backward-Euler amplitude
726// ratio per step is EXACTLY r = 1/(1 + dt nu Lambda) with
727// Lambda = 2(1-cos k h_x)/h_x^2 + 2(1-cos k h_y)/h_y^2.
728struct Zh {
729 double K = 0.0, umean = 0.0, div = 0.0, R = 0.0, pIterStep = 0.0;
730 int steps = 0;
731 std::array<double, 3> spacing{};
732 bool aniso = false;
733};
734
737Zh runZhSphere(int nx, int ny, int nz, bool arm, double L, int levels) {
738 const double RHO = 1.0, MU = 0.1, DT = 60.0, FX = 1e-3, PHI = 0.216;
739 const double R = std::pow(PHI * 3.0 / (4.0 * M_PI), 1.0 / 3.0) * L;
741 if (arm)
742 s.setPhysicalDomain({L, L, L}, {0.0, 0.0, 0.0}, {nx, ny, nz});
743 s.setRho(RHO);
744 s.setMu(MU);
745 s.setDt(DT);
746 s.setBodyForce(FX, 0.0, 0.0);
747 s.setAdvection(false);
748 s.setVelocityIterations(80);
749 s.setPressureLevels(levels);
750 s.setPressurePcg(true, 300, 1e-8);
751 const std::vector<double> cx = s.cellCentres(0), cy = s.cellCentres(1), cz = s.cellCentres(2);
752 const double c = 0.5 * L;
753 std::vector<double> sdf((std::size_t)nx * ny * nz);
754 for (int z = 0; z < nz; ++z)
755 for (int y = 0; y < ny; ++y)
756 for (int x = 0; x < nx; ++x)
757 sdf[(std::size_t)x + (std::size_t)y * nx + (std::size_t)z * nx * ny] =
758 std::sqrt((cx[x] - c) * (cx[x] - c) + (cy[y] - c) * (cy[y] - c) +
759 (cz[z] - c) * (cz[z] - c)) -
760 R;
761 s.setSolid(sdf, /*cutcellPressure=*/true);
762 std::vector<double> pit;
763 double prev = 0.0;
764 Zh r;
765 for (int it = 0; it < 400; ++it) {
766 s.step();
767 ++r.steps;
768 pit.push_back((double)s.lastPressureIterations());
769 if (it % 5 == 4) {
770 const std::vector<double> uu = s.getVelocity(0);
771 double m = 0.0;
772 for (double v : uu)
773 m += v;
774 m /= (double)uu.size();
775 if (it >= 15 && std::fabs(m - prev) < 1e-5 * (std::fabs(m) + 1e-30))
776 break;
777 prev = m;
778 }
779 }
780 const std::vector<double> uu = s.getVelocity(0);
781 double m = 0.0;
782 for (double v : uu)
783 m += v;
784 r.umean = m / (double)uu.size();
785 r.R = R;
786 r.K = FX * (L * L * L) / (6.0 * M_PI * MU * R * r.umean);
787 r.div = s.maxOpenDivergence();
788 r.spacing = s.spacing();
789 r.aniso = s.unitScales().aniso;
790 std::vector<double> half(pit.begin() + (std::ptrdiff_t)(pit.size() / 2), pit.end());
791 std::sort(half.begin(), half.end());
792 const std::size_t nh = half.size();
793 r.pIterStep = (nh % 2) ? half[nh / 2] : 0.5 * (half[nh / 2 - 1] + half[nh / 2]);
794 return r;
795}
796
798double logLogOrder(const std::vector<double>& Ns, const std::vector<double>& err) {
799 double sx = 0, sy = 0, sxx = 0, sxy = 0;
800 const double n = (double)Ns.size();
801 for (std::size_t i = 0; i < Ns.size(); ++i) {
802 const double X = std::log(Ns[i]), Y = std::log(err[i]);
803 sx += X;
804 sy += Y;
805 sxx += X * X;
806 sxy += X * Y;
807 }
808 return -(n * sxy - sx * sy) / (n * sxx - sx * sx);
809}
810
813struct React {
814 double Fx = 0.0, Fref = 0.0, rel = 0.0;
815 long nFluid = 0;
816 std::array<double, 3> spacing{};
817};
818
819React runReactionSphere(int nx, int ny, int nz, bool arm, double L, int levels, int nsteps) {
820 const double RHO = 1.0, MU = 0.1, DT = 60.0, FX = 1e-3, PHI = 0.216;
821 const double R = std::pow(PHI * 3.0 / (4.0 * M_PI), 1.0 / 3.0) * L;
823 if (arm)
824 s.setPhysicalDomain({L, L, L}, {0.0, 0.0, 0.0}, {nx, ny, nz});
825 s.setRho(RHO);
826 s.setMu(MU);
827 s.setDt(DT);
828 s.setBodyForce(FX, 0.0, 0.0);
829 s.setAdvection(false);
830 s.setVelocityIterations(200);
831 s.setVelocityResidualTolerance(1e-12);
832 s.setPressureLevels(levels);
833 s.setPressurePcg(true, 300, 1e-10);
834 {
835 namespace g = peclet::core::geom;
836 g::SceneBuilder<double> b;
837 b.addLeaf(g::kSphere, {R});
838 g::Transform<double> tr;
839 tr.translation = peclet::core::Vec3<double>{0.5 * L, 0.5 * L, 0.5 * L};
840 b.addInstance(0, tr);
841 std::vector<int> ni, ii;
842 std::vector<double> nr, ir;
843 b.encode(ni, nr, ii, ir);
844 s.setScene(ni, nr, ii, ir, /*periodic=*/true);
845 s.setSolidFromScene(/*cutcellPressure=*/true);
846 }
847 for (int it = 0; it < nsteps; ++it)
848 s.step();
849 React out;
850 out.spacing = s.spacing();
851 // The fluid u-DOF count, from the solver's OWN sampled SDF and `ibmSolidMask`'s rule: the
852 // staggered sample at (x-1/2, y, z) is the mean of the two adjacent cell values (periodic wrap),
853 // and the DOF is SOLID for `sd <= 0`.
854 const std::vector<double> sd = s.getField("sdf");
855 long nf = 0;
856 for (int z = 0; z < nz; ++z)
857 for (int y = 0; y < ny; ++y)
858 for (int x = 0; x < nx; ++x) {
859 const int xm = (x + nx - 1) % nx;
860 const double a = sd[(std::size_t)xm + (std::size_t)y * nx + (std::size_t)z * nx * ny];
861 const double b2 = sd[(std::size_t)x + (std::size_t)y * nx + (std::size_t)z * nx * ny];
862 if (0.5 * (a + b2) > 0.0)
863 ++nf;
864 }
865 out.nFluid = nf;
866 const double vcell = out.spacing[0] * out.spacing[1] * out.spacing[2];
867 out.Fref = FX * vcell * (double)nf;
868 const std::vector<double> fr = s.hydroForceTorqueReaction();
869 out.Fx = fr.empty() ? 0.0 : fr[0];
870 out.rel = std::fabs(out.Fx - out.Fref) / std::fabs(out.Fref);
871 return out;
872}
873
874void gateAnisoSphere() {
875 std::printf("=== units_anisotropic_sphere ===\n");
876 const double KREF = 7.442;
877 const double REC[3] = {7.299689474225098, 7.389063051924422, 7.416167928926183};
878 const int NS[3] = {16, 24, 32};
879 std::vector<double> Ns, errS, errC;
880 for (int i = 0; i < 3; ++i) {
881 const int N = NS[i];
882 const int levels = std::max(2, (int)std::floor(std::log2((double)N)) - 1);
883 const Zh cub = runZhSphere(N, N, N, /*arm=*/false, (double)N, levels);
884 const Zh str = runZhSphere(N, 2 * N, N / 2, /*arm=*/true, (double)N, levels);
885 const double ec = std::fabs(cub.K - KREF) / KREF, es = std::fabs(str.K - KREF) / KREF;
886 Ns.push_back((double)N);
887 errC.push_back(ec);
888 errS.push_back(es);
889 std::printf(" N=%2d cubic K %.17g err %.4f %% iters/step %.1f steps %3d div %.2e\n", N,
890 cub.K, 100.0 * ec, cub.pIterStep, cub.steps, cub.div);
891 std::printf(
892 " stretched (%d,%d,%d) spacing (%g, %g, %g) aniso %d K %.17g err %.4f %%"
893 " iters/step %.1f steps %3d div %.2e\n",
894 N, 2 * N, N / 2, str.spacing[0], str.spacing[1], str.spacing[2], (int)str.aniso, str.K,
895 100.0 * es, str.pIterStep, str.steps, str.div);
896 // (iv) the cubic control reproduces the recorded baseline value to every printed digit.
897 const double dRec = std::fabs(cub.K - REC[i]) / REC[i];
898 const int RECSTEPS[3] = {60, 80, 75};
899 // Pressure iterations/step: 7 -> 6 at N = 24 and 32 when the symmetric V-cycle became the
900 // default (doc/mg_depth_policy.md A3, WO-S3); K and the step counts are unchanged.
901 // perf_baseline.json records 6.0 there too (its iteration fields re-recorded, A3 review 5a).
902 const double RECITERS[3] = {6.0, 6.0, 6.0};
903 std::printf(
904 " cubic vs perf_baseline.json %.17g rel %.3e (bound 1e-6) steps %d "
905 "(recorded %d) iters/step %.1f (recorded %.1f)\n",
906 REC[i], dRec, cub.steps, RECSTEPS[i], cub.pIterStep, RECITERS[i]);
907 CHECK(dRec <= 1e-6);
908 CHECK(cub.steps == RECSTEPS[i]);
909 CHECK(cub.pIterStep == RECITERS[i]);
910 // (ii) err_s <= 4 err_c + 0.2 %
911 CHECK(es <= 4.0 * ec + 0.002);
912 // pressure iterations on the stretched grid: <= cubic + 2 (the §5 aspect rule)
913 std::printf(" pressure iters/step cubic %.1f stretched %.1f (bound cubic + 2)\n",
914 cub.pIterStep, str.pIterStep);
915 CHECK(str.pIterStep <= cub.pIterStep + 2.0);
916 }
917 // (i) strictly decreasing stretched errors
918 CHECK(errS[1] < errS[0]);
919 CHECK(errS[2] < errS[1]);
920 // (iii) least-squares order of the stretched error sequence
921 const double ps = logLogOrder(Ns, errS), pc = logLogOrder(Ns, errC);
922 std::printf(" LS order of the ERROR sequence: stretched %.4f (bound >= 1.5), cubic %.4f\n", ps,
923 pc);
924 CHECK(ps >= 1.5);
925
926 // --- §4.4, the hydro-force gate C4 lifts: the periodic Stokes identity F = F_body * V_fluid ---
927 {
928 const int N = 24;
929 const int levels = std::max(2, (int)std::floor(std::log2((double)N)) - 1);
930 const React rc = runReactionSphere(N, N, N, /*arm=*/false, (double)N, levels, 300);
931 const React rs = runReactionSphere(N, 2 * N, N / 2, /*arm=*/true, (double)N, levels, 300);
932 std::printf(" hydro_force_torque_reaction, periodic Stokes identity F = F_body*V_fluid:\n");
933 std::printf(
934 " cubic (%d,%d,%d) spacing (%g,%g,%g) F_x %.17g identity %.17g rel %.3e"
935 " (%ld fluid u-DOFs)\n",
936 N, N, N, rc.spacing[0], rc.spacing[1], rc.spacing[2], rc.Fx, rc.Fref, rc.rel, rc.nFluid);
937 std::printf(
938 " stretched (%d,%d,%d) spacing (%g,%g,%g) F_x %.17g identity %.17g rel %.3e"
939 " (%ld fluid u-DOFs)\n",
940 N, 2 * N, N / 2, rs.spacing[0], rs.spacing[1], rs.spacing[2], rs.Fx, rs.Fref, rs.rel,
941 rs.nFluid);
942 // "to the same relative tolerance the cubic grid achieves" (§8.3), gated as ONE shared bound
943 // rather than a ratio: both grids are at the march's own residual floor here (300 steps of the
944 // regression's dt = 60 configuration, momentum residual stop 1e-12, MG-PCG 1e-10), and 1e-5 is
945 // the order the cubic grid reaches with a decade of margin.
946 CHECK(rs.rel <= 1e-5);
947 CHECK(rc.rel <= 1e-5);
948 std::printf(" stretched/cubic relative-gap ratio %.3f (both gated at 1e-5)\n",
949 rs.rel / rc.rel);
950 }
951}
952
953// --------------------------------------------------------------------------------------------
954struct Tgv {
955 double ratio = 0.0, rExact = 0.0, relRate = 0.0, div = 0.0, prof = 0.0, pmax = 0.0;
956 std::vector<double> u, v, w, p;
957 std::array<double, 3> spacing{};
958 bool aniso = false;
959};
960
964Tgv runTgv(int nx, int ny, int nz, bool arm, double Lx, double Ly, double Lz, bool advect,
965 int nsteps, double DT = 1.0) {
966 const double RHO = 1.0, MU = 0.25;
967 const double hx = arm ? Lx / nx : 1.0, hy = arm ? Ly / ny : 1.0, hz = arm ? Lz / nz : 1.0;
968 const double Lw = arm ? Lx : (double)nx; // the vortex wavelength (x and y share it)
969 const double k = 2.0 * M_PI / Lw;
970 const double ax = 1.0;
971 const double ay = ax * (std::sin(0.5 * k * hx) / hx) * (hy / std::sin(0.5 * k * hy));
973 if (arm)
974 s.setPhysicalDomain({Lx, Ly, Lz}, {0.0, 0.0, 0.0}, {nx, ny, nz});
975 s.setRho(RHO);
976 s.setMu(MU);
977 s.setDt(DT);
978 s.setAdvection(advect);
979 s.setVelocityIterations(400);
980 s.setVelocityResidualTolerance(1e-14);
981 s.setPressurePcg(true, 300, 1e-13);
982 s.setPressureGeometry(std::vector<double>((std::size_t)nx * ny * nz, 10.0));
983 const std::size_t n = (std::size_t)nx * ny * nz;
984 std::vector<double> u(n), v(n), w(n, 0.0);
985 for (int z = 0; z < nz; ++z)
986 for (int y = 0; y < ny; ++y)
987 for (int x = 0; x < nx; ++x) {
988 const std::size_t i = (std::size_t)x + (std::size_t)y * nx + (std::size_t)z * nx * ny;
989 u[i] = ax * std::cos(k * (x * hx)) * std::sin(k * ((y + 0.5) * hy));
990 v[i] = -ay * std::sin(k * ((x + 0.5) * hx)) * std::cos(k * (y * hy));
991 }
992 s.uploadVelocity(u, v, w);
993 Tgv r;
994 const double Lambda =
995 2.0 * (1.0 - std::cos(k * hx)) / (hx * hx) + 2.0 * (1.0 - std::cos(k * hy)) / (hy * hy);
996 r.rExact = std::pow(1.0 / (1.0 + DT * (MU / RHO) * Lambda), (double)nsteps);
997 for (int it = 0; it < nsteps; ++it)
998 s.step();
999 r.u = s.getVelocity(0);
1000 r.v = s.getVelocity(1);
1001 r.w = s.getVelocity(2);
1002 r.p = s.getPressure();
1003 r.spacing = s.spacing();
1004 r.aniso = s.unitScales().aniso;
1005 double num = 0.0, den = 0.0;
1006 for (std::size_t i = 0; i < n; ++i) {
1007 num += r.u[i] * u[i];
1008 den += u[i] * u[i];
1009 }
1010 r.ratio = num / den;
1011 double pmaxu = 0.0, perr = 0.0;
1012 for (std::size_t i = 0; i < n; ++i) {
1013 pmaxu = std::fmax(pmaxu, std::fabs(u[i]));
1014 perr = std::fmax(perr, std::fabs(r.u[i] - r.ratio * u[i]));
1015 }
1016 r.prof = perr / pmaxu;
1017 r.relRate = std::fabs(r.ratio - r.rExact) / r.rExact;
1018 r.div = s.maxOpenDivergence();
1019 r.pmax = maxAbs(r.p);
1020 (void)hz;
1021 return r;
1022}
1023
1024void gateAnisoTgv() {
1025 std::printf("=== units_anisotropic_tgv ===\n");
1026 const int N = 16, NS = 10;
1027 // (a) STOKES on the stretched box L x L x L_z with cells (N, 2N, 4): h = (1, 0.5, 1).
1028 {
1029 const Tgv t =
1030 runTgv(N, 2 * N, 4, /*arm=*/true, (double)N, (double)N, 4.0, /*advect=*/false, NS);
1031 std::printf(" stretched Stokes (%d,%d,4) spacing (%g, %g, %g) aniso %d\n", N, 2 * N,
1032 t.spacing[0], t.spacing[1], t.spacing[2], (int)t.aniso);
1033 std::printf(" amplitude ratio %.17g exact %.17g rel %.3e (bound 1e-10)\n", t.ratio,
1034 t.rExact, t.relRate);
1035 std::printf(
1036 " max|div_o| %.3e (bound 1e-12) profile %.3e (bound 1e-10) max|P| %.3e "
1037 "(the phi proxy, bound 1e-12)\n",
1038 t.div, t.prof, t.pmax);
1039 CHECK(t.aniso);
1040 CHECK(t.relRate <= 1e-10);
1041 CHECK(t.div <= 1e-12);
1042 CHECK(t.prof <= 1e-10);
1043 CHECK(t.pmax <= 1e-12);
1044 }
1045 // (b) the same with advection ON: the NS run.
1046 {
1047 // dt = 0.05 here, `sdflow_tg`'s own step, which is what makes the advective CFL 0.05: at the
1048 // dt = 1 of the Stokes rows the TG amplitude gives CFL = 1 and the discrete nonlinear term's
1049 // imbalance with the pressure gradient is 2.0e-2, i.e. the O(CFL^2) advection error, not a
1050 // metric defect. dt is free here -- the Stokes rows own the exactness statement and the
1051 // bitwise control, both of which need dt = 1 (it pins tRef = 1).
1052 const Tgv t =
1053 runTgv(N, 2 * N, 4, /*arm=*/true, (double)N, (double)N, 4.0, /*advect=*/true, NS, 0.05);
1054 std::printf(
1055 " stretched NS (%d,%d,4) dt 0.05 amplitude ratio %.17g exact %.17g "
1056 "rel %.3e (bound 5e-3) max|div| %.3e (bound 1e-9)\n",
1057 N, 2 * N, t.ratio, t.rExact, t.relRate, t.div);
1058 CHECK(t.relRate <= 5e-3);
1059 CHECK(t.div <= 1e-9);
1060 }
1061 // (c) the isotropic control (N, N, 4) at extent == cells: the same bounds, and BITWISE the
1062 // cell-unit run of the same problem (rho = dt = 1 pins every reference scale to 1).
1063 {
1064 const Tgv a = runTgv(N, N, 4, /*arm=*/true, (double)N, (double)N, 4.0, /*advect=*/false, NS);
1065 const Tgv b = runTgv(N, N, 4, /*arm=*/false, (double)N, (double)N, 4.0, /*advect=*/false, NS);
1066 std::printf(" isotropic control (%d,%d,4) spacing (%g, %g, %g) aniso %d\n", N, N, a.spacing[0],
1067 a.spacing[1], a.spacing[2], (int)a.aniso);
1068 std::printf(
1069 " amplitude ratio %.17g exact %.17g rel %.3e max|div_o| %.3e profile %.3e "
1070 " max|P| %.3e\n",
1071 a.ratio, a.rExact, a.relRate, a.div, a.prof, a.pmax);
1072 CHECK(!a.aniso);
1073 CHECK(a.relRate <= 1e-10);
1074 CHECK(a.div <= 1e-12);
1075 CHECK(a.prof <= 1e-10);
1076 CHECK(a.pmax <= 1e-12);
1077 const bool bit = bitwiseEqual(a.u, b.u) && bitwiseEqual(a.v, b.v) && bitwiseEqual(a.w, b.w) &&
1078 bitwiseEqual(a.p, b.p);
1079 std::printf(" armed at extent == cells vs the cell-unit run: bitwise %s\n",
1080 bit ? "EQUAL" : "DIFFERENT");
1081 CHECK(bit);
1082 }
1083}
1084} // namespace
1085
1086// =============================================================== units_vof_aniso (PHASE 3, S2)
1087//
1088// `flow/doc/anisotropic_vof.md` §11 S2 — the gate the Phase 2 merge finally makes constructible,
1089// because `enable_vof` refused an anisotropic domain until Phase 3 landed.
1090//
1091// A stationary droplet on cells that are NOT cubes. Two statements, and they are independent:
1092//
1093// (a) THE BALANCED-FORCE IDENTITY SURVIVES THE METRIC. With a CONSTANT curvature the CSF force
1094// is `sigma' kappa' (C(i) - C(i-s_a)) * w_a` — the SAME per-axis weight `w_a = 1/h_a'^2` the
1095// momentum RHS puts on `-(P(i) - P(i-s_a))` (Phase 2 §1.3). So it is again exactly the
1096// discrete weighted gradient of `sigma' kappa' C`, the projection annihilates it, and the
1097// drop stays at machine zero. Get the weight wrong on ONE axis and this floors at the
1098// spurious-current level instead — which is precisely what the gate is for.
1099//
1100// (b) YOUNG-LAPLACE IS THE PHYSICAL JUMP. `dp = sigma * kappa` in the caller's units, on a mesh
1101// whose cells differ by a factor 4 between axes.
1102//
1103// The colour is the EXACT fraction of a physical sphere over anisotropic cells, so the geometry is
1104// the same physical problem at every aspect ratio and only the mesh changes.
1106 double maxU = 0.0, dp = 0.0, kappa = 0.0, dtCap = 0.0, hMin = 0.0;
1107};
1108
1111double sphereFracBox(double cx, double cy, double cz, double R, double x0, double y0, double z0,
1112 const double h[3], int sub = 24) {
1113 const double wx = h[0] / sub, wy = h[1] / sub;
1114 double acc = 0.0;
1115 for (int b = 0; b < sub; ++b)
1116 for (int a = 0; a < sub; ++a) {
1117 const double px = x0 + (a + 0.5) * wx, py = y0 + (b + 0.5) * wy;
1118 const double r2 = R * R - (px - cx) * (px - cx) - (py - cy) * (py - cy);
1119 if (r2 <= 0.0)
1120 continue;
1121 const double hh = std::sqrt(r2);
1122 const double lo = std::fmax(cz - hh, z0), hi = std::fmin(cz + hh, z0 + h[2]);
1123 if (hi > lo)
1124 acc += hi - lo;
1125 }
1126 return acc / (sub * sub * h[2]);
1127}
1128
1130AnisoDrop runDropletAniso(const int n[3], const double h[3], double R, bool constantKappa,
1131 int steps) {
1132 const double SIGMA = 1.0, MU = 0.1, RHO = 1.0;
1134 s.setAdvection(false);
1135 s.setPhysicalDomain({n[0] * h[0], n[1] * h[1], n[2] * h[2]}, {0.0, 0.0, 0.0}, {n[0], n[1], n[2]});
1136 s.setRho(RHO);
1137 s.setMu(MU);
1138 s.setDt(1.0);
1139 s.setVelocityResidualTolerance(0.0); // machine-precision gate: keep the fixed-sweep loop
1140 const std::size_t nc = (std::size_t)n[0] * n[1] * n[2];
1141 const double hMin = std::fmin(h[0], std::fmin(h[1], h[2]));
1142 s.setPressureGeometry(std::vector<double>(nc, 10.0 * hMin));
1143 s.setPressureChebyshev(true, 500, 1e-14);
1144 s.enableVof(); // <- refused before Phase 3; the whole point of this gate
1145 // the drop, off-centre by an irrational fraction of a cell on every axis
1146 const double ctr[3] = {0.5 * n[0] * h[0] + 0.13 * h[0], 0.5 * n[1] * h[1] + 0.27 * h[1],
1147 0.5 * n[2] * h[2] + 0.11 * h[2]};
1148 std::vector<double> C(nc);
1149 for (int k = 0; k < n[2]; ++k)
1150 for (int j = 0; j < n[1]; ++j)
1151 for (int i = 0; i < n[0]; ++i)
1152 C[(std::size_t)i + (std::size_t)j * n[0] + (std::size_t)k * n[0] * n[1]] =
1153 sphereFracBox(ctr[0], ctr[1], ctr[2], R, i * h[0], j * h[1], k * h[2], h);
1154 s.setVof(C);
1155 s.setPropertyModel("rho", peclet::flow::ClosureKind::LinearMix, "C", "", {RHO, 0.0});
1156 s.setSurfaceTension(SIGMA);
1157 AnisoDrop d;
1158 d.hMin = hMin;
1159 d.kappa = 2.0 / R; // physical 1/length
1160 if (constantKappa)
1161 s.setVofKappaConstant(d.kappa);
1162 else
1163 s.computeVofCurvature();
1164 d.dtCap = s.capillaryDt();
1165 s.setDt(0.5 * d.dtCap);
1166 for (int q = 0; q < steps; ++q)
1167 s.step();
1168 for (int c = 0; c < 3; ++c)
1169 d.maxU = std::fmax(d.maxU, maxAbs(s.getVelocity(c)));
1170 const std::vector<double> p = s.getPressure();
1171 const std::size_t inside = (std::size_t)(n[0] / 2) + (std::size_t)(n[1] / 2) * n[0] +
1172 (std::size_t)(n[2] / 2) * n[0] * n[1];
1173 const std::size_t outside = 1 + (std::size_t)1 * n[0] + (std::size_t)1 * n[0] * n[1];
1174 d.dp = p[inside] - p[outside];
1175 return d;
1176}
1177
1179 std::printf("=== units_vof_aniso (Phase 3 S2: the balanced-force CSF on BOX cells) ===\n");
1180 // Three meshes carrying the SAME physical droplet: cubic, aspect 2, aspect 4. The cell counts
1181 // keep the physical box cubic, so only the cell SHAPE changes.
1182 const double base = 1.0 / 32.0;
1183 struct Mesh {
1184 const char* name;
1185 int n[3];
1186 double h[3];
1187 };
1188 const Mesh meshes[3] = {
1189 {"cubic ", {32, 32, 32}, {base, base, base}},
1190 {"aspect 2 ", {32, 64, 32}, {base, base / 2, base}},
1191 {"aspect 4 ", {32, 64, 128}, {base, base / 2, base / 4}},
1192 };
1193 const double R = 8.0 * base; // D/h = 16 on the coarsest axis
1194 const double uScale = 1.0 * (2.0 / R) / 0.1; // sigma*kappa/mu, the naive-CSF current
1195
1196 for (int q = 0; q < 3; ++q) {
1197 const Mesh& m = meshes[q];
1198 // (a) the EXACTNESS identity: constant kappa -> the projection annihilates the force.
1199 const AnisoDrop a = runDropletAniso(m.n, m.h, R, true, 20);
1200 // (b) Young-Laplace with the same constant curvature.
1201 std::printf(" %s n = (%3d,%3d,%3d) h'/hMin = (%.2f, %.2f, %.2f)\n", m.name, m.n[0], m.n[1],
1202 m.n[2], m.h[0] / a.hMin, m.h[1] / a.hMin, m.h[2] / a.hMin);
1203 std::printf(" exactness max|u| = %.3e (gate %.3e = 1e-14 * sigma*kappa/mu)\n", a.maxU,
1204 1e-14 * uScale);
1205 std::printf(" Young-Laplace dp = %.9e vs sigma*kappa = %.9e (rel %.2e)\n", a.dp, a.kappa,
1206 std::fabs(a.dp - a.kappa) / a.kappa);
1207 std::printf(" capillary_dt = %.9e (h_min = %.6e)\n", a.dtCap, a.hMin);
1208 CHECK(a.maxU < 1e-14 * uScale);
1209 checkClose(a.dp, a.kappa, 1e-9 * a.kappa, "anisotropic Young-Laplace");
1210 // The Brackbill limit is set by the SMALLEST cell: refining one axis by 2 must shrink it by
1211 // 2^{3/2}, which is the statement that `capillary_dt` takes `min_a h_a` and not `h_x`.
1212 if (q > 0) {
1213 const double want = meshes[0].h[0] / a.hMin; // hMin ratio vs the cubic mesh
1214 const double got = runDropletAniso(meshes[0].n, meshes[0].h, R, true, 0).dtCap / a.dtCap;
1215 std::printf(" dt_sigma(cubic)/dt_sigma(this) = %.4f (h^{3/2} predicts %.4f)\n", got,
1216 std::pow(want, 1.5));
1217 CHECK(std::fabs(got - std::pow(want, 1.5)) < 1e-9 * std::pow(want, 1.5));
1218 }
1219 }
1220
1221 // (c) the COMPUTED curvature. Everything above is EXACT because a constant kappa makes the CSF
1222 // the discrete weighted gradient the projection annihilates; with the cascade's own kappa the
1223 // residual currents are the curvature error and nothing else. MEASURED: the box mesh costs a
1224 // constant factor, it does not diverge —
1225 //
1226 // spurious Ca cubic 2.499e-04 aspect 2 7.869e-04 (ratio 3.15)
1227 //
1228 // A factor ~3 at aspect 2, on a mesh whose sphere is resolved BETTER on the refined axis. The
1229 // likely mechanism is the V2.1 decision (the column direction is ordered by the INDEX normal,
1230 // which is the right frame for whether 7 cells can close a column but not for how much PHYSICAL
1231 // interface those 7 cells span) — so a stretched mesh routes more cells to the PV fallback than
1232 // its cell count suggests. NOT isolated here: confirming it needs the branch census per mesh,
1233 // and it is an accuracy characteristic rather than a defect, because the property this rung
1234 // rests on — the exactness identity above — is at machine zero on all three meshes.
1235 // The gate is therefore set from the measurement (4x) and the number is printed every run.
1236 const AnisoDrop cc = runDropletAniso(meshes[0].n, meshes[0].h, R, false, 20);
1237 const AnisoDrop ca = runDropletAniso(meshes[1].n, meshes[1].h, R, false, 20);
1238 const double caC = 0.1 * cc.maxU / 1.0, caA = 0.1 * ca.maxU / 1.0;
1239 std::printf(" computed kappa: spurious Ca cubic %.3e vs aspect-2 %.3e (ratio %.2f, gate 4)\n",
1240 caC, caA, caA / caC);
1241 CHECK(caA < 4.0 * caC);
1242 checkClose(ca.dp, ca.kappa, 5e-2 * ca.kappa, "anisotropic Young-Laplace (computed kappa)");
1243}
1244
1245namespace {
1246// --------------------------------------------------------------------------------------------
1247// SCALAR TRANSPORT + THE PHASE-CHANGE ENERGY PATH IN PHYSICAL UNITS (2026-10-02).
1248//
1249// PHYSICAL_UNITS_PLAN U3 converted the momentum and VoF setters and missed this surface:
1250// add_scalar's diffusivity, the phase-change densities, latent heat, conductivities, heat
1251// capacities, interfacial resistance and prescribed mass flux, the divergence source, the closures
1252// that read a scalar, and the getters that report them. The three gates below are the ones the
1253// first two ran without — each would have failed before the fix.
1254//
1255// A system is parameterised by its unit factors: the numerical value of a length is divided by
1256// `lam`, of a time by `tau`, of a mass by `mas` (lam = tau = mas = 1 is the reference system). A
1257// transported scalar (temperature, concentration) is never rescaled.
1258struct Units {
1259 double lam = 1.0, tau = 1.0, mas = 1.0;
1260 double len(double x) const { return x / lam; }
1261 double time(double t) const { return t / tau; }
1262 double rho(double r) const { return r * lam * lam * lam / mas; }
1263 double mu(double m) const { return m * lam * tau / mas; }
1264 double diff(double d) const { return d * tau / (lam * lam); }
1265 double vel(double u) const { return u * tau / lam; }
1266 double mdot(double m) const { return m * lam * lam * tau / mas; }
1267 double latent(double h) const { return h * tau * tau / (lam * lam); }
1268 double cond(double k) const { return k * tau * tau * tau / (mas * lam); }
1269 double rcp(double c) const { return c * lam * tau * tau / mas; }
1270 double pres(double p) const { return p * lam * tau * tau / mas; }
1271 double force(double f) const { return f * lam * lam * tau * tau / mas; } // per volume
1272 double area(double a) const { return a / (lam * lam); }
1273 double vol(double v) const { return v / (lam * lam * lam); }
1274 double energy(double e) const { return e * tau * tau / (mas * lam * lam); }
1275 double power(double p) const { return p * tau * tau * tau / (mas * lam * lam); }
1276 double rate(double r) const { return r * tau; } // 1/time
1277};
1278
1279double relDiff(const std::vector<double>& a, const std::vector<double>& b) {
1280 return relDiffScaled(a, b, 1.0);
1281}
1282double relScalar(double a, double b) { // |a - b| / |a| (or |b| when a == 0)
1283 const double den = std::fabs(a) > 0.0 ? std::fabs(a) : std::fabs(b);
1284 return den > 0.0 ? std::fabs(a - b) / den : 0.0;
1285}
1286
1291struct ScalarSine {
1292 std::vector<double> T, x;
1293 double amp = 0.0;
1294};
1295ScalarSine runScalarSine(int N, const std::array<double, 3>& ext, double D, double U, double dt,
1296 int nsteps, const Units& un, bool dtFirst, int scheme = 1) {
1297 const int ny = 4, nz = 4;
1299 s.setAdvection(false);
1300 s.setPhysicalDomain({un.len(ext[0]), un.len(ext[1]), un.len(ext[2])}, {0.0, 0.0, 0.0},
1301 {N, ny, nz});
1302 s.setRho(un.rho(1.0));
1303 if (dtFirst)
1304 s.setDt(un.time(dt));
1305 s.addScalar("T", un.diff(D), scheme, 200);
1306 if (!dtFirst)
1307 s.setDt(un.time(dt));
1308 s.setPressureGeometry(std::vector<double>((std::size_t)N * ny * nz, un.len(1.0)));
1309 const double k = 2.0 * M_PI / un.len(ext[0]);
1310 const std::vector<double> cx = s.cellCentres(0);
1311 std::vector<double> T0((std::size_t)N * ny * nz);
1312 for (int z = 0; z < nz; ++z)
1313 for (int y = 0; y < ny; ++y)
1314 for (int x = 0; x < N; ++x)
1315 T0[(std::size_t)x + (std::size_t)y * N + (std::size_t)z * N * ny] = std::sin(k * cx[x]);
1316 s.setField("T", T0);
1317 if (U != 0.0)
1318 s.setVelocity(0, std::vector<double>((std::size_t)N * ny * nz, un.vel(U)));
1319 for (int n = 0; n < nsteps; ++n)
1320 s.advanceScalars();
1321 ScalarSine r;
1322 r.T = s.getField("T");
1323 r.x = cx;
1324 double num = 0.0, den = 0.0; // projection on the (exactly discrete) sine mode
1325 for (int z = 0; z < nz; ++z)
1326 for (int y = 0; y < ny; ++y)
1327 for (int x = 0; x < N; ++x) {
1328 const double sx = std::sin(k * cx[x]);
1329 num += r.T[(std::size_t)x + (std::size_t)y * N + (std::size_t)z * N * ny] * sx;
1330 den += sx * sx;
1331 }
1332 r.amp = num / den;
1333 return r;
1334}
1335
1337double closureProbe(const Units& un, bool rhoFirst) {
1338 const int N = 8;
1340 s.setAdvection(false);
1341 s.setPhysicalDomain({un.len(1.0), un.len(1.0), un.len(1.0)}, {0.0, 0.0, 0.0}, {N, N, N});
1342 if (rhoFirst) {
1343 s.setRho(un.rho(998.0));
1344 s.setDt(un.time(1e-3));
1345 }
1346 s.setPressureGeometry(std::vector<double>((std::size_t)N * N * N, un.len(1.0)));
1347 s.addScalar("T", un.diff(1.4e-7), 1, 10);
1348 s.setField("T", std::vector<double>((std::size_t)N * N * N, 310.0));
1349 // buoyancy rho0 g beta (T - T0): rho0 a density, g an acceleration, beta 1/K
1350 s.setPropertyModel("force_z", peclet::flow::ClosureKind::BoussinesqForce, "T", "",
1351 {un.rho(998.0), un.force(9.81) / un.rho(1.0), 2.1e-4, 300.0});
1352 if (!rhoFirst) {
1353 s.setRho(un.rho(998.0));
1354 s.setDt(un.time(1e-3));
1355 }
1356 s.updateProperties();
1357 return s.getField("force_z")[0];
1358}
1359
1360void gateScalar() {
1361 std::printf("=== units_scalar_scale_invariance ===\n");
1362 const double TOL = 1e-13;
1363 const Units ref; // the reference system
1364 Units big; // a length unit 1000x, a time unit 0.037x, a mass unit 2.9e5x the reference: not
1365 // powers of ten, so no conversion factor is exact by accident
1366 big.lam = 1e3;
1367 big.tau = 3.7e-2;
1368 big.mas = 2.9e5;
1369 const int N = 32;
1370 const std::array<double, 3> ext{2.0, 4.0 * 2.0 / N, 4.0 * 2.0 / N};
1371 const double h = 2.0 / N, D = 0.05, dt = 0.5 * h * h / D;
1372 for (int adv = 0; adv < 2; ++adv) {
1373 const double U = adv ? 0.3 : 0.0; // Courant U dt/h = 0.19
1374 const ScalarSine a = runScalarSine(N, ext, D, U, dt, 40, ref, true);
1375 const ScalarSine b = runScalarSine(N, ext, D, U, dt, 40, big, false);
1376 const double rT = relDiff(a.T, b.T);
1377 std::printf(" %s: max|T_A - T_B|/max|T_A| = %.3e (B registers the scalar before set_dt)\n",
1378 adv ? "advection-diffusion (Koren)" : "diffusion ", rT);
1379 CHECK(maxAbs(a.T) > 0.1);
1380 CHECK(rT <= TOL);
1381 }
1382 // Closures reading a scalar: the Boussinesq force lands in force_z at the same INTERNAL value in
1383 // both systems and in both call orders (a scale pinned after registration re-derives it).
1384 const double fA = closureProbe(ref, true), fB = closureProbe(big, false);
1385 const double fC = closureProbe(big, true);
1386 std::printf(" Boussinesq force_z (internal): A %.17g B(rho/dt after) %.17g B(before) %.17g\n",
1387 fA, fB, fC);
1388 CHECK(std::fabs(fA) > 0.0);
1389 CHECK(relScalar(fA, fB) <= TOL);
1390 CHECK(relScalar(fA, fC) <= TOL);
1391}
1392
1393void gateScalarSine() {
1394 std::printf("=== units_scalar_sine_decay ===\n");
1395 // 1-D diffusion of a sine mode, T = sin(k x) exp(-D k^2 t), on a PHYSICAL box (Lx = 2,
1396 // D = 0.05) that is not the cell count, at two resolutions with the Fourier number D dt/h^2 = 1/4
1397 // fixed (so backward Euler's O(dt) is O(h^2) too), to t = 1/(D k^2). Two statements:
1398 // (a) the amplitude equals the EXACT backward-Euler/second-difference amplitude
1399 // r^n, r = 1/(1 + dt D 4 sin^2(k h/2)/h^2), to round-off — the physical D reaches the
1400 // operator exactly, metric included;
1401 // (b) it converges to exp(-D k^2 t) at second order.
1402 // Both on cubic cells and on BOX cells (h_y = h_z = h_x/2, so hRef = h_x/2 and the diffusing
1403 // axis carries w_x = 1/4).
1404 Units un;
1405 un.lam = 0.37; // a unit system that is neither cells nor the reference
1406 un.tau = 3.0;
1407 un.mas = 2.0;
1408 const double Lx = 2.0, D = 0.05, k = 2.0 * M_PI / Lx, tEnd = 1.0 / (D * k * k);
1409 for (int box = 0; box < 2; ++box) {
1410 double err[2] = {0, 0};
1411 for (int q = 0; q < 2; ++q) {
1412 const int N = q ? 32 : 16;
1413 const double h = Lx / N, dt0 = 0.25 * h * h / D;
1414 const int ns = (int)std::ceil(tEnd / dt0);
1415 const double dt = tEnd / ns;
1416 const double hyz = box ? 0.5 * h : h;
1417 const ScalarSine r = runScalarSine(N, {Lx, 4 * hyz, 4 * hyz}, D, 0.0, dt, ns, un, true);
1418 const double s2 = std::sin(0.5 * k * h);
1419 const double rr = 1.0 / (1.0 + dt * D * 4.0 * s2 * s2 / (h * h));
1420 const double discrete = std::pow(rr, ns), exact = std::exp(-D * k * k * tEnd);
1421 err[q] = std::fabs(r.amp - exact) / exact;
1422 const double eDisc = std::fabs(r.amp - discrete) / discrete;
1423 std::printf(
1424 " %s N=%2d (%3d steps): amplitude %.15f discrete %.15f (rel %.2e) exact %.15f "
1425 "(rel %.3e)\n",
1426 box ? "box " : "cubic", N, ns, r.amp, discrete, eDisc, exact, err[q]);
1427 CHECK(eDisc <= 1e-12);
1428 }
1429 const double order = std::log(err[0] / err[1]) / std::log(2.0);
1430 std::printf(" %s observed order %.3f\n", box ? "box " : "cubic", order);
1431 CHECK(order > 1.9 && order < 2.1);
1432 CHECK(err[1] < 1e-2);
1433 }
1434}
1435
1436// ---- the phase-change energy path ----------------------------------------------------------
1437struct PcRun {
1438 std::vector<double> C, T, u, p;
1439 double layer = 0.0;
1440 peclet::flow::IbmSolver::PhaseChangeDiagnostics d;
1441 peclet::flow::IbmSolver::PhaseChangeBudget b;
1442 double carryDep = 0.0, area = 0.0;
1443};
1444
1445double stefanLambdaU(double St) { // lambda e^{lambda^2} erf(lambda) = St/sqrt(pi)
1446 const double rhs = St / std::sqrt(M_PI);
1447 double a = 1e-8, b = 5.0;
1448 for (int i = 0; i < 200; ++i) {
1449 const double m = 0.5 * (a + b);
1450 ((m * std::exp(m * m) * std::erf(m) - rhs) > 0 ? b : a) = m;
1451 }
1452 return 0.5 * (a + b);
1453}
1454
1459PcRun runStefanUnits(int N, const Units& un, bool energy, bool scalesFirst) {
1460 const double St = 1.0, alpha = 1.0, x0p = 0.10, xep = 0.25, Fo = 0.5;
1461 const double lam = stefanLambdaU(St);
1462 const double t0 = (x0p / (2 * lam)) * (x0p / (2 * lam)) / alpha;
1463 const double te = (xep / (2 * lam)) * (xep / (2 * lam)) / alpha;
1464 const int ny = 4, nz = 4;
1465 const double D = alpha * N * N; // the reference system is the cell-unit one: h = 1
1466 const int ns = (int)std::llround((te - t0) / (Fo / D));
1467 const double dt = (te - t0) / ns;
1468 peclet::flow::IbmSolver s(N, ny, nz);
1469 s.setAdvection(false);
1470 s.setPhysicalDomain({un.len(N), un.len(ny), un.len(nz)}, {0.0, 0.0, 0.0}, {N, ny, nz});
1471 auto scales = [&] {
1472 s.setRho(un.rho(1.0));
1473 s.setMu(un.mu(1e-3));
1474 s.setDt(un.time(dt));
1475 };
1476 if (scalesFirst)
1477 scales();
1478 s.setDomainBc(0, 1, 0, 0, 0);
1479 s.setDomainBc(1, 1, 0, 0, 0);
1480 s.setPressureGeometry(std::vector<double>((std::size_t)N * ny * nz, un.len(1.0)));
1481 const double xg = x0p * N;
1482 std::vector<double> C((std::size_t)N * ny * nz, 0.0), T((std::size_t)N * ny * nz, 0.0);
1483 for (int z = 0; z < nz; ++z)
1484 for (int y = 0; y < ny; ++y)
1485 for (int x = 0; x < N; ++x) {
1486 const std::size_t i = (std::size_t)x + (std::size_t)y * N + (std::size_t)z * N * ny;
1487 C[i] = std::fmin(1.0, std::fmax(0.0, (x + 1) - xg));
1488 const double xp = (x + 0.5) / N;
1489 T[i] = xp < x0p ? 1.0 - std::erf(lam * xp / x0p) / std::erf(lam) : 0.0;
1490 }
1491 s.enableVof();
1492 s.setVof(C);
1493 s.addScalar("T", un.diff(D), 1, 60);
1494 s.setScalarBc("T", 0, 2, 1.0);
1495 s.setScalarBc("T", 1, 2, 0.0);
1496 s.setField("T", T);
1497 s.enablePhaseChange(un.rho(1.0), un.rho(1.0), un.latent(1.0));
1498 // rho c_p = 1 in the reference system, so k = rho c_p D = D
1499 s.setPhaseChangeThermal("T", 0.0, un.cond(D), un.cond(D), 0.0);
1500 if (energy) {
1501 s.setPhaseChangeEnergy(un.rcp(1.0), un.rcp(1.0));
1502 s.setPhaseChangeEnergyOrder(2);
1503 s.setPhaseChangeBudget(true);
1504 }
1505 if (!scalesFirst)
1506 scales();
1507 for (int n = 0; n < ns; ++n) {
1508 s.applyPhaseChange(un.time(dt));
1509 s.advanceScalars();
1510 }
1511 PcRun r;
1512 r.C = s.getVof();
1513 r.T = s.getField("T");
1514 double sum = 0;
1515 for (double q : r.C)
1516 sum += q;
1517 r.layer = un.len(N - sum / (ny * nz));
1518 r.d = s.phaseChangeDiagnostics();
1519 if (energy) {
1520 r.b = s.phaseChangeBudgetValues();
1521 r.carryDep = s.phaseChangeCarryDeposited();
1522 }
1523 r.area = s.vofInterfaceArea();
1524 return r;
1525}
1526
1530PcRun runSuckingUnits(int N, int nsteps, const Units& un) {
1531 const double ratio = 10.0, ja = 1.0, alpha_l = 1.0, x0p = 0.10, Fo = 0.5, cfl = 0.2;
1532 const double rr = 1.0 / ratio;
1533 double b = ja / std::sqrt(M_PI);
1534 for (int i = 0; i < 300; ++i) {
1535 const double g = b * rr;
1536 b = ja * std::exp(-g * g) / (std::sqrt(M_PI) * std::erfc(g));
1537 }
1538 const double t0 = (x0p / (2 * b)) * (x0p / (2 * b)) / alpha_l;
1539 const int ny = 4, nz = 4;
1540 const double al = alpha_l * N * N, rho_l = 1.0, rho_v = rr, cpl = 1.0;
1541 const double k_l = al * rho_l * cpl, k_v = k_l / ratio, dT = 1.0;
1542 const double h_lv = rho_l * cpl * dT / (rho_v * ja);
1543 peclet::flow::IbmSolver s(N, ny, nz);
1544 s.setAdvection(false);
1545 s.setPhysicalDomain({un.len(N), un.len(ny), un.len(nz)}, {0.0, 0.0, 0.0}, {N, ny, nz});
1546 s.setRho(un.rho(rho_l));
1547 s.setMu(un.mu(1e-3));
1548 s.setDomainBc(0, 1, 0, 0, 0);
1549 s.setDomainBc(1, 3, 0, 0, 0);
1550 s.setPressureGeometry(std::vector<double>((std::size_t)N * ny * nz, un.len(1.0)));
1551 const double X0 = x0p * N;
1552 std::vector<double> C((std::size_t)N * ny * nz, 0.0), T((std::size_t)N * ny * nz, 0.0);
1553 for (int z = 0; z < nz; ++z)
1554 for (int y = 0; y < ny; ++y)
1555 for (int x = 0; x < N; ++x) {
1556 const std::size_t i = (std::size_t)x + (std::size_t)y * N + (std::size_t)z * N * ny;
1557 C[i] = std::fmin(1.0, std::fmax(0.0, (x + 1) - X0));
1558 const double xp = (x + 0.5) / N;
1559 if (xp > x0p) {
1560 const double sv = xp / (2 * std::sqrt(alpha_l * t0)) - b * (1.0 - rr);
1561 T[i] = dT - dT * std::erfc(sv) / std::erfc(b * rr);
1562 }
1563 }
1564 s.enableVof();
1565 s.setVof(C);
1566 s.setPropertyModel("rho", peclet::flow::ClosureKind::LinearMix, "C", "",
1567 {un.rho(rho_v), un.rho(rho_l - rho_v)});
1568 s.setPressureFcg(true, 4000, 1e-10);
1569 s.addScalar("T", un.diff(al), 1, 60);
1570 s.setScalarBc("T", 0, 2, 0.0);
1571 auto farT = [&](double tt) {
1572 const double sv = 1.0 / (2 * std::sqrt(alpha_l * tt)) - b * (1.0 - rr);
1573 return dT - dT * std::erfc(sv) / std::erfc(b * rr);
1574 };
1575 s.setScalarBc("T", 1, 2, farT(t0));
1576 s.setField("T", T);
1577 s.enablePhaseChange(un.rho(rho_v), un.rho(rho_l), un.latent(h_lv));
1578 s.setPhaseChangeThermal("T", 0.0, un.cond(k_v), un.cond(k_l), 0.0);
1579 s.setPhaseChangeEnergy(un.rcp(rho_v * cpl), un.rcp(rho_l * cpl));
1580 double tcur = t0;
1581 for (int n = 0; n < nsteps; ++n) {
1582 const double Xd = b * std::sqrt(alpha_l / tcur) * N;
1583 const double dt = std::fmin(cfl / std::fmax(Xd * (1.0 - rr), 1e-30), Fo / al);
1584 s.setDt(un.time(dt)); // the FIRST one pins tRef: every constant above is re-derived
1585 s.setScalarBc("T", 1, 2, farT(tcur + dt));
1586 s.step();
1587 tcur += dt;
1588 }
1589 PcRun r;
1590 r.C = s.getVof();
1591 r.T = s.getField("T");
1592 r.u = s.getVelocity(0);
1593 r.p = s.getPressure();
1594 r.d = s.phaseChangeDiagnostics();
1595 return r;
1596}
1597
1598void gatePhaseChange() {
1599 std::printf("=== units_phase_change_scale_invariance ===\n");
1600 const double TOL = 1e-12;
1601 const Units ref;
1602 Units big; // as in gateScalar
1603 big.lam = 1e3;
1604 big.tau = 3.7e-2;
1605 big.mas = 2.9e5;
1606 // (1) kinematic Stefan, constant-diffusivity energy path (P1'), and (2) with the consistent
1607 // energy transport + the second-order package + the budget instruments.
1608 for (int energy = 0; energy < 2; ++energy) {
1609 const PcRun a = runStefanUnits(32, ref, energy != 0, true);
1610 const PcRun b = runStefanUnits(32, big, energy != 0, false);
1611 const double rC = relDiff(a.C, b.C), rT = relDiff(a.T, b.T);
1612 const double rL = relScalar(big.len(a.layer), b.layer);
1613 const double rM = relScalar(big.mdot(a.d.mdotMean), b.d.mdotMean);
1614 const double rA = relScalar(big.area(a.d.area), b.d.area);
1615 const double rAv = relScalar(big.area(a.area), b.area);
1616 const double rV = relScalar(big.vol(a.d.removedVolume), b.d.removedVolume);
1617 std::printf(
1618 " Stefan (%s): C %.2e T %.2e layer %.2e mdot_mean %.2e interface_area %.2e "
1619 "vof_interface_area %.2e removed_volume %.2e\n",
1620 energy ? "rho c_p T, order 2" : "constant D ", rC, rT, rL, rM, rA, rAv, rV);
1621 CHECK(maxAbs(a.T) > 0.1 && a.d.interfaceCells > 0);
1622 CHECK(rC <= TOL && rT <= TOL && rL <= TOL);
1623 CHECK(rM <= TOL && rA <= TOL && rAv <= TOL && rV <= TOL);
1624 if (energy) {
1625 const double rq = relScalar(big.power(a.d.qOperator), b.d.qOperator);
1626 const double rh = relScalar(big.energy(a.b.hOpen), b.b.hOpen);
1627 const double rg = relScalar(big.power(a.b.qGfm), b.b.qGfm);
1628 std::printf(" q_operator %.2e budget h_open %.2e q_gfm %.2e (carry %.3e vs %.3e)\n", rq,
1629 rh, rg, big.energy(a.carryDep), b.carryDep);
1630 CHECK(a.d.qOperator != 0.0 && a.b.qGfm != 0.0);
1631 CHECK(rq <= TOL && rh <= TOL && rg <= TOL);
1632 CHECK(std::fabs(big.energy(a.carryDep) - b.carryDep) <=
1633 TOL * std::fabs(big.energy(a.b.hOpen)) + 1e-300);
1634 }
1635 }
1636 // (3) the full step(): density ratio 10, the rho closure, the outflow face, the consistent
1637 // energy transport, every constant set before the first set_dt pins tRef.
1638 {
1639 const PcRun a = runSuckingUnits(32, 12, ref);
1640 const PcRun b = runSuckingUnits(32, 12, big);
1641 const double rC = relDiff(a.C, b.C), rT = relDiff(a.T, b.T);
1642 const double ru = relDiffScaled(b.u, a.u, big.vel(1.0)),
1643 rp = relDiffScaled(b.p, a.p, big.pres(1.0));
1644 const double rM = relScalar(big.mdot(a.d.mdotMean), b.d.mdotMean);
1645 std::printf(" sucking interface, 12 full steps: C %.2e T %.2e u %.2e p %.2e mdot %.2e\n",
1646 rC, rT, ru, rp, rM);
1647 CHECK(maxAbs(a.u) > 0.0);
1648 // measured 1.8e-15 / 7.8e-16 / 3.2e-15 / 1.7e-13 / 9.9e-16 on host-openmp: the pressure is the
1649 // FCG's own round-off (rtol 1e-10, the same iteration count in both systems)
1650 CHECK(rC <= 1e-11 && rT <= 1e-11 && ru <= 1e-11 && rp <= 1e-11 && rM <= 1e-11);
1651 }
1652 // (4) vof_interface_area on BOX cells: a plane x = x0 across a (Ly, Lz) box has area Ly*Lz in
1653 // every mode. Before 1c80540 the 'plic' mode summed the unit-cube polygon (one per column of
1654 // cells), i.e. ny*nz*hRef^2 = half the area here.
1655 {
1656 const int nx = 16, ny = 32, nz = 8;
1657 const double Lx = 1.6, Ly = 0.8, Lz = 0.4; // h = (0.1, 0.025, 0.05): three spacings
1658 peclet::flow::IbmSolver s(nx, ny, nz);
1659 s.setAdvection(false);
1660 s.setPhysicalDomain({Lx, Ly, Lz}, {0.0, 0.0, 0.0}, {nx, ny, nz});
1661 s.setRho(1.0);
1662 s.setDt(1e-3);
1663 s.setPressureGeometry(std::vector<double>((std::size_t)nx * ny * nz, 1.0));
1664 s.enableVof();
1665 std::vector<double> C((std::size_t)nx * ny * nz);
1666 for (std::size_t i = 0; i < C.size(); ++i)
1667 C[i] = std::fmin(1.0, std::fmax(0.0, (double)(i % nx) + 1.0 - 5.3));
1668 s.setVof(C);
1669 const double aDefault = s.vofInterfaceArea();
1670 s.setPhaseChangeArea(peclet::flow::vof::kAreaPlic);
1671 const double aPlic = s.vofInterfaceArea();
1672 std::printf(
1673 " box-cell plane: vof_interface_area %.15g (default sheet), %.15g (plic) vs %.15g\n",
1674 aDefault, aPlic, Ly * Lz);
1675 CHECK(relScalar(aPlic, Ly * Lz) <= 1e-12);
1676 CHECK(relScalar(aDefault, Ly * Lz) <= 1e-12);
1677 }
1678 // (5) the refusals. A prescribed mass flux is written into a field once, so under a physical
1679 // domain it needs the scales pinned; and the operator mdot without the consistent energy
1680 // transport has no heat capacity to be physical with.
1681 {
1682 peclet::flow::IbmSolver s(16, 4, 4);
1683 s.setAdvection(false);
1684 s.setPhysicalDomain({1.0, 0.25, 0.25}, {0.0, 0.0, 0.0}, {16, 4, 4});
1685 s.setPressureGeometry(std::vector<double>(16 * 4 * 4, 1.0));
1686 s.enablePhaseChange(1.0, 1000.0, 2.26e6);
1687 bool threw = false;
1688 try {
1689 s.setMassFluxUniform(1e-3);
1690 } catch (const std::runtime_error&) {
1691 threw = true;
1692 }
1693 CHECK(threw);
1694 s.setRho(1000.0);
1695 s.setDt(1e-4);
1696 s.setMassFluxUniform(1e-3); // now fine
1697 const double kInt = s.unitScales().mdotToInt();
1698 CHECK(relScalar(s.getField("mdot")[0], 1e-3 * kInt) <= 1e-15);
1699 s.addScalar("T", 1e-7, 1, 10);
1700 s.setPhaseChangeThermal("T", 373.15, 0.025, 0.6, 0.0);
1701 s.setPhaseChangeMdotOperator(true);
1702 std::vector<double> C(16 * 4 * 4, 0.0);
1703 for (int i = 0; i < 16 * 4 * 4; ++i)
1704 C[(std::size_t)i] = std::fmin(1.0, std::fmax(0.0, (i % 16) + 1 - 6.5));
1705 s.setVof(C);
1706 threw = false;
1707 try {
1708 s.applyPhaseChange(1e-4);
1709 } catch (const std::runtime_error&) {
1710 threw = true;
1711 }
1712 std::printf(" refusals: set_mass_flux before the scales, operator mdot without rho c_p: %s\n",
1713 threw ? "both raise" : "MISSING");
1714 CHECK(threw);
1715 }
1716}
1717} // namespace
1718
1719int main(int argc, char** argv) {
1720 const std::string gate = argc > 1 ? argv[1] : "identity";
1721 Kokkos::initialize(argc, argv);
1722 {
1723 if (gate == "identity")
1724 gateIdentity();
1725 else if (gate == "scale")
1726 gateScale();
1727 else if (gate == "vof")
1728 gateVofSigma();
1729 else if (gate == "aniso")
1730 gateAnisoPoiseuille();
1731 else if (gate == "sphere")
1732 gateAnisoSphere();
1733 else if (gate == "tgv")
1734 gateAnisoTgv();
1735 else if (gate == "vofaniso")
1736 gateVofAniso();
1737 else if (gate == "scalar")
1738 gateScalar();
1739 else if (gate == "scalarsine")
1740 gateScalarSine();
1741 else if (gate == "phasechange")
1742 gatePhaseChange();
1743 else {
1744 std::fprintf(stderr,
1745 "usage: test_units [identity|scale|vof|aniso|sphere|tgv|vofaniso|scalar|"
1746 "scalarsine|phasechange]\n");
1747 ++failures;
1748 }
1749 }
1750 Kokkos::finalize();
1751 if (failures) {
1752 std::fprintf(stderr, "%d failure(s)\n", failures);
1753 return 1;
1754 }
1755 std::printf("OK\n");
1756 return 0;
1757}
flow — host-facing Kokkos IBM Navier-Stokes solver (drop-in flow-style API).
y
Definition cmp.py:24
energy(vel, rho)
Definition gsv9.py:135
@ kAreaPlic
rung P0/P1: plicArea on the MYC normal. The old numbers.
Solver< Staggered > IbmSolver
int m
Definition post.py:35
tuple rc
Definition post.py:37
t
Definition post.py:21
vel
Definition prof.py:67
exact
Definition summ.py:10
static constexpr int N
static double maxAbs(const std::vector< double > &a)
static constexpr double DT
static constexpr double RHO
static constexpr double MU
static constexpr double F
static double relErr(const std::vector< double > &a, const std::vector< double > &b)
static int run(int bc)
static double X0()
static constexpr double U
static constexpr double R
double sphereFracBox(double cx, double cy, double cz, double R, double x0, double y0, double z0, const double h[3], int sub=24)
Exact liquid fraction of the box cell [x0,x0+hx] x ... inside a sphere: midpoint-subsampled in x and ...
AnisoDrop runDropletAniso(const int n[3], const double h[3], double R, bool constantKappa, int steps)
n cells per axis on a box mesh of cell size h (physical). R is the PHYSICAL radius.
void gateVofAniso()
#define CHECK(cond)
static constexpr double FX
static constexpr double SIGMA