44#include <Kokkos_Core.hpp>
68using peclet::flow::vof::plicAlpha;
71using peclet::flow::vof::plicNormalizeL1;
72using peclet::flow::vof::plicVolume;
74using peclet::flow::vof::vofIndexNormal;
75using peclet::flow::vof::VofMetric;
76using peclet::flow::vof::vofPhysNormal;
82 std::fprintf(stderr, "CHECK failed: %s\n at %s:%d\n", #cond, __FILE__, __LINE__); \
87std::size_t idx(
int x,
int y,
int z,
int nx,
int ny) {
88 return (std::size_t)x + (std::size_t)y * nx + (std::size_t)z * (std::size_t)nx * ny;
96 if (
const char* e = std::getenv(
"PECLET_P3C_AREA"))
97 s.setPhaseChangeArea(std::atoi(e));
109 if (
const char* e = std::getenv(
"PECLET_P3F_CARRY"))
110 s.setPhaseChangeCarryConserve(e[0] !=
'0');
111 if (
const char* e = std::getenv(
"PECLET_P3F_KAPPA"))
112 s.setPhaseChangeFitCurvature(std::atof(e));
117 if (
const char* e = std::getenv(
"PECLET_P3G_ORDER"))
118 s.setPhaseChangeEnergyOrder(std::atoi(e));
119 if (
const char* e = std::getenv(
"PECLET_P3G_OPMDOT"))
120 s.setPhaseChangeMdotOperator(e[0] !=
'0');
121 if (
const char* e = std::getenv(
"PECLET_P3G_GFMORDER"))
122 s.setPhaseChangeGfmOrder(std::atoi(e));
123 if (
const char* e = std::getenv(
"PECLET_P3G_CURVDIST"))
124 s.setPhaseChangeCurvatureDistance(e[0] !=
'0');
129 const double s2 = std::sqrt(2.0), hex = 3.0 * std::sqrt(3.0) / 4.0;
130 CHECK(
plicArea(1.0, 0.0, 0.0, plicAlpha(1.0, 0.0, 0.0, 0.5)) == 1.0);
131 CHECK(
plicArea(0.0, 0.0, -1.0, plicAlpha(0.0, 0.0, -1.0, 0.3)) == 1.0);
132 CHECK(std::fabs(
plicArea(0.5, 0.5, 0.0, plicAlpha(0.5, 0.5, 0.0, 0.5)) - s2) < 1e-15);
133 const double t = 1.0 / 3.0;
134 CHECK(std::fabs(
plicArea(t, t, t, plicAlpha(t, t, t, 0.5)) - hex) < 1e-15);
139 const double ms[][3] = {{1, 0, 0}, {0.5, 0.5, 0}, {0.02, 0.98, 0},
140 {
t,
t,
t}, {0.2, 0.3, 0.5}, {-0.2, 0.3, -0.5},
141 {1
e-9, 0.4, 0.6}, {0.1, 0.1, 0.8}, {0.45, 0.45, 0.1}};
143 for (
const auto& m : ms)
144 for (
double v = 0.02;
v < 0.999;
v += 0.02) {
145 const double al = plicAlpha(m[0], m[1], m[2], v);
146 const double q = std::sqrt(m[0] * m[0] + m[1] * m[1] + m[2] * m[2]);
147 const double d = 1
e-6;
149 q * (plicVolume(m[0], m[1], m[2], al + d) - plicVolume(m[0], m[1], m[2], al - d)) /
151 worst = std::fmax(worst, std::fabs(
plicArea(m[0], m[1], m[2], al) - ref));
153 std::printf(
"K1 plicArea: max |analytic - |m|2 dV/dalpha (FD)| = %.3e\n", worst);
177 const double ns[][3] = {{1, 0, 0}, {0, 0, -1}, {1, 1, 0}, {1, -1, 0},
178 {1, 1, 1}, {1, 2, 3}, {0.2, 0.3, 0.5}, {3, 1, -2},
179 {0.05, 1.0, 0.02}, {1, 0.5, 0.25}};
180 double worstPlic = 0.0, worstSplit = 0.0, worstPart = 0.0, worstColourAxis = 0.0;
181 int rows = 0, axisRows = 0;
182 for (
const auto& nn : ns) {
183 const double q = std::sqrt(nn[0] * nn[0] + nn[1] * nn[1] + nn[2] * nn[2]);
184 const double u[3] = {nn[0] /
q, nn[1] /
q, nn[2] /
q};
185 const bool axis = (std::fabs(u[0]) == 1.0 || std::fabs(u[1]) == 1.0 || std::fabs(u[2]) == 1.0);
186 for (
double off = -0.86;
off < 0.87;
off += 0.07) {
190 bool crossed =
false;
191 for (
int k = 0;
k < 8; ++
k) {
194 const double phi =
u[0] * (
p[0] - 0.5) + u[1] * (p[1] - 0.5) +
u[2] * (
p[2] - 0.5) - off;
201 if (k && ((v[k].psi < 0.0) != (v[0].psi < 0.0)))
207 double m[3] = {
u[0],
u[1],
u[2]};
208 const double l1 = plicNormalizeL1(m);
209 const double alpha = (0.5 * (
m[0] +
m[1] +
m[2])) + off / l1;
214 double sumC = 0.0, sumS = 0.0;
215 for (
int lc = 0; lc < 8; ++lc) {
219 worstPlic = std::fmax(worstPlic, std::fabs(sumC - exact) / exact);
220 worstSplit = std::fmax(worstSplit, std::fabs(sumS - sumC) / exact);
222 for (
int drop = 1;
drop < 8;
drop += 3) {
224 for (
int k = 0;
k < 8; ++
k) {
226 if ((k & drop) == drop && k != 0)
229 double sc = 0.0,
ss = 0.0;
230 for (
int lc = 0; lc < 8; ++lc) {
234 worstPart = std::fmax(worstPart, std::fabs(sc - sumC) / exact);
235 worstPart = std::fmax(worstPart, std::fabs(ss - sumC) / exact);
239 if (axis && std::fabs(off) < 0.4) {
245 for (
int k = 0;
k < 8; ++
k) {
248 const double phi =
u[0] * (
p[0] - 0.5) + u[1] * (p[1] - 0.5) +
u[2] * (
p[2] - 0.5) - off;
249 const double cc = std::fmin(1.0, std::fmax(0.0, 0.5 - phi));
253 for (
int lc = 0; lc < 8; ++lc)
255 worstColourAxis = std::fmax(worstColourAxis, std::fabs(sa - exact) / exact);
260 "K6 joined sheet on a PLANE (%d rows, %d axis-aligned): max rel |PLIC-source sum - "
261 "exact| %.3e, |split - centroid| %.3e, |retargeted - plain| %.3e, "
262 "|colour-source axis - exact| %.3e\n",
263 rows, axisRows, worstPlic, worstSplit, worstPart, worstColourAxis);
268 CHECK(worstPlic < 1e-13);
269 CHECK(worstSplit < 1e-13);
270 CHECK(worstPart < 1e-13);
271 CHECK(worstColourAxis == 0.0);
288 const double ms[][3] = {{1, 0, 0}, {0, -1, 0}, {0.5, 0.5, 0}, {0.02, 0.98, 0},
289 {0.2, 0.3, 0.5}, {-0.2, 0.3, -0.5}, {0.1, 0.1, 0.8}, {0.45, 0.45, 0.1}};
290 double worst1 = 0.0, worst2 = 0.0, worstMetric = 0.0, worstNrm = 0.0;
292 for (
const auto& m : ms) {
293 const double q = std::sqrt(m[0] * m[0] + m[1] * m[1] + m[2] * m[2]);
296 for (
int k = 1;
k < 3; ++
k)
297 if (std::fabs(m[k]) > std::fabs(m[d]))
299 const int d1 = (
d + 1) % 3, d2 = (d + 2) % 3;
300 for (
double v = 0.05;
v < 0.999;
v += 0.05) {
301 const double al = plicAlpha(m[0], m[1], m[2], v);
304 for (
int qq = 0; qq < 3; ++qq)
305 for (
int pp = 0; pp < 3; ++pp)
306 hh[pp + 3 * qq] = -(m[d1] * (pp - 1) + m[d2] * (qq - 1)) /
m[
d];
308 worstMetric = std::fmax(worstMetric, std::fabs(metric - q / std::fabs(m[d])));
311 const double dot = (
ns[0] *
m[0] +
ns[1] *
m[1] +
ns[2] *
m[2]) / q;
312 worstNrm = std::fmax(worstNrm, std::fabs(std::fabs(dot) - 1.0));
317 worst1 = std::fmax(worst1, std::fabs(a1 - a0) / a0);
318 worst2 = std::fmax(worst2, std::fabs(a2 - a0) / a0);
319 if (std::fabs(m[d]) == q) {
320 if (a1 == a0 && a2 == a0)
328 "K5 area modes on a PLANE: max rel |metric - 1| %.3e, |normal| %.3e, "
329 "mode1 vs mode0 %.3e, mode2 vs mode0 %.3e (%d axis-aligned rows BITWISE)\n",
330 worstMetric, worstNrm, worst1, worst2, exactAxis);
331 CHECK(worstMetric < 1e-15);
332 CHECK(worstNrm < 1e-15);
333 CHECK(worst1 < 1e-14);
334 CHECK(worst2 < 1e-14);
337 const double t1[3] = {0.0, 1.0, 0.0}, t2[3] = {0.0, 0.0, 1.0}, nn[3] = {1.0, 0.0, 0.0};
338 double a[6] = {0.1, 0.0, 0.0, 0.05, 0.0, 0.05},
ns[3];
340 CHECK(ns[0] == 1.0 && ns[1] == 0.0 && ns[2] == 0.0);
343 CHECK(std::fabs(ns[1] + 0.3 / std::sqrt(1.09)) < 1e-15);
349 double m[3] = {0.37, -0.51, 0.28};
350 double n[3] = {1, 0, 0};
352 const double alpha = plicAlpha(m[0], m[1], m[2], 0.4);
354 const double g[3] = {0.7, -1.3, 2.1};
355 const double Tsat = 3.5;
356 const double exact =
g[0] *
n[0] +
g[1] *
n[1] +
g[2] *
n[2];
359 for (
int dz = -2;
dz <= 2; ++
dz)
360 for (
int dy = -2;
dy <= 2; ++
dy)
361 for (
int dx = -2;
dx <= 2; ++
dx) {
362 if (!dx && !dy && !dz)
377 std::printf(
"K2 one-sided gradient fit: %.17g vs exact %.17g (n = %.4f %.4f %.4f, %d samples)\n",
378 fit, exact, n[0], n[1], n[2], fg.
n);
380 CHECK(std::fabs(fit - exact) < 1e-14);
388void quadraticGradientGate() {
389 double m[3] = {0.37, -0.51, 0.28};
390 double n[3] = {1, 0, 0};
392 const double alpha = plicAlpha(m[0], m[1], m[2], 0.4);
394 const double Tsat = 3.5,
g = 2.19,
q = -0.83;
396 for (
int dz = -2;
dz <= 2; ++
dz)
397 for (
int dy = -2;
dy <= 2; ++
dy)
398 for (
int dx = -2;
dx <= 2; ++
dx) {
399 if (!dx && !dy && !dz)
412 "K3 quadratic fit on a curved profile: quad %.17g vs exact %.17g (lin %.17g, "
415 CHECK(std::fabs(quad - g) < 1e-13);
416 CHECK(std::fabs(lin - g) > 0.1);
424 for (
double C : {0.15, 0.5, 0.87}) {
425 const double phic = 0.5 -
C, nd = -1.0;
428 std::printf(
"K4 GFM theta at C = %.2f: gas %.17g (exact %.17g), liquid %.17g (exact %.17g)\n",
429 C, thGas, 1.5 - C, thLiq, 0.5 + C);
430 CHECK(std::fabs(thGas - (1.5 - C)) < 1e-15);
431 CHECK(std::fabs(thLiq - (0.5 + C)) < 1e-15);
438 for (
double nd = 1e-3; nd > 1
e-14; nd *= 0.5) {
440 CHECK(std::fabs(th - prev) < 1e-3);
443 std::printf(
"K4 GFM theta continuity to n_d -> 0: limit %.17g (thetaMax 1.9)\n", prev);
469void gfmSecondOrderGate() {
470 const double a = 0.83,
b = -1.47,
c = 0.62;
471 double worstLap = 0.0, worstFlux = 0.0, worstLin = 0.0, worst1 = 0.0;
472 std::printf(
"K5 second-order GFM row (T = %.2f %+.2f x %+.2f x^2, exact T'' = %.4f):\n", a, b, c,
474 for (
int i = 1;
i <= 19; ++
i) {
475 const double th = 0.05 *
i;
476 const double Tg =
a +
b * th +
c * th * th, T0 =
a, Tm =
a -
b +
c;
480 const double lap = r2.aGamma * (
Tg - T0) + r2.aBehind * (Tm - T0);
482 const double q2 = r2.aGamma * (T0 -
Tg) + (r2.aBehind - 1.0) * (T0 - Tm);
483 const double q1 =
r1.aGamma * (T0 -
Tg) + (
r1.aBehind - 1.0) * (T0 - Tm);
485 const double qlin = r2.aGamma * (0.0 -
b * th) + (r2.aBehind - 1.0) * (0.0 - (-
b));
486 worstLap = std::fmax(worstLap, std::fabs(lap - 2 * c));
487 worstFlux = std::fmax(worstFlux, std::fabs(q2 + (b + c)));
488 worstLin = std::fmax(worstLin, std::fabs(qlin + b));
489 worst1 = std::fmax(worst1, std::fabs(q1 + (b + c)));
492 " theta %.2f a_G %8.4f a_B %6.4f | T'' %.17g (err %.2e) Q %.17g "
493 "(err %.2e) | order-1 Q err %+.3e\n",
494 th, r2.aGamma, r2.aBehind, lap, std::fabs(lap - 2 * c), q2, std::fabs(q2 + (b + c)),
498 "K5 worst over theta in [0.05, 0.95]: |T''_row - T''| %.3e, |Q - Q_exact| %.3e "
499 "(linear profile %.3e); the SHIPPED two-point row: %.3e\n",
500 worstLap, worstFlux, worstLin, worst1);
501 CHECK(worstLap < 1e-14);
502 CHECK(worstFlux < 1e-14);
503 CHECK(worstLin < 1e-14);
507 CHECK(
r.aGamma == 1.0 &&
r.aBehind == 1.0);
510 for (
double th : {0.1, 0.5, 1.3, 1.9}) {
512 CHECK(
rf.aGamma == 1.0 / th &&
rf.aBehind == 1.0);
516 for (
double nd : {-1.0, -0.8, -0.5, 0.6, 0.9}) {
519 const double kap = -2.0 / 12.0;
520 const double want = std::fabs(0.3 - 1.0 * nd + 0.25 * kap * (1.0 - nd * nd)) / std::fabs(nd);
522 CHECK(std::fabs(got - std::fmin(std::fmax(want, 0.1), 1.9)) < 1e-15);
524 std::printf(
"K5 curvature-consistent theta: inert at kappa = 0, closed form otherwise\n");
529 const int nx = 64,
ny = 4, nz = 4;
530 const double x0 = 32.25, mdot = 0.02,
dt = 1.0;
532 s.setAdvection(
false);
536 std::vector<double>
C((std::size_t)nx * ny * nz, 0.0);
537 for (
int z = 0;
z < nz; ++
z)
538 for (
int y = 0;
y <
ny; ++
y)
539 for (
int x = 0;
x < nx; ++
x)
540 C[idx(x, y, z, nx, ny)] = std::fmin(1.0, std::fmax(0.0, x0 - x));
543 s.enablePhaseChange(1.0, 1.0, 1.0);
546 s.setMassFluxUniform(mdot);
548 const auto c =
s.getVof();
552 return sum / (
ny * nz);
554 double worst = 0.0,
lo = 1e30, hi = -1e30;
557 for (
int k = 1;
k <= 1000; ++
k) {
558 s.applyPhaseChange(dt);
559 worst = std::fmax(worst, std::fabs(
pos() - (x0 - mdot * dt * k)));
560 const auto d =
s.phaseChangeDiagnostics();
561 lo = std::fmin(lo,
d.minC);
562 hi = std::fmax(hi,
d.maxC);
563 clips +=
d.deficitCells;
564 redist +=
d.redistributed;
567 "P0a planar regression, 1000 steps: max |x - (x0 - mdot t/rho_l)| = %.3e ; C in [%.17g, "
568 "%.17g] ; %ld cell-crossings clipped, |redistributed| = %.6g\n",
569 worst, lo, hi, clips, redist);
570 CHECK(worst < 1e-12);
578 const int nx = 64,
ny = 4, nz = 4;
579 const double rg = 1.0, rl = 100.0, mdot = 0.01,
x0 = 32.25;
581 s.setAdvection(
false);
585 s.setDomainBc(0, 1, 0, 0, 0);
586 s.setDomainBc(1, 1, 0, 0, 0);
587 s.setPressureGeometry(std::vector<double>((std::size_t)nx * ny * nz, 1.0));
588 std::vector<double>
C((std::size_t)nx * ny * nz, 0.0);
589 for (
int z = 0;
z < nz; ++
z)
590 for (
int y = 0;
y <
ny; ++
y)
591 for (
int x = 0;
x < nx; ++
x)
592 C[idx(x, y, z, nx, ny)] = std::fmin(1.0, std::fmax(0.0, x0 - x));
596 s.enablePhaseChange(rg, rl, 1.0);
599 s.setMassFluxUniform(mdot);
600 const double Sc = mdot * (1.0 / rg - 1.0 / rl);
601 std::vector<double> sink((std::size_t)nx * ny * nz, 0.0);
602 for (
int z = 0;
z < nz; ++
z)
603 for (
int y = 0;
y <
ny; ++
y)
604 sink[idx(nx - 6, y, z, nx, ny)] = -Sc;
605 s.setDivergenceSource(sink);
606 s.setPressureFcg(
true, 400, 1e-12);
607 for (
int k = 0;
k < 20; ++
k)
609 const auto u =
s.getVelocity(0);
610 const auto src =
s.getField(
"pc_source");
611 const auto usr =
s.getField(
"div_source");
612 const auto v =
s.getVelocity(1),
w =
s.getVelocity(2);
613 const double ug = mdot * (1.0 / rg - 1.0 / rl);
614 double liq = 0.0, plat = 0.0, spread = 0.0, dmax = 0.0;
615 double p0 =
u[idx(40, 0, 0, nx, ny)];
616 for (
int x = 2;
x < 30; ++
x)
617 liq = std::fmax(liq, std::fabs(u[idx(x, 0, 0, nx, ny)]));
618 for (
int x = 35;
x < 56; ++
x) {
619 plat +=
u[idx(x, 0, 0, nx, ny)] / 21.0;
620 spread = std::fmax(spread, std::fabs(u[idx(x, 0, 0, nx, ny)] - p0));
622 for (
int z = 0;
z + 1 < nz; ++
z)
623 for (
int y = 0;
y + 1 <
ny; ++
y)
624 for (
int x = 0;
x + 1 < nx; ++
x) {
625 const double d = (
u[idx(x + 1, y, z, nx, ny)] -
u[idx(x, y, z, nx, ny)]) +
626 (v[idx(x, y + 1, z, nx, ny)] - v[idx(x, y, z, nx, ny)]) +
627 (
w[idx(x, y, z + 1, nx, ny)] -
w[idx(x, y, z, nx, ny)]);
629 std::fmax(dmax, std::fabs(d - src[idx(x, y, z, nx, ny)] - usr[idx(x, y, z, nx, ny)]));
631 const auto d =
s.phaseChangeDiagnostics();
633 "P0b ratio 100, closed column + balanced sink: u_gas = %.17g (exact %.17g, rel %.3e), "
634 "plateau spread %.3e, max|u_liquid| %.3e, max|div - S| %.3e, source sum %.6g into %ld "
635 "cells, pressure iters %d\n",
636 plat, ug, (plat - ug) / ug, spread, liq, dmax,
d.sourceSum,
d.sourceCells,
637 s.lastPressureIterations());
638 CHECK(std::fabs((plat - ug) / ug) < 1e-10);
639 CHECK(liq < 1e-12 * ug + 1e-15);
641 CHECK(
s.lastPressureIterations() < 400);
643 CHECK(std::fabs(u[idx(32, 0, 0, nx, ny)]) < 1e-15);
644 CHECK(std::fabs(u[idx(33, 0, 0, nx, ny)]) < 1e-15);
649 const double rhs = St / std::sqrt(M_PI);
650 double a = 1
e-8,
b = 5.0;
651 for (
int i = 0;
i < 200; ++
i) {
652 const double m = 0.5 * (
a +
b);
653 ((
m * std::exp(m * m) * std::erf(m) - rhs) > 0 ?
b :
a) = m;
655 return 0.5 * (
a +
b);
658double stefanRun(
int N,
double& exact) {
659 const double St = 1.0,
alpha = 1.0, x0p = 0.10, xep = 0.25, Fo = 0.5;
661 const double t0 = (x0p / (2 *
lam)) * (x0p / (2 * lam)) /
alpha;
662 const double te = (xep / (2 *
lam)) * (xep / (2 * lam)) /
alpha;
663 const int ny = 4, nz = 4;
665 int ns = (
int)std::llround((te - t0) / (Fo / D));
666 const double dt = (te -
t0) / ns;
668 s.setAdvection(
false);
672 s.setDomainBc(0, 1, 0, 0, 0);
673 s.setDomainBc(1, 1, 0, 0, 0);
674 s.setPressureGeometry(std::vector<double>((std::size_t)
N * ny * nz, 1.0));
675 const double xg = x0p *
N;
676 std::vector<double>
C((std::size_t)
N * ny * nz, 0.0),
T((std::size_t)
N * ny * nz, 0.0);
677 for (
int z = 0;
z < nz; ++
z)
678 for (
int y = 0;
y <
ny; ++
y)
679 for (
int x = 0;
x <
N; ++
x) {
680 C[idx(x, y, z,
N, ny)] = std::fmin(1.0, std::fmax(0.0, (x + 1) - xg));
681 const double xp = (
x + 0.5) /
N;
682 T[idx(x, y, z,
N, ny)] = xp < x0p ? 1.0 - std::erf(lam * xp / x0p) / std::erf(lam) : 0.0;
686 s.addScalar(
"T", D, 1, 60);
687 s.setScalarBc(
"T", 0, 2, 1.0);
688 s.setScalarBc(
"T", 1, 2, 0.0);
690 s.enablePhaseChange(1.0, 1.0, 1.0);
693 s.setPhaseChangeThermal(
"T", 0.0, D, D, 0.0);
697 s.setPhaseChangePlaneDirichlet(
false);
698 s.setPhaseChangeQuadraticFit(
false);
699 for (
int k = 0;
k <
ns; ++
k) {
700 s.applyPhaseChange(dt);
703 const auto c =
s.getVof();
707 exact = 2 *
lam * std::sqrt(alpha * te) *
N;
708 const auto d =
s.phaseChangeDiagnostics();
710 "P1 Stefan N=%d (%d steps, St=1, lambda=%.6f): layer = %.5f cells, exact %.5f, rel %+.4f %% "
711 "; C in [%.3e, %.17g], unresolved %.3e, fallback %ld\n",
712 N, ns, lam,
N - sum / (ny * nz), exact, 100.0 * ((
N - sum / (ny * nz)) - exact) / exact,
713 d.minC,
d.maxC,
d.unresolved,
d.fallbackCells);
716 return N - sum / (
ny * nz);
721 const double x = stefanRun(64, exact);
722 CHECK(std::fabs(x - exact) / exact < 0.02);
730double stefanRunP23(
int N,
double& exact) {
731 const double St = 1.0,
alpha = 1.0, x0p = 0.10, xep = 0.25, Fo = 0.5;
733 const double t0 = (x0p / (2 *
lam)) * (x0p / (2 * lam)) /
alpha;
734 const double te = (xep / (2 *
lam)) * (xep / (2 * lam)) /
alpha;
735 const int ny = 4, nz = 4;
737 int ns = (
int)std::llround((te - t0) / (Fo / D));
738 const double dt = (te -
t0) / ns;
740 s.setAdvection(
false);
744 s.setDomainBc(0, 1, 0, 0, 0);
745 s.setDomainBc(1, 1, 0, 0, 0);
746 s.setPressureGeometry(std::vector<double>((std::size_t)
N * ny * nz, 1.0));
747 const double xg = x0p *
N;
748 std::vector<double>
C((std::size_t)
N * ny * nz, 0.0),
T((std::size_t)
N * ny * nz, 0.0);
749 for (
int z = 0;
z < nz; ++
z)
750 for (
int y = 0;
y <
ny; ++
y)
751 for (
int x = 0;
x <
N; ++
x) {
752 C[idx(x, y, z,
N, ny)] = std::fmin(1.0, std::fmax(0.0, (x + 1) - xg));
753 const double xp = (
x + 0.5) /
N;
754 T[idx(x, y, z,
N, ny)] = xp < x0p ? 1.0 - std::erf(lam * xp / x0p) / std::erf(lam) : 0.0;
758 s.addScalar(
"T", D, 1, 60);
759 s.setScalarBc(
"T", 0, 2, 1.0);
760 s.setScalarBc(
"T", 1, 2, 0.0);
762 s.enablePhaseChange(1.0, 1.0, 1.0);
765 s.setPhaseChangeThermal(
"T", 0.0, D, D, 0.0);
766 s.setPhaseChangePlaneDirichlet(
true);
767 s.setPhaseChangeQuadraticFit(
true);
768 for (
int k = 0;
k <
ns; ++
k) {
769 s.applyPhaseChange(dt);
772 const auto c =
s.getVof();
776 exact = 2 *
lam * std::sqrt(alpha * te) *
N;
777 const double layer =
N - sum / (
ny * nz);
778 const auto d =
s.phaseChangeDiagnostics();
780 "P1' Stefan N=%d, PLANE-ANCHORED Dirichlet + QUADRATIC fit: layer = %.5f cells, "
781 "exact %.5f, rel %+.4f %% ; C in [%.3e, %.17g]\n",
782 N, layer, exact, 100.0 * (layer - exact) / exact,
d.minC,
d.maxC);
790 const double x = stefanRunP23(64, exact);
791 CHECK(std::fabs(x - exact) / exact < 0.001);
800void energyIdentity(
double rcpRatio) {
801 const int nx = 32,
ny = 8, nz = 8;
802 const double T0 = 3.25,
U = 0.17;
803 std::vector<double>
C((std::size_t)nx * ny * nz, 0.0),
T((std::size_t)nx * ny * nz, T0);
804 for (
int z = 0;
z < nz; ++
z)
805 for (
int y = 0;
y <
ny; ++
y)
806 for (
int x = 0;
x < nx; ++
x)
807 C[idx(x, y, z, nx, ny)] =
808 std::fmin(1.0, std::fmax(0.0, 0.37 * x + 0.21 * y - 0.13 * z - 3.4));
810 s.setAdvection(
false);
814 s.setPressureGeometry(std::vector<double>((std::size_t)nx * ny * nz, 1.0));
817 s.addScalar(
"T", 0.0, 1, 1);
819 s.setVelocity(0, std::vector<double>((std::size_t)nx * ny * nz,
U));
820 s.enablePhaseChange(1.0, 1.0, 1.0);
823 s.setPhaseChangeThermal(
"T", T0, 1.0, 1.0, 0.0);
824 s.setPhaseChangeEnergy(1.0, rcpRatio);
826 for (
int k = 0;
k < 20; ++
k) {
827 s.advectVofKinematic(1.0);
828 const auto t =
s.getField(
"T");
830 worst = std::fmax(worst, std::fabs(q - T0));
833 "ENERGY uniform-T identity at rho c_p ratio %g: max |T - T0| = %.3e over 20 "
841double suckingB(
double ja,
double rr) {
842 double b = ja / std::sqrt(M_PI);
843 for (
int i = 0;
i < 300; ++
i) {
844 const double g =
b * rr;
845 b = ja * std::exp(-g * g) / (std::sqrt(M_PI) * std::erfc(g));
856 const double ratio = 10.0, ja = 1.0, alpha_l = 1.0, x0p = 0.10, xep = 0.25, Fo = 0.5, cfl = 0.2;
857 const double rr = 1.0 /
ratio,
b = suckingB(ja, rr);
858 const double t0 = (x0p / (2 *
b)) * (x0p / (2 * b)) / alpha_l;
859 const double te = (xep / (2 *
b)) * (xep / (2 * b)) / alpha_l;
860 const int ny = 4, nz = 4;
861 const double al = alpha_l *
N *
N, rho_l = 1.0, rho_v = rr, cpl = 1.0;
862 const double k_l = al * rho_l * cpl, k_v = k_l /
ratio, dT = 1.0;
863 const double h_lv = rho_l * cpl * dT / (rho_v * ja);
865 s.setAdvection(
false);
868 s.setDomainBc(0, 1, 0, 0, 0);
869 s.setDomainBc(1, 3, 0, 0, 0);
870 s.setPressureGeometry(std::vector<double>((std::size_t)
N * ny * nz, 1.0));
871 const double X0 = x0p *
N;
872 std::vector<double>
C((std::size_t)
N * ny * nz, 0.0),
T((std::size_t)
N * ny * nz, 0.0);
873 for (
int z = 0;
z < nz; ++
z)
874 for (
int y = 0;
y <
ny; ++
y)
875 for (
int x = 0;
x <
N; ++
x) {
876 C[idx(x, y, z,
N, ny)] = std::fmin(1.0, std::fmax(0.0, (x + 1) -
X0));
877 const double xp = (
x + 0.5) /
N;
879 const double sv = xp / (2 * std::sqrt(alpha_l * t0)) - b * (1.0 - rr);
880 T[idx(x, y, z,
N, ny)] = dT - dT * std::erfc(
sv) / std::erfc(b * rr);
886 s.setPressureFcg(
true, 4000, 1e-10);
887 s.addScalar(
"T", al, 1, 60);
888 s.setScalarBc(
"T", 0, 2, 0.0);
889 auto farT = [&](
double tt) {
890 const double sv = 1.0 / (2 * std::sqrt(alpha_l * tt)) - b * (1.0 - rr);
891 return dT - dT * std::erfc(
sv) / std::erfc(b * rr);
893 s.setScalarBc(
"T", 1, 2, farT(t0));
895 s.enablePhaseChange(rho_v, rho_l, h_lv);
898 s.setPhaseChangeThermal(
"T", 0.0, k_v, k_l, 0.0);
899 s.setPhaseChangeEnergy(rho_v * cpl, rho_l * cpl);
904 const double Xd =
b * std::sqrt(alpha_l / tcur) *
N;
905 double dt = std::fmin(cfl / std::fmax(Xd * (1.0 - rr), 1e-30), Fo / al);
906 dt = std::fmin(dt, te - tcur);
908 s.setScalarBc(
"T", 1, 2, farT(tcur + dt));
912 itmax = std::max<long>(itmax,
s.lastPressureIterations());
913 capped +=
s.lastPressureIterations() >= 4000 ? 1 : 0;
915 const auto c =
s.getVof();
919 const double layer =
N - sum / (
ny * nz);
920 const double exact = 2 *
b * std::sqrt(alpha_l * te) *
N;
921 const auto d =
s.phaseChangeDiagnostics();
923 "P2 sucking interface N=%d (%d steps, ratio %g, Ja %g, b = %.6f): layer = %.5f "
924 "cells, exact %.5f, rel %+.4f %% ; pressure iters max %d/4000 (capped %d), "
925 "band_div %.3e, C in [%.3e, %.17g]\n",
926 N, ns, ratio, ja, b, layer, exact, 100.0 * (layer - exact) / exact, (
int)itmax, capped,
927 d.bandDiv,
d.minC,
d.maxC);
929 CHECK(std::fabs(layer - exact) / exact < 0.01);
934 const int nx = 32,
ny = 4, nz = 4;
935 std::vector<double>
C((std::size_t)nx * ny * nz, 0.0);
936 for (
int z = 0;
z < nz; ++
z)
937 for (
int y = 0;
y <
ny; ++
y)
938 for (
int x = 0;
x < nx; ++
x)
939 C[idx(x, y, z, nx, ny)] = std::fmin(1.0, std::fmax(0.0, 16.4 - x));
941 a.setAdvection(
false);
942 b.setAdvection(
false);
949 b.enablePhaseChange(1.0, 1.0, 1.0);
951 b.setMassFluxUniform(0.0);
952 for (
int k = 0;
k < 10; ++
k)
953 b.applyPhaseChange(1.0);
954 const auto ca =
a.getVof(), cb =
b.getVof();
956 for (std::size_t i = 0;
i <
ca.size(); ++
i)
957 diff = std::fmax(diff, std::fabs(ca[i] - cb[i]));
958 std::printf(
"INERT mdot == 0: max |C_pc - C_ref| = %.3e (bitwise expected)\n", diff);
973void anisoAreaAndGradient() {
974 std::printf(
"\nK4/K5 (Phase 3): anisotropic area and one-sided gradient\n");
975 const double hs[3][3] = {{1.0, 1.0, 1.0}, {1.0, 1.0, 2.0}, {2.0, 1.5, 4.0}};
976 const double ns[4][3] = {
977 {1.0, 0.0, 0.0}, {0.6, -0.8, 0.0}, {0.4243, -0.7071, 0.5657}, {0.5774, 0.5774, 0.5774}};
981 long bitA = 0, totA = 0;
982 for (
int q = 0;
q < 3; ++
q) {
983 const VofMetric
g{{
hs[
q][0],
hs[
q][1],
hs[
q][2]}};
984 for (
int p = 0;
p < 4; ++
p) {
985 const double*
n =
ns[
p];
988 vofIndexNormal(n, g, m);
989 const double alpha = 0.5 * (
m[0] +
m[1] +
m[2]);
995 for (
int k = 1;
k < 3; ++
k)
996 if (std::fabs(n[k]) > std::fabs(n[d]))
998 const int t1 = (
d + 1) % 3, t2 = (d + 2) % 3;
999 const int sub = 2000;
1000 const double w1 =
g.h[t1] /
sub, w2 =
g.h[t2] /
sub;
1002 for (
int b = 0;
b <
sub; ++
b)
1003 for (
int a = 0;
a <
sub; ++
a) {
1005 x[t1] = (
a + 0.5) * w1;
1006 x[t2] = (
b + 0.5) * w2;
1008 const double dPlane = 0.5 * (
n[0] *
g.h[0] +
n[1] *
g.h[1] +
n[2] *
g.h[2]);
1009 x[
d] = (dPlane -
n[t1] *
x[t1] -
n[t2] *
x[t2]) / n[d];
1010 if (x[d] >= 0.0 && x[d] <=
g.h[d])
1013 const double proj = (double)inside * w1 * w2;
1014 const double ref = proj / std::fabs(n[d]);
1015 const double rel = std::fabs(got - ref) / std::fmax(ref, 1e-12);
1016 worstA = std::fmax(worstA, rel);
1020 if (got ==
plicArea(m[0], m[1], m[2], alpha))
1026 " K4a plicAreaMetric vs a 2000^2 footprint quadrature: max rel %.3e (gate 2e-3,\n"
1027 " the quadrature's own 1/sub floor); isotropic == plicArea bitwise %ld/%ld\n",
1028 worstA, bitA, totA);
1029 CHECK(worstA < 2e-3);
1030 CHECK(bitA == totA);
1033 double worstMc = 0.0;
1034 long bitMc = 0, totMc = 0;
1035 for (
int q = 0;
q < 3; ++
q) {
1036 const VofMetric
g{{
hs[
q][0],
hs[
q][1],
hs[
q][2]}};
1037 for (
int p = 0;
p < 4; ++
p) {
1038 const double*
n =
ns[
p];
1040 vofIndexNormal(n, g, m);
1041 const double qn = std::sqrt(m[0] * m[0] + m[1] * m[1] + m[2] * m[2]);
1042 const double mu[3] = {
m[0] / qn,
m[1] / qn,
m[2] / qn};
1043 for (
double off = -0.3;
off <= 0.31;
off += 0.15) {
1045 bool crossed =
false;
1046 for (
int k = 0;
k < 8; ++
k) {
1053 mu[0] * (pc[0] - 0.5) + mu[1] * (pc[1] - 0.5) + mu[2] * (pc[2] - 0.5) - off;
1060 if (k && ((v[k].psi < 0.0) != (v[0].psi < 0.0)))
1066 const double alpha = 0.5 * (mu[0] + mu[1] + mu[2]) + off;
1068 if (!(exact > 1e-6))
1070 double sum = 0.0, sumIso = 0.0;
1071 for (
int lc = 0; lc < 8; ++lc) {
1075 worstMc = std::fmax(worstMc, std::fabs(sum - exact) / exact);
1085 " K4b joined sheet on a stretched plane: max rel |sum - exact| %.3e (gate 1e-13);"
1086 "\n isotropic == the unit-metric overload bitwise %ld/%ld\n",
1087 worstMc, bitMc, totMc);
1088 CHECK(worstMc < 1e-13);
1089 CHECK(bitMc == totMc);
1097 double worstPhys = 0.0, worstIdx = 0.0;
1098 for (
int q = 1;
q < 3; ++
q) {
1099 const VofMetric
g{{
hs[
q][0],
hs[
q][1],
hs[
q][2]}};
1100 for (
int p = 0;
p < 4; ++
p) {
1101 const double*
n =
ns[
p];
1103 vofIndexNormal(n, g, m);
1105 const double sMet = vofPhysNormal(m, g, np);
1107 const double alpha = 0.5 * (
m[0] +
m[1] +
m[2]);
1109 const double Gtrue = 3.7,
Tg = 1.5;
1111 for (
int dz = -2;
dz <= 2; ++
dz)
1112 for (
int dy = -2;
dy <= 2; ++
dy)
1113 for (
int dx = -2;
dx <= 2; ++
dx) {
1114 if (dx == 0 && dy == 0 && dz == 0)
1116 const double di[3] = {(double)dx, (
double)
dy, (double)dz};
1122 const double T =
Tg + Gtrue * phiP;
1123 const double wP =
pcGradWeight(dp[0], dp[1], dp[2], np);
1128 (
double)dx, (
double)dy, (
double)dz);
1134 worstPhys = std::fmax(worstPhys, std::fabs(
pcGradSolve(fp) - Gtrue) / Gtrue);
1135 worstIdx = std::fmax(worstIdx, std::fabs(
pcGradSolve(fi) - Gtrue) / Gtrue);
1140 " K5 one-sided dT/dn on a linear profile: PHYSICAL offsets rel err %.3e"
1141 " (gate 1e-12)\n the same fit on INDEX offsets: %.3e <- what the metric"
1143 worstPhys, worstIdx);
1144 CHECK(worstPhys < 1e-12);
1145 CHECK(worstIdx > 1e-2);
1151 Kokkos::initialize(argc, argv);
1157 quadraticGradientGate();
1159 gfmSecondOrderGate();
1160 anisoAreaAndGradient();
1165 energyIdentity(1e4);
1171 std::fprintf(stderr,
"%d CHECK(s) failed\n",
failures);
1174 std::printf(
"all phase-change gates passed\n");
flow — host-facing Kokkos IBM Navier-Stokes solver (drop-in flow-style API).
double pcGradSolve2(const PcGradFit &f)
The QUADRATIC one-sided fit (WO-P23): T - T_G = G phi + Q phi^2, returning G.
void pcGradAdd(PcGradFit &f, double w, double phi, double T, double Tg)
Add one pure-phase sample at normal distance phi with temperature T.
double pcCentreDistance(double mx, double my, double mz, double alpha)
Signed distance (in cells) from the CELL CENTRE to the PLIC plane, measured along n and POSITIVE ON T...
double pcGfmTheta(double phiC, double nd, double s, double thetaMin, double thetaMax)
thetaMin keeps the coefficient finite when the plane grazes the cell centre (the standard GFM clamp);...
@ kMcDepositSplit
clipped to each cell's cube, each cell gets its own piece
@ kMcDepositCentroid
the whole triangle to the cell holding its centroid
PcGfmRow pcGfmRow(double theta, bool behind, int order)
double hfAreaElement(const double h[9])
The height function's area element sqrt(1 + h_x^2 + h_y^2), with the same central differences hfPatch...
double pcOffsetDistance(double phic, const double n[3], double dx, double dy, double dz)
Signed distance from the plane of the neighbour at INTEGER cell offset d, given the centre distance p...
double plicArea(double mx, double my, double mz, double alpha)
Area of the PLIC polygon {x in [0,1]^3 : m.x = alpha}, as a multiple of h^2.
double pcGfmThetaK(double phiC, double nd, double s, double kappa, double thetaMin, double thetaMax)
WO-P3g item 3 — the curvature-consistent GFM distance.
@ kAreaNormal
variant B: plicArea rebuilt on the cascade's normal.
@ kAreaMetric
variant A: the PLIC footprint x the cascade's slope. DEFAULT.
@ kAreaPlic
rung P0/P1: plicArea on the MYC normal. The old numbers.
@ kMcSrcColour
the C = 1/2 level set (the WO's design)
@ kMcSrcPlic
the zero of the PLIC-reconstructed signed distance (the smooth variant)
double plicAreaMetric(double mx, double my, double mz, double alpha, const VofMetric &g)
The PHYSICAL area of the PLIC polygon of an ANISOTROPIC cell, in hRef^2 (flow/doc/anisotropic_vof....
void pvSurfaceNormal(const double a[6], const double t1[3], const double t2[3], const double nn[3], double m[3])
The unit normal of the fitted paraboloid z = a0 + a1 x + a2 y + ... AT THE ORIGIN of its own frame (t...
double interfaceAreaFromNormal(int mode, double mx, double my, double mz, double alpha, double c, const double ns[3], const VofMetric &g)
The projection axis of kAreaMetric is the one ns is most aligned with — the column direction on tiers...
void mcCornerPos(int k, double p[3])
Local coordinates of dual-cube corner k (bit 0 = x, bit 1 = y, bit 2 = z).
void hfSurfaceNormal(const double h[9], int d, double m[3])
The unit surface normal implied by the SAME 3x3 patch of heights, in world components.
double pcGradSolve(const PcGradFit &f)
The fitted normal derivative dT/dn (0 if the side carried no usable sample).
double pcUnitNormal(double mx, double my, double mz, double n[3])
Unit (L2) interface normal from a PLIC normal of any scale.
double mcCubeCornerArea(const McVertex v[8], int lc, int src, int deposit, const VofMetric &g)
The interfacial area inside one dual cube that belongs to its local corner lc.
double pcGradWeight(double dx, double dy, double dz, const double n[3])
Malan's collinearity weight for a sample at cell offset d: w = xi / |d| with xi = (d....
Solver< Staggered > IbmSolver
fit(a, t, t0=40.0, t1=160.0)
One dual-cube corner = one cell centre.
Accumulator for dT/dn fitted through the interface value: T - T_G = G * phi, so G = sum_j w_j phi_j (...
static constexpr double U
static double stefanLambda(double St)
void applyAreaModeEnv(S &s)