flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_pressure_chebyshev_bounds.cpp
Go to the documentation of this file.
1// ctest `pressure_chebyshev_bounds`: the life cycle of the Chebyshev pressure driver's spectral
2// bounds (doc/mg_depth_policy_log.md 2026-10-30, the A3 review fixes).
3//
4// Case: the hydrostatic column of `pressure_wallbounded` (case B: 24 x 24 x 16, z walls, constant
5// density, uniform gravity), Chebyshev driver. Constant coefficients, so the bounds are estimated
6// once and kept across steps.
7//
8// 1. A V-cycle setter that CHANGES the V-cycle (set_pressure_vcycle_symmetric,
9// set_pressure_line_relaxation) invalidates the kept bounds: the next step re-estimates them
10// (one more estimate), and still converges. Setting the value already in force is inert (no
11// estimate). Before the fix the kept bounds of the old V-cycle survived the toggle.
12// 2. The cold-bounds growth guard, symmetric and trilinear V-cycle: the test hook scales the
13// first estimate's bounds by 0.4 (lambda_max far below the top, so the solve diverges); the
14// guard trips once, the bounds are re-estimated with the bracketing margin, the step
15// converges, and later steps keep the new bounds without tripping. Without the hook the
16// guard never trips (case 1 also checks 0 trips).
17// 3. The bounds ratchet (DECISIONS_GPU.log 2026-10-30 "A3 finish" (b)): after a trip, the
18// bounds are dropped as a coefficient rebuild drops them and the warm re-estimate is scaled
19// by 0.4 again. The ratchet keeps lambda_max at the trip's bracketed value, so the warm
20// solve neither trips its 3-iteration guard nor restarts cold. Without the ratchet the
21// warm bounds are the scaled ones and the step restarts (negative control: 1 restart).
22#include <cmath>
23#include <cstdio>
24#include <Kokkos_Core.hpp>
25#include <vector>
26
27#include "flow_ibm.hpp"
28
29namespace {
30int failures = 0;
31#define CHECK(cond) \
32 do { \
33 if (!(cond)) { \
34 std::fprintf(stderr, "CHECK failed: %s\n at %s:%d\n", #cond, __FILE__, __LINE__); \
35 ++failures; \
36 } \
37 } while (0)
38
39constexpr int NX = 24, NY = 24, NZ = 16;
40constexpr int LEVELS = 4;
41constexpr int MAXIT = 200;
42constexpr double RTOL = 1e-8;
43constexpr double MU = 0.01, DT = 1.0, GRAV = 0.1;
44constexpr double DIV_TOL = 1e-8; // relative to GRAV * DT, as pressure_wallbounded
45
46void setUp(peclet::flow::IbmSolver& s) {
47 s.setRho(1.0);
48 s.setMu(MU);
49 s.setDt(DT);
50 s.setAdvection(false);
51 s.setDomainBc(4, 1, 0, 0, 0);
52 s.setDomainBc(5, 1, 0, 0, 0);
53 s.setPressureLevels(LEVELS);
54 s.setPressureGeometry(std::vector<double>((std::size_t)NX * NY * NZ, 10.0)); // all fluid
55 s.setBodyForce(0.0, 0.0, -GRAV);
56 s.setPressureChebyshev(true, MAXIT, RTOL);
57}
58
59// One step; the iteration count must stay below the cap and the projection converged.
60void stepChecked(peclet::flow::IbmSolver& s, const char* what) {
61 s.step();
62 const long it = s.lastPressureIterations();
63 const double div = s.maxOpenDivergence() / (GRAV * DT);
64 std::printf(" %-34s its %3ld div/u %.2e estimates %ld\n", what, it, div,
65 s.numPressureEigenEstimates());
66 CHECK(it < MAXIT);
67 CHECK(div < DIV_TOL);
68}
69
70// 1. A V-cycle change invalidates the kept bounds; the same value does not.
71void toggleCase() {
72 std::printf(" 1. V-cycle setters invalidate the kept bounds\n");
74 setUp(s);
75 stepChecked(s, "step 1");
76 const long n1 = s.numPressureEigenEstimates();
77 stepChecked(s, "step 2 (kept bounds)");
78 stepChecked(s, "step 3 (kept bounds)");
79 CHECK(s.numPressureEigenEstimates() == n1); // constant coefficients: estimated once
80 struct Toggle {
81 const char* what;
82 bool lineRelax; // which setter
83 bool value;
84 long expect; // estimates added by the next step
85 };
86 const Toggle toggles[] = {{"symmetric on (unchanged)", false, true, 0},
87 {"symmetric off", false, false, 1},
88 {"symmetric off (unchanged)", false, false, 0},
89 {"symmetric on", false, true, 1},
90 {"line relaxation on (unchanged)", true, true, 0},
91 {"line relaxation off", true, false, 1},
92 {"line relaxation on", true, true, 1}};
93 for (const Toggle& t : toggles) {
94 const long n0 = s.numPressureEigenEstimates();
95 if (t.lineRelax)
96 s.setPressureLineRelaxation(t.value);
97 else
98 s.setPressureVcycleSymmetric(t.value);
99 stepChecked(s, t.what);
100 CHECK(s.numPressureEigenEstimates() - n0 == t.expect);
101 }
102 CHECK(s.numPressureChebyshevGrowthTrips() == 0);
103}
104
105// 2. The growth guard on deliberately too-small bounds.
106void guardCase(bool symmetric) {
107 std::printf(" 2. growth guard, %s V-cycle, first estimate x 0.4\n",
108 symmetric ? "symmetric" : "trilinear");
110 setUp(s);
111 s.setPressureVcycleSymmetric(symmetric);
112 s.debugSetPressureEigenScale(0.4);
113 stepChecked(s, "step 1 (scaled bounds)");
114 CHECK(s.numPressureChebyshevGrowthTrips() == 1);
115 CHECK(s.numPressureEigenEstimates() == 2); // the scaled one + the bracketed re-estimate
116 stepChecked(s, "step 2");
117 stepChecked(s, "step 3");
118 std::printf(" growth trips %ld\n", s.numPressureChebyshevGrowthTrips());
119 CHECK(s.numPressureChebyshevGrowthTrips() == 1);
120 CHECK(s.numPressureEigenEstimates() == 2);
121}
122
123// 3. The ratchet: a trip's bracketed lambda_max survives the warm re-estimate.
124void ratchetCase() {
125 std::printf(" 3. bounds ratchet: trip, drop the bounds, warm re-estimate x 0.4\n");
127 setUp(s);
128 s.debugSetPressureEigenScale(0.4);
129 stepChecked(s, "step 1 (scaled bounds, trips)");
130 CHECK(s.numPressureChebyshevGrowthTrips() == 1);
131 s.debugDropPressureBounds();
132 s.debugSetPressureEigenScale(0.4);
133 const long n0 = s.numPressureEigenEstimates();
134 stepChecked(s, "step 2 (warm x 0.4, ratcheted)");
135 std::printf(" estimates +%ld restarts %ld growth trips %ld\n",
136 s.numPressureEigenEstimates() - n0, s.numPressureChebyshevRestarts(),
137 s.numPressureChebyshevGrowthTrips());
138 CHECK(s.numPressureEigenEstimates() - n0 == 1); // the warm one only
139 CHECK(s.numPressureChebyshevRestarts() == 0);
140 CHECK(s.numPressureChebyshevGrowthTrips() == 1);
141}
142} // namespace
143
144int main(int argc, char** argv) {
145 Kokkos::initialize(argc, argv);
146 {
147 std::printf("Chebyshev pressure bounds: %dx%dx%d, %d levels, cap %d, rtol %.0e\n", NX, NY, NZ,
148 LEVELS, MAXIT, RTOL);
149 toggleCase();
150 guardCase(true);
151 guardCase(false);
152 ratchetCase();
153 if (failures == 0)
154 std::printf("PASS: pressure_chebyshev_bounds\n");
155 else
156 std::fprintf(stderr, "FAIL: %d check(s)\n", failures);
157 }
158 Kokkos::finalize();
159 return failures ? 1 : 0;
160}
flow — host-facing Kokkos IBM Navier-Stokes solver (drop-in flow-style API).
Solver< Staggered > IbmSolver
void setUp(CutcellMG &mg, const Problem &P, int mode)
t
Definition post.py:21
static constexpr int NY
static constexpr int NX
static constexpr int NZ
static constexpr double GRAV
static constexpr double DT
static constexpr double RTOL
static constexpr int MAXIT
static double value(int gx, int gy, int gz)
static constexpr double MU
static constexpr int LEVELS
#define CHECK(cond)