flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_vof_blocks_overlap.cpp
Go to the documentation of this file.
1// flow — the overlap-robustness rules of the block VoF container (doc/vof_overlap_design.md §5,
2// §11-§13), gated one mechanism at a time (review doc/vof_overlap_review.md findings 1-3).
3//
4// K the admissibility clip is LIVE ON THE BLOCK PATH: a detached cell of marker B (C = 1e-6) two
5// cells outside B's resolved surface gets |kappa| = 4.4 from the block cascade without the
6// clip and <= 1 with it (Solver, enable_vof_blocks_from_colors + enable_vof_block_csf); the
7// face force on the speck's own faces moves by at most sigma |dC| / Delta^2.
8// P every tunable of the block cascade prototype (VofBlockSet::curvProto) and the advector's
9// wispEps / metric reach every block: at seeding, after a re-centring re-allocates the block,
10// and after setWispEps / setKappaMax on live blocks. (Finding 1: pureEps once did not.)
11// W §12.3: a marker straddling the periodic x seam whose box must grow to the whole axis is
12// re-centred WITH the wrap -- fullAxis > 0, nothing discarded, volume exact, and (wispEps 0)
13// the union equal to a global-field WyAdvector to round-off.
14// R §13: 1000 kinematic steps of a translating marker at block wispEps 1e-8 conserve the volume
15// to 1e-14 relative with nothing discarded (the residue is returned), where the pre-§13
16// container leaked ~1e-10.
17#include <array>
18#include <cmath>
19#include <cstdio>
20#include <Kokkos_Core.hpp>
21#include <memory>
22#include <vector>
23
24#include "flow_ibm.hpp"
27#include "vof_advect_scenes.hpp"
28
37
38namespace {
39
40int failures = 0;
41#define CHECK(cond) \
42 do { \
43 if (!(cond)) { \
44 std::fprintf(stderr, "CHECK failed: %s\n at %s:%d\n", #cond, __FILE__, __LINE__); \
45 ++failures; \
46 } \
47 } while (0)
48
49constexpr int G = 3;
50
51double sumInner(SField f, I3 e, I3 n) {
52 auto h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), f);
53 double s = 0.0;
54 for (int z = 0; z < n.z; ++z)
55 for (int y = 0; y < n.y; ++y)
56 for (int x = 0; x < n.x; ++x)
57 s += h(L3(x + G, y + G, z + G, e));
58 return s;
59}
60
62void serialExchange(VofBlockSet& set, WyAdvector& pa, I3 gs) {
63 auto ex = std::make_shared<VofBlockExchange>();
64 std::vector<VofBox> rb(1);
65 rb[0].lo[0] = rb[0].lo[1] = rb[0].lo[2] = 0;
66 rb[0].hi[0] = gs.x;
67 rb[0].hi[1] = gs.y;
68 rb[0].hi[2] = gs.z;
69 ex->init(gs, set.periodic(), rb, 0);
71 pp.e = pa.extent();
72 pp.n = pa.inner();
73 pp.o = I3{0, 0, 0};
74 pp.g = G;
75 ex->setPatch(pp, pa.faceU(), pa.faceV(), pa.faceW());
76 set.setExchange(ex);
77}
78
79// ------------------------------------------------------------------------------------------ K
80double sphereFrac(double cx, double cy, double cz, double R, int x, int y, int z) {
81 const int NS = 8;
82 int in = 0;
83 for (int k = 0; k < NS; ++k)
84 for (int j = 0; j < NS; ++j)
85 for (int i = 0; i < NS; ++i) {
86 const double px = x + (i + 0.5) / NS - cx, py = y + (j + 0.5) / NS - cy,
87 pz = z + (k + 0.5) / NS - cz;
88 in += (px * px + py * py + pz * pz < R * R) ? 1 : 0;
89 }
90 return static_cast<double>(in) / (NS * NS * NS);
91}
92
93void gateClipOnBlockPath() {
94 std::printf(
95 "\n=== K the admissibility clip is live on the BLOCK path (a speck at B's fringe)\n");
96 constexpr int NX = 48, NY = 32, NZ = 32;
97 constexpr double R = 5.0, SIGMA = 320.0, BX = 32.3, CY = 16.2, CZ = 16.1;
98 const std::array<int, 6> box = {18, 7, 7, 41, 26, 26};
99 // (25, 15, 16): 1.84 cells outside B's surface -- measured |kappa| 4.41 unclipped
100 constexpr int SX = 25, SY = 15, SZ = 16;
101 const long nx = box[3] - box[0], ny = box[4] - box[1];
102 auto loc = [&](int x, int y, int z) {
103 return (x - box[0]) + nx * ((y - box[1]) + ny * static_cast<long>(z - box[2]));
104 };
105 auto build = [&](peclet::flow::IbmSolver& s, bool speck, bool clip) {
106 s.setAdvection(false);
107 s.setRho(1.0);
108 s.setMu(0.5);
109 s.setPressureGeometry(std::vector<double>(static_cast<std::size_t>(NX) * NY * NZ, 10.0));
110 s.enableVof();
111 s.setVof(std::vector<double>(static_cast<std::size_t>(NX) * NY * NZ, 0.0));
112 s.setSurfaceTension(SIGMA);
113 if (!clip)
114 s.setVofBlockKappaClip(false);
115 std::vector<double> c;
116 for (int z = box[2]; z < box[5]; ++z)
117 for (int y = box[1]; y < box[4]; ++y)
118 for (int x = box[0]; x < box[3]; ++x)
119 c.push_back(sphereFrac(BX, CY, CZ, R, x, y, z));
120 if (speck) {
121 CHECK(c[loc(SX, SY, SZ)] == 0.0); // the cell really is detached
122 c[loc(SX, SY, SZ)] = 1e-6;
123 }
124 s.enableVofBlocksFromColours({box}, {c});
125 s.enableVofBlockCsf(); // computes the block curvature and the CSF force
126 };
128 build(ref, false, true);
129 build(off, true, false);
130 build(on, true, true);
131 const double kOff = off.vofBlockKappa(0)[loc(SX, SY, SZ)];
132 const double kOn = on.vofBlockKappa(0)[loc(SX, SY, SZ)];
133 const long clipOn = on.vofBlockCurvatureStats().clipped,
134 clipOff = off.vofBlockCurvatureStats().clipped,
135 clipRef = ref.vofBlockCurvatureStats().clipped;
136 // face force on the speck's six faces (low face of (SX,SY,SZ) and of its +1 neighbours)
137 const long gi = SX + static_cast<long>(NX) * (SY + static_cast<long>(NY) * SZ);
138 const long st[3] = {1, NX, static_cast<long>(NX) * NY};
139 double dFon = 0.0, dFoff = 0.0, dFallOn = 0.0;
140 for (int c = 0; c < 3; ++c) {
141 const auto fr = ref.getVofBlockForce(c), fo = off.getVofBlockForce(c),
142 fn = on.getVofBlockForce(c);
143 for (long f : {gi, gi + st[c]}) {
144 dFon = std::fmax(dFon, std::fabs(fn[f] - fr[f]));
145 dFoff = std::fmax(dFoff, std::fabs(fo[f] - fr[f]));
146 }
147 for (std::size_t i = 0; i < fr.size(); ++i)
148 dFallOn = std::fmax(dFallOn, std::fabs(fn[i] - fr[i]));
149 }
150 const double bound = SIGMA * 1e-6; // sigma max|dC_speck| / Delta^2 on the speck's own faces
151 std::printf(
152 " speck kappa: clip off %.4f, clip on %.4f; clipped: on %ld, off %ld, no-speck %ld\n", kOff,
153 kOn, clipOn, clipOff, clipRef);
154 std::printf(
155 " |F - F_ref| on the speck's faces: clip on %.3e, clip off %.3e (bound %.3e); "
156 "anywhere, clip on: %.3e\n",
157 dFon, dFoff, bound, dFallOn);
158 CHECK(std::fabs(kOff) > 1.0); // the degenerate fit really is unbounded without the clip
159 CHECK(std::fabs(kOn) <= 1.0);
160 CHECK(clipOn >= 1);
161 CHECK(clipOff == 0);
162 CHECK(clipRef == 0); // a resolved marker never fires it
163 CHECK(dFon <= bound * (1.0 + 1e-12));
164}
165
166// ------------------------------------------------------------------------------------------ P
168bool sameTunables(const VofCurvature& a, const VofCurvature& b) {
169 bool ok = true;
170 for (int d = 0; d < 3; ++d)
171 ok = ok && a.metric.h[d] == b.metric.h[d];
172 return ok && a.weightWidth == b.weightWidth && a.pureEps == b.pureEps && a.monoTol == b.monoTol &&
173 a.ptWeightWidth == b.ptWeightWidth && a.cosMin == b.cosMin &&
174 a.interfaceEps == b.interfaceEps && a.debugForceFallback == b.debugForceFallback &&
175 a.debugSingleDirection == b.debugSingleDirection &&
176 a.useMixedHeightFit == b.useMixedHeightFit && a.kappaMax == b.kappaMax &&
177 a.useWorklist == b.useWorklist;
178}
179
180void gatePrototypePropagation() {
181 std::printf("\n=== P the block prototype's tunables reach every block cascade and advector\n");
182 const I3 gs{40, 40, 40};
183 WyAdvector pa;
184 pa.init(gs.x, gs.y, gs.z, 1.0, G);
185 vofscene::Block blk = vofscene::blockOf(pa, I3{0, 0, 0});
186 vofscene::fillUniform(pa, blk, 1.0, 0.0, 0.0);
187 VofBlockSet set;
188 set.init(gs, {true, true, true}, 0, 1, 1.0);
189 serialExchange(set, pa, gs);
190 // every tunable at a NON-default value, so a field the copy forgets is caught
191 const peclet::flow::vof::VofMetric m{{1.0, 1.0, 1.0 + 1e-9}};
192 set.setMetric(m);
193 VofCurvature& p = set.curvProto;
194 p.weightWidth = 2.4;
195 p.monoTol = 2e-6;
196 p.ptWeightWidth = 1.9;
197 p.cosMin = 0.21;
198 p.interfaceEps = 3e-8;
199 p.debugSingleDirection = true;
200 p.debugForceFallback = true; // P computes no curvature; set so a copy that drops it is caught
201 p.useMixedHeightFit = true;
202 p.useWorklist = false;
203 p.kappaMax = 1.7;
204 set.setWispEps(2e-9); // sets the advectors AND curvProto.pureEps
205 const VofCurvature defaults;
206 CHECK(!sameTunables(p, defaults));
207 set.enableCsf(1.0);
208 set.seedSphere(8.3, 20.1, 20.2, 5.0);
209 set.scatter(pa.colour());
210 auto allReached = [&](const char* when) {
211 bool ok = true;
212 for (const auto& b : set.blocks()) {
213 if (!b.mine())
214 continue;
215 const bool c = sameTunables(b.curvature(), set.curvProto);
216 const bool a =
217 b.advector().wispEps == set.wispEps && b.advector().metric.h[2] == set.metric().h[2];
218 std::printf(" %-34s block %ld: cascade tunables %s, advector wispEps/metric %s\n", when,
219 b.id, c ? "match" : "MISMATCH", a ? "match" : "MISMATCH");
220 ok = ok && c && a;
221 }
222 return ok;
223 };
224 CHECK(allReached("after seeding"));
225 long nRe = 0;
226 for (long s = 0; s < 40; ++s) { // 8 cells of translation: several re-centrings
227 set.advect(0.2, pa.colour());
228 nRe += set.blocks()[0].stats().recentred ? 1 : 0;
229 }
230 std::printf(" %ld re-centrings\n", nRe);
231 CHECK(nRe > 0);
232 CHECK(allReached("after re-centring"));
233 set.setWispEps(4e-9);
234 set.setKappaMax(0.9);
235 CHECK(set.curvProto.pureEps == 4e-9 && set.curvProto.kappaMax == 0.9);
236 CHECK(allReached("after setWispEps/setKappaMax"));
237}
238
239// ------------------------------------------------------------------------------------------ W
240void gateWrapRecentre() {
241 std::printf("\n=== W §12.3 lossless periodic-wrap re-centring (a seam-straddling marker)\n");
242 for (double wisp : {0.0, 1e-8}) {
243 const int NX = 20, NY = 40, NZ = 40;
244 const double h = 1.0, R = 5.0, cx = 1.3, cy = 12.3, cz = 20.1, dt = 0.2;
245 WyAdvector pa, ref;
246 pa.init(NX, NY, NZ, h, G);
247 ref.init(NX, NY, NZ, h, G);
248 const I3 e = pa.extent(), n = pa.inner();
249 vofscene::Block blk = vofscene::blockOf(pa, I3{0, 0, 0});
250 vofscene::fillUniform(pa, blk, 0.0, 1.0, 0.0);
251 vofscene::fillUniform(ref, blk, 0.0, 1.0, 0.0);
252 ref.exchange = [e](SField f) { vofscene::periodicFill(f, e, G, true, true, true); };
253 ref.wispEps = wisp;
254 {
255 SField c = ref.colour();
257 blk, KOKKOS_LAMBDA(long i, int gx, int gy, int gz) {
258 c(i) =
259 peclet::flow::vof::sphereCellFraction(cx, cy, cz, R, gx * h, gy * h, gz * h, h, 4) +
260 peclet::flow::vof::sphereCellFraction(cx + NX, cy, cz, R, gx * h, gy * h, gz * h, h,
261 4) +
262 peclet::flow::vof::sphereCellFraction(cx - NX, cy, cz, R, gx * h, gy * h, gz * h, h,
263 4);
264 });
265 ref.syncGhosts();
266 }
267 VofBlockSet set;
268 set.init(I3{NX, NY, NZ}, {true, true, true}, 0, 1, h);
269 set.bubbleEps = 0.0;
270 set.setWispEps(wisp);
271 serialExchange(set, pa, I3{NX, NY, NZ});
272 set.seedSphere(cx, cy, cz, R);
273 set.scatter(pa.colour());
274 const int lo0 = set.blocks()[0].box.lo[0], hi0 = set.blocks()[0].box.hi[0];
275 const double v0 = set.blocks()[0].stats().volume;
276 double worst = 0.0;
277 long nRe = 0;
278 for (long s = 0; s < 60; ++s) {
279 ref.advect(dt, s);
280 set.advect(dt, pa.colour());
281 nRe += set.blocks()[0].stats().recentred ? 1 : 0;
282 auto hu = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), pa.colour());
283 auto hr = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), ref.colour());
284 for (int z = 0; z < NZ; ++z)
285 for (int y = 0; y < NY; ++y)
286 for (int x = 0; x < NX; ++x) {
287 const long i = L3(x + G, y + G, z + G, e);
288 worst = std::fmax(worst, std::fabs(hu(i) - hr(i)));
289 }
290 }
291 const auto& st = set.blocks()[0].stats();
292 const double dv = std::fabs(st.volume - v0) / v0;
293 std::printf(
294 " wispEps %.0e: box x [%d,%d) -> [%d,%d); %ld re-centrings, fullAxis %ld, "
295 "discarded %.3e, volume rel %.3e, max|union - global| %.3e\n",
296 wisp, lo0, hi0, st.lo[0], st.hi[0], nRe, st.fullAxis, st.discarded, dv, worst);
297 CHECK(lo0 < 0); // seeded across the seam
298 CHECK(st.lo[0] == 0 && st.hi[0] == NX);
299 CHECK(st.fullAxis > 0);
300 CHECK(st.discarded == 0.0);
301 CHECK(dv <= 1e-14);
302 if (wisp == 0.0)
303 CHECK(worst <= 1e-14); // lossless: the union IS the global field to round-off
304 (void)n;
305 }
306}
307
308// ------------------------------------------------------------------------------------------ R
309void gateResidueReturn() {
310 std::printf("\n=== R §13 residue return: 1000 kinematic steps, volume exact at wispEps 1e-8\n");
311 const I3 gs{48, 48, 48};
312 const long steps = 1000;
313 double dv[2] = {0, 0}, disc[2] = {0, 0}, res[2] = {0, 0};
314 const double wisps[2] = {0.0, 1e-8};
315 for (int k = 0; k < 2; ++k) {
316 WyAdvector pa;
317 pa.init(gs.x, gs.y, gs.z, 1.0, G);
318 vofscene::Block blk = vofscene::blockOf(pa, I3{0, 0, 0});
319 vofscene::fillUniform(pa, blk, 0.9, 0.6, 0.3);
320 VofBlockSet set;
321 set.init(gs, {true, true, true}, 0, 1, 1.0);
322 set.setWispEps(wisps[k]);
323 serialExchange(set, pa, gs);
324 set.seedSphere(24.2, 24.1, 23.9, 8.0);
325 set.scatter(pa.colour());
326 const double v0 = set.blocks()[0].stats().volume;
327 for (long s = 0; s < steps; ++s)
328 set.advect(0.2, pa.colour());
329 const auto& st = set.blocks()[0].stats();
330 dv[k] = (st.volume - v0) / v0;
331 disc[k] = st.discarded;
332 res[k] = st.residueReturned;
333 std::printf(" wispEps %.0e: volume rel change %+.3e, discarded %.3e, residueReturned %.3e\n",
334 wisps[k], dv[k], disc[k], res[k]);
335 }
336 CHECK(std::fabs(dv[1]) <= 1e-14);
337 CHECK(disc[1] == 0.0);
338 CHECK(res[1] != 0.0); // the pass ran: residue exists and was returned
339 CHECK(res[0] == 0.0); // wispEps 0 = the W0 container verbatim: the pass never runs
340}
341
342} // namespace
343
344// ------------------------------------------------------------------------------------------ M
349 std::printf("\n=== M enable_vof_blocks on an anisotropic box: the blocks carry its metric\n");
350 constexpr int NX = 32, NY = 32, NZ = 32;
351 const std::array<int, 6> box = {8, 8, 8, 24, 24, 24};
352 // cells 1.5 : 1 : 2 -- every axis different, so a dropped or permuted metric is caught
353 peclet::flow::IbmSolver s(NX, NY, NZ, {48.0, 32.0, 64.0}, {0.0, 0.0, 0.0}, {NX, NY, NZ});
354 s.setAdvection(false);
355 s.setRho(1.0);
356 s.setMu(0.5);
357 s.setPressureGeometry(std::vector<double>(static_cast<std::size_t>(NX) * NY * NZ, 10.0));
358 s.enableVof();
359 s.setVof(std::vector<double>(static_cast<std::size_t>(NX) * NY * NZ, 0.0));
360 s.setSurfaceTension(1.0);
361 std::vector<double> c;
362 for (int z = box[2]; z < box[5]; ++z)
363 for (int y = box[1]; y < box[4]; ++y)
364 for (int x = box[0]; x < box[3]; ++x)
365 c.push_back(sphereFrac(16.2, 16.1, 15.9, 5.0, x, y, z));
366 s.enableVofBlocksFromColours({box}, {c});
367 s.enableVofBlockCsf();
368 const auto* set = s.vofBlockSet();
369 auto ratios = [](const peclet::flow::vof::VofMetric& m, const char* who) {
370 const double rx = m.h[0] / m.h[1], rz = m.h[2] / m.h[1];
371 std::printf(" %-22s h = (%.4f, %.4f, %.4f), hx/hy %.4f, hz/hy %.4f\n", who, m.h[0], m.h[1],
372 m.h[2], rx, rz);
373 return std::fabs(rx - 1.5) < 1e-12 && std::fabs(rz - 2.0) < 1e-12;
374 };
375 CHECK(set != nullptr);
376 CHECK(ratios(set->metric(), "set"));
377 CHECK(ratios(set->curvProto.metric, "curvature prototype"));
378 for (const auto& b : set->blocks()) {
379 if (!b.mine())
380 continue;
381 CHECK(ratios(b.curvature().metric, "block cascade"));
382 CHECK(ratios(b.advector().metric, "block advector"));
383 }
384}
385
386int main(int argc, char** argv) {
387 Kokkos::initialize(argc, argv);
388 {
389 std::printf("VoF block container: overlap-robustness rules (vof_overlap_design), backend %s\n",
390 peclet::flow::SExec::name());
391 gateClipOnBlockPath();
392 gatePrototypePropagation();
394 gateWrapRecentre();
395 gateResidueReturn();
396 std::printf("\n%s (%d failure%s)\n", failures ? "FAILED" : "PASSED", failures,
397 failures == 1 ? "" : "s");
398 }
399 Kokkos::finalize();
400 return failures ? 1 : 0;
401}
flow — VoF Part III rung W0: the per-bubble VoF BLOCK, a third container over the same L1 kernels (su...
flow — VoF Part III rung W0: the block gather/scatter (VOF_PLAN.md §10 design items 2 and 4).
The gather / scatter / table replication of the block container.
The container: a replicated table of blocks, the per-step orchestration (gather → advect → re-centre ...
void enableCsf(double sigmaValue)
Turn on the block CSF.
double bubbleEps
The colour threshold that defines the BUBBLE EXTENT (and hence the box).
void setExchange(std::shared_ptr< VofBlockExchangeBase > x)
std::vector< VofBlock > & blocks()
The block table.
std::array< bool, 3 > periodic() const
VofCurvature curvProto
The curvature cascade's TUNABLES for every block, copied into each block's own VofCurvature when it i...
void setKappaMax(double km)
Set the curvature admissibility clip (VofCurvature::kappaMax, index units) on the prototype AND on ev...
void seedSphere(double cx, double cy, double cz, double r, int subLevels=4, int extraPad=0)
Seed a spherical bubble (centre and radius in CELL units, global).
void init(I3 gs, std::array< bool, 3 > per, int rank, int size, double h=1.0)
const VofMetric & metric() const
void scatter(SField cLocal)
Union the current colour into cLocal without advecting (used right after seeding).
void advect(double dt, SField cLocal)
One kinematic block step: gather the face velocity, advect every local block, re-centre,...
void setMetric(const VofMetric &g)
The anisotropic cell metric (Phase 3).
void setWispEps(double eps)
Set wispEps on the set AND on every live advector (pooled ones take it on reuse).
double wispEps
The Weymouth-Yue wisp tolerance of every block advector (WyAdvector::wispEps); the solver sets it to ...
Curvature of the colour field on an extended (inner + ghost) block.
double pureEps
Tolerance of the PURITY test that ends a height-function column (hfIsFull/hfIsEmpty),...
double kappaMax
Curvature admissibility clip (doc/vof_overlap_design.md §5.1, §11): after the cascade,...
VofMetric metric
The anisotropic cell metric (Phase 3).
Weymouth-Yue split advection of a colour field on an extended (inner + ghost) block.
SField colour() const
Colour field on the extended block (x-fastest).
void init(int nx, int ny, int nz, double h, int ghost=3)
SField faceU() const
Face velocity in direction d at the +d face of each cell: uf(i,j,k) sits at (i+1/2,...
flow — host-facing Kokkos IBM Navier-Stokes solver (drop-in flow-style API).
y
Definition cmp.py:24
dict res
Definition mgd_case.py:488
Kokkos::View< double *, SMem > SField
long L3(int x, int y, int z, I3 e)
Solver< Staggered > IbmSolver
int m
Definition post.py:35
worst
Definition stab5.py:6
void forEachExtended(Block b, F fn)
Visit every cell of the extended block with (local linear index, global cell index).
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...
void fillUniform(WyAdvector &a, Block b, double ux, double uy, double uz)
Uniform translation. Discrete divergence is EXACTLY zero (identical values differenced).
Block blockOf(const WyAdvector &a, I3 origin)
static constexpr int NY
static constexpr int NX
static constexpr int NZ
This rank's own patch: the extended block the face velocity and the union colour live on.
I3 o
global index of inner cell (0,0,0)
Half-open global cell box [lo, hi).
Where this block sits in the global grid: o is the global index of inner cell (0,0,...
static constexpr int G
static constexpr double R
static void build(IbmSolver &s, std::size_t cells)
static constexpr double SIGMA
static constexpr double CY
static constexpr int SZ
static constexpr int SX
static constexpr double BX
static constexpr double CZ
static double sphereFrac(double cx, int x, int y, int z)
void gateSolverMetric()
The Solver hands the anisotropic cell metric to the block set it creates – in the documented order th...
long L3(int x, int y, int z, I3 e)
#define CHECK(cond)