78 """A plane at a generic angle, with exact PLIC fractions from the analytic plane->volume map."""
79 from itertools
import product
81 m = np.array([mx, my, mz], dtype=float)
83 alpha = 0.5 * m.sum() * n
84 c = np.zeros((n, n, n), order=
"F")
87 for i, j, k
in product(range(n), repeat=3):
88 c[i, j, k] =
_plic_volume(m[0], m[1], m[2], alpha - (m[0] * i + m[1] * j + m[2] * k))
93 """Fluid fraction of the unit cube under mx x + my y + mz z < alpha (independent oracle)."""
94 m = np.abs(np.array([mx, my, mz], dtype=float))
97 if [mx, my, mz][k] < 0:
101 return 1.0
if a > 0
else 0.0
110 return x * x * x
if x > 0
else 0.0
112 n1, n2, n3 = np.sort(m)
114 v -= cub(a - n1) + cub(a - n2) + cub(a - n3)
115 v += cub(a - n1 - n2) + cub(a - n1 - n3) + cub(a - n2 - n3)
116 v -= cub(a - n1 - n2 - n3)
117 den = 6.0 * n1 * n2 * n3
if n1 * n2 * n3 > 0
else 0.0
120 nz = [x
for x
in (n1, n2, n3)
if x > 1e-300]
122 return min(1.0, max(0.0, a / nz[0]))
125 f += (a * a) / (2 * p * q)
127 f -= ((a - p) ** 2) / (2 * p * q)
129 f -= ((a - q) ** 2) / (2 * p * q)
131 f += ((a - p - q) ** 2) / (2 * p * q)
132 return min(1.0, max(0.0, f))
133 return min(1.0, max(0.0, v / den))
137 """A sphere, sampled by 5-level octree subdivision (exact to ~1e-6 per cell)."""
139 cx = cy = cz = n * 0.5
140 c = np.zeros((n, n, n), order=
"F")
142 off = (np.arange(2**L) + 0.5) / 2**L
145 X = (i + off)[:,
None,
None] - cx
146 Y = (j + off)[
None, :,
None] - cy
148 Z = (np.arange(2**L) + 0.5) / 2**L + k - cz
149 d2 = X * X + Y * Y + Z[
None,
None, :] ** 2
150 c[i, j, k] = float((d2 < r * r).
mean())
158def make_solver(n, C, rho_g, rho_l, mu, dt, U, momentum, rho0=None):
160 s.set_rho(rho0
if rho0
is not None else rho_l)
163 s.set_advection(
False)
165 s.set_pressure_chebyshev(
True, PRESS_MAXIT, PRESS_RTOL)
168 s.set_property_model(
"rho",
"linear",
"C", [rho_g, rho_l - rho_g])
170 s.enable_vof_momentum(rho_g, rho_l)
171 one = np.ones((n, n, n), order=
"F")
172 s.set_state(U[0] * one, U[1] * one, U[2] * one)
185def gate_uniform(n=32, steps=200, scenes=(
"tilted",), ratios=(10.0, 1e2, 1e3, 1e4)):
186 """THE gate: uniform velocity at four density ratios, consistent vs inconsistent."""
188 dt = 0.2 / max(abs(x)
for x
in U)
189 print(
"\n=== GATE 1 — uniform-velocity consistency (n=%d, %d steps, dt=%g, CFL=%.2f) ==="
190 % (n, steps, dt, dt * max(abs(x)
for x
in U)))
191 print(
" U = %s, mu = 0, g = 0; error = max|u-U|/|U|_inf" % (U,))
194 print(
"\n scene: %s (sum C = %.6f)" % (scene, C.sum()))
195 print(
" %-10s | %-14s | %-14s | %s" % (
"ratio",
"consistent",
"inconsistent",
"pressure"))
199 for mom
in (
True,
False):
202 for _
in range(steps):
208 flag =
"" if all(x.ok()
for x
in watches)
else " ** INVALID (capped) **"
209 print(
" %-10g | %-14.4e | %-14.4e | %s%s"
210 % (R, row[0], row[1], watches[0], flag))
214 """The same identity on the ADVECTION alone — projection and momentum solve out of the picture.
215 This isolates whether the two advections truly share fluxes."""
217 dt = 0.2 / max(abs(x)
for x
in U)
218 print(
"\n=== GATE 1b — the consistency identity on the advection alone ===")
219 for scene
in (
"slab",
"tilted",
"droplet"):
224 for _
in range(steps):
227 for c, uc
in enumerate(U):
228 f = s.diagnostics.vof_advected_velocity(c)
229 e = max(e, float(np.max(np.abs(f - uc))))
230 errs.append(e / max(abs(x)
for x
in U))
231 d = s.diagnostics.vof_momentum_diagnostics()
232 print(
" %-8s R=%-8g err %.3e C^c in [%.4e, %.6f] floored %d min rho^c %.4g"
233 % (scene, R, errs[-1], min(d[
"min_Cc"]), max(d[
"max_Cc"]), d[
"floored"],
236 print(
" %-8s " % scene +
" ".join(
"R=%-6g %.3e" % (R, e)
for R, e
in zip(ratios, errs)))
240 """With C == const the consistent advection must be a SINGLE-PHASE transport: bitwise
241 independent of the density ratio (the rho_g/rho_l factors cancel), and volume-preserving."""
242 print(
"\n=== GATE 2 — C == const: the consistent scheme is density-ratio independent ===")
245 for val, name
in ((1.0,
"C=1 (all liquid)"), (0.0,
"C=0 (all gas)")):
246 C = np.full((n, n, n), val, order=
"F")
248 for R
in (1.0, 1e2, 1e4):
251 for _
in range(steps):
254 u = np.stack([s.get_u(), s.get_v(), s.get_w()])
257 print(
" %-18s R=%-8g reference %s" % (name, R, w))
259 d = float(np.max(np.abs(u - ref))) / max(abs(x)
for x
in U)
260 print(
" %-18s R=%-8g d=%.4e %s" % (name, R, d, w))
265 """Total momentum sum(rho_f u) in a periodic box, no forces: conserved to round-off."""
266 print(
"\n=== GATE 3 — momentum conservation in a periodic box (ratio %g) ===" % R)
270 x = (np.arange(n) + 0.5) / n
271 ux = 1.0 + 0.3 * np.sin(2 * math.pi * x)[
None,
None, :] * np.ones((n, n, n), order=
"F")
272 uy = np.full((n, n, n), 0.2, order=
"F")
273 uz = np.full((n, n, n), -0.1, order=
"F")
274 for mom
in (
True,
False):
275 s =
make_solver(n, C, 1.0, R, 0.0, 0.15, (1.3, 0.2, -0.1), mom)
276 s.set_state(np.asfortranarray(ux), uy, uz)
280 rho = s.get_field(
"rho")
282 comps = [s.get_u(), s.get_v(), s.get_w()]
284 rf = 0.5 * (rho + np.roll(rho, 1, axis=c))
285 p += float((rf * comps[c]).sum())
288 s.update_properties()
290 for _
in range(steps):
294 print(
" %-14s sum(rho_f u): %.16e -> %.16e rel %.3e %s"
295 % (
"consistent" if mom
else "inconsistent", m0, m1, abs(m1 - m0) / abs(m0), w))
300 """The rho^c floor: how often it bites and how much it matters."""
301 print(
"\n=== GATE 4 — the rho^c floor (ratio %g, %d steps) ===" % (R, steps))
305 for frac
in (1e-3, 1e-6, 1e-9, 1e-12):
307 s.diagnostics.set_vof_rho_floor(frac)
308 for _
in range(steps):
310 d = s.diagnostics.vof_momentum_diagnostics()
311 u = np.stack([s.get_u(), s.get_v(), s.get_w()])
312 dd = 0.0
if ref
is None else float(np.max(np.abs(u - ref)))
315 print(
" frac %-8g floor %-11.4g floored CVs %-6d min rho^c %.6f "
316 "C^c in [%.3e, %.6f] du vs frac=1e-3: %.3e"
317 % (frac, s.diagnostics.vof_rho_floor(), d[
"floored"], d[
"min_rho_c"],
318 min(d[
"min_Cc"]), max(d[
"max_Cc"]), dd))
323 """A viscous drop falling under buoyancy at density ratio ~800 and 15 cells/diameter.
325 WO-K asks for the Arrufat raindrop (ratio 831.8, 15 cells/D, terminal velocity within 15%).
326 That case is held together by SURFACE TENSION, which is rung V4 — without it a raindrop at its
327 terminal Weber number flattens and breaks up, so the literal case cannot be run at V2b and a
328 "terminal velocity" would not exist to measure. The surface-tension-free case with the same
329 payoff structure is a VISCOUS drop in the Stokes regime, whose terminal velocity is the
330 Hadamard-Rybczynski result
332 U_HR = (2/3) g R^2 (rho_d - rho_c)/mu_c * (mu_c + mu_d)/(2 mu_c + 3 mu_d)
334 (-> the rigid-sphere Stokes value (2/9) g R^2 drho/mu_c as mu_d/mu_c -> infinity). In a periodic
335 box the drop feels its own images, so the measured plateau sits below U_HR by the usual
336 Sangani-Acrivos-type factor at this volume fraction; the number to trust is the CONSISTENT vs
337 INCONSISTENT comparison at identical settings, with U_HR quoted as the scale.
339 Buoyancy is imposed as f_z = -(rho - <rho>) g through a LinearMix force closure, so the box
340 carries no net force and the drop falls relative to a quiescent mean.
345 rho_g, rho_l = 1.0, R
346 rho_mean = rho_g + (rho_l - rho_g) * vol
351 mu_l = mu_g * mu_ratio
354 f0 = -(rho_g - rho_mean) * g
355 f1 = -(rho_l - rho_g) * g
356 U_hr = (2.0 / 3.0) * g * R_drop**2 * (rho_l - rho_g) / mu_g * (mu_g + mu_l) / (
358 print(
"\n=== GATE 5 — falling viscous drop, ratio %g, D/h = %d, mu_l/mu_g = %g ==="
360 tau = 2.0 * R_drop**2 * rho_l / (9.0 * mu_g)
363 K = 1.0 + 1.76 * vol ** (1.0 / 3.0)
364 print(
" Hadamard-Rybczynski U_t = %.5e (unbounded), /K = %.5e with the periodic correction "
365 "K = %.3f at phi = %.4f; tau = %.0f, box %d^3, Re = %.3f"
366 % (U_hr, U_hr / K, K, vol, tau, n, rho_g * (U_hr / K) * D / mu_g))
367 print(
" %-14s | %-12s | %-12s | %-9s | %s"
368 % (
"scheme",
"U_drop",
"U/(U_HR/K)",
"max|u|",
"pressure"))
370 for mom
in (
True,
False):
377 s.set_advection(
False)
379 s.set_pressure_chebyshev(
True, PRESS_MAXIT, 1e-11)
382 s.set_property_model(
"rho",
"linear",
"C", [rho_g, rho_l - rho_g])
383 s.set_property_model(
"mu",
"linear",
"C", [mu_g, mu_l - mu_g])
384 s.set_property_model(
"force_z",
"linear",
"C", [f0, f1])
386 s.enable_vof_momentum(rho_g, rho_l)
390 for k
in range(steps):
393 except RuntimeError
as ex:
394 print(
" %-14s BLEW UP at step %d: %s" % (
"consistent" if mom
else "inconsistent",
403 hist.append(float((c * uz).sum() / m)
if m > 0
else 0.0)
406 cfl = s.diagnostics.vof_last_courant()
410 elif cfl < 0.06
and dt < dt0:
411 dt = min(dt0, dt * 1.5)
414 u_drop = np.mean(hist[-4:])
if len(hist) >= 4
else (hist[-1]
if hist
else 0.0)
415 mx = max(float(np.max(np.abs(s.get_u()))), float(np.max(np.abs(s.get_v()))),
416 float(np.max(np.abs(s.get_w()))))
417 print(
" %-14s | %-12.5e | %-12.4f | %-9.3e | %s"
418 % (
"consistent" if mom
else "inconsistent", u_drop, u_drop / (U_hr / K), mx, w))
419 print(
" plateau history (every 20 steps, last 10): %s"
420 %
" ".join(
"%.4e" % h
for h
in hist[-10:]))
421 out[
"mom" if mom
else "no"] = (u_drop, hist)
426def gate_rt(res=(24, 48), R=1000.0, steps=300):
427 """Rayleigh-Taylor at two resolutions — the WO-K near-Nyquist check (Arrufat section 5).
429 A momentum-consistent scheme can excite growth near the grid scale on under-resolved shear
430 layers. Reported here as the interface amplitude growth AND the peak |u| relative to the
431 inconsistent run at the same resolution; a near-Nyquist artefact shows up as the finer grid
432 being WORSE, which is the opposite of a convergence signature.
434 print(
"\n=== GATE 6 — Rayleigh-Taylor at two resolutions (near-Nyquist check, ratio %g) ==="
436 print(
" %-5s | %-13s | %-13s | %-13s | %s"
437 % (
"n",
"amp consistent",
"amp inconsist",
"max|u| cons",
"pressure (consistent)"))
440 for mom
in (
True,
False):
442 C = np.zeros((nx, nx, nz), order=
"F")
443 x = (np.arange(nx) + 0.5) / nx
444 z = (np.arange(nz) + 0.5) / nz
448 h = 0.5 + 0.01 * math.cos(2 * math.pi * x[i]) * math.cos(2 * math.pi * x[j])
451 C[i, j, k] = min(1.0, max(0.0, hz - k))
452 rho_g, rho_l = 1.0, R
454 vol = C.sum() / (nx * nx * nz)
455 rho_mean = rho_g + (rho_l - rho_g) * vol
462 s.set_pressure_geometry(np.full((nx, nx, nz), 10.0, order=
"F"))
463 s.set_pressure_chebyshev(
True, PRESS_MAXIT, 1e-11)
466 s.set_property_model(
"rho",
"linear",
"C", [rho_g, rho_l - rho_g])
467 s.set_property_model(
"force_z",
"linear",
"C",
468 [-(rho_g - rho_mean) * g, -(rho_l - rho_g) * g])
470 s.enable_vof_momentum(rho_g, rho_l)
473 for k
in range(steps):
480 cfl = s.diagnostics.vof_last_courant()
484 elif cfl < 0.06
and dt < dt0:
485 dt = min(dt0, dt * 1.5)
488 row.append((float(
"nan"), float(
"nan"), w))
491 col = cf.sum(axis=(0, 1)) / (nx * nx)
493 amp = float(np.max(cf.sum(axis=2)) - np.min(cf.sum(axis=2))) / nz
494 mx = max(float(np.max(np.abs(s.get_u()))), float(np.max(np.abs(s.get_w()))))
495 row.append((amp, mx, w))
497 print(
" %-5d | %-13.5e | %-13.5e | %-13.3e | %s"
498 % (n, row[0][0], row[1][0], row[0][1], row[0][2]))