flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_cg_guard_mpi.cpp
Go to the documentation of this file.
1// flow — ctest `cg_guard_mpi` (np 1, 2, 4): the exit check of the two CG pressure drivers (Q18).
2//
3// Under the trilinear V-cycle (set_pressure_vcycle_symmetric(False), the pre-A3 preconditioner),
4// above a density ratio of ~1e3 that preconditioner turns indefinite and MG-PCG / flexible CG
5// either cap or stop "converged" on their recurrence residual with a field that is not a
6// projection; the default SPD V-cycle (A3) keeps them converged there. The check recomputes the
7// TRUE residual once per CG solve and counts a solve whose true residual exceeds max(10 rtol, 1e-8)
8// r0, or that capped or broke down (diagnostics.num_pressure_unconverged_solves); it never changes
9// x, the iteration or the stop. Case: a 16^3 box, walls at +-y, periodic x and z, a tilted
10// two-layer VoF colour (a 22.5 degree line in x-y, extruded in z; frozen: re-set after every step),
11// rho and mu by LinearMix from C, Stokes, a fixed pseudo-random initial velocity, 3 steps, rtol
12// 1e-10, cap 400 (the reproduction of validation/highratio/q18, cell units). Gates, at every np
13// (each rank checks its own copy; the allreduced MIN and MAX must agree):
14// G1 FCG at ratio 1e5 on the trilinear V-cycle: the count is >= 1 and the same on every rank;
15// the last checked true residual is bitwise the same on every rank;
16// G2 Chebyshev at ratio 1e4 (the variable-density default): the count stays 0;
17// G3 PCG at ratio 1 (uniform density): the count stays 0 and every true residual is <= 1
18// (the exit meets its own tolerance);
19// G4 (rank 0, a single-rank solver, the device-resident PCG and the host FCG) G1 and G3 again;
20// G5 FCG at ratio 1e4 on the default SPD V-cycle: the count stays 0 (no false alarm).
21#include <mpi.h>
22
23#include <cmath>
24#include <cstdio>
25#include <cstring>
26#include <Kokkos_Core.hpp>
27#include <vector>
28
29#include "flow_ibm.hpp"
30#include "peclet/core/decomp/block_decomposer.hpp"
31
33
34static constexpr int N = 16, STEPS = 3, MAXIT = 400;
35static constexpr double RTOL = 1e-10;
36
37enum Driver { kPcg, kFcg, kCheb };
38
39static double colourAt(int x, int y) { // the fraction below a tilted line, 16 x 16 sub-samples
40 const double th = 22.5 * M_PI / 180.0, nx = std::cos(th), ny = std::sin(th);
41 const double x0 = 0.5 * N + 0.3 * nx, y0 = 0.5 * N + 0.3 * ny;
42 int in = 0;
43 for (int j = 0; j < 16; ++j)
44 for (int i = 0; i < 16; ++i)
45 in += ((x + (i + 0.5) / 16 - x0) * nx + (y + (j + 0.5) / 16 - y0) * ny < 0.0) ? 1 : 0;
46 return in / 256.0;
47}
48static double noiseAt(int c, int x, int y, int z) { // decomposition-independent, in [-1, 1]
49 unsigned h = 2166136261u;
50 for (int v : {c, x, y, z})
51 h = (h ^ (unsigned)v) * 16777619u;
52 h ^= h >> 13;
53 h *= 0x5bd1e995u;
54 h ^= h >> 15;
55 return 2.0 * (h & 0xffffff) / double(0xffffff) - 1.0;
56}
57
58struct Result {
59 long count = 0;
60 double lastTrue = 0.0, maxTrue = 0.0;
61};
62
63static Result run(IbmSolver& s, Driver d, double ratio, int ox, int oy, int oz, int lnx, int lny,
64 int lnz, bool trilinear = false) {
65 const double muRatio = ratio / 10.0;
66 s.setDt(1e4 * (ratio > 1.0 ? ratio / muRatio : 1.0));
67 for (int f = 2; f < 4; ++f)
68 s.setDomainBc(f, 1, 0.0, 0.0, 0.0); // walls at -y, +y
69 s.setRho(1.0);
70 s.setMu(1.0);
71 const std::size_t n = (std::size_t)lnx * lny * lnz;
72 s.setPressureGeometry(std::vector<double>(n, 1.0));
73 std::vector<double> c(n);
74 for (int z = 0; z < lnz; ++z)
75 for (int y = 0; y < lny; ++y)
76 for (int x = 0; x < lnx; ++x)
77 c[(std::size_t)x + (std::size_t)y * lnx + (std::size_t)z * lnx * lny] =
78 colourAt(x + ox, y + oy);
79 s.enableVof();
80 s.setVof(c);
81 s.setPropertyModel("mu", peclet::flow::ClosureKind::LinearMix, "C", "", {1.0, muRatio - 1.0});
82 s.setPropertyModel("rho", peclet::flow::ClosureKind::LinearMix, "C", "", {1.0, ratio - 1.0});
83 s.setAdvection(false);
84 if (trilinear)
85 s.setPressureVcycleSymmetric(false);
86 if (d == kPcg) // LAST: the density model re-selects Chebyshev
87 s.setPressurePcg(true, MAXIT, RTOL);
88 else if (d == kFcg)
89 s.setPressureFcg(true, MAXIT, RTOL);
90 else
91 s.setPressureChebyshev(true, MAXIT, RTOL);
92 for (int k = 0; k < 3; ++k) {
93 std::vector<double> v(n);
94 for (int z = 0; z < lnz; ++z)
95 for (int y = 0; y < lny; ++y)
96 for (int x = 0; x < lnx; ++x)
97 v[(std::size_t)x + (std::size_t)y * lnx + (std::size_t)z * lnx * lny] =
98 1e-12 * noiseAt(k, x + ox, y + oy, z + oz);
99 s.setVelocity(k, v);
100 }
101 Result r;
102 for (int k = 0; k < STEPS; ++k) {
103 s.step();
104 s.setVof(c); // frozen colour
105 r.lastTrue = s.lastPressureTrueResidual();
106 r.maxTrue = std::fmax(r.maxTrue, r.lastTrue);
107 }
108 r.count = s.numPressureUnconvergedSolves();
109 return r;
110}
111
112// The same value on every rank? (MIN == MAX over the communicator, bitwise for the double.)
113static bool sameOnAll(double v, MPI_Comm comm) {
114 double lo = 0, hi = 0;
115 MPI_Allreduce(&v, &lo, 1, MPI_DOUBLE, MPI_MIN, comm);
116 MPI_Allreduce(&v, &hi, 1, MPI_DOUBLE, MPI_MAX, comm);
117 return std::memcmp(&lo, &hi, sizeof(double)) == 0;
118}
119
120int main(int argc, char** argv) {
121 MPI_Init(&argc, &argv);
122 Kokkos::initialize(argc, argv);
123 int fails = 0;
124 {
125 int rank = 0, size = 1;
126 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
127 MPI_Comm_size(MPI_COMM_WORLD, &size);
128 auto dec = peclet::flow::CutcellMG::decomposition(static_cast<std::size_t>(size), N, N, N);
129 auto blk = dec.block(rank);
130 const int ox = (int)blk.origin[0], oy = (int)blk.origin[1], oz = (int)blk.origin[2];
131 const int lnx = (int)blk.size[0], lny = (int)blk.size[1], lnz = (int)blk.size[2];
132 if (rank == 0)
133 std::printf("CG GUARD MPI np=%d grid %d^3 block %dx%dx%d rtol %.0e cap %d\n", size, N,
134 lnx, lny, lnz, RTOL, MAXIT);
135 auto check = [&](bool ok, const char* what, const Result& r) {
136 int all = 0, mine = ok ? 1 : 0;
137 MPI_Allreduce(&mine, &all, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD);
138 if (rank == 0)
139 std::printf(" %-44s count %ld last true %.3e max true %.3e %s\n", what, r.count,
140 r.lastTrue, r.maxTrue, all ? "OK" : "FAIL");
141 fails += all ? 0 : 1;
142 };
143 {
144 IbmSolver s(lnx, lny, lnz);
145 s.initMpi(dec, MPI_COMM_WORLD);
146 const Result r = run(s, kFcg, 1e5, ox, oy, oz, lnx, lny, lnz, true);
147 const bool same =
148 sameOnAll((double)r.count, MPI_COMM_WORLD) && sameOnAll(r.lastTrue, MPI_COMM_WORLD);
149 check(r.count >= 1 && same, "G1 FCG ratio 1e5 trilinear: flagged, same", r);
150 }
151 {
152 IbmSolver s(lnx, lny, lnz);
153 s.initMpi(dec, MPI_COMM_WORLD);
154 const Result r = run(s, kCheb, 1e4, ox, oy, oz, lnx, lny, lnz);
155 check(r.count == 0 && sameOnAll((double)r.count, MPI_COMM_WORLD),
156 "G2 Chebyshev ratio 1e4: silent", r);
157 }
158 {
159 IbmSolver s(lnx, lny, lnz);
160 s.initMpi(dec, MPI_COMM_WORLD);
161 const Result r = run(s, kPcg, 1.0, ox, oy, oz, lnx, lny, lnz);
162 check(r.count == 0 && r.maxTrue <= 1.0 && sameOnAll(r.maxTrue, MPI_COMM_WORLD),
163 "G3 PCG uniform rho: silent, true <= tol", r);
164 }
165 {
166 IbmSolver s(lnx, lny, lnz);
167 s.initMpi(dec, MPI_COMM_WORLD);
168 const Result r = run(s, kFcg, 1e4, ox, oy, oz, lnx, lny, lnz);
169 check(r.count == 0 && sameOnAll((double)r.count, MPI_COMM_WORLD),
170 "G5 FCG ratio 1e4 SPD V-cycle: silent", r);
171 }
172 if (rank == 0) { // G4: single-rank solvers (no initMpi)
173 int f4 = 0;
174 for (int k = 0; k < 2; ++k) {
175 IbmSolver s(N, N, N);
176 const Result r = k == 0 ? run(s, kFcg, 1e5, 0, 0, 0, N, N, N, true)
177 : run(s, kPcg, 1.0, 0, 0, 0, N, N, N);
178 const bool ok = k == 0 ? r.count >= 1 : (r.count == 0 && r.maxTrue <= 1.0);
179 std::printf(" %-44s count %ld last true %.3e max true %.3e %s\n",
180 k == 0 ? "G4 single-rank FCG ratio 1e5 trilinear: flagged"
181 : "G4 single-rank PCG uniform rho: silent",
182 r.count, r.lastTrue, r.maxTrue, ok ? "OK" : "FAIL");
183 f4 += ok ? 0 : 1;
184 }
185 fails += f4;
186 }
187 MPI_Bcast(&fails, 1, MPI_INT, 0, MPI_COMM_WORLD);
188 if (rank == 0)
189 std::printf(fails ? "FAILED (%d)\n" : "OK\n", fails);
190 }
191 Kokkos::finalize();
192 MPI_Finalize();
193 return fails ? 1 : 0;
194}
flow — host-facing Kokkos IBM Navier-Stokes solver (drop-in flow-style API).
Solver< Staggered > IbmSolver
static constexpr int STEPS
static constexpr int N
static void check(bool ok, const std::string &what)
static double noiseAt(int c, int x, int y, int z)
static double colourAt(int x, int y)
static Result run(IbmSolver &s, Driver d, double ratio, int ox, int oy, int oz, int lnx, int lny, int lnz, bool trilinear=false)
static constexpr double RTOL
static bool sameOnAll(double v, MPI_Comm comm)
static constexpr int MAXIT
static int fails
static bool same(double a, double b)