flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_vof_phase_change_mpi.cpp
Go to the documentation of this file.
1// flow — VoF Part II rungs P0/P1 (WO-P01), multi-rank: phase change on a decomposition that CUTS
2// the interface.
3//
4// What can only break here:
5//
6// 1. THE PER-CELL INTERFACE DATA AT DEPTH 1 AND 2. `mdot`, the PLIC polygon area and the unit
7// normal are computed on INNER cells only and then halo-exchanged, because two consumers read
8// them outside the inner region: the clip-and-redistribute pass (a deficit may be pushed
9// ACROSS a rank boundary, so the receiving rank must recompute the donor's whole allocation)
10// and the divergence-source gather (the donor may be up to two cells outside). Both are
11// written as GATHERS with a fixed summation order — never an atomic scatter — so the result
12// is bitwise, not "at the reduction floor".
13//
14// 2. THE NON-PERIODIC GHOST. On a wall/inflow/outflow face the halo's periodic wrap would import
15// the far side's interface as a phantom donor. `pcZeroDomainGhosts` kills that, per rank-OWNED
16// face; the P1 configuration below has walls on +-x and a decomposition that cuts x.
17//
18// 3. THE PER-CELL DIRICHLET MASK of the energy scalar, which is rebuilt from the colour on every
19// rank and must agree across a boundary.
20//
21// Gates: P0a (planar regression, 1000 steps) BITWISE against the full-grid single-rank run, and P1
22// (the Stefan problem with the thermal mass flux) at the reduction floor — the energy solve's
23// red-black Gauss-Seidel is the only thing in the loop whose result depends on the decomposition,
24// and it does so at the level of its own unconverged residual.
25#include <mpi.h>
26
27#include <cmath>
28#include <cstdio>
29#include <cstdlib>
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 = 64, NY = 4, NZ = 4;
41
42static int failures = 0;
43#define CHECK(cond) \
44 do { \
45 if (!(cond)) { \
46 std::fprintf(stderr, "CHECK failed: %s\n at %s:%d\n", #cond, __FILE__, __LINE__); \
47 ++failures; \
48 } \
49 } while (0)
50
51static std::size_t gidx(int x, int y, int z) {
52 return (std::size_t)x + (std::size_t)y * NX + (std::size_t)z * NX * NY;
53}
54
55template <class Fn>
56static std::vector<double> blockOf(Fn f, int ox, int oy, int oz, int lnx, int lny, int lnz) {
57 std::vector<double> v((std::size_t)lnx * lny * lnz);
58 for (int z = 0; z < lnz; ++z)
59 for (int y = 0; y < lny; ++y)
60 for (int x = 0; x < lnx; ++x)
61 v[(std::size_t)x + (std::size_t)y * lnx + (std::size_t)z * lnx * lny] =
62 f(x + ox, y + oy, z + oz);
63 return v;
64}
65static std::vector<double> sliceOf(const std::vector<double>& g, int ox, int oy, int oz, int lnx,
66 int lny, int lnz) {
67 return blockOf([&](int x, int y, int z) { return g[gidx(x, y, z)]; }, ox, oy, oz, lnx, lny, lnz);
68}
69
70// Bitwise comparison of a distributed field against the global reference, reduced over ranks.
71static double blockDiff(const std::vector<double>& local, const std::vector<double>& global, int ox,
72 int oy, int oz, int lnx, int lny, int lnz) {
73 double m = 0.0;
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 m = std::fmax(
78 m, std::fabs(local[(std::size_t)x + (std::size_t)y * lnx + (std::size_t)z * lnx * lny] -
79 global[gidx(x + ox, y + oy, z + oz)]));
80 double g = 0.0;
81 MPI_Allreduce(&m, &g, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD);
82 return g;
83}
84
85static double stefanLambda(double St) {
86 const double rhs = St / std::sqrt(M_PI);
87 double a = 1e-8, b = 5.0;
88 for (int i = 0; i < 200; ++i) {
89 const double m = 0.5 * (a + b);
90 ((m * std::exp(m * m) * std::erf(m) - rhs) > 0 ? b : a) = m;
91 }
92 return 0.5 * (a + b);
93}
94
95// WO-P3c: run every scene below with a non-default interfacial-AREA geometry
96// (`set_phase_change_area`) when `PECLET_P3C_AREA` is set, so the planar rungs can be re-taken on
97// the cascade area without a second binary. Inert (and byte-identical) when the variable is unset.
98template <class S>
99void applyAreaModeEnv(S& s) {
100 if (const char* e = std::getenv("PECLET_P3C_AREA"))
101 s.setPhaseChangeArea(std::atoi(e));
102}
103
104// WO-P3f: the same hook for this work order's two OPTIONS, so the planar rungs and the MPI
105// decomposition gate can be re-taken with them on without a second binary. Both are inert (and
106// byte-identical) when the variables are unset. `PECLET_P3F_CARRY=1` turns on the
107// enthalpy-conserving Dirichlet overwrite (a fixed-order gather over face neighbours: the piece
108// with a real neighbour dependency, hence the one the decomposition gate is about);
109// `PECLET_P3F_KAPPA=<k>` prescribes the curvature the one-sided fits correct their sample
110// distances with (a purely per-cell function of the existing stencil, no reduction).
111template <class S>
112void applyP3fEnv(S& s) {
113 if (const char* e = std::getenv("PECLET_P3F_CARRY"))
114 s.setPhaseChangeCarryConserve(e[0] != '0');
115 if (const char* e = std::getenv("PECLET_P3F_KAPPA"))
116 s.setPhaseChangeFitCurvature(std::atof(e));
117 // WO-P3g: the second-order interfacial energy operator through the SAME hook, applied to both
118 // the reference and the distributed solver. `PECLET_P3G_ORDER=2` is the package; the three
119 // singles are the ablation. Inert (byte-identical) when unset.
120 if (const char* e = std::getenv("PECLET_P3G_ORDER"))
121 s.setPhaseChangeEnergyOrder(std::atoi(e));
122 if (const char* e = std::getenv("PECLET_P3G_OPMDOT"))
123 s.setPhaseChangeMdotOperator(e[0] != '0');
124 if (const char* e = std::getenv("PECLET_P3G_GFMORDER"))
125 s.setPhaseChangeGfmOrder(std::atoi(e));
126 if (const char* e = std::getenv("PECLET_P3G_CURVDIST"))
127 s.setPhaseChangeCurvatureDistance(e[0] != '0');
128}
129
130int main(int argc, char** argv) {
131 std::setvbuf(stdout, nullptr, _IONBF, 0);
132 MPI_Init(&argc, &argv);
133 Kokkos::initialize(argc, argv);
134 {
135 int rank = 0, size = 1;
136 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
137 MPI_Comm_size(MPI_COMM_WORLD, &size);
138 auto dec = peclet::flow::CutcellMG::decomposition(static_cast<std::size_t>(size), NX, NY, NZ);
139 auto blk = dec.block(rank);
140 const int ox = (int)blk.origin[0], oy = (int)blk.origin[1], oz = (int)blk.origin[2];
141 const int lnx = (int)blk.size[0], lny = (int)blk.size[1], lnz = (int)blk.size[2];
142 bool cutx = false;
143 for (const auto& sz : dec.sizes())
144 if ((int)sz[0] != NX)
145 cutx = true;
146 if (rank == 0)
147 std::printf("VOF PHASE CHANGE MPI np=%d grid %dx%dx%d block %dx%dx%d x cut: %s\n", size,
148 NX, NY, NZ, lnx, lny, lnz, cutx ? "yes" : "no");
149 if (size > 1)
150 CHECK(cutx); // the interface is normal to x: the decomposition MUST cut it
151
152 // ================================================================ P0a, bitwise
153 {
154 const double x0 = 32.25, mdot = 0.02, dt = 1.0;
155 auto colour = [&](int x, int, int) { return std::fmin(1.0, std::fmax(0.0, x0 - x)); };
156 std::vector<double> refC;
157 { // the full-grid single-rank run, on every rank (identical arithmetic, no reduction)
158 IbmSolver ref(NX, NY, NZ);
159 ref.setAdvection(false);
160 ref.setRho(1.0);
161 ref.setMu(0.01);
162 ref.setDt(dt);
163 ref.enableVof();
164 ref.setVof(blockOf(colour, 0, 0, 0, NX, NY, NZ));
165 ref.enablePhaseChange(1.0, 1.0, 1.0);
166 applyAreaModeEnv(ref);
167 applyP3fEnv(ref);
168 ref.setMassFluxUniform(mdot);
169 for (int k = 0; k < 1000; ++k)
170 ref.applyPhaseChange(dt);
171 refC = ref.getVof();
172 }
173 IbmSolver sd(lnx, lny, lnz);
174 sd.setAdvection(false);
175 sd.initMpi(dec, MPI_COMM_WORLD);
176 sd.setRho(1.0);
177 sd.setMu(0.01);
178 sd.setDt(dt);
179 sd.enableVof();
180 sd.setVof(blockOf(colour, ox, oy, oz, lnx, lny, lnz));
181 sd.enablePhaseChange(1.0, 1.0, 1.0);
183 applyP3fEnv(sd);
184 sd.setMassFluxUniform(mdot);
185 for (int k = 0; k < 1000; ++k)
186 sd.applyPhaseChange(dt);
187 const double d = blockDiff(sd.getVof(), refC, ox, oy, oz, lnx, lny, lnz);
188 double refSum = 0.0;
189 for (double v : refC)
190 refSum += v;
191 if (rank == 0)
192 std::printf(
193 " P0a 1000 steps: max |C_dist - C_ref| = %.3e (bitwise expected); x_ref = "
194 "%.15g, exact %.15g\n",
195 d, refSum / (NY * NZ), x0 - mdot * dt * 1000);
196 CHECK(d == 0.0);
197 }
198
199 // ================================================================ P1 Stefan, walls on +-x
200 {
201 const double St = 1.0, alpha = 1.0, x0p = 0.10, xep = 0.25, Fo = 0.5;
202 const double lam = stefanLambda(St);
203 const double t0 = (x0p / (2 * lam)) * (x0p / (2 * lam)) / alpha;
204 const double te = (xep / (2 * lam)) * (xep / (2 * lam)) / alpha;
205 const double D = alpha * NX * NX;
206 const int ns = (int)std::llround((te - t0) / (Fo / D));
207 const double dt = (te - t0) / ns;
208 const double xg = x0p * NX;
209 auto colour = [&](int x, int, int) { return std::fmin(1.0, std::fmax(0.0, (x + 1) - xg)); };
210 auto temp = [&](int x, int, int) {
211 const double xp = (x + 0.5) / NX;
212 return xp < x0p ? 1.0 - std::erf(lam * xp / x0p) / std::erf(lam) : 0.0;
213 };
214 auto configure = [&](IbmSolver& s, int nx, int ny, int nz, int Ox, int Oy, int Oz) {
215 s.setAdvection(false);
216 s.setRho(1.0);
217 s.setMu(1e-3);
218 s.setDt(dt);
219 s.setDomainBc(0, 1, 0, 0, 0);
220 s.setDomainBc(1, 1, 0, 0, 0);
221 s.setPressureGeometry(std::vector<double>((std::size_t)nx * ny * nz, 1.0));
222 s.enableVof();
223 s.setVof(blockOf(colour, Ox, Oy, Oz, nx, ny, nz));
224 s.addScalar("T", D, 1, 60);
225 s.setScalarBc("T", 0, 2, 1.0);
226 s.setScalarBc("T", 1, 2, 0.0);
227 s.setField("T", blockOf(temp, Ox, Oy, Oz, nx, ny, nz));
228 s.enablePhaseChange(1.0, 1.0, 1.0);
230 applyP3fEnv(s);
231 s.setPhaseChangeThermal("T", 0.0, D, D, 0.0);
232 };
233 std::vector<double> refC;
234 {
235 IbmSolver ref(NX, NY, NZ);
236 configure(ref, NX, NY, NZ, 0, 0, 0);
237 for (int k = 0; k < ns; ++k) {
238 ref.applyPhaseChange(dt);
239 ref.advanceScalars();
240 }
241 refC = ref.getVof();
242 }
243 IbmSolver sd(lnx, lny, lnz);
244 sd.initMpi(dec, MPI_COMM_WORLD);
245 configure(sd, lnx, lny, lnz, ox, oy, oz);
246 for (int k = 0; k < ns; ++k) {
247 sd.applyPhaseChange(dt);
248 sd.advanceScalars();
249 }
250 const double d = blockDiff(sd.getVof(), refC, ox, oy, oz, lnx, lny, lnz);
251 double refSum = 0.0;
252 for (double v : refC)
253 refSum += v;
254 const double xExact = 2 * lam * std::sqrt(alpha * te) * NX;
255 if (rank == 0)
256 std::printf(
257 " P1 Stefan %d steps: max |C_dist - C_ref| = %.3e (RB-GS reduction floor); layer_ref "
258 "= %.5f, exact %.5f (%+.4f %%)\n",
259 ns, d, NX - refSum / (NY * NZ), xExact,
260 100.0 * ((NX - refSum / (NY * NZ)) - xExact) / xExact);
261 CHECK(d < 1e-9);
262 }
263
264 // ================================================================ P2 sucking interface
265 // (WO-P23) Everything rung P2/P3 adds, distributed and on a decomposition that cuts the
266 // interface: the plane-anchored (ghost-fluid) Dirichlet rows (whose theta reads the NEIGHBOUR
267 // cell's plane normal and centre distance, i.e. depth-1 data that has to be exchanged and
268 // domain-zeroed), the quadratic one-sided fit, the per-phase k(C) / rho c_p(C) operator, and
269 // the CONSISTENT rho c_p T transport, which rides the colour advector's own g = 3 block and
270 // therefore its own halo. Coupled steps, so the pressure solve is in the loop too.
271 {
272 const char* xe = std::getenv("PECLET_P23_XEP");
273 const double ratio = 10.0, ja = 1.0, alpha_l = 1.0, x0p = 0.10, Fo = 0.5;
274 const double xep = xe ? std::atof(xe) : 0.14;
275 const double cfl = 0.2, rr = 1.0 / ratio;
276 double b = ja / std::sqrt(M_PI);
277 for (int i = 0; i < 300; ++i) {
278 const double g = b * rr;
279 b = ja * std::exp(-g * g) / (std::sqrt(M_PI) * std::erfc(g));
280 }
281 const double t0 = (x0p / (2 * b)) * (x0p / (2 * b)) / alpha_l;
282 const double te = (xep / (2 * b)) * (xep / (2 * b)) / alpha_l;
283 const double al = alpha_l * NX * NX, rho_l = 1.0, rho_v = rr, cpl = 1.0;
284 const double k_l = al * rho_l * cpl, k_v = k_l / ratio, dT = 1.0;
285 const double h_lv = rho_l * cpl * dT / (rho_v * ja);
286 const double X0 = x0p * NX;
287 auto colour = [&](int x, int, int) { return std::fmin(1.0, std::fmax(0.0, (x + 1) - X0)); };
288 auto temp = [&](int x, int, int) {
289 const double xp = (x + 0.5) / NX;
290 if (!(xp > x0p))
291 return 0.0;
292 const double sv = xp / (2 * std::sqrt(alpha_l * t0)) - b * (1.0 - rr);
293 return dT - dT * std::erfc(sv) / std::erfc(b * rr);
294 };
295 auto farT = [&](double tt) {
296 const double sv = 1.0 / (2 * std::sqrt(alpha_l * tt)) - b * (1.0 - rr);
297 return dT - dT * std::erfc(sv) / std::erfc(b * rr);
298 };
299 auto configure = [&](IbmSolver& s, int nx, int ny, int nz, int Ox, int Oy, int Oz) {
300 s.setAdvection(false);
301 s.setRho(rho_l);
302 s.setMu(1e-3);
303 s.setDomainBc(0, 1, 0, 0, 0);
304 s.setDomainBc(1, 3, 0, 0, 0);
305 s.setPressureGeometry(std::vector<double>((std::size_t)nx * ny * nz, 1.0));
306 s.enableVof();
307 s.setVof(blockOf(colour, Ox, Oy, Oz, nx, ny, nz));
308 s.setPropertyModel("rho", peclet::flow::ClosureKind::LinearMix, "C", "",
309 {rho_v, rho_l - rho_v});
310 s.setPressureFcg(true, 4000, 1e-10);
311 s.addScalar("T", al, 1, 60);
312 s.setScalarBc("T", 0, 2, 0.0);
313 s.setScalarBc("T", 1, 2, farT(t0));
314 s.setField("T", blockOf(temp, Ox, Oy, Oz, nx, ny, nz));
315 s.enablePhaseChange(rho_v, rho_l, h_lv);
317 applyP3fEnv(s);
318 s.setPhaseChangeThermal("T", 0.0, k_v, k_l, 0.0);
319 // Probe switches (findings only): PECLET_P23_OFF is a subset of "pqe" to disable —
320 // p = the plane-anchored Dirichlet, q = the quadratic fit, e = the consistent rho c_p T.
321 const char* off = std::getenv("PECLET_P23_OFF");
322 const bool offP = off && std::strchr(off, 'p');
323 const bool offQ = off && std::strchr(off, 'q');
324 const bool offE = off && std::strchr(off, 'e');
325 s.setPhaseChangePlaneDirichlet(!offP);
326 s.setPhaseChangeQuadraticFit(!offQ);
327 if (!offE)
328 s.setPhaseChangeEnergy(rho_v * cpl, rho_l * cpl);
329 };
330 auto march = [&](IbmSolver& s) {
331 double tc = t0;
332 int ns = 0;
333 while (tc < te) {
334 const double Xd = b * std::sqrt(alpha_l / tc) * NX;
335 double dt = std::fmin(cfl / std::fmax(Xd * (1.0 - rr), 1e-30), Fo / al);
336 dt = std::fmin(dt, te - tc);
337 s.setDt(dt);
338 s.setScalarBc("T", 1, 2, farT(tc + dt));
339 s.step();
340 tc += dt;
341 ++ns;
342 }
343 return ns;
344 };
345 std::vector<double> refC, refT;
346 int ns = 0;
347 {
348 IbmSolver ref(NX, NY, NZ);
349 configure(ref, NX, NY, NZ, 0, 0, 0);
350 ns = march(ref);
351 refC = ref.getVof();
352 refT = ref.getField("T");
353 }
354 IbmSolver sd(lnx, lny, lnz);
355 sd.initMpi(dec, MPI_COMM_WORLD);
356 configure(sd, lnx, lny, lnz, ox, oy, oz);
357 march(sd);
358 const double dC = blockDiff(sd.getVof(), refC, ox, oy, oz, lnx, lny, lnz);
359 const double dT_ = blockDiff(sd.getField("T"), refT, ox, oy, oz, lnx, lny, lnz);
360 double refSum = 0.0;
361 for (double v : refC)
362 refSum += v;
363 double locSum = 0.0;
364 for (double v : sd.getVof())
365 locSum += v;
366 double gSum = 0.0;
367 MPI_Allreduce(&locSum, &gSum, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
368 const double layerRef = NX - refSum / (NY * NZ), layerDist = NX - gSum / (NY * NZ);
369 const double xExact = 2 * b * std::sqrt(alpha_l * te) * NX;
370 if (rank == 0)
371 std::printf(
372 " P2 sucking %d coupled steps (plane-anchored Dirichlet + quadratic fit + consistent "
373 "rho c_p T): layer_dist = %.9f vs layer_ref = %.9f (rel %.3e); pointwise max "
374 "|C_dist - C_ref| = %.3e, |T_dist - T_ref| = %.3e; exact %.5f (%+.4f %%)\n",
375 ns, layerDist, layerRef, std::fabs(layerDist - layerRef) / layerRef, dC, dT_, xExact,
376 100.0 * (layerRef - xExact) / xExact);
377 // THE GATE IS THE PHYSICAL, DECOMPOSITION-INDEPENDENT QUANTITY, and the reason is measured.
378 // This COUPLED scene is ROUND-OFF SENSITIVE, and it is NOT an MPI defect: at np = 1, where
379 // the distributed and reference solvers differ only in the arithmetic path `initMpi` selects,
380 // the pointwise colour difference is 0.0 at 1 step, 3.3e-16 at 3, 2.4e-4 at 12 and then
381 // PLATEAUS (it does not keep growing). It appears exactly when the interface crosses a cell
382 // boundary and the two runs cross it at round-off-different moments. Bisected with
383 // PECLET_P23_OFF at 12 steps: all three WO-P23 options off gives 4.1e-14, and ANY ONE of them
384 // on gives 1.2e-4 … 2.8e-4 — the sharper interfacial treatment is what turns a crossing into
385 // a discrete event (the classification threshold `pcIsInterfacial` switches a cell's whole
386 // energy row). The INTEGRAL of the colour — the interface position, which is the physics —
387 // moves by only 5e-5 … 1.4e-4 relative, and that is what is gated. P0a and P1 above are
388 // BITWISE, so the exchange, the gather-based deposit and the redistribution are all exact;
389 // this is a property of the coupled step, not of the distribution.
390 CHECK(std::fabs(layerDist - layerRef) / layerRef < 5e-4); // measured 8.5e-5 / 5.2e-5 /
391 // 1.4e-4 at np 1 / 2 / 4
392 CHECK(dC < 1e-2);
393 CHECK(dT_ < 1e-2);
394 }
395 }
396 int fail = failures;
397 MPI_Allreduce(MPI_IN_PLACE, &fail, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD);
398 Kokkos::finalize();
399 MPI_Finalize();
400 return fail ? 1 : 0;
401}
void setMassFluxUniform(double v)
Prescribe a UNIFORM mass flux (P0), in mass per area per time.
std::vector< double > getField(const std::string &name)
void setAdvection(bool on)
std::vector< double > getVof()
void setVof(const std::vector< double > &c)
void applyPhaseChange(double dtPhysArg)
Kinematic entry point (the P0a/P1 driver): build mdot/A_G/n from the current colour and temperature,...
void enablePhaseChange(double rhoG, double rhoL, double hlv)
Turn on phase change.
void setMu(double m)
Dynamic viscosity, in the caller's units.
void setRho(double r)
Fluid density, in the caller's units.
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 int NZ
static void configure(IbmSolver &s)
static double X0()
static std::vector< double > colour(int m)
static std::vector< double > blockOf(Fn f, int ox, int oy, int oz, int lnx, int lny, int lnz)
static double blockDiff(const std::vector< double > &local, const std::vector< double > &global, int ox, int oy, int oz, int lnx, int lny, int lnz)
static std::size_t gidx(int x, int y, int z)
void applyP3fEnv(S &s)
static double stefanLambda(double St)
static std::vector< double > sliceOf(const std::vector< double > &g, int ox, int oy, int oz, int lnx, int lny, int lnz)
#define CHECK(cond)
void applyAreaModeEnv(S &s)