flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_vof_cutcell_mpi.cpp
Go to the documentation of this file.
1// flow — VoF rung V5a (WO-Q), multi-rank: the colour field transported THROUGH an immersed solid
2// on a decomposition that CUTS the spheres.
3//
4// What can only break here (nothing below is exercised by `test_vof_twophase_mpi.cpp`, which is
5// solid-free):
6//
7// 1. THE GEOMETRY OF THE COLOUR BLOCK. `eps` and the face openness are built on the solver's
8// G = 2 block and embedded into the advector's g = 3 block — the openness with the SAME
9// one-cell shift the face velocity uses — and then run through the colour field's own ghost
10// policy so that the classification at ghost DEPTH 3 is the owner's. That last step is not
11// cosmetic: the solid-band fill reads fluid neighbours at depth 3, so a locally-guessed
12// classification there would make the INNER result decomposition-dependent. Gated bitwise.
13//
14// 2. THE SOLID-BAND FILL. Three passes with a shrinking depth budget (pass k writes solid cells
15// at ghost depth <= 3-k and reads only fluid cells or cells filled in an EARLIER pass),
16// followed by a second exchange. The shrinking budget is exactly what makes every read fall
17// inside the halo the exchange has already made owner-consistent. Gated bitwise, pointwise,
18// through `vof_filled_color()`.
19//
20// 3. THE OPENNESS-WEIGHTED FLUX ACROSS A RANK BOUNDARY. One flux per face, formed once, so the
21// Weymouth-Yue telescoping — hence conservation — holds across the boundary too. The
22// KINEMATIC configuration below gates the transported colour BITWISE at np 1/2/4 by handing
23// every rank the SAME velocity field: each rank builds the full-grid single-rank solver,
24// runs it to a Stokes steady state (a deterministic computation, identical on every rank) and
25// scatters that velocity into its own block. Without that the velocity itself would carry the
26// pressure solve's reduction-order floor and no bitwise statement about the ADVECTION would be
27// possible.
28//
29// 4. The coupled step (`enable_vof` + closures + gravity through the packing) at the reduction
30// floor, with the colour volume drift measured against the projection's own divergence.
31#include <mpi.h>
32
33#include <cmath>
34#include <cstdio>
35#include <cstdlib>
36#include <cstring>
37#include <Kokkos_Core.hpp>
38#include <vector>
39
40#include "flow_ibm.hpp"
41#include "peclet/core/common/types.hpp"
42#include "peclet/core/decomp/block_decomposer.hpp"
43
45
46static constexpr int NX = 16, NY = 16, NZ = 32;
47static constexpr std::size_t GCELLS = (std::size_t)NX * NY * NZ;
48
49// Four periodic spheres, placed so the ORB's z cut at np = 2 and 4 goes THROUGH them.
50static double sdfAt(double x, double y, double z) {
51 const double cx[4] = {4.0, 11.0, 5.0, 12.0}, cy[4] = {4.0, 11.0, 12.0, 4.0},
52 cz[4] = {8.0, 16.0, 24.0, 30.0}, r = 4.5;
53 double best = 1e30;
54 for (int s = 0; s < 4; ++s)
55 for (int a = -1; a <= 1; ++a)
56 for (int b = -1; b <= 1; ++b)
57 for (int c = -1; c <= 1; ++c) {
58 const double dx = x - (cx[s] + a * NX), dy = y - (cy[s] + b * NY),
59 dz = z - (cz[s] + c * NZ);
60 const double d = std::sqrt(dx * dx + dy * dy + dz * dz) - r;
61 best = d < best ? d : best;
62 }
63 return best;
64}
65static double colourAt(int, int, int z) {
66 return z < NZ / 2 ? 1.0 : 0.0;
67}
68
69template <class Fn>
70static std::vector<double> blockOf(Fn f, int ox, int oy, int oz, int lnx, int lny, int lnz) {
71 std::vector<double> v((std::size_t)lnx * lny * lnz);
72 for (int z = 0; z < lnz; ++z)
73 for (int y = 0; y < lny; ++y)
74 for (int x = 0; x < lnx; ++x)
75 v[(std::size_t)x + (std::size_t)y * lnx + (std::size_t)z * lnx * lny] =
76 f(x + ox, y + oy, z + oz);
77 return v;
78}
79static std::vector<double> sliceOf(const std::vector<double>& g, int ox, int oy, int oz, int lnx,
80 int lny, int lnz) {
81 return blockOf(
82 [&](int x, int y, int z) {
83 return g[(std::size_t)x + (std::size_t)y * NX + (std::size_t)z * NX * NY];
84 },
85 ox, oy, oz, lnx, lny, lnz);
86}
87
88static std::vector<double> gatherGlobal(const std::vector<double>& local, int ox, int oy, int oz,
89 int lnx, int lny, int lnz, int rank, int size) {
90 std::vector<double> global;
91 if (rank == 0)
92 global.assign(GCELLS, 0.0);
93 for (int r = 0; r < size; ++r) {
94 int meta[6] = {ox, oy, oz, lnx, lny, lnz};
95 if (r == 0) {
96 if (rank == 0)
97 for (int z = 0; z < lnz; ++z)
98 for (int y = 0; y < lny; ++y)
99 std::memcpy(&global[(std::size_t)ox + (std::size_t)(y + oy) * NX +
100 (std::size_t)(z + oz) * NX * NY],
101 &local[(std::size_t)y * lnx + (std::size_t)z * lnx * lny],
102 (std::size_t)lnx * sizeof(double));
103 continue;
104 }
105 if (rank == r) {
106 MPI_Send(meta, 6, MPI_INT, 0, 100 + r, MPI_COMM_WORLD);
107 MPI_Send(local.data(), (int)local.size(), MPI_DOUBLE, 0, 200 + r, MPI_COMM_WORLD);
108 } else if (rank == 0) {
109 MPI_Recv(meta, 6, MPI_INT, r, 100 + r, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
110 std::vector<double> buf((std::size_t)meta[3] * meta[4] * meta[5]);
111 MPI_Recv(buf.data(), (int)buf.size(), MPI_DOUBLE, r, 200 + r, MPI_COMM_WORLD,
112 MPI_STATUS_IGNORE);
113 for (int z = 0; z < meta[5]; ++z)
114 for (int y = 0; y < meta[4]; ++y)
115 std::memcpy(&global[(std::size_t)meta[0] + (std::size_t)(y + meta[1]) * NX +
116 (std::size_t)(z + meta[2]) * NX * NY],
117 &buf[(std::size_t)y * meta[3] + (std::size_t)z * meta[3] * meta[4]],
118 (std::size_t)meta[3] * sizeof(double));
119 }
120 }
121 return global;
122}
123
124static double maxAbsDiff(const std::vector<double>& a, const std::vector<double>& b) {
125 // WO-R2: NaN-PROPAGATING. `std::fmax(m, NaN) == m`, so the obvious loop returns 0.000e+00 for a
126 // field that has gone entirely NaN and every bitwise gate built on it passes (WO-R found this on
127 // a drained open-boundary run). A non-finite difference must fail, so return it.
128 double m = 0;
129 for (std::size_t i = 0; i < b.size(); ++i) {
130 const double d = std::fabs(a[i] - b[i]);
131 if (!(d == d))
132 return d; // NaN
133 m = std::fmax(m, d);
134 }
135 return m;
136}
137static double sumOf(const std::vector<double>& a) {
138 double s = 0;
139 for (double v : a)
140 s += v;
141 return s;
142}
143
144int main(int argc, char** argv) {
145 std::setvbuf(stdout, nullptr, _IONBF, 0);
146 MPI_Init(&argc, &argv);
147 Kokkos::initialize(argc, argv);
148 int fail = 0;
149 {
150 int rank = 0, size = 1;
151 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
152 MPI_Comm_size(MPI_COMM_WORLD, &size);
153 auto dec = peclet::flow::CutcellMG::decomposition(static_cast<std::size_t>(size), NX, NY, NZ);
154 auto blk = dec.block(rank);
155 const int ox = (int)blk.origin[0], oy = (int)blk.origin[1], oz = (int)blk.origin[2];
156 const int lnx = (int)blk.size[0], lny = (int)blk.size[1], lnz = (int)blk.size[2];
157 const int gn[3] = {NX, NY, NZ};
158 bool cut[3] = {false, false, false};
159 for (const auto& sz : dec.sizes())
160 for (int a = 0; a < 3; ++a)
161 if ((int)sz[a] != gn[a])
162 cut[a] = true;
163 if (rank == 0)
164 std::printf("VOF CUTCELL MPI np=%d grid %dx%dx%d block %dx%dx%d cut axes: %s%s%s\n", size,
165 NX, NY, NZ, lnx, lny, lnz, cut[0] ? "x" : "", cut[1] ? "y" : "",
166 cut[2] ? "z" : "");
167 if (size > 1 && !cut[2]) {
168 if (rank == 0)
169 std::printf(" FAIL — the decomposition does not cut z, so no sphere is cut\n");
170 fail = 1;
171 }
172
173 const std::vector<double> gSdf = blockOf(
174 [](int x, int y, int z) { return sdfAt(x + 0.5, y + 0.5, z + 0.5); }, 0, 0, 0, NX, NY, NZ);
175
176 // ---- the deterministic Stokes velocity, computed identically on EVERY rank ----------------
177 // A full-grid single-rank solver: the same arithmetic on every rank, so the velocity that
178 // drives the kinematic advection carries no reduction-order dependence and the colour gate can
179 // be BITWISE rather than "at the floor".
180 std::vector<double> gU[3];
181 std::vector<double> refColour;
182 double refDrift = 0.0, refDiv = 0.0;
183 const int kinSteps = 40;
184 {
185 IbmSolver ref(NX, NY, NZ);
186 ref.setAdvection(false);
187 ref.setRho(1.0);
188 ref.setMu(0.2);
189 ref.setDt(1.0);
190 ref.setBodyForce(2e-3, 1e-3, 5e-4);
191 ref.setSolid(gSdf, true);
192 for (int i = 0; i < 40; ++i)
193 ref.step();
194 refDiv = ref.maxOpenDivergence();
195 for (int c = 0; c < 3; ++c)
196 gU[c] = ref.getVelocity(c);
197 ref.enableVof();
198 ref.setVof(blockOf(colourAt, 0, 0, 0, NX, NY, NZ));
199 const double v0 = ref.vofDiagnostics().volume;
200 const double dtA = 0.2 / ref.vofMaxCourant();
201 for (int i = 0; i < kinSteps; ++i)
202 ref.advectVofKinematic(dtA);
203 const auto d1 = ref.vofDiagnostics();
204 refDrift = (d1.volume - v0) / v0;
205 refColour = ref.getVof();
206 if (rank == 0)
207 std::printf(
208 " reference (full grid, every rank): max|div(open u)| %.3e, %d kinematic steps at "
209 "dt %.4g, drift %.3e, clipped %.3e\n",
210 refDiv, kinSteps, dtA, refDrift, d1.clippedVolume);
211 }
212
213 // ---- distributed kinematic run on the SAME velocity ---------------------------------------
214 {
215 IbmSolver sd(lnx, lny, lnz);
216 sd.setAdvection(false);
217 sd.initMpi(dec, MPI_COMM_WORLD);
218 sd.setRho(1.0);
219 sd.setMu(0.2);
220 sd.setDt(1.0);
221 sd.setSolid(sliceOf(gSdf, ox, oy, oz, lnx, lny, lnz), true);
222 sd.enableVof();
223 // geometry, pointwise: eps, the three face openness fields and the classification
224 double dg = 0.0;
225 {
226 IbmSolver r2(NX, NY, NZ);
227 r2.setAdvection(false);
228 r2.setRho(1.0);
229 r2.setMu(0.2);
230 r2.setDt(1.0);
231 r2.setSolid(gSdf, true);
232 r2.enableVof();
233 for (int w = 0; w < 5; ++w) {
234 const auto g = gatherGlobal(sd.getVofGeometry(w), ox, oy, oz, lnx, lny, lnz, rank, size);
235 if (rank == 0)
236 dg = std::fmax(dg, maxAbsDiff(g, r2.getVofGeometry(w)));
237 }
238 }
239 for (int c = 0; c < 3; ++c)
240 sd.setField(c == 0 ? "u" : (c == 1 ? "v" : "w"), sliceOf(gU[c], ox, oy, oz, lnx, lny, lnz));
241 sd.setVof(blockOf(colourAt, ox, oy, oz, lnx, lny, lnz));
242 // the solid-band fill, pointwise
243 double dfill = 0.0;
244 {
245 const auto g = gatherGlobal(sd.getVofFilledColour(), ox, oy, oz, lnx, lny, lnz, rank, size);
246 if (rank == 0) {
247 IbmSolver r2(NX, NY, NZ);
248 r2.setAdvection(false);
249 r2.setRho(1.0);
250 r2.setMu(0.2);
251 r2.setDt(1.0);
252 r2.setSolid(gSdf, true);
253 r2.enableVof();
254 r2.setVof(blockOf(colourAt, 0, 0, 0, NX, NY, NZ));
255 dfill = maxAbsDiff(g, r2.getVofFilledColour());
256 }
257 }
258 const double divD = sd.maxOpenDivergence();
259 const double locV0 = sd.vofDiagnostics().volume;
260 double v0 = 0.0;
261 MPI_Allreduce(&locV0, &v0, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
262 double cflLoc = sd.vofMaxCourant();
263 const double dtA = 0.2 / cflLoc;
264 for (int i = 0; i < kinSteps; ++i)
265 sd.advectVofKinematic(dtA);
266 const double locV1 = sd.vofDiagnostics().volume;
267 double v1 = 0.0;
268 MPI_Allreduce(&locV1, &v1, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
269 const auto gc = gatherGlobal(sd.getVof(), ox, oy, oz, lnx, lny, lnz, rank, size);
270 if (rank == 0) {
271 const double dc = maxAbsDiff(gc, refColour);
272 const double drift = (v1 - v0) / v0;
273 std::printf(
274 " [kinematic np=%d] geometry %.3e band fill %.3e colour %.3e "
275 "drift %.3e (ref %.3e) max|div| %.3e\n",
276 size, dg, dfill, dc, drift, refDrift, divD);
277 if (!(dg == 0.0) || !(dfill == 0.0) || !(dc == 0.0)) {
278 std::printf(" [kinematic np=%d] FAIL — not bitwise\n", size);
279 fail = 1;
280 }
281 if (!(std::fabs(drift) <= 1e-11)) {
282 std::printf(" [kinematic np=%d] FAIL — colour volume drift %.3e\n", size, drift);
283 fail = 1;
284 }
285 }
286 }
287
288 // ---- coupled draining through the packing (reduction floor) --------------------------------
289 {
290 const int steps = 30;
291 const double ratio = 10.0, grav = 2e-3;
292 auto setup = [&](IbmSolver& s, int aox, int aoy, int aoz, int anx, int any, int anz) {
293 s.setRho(1.0);
294 s.setMu(0.05);
295 s.setDt(0.5);
296 s.setAdvection(true);
297 s.setSolid(sliceOf(gSdf, aox, aoy, aoz, anx, any, anz), true);
298 s.setVof(blockOf(colourAt, aox, aoy, aoz, anx, any, anz));
299 s.setPropertyModel("rho", peclet::flow::ClosureKind::LinearMix, "C", "",
300 {1.0, ratio - 1.0});
301 s.setPropertyModel("mu", peclet::flow::ClosureKind::LinearMix, "C", "", {0.05, 0.45});
302 s.setPropertyModel("force_z", peclet::flow::ClosureKind::LinearMix, "rho", "",
303 {0.0, -grav});
304 };
305 IbmSolver sd(lnx, lny, lnz);
306 sd.initMpi(dec, MPI_COMM_WORLD);
307 setup(sd, ox, oy, oz, lnx, lny, lnz);
308 const double lv0 = sd.vofDiagnostics().volume;
309 double v0 = 0.0;
310 MPI_Allreduce(&lv0, &v0, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
311 long maxIt = 0;
312 for (int i = 0; i < steps; ++i) {
313 sd.step();
314 maxIt = std::max(maxIt, sd.lastPressureIterations());
315 }
316 const double lv1 = sd.vofDiagnostics().volume;
317 double v1 = 0.0;
318 MPI_Allreduce(&lv1, &v1, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
319 const double lsolid = sd.vofDiagnostics().solidSumC;
320 double solid = 0.0;
321 MPI_Allreduce(&lsolid, &solid, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
322 const double divD = sd.maxOpenDivergence();
323 const auto gc = gatherGlobal(sd.getVof(), ox, oy, oz, lnx, lny, lnz, rank, size);
324 std::vector<double> gu[3];
325 for (int c = 0; c < 3; ++c)
326 gu[c] = gatherGlobal(sd.getVelocity(c), ox, oy, oz, lnx, lny, lnz, rank, size);
327 if (rank == 0) {
328 IbmSolver ref(NX, NY, NZ);
329 setup(ref, 0, 0, 0, NX, NY, NZ);
330 for (int i = 0; i < steps; ++i)
331 ref.step();
332 double du = 0;
333 for (int c = 0; c < 3; ++c)
334 du = std::fmax(du, maxAbsDiff(gu[c], ref.getVelocity(c)));
335 const double dc = maxAbsDiff(gc, ref.getVof());
336 const double drift = (v1 - v0) / v0;
337 const double tol = size == 1 ? 0.0 : 1e-9;
338 std::printf(
339 " [coupled np=%d] colour %.3e velocity %.3e drift %.3e solid sum C %.3e "
340 "max|div| %.3e pressure iters max %ld\n",
341 size, dc, du, drift, solid, divD, maxIt);
342 if (!(dc <= tol) || !(du <= tol)) {
343 std::printf(" [coupled np=%d] FAIL — colour %.3e velocity %.3e above tol %.1e\n", size,
344 dc, du, tol);
345 fail = 1;
346 }
347 if (!(solid == 0.0)) {
348 std::printf(" [coupled np=%d] FAIL — colour in solid cells %.3e\n", size, solid);
349 fail = 1;
350 }
351 if (!(std::fabs(drift) <= 1e-8)) {
352 std::printf(" [coupled np=%d] FAIL — colour drift %.3e\n", size, drift);
353 fail = 1;
354 }
355 }
356 }
357 int g = 0;
358 MPI_Allreduce(&fail, &g, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD);
359 fail = g;
360 if (rank == 0)
361 std::printf(fail ? "VOF CUTCELL MPI: FAILED\n" : "VOF CUTCELL MPI: all checks passed\n");
362 }
363 Kokkos::finalize();
364 MPI_Finalize();
365 return fail;
366}
void setSolid(const std::vector< double > &sdfInner, bool cutcellPressure)
Host entry point: upload the inner SDF once and delegate.
std::vector< double > getVofFilledColour()
void setAdvection(bool on)
std::vector< double > getVofGeometry(int which)
std::vector< double > getVof()
void setVof(const std::vector< double > &c)
void setField(const std::string &name, const std::vector< double > &v)
std::vector< double > getVelocity(int c)
void setMu(double m)
Dynamic viscosity, in the caller's units.
void advectVofKinematic(double dtPhysArg)
dt is in the caller's time unit.
void setRho(double r)
Fluid density, in the caller's units.
vof::WyAdvector::Diagnostics vofDiagnostics()
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
double solidSumC
filled by the SOLVER from the canonical G=2 colour field
double volume
sum of eps_eff*C over inner FLUID cells: the EXACTLY conserved functional of the cut-cell scheme (cut...
static double colourAt(int, int, int z)
static double colourAt(int, int, int z)
static std::vector< double > blockOf(Fn f, int ox, int oy, int oz, int lnx, int lny, int lnz)
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 std::vector< double > sliceOf(const std::vector< double > &g, int ox, int oy, int oz, int lnx, int lny, int lnz)
static double maxAbsDiff(const std::vector< double > &a, const std::vector< double > &b)
static double sumOf(const std::vector< double > &a)
static double sdfAt(double x, double y, double z)