57#include <Kokkos_Core.hpp>
72 std::fprintf(stderr, "CHECK failed: %s\n at %s:%d\n", #cond, __FILE__, __LINE__); \
77std::size_t idx(
int x,
int y,
int z,
int nx,
int ny) {
78 return (std::size_t)x + (std::size_t)y * nx + (std::size_t)z * (std::size_t)nx * ny;
83 const int n = 8,
g = 3;
84 const I3 e{
n + 2 *
g,
n + 2 *
g,
n + 2 *
g};
85 const long len = (long)
e.x *
e.y *
e.z;
88 peclet::flow::vof::buildOutsideMask(m, e, g,
I3{0, 0, 0},
I3{
n,
n,
n},
true,
false,
false);
89 auto h = Kokkos::create_mirror_view(m);
90 Kokkos::deep_copy(h, m);
91 long marked = 0, wrong = 0;
92 for (
int z = 0;
z <
e.z; ++
z)
93 for (
int y = 0;
y <
e.y; ++
y)
94 for (
int x = 0;
x <
e.x; ++
x) {
95 const bool want = (
y -
g < 0 ||
y -
g >=
n) || (z - g < 0 || z - g >= n);
96 const bool got =
h(
L3(x, y, z, e)) != 0
u;
99 marked += got ? 1 : 0;
101 std::printf(
"A outside mask: %ld cells marked, %ld disagreements with the global predicate\n",
108void fluxInertGate() {
109 const int n = 6,
g = 3;
110 const I3 e{
n + 2 *
g,
n + 2 *
g,
n + 2 *
g};
111 const long len = (long)
e.x *
e.y *
e.z;
112 SField c(
"c", len),
mx(
"mx", len), my(
"my", len), mz(
"mz", len), al(
"al", len);
114 auto hc = Kokkos::create_mirror_view(c);
115 auto hm = Kokkos::create_mirror_view(mx);
116 auto hn = Kokkos::create_mirror_view(my);
117 auto ho = Kokkos::create_mirror_view(mz);
118 auto ha = Kokkos::create_mirror_view(al);
119 unsigned seed = 12345u;
120 auto rnd = [&seed]() {
121 seed = seed * 1103515245u + 12345u;
122 return (
double)((seed >> 16) & 0x7fff) / 32767.0;
124 for (
long i = 0;
i < len; ++
i) {
126 const double a0 = rnd() - 0.5,
a1 = rnd() - 0.5,
a2 = rnd() - 0.5;
127 const double s = std::fabs(a0) + std::fabs(a1) + std::fabs(a2) + 1
e-12;
131 ha(i) = peclet::flow::vof::plicAlpha(hm(i), hn(i), ho(i), hc(i));
133 Kokkos::deep_copy(c, hc);
134 Kokkos::deep_copy(mx, hm);
135 Kokkos::deep_copy(my, hn);
136 Kokkos::deep_copy(mz, ho);
137 Kokkos::deep_copy(al, ha);
138 Kokkos::deep_copy(zero, (
unsigned char)0);
141 for (
int d = 0;
d < 3; ++
d) {
142 const long sd =
d == 0 ? 1 : (
d == 1 ?
e.x : (long)
e.x *
e.y);
144 Kokkos::parallel_reduce(
147 {
e.x - 1,
e.y - 1,
e.z - 1}),
148 KOKKOS_LAMBDA(
int x,
int y,
int z,
long& acc) {
149 const long p =
L3(x, y, z, e);
150 for (
int k = -3;
k <= 3; ++
k) {
151 const double a = 0.07 *
k;
162 std::printf(
"B wyFaceFluxBc vs wyFaceFlux with an all-zero mask: %ld bitwise disagreements\n",
174void budgetGate(
int nz,
int nSlug,
int nAfter) {
175 const int nx = 32,
ny = 32;
176 const double W = 1.0,
dt = 0.2;
178 s.setAdvection(
false);
182 for (
int f = 0;
f < 4; ++
f)
183 s.setDomainBc(f, 1, 0, 0, 0);
184 s.setDomainBc(4, 2, 0.0, 0.0, W);
185 s.setDomainBc(5, 3, 0, 0, 0);
186 s.setPressureGeometry(std::vector<double>((std::size_t)nx * ny * nz, 10.0));
188 s.setVof(std::vector<double>((std::size_t)nx * ny * nz, 0.0));
189 s.setField(
"w", std::vector<double>((std::size_t)nx * ny * nz, W));
190 s.setVofInflow(4, 1.0);
191 s.setVofBackflow(5, 0.0);
193 double budget0 = 0.0, drift = 0.0, minC = 1.0, maxC = 0.0,
ledger = 0.0, peak = 0.0;
194 std::vector<double> peakProfile;
195 for (
int i = 0;
i < nSlug + nAfter; ++
i) {
197 s.setVofInflow(4, 0.0);
199 const auto d =
s.vofDiagnostics();
200 const auto v =
s.vofBcVolumes();
201 for (
int f = 0;
f < 6; ++
f)
203 const double budget =
d.sumC -
ledger;
206 drift = std::fmax(drift, std::fabs(budget - budget0));
207 minC = std::fmin(minC,
d.minC);
208 maxC = std::fmax(maxC,
d.maxC);
211 const auto C =
s.getVof();
212 peakProfile.assign(nz, 0.0);
213 for (
int z = 0;
z < nz; ++
z) {
215 for (
int y = 0;
y <
ny; ++
y)
216 for (
int x = 0;
x < nx; ++
x)
217 a += C[idx(x, y, z, nx, ny)];
218 peakProfile[
z] =
a / (nx *
ny);
223 int plateau = 0, edges = 0;
224 for (
double v : peakProfile) {
231 "C slug at its fullest: sum(C) %.6g over %d full planes + %d partial (expected "
232 "length %.4g cells)\n",
233 peak, plateau, edges, peak / (nx * ny));
235 const auto d =
s.vofDiagnostics();
236 const auto tot =
s.vofBcVolumesTotal();
237 const double injected =
tot[4];
238 const double rel = (injected > 0.0) ? drift / injected : drift;
240 "C budget: injected %.10g, left %.10g, remaining sum(C) %.3e; |budget drift| %.3e "
242 injected, -tot[5],
d.sumC, drift, rel);
243 std::printf(
"C C in [%.3e, %.17g]; max|div(open u)| of the prescribed field %.3e\n", minC, maxC,
244 s.maxOpenDivergence());
246 std::printf(
"C in %.10g vs out %.10g (relative closure %.3e)\n", injected, -tot[5],
247 std::fabs(injected + tot[5]) / injected);
249 CHECK(minC > -1e-12);
250 CHECK(maxC < 1.0 + 1e-12);
251 CHECK(
d.sumC < 1e-9 * injected);
252 CHECK(injected > 0.0);
253 CHECK(std::fabs(injected + tot[5]) / injected < 1e-9);
257void propGhostGate() {
258 const int nx = 8,
ny = 8, nz = 16;
259 const double rhoG = 1.0, rhoL = 1000.0;
261 s.setAdvection(
false);
265 for (
int f = 0;
f < 4; ++
f)
266 s.setDomainBc(f, 1, 0, 0, 0);
267 s.setDomainBc(4, 2, 0.0, 0.0, 0.5);
268 s.setDomainBc(5, 3, 0, 0, 0);
269 s.setPressureGeometry(std::vector<double>((std::size_t)nx * ny * nz, 10.0));
270 s.setPressureChebyshev(
true, 400, 1e-12);
272 s.setVof(std::vector<double>((std::size_t)nx * ny * nz, 0.0));
274 {rhoG, rhoL - rhoG, 0.0, 0.0});
275 s.setVofInflow(4, 1.0);
278 auto rv =
s.fieldView(
"rho");
279 auto h = Kokkos::create_mirror_view(rv);
280 Kokkos::deep_copy(h, rv);
281 const int G = 2, ex = nx + 2 *
G, ey =
ny + 2 *
G;
282 auto at = [&](
int x,
int y,
int z) {
return h(idx(x, y, z, ex, ey)); };
283 const double ghost = at(
G + nx / 2,
G + ny / 2,
G - 1);
284 const double inner = at(
G + nx / 2,
G + ny / 2,
G);
286 std::printf(
"D inflow -z: rho_ghost %.6g (want %.6g), rho_inner %.6g, rho_face %.6g\n", ghost,
288 CHECK(std::fabs(ghost - rhoL) < 1e-10 * rhoL);
290 std::printf(
"D the Neumann-copy face density would have been %.6g (ratio %.4g)\n", inner,
292 CHECK(face / inner > 100.0);
302 const int n = 8,
g = 3;
303 const I3 e{
n + 2 *
g,
n + 2 *
g,
n + 2 *
g};
304 const long len = (long)
e.x *
e.y *
e.z;
305 for (
int side = 0;
side < 2; ++
side) {
307 Kokkos::deep_copy(c, 1.0);
308 auto hw = Kokkos::create_mirror_view(w);
309 Kokkos::deep_copy(hw, 0.0);
312 const int fa = (
side == 0) ? (g - 1) : (
e.z -
g - 1);
313 for (
int y = 0;
y <
e.y; ++
y)
314 for (
int x = 0;
x <
e.x; ++
x)
315 hw(
L3(x, y, fa, e)) = (
x <
e.x / 2) ? -0.3 : +0.3;
316 Kokkos::deep_copy(w, hw);
318 auto hc = Kokkos::create_mirror_view(c);
319 Kokkos::deep_copy(hc, c);
321 const int inwardHalf = (
side == 0) ? 1 : 0;
322 long wrong = 0, layers = 0;
323 const int lo = (
side == 0) ? 0 : (
e.
z -
g);
324 for (
int y = 0;
y <
e.y; ++
y)
325 for (
int x = 0;
x <
e.x; ++
x) {
326 const bool inward = (
x >=
e.x / 2) == (inwardHalf == 1);
327 for (
int k = 0;
k <
g; ++
k) {
328 const double v = hc(
L3(x, y, lo + k, e));
329 if (v != (inward ? 0.0 : 1.0))
331 if (inward && v == 0.0)
336 "E backflow side %d: %ld cells disagree with the rule, %ld ghost cells took the "
337 "backflow colour (all %d layers)\n",
338 side, wrong, layers, g);
340 CHECK(layers == (
long)(
e.x / 2) *
e.y * g);
345void outflowVarGate() {
346 const int n = 8,
g = 2;
348 const long len = (long)
e.x *
e.y *
e.z;
350 auto hp = Kokkos::create_mirror_view(phi);
351 auto hr = Kokkos::create_mirror_view(rho);
352 for (
long i = 0;
i < len; ++
i) {
353 hp(i) = 0.001 * (double)((i * 37) % 101);
356 Kokkos::deep_copy(phi, hp);
357 Kokkos::deep_copy(rho, hr);
358 Kokkos::deep_copy(f0, 0.0);
359 Kokkos::deep_copy(f1, 0.0);
362 auto h0 = Kokkos::create_mirror_view(f0);
363 auto h1 = Kokkos::create_mirror_view(f1);
364 Kokkos::deep_copy(h0, f0);
365 Kokkos::deep_copy(h1, f1);
367 for (
long i = 0;
i < len; ++
i)
368 if (!(h0(i) == h1(i)))
371 "F bcCorrectOutflowVar at uniform rho == rho0: %ld bitwise disagreements with "
372 "bcCorrectOutflow\n",
377 for (
long i = 0;
i < len; ++
i)
379 Kokkos::deep_copy(rho, hr);
380 Kokkos::deep_copy(f1, 0.0);
382 Kokkos::deep_copy(h1, f1);
384 const long sa = (long)
e.x *
e.y;
385 for (
int y = g;
y <
e.y -
g; ++
y)
386 for (
int x = g;
x <
e.x -
g; ++
x) {
387 const long bf = (long)x + (
long)
y *
e.x + (long)(
e.z - g) * sa;
389 worst = std::fmax(worst, std::fabs(h1(bf) / h0(bf) - 1.0 / 1000.0));
391 std::printf(
"F at rho == 1000 rho0 the correction is scaled by 1/1000 to %.3e\n", worst);
392 CHECK(worst < 1e-14);
401double outflowFaceProbe(
bool corr,
double ratio,
double& divOut,
double& uInner,
double& divMut) {
402 const int nx = 32,
ny = 4, nz = 16;
403 const double rhoL = 100.0, rhoG = rhoL /
ratio;
405 s.setAdvection(
false);
408 s.setDt(ratio > 1.0 ? 0.1 : 0.2);
409 s.setDomainBc(4, 1, 0, 0, 0);
410 s.setDomainBc(5, 1, 0, 0, 0);
411 s.setDomainBc(0, 2, 0.1, 0, 0);
412 s.setDomainBc(1, 3, 0, 0, 0);
413 s.setVelocityIterations(60);
414 s.setPressureLevels(6);
415 s.setPressureGeometry(std::vector<double>((std::size_t)nx * ny * nz, 1e30));
418 std::vector<double>
C((std::size_t)nx * ny * nz, 0.0);
419 for (
int z = 0;
z < nz / 2; ++
z)
420 for (
int y = 0;
y <
ny; ++
y)
421 for (
int x = 0;
x < nx; ++
x)
422 C[idx(x, y, z, nx, ny)] = 1.0;
425 {rhoG, rhoL - rhoG, 0.0, 0.0});
427 s.setPressureFcg(
true, 400, 1e-12);
428 s.setOutflowRhoCorrection(corr);
429 for (
int i = 0;
i < 5; ++
i)
431 auto uv =
s.fieldView(
"u");
432 auto h = Kokkos::create_mirror_view(uv);
433 Kokkos::deep_copy(h, uv);
434 const int G = 2, ex = nx + 2 *
G, ey =
ny + 2 *
G;
435 const double face =
h(idx(
G + nx,
G + ny / 2,
G + nz - 1, ex, ey));
436 uInner =
h(idx(
G + nx - 1,
G + ny / 2,
G + nz - 1, ex, ey));
437 divOut =
s.maxOpenDivergenceProjected();
438 divMut =
s.maxOpenDivergence();
442void outflowReachedGate() {
443 for (
double ratio : {1.0, 10.0}) {
444 double d0 = 0, d1 = 0,
i0 = 0,
i1 = 0, m0 = 0,
m1 = 0;
445 const double on = outflowFaceProbe(
true, ratio, d0, i0, m0);
446 const double off = outflowFaceProbe(
false, ratio, d1, i1, m1);
448 "F2 ratio %-6g outflow face u: with the 1/rho_f factor %.17g, without %.17g "
450 ratio, on, off, on - off);
452 "F2 ratio %-6g last inner u %.17g -> the face is %s the zero-gradient copy; "
453 "max|div| projected %.3e / %.3e, mutating diagnostic %.3e / %.3e\n",
454 ratio, i0, (on == i0) ?
"STILL" :
"NOT", d0, d1, m0, m1);
469 CHECK(d1 > 1e3 * d0);
478std::vector<double>
sphereSdf(
int nx,
int ny,
int nz,
const std::vector<Sph>& sp) {
479 std::vector<double>
f((std::size_t)nx * ny * nz, 1e30);
480 for (
int z = 0;
z < nz; ++
z)
481 for (
int y = 0;
y <
ny; ++
y)
482 for (
int x = 0;
x < nx; ++
x) {
484 for (
const Sph& q : sp) {
485 const double dx =
x + 0.5 -
q.x,
dy =
y + 0.5 -
q.y,
dz =
z + 0.5 -
q.z;
486 d = std::fmin(d, std::sqrt(dx * dx + dy * dy + dz * dz) -
q.r);
488 f[idx(x, y, z, nx, ny)] =
d;
496void composedGate(
bool cutOutlet,
int nSlug,
int nAfter) {
497 const int nx = 20,
ny = 20, nz = 32;
498 const double W = 1.0;
500 s.setAdvection(
false);
504 for (
int f = 0;
f < 4; ++
f)
505 s.setDomainBc(f, 1, 0, 0, 0);
506 s.setDomainBc(4, 2, 0.0, 0.0, W);
507 s.setDomainBc(5, 3, 0, 0, 0);
509 std::vector<Sph>
sp{{6, 6, 12, 4.0}, {14, 14, 12, 4.0}, {6, 14, 21, 4.0}, {14, 6, 21, 4.0}};
511 sp.push_back({10, 10, (double)nz, 4.5});
513 s.setVelocityIterations(60);
514 s.setPressureLevels(4);
515 s.setPressureIterations(400);
523 s.setPressureExactResidual(
true);
524 for (
int i = 0;
i < 80; ++
i)
526 const double div =
s.maxOpenDivergenceProjected();
527 const long pit =
s.lastPressureIterations();
532 std::vector<double> c0((std::size_t)nx * ny * nz, 0.0);
533 for (
int z = nz / 2;
z < nz; ++
z)
534 for (
int y = 0;
y <
ny; ++
y)
535 for (
int x = 0;
x < nx; ++
x)
536 c0[idx(x, y, z, nx, ny)] = 1.0;
538 s.setVofInflow(4, 1.0);
539 s.setVofBackflow(5, 0.0);
540 const double vol0 =
s.vofDiagnostics().volume;
541 double budget0 = 0.0, drift = 0.0, solidSum = 0.0,
ledger = 0.0, clip = 0.0;
542 double mnF = 1e30, mxF = -1e30;
549 for (
int i = 0;
i < nSlug + nAfter; ++
i) {
550 const double c =
s.vofMaxCourant();
551 dtA = (
c > 0.0) ? std::fmin(0.25, dtA * 0.12 / c) : 0.25;
554 s.setVofInflow(4, 0.0);
556 const auto d =
s.vofDiagnostics();
557 const auto v =
s.vofBcVolumes();
558 for (
int f = 0;
f < 6; ++
f)
560 const double budget =
d.volume -
ledger;
563 drift = std::fmax(drift, std::fabs(budget - budget0));
564 solidSum = std::fmax(solidSum, std::fabs(
d.solidSumC));
565 clip +=
d.clippedVolume;
566 mnF = std::fmin(mnF,
d.minCFluid);
567 mxF = std::fmax(mxF,
d.maxCFluid);
569 const auto d1 =
s.vofDiagnostics();
570 const auto tot =
s.vofBcVolumesTotal();
571 const double injected =
tot[4];
572 const double rel = drift / (vol0 + injected);
574 "G %s: %d solid + %d cut cells, max|div(open u)| %.3e (pressure %ld), final dt %.4g\n"
575 " sum(eps_eff C) %.10g -> %.10g; injected %.10g, LEFT %.10g\n"
576 " |budget drift| %.3e (rel %.3e), max|sum C over solid| %.3e, clipped %.3e,\n"
577 " C over uncut fluid in [%.3e, %.17g]\n",
578 cutOutlet ?
"a sphere CUTS the outlet plane" :
"packing clear of both planes", d1.solidCells,
579 d1.cutCells, div, pit, dtA, vol0, d1.volume, injected, -tot[5], drift, rel, solidSum, clip,
583 CHECK(solidSum == 0.0);
584 CHECK(injected > 0.0);
585 CHECK(-tot[5] > 0.1 * vol0);
586 CHECK(mnF >= -1e-12);
587 CHECK(mxF <= 1.0 + 1e-12);
589 CHECK(d1.cutCells > 0);
594int main(
int argc,
char** argv) {
595 Kokkos::initialize(argc, argv);
600 outflowReachedGate();
603 const bool quick = std::getenv(
"PECLET_VOF_BC_QUICK") !=
nullptr;
604 budgetGate(quick ? 32 : 64, quick ? 40 : 100, quick ? 200 : 400);
605 composedGate(
false, quick ? 20 : 40, quick ? 120 : 300);
606 composedGate(
true, quick ? 20 : 40, quick ? 120 : 300);
613 std::fprintf(stderr,
"%d failure(s)\n",
failures);
flow — host-facing Kokkos IBM Navier-Stokes solver (drop-in flow-style API).
void zero(const ScalarVec &y)
Kokkos::View< unsigned char *, SMem > UCField
double wyFaceFluxBc(double a, long p, long sd, int dir, const SF &c, const SF &mx, const SF &my, const SF &mz, const SF &alpha, const MF &outside, double eps=0.0)
WO-R sibling of wyFaceFlux for a face whose DONOR lies outside the global domain.
void bcColourBackflow(SField f, I3 e, int g, int a, int s, SField un, double backflow)
inletOutlet on an outflow face: where the boundary face velocity points INTO the domain,...
void buildOutsideMask(UCField m, I3 e, int g, I3 o, I3 gs, bool px, bool py, bool pz)
1 for every ghost cell of the block that lies OUTSIDE the global domain on a non-periodic axis,...
double wyFaceFlux(double a, long p, long sd, int dir, const SF &c, const SF &mx, const SF &my, const SF &mz, const SF &alpha, double eps=0.0)
Signed Eulerian donor-cell flux through the dir-face between cell p and cell p + sd,...
void bcCorrectOutflow(BField f, BField phi, B3 ext, int g, int a, double wa=1.0)
Kokkos::MDRangePolicy< Exec, Kokkos::Rank< 3, Kokkos::Iterate::Left, Kokkos::Iterate::Left > > MDRange3
3-D range, x (the first index) fastest (both the tile order and the order within a tile).
Kokkos::View< double *, SMem > SField
long L3(int x, int y, int z, I3 e)
Kokkos::View< double *, BMem > BField
Kokkos::DefaultExecutionSpace SExec
void bcCorrectOutflowVar(BField f, BField phi, BField rho, double rho0, B3 ext, int g, int a, bool harmonic, double wa=1.0)
Solver< Staggered > IbmSolver
static std::vector< double > sphereSdf()
Kokkos::View< double *, SMem > SField
long L3(int x, int y, int z, I3 e)
static std::vector< double > ledger(IbmSolver &s)