19#include <Kokkos_Core.hpp>
33 std::fprintf(stderr, "CHECK failed: %s\n at %s:%d\n", #cond, __FILE__, __LINE__); \
38const double kDeg = M_PI / 180.0;
48Plane makePlane(
double thetaDeg,
double psiDeg,
const double p[3]) {
50 const double th = thetaDeg * kDeg, ps = psiDeg * kDeg;
51 pl.m[0] = std::sin(th) * std::cos(ps);
52 pl.m[1] = std::sin(th) * std::sin(ps);
53 pl.m[2] = std::cos(th);
54 pl.A = pl.m[0] *
p[0] + pl.m[1] *
p[1] + pl.m[2] *
p[2];
57double frac(
const Plane& pl,
int i,
int j,
int k) {
58 return planeCellFraction(pl.m[0], pl.m[1], pl.m[2], pl.A, i, j, k, 1.0);
63double fillOnce(
const double mf[3],
double cf,
double thetaDeg,
int pivot,
const int f[3],
64 const int s[3],
double* cosApp =
nullptr,
int* branch =
nullptr) {
65 const double nw[3] = {0.0, 0.0, 1.0};
66 const double sdfF = (
f[2] + 0.5) - zw;
67 double mth[3], alphaTh,
ca;
68 const int br = vofWettingPlane(mf, cf, nw, std::cos(thetaDeg * kDeg), std::sin(thetaDeg * kDeg),
69 sdfF, pivot, 1e-6, mth, alphaTh, ca);
74 const int ds[3] = {
s[0] -
f[0],
s[1] -
f[1],
s[2] -
f[2]};
75 return vofWettingFraction(mth, alphaTh, ds);
79const char* kPivName[4] = {
"volume (DEFAULT)",
"interface p_f (Afkhami-Bussmann)",
80 "wall-normal (WO-S as written)",
"contact-line"};
83 const double thetas[5] = {30.0, 60.0, 90.0, 120.0, 150.0};
84 const double psis[3] = {0.0, 37.0, 90.0};
85 std::printf(
"G0a/G0b flat-wall idempotence (EXACT m_f): max |C_fill - C_exact| over the band\n");
87 " theta | pivot 0 volume | pivot 1 interface | pivot 2 wall-normal |"
88 " pivot 3 contact-line\n");
89 double worst[4] = {0.0, 0.0, 0.0, 0.0};
90 for (
int ti = 0; ti < 5; ++ti) {
91 double e[4] = {0.0, 0.0, 0.0, 0.0};
92 for (
int pi = 0; pi < 3; ++pi)
93 for (
int piv = 0; piv < 4; ++piv) {
94 const double p[3] = {3.35, 3.6, zw};
95 const Plane pl = makePlane(thetas[ti], psis[pi], p);
96 for (
int dx = -1;
dx <= 1; ++
dx)
97 for (
int dy = -1;
dy <= 1; ++
dy) {
98 const int f[3] = {3 +
dx, 3 +
dy, 3};
99 const double cf = frac(pl, f[0], f[1], f[2]);
100 if (cf <= 1e-9 || cf >= 1.0 - 1e-9)
102 for (
int ks = 0;
ks <= 2; ++
ks) {
103 const int s[3] = {
f[0],
f[1],
ks};
104 const double got = fillOnce(pl.m, cf, thetas[ti], piv, f, s);
105 const double ref = frac(pl, s[0], s[1], s[2]);
106 e[piv] = std::fmax(e[piv], std::fabs(got - ref));
110 std::printf(
" %5.0f | %17.3e | %18.3e | %19.3e | %20.3e\n", thetas[ti], e[0], e[1], e[2],
112 for (
int piv = 0; piv < 4; ++piv)
113 worst[piv] = std::fmax(worst[piv], e[piv]);
115 for (
int piv = 0; piv < 4; ++piv)
116 std::printf(
" pivot %d (%s): worst %.3e\n", piv, kPivName[piv], worst[piv]);
117 CHECK(worst[0] <= 1e-12);
118 CHECK(worst[1] <= 1e-12);
119 CHECK(worst[3] <= 1e-12);
124 double worstAng = 0.0, worstPivot = 0.0;
125 for (
double thA = 20.0; thA <= 160.0; thA += 20.0)
126 for (
double thT = 15.0; thT <= 165.0; thT += 15.0) {
127 const double p[3] = {3.35, 3.6, zw};
128 const Plane pl = makePlane(thA, 37.0, p);
129 const int f[3] = {3, 3, 3};
130 const double cf = frac(pl, f[0], f[1], f[2]);
131 if (cf <= 1e-6 || cf >= 1.0 - 1e-6)
133 const double nw[3] = {0, 0, 1};
134 double mth[3], alphaTh,
ca;
135 vofWettingPlane(pl.m, cf, nw, std::cos(thT * kDeg), std::sin(thT * kDeg), (f[2] + 0.5) - zw,
136 kVofPivotInterface, 1e-6, mth, alphaTh, ca);
137 worstAng = std::fmax(worstAng, std::fabs(mth[2] - std::cos(thT * kDeg)));
139 double mf[3] = {pl.m[0], pl.m[1], pl.m[2]};
142 vofPlicCentroid(mf, plicAlpha(mf[0], mf[1], mf[2], cf),
pf);
143 worstPivot = std::fmax(worstPivot,
144 std::fabs(mth[0] *
pf[0] + mth[1] *
pf[1] + mth[2] *
pf[2] - alphaTh));
146 worstAng = std::fmax(worstAng, std::fabs(std::acos(ca) / kDeg - thA) / 180.0);
149 "G0c rotation: max |m_theta . n_w - cos(theta)| %.3e ; max |plane(p_f) - alpha| %.3e\n",
150 worstAng, worstPivot);
151 CHECK(worstAng <= 1e-12);
152 CHECK(worstPivot <= 1e-12);
157 const double p[3] = {3.35, 3.6, zw};
158 const Plane pl = makePlane(85.0, 0.0, p);
159 const int f[3] = {3, 3, 3};
160 const double cf = frac(pl, f[0], f[1], f[2]);
161 const int s[3] = {3, 3, 2};
162 const double c0 = fillOnce(pl.m, cf, 0.0, kVofPivotInterface, f, s);
163 const double c180 = fillOnce(pl.m, cf, 180.0, kVofPivotInterface, f, s);
164 const double c30 = fillOnce(pl.m, cf, 30.0, kVofPivotInterface, f, s);
165 const double c150 = fillOnce(pl.m, cf, 150.0, kVofPivotInterface, f, s);
167 "G0d wetting limits (C_f = %.4f at theta_a = 85 deg): C_band = %.6f (theta 0), "
168 "%.6f (30), %.6f (150), %.6f (180)\n",
169 cf, c0, c30, c150, c180);
172 CHECK(c30 > cf && c150 < cf);
183void fluidOnlyYoungs() {
185 "G0e fluid-only Youngs next to a flat wall, from EXACT plane fractions:\n"
186 " theta | angle-to-wall err | full-stencil err | azimuth err | fill err (band)\n");
187 double worstAz = 0.0, worstFill = 0.0;
188 for (
double th = 30.0; th <= 150.0; th += 30.0) {
189 double eAng = 0.0, eFull = 0.0, eAz = 0.0, eFill = 0.0;
190 for (
double psi = 0.0; psi <= 90.0; psi += 45.0)
191 for (
double sh = 0.0;
sh < 1.0;
sh += 0.25) {
192 const double p[3] = {3.0 +
sh, 3.5, zw};
193 const Plane pl = makePlane(th, psi, p);
194 const int f[3] = {3, 3, 3};
196 unsigned char fl[27], flAll[27];
197 for (
int k = 0;
k < 3; ++
k)
198 for (
int j = 0;
j < 3; ++
j)
199 for (
int i = 0;
i < 3; ++
i) {
200 const int q = plicSt(i, j, k);
201 const int cx =
f[0] +
i - 1, cy =
f[1] +
j - 1, cz =
f[2] +
k - 1;
202 c27[
q] = frac(pl, cx, cy, cz);
203 fl[
q] = (cz + 1 <= zw) ? 0u : 1u;
206 double m[3], mAll[3];
207 if (!youngsNormalFluidOnly(c27, fl, m))
209 youngsNormalFluidOnly(c27, flAll, mAll);
210 const double mn = std::sqrt(m[0] * m[0] + m[1] * m[1] + m[2] * m[2]);
211 const double an = std::sqrt(mAll[0] * mAll[0] + mAll[1] * mAll[1] + mAll[2] * mAll[2]);
213 eAng, std::fabs(std::acos(std::fmax(-1.0, std::fmin(1.0, m[2] / mn))) / kDeg - th));
215 eFull, std::fabs(std::acos(std::fmax(-1.0, std::fmin(1.0, mAll[2] / an))) / kDeg - th));
217 const double tx =
m[0], ty =
m[1];
218 if (std::sqrt(tx * tx + ty * ty) > 1e-12) {
219 double d = std::atan2(ty, tx) / kDeg - psi;
224 eAz = std::fmax(eAz, std::fabs(d));
227 const double cf = frac(pl, f[0], f[1], f[2]);
228 if (cf > 1e-9 && cf < 1.0 - 1e-9)
229 for (
int ks = 0;
ks <= 2; ++
ks) {
230 const int sc[3] = {
f[0],
f[1],
ks};
231 eFill = std::fmax(eFill, std::fabs(fillOnce(m, cf, th, kVofPivotVolume, f, sc) -
232 frac(pl, sc[0], sc[1], sc[2])));
235 std::printf(
" %5.0f | %17.3f | %16.3f | %12.3f | %16.3e\n", th, eAng, eFull, eAz, eFill);
236 worstAz = std::fmax(worstAz, eAz);
237 worstFill = std::fmax(worstFill, eFill);
239 std::printf(
" worst azimuth error %.3f deg ; worst end-to-end band error %.3e\n", worstAz,
241 CHECK(worstAz <= 3.0);
242 CHECK(worstFill <= 0.05);
261 int nx = 40, nz = 32;
265 double zw = 4.25, R = 10.0, sigma = 1.0, mu = 0.5;
266 std::vector<double> sdf, colour;
269 void build(
double thetaDeg) {
270 sdf.assign((std::size_t)nx * nx * nz, 0.0);
271 colour.assign((std::size_t)nx * nx * nz, 0.0);
273 const double ct = std::cos(thetaDeg * kDeg);
274 const double V = 2.0 / 3.0 * M_PI * R * R * R;
275 const double Rc = std::cbrt(3.0 * V / (M_PI * (1.0 - ct) * (1.0 - ct) * (2.0 + ct)));
276 const double zc = zw - Rc * ct;
277 const double cx = nx * 0.5, cy = nx * 0.5;
278 for (
int z = 0;
z < nz; ++
z)
279 for (
int y = 0;
y < nx; ++
y)
280 for (
int x = 0;
x < nx; ++
x) {
281 const std::size_t
i =
282 (std::size_t)x + (std::size_t)
y * nx + (std::size_t)z * (std::size_t)nx * nx;
283 sdf[
i] = std::fmin((z + 0.5) - zw, (nz - zw) - (z + 0.5));
285 for (
int a = 0;
a < 4; ++
a)
286 for (
int b = 0;
b < 4; ++
b)
287 for (
int c = 0;
c < 4; ++
c) {
288 const double px =
x + (
a + 0.5) / 4.0, py = y + (b + 0.5) / 4.0,
289 pz =
z + (
c + 0.5) / 4.0;
290 if (pz < zw || pz > nz - zw)
293 const double dx = px - cx,
dy = py - cy,
dz = pz - zc;
294 if (dx * dx + dy * dy + dz * dz < Rc * Rc)
297 colour[
i] =
tot ? (double)in / tot : 0.0;
306 double theta = 0, h = 0, a = 0, drift = 0, ca = 0, umaxAll = 0, apparent = 0;
307 long iters = 0, contact = 0, neighbour = 0, pure = 0, neutral = 0;
308 std::vector<double> filled, kind, eps;
311CapResult relaxCap(
double thetaSet,
int steps,
bool setAngle =
true) {
313 sc.build(setAngle ? thetaSet : 90.0);
315 s.setAdvection(
false);
318 s.setSolid(
sc.sdf,
true);
321 s.setSurfaceTension(
sc.sigma);
323 s.setContactAngle(thetaSet);
324 const double dtc = 0.5 *
s.capillaryDt();
327 const double v0 =
s.vofDiagnostics().volume;
329 r.filled =
s.getVofFilledColour();
330 for (
int i = 0;
i <
steps; ++
i) {
331 const double c =
s.vofMaxCourant();
333 dt = std::fmin(dtc, dt * 0.15 / c);
335 }
else if (dt < dtc && c < 0.075) {
336 dt = std::fmin(dtc, 1.2 * dt);
340 r.iters = std::max(
r.iters,
s.lastPressureIterations());
342 const auto d =
s.vofDiagnostics();
343 const auto cd =
s.contactAngleDiagnostics();
344 const auto cc =
s.getVof();
345 const auto eps =
s.getVofGeometry(0);
346 const int ix =
sc.nx / 2;
347 for (
int z = 0;
z <
sc.nz; ++
z) {
348 const std::size_t
i =
349 (std::size_t)ix + (std::size_t)ix *
sc.nx + (std::size_t)z * (std::size_t)
sc.nx *
sc.nx;
352 r.a = std::sqrt(std::fmax((6.0 *
d.volume / (M_PI *
r.h) -
r.h *
r.h) / 3.0, 1e-12));
353 r.theta = 2.0 * std::atan2(
r.h,
r.a) / kDeg;
354 r.drift = std::fabs(
d.volume - v0) /
v0;
355 const int z0 = (
int)std::ceil(
sc.zw);
356 for (
int c = 0;
c < 3; ++
c) {
357 const auto v =
s.getVelocity(c);
358 for (
int z = 0;
z <
sc.nz; ++
z)
359 for (
int y = 0;
y <
sc.nx; ++
y)
360 for (
int x = 0;
x <
sc.nx; ++
x) {
361 const double av = std::fabs(v[(std::size_t)x + (std::size_t)y *
sc.nx +
362 (std::size_t)z * (std::size_t)
sc.nx *
sc.nx]);
363 r.umaxAll = std::fmax(
r.umaxAll, av);
365 r.ca = std::fmax(
r.ca,
sc.mu * av /
sc.sigma);
368 r.kind =
s.getVofGeometry(4);
372 r.apparent =
cd.meanApparentAngle;
373 r.contact =
cd.contactCells;
374 r.neighbour =
cd.neighbourCells;
375 r.pure =
cd.pureCells;
376 r.neutral =
cd.neutralCells;
377 r.filled =
s.getVofFilledColour();
382 const double thetas[3] = {60.0, 90.0, 120.0};
383 const int steps = std::getenv(
"PECLET_VOF_WETTING_LONG") ? 600 : 150;
385 "G1 the prescribed angle is a fixed point (cap of the SET angle on a flat SDF wall "
386 "at z = 4.25 — CUT wall cells, D/dx = 20, sigma 1, mu 0.5, %d steps):\n",
389 for (
double th : thetas) {
390 const CapResult
r = relaxCap(th, steps);
392 " theta_set %5.1f -> %7.3f (err %+6.3f) h %6.3f a %6.3f apparent %6.2f "
393 "dV/V %.2e Ca(open) %.3e max|u| all %.3e band th/nbr/pure/neu "
394 "%ld/%ld/%ld/%ld iters %ld\n",
395 th,
r.theta,
r.theta - th,
r.h,
r.a,
r.apparent,
r.drift,
r.ca,
r.umaxAll,
r.contact,
396 r.neighbour,
r.pure,
r.neutral,
r.iters);
398 worst = std::fmax(worst, std::fabs(
r.theta - th));
400 std::printf(
" G1 worst |theta - theta_set| = %.3f deg (gate 3.0)\n", worst);
404 const CapResult
a = relaxCap(90.0, 0,
true),
b = relaxCap(90.0, 0,
false);
405 double dm = 0.0, dmReach = 0.0;
406 const int NX =
a.nx,
NZ =
a.nz;
407 const auto at = [&](
const std::vector<double>&
v,
int x,
int y,
int z) {
408 return v[(std::size_t)x + (std::size_t)
y *
NX + (std::size_t)z * (std::size_t)
NX *
NX];
410 for (
int z = 0;
z <
NZ; ++
z)
411 for (
int y = 0;
y <
NX; ++
y)
412 for (
int x = 0;
x <
NX; ++
x) {
413 if (at(
a.kind, x, y, z) < 0.5)
415 const double d = std::fabs(at(
a.filled, x, y, z) - at(
b.filled, x, y, z));
416 dm = std::fmax(dm, d);
420 for (
int dz = -3;
dz <= 3 && !reach; ++
dz)
421 for (
int dy = -3;
dy <= 3 && !reach; ++
dy)
422 for (
int dx = -3;
dx <= 3 && !reach; ++
dx) {
423 const int px =
x +
dx, py =
y +
dy, pz =
z +
dz;
424 if (px < 0 || py < 0 || pz < 0 || px >=
NX || py >=
NX || pz >=
NZ)
426 if (at(
a.kind, px, py, pz) < 0.5)
430 dmReach = std::fmax(dmReach, d);
432 const CapResult
a1 = relaxCap(90.0, steps,
true), b1 = relaxCap(90.0, steps,
false);
434 "G6 theta = 90 vs WO-Q's neutral fill on the same scene, at t = 0: "
435 "max |C_theta - C_neutral| over ALL solid cells %.3e, and over the solid cells the V3 "
436 "cascade can reach (a fluid cell within +/-3) %.3e. NOT bitwise, and it cannot be — "
437 "the neutral rule is the MEAN of the fluid face neighbours, the theta rule is a plane "
438 "FRACTION, and the two coincide only where the interface is already perpendicular to "
439 "the wall; the all-cells number is dominated by band cells deeper than the neutral "
440 "rule's 3-pass reach, which it leaves untouched and the theta walk fills.\n"
441 " after %d steps: theta %.3f (theta-fill) vs %.3f (neutral), max|u| %.3e vs %.3e, "
442 "Ca(open) %.3e vs %.3e\n",
443 dm, dmReach, steps,
a1.theta, b1.theta,
a1.umaxAll, b1.umaxAll,
a1.ca, b1.ca);
444 CHECK(std::fabs(
a1.theta - b1.theta) <= 1.0);
457void anisoIdempotence() {
458 std::printf(
"\nK3 (Phase 3) anisotropic idempotence: max |C_fill - C_exact| over the band\n");
459 const double thetas[5] = {30.0, 60.0, 90.0, 120.0, 150.0};
460 const double psis[3] = {0.0, 37.0, 90.0};
462 const double hs[3][3] = {{1.0, 1.0, 1.0}, {1.0, 1.0, 2.0}, {2.0, 1.5, 1.0}};
463 double worstAll = 0.0, worstAng = 0.0;
464 for (
int q = 0;
q < 3; ++
q) {
465 const VofMetric
g{{
hs[
q][0],
hs[
q][1],
hs[
q][2]}};
466 double worst = 0.0, wang = 0.0;
467 for (
int ti = 0; ti < 5; ++ti)
468 for (
int pi = 0; pi < 3; ++pi) {
469 const double th =
thetas[ti] * kDeg, ps = psis[pi] * kDeg;
471 const double nP[3] = {std::sin(th) * std::cos(ps), std::sin(th) * std::sin(ps),
475 vofIndexNormal(nP, g, mIdx);
476 const double p[3] = {3.35 *
g.h[0], 3.6 *
g.h[1], zw *
g.h[2]};
477 const double A = nP[0] *
p[0] + nP[1] *
p[1] + nP[2] *
p[2];
478 for (
int dx = -1;
dx <= 1; ++
dx)
479 for (
int dy = -1;
dy <= 1; ++
dy) {
480 const int f[3] = {3 +
dx, 3 +
dy, 3};
482 const double cf = planeCellFractionAniso(nP[0], nP[1], nP[2], A, f[0] *
g.h[0],
483 f[1] *
g.h[1], f[2] *
g.h[2], g);
484 if (cf <= 1e-9 || cf >= 1.0 - 1e-9)
489 const double nwIdx[3] = {0.0, 0.0,
g.h[2]};
490 const double sdfF = (
f[2] + 0.5) *
g.h[2] - zw *
g.h[2];
491 for (
int piv = 0; piv < 4; ++piv) {
492 double mth[3], alphaTh,
ca;
493 const int br = vofWettingPlane(mIdx, cf, nwIdx, std::cos(th), std::sin(th), sdfF, piv,
494 1e-6, mth, alphaTh, ca, g);
499 std::fabs(std::acos(ca < -1.0 ? -1.0 : (ca > 1.0 ? 1.0 : ca)) - th) / kDeg);
500 for (
int ks = 0;
ks <= 2; ++
ks) {
501 const int sc[3] = {
f[0],
f[1],
ks};
502 const int ds[3] = {
sc[0] -
f[0],
sc[1] -
f[1],
sc[2] -
f[2]};
503 const double got = vofWettingFraction(mth, alphaTh, ds);
504 const double ref = planeCellFractionAniso(nP[0], nP[1], nP[2], A, sc[0] *
g.h[0],
505 sc[1] *
g.h[1], sc[2] *
g.h[2], g);
508 worst = std::fmax(worst, std::fabs(got - ref));
514 " h = (%.3g, %.3g, %.3g): worst |C_fill - C_exact| %.3e apparent-angle error"
516 hs[q][0], hs[q][1], hs[q][2], worst, wang);
517 CHECK(worst <= 1e-12);
519 worstAll = std::fmax(worstAll, worst);
520 worstAng = std::fmax(worstAng, wang);
522 std::printf(
" worst over all metrics: %.3e (gate 1e-12), angle %.3e deg (gate 1 deg)\n",
528 long diff = 0,
tot = 0;
529 const VofMetric
one{{1.0, 1.0, 1.0}};
530 for (
int ti = 0; ti < 5; ++ti)
531 for (
int pi = 0; pi < 3; ++pi)
532 for (
int piv = 0; piv < 4; ++piv) {
533 const double p[3] = {3.35, 3.6, zw};
534 const Plane pl = makePlane(thetas[ti], psis[pi], p);
535 for (
int dx = -1;
dx <= 1; ++
dx)
536 for (
int dy = -1;
dy <= 1; ++
dy) {
537 const int f[3] = {3 +
dx, 3 +
dy, 3};
538 const double cf = frac(pl, f[0], f[1], f[2]);
539 if (cf <= 1e-9 || cf >= 1.0 - 1e-9)
541 const double nw[3] = {0.0, 0.0, 1.0};
542 const double sdfF = (
f[2] + 0.5) - zw;
544 vofWettingPlane(pl.m, cf, nw, std::cos(thetas[ti] * kDeg),
545 std::sin(thetas[ti] * kDeg), sdfF, piv, 1e-6, m1, a1, c1);
546 vofWettingPlane(pl.m, cf, nw, std::cos(thetas[ti] * kDeg),
547 std::sin(thetas[ti] * kDeg), sdfF, piv, 1e-6, m2, a2, c2, one);
549 if (!(m1[0] == m2[0] && m1[1] == m2[1] && m1[2] == m2[2] && a1 == a2 && c1 == c2))
553 std::printf(
" RULE B: unit-metric overload vs explicit {1,1,1}: %ld/%ld differing\n", diff,
561int main(
int argc,
char** argv) {
562 std::setvbuf(stdout,
nullptr, _IONBF, 0);
563 Kokkos::initialize(argc, argv);
570 if (!std::getenv(
"PECLET_VOF_WETTING_KERNEL_ONLY"))
575 std::fprintf(stderr,
"%d check(s) FAILED\n",
failures);
577 std::printf(
"all vof wetting (G0) checks passed\n");
flow — host-facing Kokkos IBM Navier-Stokes solver (drop-in flow-style API).
Solver< Staggered > IbmSolver
one(n, rho_ratio, mu_ratio, stress, steps, adv, dtf, grav=False, sigma="none")
flow — thin shim: the container-free VoF PLIC geometric toolbox (rung V0) now live in core.
static void build(IbmSolver &s, std::size_t cells)
flow — thin shim: the container-free VoF theta-consistent solid-band fill (rung V5b) now live in core...