29#include <Kokkos_Core.hpp>
46 std::fprintf(stderr, "CHECK failed: %s\n at %s:%d\n", #cond, __FILE__, __LINE__); \
51std::size_t idx(
int x,
int y,
int z,
int nx,
int ny) {
52 return (std::size_t)x + (std::size_t)y * nx + (std::size_t)z * (std::size_t)nx * ny;
54double maxAbs(
const std::vector<double>& v) {
57 m = std::fmax(m, std::fabs(x));
61double maxFaceVel(S& s) {
62 return std::fmax(std::fmax(
maxAbs(
s.getFaceVelocity(0)),
maxAbs(
s.getFaceVelocity(1))),
67std::vector<double> sphereC(
int n,
double R,
double cx,
double cy,
double cz,
int sub = 24) {
68 std::vector<double>
C((std::size_t)n * n * n, 0.0);
69 const double w = 1.0 /
sub;
70 for (
int k = 0;
k <
n; ++
k)
71 for (
int j = 0;
j <
n; ++
j)
72 for (
int i = 0;
i <
n; ++
i) {
74 for (
int b = 0;
b <
sub; ++
b)
75 for (
int a = 0;
a <
sub; ++
a) {
76 const double px =
i + (
a + 0.5) * w, py = j + (b + 0.5) *
w;
77 const double r2 =
R *
R - (px - cx) * (px - cx) - (py - cy) * (py - cy);
80 const double h = std::sqrt(r2);
81 const double lo = std::fmax(cz - h, (
double)k), hi = std::fmin(cz + h, (
double)k + 1);
92 double ratio = 1.0, mu = 0.1, sigma = 1.0;
93 bool walls =
false, plane =
false;
97double runDrop(
const DropCfg& c,
bool on,
long* bfIters =
nullptr) {
99 const double R = 8.0, cx =
n / 2 + 0.13, cy =
n / 2 + 0.27, cz =
n / 2 + 0.11;
101 s.setAdvection(
false);
105 s.setVelocityResidualTolerance(0.0);
107 for (
int f = 0;
f < 6; ++
f)
108 s.setDomainBc(f, 1, 0, 0, 0);
110 std::vector<double>
sdf((std::size_t)n * n * n);
111 for (
int z = 0;
z <
n; ++
z)
112 for (
int y = 0;
y <
n; ++
y)
113 for (
int x = 0;
x <
n; ++
x)
114 sdf[idx(x, y, z, n, n)] = (
z + 0.5) - (cz - 0.5 *
R);
115 s.setSolid(sdf,
true);
117 s.setPressureGeometry(std::vector<double>((std::size_t)n * n * n, 10.0));
119 s.setPressureChebyshev(
true, 500, 1e-14);
121 s.setVof(sphereC(n,
R, cx, cy, cz));
122 s.setPropertyModel(
"rho", ClosureKind::LinearMix,
"C",
"", {1.0,
c.ratio - 1.0});
123 s.setSurfaceTension(
c.sigma);
124 s.setVofKappaConstant(0.25);
125 s.setDt(0.5 *
s.capillaryDt());
127 s.setBalancedForceProjection(
true);
128 s.setPressureChebyshev(
true, 500, 1e-14);
130 for (
int k = 0;
k <
c.steps; ++
k) {
132 it = std::max(it,
s.lastBalancedForceIterations());
136 return maxFaceVel(s);
141 double faceU = 0, pErr = 0;
144HydroResult
hydrostatic(
double ratio,
double mu,
int steps,
bool sphere,
bool on) {
146 const double g = 0.1;
148 s.setAdvection(
false);
152 s.setVelocityResidualTolerance(0.0);
153 s.setDomainBc(4, 1, 0, 0, 0);
154 s.setDomainBc(5, 1, 0, 0, 0);
155 std::vector<double>
sdf((std::size_t)
N *
N *
NZ, 10.0);
157 for (
int z = 0;
z <
NZ; ++
z)
158 for (
int y = 0;
y <
N; ++
y)
159 for (
int x = 0;
x <
N; ++
x) {
160 const double dx =
x + 0.5 - 8.2,
dy =
y + 0.5 - 7.9,
dz =
z + 0.5 - 16.3;
161 sdf[idx(x, y, z,
N,
N)] = std::sqrt(dx * dx + dy * dy + dz * dz) - 5.0;
164 s.setSolid(sdf,
true);
166 s.setPressureGeometry(sdf);
167 auto heavy = [&](
int z) {
return z <
NZ / 2; };
168 std::vector<double> fld((std::size_t)
N *
N *
NZ);
169 for (
int z = 0;
z <
NZ; ++
z)
170 for (
int y = 0;
y <
N; ++
y)
171 for (
int x = 0;
x <
N; ++
x)
172 fld[idx(x, y, z,
N,
N)] = heavy(z) ?
ratio : 1.0;
174 s.setField(
"rho", fld);
175 s.setDensityMode(
true);
176 s.setPropertyModel(
"force_z", ClosureKind::LinearMix,
"rho",
"", std::vector<double>{0.0, -
g});
178 s.setBalancedForceProjection(
true);
179 for (
int k = 0;
k <
steps; ++
k)
182 r.faceU = maxFaceVel(s);
183 const auto p =
s.getPressure();
184 for (
int z = 1;
z <
NZ; ++
z)
185 for (
int y = 0;
y <
N; ++
y)
186 for (
int x = 0;
x <
N; ++
x) {
187 if (sdf[idx(x, y, z,
N,
N)] < 0.0 || sdf[idx(x, y, z - 1,
N,
N)] < 0.0)
189 const double dp =
p[idx(x, y, z,
N,
N)] -
p[idx(x, y, z - 1,
N,
N)];
190 const double rf = 0.5 * ((heavy(z) ?
ratio : 1.0) + (heavy(z - 1) ?
ratio : 1.0));
191 r.pErr = std::fmax(
r.pErr, std::fabs(dp + g * rf) / (g * ratio));
197 std::printf(
"\n=== (b) staggered drop, constant kappa, 30 steps: face |u| OFF -> ON\n");
198 for (
double ratio : {1.0, 10.0, 100.0, 1000.0}) {
202 const double off = runDrop(c,
false), on = runDrop(c,
true, &it);
203 std::printf(
" ratio %6g OFF %.3e ON %.3e (balanced-force iters max %ld)\n", ratio, off, on,
207 for (
double mu : {0.0, 0.01, 0.1}) {
211 const double off = runDrop(c,
false), on = runDrop(c,
true);
212 std::printf(
" ratio 1000 mu %-5g OFF %.3e ON %.3e\n", mu, off, on);
215 std::printf(
"\n=== (d) low-Ca walled drop (sigma 0.01, mu 10, walls on every face), 30 steps\n");
216 for (
double ratio : {1.0, 1000.0}) {
222 const double off = runDrop(c,
false), on = runDrop(c,
true);
223 std::printf(
" ratio %6g OFF %.3e (reported) ON %.3e\n", ratio, off, on);
227 "\n=== (e)(ii) constant-kappa sessile cap: the drop cut by an immersed plane, mu 0.1\n");
228 for (
double ratio : {1.0, 1000.0}) {
232 const double off = runDrop(c,
false), on = runDrop(c,
true);
233 std::printf(
" ratio %6g OFF %.3e (reported) ON %.3e\n", ratio, off, on);
240 "\n=== (c) hydrostatic column, walled, ratio 1000: ON after ONE step (OFF 100 steps)\n");
241 for (
double mu : {0.0, 1
e-3, 1
e-2, 1
e-1}) {
243 const auto on =
hydrostatic(1000.0, mu, 1,
false,
true);
244 std::printf(
" mu %-6g OFF face %.3e dP/dz %.3e | ON(1 step) face %.3e dP/dz %.3e\n", mu,
245 off.faceU,
off.pErr, on.faceU, on.pErr);
246 CHECK(on.faceU < 1e-12);
247 CHECK(on.pErr < 1e-10);
250 "\n=== (e)(i) ratio-1000 column with a solid sphere (R = 5) across the interface, "
251 "mu 0.1, 30 steps\n");
253 const auto on =
hydrostatic(1000.0, 0.1, 30,
true,
true);
254 std::printf(
" OFF face %.3e dP/dz %.3e (reported) | ON face %.3e dP/dz %.3e\n",
off.faceU,
255 off.pErr, on.faceU, on.pErr);
256 CHECK(on.faceU < 1e-12);
257 CHECK(on.pErr < 1e-10);
260void expectThrow(
const char* what,
const std::function<
void()>& f) {
265 }
catch (
const std::exception& e) {
269 std::printf(
" %-34s -> throws: %s%s%.90s\n", what, threw ?
"yes" :
"NO", threw ?
" " :
"",
275 std::printf(
"\n=== (f) refusals\n");
277 const std::vector<double>
fluid((std::size_t)
N *
N *
N, 10.0);
278 auto base = [&](
auto&
s) {
279 s.setAdvection(
false);
284 expectThrow(
"porous continuity", [&] {
287 s.setPressureGeometry(fluid);
288 s.setPorousContinuity(
true);
289 s.setBalancedForceProjection(
true);
292 expectThrow(
"ghost projection", [&] {
295 s.setGhostProjection(
true);
296 s.setBalancedForceProjection(
true);
297 s.setPressureGeometry(fluid);
300 expectThrow(
"block CSF", [&] {
303 s.setPressureGeometry(fluid);
305 s.enableVofBlocks({{{
N / 2.0,
N / 2.0,
N / 2.0, 4.0}}});
306 s.setSurfaceTension(0.01);
307 s.enableVofBlockCsf();
308 s.setBalancedForceProjection(
true);
311 expectThrow(
"inflow/outflow face", [&] {
314 s.setDomainBc(0, 2, 0.1, 0.0, 0.0);
315 s.setDomainBc(1, 3, 0, 0, 0);
316 s.setPressureGeometry(fluid);
317 s.setBalancedForceProjection(
true);
325 s.setDomainBc(0, 2, 0.1, 0.0, 0.0);
326 s.setDomainBc(1, 3, 0, 0, 0);
327 s.setPressureGeometry(fluid);
328 s.setDensityMode(
true);
333 }
catch (
const std::exception& e) {
335 std::printf(
" V8 default + open face threw: %s\n",
e.what());
337 std::printf(
" V8 default + inflow/outflow: %s, option %s\n", threw ?
"THREW" :
"ran",
338 s.balancedForceProjection() ?
"ON" :
"OFF");
340 CHECK(!
s.balancedForceProjection());
342 expectThrow(
"V8 + inflow/outflow face, explicit ON", [&] {
345 s.setDomainBc(0, 2, 0.1, 0.0, 0.0);
346 s.setDomainBc(1, 3, 0, 0, 0);
347 s.setPressureGeometry(fluid);
348 s.setDensityMode(
true);
349 s.setBalancedForceProjection(
true);
352 expectThrow(
"non-incremental pressure", [&] {
355 s.setPressureGeometry(fluid);
356 s.setIncrementalPressure(
false);
357 s.setBalancedForceProjection(
true);
364int main(
int argc,
char** argv) {
365 Kokkos::initialize(argc, argv);
373 std::fprintf(stderr,
"\n%d CHECK(s) failed\n",
failures);
376 std::printf(
"\nAll staggered balanced-force projection gates passed.\n");
flow — host-facing Kokkos IBM Navier-Stokes solver (drop-in flow-style API).
hydrostatic(ratio, mu=0.0, steps=100, g=0.1, dt=1.0, N=8, NZ=24)
static double maxAbs(const std::vector< double > &a)
peclet::flow::Solver< peclet::flow::Staggered > Stag
peclet::flow::Solver< peclet::flow::Colocated > Colo
static constexpr double R