20#include <Kokkos_Core.hpp>
44 std::fprintf(stderr, "CHECK failed: %s\n at %s:%d\n", #cond, __FILE__, __LINE__); \
52 auto h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), f);
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));
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;
80double sphereFrac(
double cx,
double cy,
double cz,
double R,
int x,
int y,
int z) {
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;
90 return static_cast<double>(in) / (NS * NS * NS);
93void gateClipOnBlockPath() {
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};
100 constexpr int SX = 25, SY = 15,
SZ = 16;
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]));
106 s.setAdvection(
false);
109 s.setPressureGeometry(std::vector<double>(
static_cast<std::size_t
>(
NX) *
NY *
NZ, 10.0));
111 s.setVof(std::vector<double>(
static_cast<std::size_t
>(
NX) *
NY *
NZ, 0.0));
112 s.setSurfaceTension(
SIGMA);
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)
122 c[loc(
SX, SY,
SZ)] = 1
e-6;
124 s.enableVofBlocksFromColours({
box}, {
c});
125 s.enableVofBlockCsf();
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;
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]));
147 for (std::size_t i = 0;
i <
fr.size(); ++
i)
148 dFallOn = std::fmax(dFallOn, std::fabs(fn[i] - fr[i]));
152 " speck kappa: clip off %.4f, clip on %.4f; clipped: on %ld, off %ld, no-speck %ld\n", kOff,
153 kOn, clipOn, clipOff, clipRef);
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);
159 CHECK(std::fabs(kOn) <= 1.0);
163 CHECK(dFon <= bound * (1.0 + 1e-12));
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;
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};
188 set.
init(gs, {
true,
true,
true}, 0, 1, 1.0);
189 serialExchange(set, pa, gs);
191 const peclet::flow::vof::VofMetric
m{{1.0, 1.0, 1.0 + 1
e-9}};
196 p.ptWeightWidth = 1.9;
198 p.interfaceEps = 3
e-8;
199 p.debugSingleDirection =
true;
200 p.debugForceFallback =
true;
201 p.useMixedHeightFit =
true;
202 p.useWorklist =
false;
206 CHECK(!sameTunables(p, defaults));
210 auto allReached = [&](
const char* when) {
212 for (
const auto& b : set.
blocks()) {
215 const bool c = sameTunables(
b.curvature(), set.
curvProto);
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");
224 CHECK(allReached(
"after seeding"));
226 for (
long s = 0;
s < 40; ++
s) {
228 nRe += set.
blocks()[0].stats().recentred ? 1 : 0;
230 std::printf(
" %ld re-centrings\n", nRe);
232 CHECK(allReached(
"after re-centring"));
236 CHECK(allReached(
"after setWispEps/setKappaMax"));
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, 1
e-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;
257 blk, KOKKOS_LAMBDA(
long i,
int gx,
int gy,
int gz) {
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,
262 peclet::flow::vof::sphereCellFraction(cx -
NX, cy, cz,
R, gx * h, gy * h, gz * h, h,
271 serialExchange(set, pa,
I3{
NX,
NY,
NZ});
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;
278 for (
long s = 0;
s < 60; ++
s) {
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)));
291 const auto&
st = set.
blocks()[0].stats();
292 const double dv = std::fabs(
st.volume - v0) /
v0;
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);
303 CHECK(worst <= 1e-14);
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, 1
e-8};
315 for (
int k = 0;
k < 2; ++
k) {
321 set.
init(gs, {
true,
true,
true}, 0, 1, 1.0);
323 serialExchange(set, pa, gs);
326 const double v0 = set.
blocks()[0].stats().volume;
327 for (
long s = 0;
s <
steps; ++
s)
329 const auto&
st = set.
blocks()[0].stats();
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]);
336 CHECK(std::fabs(dv[1]) <= 1e-14);
337 CHECK(disc[1] == 0.0);
338 CHECK(res[1] != 0.0);
339 CHECK(res[0] == 0.0);
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};
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);
357 s.setPressureGeometry(std::vector<double>(
static_cast<std::size_t
>(
NX) *
NY *
NZ, 10.0));
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],
373 return std::fabs(rx - 1.5) < 1e-12 && std::fabs(rz - 2.0) < 1e-12;
375 CHECK(set !=
nullptr);
378 for (
const auto& b : set->
blocks()) {
381 CHECK(ratios(b.curvature().metric,
"block cascade"));
382 CHECK(ratios(b.advector().metric,
"block advector"));
386int main(
int argc,
char** argv) {
387 Kokkos::initialize(argc, argv);
389 std::printf(
"VoF block container: overlap-robustness rules (vof_overlap_design), backend %s\n",
390 peclet::flow::SExec::name());
391 gateClipOnBlockPath();
392 gatePrototypePropagation();
396 std::printf(
"\n%s (%d failure%s)\n",
failures ?
"FAILED" :
"PASSED",
failures,
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).
Kokkos::View< double *, SMem > SField
long L3(int x, int y, int z, I3 e)
Solver< Staggered > IbmSolver
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)
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 double R
static void build(IbmSolver &s, std::size_t cells)
static constexpr double SIGMA
static constexpr double CY
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)