27#include <Kokkos_Core.hpp>
42void check(
bool ok,
const std::string& what) {
43 std::printf(
" %-96s %s\n",
what.c_str(), ok ?
"PASS" :
"FAIL");
53 int it = -1, restarts = 0, discarded = -1;
54 bool failed =
false, fp32 =
false, threw =
false;
55 std::vector<double>
x;
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;
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),
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&) {
84Run fresh(
const Problem& P,
int mode,
int driver,
int kind = 0,
int at = 0,
bool strict =
false) {
88 return solveWith(mg, P, driver, kind, at);
90std::string itStr(
const Run& r) {
91 return std::to_string(
r.it) + (
r.failed ?
" failed" :
"") + (
r.fp32 ?
" fp32" :
" fp64");
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;
100void levelChecks(
const Problem& P) {
101 for (
int driver : {0, 1}) {
102 const std::string
tag =
P.name + (
driver == 0 ?
" PCG" :
" FCG");
105 std::printf(
" %s: fp64 %s, auto %s\n",
tag.c_str(), itStr(ref64).c_str(),
106 itStr(auto0).c_str());
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) +
")");
115 if (auto0.restarts == 1)
116 check(restartedIsFp64(auto0, ref64),
118 ": 'auto' unfaulted: the FP32 attempt does not converge -> natural restart == "
119 "fp64 cold (discarded " +
120 std::to_string(auto0.discarded) +
")");
122 check(auto0.fp32 && auto0.restarts == 0,
123 tag +
": 'auto' unfaulted runs the FP32 V-cycle, converges, no restart");
126 check(restartedIsFp64(a, ref64), tag +
": (a) kind 1 at 3 -> restart == fp64 cold (" +
127 itStr(a) +
", discarded " + std::to_string(
a.discarded) +
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));
136 check(restartedIsFp64(e, ref64),
137 tag +
": (e) strict + 'auto' + kind 1 at 1: no throw, restart == fp64 cold");
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 &&
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");
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 " +
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");
166 check(
e2.threw, tag +
": (e) strict + forced 'fp32' + kind 1 at 1 throws");
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;
174std::unique_ptr<IbmSolver> bubble(
int vprec,
int guessMode) {
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);
187 auto s = std::make_unique<IbmSolver>(n, n, n);
188 s->setAdvection(
false);
192 s->setPressureGeometry(std::vector<double>(nc, 10.0));
196 s->enableVofMomentum(rhoG, rhoL);
197 s->setPressurePcg(
true, 200, 1e-10);
198 s->setPressureVcyclePrecision(vprec);
199 s->setPressureInitialGuess(guessMode, 2);
200 s->uploadVelocity(u, v, w);
203void assembledCheck() {
206 for (
int k = 1;
k <= 3; ++
k) {
210 const bool sameState =
A->getPressure() ==
B->getPressure();
211 const int histA =
A->pressureGuessCount();
213 A->debugSetPressureFp32Fault(1, 3);
215 A->debugSetPressureFp32Fault(0, 0);
218 B->setPressureVcyclePrecision(kFp64);
219 B->setPressureInitialGuess(
kZero, 2);
222 const std::vector<double>
pA =
A->getPressure(),
pB =
B->getPressure();
224 pA.size() ==
pB.size() && std::memcmp(
pA.data(),
pB.data(),
pA.size() *
sizeof(
double)) == 0;
226 " bubble: history %d before step 4 (%s), %d after; iterations A %ld B %ld; "
228 histA, sameState ?
"A == B" :
"A != B",
A->pressureGuessCount(),
A->lastPressureIterations(),
229 B->lastPressureIterations(),
A->pressureVcycleRestarts());
231 check(sameState &&
same &&
A->pressureVcycleRestarts() == 0 && !
A->pressureVcycleFp32(),
232 "bubble (g): host 'auto' == 'fp64': the fault inert, step 4 p == the unfaulted step");
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");
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");
250 std::printf(
"%s (%d failure%s)\n",
fails ?
"FAIL" :
"PASS",
fails,
fails == 1 ?
"" :
"s");
252 return fails ? 1 : 0;
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 lastDiscardedIters() const
bool lastVcycleFp32() const
int vcycleRestarts() const
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,...
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)
Kokkos::View< double *, Kokkos::HostSpace > x
static void check(bool ok, const std::string &what)
static bool same(double a, double b)