flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_projected_guess_mpi.cpp
Go to the documentation of this file.
1// flow — ctest `projected_guess_mpi` (np 1, 2, 4): the projected initial guess of the projection
2// solve on the DISTRIBUTED PCG / FCG (doc/vof_projection_distributed_design.md §6, gates G-ED1..5;
3// the single-rank scheme is doc/vof_projection_cost_design.md §5.3).
4//
5// Distributed, the Gram packet {G11, G12, G22, g1, g2} is allreduced with MPI_SUM and rref with
6// MPI_MAX before the host 2x2 solve, so every rank takes the same c; the guess history is reset by
7// init_mpi and by every redistribute (a size-preserving one passes the phiPrev1 extent check
8// unnoticed). Two configurations on a 16x16x32 grid (the ORB cuts z at np 2, x and z at np 4):
9// * `cyl` — the IBM cylinder of tests/python/test_projected_guess.py (axis y, R = 4, cut-cell
10// pressure, Stokes, body force 1e-3, three MG levels);
11// * `vof` — the periodic sheared plane of vof_momentum_mpi (variable density by LinearMix at
12// ratio 10, momentum-consistent VoF, a sinusoidal cell force).
13// Arms 'projected' PCG history 1, PCG history 2, FCG history 2, each against 'zero' on the same
14// driver, rtol 1e-10, 10 steps. Gates (rank 0 builds the single-rank references):
15// G-ED1 np 1: u v w p (C) memcmp-equal to the single-rank 'projected' run, per-step iterations
16// equal;
17// G-ED2 np > 1: within the np-class tolerances of vof_*_mpi (1e-11 relative, de-meaned p),
18// iterations per step <= 'zero' at the same np + 1;
19// G-E3 (every np) step 1 of 'projected' (empty history) bitwise to 'zero'; after a state write
20// (uploadVelocity, same values, on both twins) the next step bitwise a twin switched to
21// 'zero'; the guess was live before it (history 2, state not bitwise 'zero');
22// G-ED4 a size-preserving redistribute under 'projected' resets the history: the next step is
23// bitwise a twin switched to 'zero', the following one runs on history 1.
24// G-E1, G-E2, G-E4 at np 2 are traced (debug output only): `--trace` runs the 'projected' arms
25// with PECLET_FLOW_MG_DEBUG=2 (set before Kokkos starts) and prints the guess trace on rank 0 for
26// gate_dist's edgate.sh to read; it gates nothing itself.
27#include <mpi.h>
28
29#include <algorithm>
30#include <cmath>
31#include <cstdio>
32#include <cstdlib>
33#include <cstring>
34#include <Kokkos_Core.hpp>
35#include <string>
36#include <vector>
37
38#include "flow_ibm.hpp"
39#include "peclet/core/common/types.hpp"
40#include "peclet/core/decomp/block_decomposer.hpp"
41
43
44static constexpr int NX = 16, NY = 16, NZ = 32, STEPS = 10, MAXIT = 400;
45static constexpr double RTOL = 1e-10;
46static constexpr std::size_t GCELLS = (std::size_t)NX * NY * NZ;
47
48struct Case {
49 const char* name;
50 bool vof;
51};
52struct Arm {
53 const char* name;
54 bool fcg;
55 int hist;
56};
57enum Guess { kAuto = 0, kZero = 1, kProjected = 2 };
58
59static double cylSdf(int x, int, int z) {
60 const double dx = x + 0.5 - 8.0, dz = z + 0.5 - 16.0;
61 return std::sqrt(dx * dx + dz * dz) - 4.0;
62}
63static double colourAt(int x, int y, int z) { // vof_momentum_mpi's tilted sharp plane
64 double m[3] = {0.6, -0.5, 0.62456};
65 const double l1 = std::fabs(m[0]) + std::fabs(m[1]) + std::fabs(m[2]);
66 for (double& v : m)
67 v /= l1;
68 const double alpha = 0.5 * (m[0] * NX + m[1] * NY + m[2] * NZ);
69 return peclet::flow::vof::plicVolume(m[0], m[1], m[2], alpha - (m[0] * x + m[1] * y + m[2] * z));
70}
71static double forceXAt(int, int, int z) {
72 return 5e-2 * std::sin(2 * M_PI * (z + 0.5) / NZ);
73}
74
75template <class Fn>
76static std::vector<double> blockOf(Fn f, int ox, int oy, int oz, int lnx, int lny, int lnz) {
77 std::vector<double> v((std::size_t)lnx * lny * lnz);
78 for (int z = 0; z < lnz; ++z)
79 for (int y = 0; y < lny; ++y)
80 for (int x = 0; x < lnx; ++x)
81 v[(std::size_t)x + (std::size_t)y * lnx + (std::size_t)z * lnx * lny] =
82 f(x + ox, y + oy, z + oz);
83 return v;
84}
85
86static void configure(IbmSolver& s, const Case& c, const Arm& a, int guess, int ox, int oy, int oz,
87 int lnx, int lny, int lnz) {
88 if (!c.vof) {
89 s.setRho(1.0);
90 s.setMu(0.01);
91 s.setDt(1.0);
92 s.setAdvection(false);
93 s.setBodyForce(1e-3, 0.0, 0.0);
94 s.setPressureLevels(3);
95 s.setSolid(blockOf(cylSdf, ox, oy, oz, lnx, lny, lnz), true);
96 } else {
97 const double ratio = 10.0;
98 s.setRho(ratio);
99 s.setMu(0.05);
100 s.setDt(0.2);
101 s.setAdvection(true);
102 s.setPressureGeometry(std::vector<double>((std::size_t)lnx * lny * lnz, 10.0));
103 s.setVof(blockOf(colourAt, ox, oy, oz, lnx, lny, lnz));
104 s.setPropertyModel("rho", peclet::flow::ClosureKind::LinearMix, "C", "", {1.0, ratio - 1.0});
105 s.enableVofMomentum(1.0, ratio);
106 s.enableCellForce();
107 s.setField("force_x", blockOf(forceXAt, ox, oy, oz, lnx, lny, lnz));
108 }
109 if (a.fcg) // LAST: the density mode re-selects the driver
110 s.setPressureFcg(true, MAXIT, RTOL);
111 else
112 s.setPressurePcg(true, MAXIT, RTOL);
113 s.setPressureInitialGuess(guess, a.hist);
114}
115
116static std::vector<std::vector<double>> stateOf(IbmSolver& s, bool vof) {
117 std::vector<std::vector<double>> f;
118 for (int k = 0; k < 3; ++k)
119 f.push_back(s.getVelocity(k));
120 f.push_back(s.getPressure());
121 if (vof)
122 f.push_back(s.getVof());
123 return f;
124}
125static bool sameBits(const std::vector<std::vector<double>>& a,
126 const std::vector<std::vector<double>>& b) {
127 if (a.size() != b.size())
128 return false;
129 for (std::size_t k = 0; k < a.size(); ++k)
130 if (a[k].size() != b[k].size() ||
131 std::memcmp(a[k].data(), b[k].data(), a[k].size() * sizeof(double)) != 0)
132 return false;
133 return true;
134}
135// Bitwise on EVERY rank (each compares its own block).
136static bool sameBitsAll(IbmSolver& a, IbmSolver& b, bool vof) {
137 int ok = sameBits(stateOf(a, vof), stateOf(b, vof)) ? 1 : 0, all = 0;
138 MPI_Allreduce(&ok, &all, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD);
139 return all == 1;
140}
141
142static std::vector<double> gatherGlobal(const std::vector<double>& local, int ox, int oy, int oz,
143 int lnx, int lny, int lnz, int rank, int size) {
144 std::vector<double> global;
145 if (rank == 0)
146 global.assign(GCELLS, 0.0);
147 for (int r = 0; r < size; ++r) {
148 int meta[6] = {ox, oy, oz, lnx, lny, lnz};
149 if (r == 0) {
150 if (rank == 0)
151 for (int z = 0; z < lnz; ++z)
152 for (int y = 0; y < lny; ++y)
153 std::memcpy(&global[(std::size_t)ox + (std::size_t)(y + oy) * NX +
154 (std::size_t)(z + oz) * NX * NY],
155 &local[(std::size_t)y * lnx + (std::size_t)z * lnx * lny],
156 (std::size_t)lnx * sizeof(double));
157 continue;
158 }
159 if (rank == r) {
160 MPI_Send(meta, 6, MPI_INT, 0, 100 + r, MPI_COMM_WORLD);
161 MPI_Send(local.data(), (int)local.size(), MPI_DOUBLE, 0, 200 + r, MPI_COMM_WORLD);
162 } else if (rank == 0) {
163 MPI_Recv(meta, 6, MPI_INT, r, 100 + r, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
164 std::vector<double> buf((std::size_t)meta[3] * meta[4] * meta[5]);
165 MPI_Recv(buf.data(), (int)buf.size(), MPI_DOUBLE, r, 200 + r, MPI_COMM_WORLD,
166 MPI_STATUS_IGNORE);
167 for (int z = 0; z < meta[5]; ++z)
168 for (int y = 0; y < meta[4]; ++y)
169 std::memcpy(&global[(std::size_t)meta[0] + (std::size_t)(y + meta[1]) * NX +
170 (std::size_t)(z + meta[2]) * NX * NY],
171 &buf[(std::size_t)y * meta[3] + (std::size_t)z * meta[3] * meta[4]],
172 (std::size_t)meta[3] * sizeof(double));
173 }
174 }
175 return global;
176}
177static double maxAbsDiff(const std::vector<double>& a, const std::vector<double>& b) {
178 double m = 0; // NaN-propagating (vof_momentum_mpi's WO-R2 note)
179 for (std::size_t i = 0; i < b.size(); ++i) {
180 const double d = std::fabs(a[i] - b[i]);
181 if (!(d == d))
182 return d;
183 m = std::fmax(m, d);
184 }
185 return m;
186}
187static std::string dbgDiff(const std::vector<double>& a, const std::vector<double>& b) {
188 long nb = 0, nv = 0;
189 double m = 0;
190 for (std::size_t i = 0; i < a.size(); ++i) {
191 if (std::memcmp(&a[i], &b[i], sizeof(double)) != 0)
192 ++nb;
193 if (a[i] != b[i]) {
194 ++nv;
195 m = std::fmax(m, std::fabs(a[i] - b[i]));
196 }
197 }
198 char buf[96];
199 std::snprintf(buf, sizeof buf, "bits %ld values %ld max %.2e", nb, nv, m);
200 return buf;
201}
202static double maxAbs(const std::vector<double>& a) {
203 double m = 0;
204 for (double v : a)
205 m = std::fmax(m, std::fabs(v));
206 return m;
207}
208static std::vector<double> demean(std::vector<double> v) { // the periodic pressure gauge
209 double s = 0;
210 for (double x : v)
211 s += x;
212 const double m = s / (double)v.size();
213 for (double& x : v)
214 x -= m;
215 return v;
216}
217
218static int fails = 0;
219static void check(int rank, bool ok, const std::string& what) {
220 if (rank == 0)
221 std::printf(" %-100s %s\n", what.c_str(), ok ? "PASS" : "FAIL");
222 if (!ok)
223 ++fails;
224}
225
226int main(int argc, char** argv) {
227 const bool traceMode = argc > 1 && std::strcmp(argv[1], "--trace") == 0;
228 if (traceMode) { // debug output only, read once at the first solve (mgDebugLevel's static)
229 setenv("PECLET_FLOW_MG_DEBUG", "2", 1);
230 setenv("PECLET_FLOW_MG_DEBUG_SOLVES", "200", 1);
231 }
232 MPI_Init(&argc, &argv);
233 Kokkos::initialize(argc, argv);
234 {
235 int rank = 0, size = 1;
236 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
237 MPI_Comm_size(MPI_COMM_WORLD, &size);
238 auto dec = peclet::flow::CutcellMG::decomposition(static_cast<std::size_t>(size), NX, NY, NZ);
239 auto blk = dec.block(rank);
240 const int ox = (int)blk.origin[0], oy = (int)blk.origin[1], oz = (int)blk.origin[2];
241 const int lnx = (int)blk.size[0], lny = (int)blk.size[1], lnz = (int)blk.size[2];
242 if (rank == 0)
243 std::printf("PROJECTED GUESS MPI np=%d grid %dx%dx%d block %dx%dx%d rtol %.0e%s\n", size,
244 NX, NY, NZ, lnx, lny, lnz, RTOL, traceMode ? " (--trace)" : "");
245 const Case cases[] = {{"cyl", false}, {"vof", true}};
246 const Arm arms[] = {{"pcg h1", false, 1}, {"pcg h2", false, 2}, {"fcg h2", true, 2}};
247 auto dist = [&](IbmSolver& s, const Case& c, const Arm& a, int guess) {
248 s.initMpi(dec, MPI_COMM_WORLD);
249 configure(s, c, a, guess, ox, oy, oz, lnx, lny, lnz);
250 };
251
252 if (traceMode) {
253 for (const Case& c : cases)
254 for (const Arm& a : arms) {
255 if (rank == 0) {
256 std::printf("TRACE %s %s\n", c.name, a.name);
257 std::fflush(stdout);
258 }
259 IbmSolver s(lnx, lny, lnz);
260 dist(s, c, a, kProjected);
261 for (int k = 0; k < STEPS; ++k)
262 s.step();
263 std::fflush(stdout);
264 }
265 if (rank == 0)
266 std::printf("TRACE done\n");
267 } else {
268 for (const Case& c : cases) {
269 for (const Arm& a : arms) {
270 const std::string tag = std::string(c.name) + " " + a.name + " np" + std::to_string(size);
271 // ---- G-ED1 / G-ED2: 'projected' and 'zero' at this np, against np 1 -----------------
272 IbmSolver sp(lnx, lny, lnz), sz(lnx, lny, lnz);
273 dist(sp, c, a, kProjected);
274 dist(sz, c, a, kZero);
275 std::vector<long> itP, itZ;
276 for (int k = 0; k < STEPS; ++k) {
277 sp.step();
278 sz.step();
279 itP.push_back(sp.lastPressureIterations());
280 itZ.push_back(sz.lastPressureIterations());
281 }
282 const auto fp = stateOf(sp, c.vof), fz = stateOf(sz, c.vof);
283 std::vector<std::vector<double>> gp, gz;
284 for (std::size_t k = 0; k < fp.size(); ++k) {
285 gp.push_back(gatherGlobal(fp[k], ox, oy, oz, lnx, lny, lnz, rank, size));
286 gz.push_back(gatherGlobal(fz[k], ox, oy, oz, lnx, lny, lnz, rank, size));
287 }
288 int ok1 = 1, ok2 = 1;
289 if (rank == 0) {
290 // The reference: single rank at np 1 (G-ED1); at np > 1 the np 1 DISTRIBUTED run
291 // (G-ED2 is "np 2/4 vs np 1"), on rank 0 alone over MPI_COMM_SELF.
292 IbmSolver ref(NX, NY, NZ), refz(NX, NY, NZ);
293 if (size > 1) {
294 const auto dec1 = peclet::flow::CutcellMG::decomposition(1, NX, NY, NZ);
295 ref.initMpi(dec1, MPI_COMM_SELF);
296 refz.initMpi(dec1, MPI_COMM_SELF);
297 }
298 configure(ref, c, a, kProjected, 0, 0, 0, NX, NY, NZ);
299 configure(refz, c, a, kZero, 0, 0, 0, NX, NY, NZ);
300 std::vector<long> itR, itRz;
301 for (int k = 0; k < STEPS; ++k) {
302 ref.step();
303 refz.step();
304 itR.push_back(ref.lastPressureIterations());
305 itRz.push_back(refz.lastPressureIterations());
306 }
307 const auto fr = stateOf(ref, c.vof), frz = stateOf(refz, c.vof);
308 long sP = 0, sZ = 0, sR = 0;
309 for (int k = 0; k < STEPS; ++k) {
310 sP += itP[k];
311 sZ += itZ[k];
312 sR += itR[k];
313 }
314 std::string per; // per field: 'projected' / the 'zero' control, max |difference|
315 const char* nm[] = {"u", "v", "w", "p", "C"};
316 for (std::size_t k = 0; k < gp.size(); ++k) {
317 char buf[64];
318 std::snprintf(buf, sizeof buf, " %s %.2e/%.2e", nm[k], maxAbsDiff(gp[k], fr[k]),
319 maxAbsDiff(gz[k], frz[k]));
320 per += buf;
321 }
322 if (size == 1) {
323 // §2's premise: serial <-> np 1 bitwise. Where the 'zero' control itself is not
324 // (the staggered cut-cell IBM momentum path, pre-existing), the memcmp gate is n/a:
325 // gated are the per-step iterations, and both deviations are printed.
326 const bool base = sameBits(gz, frz) && itZ == itRz;
327 ok1 = itP == itR && (!base || sameBits(gp, fr));
328 std::printf(
329 " [%s] G-ED1 vs single rank: fields %s, iterations %ld vs %ld%s | "
330 "'zero' control %s | max|d| proj/zero%s\n",
331 tag.c_str(), sameBits(gp, fr) ? "memcmp-equal" : "DIFFER", sP, sR,
332 itP == itR ? " (equal per step)" : " (DIFFER per step)",
333 base ? "bitwise" : "NOT bitwise (memcmp n/a)", per.c_str());
334 } else {
335 // the np-class tolerances of vof_*_mpi: 1e-11 relative, the periodic p de-meaned
336 double du = 0, umag = 0, duz = 0;
337 for (int k = 0; k < 3; ++k) {
338 du = std::fmax(du, maxAbsDiff(gp[k], fr[k]));
339 duz = std::fmax(duz, maxAbsDiff(gz[k], frz[k]));
340 umag = std::fmax(umag, maxAbs(fr[k]));
341 }
342 const double dp = maxAbsDiff(demean(gp[3]), demean(fr[3]));
343 const double dpz = maxAbsDiff(demean(gz[3]), demean(frz[3]));
344 const double pmag = maxAbs(demean(fr[3]));
345 const double dc = c.vof ? maxAbsDiff(gp[4], fr[4]) : 0.0;
346 const double utol = std::fmax(1e-12, 1e-11 * umag);
347 const double ptol = std::fmax(1e-12, 1e-11 * pmag);
348 bool itok = true;
349 int worst = -1000;
350 for (int k = 0; k < STEPS; ++k) {
351 itok = itok && itP[k] <= itZ[k] + 1;
352 worst = std::max(worst, (int)(itP[k] - itZ[k]));
353 }
354 ok2 = du <= utol && dp <= ptol && dc <= 1e-11 && itok;
355 std::printf(
356 " [%s] G-ED2 vs np1: du %.3e (tol %.1e) dp %.3e (tol %.1e, |p| %.2e) dC %.3e | "
357 "'zero' du %.3e dp %.3e | iters proj %ld zero %ld (np1 proj %ld), "
358 "max(proj-zero) per step %+d\n",
359 tag.c_str(), du, utol, dp, ptol, pmag, dc, duz, dpz, sP, sZ, sR, worst);
360 }
361 }
362 MPI_Bcast(&ok1, 1, MPI_INT, 0, MPI_COMM_WORLD);
363 MPI_Bcast(&ok2, 1, MPI_INT, 0, MPI_COMM_WORLD);
364 if (size == 1)
365 check(rank, ok1, "[" + tag + "] G-ED1 np 1 == single rank, bitwise, iterations equal");
366 else
367 check(rank, ok2, "[" + tag + "] G-ED2 np-class tolerance, iterations <= 'zero' + 1");
368
369 if (a.hist != 2)
370 continue;
371 // ---- G-E3: the empty history is the cold solve; a state write restarts it ----------
372 {
373 IbmSolver ta(lnx, lny, lnz), tb(lnx, lny, lnz), tz(lnx, lny, lnz);
374 dist(ta, c, a, kProjected);
375 dist(tb, c, a, kProjected);
376 dist(tz, c, a, kZero);
377 ta.step();
378 tb.step();
379 tz.step();
380 check(rank, sameBitsAll(ta, tz, c.vof),
381 "[" + tag + "] G-E3 step 1 of 'projected' == 'zero', bitwise");
382 for (int k = 0; k < 4; ++k) {
383 ta.step();
384 tb.step();
385 tz.step();
386 }
387 const bool live = ta.pressureGuessCount() == 2 && !sameBitsAll(ta, tz, c.vof);
388 check(rank, live, "[" + tag + "] the guess is live (history 2, not bitwise 'zero')");
389 for (IbmSolver* t : {&ta, &tb}) // the state write, same values, on both twins
390 t->uploadVelocity(t->getVelocity(0), t->getVelocity(1), t->getVelocity(2));
391 tb.setPressureInitialGuess(kZero, 2);
392 ta.step();
393 tb.step();
394 check(rank, ta.pressureGuessCount() == 1 && sameBitsAll(ta, tb, c.vof),
395 "[" + tag + "] G-E3 the step after a state write == the twin's 'zero' step");
396 }
397 // ---- G-ED4: a size-preserving redistribute restarts the history --------------------
398 {
399 IbmSolver ta(lnx, lny, lnz), tb(lnx, lny, lnz);
400 dist(ta, c, a, kProjected);
401 dist(tb, c, a, kProjected);
402 for (int k = 0; k < 4; ++k) {
403 ta.step();
404 tb.step();
405 }
406 const bool before = ta.pressureGuessCount() == 2;
407 ta.redistribute(dec); // the same partition: every block keeps its size
408 tb.redistribute(dec);
409 const bool reset = ta.pressureGuessCount() == 0;
410 tb.setPressureInitialGuess(kZero, 2);
411 ta.step();
412 tb.step();
413 const bool cold = sameBitsAll(ta, tb, c.vof);
414 const bool h1 = ta.pressureGuessCount() == 1; // the next solve runs on history 1
415 ta.step();
416 tb.step();
417 const bool used = ta.pressureGuessCount() == 2 && !sameBitsAll(ta, tb, c.vof);
418 check(rank, before && reset && cold && h1 && used,
419 "[" + tag + "] G-ED4 redistribute resets: next step == 'zero', then history 1");
420 }
421 }
422 }
423 if (rank == 0)
424 std::printf(fails ? "FAILED (%d)\n" : "OK\n", fails);
425 }
426 }
427 Kokkos::finalize();
428 MPI_Finalize();
429 return fails ? 1 : 0;
430}
flow — host-facing Kokkos IBM Navier-Stokes solver (drop-in flow-style API).
Solver< Staggered > IbmSolver
static constexpr int NY
static constexpr int NX
static constexpr std::size_t GCELLS
static constexpr int NZ
const char * name
static constexpr int STEPS
static double colourAt(int, int, int z)
static bool sameBits(const std::vector< double > &a, const std::vector< double > &b)
static constexpr double RTOL
static constexpr int MAXIT
static double maxAbs(const std::vector< double > &a)
static std::vector< double > demean(std::vector< double > v)
static std::vector< std::vector< double > > stateOf(IbmSolver &s, bool vof)
static std::vector< double > blockOf(Fn f, int ox, int oy, int oz, int lnx, int lny, int lnz)
static bool sameBits(const std::vector< std::vector< double > > &a, const std::vector< std::vector< double > > &b)
static double forceXAt(int, int, int z)
static std::vector< double > gatherGlobal(const std::vector< double > &local, int ox, int oy, int oz, int lnx, int lny, int lnz, int rank, int size)
static bool sameBitsAll(IbmSolver &a, IbmSolver &b, bool vof)
static int fails
static double cylSdf(int x, int, int z)
static double maxAbsDiff(const std::vector< double > &a, const std::vector< double > &b)
static std::string dbgDiff(const std::vector< double > &a, const std::vector< double > &b)
static void check(int rank, bool ok, const std::string &what)
static double colourAt(int x, int y, int z)
static void configure(IbmSolver &s, const Case &c, const Arm &a, int guess, int ox, int oy, int oz, int lnx, int lny, int lnz)