flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_vof_momentum_mpi.cpp
Go to the documentation of this file.
1// flow — multi-rank validation of MOMENTUM-CONSISTENT VoF transport (rung V2b, WO-K): `rho^c u_c`
2// advected on the half-shifted MAC control volumes by the same geometric fluxes as the colour
3// field.
4//
5// What is new here relative to `test_vof_twophase_mpi.cpp` (rung V2a), which gated the colour
6// chain:
7//
8// 1. THE SHIFTED CONTROL VOLUMES AND THEIR OWNERSHIP. `C^c` and the transported velocity are
9// ordinary cell fields on the advector's g = 3 block, indexed in `flow`'s own low-face
10// convention precisely so that the owned control volumes ARE the block's inner region and the
11// colour halo carries them unchanged. If that correspondence were off by one along a
12// component's own axis, a control volume would be owned by two ranks or by none — invisible
13// single-rank, and visible here as a pointwise difference at the rank boundary.
14// 2. SIX MORE HALO EXCHANGES PER SWEEP. `C^c` and the transported velocity are exchanged before
15// every sweep (the flux clamp reads the DONOR's own colour, which is a ghost on the first
16// owned face). Their ghost policy must be the colour's, i.e. globally-clamped on a
17// non-periodic axis (WO-E finding 2) — `walls-z` cuts the walled axis and exercises it.
18// 3. THE CONSISTENCY IDENTITY UNDER DECOMPOSITION. The uniform-velocity identity is exact in
19// floating point; it must stay exact on every rank, which is a statement that the flux at a
20// rank boundary is computed identically on both sides.
21//
22// Comparison protocol is `tests/kokkos_mpi`'s: pointwise against a full-grid single-rank reference
23// built on rank 0. np = 1 is bit-exact; np > 1 inherits the MPI reduction-order floor of the
24// pressure driver (Chebyshev bounds + removeMean), which the colour and the momentum inherit
25// through the velocity that advects them.
26#include <mpi.h>
27
28#include <cmath>
29#include <cstdio>
30#include <cstring>
31#include <Kokkos_Core.hpp>
32#include <vector>
33
34#include "flow_ibm.hpp"
35#include "peclet/core/common/types.hpp"
36#include "peclet/core/decomp/block_decomposer.hpp"
37
39
40static constexpr int NX = 16, NY = 16, NZ = 32;
41static constexpr std::size_t GCELLS = (std::size_t)NX * NY * NZ;
42
43struct Config {
44 const char* name;
45 bool walls; // uniform-velocity identity in a walled box (else the periodic sheared sphere)
46 double ratio;
47 double dt;
48 int steps;
49};
50
51static const double UNIF[3] = {1.0, 0.6, -0.4};
52
53// A tilted sharp plane: a pure function of GLOBAL indices, so every block gets bit-identical data
54// to the corresponding part of the single-rank run. Exact fractions from the analytic plane->volume
55// relation the reconstruction inverts.
56static double colourAt(int x, int y, int z) {
57 double m[3] = {0.6, -0.5, 0.62456};
58 const double l1 = std::fabs(m[0]) + std::fabs(m[1]) + std::fabs(m[2]);
59 for (double& v : m)
60 v /= l1;
61 const double alpha = 0.5 * (m[0] * NX + m[1] * NY + m[2] * NZ);
62 return peclet::flow::vof::plicVolume(m[0], m[1], m[2], alpha - (m[0] * x + m[1] * y + m[2] * z));
63}
64static double forceXAt(int, int, int z) {
65 return 5e-2 * std::sin(2 * M_PI * (z + 0.5) / NZ);
66}
67
68template <class Fn>
69static std::vector<double> blockOf(Fn f, int ox, int oy, int oz, int lnx, int lny, int lnz) {
70 std::vector<double> v((std::size_t)lnx * lny * lnz);
71 for (int z = 0; z < lnz; ++z)
72 for (int y = 0; y < lny; ++y)
73 for (int x = 0; x < lnx; ++x)
74 v[(std::size_t)x + (std::size_t)y * lnx + (std::size_t)z * lnx * lny] =
75 f(x + ox, y + oy, z + oz);
76 return v;
77}
78
79static void configure(IbmSolver& s, const Config& c, int ox, int oy, int oz, int lnx, int lny,
80 int lnz) {
81 s.setRho(c.ratio);
82 s.setMu(c.walls ? 0.0 : 0.05);
83 s.setDt(c.dt);
84 s.setAdvection(!c.walls);
85 if (c.walls) {
86 s.setDomainBc(4, 2, UNIF[0], UNIF[1], UNIF[2]); // inflow -z, the CUT axis
87 s.setDomainBc(5, 3, 0, 0, 0); // outflow +z
88 }
89 s.setPressureGeometry(std::vector<double>((std::size_t)lnx * lny * lnz, 10.0));
90 s.setVof(blockOf(colourAt, ox, oy, oz, lnx, lny, lnz));
91 s.setPropertyModel("rho", peclet::flow::ClosureKind::LinearMix, "C", "", {1.0, c.ratio - 1.0});
92 s.enableVofMomentum(1.0, c.ratio);
93 if (!c.walls) {
94 s.enableCellForce();
95 s.setField("force_x", blockOf(forceXAt, ox, oy, oz, lnx, lny, lnz));
96 }
97 const std::size_t nc = (std::size_t)lnx * lny * lnz;
98 if (c.walls) // the uniform-velocity identity configuration
99 s.uploadVelocity(std::vector<double>(nc, UNIF[0]), std::vector<double>(nc, UNIF[1]),
100 std::vector<double>(nc, UNIF[2]));
101}
102
103static std::vector<double> gatherGlobal(const std::vector<double>& local, int ox, int oy, int oz,
104 int lnx, int lny, int lnz, int rank, int size) {
105 std::vector<double> global;
106 if (rank == 0)
107 global.assign(GCELLS, 0.0);
108 for (int r = 0; r < size; ++r) {
109 int meta[6] = {ox, oy, oz, lnx, lny, lnz};
110 if (r == 0) {
111 if (rank == 0)
112 for (int z = 0; z < lnz; ++z)
113 for (int y = 0; y < lny; ++y)
114 std::memcpy(&global[(std::size_t)ox + (std::size_t)(y + oy) * NX +
115 (std::size_t)(z + oz) * NX * NY],
116 &local[(std::size_t)y * lnx + (std::size_t)z * lnx * lny],
117 (std::size_t)lnx * sizeof(double));
118 continue;
119 }
120 if (rank == r) {
121 MPI_Send(meta, 6, MPI_INT, 0, 100 + r, MPI_COMM_WORLD);
122 MPI_Send(local.data(), (int)local.size(), MPI_DOUBLE, 0, 200 + r, MPI_COMM_WORLD);
123 } else if (rank == 0) {
124 MPI_Recv(meta, 6, MPI_INT, r, 100 + r, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
125 std::vector<double> buf((std::size_t)meta[3] * meta[4] * meta[5]);
126 MPI_Recv(buf.data(), (int)buf.size(), MPI_DOUBLE, r, 200 + r, MPI_COMM_WORLD,
127 MPI_STATUS_IGNORE);
128 for (int z = 0; z < meta[5]; ++z)
129 for (int y = 0; y < meta[4]; ++y)
130 std::memcpy(&global[(std::size_t)meta[0] + (std::size_t)(y + meta[1]) * NX +
131 (std::size_t)(z + meta[2]) * NX * NY],
132 &buf[(std::size_t)y * meta[3] + (std::size_t)z * meta[3] * meta[4]],
133 (std::size_t)meta[3] * sizeof(double));
134 }
135 }
136 return global;
137}
138
139static double maxAbsDiff(const std::vector<double>& a, const std::vector<double>& b) {
140 // WO-R2: NaN-PROPAGATING. `std::fmax(m, NaN) == m`, so the obvious loop returns 0.000e+00 for a
141 // field that has gone entirely NaN and every bitwise gate built on it passes (WO-R found this on
142 // a drained open-boundary run). A non-finite difference must fail, so return it.
143 double m = 0;
144 for (std::size_t i = 0; i < b.size(); ++i) {
145 const double d = std::fabs(a[i] - b[i]);
146 if (!(d == d))
147 return d; // NaN
148 m = std::fmax(m, d);
149 }
150 return m;
151}
152static double maxAbs(const std::vector<double>& a) {
153 double m = 0;
154 for (double v : a)
155 m = std::fmax(m, std::fabs(v));
156 return m;
157}
158static double maxAbsDev(const std::vector<double>& a, double r) {
159 double m = 0;
160 for (double v : a)
161 m = std::fmax(m, std::fabs(v - r));
162 return m;
163}
164// In a fully periodic box the pressure carries an arbitrary additive gauge, fixed by `removeMean`
165// whose MPI_SUM order depends on the rank count. Comparing the DE-MEANED field measures the
166// physics rather than that gauge; on the walled configuration the outflow Dirichlet pins P and the
167// raw field is compared.
168static std::vector<double> demean(std::vector<double> v) {
169 double s = 0;
170 for (double x : v)
171 s += x;
172 const double m = s / (double)v.size();
173 for (double& x : v)
174 x -= m;
175 return v;
176}
177static double sumOf(const std::vector<double>& a) {
178 double s = 0;
179 for (double v : a)
180 s += v;
181 return s;
182}
183
184// G-M2-8 (doc/vof_momentum_second_order.md §9): the 'muscl_phase' advection ALONE (no projection,
185// no momentum solve, so no MPI reduction enters) must be bitwise the single-rank one. A sharp
186// tilted interface moved by a non-uniform, discretely divergence-free field (each face component is
187// independent of its own coordinate), periodic, ratio 1e3, ten advections. Exercises the face
188// values at rank boundaries: the per-phase slopes read the donor's neighbours, the cap reads the
189// donor's other face and its ghost-layer frozen flag (freezeGhostFlags). 'muscl_mixed_capped' runs
190// too: its cap bites here, so a ghost flag that differed from the owner's would show.
191static double phaseU(int, int y, int z) {
192 return 0.6 + 0.3 * std::sin(2 * M_PI * (y + 0.5) / NY) * std::cos(2 * M_PI * z / NZ);
193}
194static double phaseV(int, int, int z) {
195 return -0.4 + 0.3 * std::cos(2 * M_PI * (z + 0.5) / NZ);
196}
197static double phaseW(int x, int, int) {
198 return 0.3 + 0.4 * std::sin(2 * M_PI * (x + 0.5) / NX + 0.3);
199}
200static void configurePhase(IbmSolver& s, const char* scheme, int ox, int oy, int oz, int lnx,
201 int lny, int lnz) {
202 const double ratio = 1000.0;
203 s.setRho(ratio);
204 s.setMu(0.0);
205 s.setDt(0.2);
206 s.setPressureGeometry(std::vector<double>((std::size_t)lnx * lny * lnz, 10.0));
207 s.setVof(blockOf(colourAt, ox, oy, oz, lnx, lny, lnz));
208 s.setPropertyModel("rho", peclet::flow::ClosureKind::LinearMix, "C", "", {1.0, ratio - 1.0});
209 s.enableVofMomentum(1.0, ratio);
210 s.setVofMomentumScheme(scheme);
211 s.uploadVelocity(blockOf(phaseU, ox, oy, oz, lnx, lny, lnz),
212 blockOf(phaseV, ox, oy, oz, lnx, lny, lnz),
213 blockOf(phaseW, ox, oy, oz, lnx, lny, lnz));
214}
215
216int main(int argc, char** argv) {
217 MPI_Init(&argc, &argv);
218 Kokkos::initialize(argc, argv);
219 int fail = 0;
220 {
221 int rank = 0, size = 1;
222 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
223 MPI_Comm_size(MPI_COMM_WORLD, &size);
224
225 auto dec = peclet::flow::CutcellMG::decomposition(static_cast<std::size_t>(size), NX, NY, NZ);
226 auto blk = dec.block(rank);
227 const int ox = (int)blk.origin[0], oy = (int)blk.origin[1], oz = (int)blk.origin[2];
228 const int lnx = (int)blk.size[0], lny = (int)blk.size[1], lnz = (int)blk.size[2];
229 const int gn[3] = {NX, NY, NZ};
230 bool cut[3] = {false, false, false};
231 for (const auto& sz : dec.sizes())
232 for (int a = 0; a < 3; ++a)
233 if ((int)sz[a] != gn[a])
234 cut[a] = true;
235 if (rank == 0)
236 std::printf("VOF MOMENTUM MPI np=%d grid %dx%dx%d block %dx%dx%d cut axes: %s%s%s\n", size,
237 NX, NY, NZ, lnx, lny, lnz, cut[0] ? "x" : "", cut[1] ? "y" : "",
238 cut[2] ? "z" : "");
239
240 const Config configs[] = {{"walls-z", true, 1000.0, 0.2, 10},
241 {"shear-per", false, 10.0, 0.2, 40}};
242
243 for (const Config& c : configs) {
244 if (size > 1 && !cut[2]) {
245 if (rank == 0)
246 std::printf(" [%-9s np=%d] FAIL — the decomposition does NOT cut z\n", c.name, size);
247 fail = 1;
248 continue;
249 }
250 IbmSolver sd(lnx, lny, lnz);
251 sd.initMpi(dec, MPI_COMM_WORLD);
252 configure(sd, c, ox, oy, oz, lnx, lny, lnz);
253
254 // THE CONSISTENCY IDENTITY, ON THE FIRST STEP, PER RANK (the `walls-z` configuration, which
255 // is seeded with a uniform velocity): the advection's output must be uniform BITWISE — on
256 // every rank, at every decomposition. That is the statement that the flux at a rank boundary
257 // is computed identically on both sides.
258 sd.step();
259 double identLoc = 0.0;
260 if (c.walls)
261 for (int comp = 0; comp < 3; ++comp)
262 identLoc = std::fmax(identLoc, maxAbsDev(sd.getVofAdvectedVelocity(comp), UNIF[comp]));
263 double ident = 0.0;
264 MPI_Allreduce(&identLoc, &ident, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD);
265
266 const double v0Loc = sumOf(sd.getVof());
267 double v0 = 0.0;
268 MPI_Allreduce(&v0Loc, &v0, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
269 for (int it = 1; it < c.steps; ++it)
270 sd.step();
271 const double v1Loc = sumOf(sd.getVof());
272 double v1 = 0.0;
273 MPI_Allreduce(&v1Loc, &v1, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
274
275 std::vector<double> gu[3], gua[3];
276 for (int comp = 0; comp < 3; ++comp) {
277 gu[comp] = gatherGlobal(sd.getVelocity(comp), ox, oy, oz, lnx, lny, lnz, rank, size);
278 gua[comp] =
279 gatherGlobal(sd.getVofAdvectedVelocity(comp), ox, oy, oz, lnx, lny, lnz, rank, size);
280 }
281 const std::vector<double> gp =
282 gatherGlobal(sd.getPressure(), ox, oy, oz, lnx, lny, lnz, rank, size);
283 const std::vector<double> gc =
284 gatherGlobal(sd.getVof(), ox, oy, oz, lnx, lny, lnz, rank, size);
285
286 if (rank == 0) {
287 if (c.walls)
288 std::printf(" [%-9s np=%d] uniform-velocity identity on the advection: %.17g%s\n",
289 c.name, size, ident, ident == 0.0 ? " (BITWISE, all ranks)" : "");
290 if (!(ident == 0.0)) {
291 std::printf(" [%-9s np=%d] FAIL — the identity is not exact under decomposition\n",
292 c.name, size);
293 fail = 1;
294 }
295 IbmSolver ref(NX, NY, NZ);
296 configure(ref, c, 0, 0, 0, NX, NY, NZ);
297 for (int it = 0; it < c.steps; ++it)
298 ref.step();
299 double du = 0, umag = 0, dua = 0;
300 for (int comp = 0; comp < 3; ++comp) {
301 du = std::fmax(du, maxAbsDiff(gu[comp], ref.getVelocity(comp)));
302 dua = std::fmax(dua, maxAbsDiff(gua[comp], ref.getVofAdvectedVelocity(comp)));
303 umag = std::fmax(umag, maxAbs(ref.getVelocity(comp)));
304 }
305 const double dp = c.walls ? maxAbsDiff(gp, ref.getPressure())
306 : maxAbsDiff(demean(gp), demean(ref.getPressure()));
307 const double dc = maxAbsDiff(gc, ref.getVof());
308 const double pmag = maxAbs(ref.getPressure());
309 const double drift = (v1 - v0) / v0;
310 const double utol = (size == 1) ? 0.0 : std::fmax(1e-12, 1e-11 * umag);
311 const double ptol = (size == 1) ? 0.0 : std::fmax(1e-12, 1e-11 * pmag);
312 const double ctol = (size == 1) ? 0.0 : 1e-11;
313 std::printf(
314 " [%-9s np=%d] du %.3e du_adv %.3e (tol %.1e, |u| %.2e) dp %.3e (tol %.1e, "
315 "|P| %.2e) dC %.3e (tol %.1e) dV/V %+.3e\n",
316 c.name, size, du, dua, utol, umag, dp, ptol, pmag, dc, ctol, drift);
317 if (!(du <= utol) || !(dua <= utol) || !(dp <= ptol) || !(dc <= ctol)) {
318 std::printf(" [%-9s np=%d] FAIL — distributed vs single-rank\n", c.name, size);
319 fail = 1;
320 }
321 if (!c.walls && !(std::fabs(drift) < 1e-11)) {
322 std::printf(" [%-9s np=%d] FAIL — colour volume drift %.3e\n", c.name, size, drift);
323 fail = 1;
324 }
325 }
326 MPI_Bcast(&fail, 1, MPI_INT, 0, MPI_COMM_WORLD);
327 }
328 for (const char* scheme : {"muscl_phase", "muscl_mixed_capped"}) { // G-M2-8
329 const int nAdv = 10;
330 IbmSolver sd(lnx, lny, lnz);
331 sd.initMpi(dec, MPI_COMM_WORLD);
332 configurePhase(sd, scheme, ox, oy, oz, lnx, lny, lnz);
333 long capLoc = 0;
334 for (int k = 0; k < nAdv; ++k) {
336 capLoc += sd.vofMomentumDiagnostics().capped;
337 }
338 long cap = 0;
339 MPI_Allreduce(&capLoc, &cap, 1, MPI_LONG, MPI_SUM, MPI_COMM_WORLD);
340 std::vector<double> gua[3];
341 for (int comp = 0; comp < 3; ++comp)
342 gua[comp] =
343 gatherGlobal(sd.getVofAdvectedVelocity(comp), ox, oy, oz, lnx, lny, lnz, rank, size);
344 const std::vector<double> gc =
345 gatherGlobal(sd.getVof(), ox, oy, oz, lnx, lny, lnz, rank, size);
346 if (rank == 0) {
347 IbmSolver ref(NX, NY, NZ);
348 configurePhase(ref, scheme, 0, 0, 0, NX, NY, NZ);
349 for (int k = 0; k < nAdv; ++k)
350 ref.advectVofMomentum();
351 double dua = 0.0, umag = 0.0;
352 for (int comp = 0; comp < 3; ++comp) {
353 dua = std::fmax(dua, maxAbsDiff(gua[comp], ref.getVofAdvectedVelocity(comp)));
354 umag = std::fmax(umag, maxAbs(ref.getVofAdvectedVelocity(comp)));
355 }
356 const double dc = maxAbsDiff(gc, ref.getVof());
357 std::printf(
358 " [phase-adv np=%d] G-M2-8 %s, %d advections, ratio 1e3: du_adv %.3e "
359 "(|u| %.2e) dC %.3e (== 0 required; capped faces, all ranks %ld)\n",
360 size, scheme, nAdv, dua, umag, dc, cap);
361 if (!(dua == 0.0) || !(dc == 0.0)) {
362 std::printf(
363 " [phase-adv np=%d] FAIL — the %s advection is not bitwise "
364 "single-rank\n",
365 size, scheme);
366 fail = 1;
367 }
368 }
369 MPI_Bcast(&fail, 1, MPI_INT, 0, MPI_COMM_WORLD);
370 }
371 if (rank == 0)
372 std::printf(fail ? "FAILED\n" : "OK\n");
373 }
374 Kokkos::finalize();
375 MPI_Finalize();
376 return fail;
377}
std::vector< double > getVofAdvectedVelocity(int c)
std::vector< double > getVof()
vof::MomentumConsistentAdvector::Diagnostics vofMomentumDiagnostics()
std::vector< double > getVelocity(int c)
std::vector< double > getPressure()
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
long capped
faces whose slope the mass-aware cap scaled (theta < 1), last step
static double colourAt(int, int, int z)
static double forceXAt(int, int, int z)
static double maxAbs(const std::vector< double > &a)
static std::vector< double > demean(std::vector< double > v)
static const double UNIF[3]
static std::vector< double > blockOf(Fn f, int ox, int oy, int oz, int lnx, int lny, int lnz)
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 void configurePhase(IbmSolver &s, const char *scheme, int ox, int oy, int oz, int lnx, int lny, int lnz)
static double maxAbsDev(const std::vector< double > &a, double r)
static double phaseU(int, int y, int z)
static double maxAbsDiff(const std::vector< double > &a, const std::vector< double > &b)
static double sumOf(const std::vector< double > &a)
static double phaseW(int x, int, int)
static double colourAt(int x, int y, int z)
static double phaseV(int, int, int z)
static void configure(IbmSolver &s, const Config &c, int ox, int oy, int oz, int lnx, int lny, int lnz)