flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_balanced_force.cpp
Go to the documentation of this file.
1// The balanced-force projection on the STAGGERED grid (doc/collocated_varrho_forces.md §4.6-4.7,
2// gate G8).
3//
4// `set_balanced_force_projection(True)` solves, once per step before the momentum predictor,
5// D(O c w G X) = D(O c beta) with the projection's own operator (c = rho0/rho_f, beta = the
6// predictor's own face force) and moves X into P. A gradient force is then balanced by the
7// pressure BEFORE the implicit momentum operator A = rho_f/dt - mu*Lap acts, so the static
8// balances the staggered predictor only APPROACHES at mu > 0 (WO-P's mu*dt^2 residue: A does not
9// commute with the discrete gradient at variable rho or next to a wall or a solid) are exact from
10// the first step. The option is state-independent, so it changes neither stability nor the
11// converged steady state; OFF is byte-identical (gated by the test_vof_* / test_vardensity_*
12// outputs and tests/regression/state_hash.py, not here).
13//
14// (b) T2 staggered drop, constant kappa, ratio 1 / 10 / 100 / 1000 and the ratio-1000 mu sweep:
15// face < 1e-13 after 30 steps.
16// (c) hydrostatic column, walled, ratio 1000, mu in {0, 1e-3, 1e-2, 1e-1}: dP/dz < 1e-10 after
17// ONE step, face < 1e-12.
18// (d) low-Ca walled drop, 32^3, walls on every face, R = 8, kappa = 0.25, sigma = 0.01, mu = 10,
19// dt = 0.5 capillary_dt: face < 1e-13 ON; OFF reported.
20// (e) immersed solids: (i) a ratio-1000 walled column with a solid sphere (R = 5) crossing the
21// interface, mu = 0.1: face < 1e-12 and dP/dz between fluid cells < 1e-10 after 30 steps;
22// (ii) the drop of (d) cut by an immersed plane 0.5 R below its centre, ratio 1 and 1000,
23// mu = 0.1: face < 1e-12. OFF reported.
24// (f) refusals: porous, ghost, block CSF, inflow/outflow, non-incremental -> named errors; the
25// V8 DEFAULT with an inflow/outflow face resolves OFF (notice) instead of throwing.
26#include <cmath>
27#include <cstdio>
28#include <functional>
29#include <Kokkos_Core.hpp>
30#include <memory>
31#include <stdexcept>
32#include <string>
33#include <vector>
34
35#include "flow_ibm.hpp"
36
37namespace {
41
42int failures = 0;
43#define CHECK(cond) \
44 do { \
45 if (!(cond)) { \
46 std::fprintf(stderr, "CHECK failed: %s\n at %s:%d\n", #cond, __FILE__, __LINE__); \
47 ++failures; \
48 } \
49 } while (0)
50
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;
53}
54double maxAbs(const std::vector<double>& v) {
55 double m = 0;
56 for (double x : v)
57 m = std::fmax(m, std::fabs(x));
58 return m;
59}
60template <class S>
61double maxFaceVel(S& s) {
62 return std::fmax(std::fmax(maxAbs(s.getFaceVelocity(0)), maxAbs(s.getFaceVelocity(1))),
63 maxAbs(s.getFaceVelocity(2)));
64}
65
66// Volume fractions of a sphere (the sampler of test_vof_collocated.cpp).
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) {
73 double acc = 0.0;
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);
78 if (r2 <= 0.0)
79 continue;
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);
82 if (hi > lo)
83 acc += hi - lo;
84 }
85 C[idx(i, j, k, n, n)] = acc / (sub * sub);
86 }
87 return C;
88}
89
90// ------------------------------------------------------------------ droplets: (b), (d), (e)(ii)
91struct DropCfg {
92 double ratio = 1.0, mu = 0.1, sigma = 1.0;
93 bool walls = false, plane = false;
94 int steps = 30;
95};
96
97double runDrop(const DropCfg& c, bool on, long* bfIters = nullptr) {
98 const int n = 32;
99 const double R = 8.0, cx = n / 2 + 0.13, cy = n / 2 + 0.27, cz = n / 2 + 0.11;
100 Stag s(n, n, n);
101 s.setAdvection(false);
102 s.setRho(c.ratio);
103 s.setMu(c.mu);
104 s.setDt(1.0);
105 s.setVelocityResidualTolerance(0.0);
106 if (c.walls)
107 for (int f = 0; f < 6; ++f)
108 s.setDomainBc(f, 1, 0, 0, 0);
109 if (c.plane) { // an immersed plane 0.5 R below the drop centre: solid below z = cz - R/2
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);
116 } else {
117 s.setPressureGeometry(std::vector<double>((std::size_t)n * n * n, 10.0));
118 }
119 s.setPressureChebyshev(true, 500, 1e-14);
120 s.enableVof();
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());
126 if (on)
127 s.setBalancedForceProjection(true);
128 s.setPressureChebyshev(true, 500, 1e-14);
129 long it = 0;
130 for (int k = 0; k < c.steps; ++k) {
131 s.step();
132 it = std::max(it, s.lastBalancedForceIterations());
133 }
134 if (bfIters)
135 *bfIters = it;
136 return maxFaceVel(s);
137}
138
139// ------------------------------------------------------------------ hydrostatic: (c), (e)(i)
140struct HydroResult {
141 double faceU = 0, pErr = 0;
142};
143
144HydroResult hydrostatic(double ratio, double mu, int steps, bool sphere, bool on) {
145 const int N = sphere ? 16 : 8, NZ = sphere ? 32 : 24;
146 const double g = 0.1;
147 Stag s(N, N, NZ);
148 s.setAdvection(false);
149 s.setRho(1.0);
150 s.setMu(mu);
151 s.setDt(1.0);
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);
156 if (sphere)
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;
162 }
163 if (sphere)
164 s.setSolid(sdf, true);
165 else
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;
173 s.addField("rho");
174 s.setField("rho", fld);
175 s.setDensityMode(true);
176 s.setPropertyModel("force_z", ClosureKind::LinearMix, "rho", "", std::vector<double>{0.0, -g});
177 if (on)
178 s.setBalancedForceProjection(true);
179 for (int k = 0; k < steps; ++k)
180 s.step();
181 HydroResult r;
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)
188 continue; // P inside the solid is not a fluid pressure
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));
192 }
193 return r;
194}
195
196void gateDrop() {
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}) {
199 DropCfg c;
200 c.ratio = ratio;
201 long it = 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,
204 it);
205 CHECK(on < 1e-13);
206 }
207 for (double mu : {0.0, 0.01, 0.1}) {
208 DropCfg c;
209 c.ratio = 1000.0;
210 c.mu = mu;
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);
213 CHECK(on < 1e-13);
214 }
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}) {
217 DropCfg c;
218 c.ratio = ratio;
219 c.mu = 10.0;
220 c.sigma = 0.01;
221 c.walls = true;
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);
224 CHECK(on < 1e-13);
225 }
226 std::printf(
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}) {
229 DropCfg c;
230 c.ratio = ratio;
231 c.plane = true;
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);
234 CHECK(on < 1e-12);
235 }
236}
237
238void gateHydro() {
239 std::printf(
240 "\n=== (c) hydrostatic column, walled, ratio 1000: ON after ONE step (OFF 100 steps)\n");
241 for (double mu : {0.0, 1e-3, 1e-2, 1e-1}) {
242 const auto off = hydrostatic(1000.0, mu, 100, false, false);
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);
248 }
249 std::printf(
250 "\n=== (e)(i) ratio-1000 column with a solid sphere (R = 5) across the interface, "
251 "mu 0.1, 30 steps\n");
252 const auto off = hydrostatic(1000.0, 0.1, 30, true, false);
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);
258}
259
260void expectThrow(const char* what, const std::function<void()>& f) {
261 bool threw = false;
262 std::string msg;
263 try {
264 f();
265 } catch (const std::exception& e) {
266 threw = true;
267 msg = e.what();
268 }
269 std::printf(" %-34s -> throws: %s%s%.90s\n", what, threw ? "yes" : "NO", threw ? " " : "",
270 msg.c_str());
271 CHECK(threw);
272}
273
274void gateRefusals() {
275 std::printf("\n=== (f) refusals\n");
276 const int N = 16;
277 const std::vector<double> fluid((std::size_t)N * N * N, 10.0);
278 auto base = [&](auto& s) {
279 s.setAdvection(false);
280 s.setRho(1.0);
281 s.setMu(0.01);
282 s.setDt(1.0);
283 };
284 expectThrow("porous continuity", [&] {
285 Stag s(N, N, N);
286 base(s);
287 s.setPressureGeometry(fluid);
288 s.setPorousContinuity(true);
289 s.setBalancedForceProjection(true);
290 s.step();
291 });
292 expectThrow("ghost projection", [&] {
293 Stag s(N, N, N);
294 base(s);
295 s.setGhostProjection(true);
296 s.setBalancedForceProjection(true);
297 s.setPressureGeometry(fluid);
298 s.step();
299 });
300 expectThrow("block CSF", [&] {
301 Stag s(N, N, N);
302 base(s);
303 s.setPressureGeometry(fluid);
304 s.enableVof();
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);
309 s.step();
310 });
311 expectThrow("inflow/outflow face", [&] {
312 Stag s(N, N, N);
313 base(s);
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);
318 s.step();
319 });
320 // The V8 DEFAULT with an inflow/outflow face resolves OFF (a one-time stderr notice), and the
321 // step runs; only an EXPLICIT ON with an open face throws the named error.
322 {
323 Colo s(N, N, N);
324 base(s);
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); // V8: the option's default would be ON
329 bool threw = false;
330 try {
331 s.step();
332 s.step();
333 } catch (const std::exception& e) {
334 threw = true;
335 std::printf(" V8 default + open face threw: %s\n", e.what());
336 }
337 std::printf(" V8 default + inflow/outflow: %s, option %s\n", threw ? "THREW" : "ran",
338 s.balancedForceProjection() ? "ON" : "OFF");
339 CHECK(!threw);
340 CHECK(!s.balancedForceProjection());
341 }
342 expectThrow("V8 + inflow/outflow face, explicit ON", [&] {
343 Colo s(N, N, N);
344 base(s);
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);
350 s.step();
351 });
352 expectThrow("non-incremental pressure", [&] {
353 Stag s(N, N, N);
354 base(s);
355 s.setPressureGeometry(fluid);
356 s.setIncrementalPressure(false);
357 s.setBalancedForceProjection(true);
358 s.step();
359 });
360}
361
362} // namespace
363
364int main(int argc, char** argv) {
365 Kokkos::initialize(argc, argv);
366 {
367 gateRefusals();
368 gateHydro();
369 gateDrop();
370 }
371 Kokkos::finalize();
372 if (failures) {
373 std::fprintf(stderr, "\n%d CHECK(s) failed\n", failures);
374 return 1;
375 }
376 std::printf("\nAll staggered balanced-force projection gates passed.\n");
377 return 0;
378}
flow — host-facing Kokkos IBM Navier-Stokes solver (drop-in flow-style API).
y
Definition cmp.py:24
int m
Definition post.py:35
hydrostatic(ratio, mu=0.0, steps=100, g=0.1, dt=1.0, N=8, NZ=24)
static constexpr int NZ
static constexpr int N
#define CHECK(cond)
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