flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_pressure_restart.cpp
Go to the documentation of this file.
1
24#include <cmath>
25#include <cstdio>
26#include <cstring>
27#include <Kokkos_Core.hpp>
28#include <memory>
29#include <stdexcept>
30#include <string>
31#include <vector>
32
33#include "flow_ibm.hpp"
34#include "mg_fp32_problems.hpp"
35
36using namespace peclet::flow;
37using namespace peclet_flow_fp32_problems;
38
39namespace {
40
41int fails = 0;
42void check(bool ok, const std::string& what) {
43 std::printf(" %-96s %s\n", what.c_str(), ok ? "PASS" : "FAIL");
44 if (!ok)
45 ++fails;
46}
47
48// Does 'auto' run the FP32 V-cycle on this backend? WO-A: on device backends only (host 'auto' is
49// 'fp64', so there the fault is inert and nothing restarts).
50const bool kAutoFp32 = !CutcellMG::kHostMemory;
51
52struct Run {
53 int it = -1, restarts = 0, discarded = -1;
54 bool failed = false, fp32 = false, threw = false;
55 std::vector<double> x;
56};
57bool sameX(const Run& a, const Run& b) {
58 return a.x.size() == b.x.size() &&
59 std::memcmp(a.x.data(), b.x.data(), a.x.size() * sizeof(double)) == 0;
60}
61
62// One solve of P on `mg` (driver 0 = PCG, 1 = FCG) from x = 0 with the fault armed for it.
63Run solveWith(CutcellMG& mg, const Problem& P, int driver, int kind, int at) {
64 const std::vector<double> hb = rhsOf(P);
65 const std::size_t n = hb.size();
66 CCField b = toDevice(hb, "b"), x("x", n), r("r", n), p("p", n), z("z", n), zp("zp", n),
67 Ap("Ap", n);
68 mg.debugSetFp32Fault(kind, at);
69 Run out;
70 try {
71 out.it = driver == 0 ? mg.solvePCG(b, x, r, p, z, Ap, 200, 1e-10, 2, 2, 12)
72 : mg.solveFCG(b, x, r, p, z, zp, Ap, 200, 1e-10, 2, 2, 12);
73 } catch (const std::runtime_error&) {
74 out.threw = true;
75 }
76 mg.debugSetFp32Fault(0, 0);
77 out.failed = mg.lastSolveFailed();
78 out.fp32 = mg.lastVcycleFp32();
79 out.restarts = mg.vcycleRestarts();
80 out.discarded = mg.lastDiscardedIters();
81 out.x = toHost(x);
82 return out;
83}
84Run fresh(const Problem& P, int mode, int driver, int kind = 0, int at = 0, bool strict = false) {
85 CutcellMG mg;
86 setUp(mg, P, mode);
87 mg.setStrictPressure(strict);
88 return solveWith(mg, P, driver, kind, at);
89}
90std::string itStr(const Run& r) {
91 return std::to_string(r.it) + (r.failed ? " failed" : "") + (r.fp32 ? " fp32" : " fp64");
92}
93
94// (a) the restarted solve equals the 'fp64' cold solve.
95bool restartedIsFp64(const Run& a, const Run& ref64) {
96 return sameX(a, ref64) && a.it == ref64.it && a.failed == ref64.failed && !a.fp32 &&
97 a.restarts == 1 && !a.threw;
98}
99
100void levelChecks(const Problem& P) {
101 for (int driver : {0, 1}) {
102 const std::string tag = P.name + (driver == 0 ? " PCG" : " FCG");
103 const Run ref64 = fresh(P, CutcellMG::kVcycleFp64, driver);
104 const Run auto0 = fresh(P, CutcellMG::kVcycleAuto, driver);
105 std::printf(" %s: fp64 %s, auto %s\n", tag.c_str(), itStr(ref64).c_str(),
106 itStr(auto0).c_str());
107 if (!kAutoFp32) {
108 // host 'auto' is 'fp64' (WO-A): no FP32 attempt, so the fault is inert and nothing restarts
109 const Run a = fresh(P, CutcellMG::kVcycleAuto, driver, 1, 3);
110 check(sameX(a, ref64) && a.it == ref64.it && !a.fp32 && a.restarts == 0,
111 tag + ": host 'auto' == 'fp64', the fault inert, no restart (" + itStr(a) + ")");
112 } else {
113 // cyl and pack at ratio 1e4: the FP32 attempt itself caps or breaks down (so does FP64, the
114 // coarsening limit), i.e. the unfaulted 'auto' solve is a NATURAL restart
115 if (auto0.restarts == 1)
116 check(restartedIsFp64(auto0, ref64),
117 tag +
118 ": 'auto' unfaulted: the FP32 attempt does not converge -> natural restart == "
119 "fp64 cold (discarded " +
120 std::to_string(auto0.discarded) + ")");
121 else
122 check(auto0.fp32 && auto0.restarts == 0,
123 tag + ": 'auto' unfaulted runs the FP32 V-cycle, converges, no restart");
124 // (a)
125 const Run a = fresh(P, CutcellMG::kVcycleAuto, driver, 1, 3);
126 check(restartedIsFp64(a, ref64), tag + ": (a) kind 1 at 3 -> restart == fp64 cold (" +
127 itStr(a) + ", discarded " + std::to_string(a.discarded) +
128 ")");
129 // (b)
130 const Run b = fresh(P, CutcellMG::kVcycleAuto, driver, 2, 2);
131 check(restartedIsFp64(b, ref64) && b.discarded == 2,
132 tag + ": (b) kind 2 at 2 -> restart == fp64 cold, discarded " +
133 std::to_string(b.discarded));
134 // (e)
135 const Run e = fresh(P, CutcellMG::kVcycleAuto, driver, 1, 1, /*strict=*/true);
136 check(restartedIsFp64(e, ref64),
137 tag + ": (e) strict + 'auto' + kind 1 at 1: no throw, restart == fp64 cold");
138 // (f)
139 CutcellMG mg;
141 const Run f0 = solveWith(mg, P, driver, 1, 3);
142 const Run f1 = solveWith(mg, P, driver, 0, 0);
143 check(restartedIsFp64(f0, ref64) && sameX(f1, ref64) && f1.it == ref64.it && !f1.fp32 &&
144 f1.restarts == 1,
145 tag + ": (f) after a restart 'auto' stays FP64, no second restart");
147 const Run f2 = solveWith(mg, P, driver, 0, 0);
148 check(sameX(f2, auto0) && f2.it == auto0.it && f2.fp32 == auto0.fp32 &&
149 f2.restarts == 1 + auto0.restarts,
150 tag + ": (f) setVcyclePrecision(auto) re-arms: bitwise a fresh 'auto' solve");
151 }
152 // (c)
153 const Run c1 = fresh(P, CutcellMG::kVcycleFp32, driver, 1, 3);
154 const Run c2 = fresh(P, CutcellMG::kVcycleFp32, driver, 2, 2);
155 check(c1.restarts == 0 && c1.failed && c1.fp32 && c2.restarts == 0 && c2.it == 2 && c2.fp32,
156 tag + ": (c) forced 'fp32': no restart; kind 1 failed (" + itStr(c1) + "), kind 2 " +
157 itStr(c2));
158 // (d)
159 const Run d1 = fresh(P, CutcellMG::kVcycleFp64, driver, 1, 3);
160 const Run d2 = fresh(P, CutcellMG::kVcycleFp64, driver, 2, 2);
161 check(sameX(d1, ref64) && d1.it == ref64.it && sameX(d2, ref64) && d2.it == ref64.it &&
162 d1.restarts == 0 && d2.restarts == 0,
163 tag + ": (d) 'fp64' with either fault == no fault");
164 // (e), forced
165 const Run e2 = fresh(P, CutcellMG::kVcycleFp32, driver, 1, 1, /*strict=*/true);
166 check(e2.threw, tag + ": (e) strict + forced 'fp32' + kind 1 at 1 throws");
167 }
168}
169
170// --- (g): the assembled VoF case ---------------------------------------------------------------
171std::size_t idx3(int x, int y, int z, int n) {
172 return (std::size_t)x + (std::size_t)y * n + (std::size_t)z * (std::size_t)n * n;
173}
174std::unique_ptr<IbmSolver> bubble(int vprec, int guessMode) {
175 const int n = 32;
176 const double rhoG = 1.0, rhoL = 100.0, r = 7.0, c = 0.5 * n;
177 const std::size_t nc = (std::size_t)n * n * n;
178 std::vector<double> C(nc), u(nc), v(nc, 0.0), w(nc, 0.0);
179 for (int z = 0; z < n; ++z)
180 for (int y = 0; y < n; ++y)
181 for (int x = 0; x < n; ++x) {
182 C[idx3(x, y, z, n)] = vof::sphereCellFraction(c, c, c, r, x, y, z, 1.0, 4);
183 u[idx3(x, y, z, n)] = 0.2 * std::sin(2.0 * M_PI * x / n) * std::cos(2.0 * M_PI * y / n);
184 }
185 for (double& cc : C)
186 cc = 1.0 - cc; // liquid (C = 1) around a gas bubble
187 auto s = std::make_unique<IbmSolver>(n, n, n);
188 s->setAdvection(false);
189 s->setRho(rhoL);
190 s->setMu(0.01);
191 s->setDt(0.2);
192 s->setPressureGeometry(std::vector<double>(nc, 10.0));
193 s->enableVof();
194 s->setVof(C);
195 s->setPropertyModel("rho", ClosureKind::LinearMix, "C", "", {rhoG, rhoL - rhoG});
196 s->enableVofMomentum(rhoG, rhoL);
197 s->setPressurePcg(true, 200, 1e-10); // last: the property model re-selects Chebyshev
198 s->setPressureVcyclePrecision(vprec);
199 s->setPressureInitialGuess(guessMode, 2);
200 s->uploadVelocity(u, v, w);
201 return s;
202}
203void assembledCheck() {
204 constexpr int kAuto = 0, kFp64 = 1, kZero = 1, kProjected = 2;
205 auto A = bubble(kAuto, kProjected), B = bubble(kAuto, kProjected);
206 for (int k = 1; k <= 3; ++k) {
207 A->step();
208 B->step();
209 }
210 const bool sameState = A->getPressure() == B->getPressure();
211 const int histA = A->pressureGuessCount();
212 // step 4 of A: the projection's FP32 attempt fails on its 3rd V-cycle and is restarted
213 A->debugSetPressureFp32Fault(1, 3);
214 A->step();
215 A->debugSetPressureFp32Fault(0, 0);
216 // step 4 of B: 'fp64' + 'zero' from the same state (host after WO-A: B unchanged, unfaulted)
217 if (kAutoFp32) {
218 B->setPressureVcyclePrecision(kFp64);
219 B->setPressureInitialGuess(kZero, 2);
220 }
221 B->step();
222 const std::vector<double> pA = A->getPressure(), pB = B->getPressure();
223 const bool same =
224 pA.size() == pB.size() && std::memcmp(pA.data(), pB.data(), pA.size() * sizeof(double)) == 0;
225 std::printf(
226 " bubble: history %d before step 4 (%s), %d after; iterations A %ld B %ld; "
227 "restarts A %d\n",
228 histA, sameState ? "A == B" : "A != B", A->pressureGuessCount(), A->lastPressureIterations(),
229 B->lastPressureIterations(), A->pressureVcycleRestarts());
230 if (!kAutoFp32) {
231 check(sameState && same && A->pressureVcycleRestarts() == 0 && !A->pressureVcycleFp32(),
232 "bubble (g): host 'auto' == 'fp64': the fault inert, step 4 p == the unfaulted step");
233 return;
234 }
235 check(sameState && histA == 2, "bubble (g): A and B reach step 4 bitwise, history 2");
236 check(same && A->lastPressureIterations() == B->lastPressureIterations() &&
237 A->pressureVcycleRestarts() == 1 && A->pressureGuessCount() == 2 &&
238 !A->pressureVcycleFp32(),
239 "bubble (g): step 4 p memcmp-equal to 'fp64'+'zero', one restart, history advances");
240}
241
242} // namespace
243
244int main(int argc, char** argv) {
245 Kokkos::initialize(argc, argv);
246 std::printf("pressure_restart (doc/vof_projection_distributed_design.md §4, G-R)\n");
247 for (const Problem& P : {columnProblem(), cylProblem(), packProblem()})
248 levelChecks(P);
249 assembledCheck();
250 std::printf("%s (%d failure%s)\n", fails ? "FAIL" : "PASS", fails, fails == 1 ? "" : "s");
251 Kokkos::finalize();
252 return fails ? 1 : 0;
253}
static constexpr bool kHostMemory
void setStrictPressure(bool on)
Throw instead of reporting when the preconditioner returns a non-finite correction.
int solvePCG(CCField b, CCField x, CCField r, CCField p, CCField z, CCField Ap, int maxit, double rtol, int pre, int post, int bottom, const StarOverlay *star=nullptr, int nStar=0, C3 nnStar=C3{0, 0, 0})
void debugSetFp32Fault(int kind, int at)
int solveFCG(CCField b, CCField x, CCField r, CCField p, CCField z, CCField zp, CCField Ap, int maxit, double rtol, int pre, int post, int bottom, const StarOverlay *star=nullptr, int nStar=0, C3 nnStar=C3{0, 0, 0})
void setVcyclePrecision(int mode)
flow — host-facing Kokkos IBM Navier-Stokes solver (drop-in flow-style API).
The FP32 V-cycle test problems (column, cyl, slab, pack) and their right-hand side,...
y
Definition cmp.py:24
Kokkos::View< double *, CCMem > CCField
std::vector< double > rhsOf(const Problem &P)
CCField toDevice(const std::vector< double > &v, const char *name)
void setUp(CutcellMG &mg, const Problem &P, int mode)
std::vector< typename V::non_const_value_type > toHost(const V &f)
dict A
Definition post.py:33
B
Definition post.py:34
P
Definition stab5.py:16
Kokkos::View< double *, Kokkos::HostSpace > x
static void check(bool ok, const std::string &what)
static int fails
static bool same(double a, double b)