flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_vof_cutcell.cpp
Go to the documentation of this file.
1// VoF rung V5a (WO-Q) — the colour field transported THROUGH an immersed solid: openness-weighted
2// Weymouth-Yue fluxes, the update in fluid-volume units, and the neutral (90 deg) solid-band fill.
3//
4// Gates, in the order they are run:
5//
6// G3 THE TWO-BLOCK BRIDGE WITH GEOMETRY. The openness the solver hands the advector must be the
7// SAME face, i.e. shifted by one cell along the component's own axis exactly as the face
8// velocity is (`vof::copyFaceVelocity`, WO-J's 35 %-of-the-volume trap, here for the
9// openness). Two halves: (a) the geometry fields themselves, against an INDEPENDENT build of
10// `buildOpenness` / `buildCellFraction` on a g=3 block with the shift written out by hand;
11// (b) the transported colour, against a standalone `WyAdvector` driven by the same velocity
12// and the same geometry — bitwise.
13//
14// G2 CONSERVATION THROUGH A PACKING (kinematic). A periodic sphere array, the single-phase
15// solver run to a Stokes steady state, then that PROJECTED velocity frozen and a liquid slab
16// advected by `advect_vof` alone. The conserved functional is `sum eps_eff C` (see
17// `vof/cutcell.hpp` rule 1); the floor is the projection's own `max|div(open u)|` and both are
18// reported. Also: zero colour in solid cells EXACTLY, boundedness in the uncut fluid cells,
19// and the clipped volume.
20//
21// G5 THE 90-DEGREE NEUTRAL FILL. A liquid cap on a flat SDF wall at a HALF-INTEGER z (so the wall
22// cells are genuinely cut) with surface tension: a hemisphere is the equilibrium of the
23// neutral fill, and the apparent contact angle measured from the cap volume, its height and
24// its contact radius must come back at 90 deg.
25#include <cmath>
26#include <cstdio>
27#include <cstdlib>
28#include <Kokkos_Core.hpp>
29#include <vector>
30
31#include "flow_ibm.hpp"
32#include "vof/plic.hpp"
33#include "vof_advect_scenes.hpp"
34
35namespace {
43
44int failures = 0;
45#define CHECK(cond) \
46 do { \
47 if (!(cond)) { \
48 std::fprintf(stderr, "CHECK failed: %s\n at %s:%d\n", #cond, __FILE__, __LINE__); \
49 ++failures; \
50 } \
51 } while (0)
52
53std::size_t idx(int x, int y, int z, int nx, int ny) {
54 return (std::size_t)x + (std::size_t)y * nx + (std::size_t)z * (std::size_t)nx * ny;
55}
56
57// ---------------------------------------------------------------- the scene
58//
59// A periodic array of spheres, given ANALYTICALLY so the same function can be sampled on the
60// solver's inner region and on a g=3 test block (gate G3 needs both). Cell centres are at
61// (i+1/2)h with h = 1 (flow works in cell units on the colour block).
62struct SphereArray {
63 int n = 0;
64 double cx[8], cy[8], cz[8], r[8];
65 int N = 0; // periodic period, in cells
66 double sdfAt(double x, double y, double z) const {
67 double best = 1e30;
68 for (int s = 0; s < n; ++s)
69 for (int px = -1; px <= 1; ++px)
70 for (int py = -1; py <= 1; ++py)
71 for (int pz = -1; pz <= 1; ++pz) {
72 const double dx = x - (cx[s] + px * N), dy = y - (cy[s] + py * N),
73 dz = z - (cz[s] + pz * N);
74 const double d = std::sqrt(dx * dx + dy * dy + dz * dz) - r[s];
75 best = d < best ? d : best;
76 }
77 return best; // > 0 in fluid (flow's convention)
78 }
79};
80
81SphereArray makeArray(int N) {
82 SphereArray a;
83 a.N = N;
84 a.n = 4;
85 const double f = N / 32.0;
86 const double px[4] = {6.0, 20.0, 11.0, 26.0}, py[4] = {7.0, 9.0, 24.0, 22.0},
87 pz[4] = {8.0, 23.0, 19.0, 6.0};
88 for (int s = 0; s < 4; ++s) {
89 a.cx[s] = px[s] * f;
90 a.cy[s] = py[s] * f;
91 a.cz[s] = pz[s] * f;
92 a.r[s] = 6.0 * f;
93 }
94 return a;
95}
96
97std::vector<double> arraySdfInner(const SphereArray& a, int N) {
98 std::vector<double> sd((std::size_t)N * N * N);
99 for (int z = 0; z < N; ++z)
100 for (int y = 0; y < N; ++y)
101 for (int x = 0; x < N; ++x)
102 sd[idx(x, y, z, N, N)] = a.sdfAt(x + 0.5, y + 0.5, z + 0.5);
103 return sd;
104}
105
106// The same SDF on an extended block of ghost width g (gate G3's independent geometry build).
107void arraySdfBlock(const SphereArray& a, CCField dst, I3 e, int g) {
108 auto h = Kokkos::create_mirror_view(dst);
109 for (int z = 0; z < e.z; ++z)
110 for (int y = 0; y < e.y; ++y)
111 for (int x = 0; x < e.x; ++x)
112 h(L3(x, y, z, e)) = a.sdfAt(x - g + 0.5, y - g + 0.5, z - g + 0.5);
113 Kokkos::deep_copy(dst, h);
114}
115
116std::vector<double> hostOf(SField v) {
117 auto h = Kokkos::create_mirror_view(v);
118 Kokkos::deep_copy(h, v);
119 std::vector<double> out(v.extent(0));
120 for (std::size_t i = 0; i < out.size(); ++i)
121 out[i] = h(i);
122 return out;
123}
124
125// Run the single-phase solver to a Stokes steady state on the packing and return it.
126void stokesSteady(peclet::flow::IbmSolver& s, int steps) {
127 for (int i = 0; i < steps; ++i)
128 s.step();
129}
130
131// ================================================================ G3: the bridge with geometry
132void bridgeWithGeometry() {
133 const int N = 24;
134 const SphereArray arr = makeArray(N);
136 s.setAdvection(false);
137 s.setRho(1.0);
138 s.setMu(0.2);
139 s.setDt(1.0);
140 s.setBodyForce(2e-3, 1e-3, 0.0);
141 s.setSolid(arraySdfInner(arr, N), true);
142 stokesSteady(s, 40);
143 s.enableVof();
144 CHECK(s.vofHasGeometry());
145
146 // --- (a) the geometry fields, against an independent build on a g=3 block --------------------
148 const I3 e3{N + 2 * g, N + 2 * g, N + 2 * g};
149 const long len = (long)e3.x * e3.y * e3.z;
150 CCField sdf3("t::sdf3", len), ox3("t::ox3", len), oy3("t::oy3", len), oz3("t::oz3", len),
151 cs3("t::cs3", len);
152 arraySdfBlock(arr, sdf3, e3, g);
153 peclet::flow::buildOpenness(ox3, oy3, oz3, CCConst(sdf3), C3{e3.x, e3.y, e3.z}, 1.0, 1.0, 1.0,
154 s.apertureOrder());
155 peclet::flow::buildCellFraction(cs3, CCConst(sdf3), C3{e3.x, e3.y, e3.z}, 1);
156 const std::vector<double> refO[3] = {hostOf(ox3), hostOf(oy3), hostOf(oz3)};
157 const auto refE = hostOf(cs3);
158 const long sd3[3] = {1, (long)e3.x, (long)e3.x * e3.y};
159
160 const auto& adv = s.vofAdvector();
161 double dOpen = 0.0, dEps = 0.0;
162 for (int d = 0; d < 3; ++d) {
163 const auto got = hostOf(adv.faceOpenness(d));
164 for (int z = g; z < g + N; ++z)
165 for (int y = g; y < g + N; ++y)
166 for (int x = g; x < g + N; ++x) {
167 const long i = L3(x, y, z, e3);
168 // buildOpenness gives the LOW (-d) face of each cell; the advector wants the HIGH (+d)
169 // face of the same cell, i.e. the low face of the cell one step up in d. That written-out
170 // shift is exactly what `vof::copyFaceVelocity` does inside the solver.
171 dOpen = std::fmax(dOpen, std::fabs(got[i] - refO[d][i + sd3[d]]));
172 }
173 }
174 {
175 const auto got = hostOf(adv.epsFraction());
176 for (int z = g; z < g + N; ++z)
177 for (int y = g; y < g + N; ++y)
178 for (int x = g; x < g + N; ++x) {
179 const long i = L3(x, y, z, e3);
180 dEps = std::fmax(dEps, std::fabs(got[i] - refE[i]));
181 }
182 }
183 std::printf(
184 "G3a geometry embed: max|openness - independent| %.3e max|eps - independent| %.3e\n", dOpen,
185 dEps);
186 CHECK(dOpen == 0.0);
187 CHECK(dEps == 0.0);
188
189 // --- (b) the transported colour, against a standalone advector -------------------------------
190 // Seed a slab and transport it BOTH ways with the same (frozen, projected) velocity.
191 std::vector<double> c0((std::size_t)N * N * N, 0.0);
192 for (int z = 0; z < N; ++z)
193 for (int y = 0; y < N; ++y)
194 for (int x = 0; x < N; ++x)
195 c0[idx(x, y, z, N, N)] = (z < N / 2) ? 1.0 : ((z == N / 2) ? 0.37 : 0.0);
196 s.setVof(c0);
197
199 ref.init(N, N, N, 1.0, g);
200 // WO-R2 item 4: configure the reference like the solver, wisp tolerance included, or this gate
201 // measures the difference of two mixed-cell predicates (2.594e-09) instead of the bridge.
203 ref.interfaceLocalCfl = true;
204 ref.enableGeometry();
205 // The reference's geometry: the INDEPENDENT fields above, embedded with the shift by hand.
206 {
207 auto ho = Kokkos::create_mirror_view(ref.faceOpenness(0));
208 for (int d = 0; d < 3; ++d) {
209 auto hd = Kokkos::create_mirror_view(ref.faceOpenness(d));
210 for (long i = 0; i < len; ++i)
211 hd(i) = (i + sd3[d] < len) ? refO[d][i + sd3[d]] : 0.0;
212 Kokkos::deep_copy(ref.faceOpenness(d), hd);
213 }
214 auto he = Kokkos::create_mirror_view(ref.epsFraction());
215 for (long i = 0; i < len; ++i)
216 he(i) = refE[i];
217 Kokkos::deep_copy(ref.epsFraction(), he);
218 (void)ho;
219 }
220 ref.exchange = [&ref, e3, g](SField f) {
221 vofscene::periodicFill(f, e3, g, true, true, true);
222 if (ref.hasGeometry() && f.data() == ref.colour().data()) {
223 ref.solidBandFill();
224 vofscene::periodicFill(f, e3, g, true, true, true);
225 }
226 };
227 vofscene::periodicFill(ref.faceOpenness(0), e3, g, true, true, true);
228 vofscene::periodicFill(ref.faceOpenness(1), e3, g, true, true, true);
229 vofscene::periodicFill(ref.faceOpenness(2), e3, g, true, true, true);
230 vofscene::periodicFill(ref.epsFraction(), e3, g, true, true, true);
231 ref.classifyGeometry();
232 vofscene::periodicFill(ref.kindDouble(), e3, g, true, true, true);
233 ref.finalizeGeometry();
234 { // the same colour, with solid cells zeroed exactly as the solver's canonical field is
235 auto hc = Kokkos::create_mirror_view(ref.colour());
236 auto hk = Kokkos::create_mirror_view(ref.kindDouble());
237 Kokkos::deep_copy(hk, ref.kindDouble());
238 for (long i = 0; i < len; ++i)
239 hc(i) = 0.0;
240 for (int z = 0; z < N; ++z)
241 for (int y = 0; y < N; ++y)
242 for (int x = 0; x < N; ++x) {
243 const long i = L3(x + g, y + g, z + g, e3);
244 hc(i) = hk(i) > 0.5 ? 0.0 : c0[idx(x, y, z, N, N)];
245 }
246 Kokkos::deep_copy(ref.colour(), hc);
247 }
248 ref.syncGhosts();
249 // The solver's own face velocity, re-indexed into the advector's high-face convention.
250 const auto uu = s.getVelocity(0), vv = s.getVelocity(1), ww = s.getVelocity(2);
251 {
252 auto hu = Kokkos::create_mirror_view(ref.faceU());
253 auto hv = Kokkos::create_mirror_view(ref.faceV());
254 auto hw = Kokkos::create_mirror_view(ref.faceW());
255 for (int z = 0; z < e3.z; ++z)
256 for (int y = 0; y < e3.y; ++y)
257 for (int x = 0; x < e3.x; ++x) {
258 const int gx = ((x - g) % N + N) % N, gy = ((y - g) % N + N) % N,
259 gz = ((z - g) % N + N) % N;
260 const long i = L3(x, y, z, e3);
261 hu(i) = uu[idx((gx + 1) % N, gy, gz, N, N)];
262 hv(i) = vv[idx(gx, (gy + 1) % N, gz, N, N)];
263 hw(i) = ww[idx(gx, gy, (gz + 1) % N, N, N)];
264 }
265 Kokkos::deep_copy(ref.faceU(), hu);
266 Kokkos::deep_copy(ref.faceV(), hv);
267 Kokkos::deep_copy(ref.faceW(), hw);
268 }
269 const double dth = 1.0; // h = 1
270 double cflRef = ref.maxCourantInterfaceAuto(dth);
271 const double dtA = cflRef > 0 ? 0.2 / cflRef : 1.0;
272 s.setDt(dtA);
273 const int steps = 20;
274 for (int t = 0; t < steps; ++t) {
275 ref.advect(dtA, t);
276 s.advectVofKinematic(dtA);
277 }
278 const auto cs = s.getVof();
279 const auto cr = hostOf(ref.colour());
280 const auto kr = hostOf(ref.kindDouble());
281 // The canonical "C" carries 0 in solid cells while the working block carries the neutral band
282 // fill, so the comparison is over FLUID cells (the fill itself is gated by G5).
283 double dmax = 0.0, fillMax = 0.0;
284 for (int z = 0; z < N; ++z)
285 for (int y = 0; y < N; ++y)
286 for (int x = 0; x < N; ++x) {
287 const long i = L3(x + g, y + g, z + g, e3);
288 if (kr[i] > 0.5) {
289 fillMax = std::fmax(fillMax, cr[i]);
290 continue;
291 }
292 dmax = std::fmax(dmax, std::fabs(cs[idx(x, y, z, N, N)] - cr[i]));
293 }
294 std::printf(
295 "G3b solver vs standalone through the packing (%d steps, cfl 0.2, dt %.4g): max|dC| over "
296 "fluid cells %.3e (standalone band fill reaches %.3f)\n",
297 steps, dtA, dmax, fillMax);
298 CHECK(dmax == 0.0);
299}
300
301// ================================================================ G2: conservation
302void conservationThroughPacking(int N, int nAdv) {
303 const SphereArray arr = makeArray(N);
305 s.setAdvection(false);
306 s.setRho(1.0);
307 s.setMu(0.2);
308 s.setDt(1.0);
309 s.setBodyForce(2e-3, 1e-3, 5e-4);
310 s.setSolid(arraySdfInner(arr, N), true);
311 stokesSteady(s, 60);
312 const double div = s.maxOpenDivergence();
313 const long pit = s.lastPressureIterations();
314 s.enableVof();
315 std::vector<double> c0((std::size_t)N * N * N, 0.0);
316 for (int z = 0; z < N; ++z)
317 for (int y = 0; y < N; ++y)
318 for (int x = 0; x < N; ++x)
319 c0[idx(x, y, z, N, N)] = (z < N / 2) ? 1.0 : 0.0;
320 s.setVof(c0);
321
322 const auto d0 = s.vofDiagnostics();
323 const double cfl = s.vofMaxCourant(); // uses the current dt (= 1)
324 const double dtA = cfl > 0 ? 0.2 / cfl : 1.0;
325 double clip = 0.0, mnF = 1e30, mxF = -1e30, solidSum = 0.0;
326 for (int t = 0; t < nAdv; ++t) {
327 s.advectVofKinematic(dtA);
328 const auto d = s.vofDiagnostics();
329 clip += d.clippedVolume;
330 mnF = std::fmin(mnF, d.minCFluid);
331 mxF = std::fmax(mxF, d.maxCFluid);
332 solidSum = std::fmax(solidSum, std::fabs(d.solidSumC));
333 }
334 const auto d1 = s.vofDiagnostics();
335 const double drift = std::fabs(d1.volume - d0.volume) / d0.volume;
336 const double rawDrift = std::fabs(d1.rawVolume - d0.rawVolume) / d0.rawVolume;
337 std::printf(
338 "G2 packing %d^3, %d kinematic steps at cfl 0.2 (dt %.4g):\n"
339 " max|div(open u)| %.3e (pressure iters %ld)\n"
340 " sum eps_eff C: %.15e -> %.15e relative drift %.3e\n"
341 " sum eps C: %.15e -> %.15e relative drift %.3e\n"
342 " solid cells %ld, cut cells %ld, max|sum C over solid| %.3e\n"
343 " min/max C over UNCUT fluid cells %.3e / %.15e\n"
344 " clipped liquid volume, total over the run %.6e (%.3e of the liquid volume)\n",
345 N, nAdv, dtA, div, pit, d0.volume, d1.volume, drift, d0.rawVolume, d1.rawVolume, rawDrift,
346 d1.solidCells, d1.cutCells, solidSum, mnF, mxF, clip, clip / d0.volume);
347 CHECK(div <= 1e-10);
348 CHECK(drift <= 1e-11);
349 CHECK(solidSum == 0.0);
350 CHECK(mnF >= -1e-12);
351 CHECK(mxF <= 1.0 + 1e-12);
352 CHECK(d1.solidCells > 0);
353 CHECK(d1.cutCells > 0);
354}
355
356// ================================================================ G5: the 90-degree neutral fill
357//
358// A liquid cap on a flat SDF wall. The wall occupies z < zw with zw a HALF-INTEGER in cell units,
359// so the cells straddling it are genuinely cut (eps = 1/2) — a wall on a cell face would be a
360// staircase and would not exercise the cut-cell path at all.
361void neutralFillCap(int steps, bool zeroSolid = true) {
362 const int NX = 40, NY = 40, NZ = 28;
363 const double zw = 3.5; // the wall surface, in cell units (cell k spans [k, k+1])
364 const double R = 12.0; // D/dx = 24
365 const double sigma = 1.0;
366 std::vector<double> sd((std::size_t)NX * NY * NZ);
367 for (int z = 0; z < NZ; ++z)
368 for (int y = 0; y < NY; ++y)
369 for (int x = 0; x < NX; ++x)
370 sd[idx(x, y, z, NX, NY)] = (z + 0.5) - zw; // > 0 above the wall = fluid
371 // The initial hemisphere, by 4^3 midpoint subsampling (the same quadrature the fluid fraction
372 // uses, so the initial colour and the geometry agree cell by cell).
373 const double cx = NX * 0.5, cy = NY * 0.5;
374 std::vector<double> c0((std::size_t)NX * NY * NZ, 0.0);
375 for (int z = 0; z < NZ; ++z)
376 for (int y = 0; y < NY; ++y)
377 for (int x = 0; x < NX; ++x) {
378 int in = 0, tot = 0;
379 for (int a = 0; a < 4; ++a)
380 for (int b = 0; b < 4; ++b)
381 for (int c = 0; c < 4; ++c) {
382 const double px = x + (a + 0.5) / 4.0, py = y + (b + 0.5) / 4.0,
383 pz = z + (c + 0.5) / 4.0;
384 if (pz < zw)
385 continue; // solid: C is a fraction of the FLUID volume
386 ++tot;
387 const double dx = px - cx, dy = py - cy, dz = pz - zw;
388 if (dx * dx + dy * dy + dz * dz < R * R)
389 ++in;
390 }
391 c0[idx(x, y, z, NX, NY)] = tot ? (double)in / tot : 0.0;
392 }
393
395 s.setAdvection(false);
396 s.setRho(1.0);
397 s.setMu(0.05);
398 s.setSolid(sd, true);
399 s.enableVof();
400 s.setVofSolidColourZero(zeroSolid);
401 s.setVof(c0);
402 s.setSurfaceTension(sigma);
403 const double dtSig = s.capillaryDt();
404 s.setDt(0.5 * dtSig);
405 const double V00 = s.vofDiagnostics().volume;
406 long maxIt = 0;
407 double uTrace[4] = {0, 0, 0, 0};
408 for (int i = 0; i < steps; ++i) {
409 s.step();
410 maxIt = std::max(maxIt, s.lastPressureIterations());
411 if ((i + 1) % (steps / 4) == 0) {
412 const int slot = std::min(3, (i + 1) / (steps / 4) - 1);
413 double m = 0;
414 for (int c = 0; c < 3; ++c)
415 for (double v : s.getVelocity(c))
416 m = std::fmax(m, std::fabs(v));
417 uTrace[slot] = m;
418 }
419 }
420 // The curvature census in the WALL BAND (the two fluid planes above the wall) — the WO asks for
421 // it because the neutral fill is what those stencils read.
422 const auto cens = [&] {
423 s.computeVofCurvature();
424 return s.vofCurvatureStats();
425 }();
426 const auto kap = s.getVofCurvature();
427 const auto kbr = s.getVofCurvatureBranch();
428 double kmaxWall = 0.0;
429 long brWall[7] = {0, 0, 0, 0, 0, 0, 0};
430 for (int z = (int)std::ceil(zw); z < (int)std::ceil(zw) + 2; ++z)
431 for (int y = 0; y < NY; ++y)
432 for (int x = 0; x < NX; ++x) {
433 const std::size_t i = idx(x, y, z, NX, NY);
434 const int b = (int)kbr[i];
435 if (b >= 0 && b < 7)
436 ++brWall[b];
437 if (b != 0)
438 kmaxWall = std::fmax(kmaxWall, std::fabs(kap[i]));
439 }
440 const auto d = s.vofDiagnostics();
441 const auto cc = s.getVof();
442 // h: the liquid column height on the axis (sum of C over the fluid column at the centre).
443 double h = 0.0;
444 {
445 const int x0 = NX / 2, y0 = NY / 2;
446 for (int z = 0; z < NZ; ++z)
447 h += cc[idx(x0, y0, z, NX, NY)] * ((z + 1 <= zw) ? 0.0 : std::fmin(1.0, z + 1 - zw));
448 }
449 // a: from the liquid AREA in the first FULL fluid plane above the wall (more robust than a
450 // contour, and the C = 1/2 contour of a resolved cap is its area radius to O(h^2)).
451 const int z0 = (int)std::ceil(zw);
452 double area = 0.0;
453 for (int y = 0; y < NY; ++y)
454 for (int x = 0; x < NX; ++x)
455 area += cc[idx(x, y, z0, NX, NY)];
456 const double a = std::sqrt(area / M_PI);
457 const double theta = 2.0 * std::atan2(h, a) * 180.0 / M_PI;
458 const double Rc = (a * a + h * h) / (2.0 * h);
459 const double V = d.volume;
460 const double V0 = 2.0 * M_PI * R * R * R / 3.0;
461 // Young-Laplace: the pressure jump across the cap.
462 const auto pf = s.getField("p");
463 double pIn = 0.0, pOut = 0.0;
464 int nIn = 0, nOut = 0;
465 for (int z = 0; z < NZ; ++z)
466 for (int y = 0; y < NY; ++y)
467 for (int x = 0; x < NX; ++x) {
468 const double dx = x + 0.5 - cx, dy = y + 0.5 - cy, dz = z + 0.5 - zw;
469 const double r = std::sqrt(dx * dx + dy * dy + dz * dz);
470 if (dz < 0.5)
471 continue;
472 if (r < 0.55 * R) {
473 pIn += pf[idx(x, y, z, NX, NY)];
474 ++nIn;
475 } else if (r > 1.5 * R && r < 1.8 * R) {
476 pOut += pf[idx(x, y, z, NX, NY)];
477 ++nOut;
478 }
479 }
480 const double dP = (nIn ? pIn / nIn : 0.0) - (nOut ? pOut / nOut : 0.0);
481 // max|u| over the OPEN fluid only (z >= ceil(zw)+1): the velocity DOFs that live on faces inside
482 // or on the wall are IBM-constrained, not flow, and reporting them as a spurious current would
483 // measure the immersed boundary rather than the surface-tension balance.
484 double umax = 0.0, umaxAll = 0.0;
485 for (int c = 0; c < 3; ++c) {
486 const auto v = s.getVelocity(c);
487 for (int z = 0; z < NZ; ++z)
488 for (int y = 0; y < NY; ++y)
489 for (int x = 0; x < NX; ++x) {
490 const double a = std::fabs(v[idx(x, y, z, NX, NY)]);
491 umaxAll = std::fmax(umaxAll, a);
492 if (z >= (int)std::ceil(zw) + 1)
493 umax = std::fmax(umax, a);
494 }
495 }
496 const double Ca = 0.05 * umax / sigma; // mu of this scene
497 std::printf(
498 "G5 cap on a cut wall (D/dx %.0f, %d steps at 0.5 dt_sigma = %.4g, solid colour %s), "
499 "pressure iters max %ld:\n"
500 " volume %.6f -> %.6f (drift %.3e); analytic hemisphere %.6f\n"
501 " h %.4f a %.4f -> apparent theta %.3f deg (target 90)\n"
502 " cap sphere radius %.4f (target %.1f); Young-Laplace dP %.6f vs 2 sigma/R %.6f "
503 "(rel %.3e)\n"
504 " max|u| (open fluid) %.3e -> spurious Ca %.3e ; max|u| incl. the wall band %.3e ;\n"
505 " trace of max|u| (all cells) over the run %.2e %.2e %.2e %.2e\n"
506 " wall band: max|kappa| %.4f, branches 0/1/2/3/4/5/6 = %ld/%ld/%ld/%ld/%ld/%ld/%ld "
507 "(global no_estimate %ld)\n"
508 " clipped volume last step %.3e ; clamped faces %ld ; solid sum C %.3e\n",
509 2 * R, steps, 0.5 * dtSig, zeroSolid ? "ZERO" : "band fill", maxIt, V00, V,
510 std::fabs(V - V00) / V00, V0, h, a, theta, Rc, R, dP, 2.0 * sigma / Rc,
511 std::fabs(dP - 2.0 * sigma / Rc) / (2.0 * sigma / Rc), umax, Ca, umaxAll, uTrace[0],
512 uTrace[1], uTrace[2], uTrace[3], kmaxWall, brWall[0], brWall[1], brWall[2], brWall[3],
513 brWall[4], brWall[5], brWall[6], cens.noEstimate, d.clippedVolume, d.clampedFaces,
514 d.solidSumC);
515 CHECK(std::fabs(theta - 90.0) <= 3.0);
516 CHECK(std::fabs(dP - 2.0 * sigma / Rc) / (2.0 * sigma / Rc) <= 0.01);
517 if (zeroSolid)
518 CHECK(d.solidSumC == 0.0);
519 CHECK(std::fabs(V - V00) / V00 <= 1e-9);
520}
521
522// ================================================================ G8: momentum consistency in cut
523// cells (WO-Q item 8)
524//
525// G8a THE CONSISTENCY IDENTITY. `enable_vof_momentum` + a packing + a UNIFORM velocity: the
526// advected velocity must come back as that same uniform value. Every term of the deviation form
527// `u_new = u_old + dev/(eps^e rho_new)` is a DIFFERENCE OF VELOCITIES, so `dev` is exactly zero
528// for a uniform field whatever `eps^e` and the face openness are — the identity is bitwise in
529// CUT cells too, not only away from them, and there is no tolerance to choose.
530// G8b COUPLED DRAINING through the packing at ratio 10 with gravity and momentum consistency: the
531// colour drift per step, boundedness, `max|u|`, and the pressure iteration count against its
532// cap (rule 3b: a capped run is INVALID).
533void momentumCutCells() {
534 const int N = 24;
535 const SphereArray arr = makeArray(N);
536 const auto sdf = arraySdfInner(arr, N);
537 const double U[3] = {1.0, 0.6, -0.4};
538 // --- G8a ------------------------------------------------------------------------------------
539 for (double R : {10.0, 1e2, 1e3}) {
541 s.setRho(R);
542 s.setMu(0.0);
543 // The CUT-CELL Courant number (`o_f |a_f| / max(eps, 0.1)`) is up to 6x the plain one in this
544 // packing — at dt = 0.2, |U| = 1 the plain CFL is 0.2 but the cut one reads 1.24 and `advect()`
545 // aborts, which is the limiter doing exactly its job. dt = 0.04 puts the cut number at 0.25.
546 s.setDt(0.04);
547 s.setSolid(sdf, true);
548 s.setPressureChebyshev(true, 300, 1e-13);
549 s.enableVof();
550 std::vector<double> c0((std::size_t)N * N * N);
551 for (int z = 0; z < N; ++z)
552 for (int y = 0; y < N; ++y)
553 for (int x = 0; x < N; ++x)
554 c0[idx(x, y, z, N, N)] = peclet::flow::vof::sphereCellFraction(0.5 * N, 0.5 * N, 0.5 * N,
555 0.28 * N, x, y, z, 1.0, 4);
556 s.setVof(c0);
557 s.setPropertyModel("rho", peclet::flow::ClosureKind::LinearMix, "C", "", {1.0, R - 1.0});
558 s.enableVofMomentum(1.0, R);
559 const std::size_t nc = (std::size_t)N * N * N;
560 s.uploadVelocity(std::vector<double>(nc, U[0]), std::vector<double>(nc, U[1]),
561 std::vector<double>(nc, U[2]));
562 s.advectVofMomentum();
563 double dev = 0.0;
564 for (int c = 0; c < 3; ++c)
565 for (double v : s.getVofAdvectedVelocity(c))
566 dev = std::fmax(dev, std::fabs(v - U[c]));
567 const auto md = s.vofMomentumDiagnostics();
568 std::printf(
569 "G8a packing + uniform U, ratio %-6g: max|u_adv - U| = %.17g%s (rho^e floor hit %ld, "
570 "flux clamps %ld)\n",
571 R, dev, dev == 0.0 ? " BITWISE" : "", md.floored, md.clamped);
572 CHECK(dev == 0.0);
573 CHECK(md.floored == 0);
574 }
575 // --- G8a for every momentum scheme (doc/vof_momentum_second_order.md G-M2-1) ----------------
576 for (const char* sch : {"upwind", "muscl_mixed", "muscl_mixed_capped", "muscl_phase"})
577 for (double R : {1.0, 1e2, 1e3, 1e4}) {
579 s.setRho(R);
580 s.setMu(0.0);
581 s.setDt(0.04);
582 s.setSolid(sdf, true);
583 s.setPressureChebyshev(true, 300, 1e-13);
584 s.enableVof();
585 std::vector<double> c0((std::size_t)N * N * N);
586 for (int z = 0; z < N; ++z)
587 for (int y = 0; y < N; ++y)
588 for (int x = 0; x < N; ++x)
589 c0[idx(x, y, z, N, N)] = peclet::flow::vof::sphereCellFraction(
590 0.5 * N, 0.5 * N, 0.5 * N, 0.28 * N, x, y, z, 1.0, 4);
591 s.setVof(c0);
592 s.setPropertyModel("rho", peclet::flow::ClosureKind::LinearMix, "C", "", {1.0, R - 1.0});
593 s.enableVofMomentum(1.0, R);
594 s.setVofMomentumScheme(sch);
595 const std::size_t nc = (std::size_t)N * N * N;
596 s.uploadVelocity(std::vector<double>(nc, U[0]), std::vector<double>(nc, U[1]),
597 std::vector<double>(nc, U[2]));
598 double dev = 0.0;
599 for (int k = 0; k < 3; ++k) { // three advections: the colour moves, the field stays uniform
600 s.advectVofMomentum();
601 for (int c = 0; c < 3; ++c)
602 for (double v : s.getVofAdvectedVelocity(c))
603 dev = std::fmax(dev, std::fabs(v - U[c]));
604 }
605 const auto md = s.vofMomentumDiagnostics();
606 std::printf(
607 "G8a %-18s packing + uniform U, ratio %-6g, 3 advections: max|u_adv - U| = "
608 "%.17g%s (floored %ld, clamped %ld, capped %ld)\n",
609 sch, R, dev, dev == 0.0 ? " BITWISE" : "", md.floored, md.clamped, md.capped);
610 CHECK(dev == 0.0);
611 CHECK(md.floored == 0);
612 }
613 // --- G8b ------------------------------------------------------------------------------------
614 // The body force is the ZERO-MEAN buoyancy `f_z = -g (rho - <rho>)`, not `-g rho`: in a fully
615 // periodic box the projection removes only GRADIENTS, so a force with a non-zero mean accelerates
616 // the whole fluid without bound and the case has no steady state to measure a drift against.
617 //
618 // MEASURED with the non-zero-mean force (`PECLET_VOF_CUTCELL_NONZERO_FORCE=1`), and RECORDED as
619 // an open question rather than explained: with momentum consistency ON the run is clean and
620 // conservative to 1e-12 for 155 steps — `C^e` inside [0,1] to the last bit, `rho^e` never
621 // floored, 9-10 pressure iterations — and then at step ~160, with `max|u|` around 0.19, `C^e`
622 // goes to +inf inside the ADVECTION and the velocity follows. With momentum consistency OFF the
623 // identical case runs the full 200 steps (drift -2.3e-13, max|u| 0.236). So it is the
624 // momentum-consistent cut-cell path, not the colour transport and not the unbounded acceleration
625 // on its own; the trace shows no bounded-quantity precursor at all, which is why it is reported
626 // instead of patched.
627 for (int momentumOn = 1; momentumOn >= 0; --momentumOn) {
628 const double R = 10.0, grav = 2e-3;
629 const double liq = 0.25, rhoMean = liq * R + (1.0 - liq);
631 s.setAdvection(false);
632 s.setRho(1.0);
633 s.setMu(0.05);
634 s.setDt(0.5);
635 s.setSolid(sdf, true);
636 s.enableVof();
637 std::vector<double> c0((std::size_t)N * N * N, 0.0);
638 for (int z = 0; z < N; ++z)
639 for (int y = 0; y < N; ++y)
640 for (int x = 0; x < N; ++x)
641 c0[idx(x, y, z, N, N)] = (z >= 3 * N / 4) ? 1.0 : 0.0; // a liquid layer on top
642 s.setVof(c0);
643 s.setPropertyModel("rho", peclet::flow::ClosureKind::LinearMix, "C", "", {1.0, R - 1.0});
644 s.setPropertyModel("mu", peclet::flow::ClosureKind::LinearMix, "C", "", {0.05, 0.45});
645 const bool nonZeroMean = std::getenv("PECLET_VOF_CUTCELL_NONZERO_FORCE") != nullptr;
646 s.setPropertyModel("force_z", peclet::flow::ClosureKind::LinearMix, "rho", "",
647 {nonZeroMean ? 0.0 : grav * rhoMean, -grav});
648 if (momentumOn)
649 s.enableVofMomentum(1.0, R);
650 const double v0 = s.vofDiagnostics().volume;
651 long maxIt = 0;
652 const int steps = 200;
653 double umax = 0.0, clip = 0.0, mn = 1e30, mx = -1e30;
654 for (int i = 0; i < steps; ++i) {
655 s.step();
656 maxIt = std::max(maxIt, s.lastPressureIterations());
657 const auto d = s.vofDiagnostics();
658 clip += d.clippedVolume;
659 mn = std::fmin(mn, d.minCFluid);
660 mx = std::fmax(mx, d.maxCFluid);
661 if (std::getenv("PECLET_VOF_CUTCELL_TRACE") && (i % 5) == 0) {
662 double um = 0;
663 for (int c = 0; c < 3; ++c)
664 for (double v : s.getVelocity(c))
665 um = um > std::fabs(v) ? um : std::fabs(v);
666 double mnCc = 0, mxCc = 0, mnRho = 0;
667 long floored = 0;
668 if (momentumOn) {
669 const auto md = s.vofMomentumDiagnostics();
670 for (int c = 0; c < 3; ++c) {
671 mnCc = std::fmin(mnCc, md.minCc[c]);
672 mxCc = std::fmax(mxCc, md.maxCc[c]);
673 }
674 mnRho = md.minRhoC;
675 floored = md.floored;
676 }
677 std::printf(
678 " trace %3d vol %.12e minC %.3e maxC %.6f max|u| %.4e cfl %.4f iters %ld "
679 "C^e [%.3e, %.6f] minRho^e %.4e floored %ld\n",
680 i, d.volume, d.minCFluid, d.maxCFluid, um, s.vofLastCourant(),
681 s.lastPressureIterations(), mnCc, mxCc, mnRho, floored);
682 }
683 }
684 for (int c = 0; c < 3; ++c)
685 for (double v : s.getVelocity(c))
686 umax = std::fmax(umax, std::fabs(v));
687 const auto d1 = s.vofDiagnostics();
688 const double drift = (d1.volume - v0) / v0;
689 std::printf(
690 "G8b packing draining, ratio %g, %d coupled steps, momentum consistency %s:\n"
691 " colour drift %.3e (%.3e per step), max|div(open u)| %.3e, pressure iters max %ld\n"
692 " min/max C over uncut fluid %.3e / %.15f, clipped volume total %.3e, max|u| %.3e, "
693 "solid sum C %.3e\n",
694 R, steps, momentumOn ? "ON" : "off", drift, drift / steps, s.maxOpenDivergence(), maxIt, mn,
695 mx, clip, umax, d1.solidSumC);
696 CHECK(!std::isnan(umax) && !std::isnan(drift));
697 CHECK(std::fabs(drift) / steps <= 1e-10);
698 CHECK(d1.solidSumC == 0.0);
699 CHECK(maxIt < 200);
700 }
701}
702
703} // namespace
704
705int main(int argc, char** argv) {
706 std::setvbuf(stdout, nullptr, _IONBF, 0);
707 Kokkos::initialize(argc, argv);
708 {
709 bool quick = true;
710 if (const char* e = std::getenv("PECLET_VOF_CUTCELL_LONG"))
711 quick = (e[0] == '0');
712 bridgeWithGeometry();
713 conservationThroughPacking(quick ? 24 : 48, quick ? 200 : 500);
714 if (!std::getenv("PECLET_VOF_CUTCELL_SKIP_G5")) {
715 neutralFillCap(quick ? 120 : 200);
716 // The measured ablation on what the CANONICAL colour carries inside the solid (the working
717 // block always carries the neutral fill): 0 is the WO-Q contract, but the CSF and the
718 // property closures read the canonical field, so a wall face then sees a full colour jump.
719 if (std::getenv("PECLET_VOF_CUTCELL_FILL_ABLATION"))
720 neutralFillCap(quick ? 120 : 200, false);
721 }
722 momentumCutCells();
723 }
724 Kokkos::finalize();
725 if (failures)
726 std::fprintf(stderr, "%d check(s) FAILED\n", failures);
727 else
728 std::printf("all vof cut-cell checks passed\n");
729 return failures ? 1 : 0;
730}
Weymouth-Yue split advection of a colour field on an extended (inner + ghost) block.
static constexpr double defaultVofWispEps()
Weymouth-Yue split advection of a colour field on an extended (inner + ghost) block.
flow — host-facing Kokkos IBM Navier-Stokes solver (drop-in flow-style API).
y
Definition cmp.py:24
Slot slot(const Kokkos::View< double *, CCMem > &ks, int k)
Kokkos::View< double *, SMem > SField
long L3(int x, int y, int z, I3 e)
Kokkos::View< const double *, CCMem > CCConst
void buildCellFraction(CCField cs, CCConst sdf, C3 e, int g)
Kokkos::View< double *, CCMem > CCField
void buildOpenness(CCField ox, CCField oy, CCField oz, CCConst sdf, C3 ext, double dx, double dy, double dz, int order=1)
Solver< Staggered > IbmSolver
int m
Definition post.py:35
um
Definition post.py:21
t
Definition post.py:21
void periodicFill(SField f, I3 e, int g, bool px, bool py, bool pz)
Periodic wrap of a single-block extended field (serial reference path; the MPI path uses core's GridH...
flow — thin shim: the container-free VoF PLIC geometric toolbox (rung V0) now live in core.
static constexpr int NY
static constexpr int NX
static constexpr int NZ
static constexpr int N
Kokkos::View< const double *, CCMem > CCConst
Kokkos::View< double *, CCMem > CCField
static double sdfAt(int gx, int gy, int gz, int nx, int ny, int nz, double dref)
static constexpr double U
static constexpr double R
Kokkos::View< double *, SMem > SField
long L3(int x, int y, int z, I3 e)
#define CHECK(cond)