50#include <Kokkos_Core.hpp>
66 std::fprintf(stderr, "CHECK failed: %s\n at %s:%d\n", #cond, __FILE__, __LINE__); \
71std::size_t idx(
int x,
int y,
int z,
int nx,
int ny) {
72 return (std::size_t)x + (std::size_t)y * nx + (std::size_t)z * (std::size_t)nx * ny;
74double maxAbs(
const std::vector<double>& v) {
77 m = std::fmax(m, std::fabs(x));
80double maxAbsDiff(
const std::vector<double>& a,
const std::vector<double>& b) {
85 for (std::size_t i = 0;
i <
a.size(); ++
i) {
86 const double d = std::fabs(a[i] - b[i]);
93double sum(
const std::vector<double>& v) {
110 const double h = 1.0 /
N,
dt = 0.1 *
h,
T = 3.0;
113 const double dtSolver =
dt /
h;
114 const int steps = 20;
123 const I3 eR =
ref.extent();
124 const int gR =
ref.ghost();
129 for (
int s = 0;
s <
steps; ++
s) {
133 auto refHost = Kokkos::create_mirror_view(
ref.colour());
134 Kokkos::deep_copy(refHost,
ref.colour());
141 s.setAdvection(
false);
147 s.setVelocityResidualTolerance(0.0);
148 s.setPressureGeometry(std::vector<double>((std::size_t)
N *
N *
N, 10.0));
155 auto sh = Kokkos::create_mirror_view(seed.
colour());
156 Kokkos::deep_copy(sh, seed.
colour());
158 const int gS = seed.
ghost();
159 std::vector<double> c0((std::size_t)
N *
N *
N);
160 for (
int z = 0;
z <
N; ++
z)
161 for (
int y = 0;
y <
N; ++
y)
162 for (
int x = 0;
x <
N; ++
x)
163 c0[idx(x, y, z,
N,
N)] =
sh(
L3(x + gS, y + gS, z + gS, eS));
170 const int gP =
probe.ghost();
172 std::vector<double> uu((std::size_t)
N *
N *
N), vv(uu.size()), ww(uu.size());
175 auto hu = Kokkos::create_mirror_view(
probe.faceU());
176 auto hv = Kokkos::create_mirror_view(
probe.faceV());
177 auto hw = Kokkos::create_mirror_view(
probe.faceW());
178 Kokkos::deep_copy(hu,
probe.faceU());
179 Kokkos::deep_copy(hv,
probe.faceV());
180 Kokkos::deep_copy(hw,
probe.faceW());
181 for (
int z = 0;
z <
N; ++
z)
182 for (
int y = 0;
y <
N; ++
y)
183 for (
int x = 0;
x <
N; ++
x) {
185 const int xm = (
x +
N - 1) %
N, ym = (y +
N - 1) %
N, zm = (
z +
N - 1) %
N;
186 uu[idx(x, y, z,
N,
N)] = hu(
L3(xm + gP, y + gP, z + gP, eP));
187 vv[idx(x, y, z,
N,
N)] = hv(
L3(x + gP, ym + gP, z + gP, eP));
188 ww[idx(x, y, z,
N,
N)] = hw(
L3(x + gP, y + gP, zm + gP, eP));
195 const auto cs =
s.getVof();
196 double dmax = 0.0,
l1 = 0.0;
197 for (
int z = 0;
z <
N; ++
z)
198 for (
int y = 0;
y <
N; ++
y)
199 for (
int x = 0;
x <
N; ++
x) {
200 const double d =
cs[idx(x, y, z,
N,
N)] - refHost(
L3(x +
gR, y +
gR, z +
gR, eR));
201 dmax = std::fmax(dmax, std::fabs(d));
204 std::printf(
"A bridge (LeVeque, %d steps): max|C_solver - C_standalone| %.3e L1 %.3e\n", steps,
242double hydrostaticC(
double ratio,
bool freeze,
bool harmonic,
int steps,
double g,
double dt) {
243 const int N = 8,
NZ = 24;
245 s.setAdvection(
false);
250 s.setVelocityResidualTolerance(0.0);
252 s.setDomainBc(4, 1, 0, 0, 0);
253 s.setDomainBc(5, 1, 0, 0, 0);
254 s.setPressureGeometry(std::vector<double>((std::size_t)
N *
N *
NZ, 10.0));
255 std::vector<double> c0((std::size_t)
N *
N *
NZ);
256 for (
int z = 0;
z <
NZ; ++
z)
257 for (
int y = 0;
y <
N; ++
y)
258 for (
int x = 0;
x <
N; ++
x)
259 c0[idx(x, y, z,
N,
N)] = (
z <
NZ / 2) ? 1.0 : 0.0;
263 s.exchangeField(
"C");
271 s.setRhoFaceHarmonic(
true);
275 last = std::fmax(
maxAbs(
s.getVelocity(0)),
277 CHECK(!std::isnan(last));
278 if (std::isnan(last))
282 const auto cNow = freeze ? c0 :
s.getVof();
283 const auto p =
s.getPressure();
285 const int xc =
N / 2, yc =
N / 2;
286 for (
int z = 1;
z <
NZ; ++
z) {
287 const double dp =
p[idx(xc, yc, z,
N,
N)] -
p[idx(xc, yc, z - 1,
N,
N)];
288 const double ra = 1.0 + (
ratio - 1.0) * cNow[idx(xc, yc, z,
N,
N)];
289 const double rb = 1.0 + (
ratio - 1.0) * cNow[idx(xc, yc, z - 1,
N,
N)];
290 perr = std::fmax(perr, std::fabs(dp + g * 0.5 * (ra + rb)) / (g * ratio));
292 std::printf(
"B ratio %-6g %-14s: steady max|u| %.3e dP/dz rel-err %.3e\n", ratio,
293 harmonic ?
"HARMONIC rho_f" : (freeze ?
"frozen C" :
"free C"), last, perr);
296 return freeze ? last :
perr;
301double hydrostaticRhoRef(
double ratio,
int steps,
double g,
double dt) {
302 const int N = 8,
NZ = 24;
304 s.setAdvection(
false);
308 s.setDomainBc(4, 1, 0, 0, 0);
309 s.setDomainBc(5, 1, 0, 0, 0);
310 s.setPressureGeometry(std::vector<double>((std::size_t)
N *
N *
NZ, 10.0));
311 std::vector<double>
rho((std::size_t)
N *
N *
NZ);
312 for (
int z = 0;
z <
NZ; ++
z)
313 for (
int y = 0;
y <
N; ++
y)
314 for (
int x = 0;
x <
N; ++
x)
315 rho[idx(x, y, z,
N,
N)] = (
z <
NZ / 2) ? ratio : 1.0;
317 s.setField(
"rho", rho);
318 s.setDensityMode(
true);
323 last = std::fmax(
maxAbs(
s.getVelocity(0)),
330void conservation(
int steps) {
332 const double dt = 1.0, amp = 5
e-4,
ratio = 10.0;
337 s.setAdvection(
true);
338 s.setPressureGeometry(std::vector<double>((std::size_t)
N *
N *
N, 10.0));
340 std::vector<double> c0((std::size_t)
N *
N *
N),
fx(c0.size());
341 for (
int z = 0;
z <
N; ++
z)
342 for (
int y = 0;
y <
N; ++
y)
343 for (
int x = 0;
x <
N; ++
x) {
344 const double dx =
x + 0.5 -
N / 2.0,
dy =
y + 0.5 -
N / 2.0,
dz =
z + 0.5 -
N / 2.0;
345 const double r = std::sqrt(dx * dx + dy * dy + dz * dz);
346 c0[idx(x, y, z,
N,
N)] = std::fmin(1.0, std::fmax(0.0, 0.5 - (r -
N / 5.0)));
347 fx[idx(x, y, z,
N,
N)] = amp * std::sin(2 * M_PI * (z + 0.5) /
N);
353 s.setField(
"force_x", fx);
354 const double v0 = sum(
s.getVof());
355 double dmax = 0.0, cfl = 0.0, peak = 0.0, drift = 0.0;
358 dmax = std::fmax(dmax, std::fabs(
s.maxOpenDivergence()));
359 cfl = std::fmax(cfl,
s.vofLastCourant());
360 drift = (sum(
s.getVof()) -
v0) / v0;
361 peak = std::fmax(peak, std::fabs(drift));
363 const auto d =
s.vofDiagnostics();
365 "C conservation (%d coupled steps, ratio %g): dV/V final %+.3e PEAK %.3e "
366 "max|div(open u)| %.2e max cfl %.4f C in [%.2e, %.10f] mixed %ld\n",
367 steps, ratio, drift, peak, dmax, cfl,
d.minC,
d.maxC,
d.mixed);
374 CHECK(
d.maxC <= 1.0 + 1e-12 &&
d.minC > -1e-12);
378void constantColour() {
380 const std::size_t
n = (std::size_t)
N *
N *
N;
384 std::vector<double>
fx(n), ones(n, 1.0);
385 for (
int z = 0;
z <
N; ++
z)
386 for (
int y = 0;
y <
N; ++
y)
387 for (
int x = 0;
x <
N; ++
x)
388 fx[idx(x, y, z,
N,
N)] = 1
e-3 * std::sin(2 * M_PI * (z + 0.5) /
N);
393 s.setVelocityResidualTolerance(0.0);
394 s.setAdvection(
true);
395 s.setPressureGeometry(std::vector<double>(n, 10.0));
397 s.setField(
"force_x", fx);
402 std::vector<std::vector<double>>
u(2),
p(2);
403 std::vector<double> cEnd;
404 for (
int vofOn = 0; vofOn < 2; ++vofOn) {
409 for (
int it = 0;
it < 30; ++
it)
411 u[vofOn] =
s.getVelocity(0);
412 p[vofOn] =
s.getPressure();
417 for (
double c : cEnd)
418 dC = std::fmax(dC, std::fabs(c - 1.0));
421 "D1 uniform C stationary: max|C-1| %.3e\nD2 VoF inert (no closure): du %.3e dp %.3e "
423 dC, du, dp,
maxAbs(u[0]));
425 CHECK(du == 0.0 && dp == 0.0);
428 std::vector<std::vector<double>> uv(2), pv(2);
429 for (
int viaC = 0; viaC < 2; ++viaC) {
437 s.setField(
"rho", std::vector<double>(n, 2.0));
438 s.setDensityMode(
true);
440 for (
int it = 0;
it < 30; ++
it)
442 uv[viaC] =
s.getVelocity(0);
443 pv[viaC] =
s.getPressure();
446 std::printf(
"D3 rho(C==1) == hand-set uniform rho: du %.3e dp %.3e\n", duv, dpv);
447 CHECK(duv == 0.0 && dpv == 0.0);
456void interfaceLocalCfl() {
458 const double dt = 1.0;
460 s.setAdvection(
false);
464 s.setPressureGeometry(std::vector<double>((std::size_t)
N *
N *
N, 10.0));
466 std::vector<double> c0((std::size_t)
N *
N *
N),
fx(c0.size());
467 for (
int z = 0;
z <
N; ++
z)
468 for (
int y = 0;
y <
N; ++
y)
469 for (
int x = 0;
x <
N; ++
x) {
470 const double dx =
x + 0.5 -
N / 2.0,
dy =
y + 0.5 -
N / 2.0,
dz =
z + 0.5 -
N / 2.0;
471 c0[idx(x, y, z,
N,
N)] =
472 std::fmin(1.0, std::fmax(0.0, 0.5 - (std::sqrt(dx * dx + dy * dy + dz * dz) - 3.0)));
474 const double a = (
z + 0.5 - 3.0) / 1.5;
475 fx[idx(x, y, z,
N,
N)] = 2
e-3 * std::exp(-a * a);
480 s.setField(
"force_x", fx);
481 for (
int it = 0;
it < 40; ++
it)
483 const double local =
s.vofLastCourant();
485 for (
int c = 0;
c < 3; ++
c)
486 global = std::fmax(global,
maxAbs(
s.getVelocity(c)) * dt);
487 std::printf(
"F CFL: interface-local %.4f global %.4f (over-throttle factor %.2fx)\n", local,
488 global, global / std::fmax(local, 1e-300));
489 CHECK(local < 0.5 * global);
494int main(
int argc,
char** argv) {
495 Kokkos::initialize(argc, argv);
501 for (
double ratio : {3.0, 1000.0}) {
502 const double frozen = hydrostaticC(ratio,
true,
false, 100, 0.1, 1.0);
503 const double ref = hydrostaticRhoRef(ratio, 100, 0.1, 1.0);
504 std::printf(
"B2 ratio %-6g frozen-C chain vs hand-set rho: %.17g vs %.17g (%s)\n", ratio,
505 frozen, ref, frozen == ref ?
"BITWISE EQUAL" :
"DIFFER");
506 CHECK(frozen == ref);
507 CHECK(frozen < 1e-12);
508 hydrostaticC(ratio,
false,
false, 100, 0.1, 1.0);
523 perr = hydrostaticC(1000.0,
false,
true, 100, 0.1, 1.0);
524 std::printf(
"E harmonic rho_f dP/dz rel-err %.3e (arithmetic is the consistent mean)\n",
526 }
catch (
const std::exception& ex) {
528 std::printf(
"E harmonic rho_f: the ablation left the boundedness cap altogether — %s\n",
531 CHECK(blewUp || perr > 1e-3);
535 conservation(std::getenv(
"PECLET_VOF_TWOPHASE_LONG") ? 1000 : 200);
542 std::fprintf(stderr,
"%d failure(s)\n",
failures);
Weymouth-Yue split advection of a colour field on an extended (inner + ghost) block.
SField colour() const
Colour field on the extended block (x-fastest).
void init(int nx, int ny, int nz, double h, int ghost=3)
static constexpr int kVofG
static constexpr double defaultVofWispEps()
Weymouth-Yue split advection of a colour field on an extended (inner + ghost) block.
flow — host-facing Kokkos IBM Navier-Stokes solver (drop-in flow-style API).
Kokkos::View< double *, SMem > SField
long L3(int x, int y, int z, I3 e)
Solver< Staggered > IbmSolver
void fillLeVeque(WyAdvector &a, Block b, double h, double phase)
void periodicFill(SField f, I3 e, int g, bool px, bool py, bool pz)
Periodic wrap of a single-block extended field (serial reference path; the MPI path uses core's GridH...
void initSphere(SField c, Block b, double h, double cx, double cy, double cz, double r, int levels=4)
Sphere of fluid (C = 1 inside), exact to ~5e-7 relative volume at R/h = 16 via the octree helper in p...
Where this block sits in the global grid: o is the global index of inner cell (0,0,...
static double maxAbs(const std::vector< double > &a)
static double maxAbsDiff(const std::vector< double > &a, const std::vector< double > &b)
static void build(IbmSolver &s, std::size_t cells)
Kokkos::View< double *, SMem > SField
long L3(int x, int y, int z, I3 e)