69#include <Kokkos_Core.hpp>
80 std::fprintf(stderr, "CHECK failed: %s\n at %s:%d\n", #cond, __FILE__, __LINE__); \
85void checkClose(
double a,
double b,
double tol,
const char* what) {
86 if (!(std::fabs(a - b) <= tol)) {
87 std::fprintf(stderr,
"CHECK_CLOSE failed (%s): %.17g vs %.17g (|d| = %.3e > %.3e)\n", what, a,
88 b, std::fabs(a - b), tol);
93bool bitwiseEqual(
const std::vector<double>& a,
const std::vector<double>& b) {
94 if (
a.size() !=
b.size())
96 return std::memcmp(
a.data(),
b.data(),
a.size() *
sizeof(
double)) == 0;
99double maxAbs(
const std::vector<double>& a) {
102 if (!(std::fabs(v) <= m))
109double relDiffScaled(
const std::vector<double>& a,
const std::vector<double>& b,
double k) {
112 for (std::size_t i = 0;
i <
a.size(); ++
i) {
113 const double d = std::fabs(a[i] - k * b[i]);
126 std::vector<double> u, v, w, p;
127 std::array<double, 3> spacing{};
133Fields runChannel(
int nx,
int ny,
int nz,
double lam,
bool arm) {
135 const double H = 1.0 /
lam;
136 const double ylo = (std::round(0.30 * ny) + 0.5) * H, yhi = (std::round(0.70 * ny) + 0.5) *
H;
138 s.setAdvection(
false);
140 s.setPhysicalDomain({nx *
H,
ny *
H, nz *
H}, {0.0, 0.0, 0.0}, {nx,
ny, nz});
144 s.setBodyForce(
F, 0.0, 0.0);
145 s.setVelocityIterations(400);
146 s.setVelocityResidualTolerance(1e-14);
147 s.setPressurePcg(
true, 80, 1e-13);
154 const std::vector<double> cx =
s.cellCentres(0), cy =
s.cellCentres(1), cz =
s.cellCentres(2);
155 std::vector<double>
sdf((std::size_t)nx * ny * nz);
156 for (
int z = 0;
z < nz; ++
z)
157 for (
int y = 0;
y <
ny; ++
y)
158 for (
int x = 0;
x < nx; ++
x)
159 sdf[(std::size_t)
x + (std::size_t)y * nx + (std::size_t)
z * nx *
ny] =
160 std::min(cy[y] - ylo, yhi - cy[y]);
163 s.setSolid(sdf,
false);
164 for (
int it = 0;
it < 300; ++
it)
167 f.u =
s.getVelocity(0);
168 f.v =
s.getVelocity(1);
169 f.w =
s.getVelocity(2);
170 f.p =
s.getPressure();
171 f.spacing =
s.spacing();
178Fields runSphere(
int N,
double lam,
bool arm,
bool scene) {
180 const double H = 1.0 /
lam;
181 const double R = 0.30 *
N *
H;
184 s.setPhysicalDomain({
N *
H,
N *
H,
N *
H}, {0.0, 0.0, 0.0}, {
N,
N,
N});
188 s.setBodyForce(
F, 0.0, 0.0);
189 s.setAdvection(
false);
190 s.setVelocityIterations(200);
191 s.setVelocityResidualTolerance(1e-14);
192 s.setPressurePcg(
true, 120, 1e-13);
199 namespace g = peclet::core::geom;
200 g::SceneBuilder<double>
b;
201 b.addLeaf(g::kSphere, {
R});
202 g::Transform<double>
tr;
204 tr.translation = peclet::core::Vec3<double>{0.5 *
N *
H, 0.5 *
N *
H, 0.5 *
N *
H};
205 b.addInstance(0, tr);
206 std::vector<int> ni, ii;
207 std::vector<double> nr, ir;
208 b.encode(ni, nr, ii, ir);
209 s.setScene(ni, nr, ii, ir,
true);
210 s.setSolidFromScene(
true);
212 const std::vector<double> cx =
s.cellCentres(0), cy =
s.cellCentres(1), cz =
s.cellCentres(2);
213 const double c = 0.5 *
N *
H;
214 std::vector<double>
sdf((std::size_t)
N *
N *
N);
215 for (
int z = 0;
z <
N; ++
z)
216 for (
int y = 0;
y <
N; ++
y)
217 for (
int x = 0;
x <
N; ++
x)
218 sdf[(std::size_t)
x + (std::size_t)y *
N + (std::size_t)
z *
N *
N] =
219 std::sqrt((cx[x] - c) * (cx[x] - c) + (cy[y] - c) * (cy[y] - c) +
220 (cz[z] - c) * (cz[z] - c)) -
222 s.setSolid(sdf,
true);
224 for (
int it = 0;
it < 60; ++
it)
227 f.u =
s.getVelocity(0);
228 f.v =
s.getVelocity(1);
229 f.w =
s.getVelocity(2);
230 f.p =
s.getPressure();
231 f.spacing =
s.spacing();
232 f.div =
s.maxOpenDivergence();
240std::vector<double> sphereColour(
int n,
double R,
double cx,
double cy,
double cz,
int sub = 24) {
241 std::vector<double>
C((std::size_t)n * n * n, 0.0);
242 const double w = 1.0 /
sub;
243 for (
int k = 0;
k <
n; ++
k)
244 for (
int j = 0;
j <
n; ++
j)
245 for (
int i = 0;
i <
n; ++
i) {
247 for (
int b = 0;
b <
sub; ++
b)
248 for (
int a = 0;
a <
sub; ++
a) {
249 const double px =
i + (
a + 0.5) * w, py = j + (b + 0.5) *
w;
250 const double r2 =
R *
R - (px - cx) * (px - cx) - (py - cy) * (py - cy);
253 const double hh = std::sqrt(r2);
254 const double lo = std::fmax(cz - hh, (
double)k), hi = std::fmin(cz + hh, (
double)k + 1);
258 C[(std::size_t)i + (std::size_t)
j *
n + (std::size_t)k * n * n] = acc / (sub * sub);
272Drop runDroplet(
int n,
double Rcells,
double lam,
bool constantKappa,
int steps) {
273 const double H = 1.0 /
lam;
274 const double SIGMA = 1.0;
277 s.setAdvection(
false);
278 s.setPhysicalDomain({
n *
H,
n *
H,
n *
H}, {0.0, 0.0, 0.0}, {
n,
n,
n});
282 s.setVelocityResidualTolerance(0.0);
283 s.setPressureGeometry(std::vector<double>((std::size_t)n * n * n, 10.0 * H));
284 s.setPressureChebyshev(
true, 500, 1e-14);
286 const std::vector<double>
C = sphereColour(n, Rcells, n / 2 + 0.13, n / 2 + 0.27, n / 2 + 0.11);
289 s.setSurfaceTension(
SIGMA);
291 d.kappa = 2.0 / (Rcells *
H);
293 s.setVofKappaConstant(
d.kappa);
295 s.computeVofCurvature();
296 d.dtCap =
s.capillaryDt();
297 s.setDt(0.5 *
d.dtCap);
298 for (
int k = 0;
k <
steps; ++
k)
300 for (
int c = 0;
c < 3; ++
c)
301 d.maxU = std::fmax(
d.maxU,
maxAbs(
s.getVelocity(c)));
303 const std::vector<double>
p =
s.getPressure();
304 const std::size_t inside =
305 (std::size_t)(n / 2) + (std::size_t)(n / 2) *
n + (std::size_t)(n / 2) *
n *
n;
306 const std::size_t outside = 1 + (std::size_t)1 * n + (std::size_t)1 *
n *
n;
307 d.dp =
p[inside] -
p[outside];
313 std::printf(
"=== units_vof_sigma ===\n");
315 const double Rc = 8.0;
316 const double lam = 1
e2;
319 const Drop
a = runDroplet(
N, Rc, 1.0,
true, 20);
320 const Drop
b = runDroplet(
N, Rc, lam,
true, 20);
321 std::printf(
" constant kappa: max|u| %.3e (lam=1) vs %.3e (lam=%g) -> %.3e rescaled\n",
a.maxU,
322 b.maxU, lam,
b.maxU * lam);
325 const double uScale = 1.0 *
a.kappa / 0.1;
326 CHECK(
a.maxU < 1e-14 * uScale);
327 CHECK(
b.maxU * lam < 1e-14 * uScale);
328 checkClose(
a.dp, 1.0 *
a.kappa, 1e-9 *
a.kappa,
"Young-Laplace at lam=1");
329 checkClose(
b.dp / lam,
a.dp, 1e-9 *
a.kappa,
"Young-Laplace at lam=1e2 (rescaled)");
330 std::printf(
" Young-Laplace: dp %.9e vs sigma*kappa %.9e (lam=%g: %.9e rescaled)\n",
a.dp,
331 a.kappa, lam,
b.dp / lam);
332 checkClose(
b.kappa / lam,
a.kappa, 1e-12 *
a.kappa,
"kappa scales as 1/length");
333 checkClose(
b.dtCap,
a.dtCap, 1e-12 *
a.dtCap,
334 "capillary_dt invariant under a length-unit change");
335 std::printf(
" capillary_dt %.9e vs %.9e (invariant: sigma and the time unit are)\n",
a.dtCap,
339 const Drop
ca = runDroplet(
N, Rc, 1.0,
false, 20);
340 const Drop cb = runDroplet(
N, Rc, lam,
false, 20);
341 const double ru = std::fabs(
ca.maxU - cb.maxU * lam) / std::fabs(
ca.maxU);
342 const double rp = std::fabs(
ca.dp - cb.dp / lam) / std::fabs(
ca.dp);
344 " computed kappa: max|u| %.6e vs %.6e rescaled (rel %.3e); dp %.6e vs %.6e "
346 ca.maxU, cb.maxU * lam, ru,
ca.dp, cb.dp / lam, rp);
360 std::vector<double> yc;
361 double ylo = 0.0, yhi = 0.0;
362 std::array<double, 3> w{1.0, 1.0, 1.0};
366Channel runChannelBox(
int nx,
int ny,
int nz,
const std::array<double, 3>& extent,
int jlo,
int jhi,
368 const double RHO = 1.0,
MU = 0.1,
F = 0.01,
DT = 50.0;
370 s.setAdvection(
false);
372 s.setPhysicalDomain(extent, {0.0, 0.0, 0.0}, {nx,
ny, nz});
376 s.setBodyForce(
F, 0.0, 0.0);
377 s.setVelocityIterations(400);
378 s.setVelocityResidualTolerance(1e-14);
379 s.setPressurePcg(
true, 80, 1e-13);
381 ch.yc =
s.cellCentres(1);
384 std::vector<double>
sdf((std::size_t)nx * ny * nz);
385 for (
int z = 0;
z < nz; ++
z)
386 for (
int y = 0;
y <
ny; ++
y)
387 for (
int x = 0;
x < nx; ++
x)
388 sdf[(std::size_t)
x + (std::size_t)y * nx + (std::size_t)
z * nx *
ny] =
389 std::min(ch.yc[y] - ch.ylo, ch.yhi - ch.yc[y]);
390 s.setSolid(sdf,
false);
391 for (
int it = 0;
it < 300; ++
it)
393 ch.f.u =
s.getVelocity(0);
394 ch.f.v =
s.getVelocity(1);
395 ch.f.w =
s.getVelocity(2);
396 ch.f.p =
s.getPressure();
397 ch.f.spacing =
s.spacing();
398 const auto&
u =
s.unitScales();
399 ch.w = {
u.w[0],
u.w[1],
u.w[2]};
407void checkChannel(
const char* what,
const Channel& ch,
int nx,
int ny,
int nz,
int jlo,
int jhi,
409 const double MU = 0.1,
F = 0.01;
410 const double H = ch.yhi - ch.ylo;
411 const double umax =
F *
H *
H / (8.0 *
MU);
412 std::printf(
" %s: spacing (%.17g, %.17g, %.17g) aniso %d w (%.17g, %.17g, %.17g)\n", what,
413 ch.f.spacing[0], ch.f.spacing[1], ch.f.spacing[2], (
int)ch.aniso, ch.w[0], ch.w[1],
415 double relErr = 0.0, wallMax = 0.0;
416 for (
int z = 0;
z < nz; ++
z)
417 for (
int y = 0;
y <
ny; ++
y)
418 for (
int x = 0;
x < nx; ++
x) {
419 const std::size_t
i = (std::size_t)x + (std::size_t)
y * nx + (std::size_t)z * nx * ny;
420 const double uu = ch.f.u[
i];
421 if (y <= jlo || y >= jhi) {
422 if (!(std::fabs(uu) <= wallMax))
423 wallMax = std::fabs(uu);
426 const double ex =
F * (ch.yc[
y] - ch.ylo) * (ch.yhi - ch.yc[y]) / (2.0 *
MU);
427 const double d = std::fabs(uu - ex) /
umax;
433 " H %.17g u_max %.17g max rel |u - parabola| %.3e (bound %.0e) max|v| %.3e "
434 "max|w| %.3e max|u| on the wall/solid rows %.3e\n",
435 H, umax,
relErr, bound, mv, mw, wallMax);
438 " NOTE: a miss that is a SINGLE one-signed multiplicative factor across the "
439 "whole profile (rel err proportional to u, one constant ratio on every fluid row) "
440 "is the FLOAT momentum-operator storage (`IbmSolver::FV`, MReal; WO-M / "
441 "docs/SCALING_ISSUES.md #1), not the metric -- rebuild with "
442 "-DPECLET_FLOW_OPERATOR_DOUBLE to separate the two.\n");
444 CHECK(wallMax == 0.0);
445 CHECK(mv <= 1e-12 * umax);
446 CHECK(mw <= 1e-12 * umax);
449void gateAnisoPoiseuille() {
450 std::printf(
"=== units_anisotropic_poiseuille ===\n");
456 const Channel ch = runChannelBox(16, 40, 8, {16.0, 10.0, 16.0}, 12, 28,
true);
458 CHECK(ch.f.spacing[0] == 1.0 && ch.f.spacing[1] == 0.25 && ch.f.spacing[2] == 2.0);
459 CHECK(ch.w[0] == 0.0625 && ch.w[1] == 1.0 && ch.w[2] == 0.015625);
460 CHECK(ch.ylo == 3.125 && ch.yhi == 7.125);
461 checkChannel(
"stretched EXACTNESS (16, 40, 8) on (16, 10, 16)", ch, 16, 40, 8, 12, 28, 1e-9);
470 const Channel ch = runChannelBox(16, 40, 8, {16.0, 12.0, 16.0}, 12, 28,
true);
472 checkClose(ch.f.spacing[0], 1.0, 0.0,
"aniso spacing x");
473 checkClose(ch.f.spacing[1], 0.3, 1e-17,
"aniso spacing y");
474 checkClose(ch.f.spacing[2], 2.0, 0.0,
"aniso spacing z");
475 checkClose(ch.ylo, 3.75, 1e-14,
"aniso wall ylo");
476 checkClose(ch.yhi, 8.55, 1e-14,
"aniso wall yhi");
477 checkChannel(
"stretched FLOAT-FLOOR (16, 40, 8) on (16, 12, 16)", ch, 16, 40, 8, 12, 28, 1e-7);
487 const Channel
a = runChannelBox(16, 16, 16, {16.0, 16.0, 16.0}, 5, 11,
true);
489 CHECK(
a.w[0] == 1.0 &&
a.w[1] == 1.0 &&
a.w[2] == 1.0);
490 CHECK(
a.f.spacing[0] == 1.0 &&
a.f.spacing[1] == 1.0 &&
a.f.spacing[2] == 1.0);
491 checkChannel(
"isotropic control (16, 16, 16) at extent == cells", a, 16, 16, 16, 5, 11, 1e-9);
501 sc.setAdvection(
false);
502 sc.setPhysicalDomain({16.0, 12.0, 16.0}, {0.0, 0.0, 0.0}, {16, 40, 8});
503 const auto sp =
sc.spacing();
504 std::printf(
" ADMITTED (collocated): spacing (%.17g, %.17g, %.17g), aniso %d\n", sp[0],
505 sp[1], sp[2], (
int)
sc.unitScales().aniso);
507 }
catch (
const std::exception& ex) {
509 std::printf(
" UNEXPECTED refusal (collocated): %s\n", ex.what());
525 ss.setAdvection(
false);
526 ss.setPhysicalDomain({16.0, 12.0, 16.0}, {0.0, 0.0, 0.0}, {16, 40, 8});
528 std::printf(
" ADMITTED (enable_vof — Phase 3; see units_vof_aniso)\n");
529 }
catch (
const std::exception& ex) {
531 std::printf(
" UNEXPECTED refusal (enable_vof): %s\n", ex.what());
537 ss.setAdvection(
false);
538 ss.setPhysicalDomain({16.0, 12.0, 16.0}, {0.0, 0.0, 0.0}, {16, 40, 8});
539 ss.hydroForceTorque();
540 std::printf(
" ADMITTED (hydro_force_torque)\n");
541 }
catch (
const std::exception& ex) {
543 std::printf(
" UNEXPECTED refusal (hydro_force_torque): %s\n", ex.what());
551 std::printf(
"=== units_identity ===\n");
556 s.setAdvection(
false);
557 CHECK(!
s.hasPhysicalDomain());
558 const auto h =
s.spacing();
559 CHECK(h[0] == 1.0 && h[1] == 1.0 && h[2] == 1.0);
560 const auto e =
s.domainExtent();
561 CHECK(e[0] == 6.0 && e[1] == 8.0 && e[2] == 10.0);
562 const auto o =
s.domainOrigin();
563 CHECK(o[0] == 0.0 && o[1] == 0.0 && o[2] == 0.0);
564 const auto c =
s.cellCentres(1);
566 CHECK(c[0] == 0.5 && c[7] == 7.5);
572 s.setAdvection(
false);
573 s.setPhysicalDomain({6.0, 8.0, 10.0}, {-2.0, 0.5, 100.0}, {6, 8, 10});
574 CHECK(
s.hasPhysicalDomain());
575 const auto h =
s.spacing();
576 CHECK(h[0] == 1.0 && h[1] == 1.0 && h[2] == 1.0);
577 const auto o =
s.domainOrigin();
578 CHECK(o[0] == -2.0 && o[1] == 0.5 && o[2] == 100.0);
579 const auto cx =
s.cellCentres(0);
580 CHECK(cx[0] == -1.5 && cx[5] == 3.5);
581 const auto cz =
s.cellCentres(2);
582 CHECK(cz[0] == 100.5 && cz[9] == 109.5);
583 const auto&
u =
s.unitScales();
587 CHECK(
u.rhoRef == 1.0 &&
u.tRef == 1.0);
588 CHECK(
u.muToInt() == 1.0 &&
u.pToInt() == 1.0 &&
u.forceToInt(0) == 1.0);
589 CHECK(
u.velToInt(2) == 1.0 &&
u.lenToInt() == 1.0 &&
u.sigmaToInt() == 1.0);
594 auto run = [](
bool arm) {
595 const int nx = 8,
ny = 24, nz = 8;
597 s.setAdvection(
false);
599 s.setPhysicalDomain({(double)nx, (
double)
ny, (double)nz}, {0.0, 0.0, 0.0}, {nx,
ny, nz});
603 s.setBodyForce(0.01, 0.0, 0.0);
604 s.setVelocityIterations(60);
605 s.setPressureIterations(4);
606 const double ylo = 7.5, yhi =
ny - 7.5;
607 std::vector<double>
sdf((std::size_t)nx * ny * nz);
608 for (
int z = 0;
z < nz; ++
z)
609 for (
int y = 0;
y <
ny; ++
y)
610 for (
int x = 0;
x < nx; ++
x)
611 sdf[(std::size_t)
x + (std::size_t)y * nx + (std::size_t)
z * nx *
ny] =
612 std::min((y + 0.5) - ylo, yhi - (y + 0.5));
613 s.setSolid(sdf,
true);
614 for (
int it = 0;
it < 30; ++
it)
617 f.u =
s.getVelocity(0);
618 f.v =
s.getVelocity(1);
619 f.w =
s.getVelocity(2);
620 f.p =
s.getPressure();
623 const Fields
a =
run(
false),
b =
run(
true);
629 std::printf(
" extent == cells, rho = dt = 1: bitwise identical to the cell-unit run\n");
635 std::printf(
"=== units_scale_invariance ===\n");
636 const double lam = 1
e3;
637 const double TOL = 1
e-13;
640 const int nx = 8,
ny = 24, nz = 8;
641 const Fields
a = runChannel<peclet::flow::Staggered>(nx, ny, nz, 1.0,
true);
642 const Fields
b = runChannel<peclet::flow::Staggered>(nx, ny, nz, lam,
true);
643 checkClose(
a.spacing[0], 1.0, 0.0,
"channel spacing (system A)");
644 checkClose(
b.spacing[0], 1.0 / lam, 1e-18,
"channel spacing (system B)");
646 const double ru = relDiffScaled(
a.u,
b.u, lam);
647 const double rp = relDiffScaled(
a.p,
b.p, 1.0 / lam);
648 std::printf(
" Poiseuille (sampled SDF): u %.3e p %.3e\n", ru, rp);
656 const Fields
a = runSphere<peclet::flow::Staggered>(
N, 1.0,
true, scene != 0);
657 const Fields
b = runSphere<peclet::flow::Staggered>(
N, lam,
true, scene != 0);
658 const double ru = relDiffScaled(
a.u,
b.u, lam);
659 const double rv = relDiffScaled(
a.v,
b.v, lam);
660 const double rp = relDiffScaled(
a.p,
b.p, 1.0 / lam);
665 const double rd = std::fabs(
a.div -
b.div);
666 std::printf(
" sphere Stokes (%s): u %.3e v %.3e p %.3e div %.3e vs %.3e (d %.3e)\n",
667 scene ?
"analytic scene" :
"sampled SDF", ru, rv, rp,
a.div,
b.div, rd);
672 CHECK(
a.div < 1e-9 &&
b.div < 1e-9);
729 double K = 0.0, umean = 0.0, div = 0.0, R = 0.0, pIterStep = 0.0;
731 std::array<double, 3> spacing{};
737Zh runZhSphere(
int nx,
int ny,
int nz,
bool arm,
double L,
int levels) {
738 const double RHO = 1.0,
MU = 0.1,
DT = 60.0,
FX = 1
e-3,
PHI = 0.216;
739 const double R = std::pow(PHI * 3.0 / (4.0 * M_PI), 1.0 / 3.0) *
L;
742 s.setPhysicalDomain({
L,
L,
L}, {0.0, 0.0, 0.0}, {nx,
ny, nz});
746 s.setBodyForce(
FX, 0.0, 0.0);
747 s.setAdvection(
false);
748 s.setVelocityIterations(80);
749 s.setPressureLevels(levels);
750 s.setPressurePcg(
true, 300, 1e-8);
751 const std::vector<double> cx =
s.cellCentres(0), cy =
s.cellCentres(1), cz =
s.cellCentres(2);
752 const double c = 0.5 *
L;
753 std::vector<double>
sdf((std::size_t)nx * ny * nz);
754 for (
int z = 0;
z < nz; ++
z)
755 for (
int y = 0;
y <
ny; ++
y)
756 for (
int x = 0;
x < nx; ++
x)
757 sdf[(std::size_t)
x + (std::size_t)y * nx + (std::size_t)
z * nx *
ny] =
758 std::sqrt((cx[x] - c) * (cx[x] - c) + (cy[y] - c) * (cy[y] - c) +
759 (cz[z] - c) * (cz[z] - c)) -
761 s.setSolid(sdf,
true);
762 std::vector<double>
pit;
765 for (
int it = 0;
it < 400; ++
it) {
768 pit.push_back((
double)
s.lastPressureIterations());
770 const std::vector<double> uu =
s.getVelocity(0);
774 m /= (double)uu.size();
775 if (it >= 15 && std::fabs(m - prev) < 1e-5 * (std::fabs(m) + 1e-30))
780 const std::vector<double> uu =
s.getVelocity(0);
784 r.umean =
m / (double)uu.size();
786 r.K =
FX * (
L *
L *
L) / (6.0 * M_PI *
MU *
R *
r.umean);
787 r.div =
s.maxOpenDivergence();
788 r.spacing =
s.spacing();
789 r.aniso =
s.unitScales().aniso;
790 std::vector<double>
half(
pit.begin() + (std::ptrdiff_t)(
pit.size() / 2),
pit.end());
791 std::sort(
half.begin(),
half.end());
792 const std::size_t nh =
half.size();
793 r.pIterStep = (nh % 2) ? half[nh / 2] : 0.5 * (
half[nh / 2 - 1] +
half[nh / 2]);
798double logLogOrder(
const std::vector<double>& Ns,
const std::vector<double>& err) {
799 double sx = 0, sy = 0, sxx = 0, sxy = 0;
800 const double n = (double)
Ns.size();
801 for (std::size_t i = 0;
i <
Ns.size(); ++
i) {
802 const double X = std::log(Ns[i]),
Y = std::log(err[i]);
808 return -(
n * sxy -
sx * sy) / (n * sxx - sx * sx);
814 double Fx = 0.0, Fref = 0.0, rel = 0.0;
816 std::array<double, 3> spacing{};
819React runReactionSphere(
int nx,
int ny,
int nz,
bool arm,
double L,
int levels,
int nsteps) {
820 const double RHO = 1.0,
MU = 0.1,
DT = 60.0,
FX = 1
e-3,
PHI = 0.216;
821 const double R = std::pow(PHI * 3.0 / (4.0 * M_PI), 1.0 / 3.0) *
L;
824 s.setPhysicalDomain({
L,
L,
L}, {0.0, 0.0, 0.0}, {nx,
ny, nz});
828 s.setBodyForce(
FX, 0.0, 0.0);
829 s.setAdvection(
false);
830 s.setVelocityIterations(200);
831 s.setVelocityResidualTolerance(1e-12);
832 s.setPressureLevels(levels);
833 s.setPressurePcg(
true, 300, 1e-10);
835 namespace g = peclet::core::geom;
836 g::SceneBuilder<double>
b;
837 b.addLeaf(g::kSphere, {
R});
838 g::Transform<double>
tr;
839 tr.translation = peclet::core::Vec3<double>{0.5 *
L, 0.5 *
L, 0.5 *
L};
840 b.addInstance(0, tr);
841 std::vector<int> ni, ii;
842 std::vector<double> nr, ir;
843 b.encode(ni, nr, ii, ir);
844 s.setScene(ni, nr, ii, ir,
true);
845 s.setSolidFromScene(
true);
847 for (
int it = 0;
it < nsteps; ++
it)
850 out.spacing =
s.spacing();
854 const std::vector<double> sd =
s.getField(
"sdf");
856 for (
int z = 0;
z < nz; ++
z)
857 for (
int y = 0;
y <
ny; ++
y)
858 for (
int x = 0;
x < nx; ++
x) {
859 const int xm = (
x + nx - 1) % nx;
860 const double a = sd[(std::size_t)xm + (std::size_t)
y * nx + (std::size_t)z * nx * ny];
861 const double b2 = sd[(std::size_t)x + (std::size_t)
y * nx + (std::size_t)z * nx * ny];
862 if (0.5 * (a + b2) > 0.0)
866 const double vcell =
out.spacing[0] *
out.spacing[1] *
out.spacing[2];
867 out.Fref =
FX * vcell * (double)nf;
868 const std::vector<double>
fr =
s.hydroForceTorqueReaction();
869 out.Fx =
fr.empty() ? 0.0 :
fr[0];
870 out.rel = std::fabs(
out.Fx -
out.Fref) / std::fabs(
out.Fref);
874void gateAnisoSphere() {
875 std::printf(
"=== units_anisotropic_sphere ===\n");
876 const double KREF = 7.442;
877 const double REC[3] = {7.299689474225098, 7.389063051924422, 7.416167928926183};
878 const int NS[3] = {16, 24, 32};
879 std::vector<double>
Ns, errS, errC;
880 for (
int i = 0;
i < 3; ++
i) {
882 const int levels = std::max(2, (
int)std::floor(std::log2((
double)
N)) - 1);
883 const Zh cub = runZhSphere(
N,
N,
N,
false, (
double)
N, levels);
884 const Zh str = runZhSphere(
N, 2 *
N,
N / 2,
true, (
double)
N, levels);
885 const double ec = std::fabs(cub.K - KREF) /
KREF,
es = std::fabs(str.K - KREF) /
KREF;
886 Ns.push_back((
double)
N);
889 std::printf(
" N=%2d cubic K %.17g err %.4f %% iters/step %.1f steps %3d div %.2e\n",
N,
890 cub.K, 100.0 * ec, cub.pIterStep, cub.steps, cub.div);
892 " stretched (%d,%d,%d) spacing (%g, %g, %g) aniso %d K %.17g err %.4f %%"
893 " iters/step %.1f steps %3d div %.2e\n",
894 N, 2 *
N,
N / 2, str.spacing[0], str.spacing[1], str.spacing[2], (
int)str.aniso, str.K,
895 100.0 * es, str.pIterStep, str.steps, str.div);
897 const double dRec = std::fabs(cub.K - REC[i]) / REC[
i];
898 const int RECSTEPS[3] = {60, 80, 75};
902 const double RECITERS[3] = {6.0, 6.0, 6.0};
904 " cubic vs perf_baseline.json %.17g rel %.3e (bound 1e-6) steps %d "
905 "(recorded %d) iters/step %.1f (recorded %.1f)\n",
906 REC[i], dRec, cub.steps, RECSTEPS[i], cub.pIterStep, RECITERS[i]);
908 CHECK(cub.steps == RECSTEPS[i]);
909 CHECK(cub.pIterStep == RECITERS[i]);
911 CHECK(es <= 4.0 * ec + 0.002);
913 std::printf(
" pressure iters/step cubic %.1f stretched %.1f (bound cubic + 2)\n",
914 cub.pIterStep, str.pIterStep);
915 CHECK(str.pIterStep <= cub.pIterStep + 2.0);
918 CHECK(errS[1] < errS[0]);
919 CHECK(errS[2] < errS[1]);
921 const double ps = logLogOrder(Ns, errS), pc = logLogOrder(Ns, errC);
922 std::printf(
" LS order of the ERROR sequence: stretched %.4f (bound >= 1.5), cubic %.4f\n", ps,
929 const int levels = std::max(2, (
int)std::floor(std::log2((
double)
N)) - 1);
930 const React
rc = runReactionSphere(
N,
N,
N,
false, (
double)
N, levels, 300);
931 const React rs = runReactionSphere(
N, 2 *
N,
N / 2,
true, (
double)
N, levels, 300);
932 std::printf(
" hydro_force_torque_reaction, periodic Stokes identity F = F_body*V_fluid:\n");
934 " cubic (%d,%d,%d) spacing (%g,%g,%g) F_x %.17g identity %.17g rel %.3e"
935 " (%ld fluid u-DOFs)\n",
936 N,
N,
N,
rc.spacing[0],
rc.spacing[1],
rc.spacing[2],
rc.Fx,
rc.Fref,
rc.rel,
rc.nFluid);
938 " stretched (%d,%d,%d) spacing (%g,%g,%g) F_x %.17g identity %.17g rel %.3e"
939 " (%ld fluid u-DOFs)\n",
940 N, 2 *
N,
N / 2, rs.spacing[0], rs.spacing[1], rs.spacing[2], rs.Fx, rs.Fref, rs.rel,
946 CHECK(rs.rel <= 1e-5);
948 std::printf(
" stretched/cubic relative-gap ratio %.3f (both gated at 1e-5)\n",
955 double ratio = 0.0, rExact = 0.0, relRate = 0.0, div = 0.0, prof = 0.0, pmax = 0.0;
956 std::vector<double> u, v, w, p;
957 std::array<double, 3> spacing{};
964Tgv runTgv(
int nx,
int ny,
int nz,
bool arm,
double Lx,
double Ly,
double Lz,
bool advect,
965 int nsteps,
double DT = 1.0) {
966 const double RHO = 1.0,
MU = 0.25;
967 const double hx = arm ? Lx / nx : 1.0, hy = arm ? Ly /
ny : 1.0, hz = arm ? Lz / nz : 1.0;
968 const double Lw = arm ? Lx : (double)nx;
969 const double k = 2.0 * M_PI / Lw;
970 const double ax = 1.0;
971 const double ay =
ax * (std::sin(0.5 * k * hx) / hx) * (hy / std::sin(0.5 * k * hy));
974 s.setPhysicalDomain({Lx, Ly, Lz}, {0.0, 0.0, 0.0}, {nx,
ny, nz});
979 s.setVelocityIterations(400);
980 s.setVelocityResidualTolerance(1e-14);
981 s.setPressurePcg(
true, 300, 1e-13);
982 s.setPressureGeometry(std::vector<double>((std::size_t)nx * ny * nz, 10.0));
983 const std::size_t
n = (std::size_t)nx * ny * nz;
984 std::vector<double>
u(n),
v(n),
w(n, 0.0);
985 for (
int z = 0;
z < nz; ++
z)
986 for (
int y = 0;
y <
ny; ++
y)
987 for (
int x = 0;
x < nx; ++
x) {
988 const std::size_t
i = (std::size_t)x + (std::size_t)
y * nx + (std::size_t)z * nx * ny;
989 u[
i] =
ax * std::cos(k * (x * hx)) * std::sin(k * ((y + 0.5) * hy));
990 v[
i] = -ay * std::sin(k * ((x + 0.5) * hx)) * std::cos(k * (y * hy));
992 s.uploadVelocity(u, v, w);
994 const double Lambda =
995 2.0 * (1.0 - std::cos(k * hx)) / (hx * hx) + 2.0 * (1.0 - std::cos(k * hy)) / (hy * hy);
996 r.rExact = std::pow(1.0 / (1.0 +
DT * (
MU /
RHO) * Lambda), (
double)nsteps);
997 for (
int it = 0;
it < nsteps; ++
it)
999 r.u =
s.getVelocity(0);
1000 r.v =
s.getVelocity(1);
1001 r.w =
s.getVelocity(2);
1002 r.p =
s.getPressure();
1003 r.spacing =
s.spacing();
1004 r.aniso =
s.unitScales().aniso;
1005 double num = 0.0,
den = 0.0;
1006 for (std::size_t i = 0;
i <
n; ++
i) {
1011 double pmaxu = 0.0,
perr = 0.0;
1012 for (std::size_t i = 0;
i <
n; ++
i) {
1013 pmaxu = std::fmax(pmaxu, std::fabs(u[i]));
1014 perr = std::fmax(perr, std::fabs(
r.u[i] -
r.ratio * u[i]));
1016 r.prof =
perr / pmaxu;
1017 r.relRate = std::fabs(
r.ratio -
r.rExact) /
r.rExact;
1018 r.div =
s.maxOpenDivergence();
1024void gateAnisoTgv() {
1025 std::printf(
"=== units_anisotropic_tgv ===\n");
1026 const int N = 16,
NS = 10;
1030 runTgv(
N, 2 *
N, 4,
true, (
double)
N, (
double)
N, 4.0,
false, NS);
1031 std::printf(
" stretched Stokes (%d,%d,4) spacing (%g, %g, %g) aniso %d\n",
N, 2 *
N,
1032 t.spacing[0],
t.spacing[1],
t.spacing[2], (
int)
t.aniso);
1033 std::printf(
" amplitude ratio %.17g exact %.17g rel %.3e (bound 1e-10)\n",
t.ratio,
1034 t.rExact,
t.relRate);
1036 " max|div_o| %.3e (bound 1e-12) profile %.3e (bound 1e-10) max|P| %.3e "
1037 "(the phi proxy, bound 1e-12)\n",
1038 t.div,
t.prof,
t.pmax);
1040 CHECK(
t.relRate <= 1e-10);
1053 runTgv(
N, 2 *
N, 4,
true, (
double)
N, (
double)
N, 4.0,
true, NS, 0.05);
1055 " stretched NS (%d,%d,4) dt 0.05 amplitude ratio %.17g exact %.17g "
1056 "rel %.3e (bound 5e-3) max|div| %.3e (bound 1e-9)\n",
1057 N, 2 *
N,
t.ratio,
t.rExact,
t.relRate,
t.div);
1058 CHECK(
t.relRate <= 5e-3);
1064 const Tgv
a = runTgv(
N,
N, 4,
true, (
double)
N, (
double)
N, 4.0,
false, NS);
1065 const Tgv
b = runTgv(
N,
N, 4,
false, (
double)
N, (
double)
N, 4.0,
false, NS);
1066 std::printf(
" isotropic control (%d,%d,4) spacing (%g, %g, %g) aniso %d\n",
N,
N,
a.spacing[0],
1067 a.spacing[1],
a.spacing[2], (
int)
a.aniso);
1069 " amplitude ratio %.17g exact %.17g rel %.3e max|div_o| %.3e profile %.3e "
1071 a.ratio,
a.rExact,
a.relRate,
a.div,
a.prof,
a.pmax);
1073 CHECK(
a.relRate <= 1e-10);
1077 const bool bit = bitwiseEqual(
a.u,
b.u) && bitwiseEqual(
a.v,
b.v) && bitwiseEqual(
a.w,
b.w) &&
1078 bitwiseEqual(
a.p,
b.p);
1079 std::printf(
" armed at extent == cells vs the cell-unit run: bitwise %s\n",
1080 bit ?
"EQUAL" :
"DIFFERENT");
1111double sphereFracBox(
double cx,
double cy,
double cz,
double R,
double x0,
double y0,
double z0,
1112 const double h[3],
int sub = 24) {
1113 const double wx = h[0] / sub, wy = h[1] / sub;
1115 for (
int b = 0; b < sub; ++b)
1116 for (
int a = 0; a < sub; ++a) {
1117 const double px = x0 + (a + 0.5) * wx, py = y0 + (b + 0.5) * wy;
1118 const double r2 =
R *
R - (px - cx) * (px - cx) - (py - cy) * (py - cy);
1121 const double hh = std::sqrt(r2);
1122 const double lo = std::fmax(cz - hh, z0), hi = std::fmin(cz + hh, z0 + h[2]);
1126 return acc / (sub * sub * h[2]);
1132 const double SIGMA = 1.0,
MU = 0.1,
RHO = 1.0;
1134 s.setAdvection(
false);
1135 s.setPhysicalDomain({n[0] * h[0], n[1] * h[1], n[2] * h[2]}, {0.0, 0.0, 0.0}, {n[0], n[1], n[2]});
1139 s.setVelocityResidualTolerance(0.0);
1140 const std::size_t nc = (std::size_t)n[0] * n[1] * n[2];
1141 const double hMin = std::fmin(h[0], std::fmin(h[1], h[2]));
1142 s.setPressureGeometry(std::vector<double>(nc, 10.0 * hMin));
1143 s.setPressureChebyshev(
true, 500, 1e-14);
1146 const double ctr[3] = {0.5 * n[0] * h[0] + 0.13 * h[0], 0.5 * n[1] * h[1] + 0.27 * h[1],
1147 0.5 * n[2] * h[2] + 0.11 * h[2]};
1148 std::vector<double> C(nc);
1149 for (
int k = 0; k < n[2]; ++k)
1150 for (
int j = 0; j < n[1]; ++j)
1151 for (
int i = 0; i < n[0]; ++i)
1152 C[(std::size_t)i + (std::size_t)j * n[0] + (std::size_t)k * n[0] * n[1]] =
1153 sphereFracBox(ctr[0], ctr[1], ctr[2],
R, i * h[0], j * h[1], k * h[2], h);
1156 s.setSurfaceTension(
SIGMA);
1161 s.setVofKappaConstant(d.kappa);
1163 s.computeVofCurvature();
1164 d.dtCap = s.capillaryDt();
1165 s.setDt(0.5 * d.dtCap);
1166 for (
int q = 0; q < steps; ++q)
1168 for (
int c = 0; c < 3; ++c)
1169 d.maxU = std::fmax(d.maxU,
maxAbs(s.getVelocity(c)));
1170 const std::vector<double> p = s.getPressure();
1171 const std::size_t inside = (std::size_t)(n[0] / 2) + (std::size_t)(n[1] / 2) * n[0] +
1172 (std::size_t)(n[2] / 2) * n[0] * n[1];
1173 const std::size_t outside = 1 + (std::size_t)1 * n[0] + (std::size_t)1 * n[0] * n[1];
1174 d.dp = p[inside] - p[outside];
1179 std::printf(
"=== units_vof_aniso (Phase 3 S2: the balanced-force CSF on BOX cells) ===\n");
1182 const double base = 1.0 / 32.0;
1188 const Mesh meshes[3] = {
1189 {
"cubic ", {32, 32, 32}, {base, base, base}},
1190 {
"aspect 2 ", {32, 64, 32}, {base, base / 2, base}},
1191 {
"aspect 4 ", {32, 64, 128}, {base, base / 2, base / 4}},
1193 const double R = 8.0 * base;
1194 const double uScale = 1.0 * (2.0 /
R) / 0.1;
1196 for (
int q = 0; q < 3; ++q) {
1197 const Mesh& m = meshes[q];
1201 std::printf(
" %s n = (%3d,%3d,%3d) h'/hMin = (%.2f, %.2f, %.2f)\n", m.name, m.n[0], m.n[1],
1202 m.n[2], m.h[0] / a.hMin, m.h[1] / a.hMin, m.h[2] / a.hMin);
1203 std::printf(
" exactness max|u| = %.3e (gate %.3e = 1e-14 * sigma*kappa/mu)\n", a.maxU,
1205 std::printf(
" Young-Laplace dp = %.9e vs sigma*kappa = %.9e (rel %.2e)\n", a.dp, a.kappa,
1206 std::fabs(a.dp - a.kappa) / a.kappa);
1207 std::printf(
" capillary_dt = %.9e (h_min = %.6e)\n", a.dtCap, a.hMin);
1208 CHECK(a.maxU < 1e-14 * uScale);
1209 checkClose(a.dp, a.kappa, 1e-9 * a.kappa,
"anisotropic Young-Laplace");
1213 const double want = meshes[0].h[0] / a.hMin;
1215 std::printf(
" dt_sigma(cubic)/dt_sigma(this) = %.4f (h^{3/2} predicts %.4f)\n", got,
1216 std::pow(want, 1.5));
1217 CHECK(std::fabs(got - std::pow(want, 1.5)) < 1e-9 * std::pow(want, 1.5));
1238 const double caC = 0.1 * cc.
maxU / 1.0, caA = 0.1 * ca.maxU / 1.0;
1239 std::printf(
" computed kappa: spurious Ca cubic %.3e vs aspect-2 %.3e (ratio %.2f, gate 4)\n",
1240 caC, caA, caA / caC);
1241 CHECK(caA < 4.0 * caC);
1242 checkClose(ca.dp, ca.kappa, 5e-2 * ca.kappa,
"anisotropic Young-Laplace (computed kappa)");
1259 double lam = 1.0, tau = 1.0, mas = 1.0;
1260 double len(
double x)
const {
return x / lam; }
1261 double time(
double t)
const {
return t / tau; }
1262 double rho(
double r)
const {
return r * lam * lam * lam / mas; }
1263 double mu(
double m)
const {
return m * lam * tau / mas; }
1264 double diff(
double d)
const {
return d * tau / (lam * lam); }
1265 double vel(
double u)
const {
return u * tau / lam; }
1266 double mdot(
double m)
const {
return m * lam * lam * tau / mas; }
1267 double latent(
double h)
const {
return h * tau * tau / (lam * lam); }
1268 double cond(
double k)
const {
return k * tau * tau * tau / (mas * lam); }
1269 double rcp(
double c)
const {
return c * lam * tau * tau / mas; }
1270 double pres(
double p)
const {
return p * lam * tau * tau / mas; }
1271 double force(
double f)
const {
return f * lam * lam * tau * tau / mas; }
1272 double area(
double a)
const {
return a / (lam * lam); }
1273 double vol(
double v)
const {
return v / (lam * lam * lam); }
1274 double energy(
double e)
const {
return e * tau * tau / (mas * lam * lam); }
1275 double power(
double p)
const {
return p * tau * tau * tau / (mas * lam * lam); }
1276 double rate(
double r)
const {
return r * tau; }
1279double relDiff(
const std::vector<double>& a,
const std::vector<double>& b) {
1280 return relDiffScaled(a, b, 1.0);
1282double relScalar(
double a,
double b) {
1283 const double den = std::fabs(a) > 0.0 ? std::fabs(a) : std::fabs(
b);
1284 return den > 0.0 ? std::fabs(a - b) /
den : 0.0;
1292 std::vector<double> T, x;
1295ScalarSine runScalarSine(
int N,
const std::array<double, 3>& ext,
double D,
double U,
double dt,
1296 int nsteps,
const Units& un,
bool dtFirst,
int scheme = 1) {
1297 const int ny = 4, nz = 4;
1299 s.setAdvection(
false);
1300 s.setPhysicalDomain({un.len(ext[0]), un.len(ext[1]), un.len(ext[2])}, {0.0, 0.0, 0.0},
1302 s.setRho(un.rho(1.0));
1304 s.setDt(un.time(dt));
1305 s.addScalar(
"T", un.diff(D), scheme, 200);
1307 s.setDt(un.time(dt));
1308 s.setPressureGeometry(std::vector<double>((std::size_t)
N * ny * nz, un.len(1.0)));
1309 const double k = 2.0 * M_PI / un.len(ext[0]);
1310 const std::vector<double> cx =
s.cellCentres(0);
1311 std::vector<double> T0((std::size_t)
N * ny * nz);
1312 for (
int z = 0;
z < nz; ++
z)
1313 for (
int y = 0;
y <
ny; ++
y)
1314 for (
int x = 0;
x <
N; ++
x)
1315 T0[(std::size_t)
x + (std::size_t)y *
N + (std::size_t)
z *
N *
ny] = std::sin(k * cx[x]);
1316 s.setField(
"T", T0);
1318 s.setVelocity(0, std::vector<double>((std::size_t)
N * ny * nz, un.vel(
U)));
1319 for (
int n = 0;
n < nsteps; ++
n)
1322 r.T =
s.getField(
"T");
1324 double num = 0.0,
den = 0.0;
1325 for (
int z = 0;
z < nz; ++
z)
1326 for (
int y = 0;
y <
ny; ++
y)
1327 for (
int x = 0;
x <
N; ++
x) {
1328 const double sx = std::sin(k * cx[x]);
1329 num +=
r.T[(std::size_t)x + (std::size_t)
y *
N + (std::size_t)z *
N * ny] * sx;
1337double closureProbe(
const Units& un,
bool rhoFirst) {
1340 s.setAdvection(
false);
1341 s.setPhysicalDomain({un.len(1.0), un.len(1.0), un.len(1.0)}, {0.0, 0.0, 0.0}, {
N,
N,
N});
1343 s.setRho(un.rho(998.0));
1344 s.setDt(un.time(1e-3));
1346 s.setPressureGeometry(std::vector<double>((std::size_t)
N *
N *
N, un.len(1.0)));
1347 s.addScalar(
"T", un.diff(1.4e-7), 1, 10);
1348 s.setField(
"T", std::vector<double>((std::size_t)
N *
N *
N, 310.0));
1351 {un.rho(998.0), un.force(9.81) / un.rho(1.0), 2.1e-4, 300.0});
1353 s.setRho(un.rho(998.0));
1354 s.setDt(un.time(1e-3));
1356 s.updateProperties();
1357 return s.getField(
"force_z")[0];
1361 std::printf(
"=== units_scalar_scale_invariance ===\n");
1362 const double TOL = 1
e-13;
1370 const std::array<double, 3> ext{2.0, 4.0 * 2.0 /
N, 4.0 * 2.0 /
N};
1371 const double h = 2.0 /
N,
D = 0.05,
dt = 0.5 *
h *
h /
D;
1372 for (
int adv = 0; adv < 2; ++adv) {
1373 const double U = adv ? 0.3 : 0.0;
1374 const ScalarSine
a = runScalarSine(
N, ext, D,
U, dt, 40, ref,
true);
1375 const ScalarSine
b = runScalarSine(
N, ext, D,
U, dt, 40, big,
false);
1376 const double rT = relDiff(
a.T,
b.T);
1377 std::printf(
" %s: max|T_A - T_B|/max|T_A| = %.3e (B registers the scalar before set_dt)\n",
1378 adv ?
"advection-diffusion (Koren)" :
"diffusion ", rT);
1384 const double fA = closureProbe(ref,
true), fB = closureProbe(big,
false);
1385 const double fC = closureProbe(big,
true);
1386 std::printf(
" Boussinesq force_z (internal): A %.17g B(rho/dt after) %.17g B(before) %.17g\n",
1388 CHECK(std::fabs(fA) > 0.0);
1389 CHECK(relScalar(fA, fB) <= TOL);
1390 CHECK(relScalar(fA, fC) <= TOL);
1393void gateScalarSine() {
1394 std::printf(
"=== units_scalar_sine_decay ===\n");
1408 const double Lx = 2.0,
D = 0.05,
k = 2.0 * M_PI / Lx, tEnd = 1.0 / (
D *
k *
k);
1409 for (
int box = 0;
box < 2; ++
box) {
1410 double err[2] = {0, 0};
1411 for (
int q = 0;
q < 2; ++
q) {
1412 const int N =
q ? 32 : 16;
1413 const double h = Lx /
N, dt0 = 0.25 *
h *
h /
D;
1414 const int ns = (
int)std::ceil(tEnd / dt0);
1415 const double dt = tEnd /
ns;
1416 const double hyz =
box ? 0.5 *
h :
h;
1417 const ScalarSine
r = runScalarSine(
N, {Lx, 4 * hyz, 4 * hyz},
D, 0.0,
dt,
ns, un,
true);
1418 const double s2 = std::sin(0.5 * k * h);
1419 const double rr = 1.0 / (1.0 +
dt *
D * 4.0 *
s2 *
s2 / (
h *
h));
1420 const double discrete = std::pow(rr, ns),
exact = std::exp(-D * k * k * tEnd);
1422 const double eDisc = std::fabs(
r.amp - discrete) / discrete;
1424 " %s N=%2d (%3d steps): amplitude %.15f discrete %.15f (rel %.2e) exact %.15f "
1426 box ?
"box " :
"cubic",
N, ns,
r.amp, discrete, eDisc, exact, err[q]);
1427 CHECK(eDisc <= 1e-12);
1429 const double order = std::log(err[0] / err[1]) / std::log(2.0);
1430 std::printf(
" %s observed order %.3f\n", box ?
"box " :
"cubic", order);
1431 CHECK(order > 1.9 && order < 2.1);
1432 CHECK(err[1] < 1e-2);
1438 std::vector<double> C, T, u, p;
1440 peclet::flow::IbmSolver::PhaseChangeDiagnostics d;
1441 peclet::flow::IbmSolver::PhaseChangeBudget b;
1442 double carryDep = 0.0, area = 0.0;
1445double stefanLambdaU(
double St) {
1446 const double rhs = St / std::sqrt(M_PI);
1447 double a = 1
e-8,
b = 5.0;
1448 for (
int i = 0;
i < 200; ++
i) {
1449 const double m = 0.5 * (
a +
b);
1450 ((
m * std::exp(m * m) * std::erf(m) - rhs) > 0 ?
b :
a) = m;
1452 return 0.5 * (
a +
b);
1459PcRun runStefanUnits(
int N,
const Units& un,
bool energy,
bool scalesFirst) {
1460 const double St = 1.0,
alpha = 1.0, x0p = 0.10, xep = 0.25, Fo = 0.5;
1461 const double lam = stefanLambdaU(St);
1462 const double t0 = (x0p / (2 *
lam)) * (x0p / (2 * lam)) /
alpha;
1463 const double te = (xep / (2 *
lam)) * (xep / (2 * lam)) /
alpha;
1464 const int ny = 4, nz = 4;
1466 const int ns = (
int)std::llround((te - t0) / (Fo / D));
1467 const double dt = (te -
t0) / ns;
1469 s.setAdvection(
false);
1470 s.setPhysicalDomain({un.len(
N), un.len(ny), un.len(nz)}, {0.0, 0.0, 0.0}, {
N,
ny, nz});
1472 s.setRho(un.rho(1.0));
1473 s.setMu(un.mu(1e-3));
1474 s.setDt(un.time(dt));
1478 s.setDomainBc(0, 1, 0, 0, 0);
1479 s.setDomainBc(1, 1, 0, 0, 0);
1480 s.setPressureGeometry(std::vector<double>((std::size_t)
N * ny * nz, un.len(1.0)));
1481 const double xg = x0p *
N;
1482 std::vector<double>
C((std::size_t)
N * ny * nz, 0.0),
T((std::size_t)
N * ny * nz, 0.0);
1483 for (
int z = 0;
z < nz; ++
z)
1484 for (
int y = 0;
y <
ny; ++
y)
1485 for (
int x = 0;
x <
N; ++
x) {
1486 const std::size_t
i = (std::size_t)x + (std::size_t)
y *
N + (std::size_t)z *
N * ny;
1487 C[
i] = std::fmin(1.0, std::fmax(0.0, (x + 1) - xg));
1488 const double xp = (
x + 0.5) /
N;
1489 T[
i] = xp < x0p ? 1.0 - std::erf(lam * xp / x0p) / std::erf(lam) : 0.0;
1493 s.addScalar(
"T", un.diff(D), 1, 60);
1494 s.setScalarBc(
"T", 0, 2, 1.0);
1495 s.setScalarBc(
"T", 1, 2, 0.0);
1497 s.enablePhaseChange(un.rho(1.0), un.rho(1.0), un.latent(1.0));
1499 s.setPhaseChangeThermal(
"T", 0.0, un.cond(D), un.cond(D), 0.0);
1501 s.setPhaseChangeEnergy(un.rcp(1.0), un.rcp(1.0));
1502 s.setPhaseChangeEnergyOrder(2);
1503 s.setPhaseChangeBudget(
true);
1507 for (
int n = 0;
n <
ns; ++
n) {
1508 s.applyPhaseChange(un.time(dt));
1513 r.T =
s.getField(
"T");
1515 for (
double q :
r.C)
1517 r.layer = un.len(
N - sum / (ny * nz));
1518 r.d =
s.phaseChangeDiagnostics();
1520 r.b =
s.phaseChangeBudgetValues();
1521 r.carryDep =
s.phaseChangeCarryDeposited();
1523 r.area =
s.vofInterfaceArea();
1530PcRun runSuckingUnits(
int N,
int nsteps,
const Units& un) {
1531 const double ratio = 10.0, ja = 1.0, alpha_l = 1.0, x0p = 0.10, Fo = 0.5, cfl = 0.2;
1532 const double rr = 1.0 /
ratio;
1533 double b = ja / std::sqrt(M_PI);
1534 for (
int i = 0;
i < 300; ++
i) {
1535 const double g =
b * rr;
1536 b = ja * std::exp(-g * g) / (std::sqrt(M_PI) * std::erfc(g));
1538 const double t0 = (x0p / (2 *
b)) * (x0p / (2 * b)) / alpha_l;
1539 const int ny = 4, nz = 4;
1540 const double al = alpha_l *
N *
N, rho_l = 1.0, rho_v = rr, cpl = 1.0;
1541 const double k_l = al * rho_l * cpl, k_v = k_l /
ratio, dT = 1.0;
1542 const double h_lv = rho_l * cpl * dT / (rho_v * ja);
1544 s.setAdvection(
false);
1545 s.setPhysicalDomain({un.len(
N), un.len(ny), un.len(nz)}, {0.0, 0.0, 0.0}, {
N,
ny, nz});
1546 s.setRho(un.rho(rho_l));
1547 s.setMu(un.mu(1e-3));
1548 s.setDomainBc(0, 1, 0, 0, 0);
1549 s.setDomainBc(1, 3, 0, 0, 0);
1550 s.setPressureGeometry(std::vector<double>((std::size_t)
N * ny * nz, un.len(1.0)));
1551 const double X0 = x0p *
N;
1552 std::vector<double>
C((std::size_t)
N * ny * nz, 0.0),
T((std::size_t)
N * ny * nz, 0.0);
1553 for (
int z = 0;
z < nz; ++
z)
1554 for (
int y = 0;
y <
ny; ++
y)
1555 for (
int x = 0;
x <
N; ++
x) {
1556 const std::size_t
i = (std::size_t)x + (std::size_t)
y *
N + (std::size_t)z *
N * ny;
1557 C[
i] = std::fmin(1.0, std::fmax(0.0, (x + 1) -
X0));
1558 const double xp = (
x + 0.5) /
N;
1560 const double sv = xp / (2 * std::sqrt(alpha_l * t0)) - b * (1.0 - rr);
1561 T[
i] = dT - dT * std::erfc(
sv) / std::erfc(b * rr);
1567 {un.rho(rho_v), un.rho(rho_l - rho_v)});
1568 s.setPressureFcg(
true, 4000, 1e-10);
1569 s.addScalar(
"T", un.diff(al), 1, 60);
1570 s.setScalarBc(
"T", 0, 2, 0.0);
1571 auto farT = [&](
double tt) {
1572 const double sv = 1.0 / (2 * std::sqrt(alpha_l * tt)) - b * (1.0 - rr);
1573 return dT - dT * std::erfc(
sv) / std::erfc(b * rr);
1575 s.setScalarBc(
"T", 1, 2, farT(t0));
1577 s.enablePhaseChange(un.rho(rho_v), un.rho(rho_l), un.latent(h_lv));
1578 s.setPhaseChangeThermal(
"T", 0.0, un.cond(k_v), un.cond(k_l), 0.0);
1579 s.setPhaseChangeEnergy(un.rcp(rho_v * cpl), un.rcp(rho_l * cpl));
1581 for (
int n = 0;
n < nsteps; ++
n) {
1582 const double Xd =
b * std::sqrt(alpha_l / tcur) *
N;
1583 const double dt = std::fmin(cfl / std::fmax(Xd * (1.0 - rr), 1e-30), Fo / al);
1584 s.setDt(un.time(dt));
1585 s.setScalarBc(
"T", 1, 2, farT(tcur + dt));
1591 r.T =
s.getField(
"T");
1592 r.u =
s.getVelocity(0);
1593 r.p =
s.getPressure();
1594 r.d =
s.phaseChangeDiagnostics();
1598void gatePhaseChange() {
1599 std::printf(
"=== units_phase_change_scale_invariance ===\n");
1600 const double TOL = 1
e-12;
1609 const PcRun
a = runStefanUnits(32, ref, energy != 0,
true);
1610 const PcRun
b = runStefanUnits(32, big, energy != 0,
false);
1611 const double rC = relDiff(
a.C,
b.C), rT = relDiff(
a.T,
b.T);
1612 const double rL = relScalar(big.len(
a.layer),
b.layer);
1613 const double rM = relScalar(big.mdot(
a.d.mdotMean),
b.d.mdotMean);
1614 const double rA = relScalar(big.area(
a.d.area),
b.d.area);
1615 const double rAv = relScalar(big.area(
a.area),
b.area);
1616 const double rV = relScalar(big.vol(
a.d.removedVolume),
b.d.removedVolume);
1618 " Stefan (%s): C %.2e T %.2e layer %.2e mdot_mean %.2e interface_area %.2e "
1619 "vof_interface_area %.2e removed_volume %.2e\n",
1620 energy ?
"rho c_p T, order 2" :
"constant D ", rC, rT, rL, rM, rA, rAv, rV);
1622 CHECK(rC <= TOL && rT <= TOL && rL <= TOL);
1623 CHECK(rM <= TOL && rA <= TOL && rAv <= TOL && rV <= TOL);
1625 const double rq = relScalar(big.power(
a.d.qOperator),
b.d.qOperator);
1626 const double rh = relScalar(big.energy(
a.b.hOpen),
b.b.hOpen);
1627 const double rg = relScalar(big.power(
a.b.qGfm),
b.b.qGfm);
1628 std::printf(
" q_operator %.2e budget h_open %.2e q_gfm %.2e (carry %.3e vs %.3e)\n", rq,
1629 rh, rg, big.energy(
a.carryDep),
b.carryDep);
1630 CHECK(
a.d.qOperator != 0.0 &&
a.b.qGfm != 0.0);
1631 CHECK(rq <= TOL && rh <= TOL && rg <= TOL);
1632 CHECK(std::fabs(big.energy(
a.carryDep) -
b.carryDep) <=
1633 TOL * std::fabs(big.energy(
a.b.hOpen)) + 1e-300);
1639 const PcRun
a = runSuckingUnits(32, 12, ref);
1640 const PcRun
b = runSuckingUnits(32, 12, big);
1641 const double rC = relDiff(
a.C,
b.C), rT = relDiff(
a.T,
b.T);
1642 const double ru = relDiffScaled(
b.u,
a.u, big.vel(1.0)),
1643 rp = relDiffScaled(
b.p,
a.p, big.pres(1.0));
1644 const double rM = relScalar(big.mdot(
a.d.mdotMean),
b.d.mdotMean);
1645 std::printf(
" sucking interface, 12 full steps: C %.2e T %.2e u %.2e p %.2e mdot %.2e\n",
1646 rC, rT, ru, rp, rM);
1650 CHECK(rC <= 1e-11 && rT <= 1e-11 && ru <= 1e-11 && rp <= 1e-11 && rM <= 1e-11);
1656 const int nx = 16,
ny = 32, nz = 8;
1657 const double Lx = 1.6, Ly = 0.8, Lz = 0.4;
1659 s.setAdvection(
false);
1660 s.setPhysicalDomain({Lx, Ly, Lz}, {0.0, 0.0, 0.0}, {nx,
ny, nz});
1663 s.setPressureGeometry(std::vector<double>((std::size_t)nx * ny * nz, 1.0));
1665 std::vector<double>
C((std::size_t)nx * ny * nz);
1666 for (std::size_t i = 0;
i <
C.size(); ++
i)
1667 C[i] = std::fmin(1.0, std::fmax(0.0, (
double)(i % nx) + 1.0 - 5.3));
1669 const double aDefault =
s.vofInterfaceArea();
1671 const double aPlic =
s.vofInterfaceArea();
1673 " box-cell plane: vof_interface_area %.15g (default sheet), %.15g (plic) vs %.15g\n",
1674 aDefault, aPlic, Ly * Lz);
1675 CHECK(relScalar(aPlic, Ly * Lz) <= 1e-12);
1676 CHECK(relScalar(aDefault, Ly * Lz) <= 1e-12);
1683 s.setAdvection(
false);
1684 s.setPhysicalDomain({1.0, 0.25, 0.25}, {0.0, 0.0, 0.0}, {16, 4, 4});
1685 s.setPressureGeometry(std::vector<double>(16 * 4 * 4, 1.0));
1686 s.enablePhaseChange(1.0, 1000.0, 2.26e6);
1689 s.setMassFluxUniform(1e-3);
1690 }
catch (
const std::runtime_error&) {
1696 s.setMassFluxUniform(1e-3);
1697 const double kInt =
s.unitScales().mdotToInt();
1698 CHECK(relScalar(
s.getField(
"mdot")[0], 1e-3 * kInt) <= 1e-15);
1699 s.addScalar(
"T", 1e-7, 1, 10);
1700 s.setPhaseChangeThermal(
"T", 373.15, 0.025, 0.6, 0.0);
1701 s.setPhaseChangeMdotOperator(
true);
1702 std::vector<double>
C(16 * 4 * 4, 0.0);
1703 for (
int i = 0;
i < 16 * 4 * 4; ++
i)
1704 C[(std::size_t)
i] = std::fmin(1.0, std::fmax(0.0, (i % 16) + 1 - 6.5));
1708 s.applyPhaseChange(1e-4);
1709 }
catch (
const std::runtime_error&) {
1712 std::printf(
" refusals: set_mass_flux before the scales, operator mdot without rho c_p: %s\n",
1713 threw ?
"both raise" :
"MISSING");
1720 const std::string gate = argc > 1 ? argv[1] :
"identity";
1721 Kokkos::initialize(argc, argv);
1723 if (gate ==
"identity")
1725 else if (gate ==
"scale")
1727 else if (gate ==
"vof")
1729 else if (gate ==
"aniso")
1730 gateAnisoPoiseuille();
1731 else if (gate ==
"sphere")
1733 else if (gate ==
"tgv")
1735 else if (gate ==
"vofaniso")
1737 else if (gate ==
"scalar")
1739 else if (gate ==
"scalarsine")
1741 else if (gate ==
"phasechange")
1744 std::fprintf(stderr,
1745 "usage: test_units [identity|scale|vof|aniso|sphere|tgv|vofaniso|scalar|"
1746 "scalarsine|phasechange]\n");
1752 std::fprintf(stderr,
"%d failure(s)\n",
failures);
1755 std::printf(
"OK\n");
flow — host-facing Kokkos IBM Navier-Stokes solver (drop-in flow-style API).
std::string num(double v)
@ kAreaPlic
rung P0/P1: plicArea on the MYC normal. The old numbers.
Solver< Staggered > IbmSolver
static double maxAbs(const std::vector< double > &a)
static constexpr double DT
static constexpr double RHO
static constexpr double MU
static constexpr double F
static double relErr(const std::vector< double > &a, const std::vector< double > &b)
static constexpr double U
static constexpr double R
double sphereFracBox(double cx, double cy, double cz, double R, double x0, double y0, double z0, const double h[3], int sub=24)
Exact liquid fraction of the box cell [x0,x0+hx] x ... inside a sphere: midpoint-subsampled in x and ...
AnisoDrop runDropletAniso(const int n[3], const double h[3], double R, bool constantKappa, int steps)
n cells per axis on a box mesh of cell size h (physical). R is the PHYSICAL radius.
static constexpr double FX
static constexpr double SIGMA