flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_vof_blocks_ns_mpi.cpp
Go to the documentation of this file.
1// flow — VoF Part III rung W2 (WO-W12) gate 5: the BLOCK container coupled to Navier-Stokes,
2// distributed.
3//
4// The container itself is gated bitwise at np 1/2/4/8 by `test_vof_blocks_mpi` (rung W0/W1, a
5// standalone harness). What is gated HERE is the coupling: the union colour driving the closures,
6// the per-block curvature cascade, and the block CSF face force scattered UNPACK_SUM into the
7// momentum RHS, all inside a real `IbmSolver::step()`.
8//
9// B1 the coupled step against a full-grid single-rank reference — u, v, w, P and the union C
10// after 12 steps of a real two-phase run with the block CSF on. np = 1 must be BITWISE; np > 1
11// carries the documented reduction-order floor of the pressure driver's allreduces
12// (`doc/variable_density_projection.md` §3.1), so it is a tolerance comparison.
13// B2 the per-marker volumes agree with the single-rank run to the reduction floor, and the
14// markers OVERLAP (the union carries less liquid than the markers do) -- the property that
15// makes the whole container worth having, now measured through the NS coupling.
16//
17// Grid 24x24x48 with two bubbles stacked along z: the aligned ORB cuts the long z axis at np = 2
18// and 4, and the bubbles are placed so the cut passes THROUGH one of them -- so a block's gather
19// draws from several owners, its scatter lands on several, and its CSF force is summed across a
20// rank boundary. Masters are round-robin by block id, so at np >= 2 the two markers have different
21// masters.
22#include <mpi.h>
23
24#include <cmath>
25#include <cstdlib>
26#include <cstdio>
27#include <cstring>
28#include <Kokkos_Core.hpp>
29#include <vector>
30
31#include "flow_ibm.hpp"
32#include "peclet/core/common/types.hpp"
33#include "peclet/core/decomp/block_decomposer.hpp"
34
37
38static constexpr int NX = 24, NY = 24, NZ = 48;
39static constexpr std::size_t GCELLS = (std::size_t)NX * NY * NZ;
40// ratio 10, the rung's rated regime with motion; sigma chosen so the capillary dt is not absurd.
41static constexpr double RHO_L = 10.0, RHO_G = 1.0, MU_L = 0.5, MU_G = 0.05, SIGMA = 4.0;
42static constexpr double RB = 4.5, GRAV = 2.0e-3;
43// the two markers, stacked along the CUT axis; the ORB's np=2 cut at z = 24 passes through the
44// upper one's band.
45static const double CX[2] = {NX / 2.0 + 0.13, NX / 2.0 + 0.13};
46static const double CY[2] = {NY / 2.0 - 0.21, NY / 2.0 - 0.21};
47static const double CZ[2] = {14.0 + 0.31, 24.0 + 0.31};
48static const int ZSPLIT = 20; // the seed boxes are clipped here so each marker gets ONE sphere
49
50static double bubbleAt(int b, int x, int y, int z) {
51 const int NS = 12;
52 double acc = 0.0;
53 for (int j = 0; j < NS; ++j)
54 for (int i = 0; i < NS; ++i) {
55 const double px = x + (i + 0.5) / NS - CX[b], py = y + (j + 0.5) / NS - CY[b];
56 const double r2 = RB * RB - px * px - py * py;
57 if (r2 <= 0.0)
58 continue;
59 const double h = std::sqrt(r2);
60 const double lo = std::fmax(CZ[b] - h, (double)z), hi = std::fmin(CZ[b] + h, (double)z + 1);
61 if (hi > lo)
62 acc += hi - lo;
63 }
64 return acc / (NS * NS);
65}
66
67static void configure(IbmSolver& s, int ox, int oy, int oz, int lnx, int lny, int lnz,
68 bool csf) {
69 // pre-flip default (Stokes) kept; NS physics: CHANGELOG "advection default"
70 s.setAdvection(false);
71 s.setRho(RHO_L);
72 s.setMu(MU_L);
73 s.setDomainBc(4, 1, 0, 0, 0);
74 s.setDomainBc(5, 1, 0, 0, 0); // walls +-z (the CUT axis)
75 s.setPressureGeometry(std::vector<double>((std::size_t)lnx * lny * lnz, 10.0));
76 s.setPressureChebyshev(true, 400, 1e-14);
77 s.enableVof();
78 // the UNION convention: C = 1 inside a marker (the dispersed phase)
79 std::vector<double> c((std::size_t)lnx * lny * lnz, 0.0);
80 for (int z = 0; z < lnz; ++z)
81 for (int y = 0; y < lny; ++y)
82 for (int x = 0; x < lnx; ++x) {
83 const double v = std::fmax(bubbleAt(0, x + ox, y + oy, z + oz),
84 bubbleAt(1, x + ox, y + oy, z + oz));
85 c[(std::size_t)x + (std::size_t)y * lnx + (std::size_t)z * lnx * lny] = v;
86 }
87 s.setVof(c);
88 s.setPropertyModel("rho", ClosureKind::LinearMix, "C", "", {RHO_L, RHO_G - RHO_L});
89 s.setPropertyModel("mu", ClosureKind::LinearMix, "C", "", {MU_L, MU_G - MU_L});
90 s.setPropertyModel("force_z", ClosureKind::LinearMix, "C", "",
91 {-RHO_L * GRAV, -(RHO_G - RHO_L) * GRAV});
92 if (csf)
93 s.setSurfaceTension(SIGMA);
94 std::vector<std::array<int, 6>> boxes;
95 for (int b = 0; b < 2; ++b) {
96 std::array<int, 6> q;
97 const double c3[3] = {CX[b], CY[b], CZ[b]};
98 for (int d = 0; d < 3; ++d) {
99 q[d] = (int)std::floor(c3[d] - RB) - 1;
100 q[3 + d] = (int)std::ceil(c3[d] + RB) + 1;
101 }
102 if (b == 0)
103 q[5] = ZSPLIT;
104 else
105 q[2] = ZSPLIT;
106 boxes.push_back(q);
107 }
108 s.enableVofBlocksFromField(boxes);
109 if (csf) {
110 s.enableVofBlockCsf();
111 s.setDt(0.25 * s.capillaryDt());
112 } else {
113 s.setDt(0.05);
114 }
115}
116
117static std::vector<double> gatherGlobal(const std::vector<double>& local, int ox, int oy, int oz,
118 int lnx, int lny, int lnz, int rank, int size) {
119 std::vector<double> global;
120 if (rank == 0)
121 global.assign(GCELLS, 0.0);
122 for (int r = 0; r < size; ++r) {
123 int meta[6] = {ox, oy, oz, lnx, lny, lnz};
124 if (r == 0) {
125 if (rank == 0)
126 for (int z = 0; z < lnz; ++z)
127 for (int y = 0; y < lny; ++y)
128 std::memcpy(&global[(std::size_t)ox + (std::size_t)(y + oy) * NX +
129 (std::size_t)(z + oz) * NX * NY],
130 &local[(std::size_t)y * lnx + (std::size_t)z * lnx * lny],
131 (std::size_t)lnx * sizeof(double));
132 continue;
133 }
134 if (rank == r) {
135 MPI_Send(meta, 6, MPI_INT, 0, 100 + r, MPI_COMM_WORLD);
136 MPI_Send(local.data(), (int)local.size(), MPI_DOUBLE, 0, 200 + r, MPI_COMM_WORLD);
137 } else if (rank == 0) {
138 MPI_Recv(meta, 6, MPI_INT, r, 100 + r, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
139 std::vector<double> buf((std::size_t)meta[3] * meta[4] * meta[5]);
140 MPI_Recv(buf.data(), (int)buf.size(), MPI_DOUBLE, r, 200 + r, MPI_COMM_WORLD,
141 MPI_STATUS_IGNORE);
142 for (int z = 0; z < meta[5]; ++z)
143 for (int y = 0; y < meta[4]; ++y)
144 std::memcpy(&global[(std::size_t)meta[0] + (std::size_t)(y + meta[1]) * NX +
145 (std::size_t)(z + meta[2]) * NX * NY],
146 &buf[(std::size_t)y * meta[3] + (std::size_t)z * meta[3] * meta[4]],
147 (std::size_t)meta[3] * sizeof(double));
148 }
149 }
150 return global;
151}
152
153static double maxDiff(const std::vector<double>& a, const std::vector<double>& b) {
154 double m = 0;
155 for (std::size_t i = 0; i < b.size(); ++i)
156 m = std::fmax(m, std::fabs(a[i] - b[i]));
157 return m;
158}
159static long countBitDiff(const std::vector<double>& a, const std::vector<double>& b, double& mx) {
160 long n = 0;
161 mx = 0.0;
162 for (std::size_t i = 0; i < b.size(); ++i)
163 if (a[i] != b[i]) {
164 ++n;
165 mx = std::fmax(mx, std::fabs(a[i] - b[i]));
166 }
167 return n;
168}
169
170int main(int argc, char** argv) {
171 MPI_Init(&argc, &argv);
172 Kokkos::initialize(argc, argv);
173 int fail = 0;
174 {
175 int rank = 0, size = 1;
176 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
177 MPI_Comm_size(MPI_COMM_WORLD, &size);
178
179 auto dec = peclet::flow::CutcellMG::decomposition(static_cast<std::size_t>(size), NX, NY, NZ);
180 auto blk = dec.block(rank);
181 const int ox = (int)blk.origin[0], oy = (int)blk.origin[1], oz = (int)blk.origin[2];
182 const int lnx = (int)blk.size[0], lny = (int)blk.size[1], lnz = (int)blk.size[2];
183 const int gn[3] = {NX, NY, NZ};
184 bool cut[3] = {false, false, false};
185 for (const auto& sz : dec.sizes())
186 for (int a = 0; a < 3; ++a)
187 if ((int)sz[a] != gn[a])
188 cut[a] = true;
189 if (rank == 0)
190 std::printf(
191 "VOF BLOCK NS MPI (rung W2, WO-W12) np=%d grid %dx%dx%d block %dx%dx%d cut axes: "
192 "%s%s%s\n",
193 size, NX, NY, NZ, lnx, lny, lnz, cut[0] ? "x" : "", cut[1] ? "y" : "", cut[2] ? "z" : "");
194 if (size > 1 && !cut[2]) {
195 if (rank == 0)
196 std::printf(" FAIL - the decomposition does NOT cut z, so no marker is cut\n");
197 fail = 1;
198 }
199
200 const int STEPS = (argc > 1) ? std::atoi(argv[1]) : 12;
201 // cfg 2 (host backends only, WO-H6 review note 8): configuration 0 with BOTH sides pinned to
202 // 'algebraic', so distributed GraphAMG stays gated bitwise against single-rank GraphAMG at
203 // np = 1 -- still the production engine for an outflow face, a bottom above 8192 cells or more
204 // than 64 components -- now that the host default ('auto') takes the 'direct' bottom.
205 constexpr bool hostBackend =
206 std::is_same_v<peclet::flow::CCExec::memory_space, Kokkos::HostSpace>;
207 for (int cfg = 0; cfg < (hostBackend ? 3 : 2); ++cfg) {
208 const bool csf = (cfg == 1);
209 if (rank == 0)
210 std::printf(" --- configuration: block advection + varRho%s%s\n",
211 csf ? " + the BLOCK CSF" : " (surface tension OFF)",
212 cfg == 2 ? ", bottom 'algebraic' on both sides" : "");
213 IbmSolver sd(lnx, lny, lnz);
214 sd.initMpi(dec, MPI_COMM_WORLD);
215 configure(sd, ox, oy, oz, lnx, lny, lnz, csf);
216 if (cfg == 2)
217 sd.setPressureBottomSolver(2); // 'algebraic'
218 // vof_overlap_design G0: the overlap rules must be INERT on this non-colliding pair -- no
219 // clipped curvature, no debris, no marker overlap (so no phantom capillary bound) -- at
220 // every step, on every rank.
221 long nClip = 0, nDebris = 0, nOverlap = 0;
222 for (int k = 0; k < STEPS; ++k) {
223 sd.step();
224 if (csf) {
225 nClip += sd.vofBlockCurvatureStats().clipped;
226 for (const auto& q : sd.vofBlockStats())
227 nDebris += q.debrisCells + q.debrisUnresolved + (q.debrisReturned != 0.0 ? 1 : 0);
228 nOverlap += sd.vofBlockOverlapCensus().cells;
229 }
230 }
231 {
232 long loc[3] = {nClip, nDebris, nOverlap}, glb[3];
233 MPI_Allreduce(loc, glb, 3, MPI_LONG, MPI_SUM, MPI_COMM_WORLD);
234 if (rank == 0)
235 std::printf(" overlap-design counters over %d steps: clipped %ld, debris cells %ld, "
236 "overlap cells %ld\n", STEPS, glb[0], glb[1], glb[2]);
237 if (glb[0] != 0 || glb[1] != 0 || glb[2] != 0) {
238 if (rank == 0)
239 std::printf(" FAIL: an overlap-design rule fired on a non-colliding pair\n");
240 fail = 1;
241 }
242 }
243
244 // per-marker volumes: only the master carries them, so a SUM over ranks is the census
245 const auto stats = sd.vofBlockStats();
246 if (rank == 0)
247 for (const auto& q : stats)
248 std::printf(" dist block %ld master %d box [%d,%d)x[%d,%d)x[%d,%d)\n", q.id, q.master,
249 q.lo[0], q.hi[0], q.lo[1], q.hi[1], q.lo[2], q.hi[2]);
250 std::vector<double> vLoc(stats.size(), 0.0), vAll(stats.size(), 0.0);
251 std::vector<double> aLoc(stats.size(), 0.0), aAll(stats.size(), 0.0);
252 for (std::size_t i = 0; i < stats.size(); ++i) {
253 vLoc[i] = stats[i].volume;
254 aLoc[i] = stats[i].area;
255 }
256 MPI_Allreduce(vLoc.data(), vAll.data(), (int)vLoc.size(), MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
257 MPI_Allreduce(aLoc.data(), aAll.data(), (int)aLoc.size(), MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
258
259 std::vector<double> gf[3];
260 if (csf)
261 for (int c = 0; c < 3; ++c)
262 gf[c] = gatherGlobal(sd.getVofBlockForce(c), ox, oy, oz, lnx, lny, lnz, rank, size);
263 std::vector<double> g[5];
264 g[0] = gatherGlobal(sd.getVelocity(0), ox, oy, oz, lnx, lny, lnz, rank, size);
265 g[1] = gatherGlobal(sd.getVelocity(1), ox, oy, oz, lnx, lny, lnz, rank, size);
266 g[2] = gatherGlobal(sd.getVelocity(2), ox, oy, oz, lnx, lny, lnz, rank, size);
267 g[3] = gatherGlobal(sd.getPressure(), ox, oy, oz, lnx, lny, lnz, rank, size);
268 g[4] = gatherGlobal(sd.getVof(), ox, oy, oz, lnx, lny, lnz, rank, size);
269
270 if (rank == 0) {
271 IbmSolver ref(NX, NY, NZ);
272 configure(ref, 0, 0, 0, NX, NY, NZ, csf);
273 // The distributed solve runs the 'direct' bottom on a host backend since
274 // doc/vof_step_performance_design.md §14.3 H-6 (the allgathered global bottom) and keeps
275 // GraphAMG on a device (§13.9 Q-D7), where the single-rank solver would take the device
276 // bottom; the np = 1 bit-exact gate pins the reference to the engine the distributed path
277 // runs.
278 ref.setPressureBottomSolver(hostBackend && cfg != 2 ? 0 : 2);
279 for (int k = 0; k < STEPS; ++k)
280 ref.step();
281 std::vector<double> r[5] = {ref.getVelocity(0), ref.getVelocity(1), ref.getVelocity(2),
282 ref.getPressure(), ref.getVof()};
283 const char* fn[5] = {"u", "v", "w", "P", "C"};
284 double d[5];
285 long nb[5];
286 double mb[5];
287 for (int i = 0; i < 5; ++i) {
288 d[i] = maxDiff(g[i], r[i]);
289 nb[i] = countBitDiff(g[i], r[i], mb[i]);
290 }
291 std::printf(" np=%d after %d steps:", size, STEPS);
292 for (int i = 0; i < 5; ++i)
293 std::printf(" d%s %.2e", fn[i], d[i]);
294 std::printf("\n");
295 std::printf(" bitwise-differing cells:");
296 for (int i = 0; i < 5; ++i)
297 std::printf(" %s %ld", fn[i], nb[i]);
298 std::printf("\n");
299 if (csf) {
300 double df = 0.0;
301 long nf = 0;
302 for (int c = 0; c < 3; ++c) {
303 const std::vector<double> rf = ref.getVofBlockForce(c);
304 double m = 0.0;
305 nf += countBitDiff(gf[c], rf, m);
306 df = std::fmax(df, maxDiff(gf[c], rf));
307 }
308 std::printf(" scattered CSF face force vs np=1: max|d| %.3e over %ld bit-differing "
309 "cells\n", df, nf);
310 }
311 for (const auto& q : ref.vofBlockStats())
312 std::printf(" ref block %ld box [%d,%d)x[%d,%d)x[%d,%d)\n", q.id, q.lo[0], q.hi[0],
313 q.lo[1], q.hi[1], q.lo[2], q.hi[2]);
314 const auto rs = ref.vofBlockStats();
315 double dv = 0.0, da = 0.0, sumMark = 0.0, sumUnion = 0.0;
316 for (std::size_t i = 0; i < rs.size(); ++i) {
317 dv = std::fmax(dv, std::fabs(vAll[i] - rs[i].volume));
318 da = std::fmax(da, std::fabs(aAll[i] - rs[i].area));
319 sumMark += rs[i].volume;
320 }
321 for (double q : r[4])
322 sumUnion += q;
323 std::printf(" markers: V %.6f / %.6f, area %.2f / %.2f; |dV| vs np=1 %.2e, |dA| %.2e\n",
324 rs[0].volume, rs[1].volume, rs[0].area, rs[1].area, dv, da);
325 std::printf(" UNION deficit sum(V_marker) - sum(C_union) = %.4f cells (the shared liquid; "
326 "> 0 means the two markers OVERLAP, which a single field cannot do)\n",
327 sumMark - sumUnion);
328 // np = 1 bitwise; np > 1 at the pressure driver's own reduction-order floor, the SAME
329 // tolerances `test_vof_surface_tension_mpi` uses for its coupled gate.
330 const double tolU = (size == 1) ? 0.0 : 1e-12;
331 const double tolP = (size == 1) ? 0.0 : 1e-9;
332 const double tolC = (size == 1) ? 0.0 : 1e-12;
333 const double tol[5] = {tolU, tolU, tolU, tolP, tolC};
334 for (int i = 0; i < 5; ++i)
335 if (!(d[i] <= tol[i])) {
336 std::printf(" FAIL: d%s = %.3e exceeds %.1e\n", fn[i], d[i], tol[i]);
337 fail = 1;
338 }
339 if (!(dv <= (size == 1 ? 0.0 : 1e-11))) {
340 std::printf(" FAIL: marker volume drift %.3e\n", dv);
341 fail = 1;
342 }
343 }
344 MPI_Bcast(&fail, 1, MPI_INT, 0, MPI_COMM_WORLD);
345 }
346 if (rank == 0)
347 std::printf("%s\n", fail ? "FAILED" : "PASSED");
348 }
349 Kokkos::finalize();
350 MPI_Finalize();
351 return fail;
352}
std::vector< double > getVof()
vof::VofCurvature::Stats vofBlockCurvatureStats() const
std::vector< double > getVofBlockForce(int c)
std::vector< double > getVelocity(int c)
VofOverlapCensus vofBlockOverlapCensus() const
void setPressureBottomSolver(int engine)
std::vector< vof::VofBlockStats > vofBlockStats() const
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
long clipped
Cells whose curvature the admissibility clip (kappaMax) bounded this call.
static constexpr int STEPS
static constexpr double GRAV
static constexpr double SIGMA
static constexpr double CY
static constexpr double RHO_G
static constexpr double RHO_L
static constexpr double CZ
static constexpr double MU_G
static const int ZSPLIT
static double maxDiff(const std::vector< double > &a, const std::vector< double > &b)
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 configure(IbmSolver &s, int ox, int oy, int oz, int lnx, int lny, int lnz, bool csf)
static long countBitDiff(const std::vector< double > &a, const std::vector< double > &b, double &mx)
static constexpr double RB
static double bubbleAt(int b, int x, int y, int z)
static constexpr double MU_L
static const double CX[2]