flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_vof_surface_tension_mpi.cpp
Go to the documentation of this file.
1// flow — multi-rank validation of the balanced-force CSF (rung V4, WO-P).
2//
3// Two things are gated, and they are different in kind.
4//
5// S1 THE EXACTNESS GATE, AT EVERY DECOMPOSITION. A stationary droplet with a CONSTANT curvature
6// and a uniform density: the surface-tension force is then exactly the discrete gradient of
7// `sigma*kappa*C`, the projection annihilates it, and `max|u|` must sit at MACHINE ZERO on
8// every rank at np = 1, 2 and 4. This is the momentum analogue of the hydrostatic acid test's
9// multi-rank half (`test_vardensity_mpi`'s `walls-z`), and it catches the same class of defect
10// it does: a face force assembled from a ghost the exchange did not fill is invisible in the
11// single-rank run and shows up here as a rank-boundary velocity. The colour field's ghost, the
12// curvature field's ghost and the branch field's ghost all feed that one face value.
13//
14// S2 THE COUPLED STEP AGAINST A FULL-GRID SINGLE-RANK REFERENCE — velocity, pressure, colour AND
15// curvature after 10 steps of a real two-phase run with surface tension on. np = 1 must be
16// BITWISE; np > 1 carries the documented reduction-order floor of the pressure driver's
17// allreduces (`doc/variable_density_projection.md` §3.1), so it is a tolerance comparison —
18// except for kappa and the branch field, which contain no reduction at all and must be bitwise
19// at every np, exactly as `test_vof_curvature_mpi` asserts.
20//
21// Grid 16x16x32: the aligned ORB cuts the long z axis at np = 2 and 4, and the droplet is centred
22// so its interface straddles that cut. `walls-z` puts domain walls on the CUT axis so the colour
23// and curvature ghosts go through the non-periodic (clamped) fill on a rank boundary.
24#include <mpi.h>
25
26#include <cmath>
27#include <cstdio>
28#include <cstring>
29#include <Kokkos_Core.hpp>
30#include <vector>
31
32#include "flow_ibm.hpp"
33#include "peclet/core/common/types.hpp"
34#include "peclet/core/decomp/block_decomposer.hpp"
35
38
39static constexpr int NX = 16, NY = 16, NZ = 32;
40static constexpr std::size_t GCELLS = (std::size_t)NX * NY * NZ;
41static constexpr double RHO_G = 1.0, RHO_L = 10.0, SIGMA = 1.0, MU = 0.05, RDROP = 5.0;
42
43struct Config {
44 const char* name;
45 bool walls; // walls +-z (the CUT axis) vs fully periodic
46 bool constKappa; // S1: freeze kappa at a constant and use a uniform density
47};
48
49// Volume fractions of a sphere at a GLOBAL cell index — a pure function of global indices, so each
50// block gets bit-identical data to the corresponding part of the full-grid run. Exact in z,
51// 16 x 16 subsampled in (x,y) (a crude 3-D subsample manufactures full/empty cell PAIRS whose
52// shared face carries an interface no cell knows about — the CSF's orphan-face defect).
53static double colourAt(const Config& c, int x, int y, int z) {
54 const double cx = NX / 2.0 + 0.137, cy = NY / 2.0 - 0.311;
55 const double cz = c.walls ? NZ / 4.0 + 0.241 : NZ / 2.0 + 0.241;
56 const int NS = 16;
57 double acc = 0.0;
58 for (int j = 0; j < NS; ++j)
59 for (int i = 0; i < NS; ++i) {
60 const double px = x + (i + 0.5) / NS - cx, py = y + (j + 0.5) / NS - cy;
61 const double r2 = RDROP * RDROP - px * px - py * py;
62 if (r2 <= 0.0)
63 continue;
64 const double h = std::sqrt(r2);
65 const double lo = std::fmax(cz - h, (double)z), hi = std::fmin(cz + h, (double)z + 1);
66 if (hi > lo)
67 acc += hi - lo;
68 }
69 return acc / (NS * NS);
70}
71
72template <class Fn>
73static std::vector<double> blockOf(Fn f, int ox, int oy, int oz, int lnx, int lny, int lnz) {
74 std::vector<double> v((std::size_t)lnx * lny * lnz);
75 for (int z = 0; z < lnz; ++z)
76 for (int y = 0; y < lny; ++y)
77 for (int x = 0; x < lnx; ++x)
78 v[(std::size_t)x + (std::size_t)y * lnx + (std::size_t)z * lnx * lny] =
79 f(x + ox, y + oy, z + oz);
80 return v;
81}
82
83static void configure(IbmSolver& s, const Config& c, int ox, int oy, int oz, int lnx, int lny,
84 int lnz) {
85 s.setAdvection(false);
86 const double rl = c.constKappa ? RHO_G : RHO_L;
87 s.setRho(rl);
88 s.setMu(MU);
89 s.setDt(1.0);
90 // The balanced-force exactness gates (max|u| < 1e-13) need the momentum solve at machine
91 // precision: the default residual stop (1e-5, 2026-09-02) leaves a 5e-12 spurious current, so
92 // these tests keep the legacy fixed-sweep loop.
93 s.setVelocityResidualTolerance(0.0);
94 if (c.walls) {
95 s.setDomainBc(4, 1, 0, 0, 0);
96 s.setDomainBc(5, 1, 0, 0, 0); // walls +-z (the CUT axis)
97 }
98 s.setPressureGeometry(std::vector<double>((std::size_t)lnx * lny * lnz, 10.0));
99 s.setPressureChebyshev(true, 400, 1e-14);
100 s.enableVof();
101 s.setVof(blockOf([&](int x, int y, int z) { return colourAt(c, x, y, z); }, ox, oy, oz, lnx, lny,
102 lnz));
103 s.setPropertyModel("rho", ClosureKind::LinearMix, "C", "", {RHO_G, rl - RHO_G});
104 s.setSurfaceTension(SIGMA);
105 if (c.constKappa)
106 s.setVofKappaConstant(2.0 / RDROP);
107 s.setDt(0.5 * s.capillaryDt());
108}
109
110static std::vector<double> gatherGlobal(const std::vector<double>& local, int ox, int oy, int oz,
111 int lnx, int lny, int lnz, int rank, int size) {
112 std::vector<double> global;
113 if (rank == 0)
114 global.assign(GCELLS, 0.0);
115 for (int r = 0; r < size; ++r) {
116 int meta[6] = {ox, oy, oz, lnx, lny, lnz};
117 if (r == 0) {
118 if (rank == 0)
119 for (int z = 0; z < lnz; ++z)
120 for (int y = 0; y < lny; ++y)
121 std::memcpy(&global[(std::size_t)ox + (std::size_t)(y + oy) * NX +
122 (std::size_t)(z + oz) * NX * NY],
123 &local[(std::size_t)y * lnx + (std::size_t)z * lnx * lny],
124 (std::size_t)lnx * sizeof(double));
125 continue;
126 }
127 if (rank == r) {
128 MPI_Send(meta, 6, MPI_INT, 0, 100 + r, MPI_COMM_WORLD);
129 MPI_Send(local.data(), (int)local.size(), MPI_DOUBLE, 0, 200 + r, MPI_COMM_WORLD);
130 } else if (rank == 0) {
131 MPI_Recv(meta, 6, MPI_INT, r, 100 + r, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
132 std::vector<double> buf((std::size_t)meta[3] * meta[4] * meta[5]);
133 MPI_Recv(buf.data(), (int)buf.size(), MPI_DOUBLE, r, 200 + r, MPI_COMM_WORLD,
134 MPI_STATUS_IGNORE);
135 for (int z = 0; z < meta[5]; ++z)
136 for (int y = 0; y < meta[4]; ++y)
137 std::memcpy(&global[(std::size_t)meta[0] + (std::size_t)(y + meta[1]) * NX +
138 (std::size_t)(z + meta[2]) * NX * NY],
139 &buf[(std::size_t)y * meta[3] + (std::size_t)z * meta[3] * meta[4]],
140 (std::size_t)meta[3] * sizeof(double));
141 }
142 }
143 return global;
144}
145
146static double maxDiff(const std::vector<double>& a, const std::vector<double>& b) {
147 double m = 0;
148 for (std::size_t i = 0; i < b.size(); ++i)
149 m = std::fmax(m, std::fabs(a[i] - b[i]));
150 return m;
151}
152static long countBitDiff(const std::vector<double>& a, const std::vector<double>& b, double& mx) {
153 long n = 0;
154 mx = 0.0;
155 for (std::size_t i = 0; i < b.size(); ++i)
156 if (a[i] != b[i]) {
157 ++n;
158 mx = std::fmax(mx, std::fabs(a[i] - b[i]));
159 }
160 return n;
161}
162static double maxAbs(const std::vector<double>& v) {
163 double m = 0;
164 for (double x : v)
165 m = std::fmax(m, std::fabs(x));
166 return m;
167}
168
169int main(int argc, char** argv) {
170 MPI_Init(&argc, &argv);
171 Kokkos::initialize(argc, argv);
172 int fail = 0;
173 {
174 int rank = 0, size = 1;
175 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
176 MPI_Comm_size(MPI_COMM_WORLD, &size);
177
178 auto dec = peclet::flow::CutcellMG::decomposition(static_cast<std::size_t>(size), NX, NY, NZ);
179 auto blk = dec.block(rank);
180 const int ox = (int)blk.origin[0], oy = (int)blk.origin[1], oz = (int)blk.origin[2];
181 const int lnx = (int)blk.size[0], lny = (int)blk.size[1], lnz = (int)blk.size[2];
182 const int gn[3] = {NX, NY, NZ};
183 bool cut[3] = {false, false, false};
184 for (const auto& sz : dec.sizes())
185 for (int a = 0; a < 3; ++a)
186 if ((int)sz[a] != gn[a])
187 cut[a] = true;
188 if (rank == 0)
189 std::printf(
190 "VOF SURFACE TENSION MPI np=%d grid %dx%dx%d block %dx%dx%d cut axes: %s%s%s\n", size,
191 NX, NY, NZ, lnx, lny, lnz, cut[0] ? "x" : "", cut[1] ? "y" : "", cut[2] ? "z" : "");
192
193 const Config configs[] = {{"exact-per", false, true},
194 {"exact-walls-z", true, true},
195 {"drop-per", false, false},
196 {"drop-walls-z", true, false}};
197
198 for (const Config& c : configs) {
199 if (size > 1 && !cut[2]) {
200 if (rank == 0)
201 std::printf(" [%-14s np=%d] FAIL — the decomposition does NOT cut z\n", c.name, size);
202 fail = 1;
203 continue;
204 }
205
206 IbmSolver sd(lnx, lny, lnz);
207 sd.initMpi(dec, MPI_COMM_WORLD);
208 configure(sd, c, ox, oy, oz, lnx, lny, lnz);
209 for (int k = 0; k < 10; ++k)
210 sd.step();
211
212 // S1: the exactness gate is a per-rank statement — reduce the max over ranks.
213 double loc = std::fmax(std::fmax(maxAbs(sd.getVelocity(0)), maxAbs(sd.getVelocity(1))),
214 maxAbs(sd.getVelocity(2)));
215 double gmax = loc;
216 MPI_Allreduce(&loc, &gmax, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD);
217
218 std::vector<double> g[6];
219 const char* fn[6] = {"u", "v", "w", "P", "C", "kappa"};
220 g[0] = gatherGlobal(sd.getVelocity(0), ox, oy, oz, lnx, lny, lnz, rank, size);
221 g[1] = gatherGlobal(sd.getVelocity(1), ox, oy, oz, lnx, lny, lnz, rank, size);
222 g[2] = gatherGlobal(sd.getVelocity(2), ox, oy, oz, lnx, lny, lnz, rank, size);
223 g[3] = gatherGlobal(sd.getPressure(), ox, oy, oz, lnx, lny, lnz, rank, size);
224 g[4] = gatherGlobal(sd.getVof(), ox, oy, oz, lnx, lny, lnz, rank, size);
225 g[5] = gatherGlobal(sd.getVofCurvature(), ox, oy, oz, lnx, lny, lnz, rank, size);
226 const std::vector<double> gb =
227 gatherGlobal(sd.getVofCurvatureBranch(), ox, oy, oz, lnx, lny, lnz, rank, size);
228
229 if (rank == 0) {
230 IbmSolver ref(NX, NY, NZ);
231 configure(ref, c, 0, 0, 0, NX, NY, NZ);
232 for (int k = 0; k < 10; ++k)
233 ref.step();
234 std::vector<double> r[6] = {ref.getVelocity(0), ref.getVelocity(1), ref.getVelocity(2),
235 ref.getPressure(), ref.getVof(), ref.getVofCurvature()};
236 double d[6];
237 for (int i = 0; i < 6; ++i)
238 d[i] = maxDiff(g[i], r[i]);
239 double dkb = 0.0, dkk = 0.0;
240 const long nkb = countBitDiff(gb, ref.getVofCurvatureBranch(), dkb);
241 const long nkk = countBitDiff(g[5], r[5], dkk);
242
243 std::printf(
244 " [%-14s np=%d] max|u| = %.4e du %.2e dv %.2e dw %.2e dP %.2e"
245 " dC %.2e dkappa %.2e\n",
246 c.name, size, gmax, d[0], d[1], d[2], d[3], d[4], d[5]);
247 (void)fn;
248
249 // The BRANCH field is bitwise at every np: the cascade contains no reduction, and the
250 // branch is a discrete label, so a colour perturbed at 1e-15 still lands on the same tier.
251 // kappa itself is bitwise only where the colour is — i.e. at np = 1, and in the frozen
252 // (`exact-*`) configurations at every np. In a COUPLED run at np > 1 the colour carries the
253 // pressure driver's reduction-order residue (dC below) and kappa carries its image; that is
254 // WO-J's documented floor, not a defect of this rung.
255 if (nkb != 0) {
256 std::printf(" FAIL — the curvature BRANCH is decomposition-dependent (%ld cells)\n",
257 nkb);
258 fail = 1;
259 }
260 if (size == 1 && nkk != 0) {
261 std::printf(" FAIL — kappa is not bitwise at np = 1 (%ld cells, max |d| %.3e)\n",
262 nkk, dkk);
263 fail = 1;
264 }
265 if (c.constKappa) {
266 // S1. With a constant kappa the force IS a discrete gradient of sigma*kappa*C. In the
267 // PERIODIC box the semi-implicit momentum operator commutes with that gradient (uniform
268 // rho, no boundary), so the projection annihilates it in ONE step and max|u| is at
269 // machine zero. With WALLS it does not commute — the face-folded viscous operator does
270 // not leave a pressure-gradient field invariant next to a Dirichlet boundary — so the
271 // same fixed point is approached instead of hit: measured 2.1e-10 at 10 steps,
272 // 1.2e-10 at 30, 2.9e-11 at 100, 3.6e-12 at 300, and 1.1e-16 at mu = 0. The multi-rank
273 // statement there is the du/dv/dw/dP comparison below, which is what this test is for.
274 const double tol = c.walls ? 1e-8 : 1e-13;
275 if (!(gmax < tol)) {
276 std::printf(" FAIL — the exactness gate is above %.0e (%.3e)\n", tol, gmax);
277 fail = 1;
278 }
279 }
280 // S2: np = 1 bitwise; np > 1 at the pressure driver's reduction-order floor.
281 const double tolU = (size == 1) ? 0.0 : 1e-12;
282 const double tolP = (size == 1) ? 0.0 : 1e-9;
283 const double tolC = (size == 1) ? 0.0 : 1e-12;
284 const double tolK = (size == 1) ? 0.0 : 1e-12;
285 if (d[5] > tolK) {
286 std::printf(" FAIL — kappa differs by more than the colour's own floor (%.2e)\n",
287 d[5]);
288 fail = 1;
289 }
290 if (d[0] > tolU || d[1] > tolU || d[2] > tolU || d[3] > tolP || d[4] > tolC) {
291 std::printf(
292 " FAIL — the distributed step does not match the single-rank reference"
293 " (tolerances u %.0e P %.0e C %.0e)\n",
294 tolU, tolP, tolC);
295 fail = 1;
296 }
297 }
298 MPI_Bcast(&fail, 1, MPI_INT, 0, MPI_COMM_WORLD);
299 }
300 if (rank == 0)
301 std::printf("%s\n", fail ? "FAILED" : "PASSED");
302 }
303 Kokkos::finalize();
304 MPI_Finalize();
305 return fail;
306}
std::vector< double > getVofCurvature()
The interface curvature kappa = 2H in the caller's units, i.e.
std::vector< double > getVof()
std::vector< double > getVofCurvatureBranch()
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
static double maxAbs(const std::vector< double > &a)
static constexpr double MU
static constexpr double SIGMA
static constexpr double RHO_G
static constexpr double RHO_L
static double maxAbs(const std::vector< double > &v)
static std::vector< double > blockOf(Fn f, int ox, int oy, int oz, int lnx, int lny, int lnz)
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 long countBitDiff(const std::vector< double > &a, const std::vector< double > &b, double &mx)
static double colourAt(const Config &c, int x, int y, int z)
static void configure(IbmSolver &s, const Config &c, int ox, int oy, int oz, int lnx, int lny, int lnz)
static constexpr double RDROP