flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
vof_surface_tension.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2"""VoF rung V4 (WO-P) — the balanced-force CSF physics battery.
3
4The *algebraic* gates of this rung (the exactness gate, the operator ablation, Young-Laplace,
5the capillary dt, inertness, the wisp guard) live in `tests/kokkos/test_vof_surface_tension.cpp`
6and run in the ctest. What is here is the PHYSICS: the cases where surface tension has to produce
7a number somebody else has already published.
8
9 static spurious currents of a stationary droplet: Ca = mu max|u| / sigma vs resolution,
10 and the wisp-guard ablation at a resolution where it is destructive.
11 wave a small-amplitude standing capillary wave against the analytical dispersion
12 relation omega^2 = sigma k^3 / (rho1 + rho2) and the 2 nu k^2 viscous decay rate
13 (Prosperetti 1981; Popinet 2009 section 4.2).
14 lamb a droplet oscillating in mode n = 2 against Lamb's (1932, art. 275) frequency
15 omega^2 = n(n-1)(n+1)(n+2) sigma / (R^3 ((n+1) rho_in + n rho_out)), AND (2026-09-02)
16 the exact viscous mode of vof_capillary_references.py.
17 hysing the Hysing et al. (IJNMF 60:1259, 2009) rising-bubble benchmark, both cases,
18 against the published reference quantities. READ THE CAVEAT it prints.
19 falling the falling-drop terminal velocity WO-K had to defer, re-run WITH surface tension
20 and with the momentum solve driven to a tolerance instead of a fixed sweep count.
21 limits step economics: does the capillary dt or the Weymouth-Yue CFL bind, and at what
22 cost in steps?
23
24UNITS. The solver's cell size is 1 and its time unit is the physical second, so a physical
25problem of size L on nx cells is mapped with s = nx / L cells per unit length:
26
27 rho' = rho mu' = s^2 mu sigma' = s^3 sigma (rho g)' = s rho g u' = s u t' = t
28
29(derived by substituting x = x'/s, u = u'/s, t = t' into the momentum equation and requiring
30every term to scale alike; `Scale` below is that map, and every case states its physical numbers).
31
32EVERY gate that reports a functional records the max pressure-iteration count against its cap and
33the flux divergence, and marks a capped run INVALID rather than merely flagging it (shared
34preamble rule 3b).
35
36Usage: PYTHONPATH=<build> python tests/study/vof_surface_tension.py [gate ...] [--quick]
37"""
38import math
39import sys
40import time
41
42import numpy as np
43
44import peclet.flow as pf
45
46sys.path.insert(0, __import__('os').path.dirname(__import__('os').path.abspath(__file__)))
47from vof_capillary_references import wave_mode, drop_mode # noqa: E402
48
49QUICK = "--quick" in sys.argv
50# --rtol R: the static gate's pressure-driver rtol (default 1e-14); the momentum solve follows it.
51# An instrument for the tolerance study (tests/study/vof_perf/d1_tolerance.py), which also reads
52# STATIC_RESULTS: one dict per resolution rung of the last gate_static() call.
53RTOL = float(sys.argv[sys.argv.index("--rtol") + 1]) if "--rtol" in sys.argv else None
54GATES = [a for i, a in enumerate(sys.argv[1:], 1)
55 if not a.startswith("--") and sys.argv[i - 1] != "--rtol"]
56STATIC_RESULTS = []
57
58
59# --------------------------------------------------------------------------- helpers
60class Scale:
61 """Physical <-> solver (cell size 1, time in seconds) unit map."""
62
63 def __init__(self, cells_per_length):
64 self.s = float(cells_per_length)
65
66 def mu(self, mu):
67 return self.s ** 2 * mu
68
69 def sigma(self, sigma):
70 return self.s ** 3 * sigma
71
72 def bodyforce(self, rho_g): # a force per unit volume, e.g. rho*g
73 return self.s * rho_g
74
75 def vel_to_phys(self, u):
76 return u / self.s
77
78 def len_to_cells(self, x):
79 return self.s * x
80
81
82def maxvel(s):
83 return max(np.abs(s.get_u()).max(), np.abs(s.get_v()).max(), np.abs(s.get_w()).max())
84
85
86class Solve:
87 """A run's solver health, per the shared preamble's rule 3b."""
88
89 def __init__(self, cap):
90 self.cap = cap
91 self.iters = 0
92 self.div = 0.0
93
94 def sample(self, s):
95 self.iters = max(self.iters, s.diagnostics.last_pressure_iterations())
96 self.div = max(self.div, s.max_open_divergence())
97
98 @property
99 def valid(self):
100 return self.iters < self.cap
101
102 def __str__(self):
103 return (f"pressure {self.iters}/{self.cap} "
104 f"{'OK' if self.valid else '*** CAPPED -> RUN INVALID ***'}, "
105 f"max|div(open u)| {self.div:.2e}")
106
107
108def sphere_fractions(shape, R, c, sub=24):
109 """Volume fractions of a sphere: exact in z, sub x sub subsampled in (x,y)."""
110 nx, ny, nz = shape
111 ax = (np.arange(nx)[:, None] + (np.arange(sub)[None, :] + 0.5) / sub).ravel()
112 ay = (np.arange(ny)[:, None] + (np.arange(sub)[None, :] + 0.5) / sub).ravel()
113 X, Y = ax[:, None], ay[None, :]
114 half = np.sqrt(np.maximum(R * R - (X - c[0]) ** 2 - (Y - c[1]) ** 2, 0.0))
115 z0, z1 = c[2] - half, c[2] + half
116 C = np.zeros((nx, ny, nz))
117 for k in range(nz):
118 seg = np.maximum(np.minimum(z1, k + 1) - np.maximum(z0, k), 0.0)
119 C[:, :, k] = seg.reshape(nx, sub, ny, sub).mean(axis=(1, 3))
120 return np.asfortranarray(C)
121
122
123def cylinder_fractions(shape, R, cx, cz, sub=32):
124 """Volume fractions of a cylinder with its axis along y (a 2-D disc), exact in z."""
125 nx, ny, nz = shape
126 ax = (np.arange(nx)[:, None] + (np.arange(sub)[None, :] + 0.5) / sub).ravel()
127 half = np.sqrt(np.maximum(R * R - (ax - cx) ** 2, 0.0))
128 z0, z1 = cz - half, cz + half
129 C = np.zeros((nx, ny, nz))
130 for k in range(nz):
131 seg = np.maximum(np.minimum(z1, k + 1) - np.maximum(z0, k), 0.0)
132 col = seg.reshape(nx, sub).mean(axis=1)
133 C[:, :, k] = col[:, None]
134 return np.asfortranarray(C)
135
136
138 """Times of the sign changes of y (linear interpolation)."""
139 out = []
140 for i in range(len(y) - 1):
141 if y[i] == 0.0:
142 out.append(t[i])
143 elif y[i] * y[i + 1] < 0.0:
144 out.append(t[i] + (t[i + 1] - t[i]) * y[i] / (y[i] - y[i + 1]))
145 return np.array(out)
146
147
148# --------------------------------------------------------------------------- static droplet
150 print("\n" + "=" * 96)
151 print("STATIC DROPLET — the spurious capillary number Ca = mu max|u| / sigma")
152 print("=" * 96)
153 print(" The balanced-force identity itself is EXACT (the ctest's P1: max|u| ~ 1e-17 with a")
154 print(" constant kappa, at every resolution and viscosity). What is left here is the")
155 print(" curvature error, and Ca is essentially its cell-unit magnitude.\n")
156 rungs = [(16, 4.0), (32, 8.0), (48, 12.0)] if QUICK else [(16, 4.0), (32, 8.0), (48, 12.0),
157 (64, 16.0)]
158 prev = None
159 STATIC_RESULTS.clear()
160 for n, R in rungs:
161 s = pf.Solver(n, n, n)
162 s.set_rho(1.0)
163 s.set_mu(0.1)
164 s.set_advection(False)
165 s.set_pressure_geometry(np.full((n, n, n), 10.0, order="F"))
166 s.set_pressure_chebyshev(True, 500, RTOL or 1e-14)
167 s.enable_vof()
168 s.set_vof(sphere_fractions((n, n, n), R, (n / 2 + 0.13, n / 2 + 0.27, n / 2 + 0.11)))
169 s.set_property_model("rho", "linear", "C", [1.0, 0.0])
170 s.set_surface_tension(1.0)
171 s.set_dt(0.5 * s.capillary_dt())
172 h = Solve(500)
173 for _ in range(60):
174 s.step()
175 h.sample(s)
176 ca = 0.1 * maxvel(s) / 1.0
177 STATIC_RESULTS.append(dict(n=n, R=R, max_u=maxvel(s), ca=ca, p_iters_max=h.iters,
178 div_max=h.div, capped=not h.valid))
179 br, ka = s.vof_curvature_branch(), s.vof_curvature()
180 d = (br > 0.5) & (br < 5.5)
181 dk = np.abs(ka[d] - 2.0 / R)
182 d2 = s.diagnostics.csf_diagnostics()
183 print(f" D/dx = {2*R:5.1f} Ca = {ca:.3e}"
184 + (f" ({prev/ca:.2f} x the coarser rung)" if prev else " ")
185 + f" dkappa rms {dk.std():.2e} max {dk.max():.2e}"
186 f" orphan faces {d2['orphan_faces']}")
187 print(f" {h}")
188 prev = ca
189
190 print("\n WISP-GUARD ABLATION at 64^3 (set_vof_interface_eps(0) — the rung-V3 predicate):")
191 for eps in (1e-8, 0.0):
192 n, R = 64, 16.0
193 s = pf.Solver(n, n, n)
194 s.set_rho(1.0)
195 s.set_mu(0.1)
196 s.set_advection(False)
197 s.set_pressure_geometry(np.full((n, n, n), 10.0, order="F"))
198 s.set_pressure_chebyshev(True, 500, RTOL or 1e-14)
199 s.enable_vof()
200 s.set_vof(sphere_fractions((n, n, n), R, (n / 2 + 0.13, n / 2 + 0.27, n / 2 + 0.11)))
201 s.set_property_model("rho", "linear", "C", [1.0, 0.0])
202 s.set_surface_tension(1.0)
203 s.diagnostics.set_vof_interface_eps(eps)
204 s.set_dt(0.5 * s.capillary_dt())
205 u = []
206 try:
207 for _ in range(60):
208 s.step()
209 u.append(maxvel(s))
210 except RuntimeError as e:
211 print(f" eps = {eps:g}: DIVERGED at step {len(u)} — {str(e)[:70]}")
212 continue
213 br, ka = s.vof_curvature_branch(), s.vof_curvature()
214 d = (br > 0.5) & (br < 5.5)
215 print(f" eps = {eps:<6g} max|u| step 1 {u[0]:.3e} -> step 60 {u[-1]:.3e}"
216 f" cells served {d.sum()} max|kappa| {np.abs(ka[d]).max():.3e} (2/R = {2/R:.4f})")
217
218
219# --------------------------------------------------------------------------- capillary wave
221 print("\n" + "=" * 96)
222 print("CAPILLARY WAVE — dispersion omega^2 = sigma k^3 / (rho1 + rho2) (Prosperetti 1981)")
223 print("=" * 96)
224 print(" Quasi-2D: a standing wave of wavelength lambda = Lx along x on a flat interface at")
225 print(" mid-height, periodic in x and y, walls +-z. Small amplitude (a0 = lambda/100) and")
226 print(" matched kinematic viscosities, so the inviscid dispersion relation is the leading")
227 print(" behaviour and the decay rate is 2 nu k^2. Solver units throughout (cell = 1).\n")
228 print(f" {'nx':>4} {'lambda':>7} {'nu':>8} {'omega_num':>11} {'omega_th':>11} {'err':>8}"
229 f" {'gamma_num':>10} {'gamma_th':>10} {'err':>8}")
230 rungs = [(32, 0.005)] if QUICK else [(32, 0.005), (64, 0.005), (32, 0.02)]
231 for nx, nu in rungs:
232 nz = nx
233 ny = 4
234 rho = 1.0
235 sigma = 1.0
236 lam = float(nx)
237 k = 2 * math.pi / lam
238 a0 = lam / 100.0
239 omega_th = math.sqrt(sigma * k ** 3 / (2 * rho))
240 gamma_th = 2 * nu * k * k
241 # 2026-09-02: the inviscid omega_th and the free-surface 2 nu k^2 are the WRONG references
242 # for a two-fluid interface (VOF_NEXT_SESSION Item 2) -- the exact viscous normal mode is
243 # in vof_capillary_references.py and is what the gate reports against.
244 s_ex, _ = wave_mode(k, nu, sigma, rho, H=nz / 2.0)
245 omega_ex, gamma_ex = s_ex.imag, -s_ex.real
246
247 s = pf.Solver(nx, ny, nz)
248 s.set_rho(rho)
249 s.set_mu(nu * rho)
250 s.set_advection(False)
251 s.set_domain_bc("-z", "wall", (0, 0, 0))
252 s.set_domain_bc("+z", "wall", (0, 0, 0))
253 s.set_pressure_geometry(np.full((nx, ny, nz), 10.0, order="F"))
254 s.set_pressure_chebyshev(True, 500, 1e-11)
255 s.enable_vof()
256 # exact fractions of a cosine interface z = nz/2 + a0 cos(k x)
257 xe = np.arange(nx + 1)
258 zi = nz / 2.0 + a0 * (np.sin(k * xe[1:]) - np.sin(k * xe[:-1])) / (k * 1.0)
259 C = np.zeros((nx, ny, nz))
260 for kk in range(nz):
261 C[:, :, kk] = np.clip(zi - kk, 0.0, 1.0)[:, None]
262 s.set_vof(np.asfortranarray(C))
263 s.set_property_model("rho", "linear", "C", [rho, 0.0])
264 s.set_surface_tension(sigma)
265 dt = 0.5 * s.capillary_dt()
266 s.set_dt(dt)
267
268 period = 2 * math.pi / omega_th
269 nsteps = int(2.5 * period / dt)
270 h = Solve(500)
271 t, amp = [], []
272 for i in range(nsteps):
273 s.step()
274 h.sample(s)
275 Cf = s.get_vof()
276 # interface height from the column sum (exact for a single-valued interface)
277 col = Cf.sum(axis=2)[:, 0]
278 zint = col - (nz / 2.0)
279 t.append((i + 1) * dt)
280 amp.append(float(np.dot(zint, np.cos(k * (np.arange(nx) + 0.5))) * 2.0 / nx))
281 t = np.array(t)
282 amp = np.array(amp)
283 zc = zero_crossings(t, amp)
284 if len(zc) >= 3:
285 omega_num = math.pi / np.mean(np.diff(zc))
286 else:
287 omega_num = float("nan")
288 # decay rate from the successive extrema
289 pk = [i for i in range(1, len(amp) - 1)
290 if abs(amp[i]) > abs(amp[i - 1]) and abs(amp[i]) > abs(amp[i + 1])]
291 if len(pk) >= 2:
292 gamma_num = -math.log(abs(amp[pk[-1]]) / abs(amp[pk[0]])) / (t[pk[-1]] - t[pk[0]])
293 else:
294 gamma_num = float("nan")
295 print(f" {nx:>4} {lam:>7.0f} {nu:>8.4g} {omega_num:>11.5f} {omega_th:>11.5f}"
296 f" {100*(omega_num/omega_th-1):>7.2f}% {gamma_num:>10.3e} {gamma_th:>10.3e}"
297 f" {100*(gamma_num/gamma_th-1):>7.1f}%")
298 print(f" EXACT viscous two-fluid mode: omega {omega_ex:.5f} (sim {100*(omega_num/omega_ex-1):+.2f}%), "
299 f"gamma {gamma_ex:.3e} (sim {100*(gamma_num/gamma_ex-1):+.1f}%)")
300 print(f" a0 = {a0:.3f} cells, {nsteps} steps over 2.5 periods; {h}")
301
302
303# --------------------------------------------------------------------------- Lamb oscillation
305 print("\n" + "=" * 96)
306 print("OSCILLATING DROPLET — Lamb (1932) art. 275 mode n = 2")
307 print("=" * 96)
308 print(" omega_n^2 = n(n-1)(n+1)(n+2) sigma / (R^3 ((n+1) rho_in + n rho_out))")
309 print(" A prolate perturbation of a sphere, amplitude 5% of R, released from rest; the")
310 print(" frequency is read off the zero crossings of the n = 2 moment of the colour field.\n")
311 print(f" {'n':>4} {'R':>5} {'phi':>7} {'ratio':>7} {'omega_num':>11} {'omega_th':>11}"
312 f" {'err':>8}")
313 # Two sweeps: (a) fixed drop-to-box ratio phi = 6.5 % at two resolutions -- the DISCRETIZATION
314 # rung; (b) the same drop R = 8 in a bigger and bigger periodic box -- the CONFINEMENT rung.
315 # Lamb's frequency is for a drop in an UNBOUNDED outer fluid, and the periodic images add
316 # outer-fluid inertia, which lowers omega. The ratio-100 row is the same statement from the
317 # other side: with a 100x lighter outer fluid there is much less added mass to confine.
318 rungs = [(32, 8.0, 1.0), (48, 8.0, 1.0)] if QUICK else [(32, 8.0, 1.0), (48, 12.0, 1.0),
319 (48, 8.0, 1.0), (64, 8.0, 1.0),
320 (48, 12.0, 100.0)]
321 for n, R, ratio in rungs:
322 sigma = 1.0
323 rho_in, rho_out = ratio, 1.0
324 mu = 0.02
325 eps = 0.05
326 nn = 2
327 omega_th = math.sqrt(nn * (nn - 1) * (nn + 1) * (nn + 2) * sigma
328 / (R ** 3 * ((nn + 1) * rho_in + nn * rho_out)))
329 # prolate spheroid of the same volume: a = R(1+eps), b = c = R/sqrt(1+eps)
330 a = R * (1 + eps)
331 b = R / math.sqrt(1 + eps)
332 cx = cy = cz = n / 2 + 0.137
333 sub = 16
334 ax = (np.arange(n)[:, None] + (np.arange(sub)[None, :] + 0.5) / sub).ravel()
335 X, Y = ax[:, None], ax[None, :]
336 q = 1.0 - ((X - cx) ** 2 + (Y - cy) ** 2) / b ** 2
337 half = a * np.sqrt(np.maximum(q, 0.0))
338 C = np.zeros((n, n, n))
339 for kk in range(n):
340 seg = np.maximum(np.minimum(cz + half, kk + 1) - np.maximum(cz - half, kk), 0.0)
341 C[:, :, kk] = seg.reshape(n, sub, n, sub).mean(axis=(1, 3))
342
343 s = pf.Solver(n, n, n)
344 s.set_rho(rho_in)
345 s.set_mu(mu)
346 s.set_advection(False)
347 s.set_pressure_geometry(np.full((n, n, n), 10.0, order="F"))
348 s.set_pressure_chebyshev(True, 500, 1e-11)
349 s.enable_vof()
350 s.set_vof(np.asfortranarray(C))
351 s.set_property_model("rho", "linear", "C", [rho_out, rho_in - rho_out])
352 s.set_surface_tension(sigma)
353 dt = 0.5 * s.capillary_dt()
354 s.set_dt(dt)
355 period = 2 * math.pi / omega_th
356 nsteps = int(2.5 * period / dt)
357 xs = (np.arange(n) + 0.5) - cx
358 ys = (np.arange(n) + 0.5) - cy
359 zs = (np.arange(n) + 0.5) - cz
360 XX = xs[:, None, None] ** 2
361 YY = ys[None, :, None] ** 2
362 ZZ = zs[None, None, :] ** 2
363 h = Solve(500)
364 t, m2 = [], []
365 for i in range(nsteps):
366 s.step()
367 h.sample(s)
368 Cf = s.get_vof()
369 v = Cf.sum()
370 # the n = 2 shape moment <2z^2 - x^2 - y^2>
371 m = float((Cf * (2 * ZZ - XX - YY)).sum() / v)
372 t.append((i + 1) * dt)
373 m2.append(m)
374 t = np.array(t)
375 m2 = np.array(m2) - np.mean(np.array(m2)[len(m2) // 2:])
376 zc = zero_crossings(t, m2)
377 omega_num = math.pi / np.mean(np.diff(zc)) if len(zc) >= 3 else float("nan")
378 phi = (4 / 3) * math.pi * R ** 3 / n ** 3
379 print(f" {n:>4} {R:>5.1f} {100*phi:>6.2f}% {ratio:>7.0f} {omega_num:>11.5f}"
380 f" {omega_th:>11.5f} {100*(omega_num/omega_th-1):>7.2f}%")
381 if ratio == 1.0: # 2026-09-02: the exact viscous mode (equal fluids); see Item 2 addendum
382 try:
383 s_ex, _ = drop_mode(nn, R, mu / rho_in, sigma, rho_in)
384 print(f" EXACT viscous mode: omega {s_ex.imag:.5f} (sim {100*(omega_num/s_ex.imag-1):+.2f}%), "
385 f"gamma {-s_ex.real:.3e}; the residual after the viscous shift is the OPEN inviscid deficit")
386 except ImportError:
387 print(" (mpmath missing: exact viscous drop mode not computed)")
388 print(f" {nsteps} steps over 2.5 periods, dt = {dt:.4g}; {h}")
389
390
391# --------------------------------------------------------------------------- Hysing bubble
392HYSING = {
393 1: dict(rho1=1000.0, rho2=100.0, mu1=10.0, mu2=1.0, g=0.98, sigma=24.5,
394 ref_vmax=0.2417, ref_tvmax=0.9215, ref_yc=1.081, ref_circ=0.9013),
395 2: dict(rho1=1000.0, rho2=1.0, mu1=10.0, mu2=0.1, g=0.98, sigma=1.96,
396 ref_vmax=0.2502, ref_tvmax=0.7317, ref_yc=1.1376, ref_circ=0.5869),
397}
398
399
400def gate_hysing(case=1, nx=64):
401 p = HYSING[case]
402 print("\n" + "=" * 96)
403 print(f"HYSING RISING BUBBLE, case {case} (Hysing et al., IJNMF 60:1259, 2009), nx = {nx}")
404 print("=" * 96)
405 print(" LATERAL BC. The benchmark prescribes FREE-SLIP side walls, which this solver does not")
406 print(" have (mac_bc.hpp offers periodic / no-slip / Dirichlet / outflow). The lateral")
407 print(" condition here is PERIODIC — and for a bubble on the centreline that is not an")
408 print(" approximation: mirroring about x = 0 and x = 1 puts images at -0.5 and 1.5, i.e. at")
409 print(" spacing 1, and the mirror of a laterally symmetric bubble IS its translate. Case 1")
410 print(" stays symmetric, so the two conditions are equivalent there; case 2 develops skirts")
411 print(" and filaments that break the symmetry, so for case 2 it is an approximation.")
412 print(" NOT reported: circularity. It needs the PLIC interface length, which the solver does")
413 print(" not expose today; y_c(3) and max v_rise are the two the reference tabulates that can")
414 print(" be computed from the colour field alone.\n")
415 Lx, Lz = 1.0, 2.0
416 nz = 2 * nx
417 ny = 4
418 sc = Scale(nx / Lx)
419 R = sc.len_to_cells(0.25)
420 s = pf.Solver(nx, ny, nz)
421 s.set_rho(p["rho1"])
422 s.set_mu(sc.mu(p["mu1"]))
423 s.set_domain_bc("-z", "wall", (0, 0, 0))
424 s.set_domain_bc("+z", "wall", (0, 0, 0)) # no-slip top/bottom, as the benchmark prescribes
425 s.set_pressure_geometry(np.full((nx, ny, nz), 10.0, order="F"))
426 s.set_pressure_chebyshev(True, 600, 1e-12)
427 s.enable_vof()
428 # C = 1 in the HEAVY fluid (the surrounding liquid), 0 in the bubble
429 C = 1.0 - cylinder_fractions((nx, ny, nz), R, nx / 2.0, sc.len_to_cells(0.5))
430 s.set_vof(np.asfortranarray(C))
431 s.set_property_model("rho", "linear", "C", [p["rho2"], p["rho1"] - p["rho2"]])
432 s.set_property_model("mu", "linear", "C", [sc.mu(p["mu2"]), sc.mu(p["mu1"] - p["mu2"])])
433 s.set_surface_tension(sc.sigma(p["sigma"]))
434 # Momentum-consistent transport (V2b): the same geometric fluxes carry rho^c u_c. At case 1's
435 # density ratio 10 it moves the peak rise velocity by 15 % (see the findings entry) and at
436 # case 2's ratio 1000 it is what the rung is FOR.
437 s.enable_vof_momentum(p["rho2"], p["rho1"])
438 # buoyancy f_z = -rho(C) g, as a LinearMix on C (the +-z walls carry the net force)
439 s.set_property_model("force_z", "linear", "C",
440 [-sc.bodyforce(p["rho2"] * p["g"]),
441 -sc.bodyforce((p["rho1"] - p["rho2"]) * p["g"])])
442 T = 3.0
443 dt = 0.5 * s.capillary_dt()
444 s.set_dt(dt)
445 lim = s.vof_step_limits()
446 print(f" h = 1/{nx}, dt_sigma = {s.capillary_dt():.4e} s")
447 print(f" step limits at t=0: WY CFL dt {lim['cfl_dt']:.4g}, capillary dt "
448 f"{lim['capillary_dt']:.4g} -> {'CAPILLARY' if lim['capillary_binds'] else 'CFL'} binds")
449 print(f" ADAPTIVE dt = 0.4 x min(WY CFL, capillary), re-picked every 10 steps: the bubble")
450 print(f" accelerates, so the WY cap takes over from the capillary limit part-way through.")
451 zs = (np.arange(nz) + 0.5) / sc.s
452 h = Solve(600)
453 vmax, tvmax, yc = 0.0, 0.0, 0.0
454 t0 = time.time()
455 t = 0.0
456 i = 0
457 ncap, ncfl = 0, 0
458 while t < T:
459 if i % 10 == 0:
460 L = s.vof_step_limits()
461 dt = 0.4 * min(L["cfl_dt"], L["capillary_dt"])
462 s.set_dt(dt)
463 ncap += int(L["capillary_binds"])
464 ncfl += int(not L["capillary_binds"])
465 t += dt
466 i += 1
467 nsteps = i
468 s.step()
469 h.sample(s)
470 Cf = s.get_vof()
471 B = 1.0 - Cf # the bubble indicator
472 vb = B.sum()
473 ycm = float((B.sum(axis=(0, 1)) * zs).sum() / vb)
474 w = s.get_w()
475 vrise = float((B * w).sum() / vb) / sc.s
476 if vrise > vmax:
477 vmax, tvmax = vrise, t
478 yc = ycm
479 if i % 100 == 0:
480 print(f" t = {t:5.3f} dt = {dt:.3e} y_c = {ycm:.4f} v_rise = {vrise:.4f} "
481 f"V/V0 = {vb/ (math.pi*0.25**2*sc.s**2*ny):.6f}")
482 print(f" measured: v_rise max {vmax:.4f} at t = {tvmax:.3f}; y_c(3) = {yc:.4f}")
483 print(f" reference: v_rise max {p['ref_vmax']:.4f} at t = {p['ref_tvmax']:.3f}; "
484 f"y_c(3) = {p['ref_yc']:.4f}")
485 print(f" deviation: v_rise {100*(vmax/p['ref_vmax']-1):+.1f} %, "
486 f"y_c {100*(yc/p['ref_yc']-1):+.1f} %")
487 print(f" {nsteps} steps to t = {T}; the binding limit was CAPILLARY on {ncap} of the "
488 f"{ncap+ncfl} dt re-picks and the WY CFL on {ncfl}")
489 print(f" {h} ({time.time()-t0:.0f} s)")
490
491
492# --------------------------------------------------------------------------- falling drop
494 print("\n" + "=" * 96)
495 print("FALLING DROP — the terminal velocity WO-K deferred, re-run with surface tension")
496 print("=" * 96)
497 print(" WO-K reached 9 % of Hadamard-Rybczynski and named an under-resolved momentum solve")
498 print(" as the suspected cause. BOTH halves of that turn out to be wrong, and the gate as")
499 print(" written measured the wrong quantity:")
500 print("")
501 print(" (1) A periodic box driven by a ZERO-MEAN body force conserves total MOMENTUM, not")
502 print(" total volume flux. The box's mean velocity is a free mode of the projection, and")
503 print(" with rho_l/rho_g = 800 at phi = 1.6 % the light ambient recoils at ~13 x the")
504 print(" drop's speed (rho_g (1-phi) U_a = -rho_l phi U_d). So the LAB-FRAME drop velocity")
505 print(" is a near-cancellation of two much larger numbers and is not the settling")
506 print(" velocity. The quantity Hadamard-Rybczynski predicts is U_drop - U_ambient.")
507 print(" (2) Driving the momentum smoother to a tolerance instead of a fixed sweep count does")
508 print(" NOT rescue the lab-frame number -- it moves it further, precisely because that")
509 print(" number is a cancellation. Both are reported below.")
510 print("")
511 print(" Also corrected here: Hadamard-Rybczynski is U = (2/3)(d rho) g R^2/mu_o *")
512 print(" (mu_o + mu_i)/(2 mu_o + 3 mu_i) -- the denominator carries 3 mu_i, and the rigid")
513 print(" limit mu_i -> inf must return Stokes' 2 (d rho) g R^2/(9 mu_o).\n")
514 rho_l, rho_g = 800.0, 1.0
515 mu_out = 50.0
516 mu_ratio = 100.0
517 sigma = 16.0
518 g = 3.4e-4
519
520 def hr(R, mo, lam):
521 return (2.0 / 3.0) * (rho_l - rho_g) * g * R * R / mo * (1 + lam) / (2 + 3 * lam)
522
523 def run(n, D, cap, rtol, label):
524 R = D / 2
525 phi = (4 / 3) * math.pi * R ** 3 / n ** 3
526 s = pf.Solver(n, n, n)
527 s.set_rho(rho_l)
528 s.set_mu(mu_out)
529 s.set_advection(False)
530 s.set_pressure_geometry(np.full((n, n, n), 10.0, order="F"))
531 s.set_pressure_chebyshev(True, 400, 1e-12)
532 s.enable_vof()
533 s.set_vof(sphere_fractions((n, n, n), R, (n / 2 + 0.13, n / 2 + 0.27, n / 2 + 0.11)))
534 s.set_property_model("rho", "linear", "C", [rho_g, rho_l - rho_g])
535 s.set_property_model("mu", "linear", "C", [mu_out, mu_ratio * mu_out - mu_out])
536 s.set_surface_tension(sigma)
537 s.enable_vof_momentum(rho_g, rho_l)
538 s.set_property_model("force_z", "linear", "C",
539 [g * (rho_l - rho_g) * phi, -g * (rho_l - rho_g)])
540 if rtol > 0:
541 s.diagnostics.set_velocity_solver_params(cap, rtol, 2)
542 else:
543 s.diagnostics.set_velocity_solver_params(cap)
544 dt = 0.5 * s.capillary_dt()
545 s.set_dt(dt)
546 tau = 2.0 * R * R * rho_l / (9.0 * mu_out)
547 nsteps = int((2.0 if QUICK else 6.0) * tau / dt)
548 h = Solve(400)
549 sweeps = 0
550 hist = []
551 for i in range(nsteps):
552 s.step()
553 h.sample(s)
554 sweeps = max(sweeps, s.diagnostics.last_step_timers()["momentum_sweeps"])
555 C = s.get_vof()
556 w = s.get_w()
557 ud = float((C * w).sum() / C.sum())
558 ua = float(((1 - C) * w).sum() / (1 - C).sum())
559 hist.append((ud, ua))
560 tail = hist[-max(10, nsteps // 20):]
561 Ud = float(np.mean([a for a, _ in tail]))
562 Ua = float(np.mean([b for _, b in tail]))
563 Urel = Ud - Ua
564 U0 = hr(R, mu_out, mu_ratio)
565 # Hasimoto (1959) simple-cubic array drag correction, in its DENOMINATOR form
566 # F = 6 pi mu a U / (1 - 1.7601 c^{1/3} + c - 1.5593 c^2)
567 # -- not the linearised 1 + 1.7601 c^{1/3}, which under-corrects by 20 % already at
568 # c = 0.016 (1.443 against 1.746) and is what WO-K's substitute quoted.
569 K = 1.0 / (1.0 - 1.7601 * phi ** (1 / 3) + phi - 1.5593 * phi ** 2)
570 print(f" {label}")
571 print(f" D/h {D:.0f} phi {phi:.4f} U_HR {U0:.4e} K {K:.3f} U_HR/K {U0/K:.4e}")
572 print(f" U_drop(lab) {Ud:.4e} -> {Ud/(-U0/K):.4f} x "
573 f"U_ambient {Ua:.4e} recoil ratio {-Ua/Ud:.1f}")
574 print(f" U_rel = U_drop - U_ambient = {Urel:.4e} -> "
575 f"**{Urel/(-U0/K):.4f} x (U_HR/K)**")
576 print(f" dt {dt:.3g} = 0.5 dt_sigma, {nsteps} steps = "
577 f"{nsteps*dt/tau:.1f} tau, max sweeps/step {sweeps}; {h}")
578 return Urel / (-U0 / K)
579
580 print(" (a) the momentum-solve hypothesis, at D/h = 15:")
581 run(48, 15.0, 20, 0.0, "fixed 20 sweeps (WO-K's setting)")
582 run(48, 15.0, 400, 1e-4, "tolerance 1e-4, cap 400")
583 if not QUICK:
584 run(48, 15.0, 2000, 1e-6, "tolerance 1e-6, cap 2000")
585 print("\n (b) resolution, with the momentum solve converged (tolerance 1e-4):")
586 for n, D in ((48, 10.0), (48, 15.0), (64, 20.0)) if not QUICK else ((48, 10.0), (48, 15.0),):
587 run(n, D, 400, 1e-4, f"n = {n}, D/h = {D:.0f}")
588
589
590# --------------------------------------------------------------------------- step economics
592 print("\n" + "=" * 96)
593 print("STEP ECONOMICS — does the capillary dt or the Weymouth-Yue CFL bind?")
594 print("=" * 96)
595 print(" A pore-scale configuration in physical units: water/air (sigma = 0.072 N/m,")
596 print(" rho 1000/1.2, mu 1e-3/1.8e-5), a pore of diameter d driven at a capillary number")
597 print(" Ca = mu U / sigma. The two explicit limits are")
598 print(" capillary dt < sqrt((rho1+rho2) h^3 / (4 pi sigma))")
599 print(" transport dt < CFL_WY h / |u|, CFL_WY = 0.25 (Weymouth's 3-D bound)")
600 print(" so their ratio is dt_cfl/dt_sigma = (CFL/|u'|) sqrt(4 pi sigma' / (rho_sum))")
601 print(" dt_sigma shrinks as h^{3/2} and dt_cfl only as h, so REFINING makes the capillary")
602 print(" limit MORE binding, not less: their ratio grows as h^{-1/2}.\n")
603 rho1, rho2 = 1000.0, 1.2
604 sigma_p = 0.072
605 mu1 = 1e-3
606 print(f" {'d [um]':>8} {'cells/d':>8} {'Ca':>8} {'U [m/s]':>10} {'dt_sig [s]':>12}"
607 f" {'dt_cfl [s]':>12} {'binds':>10} {'steps for 1 pore':>18}")
608 for d_um in (50.0, 200.0):
609 for cells in (16, 32, 64):
610 for Ca in (1e-6, 1e-4, 1e-2):
611 d = d_um * 1e-6
612 sc = Scale(cells / d)
613 U = Ca * sigma_p / mu1
614 sig_s = sc.sigma(sigma_p)
615 dt_sig = math.sqrt((rho1 + rho2) * 1.0 / (4 * math.pi * sig_s))
616 dt_cfl = 0.25 / (sc.s * U)
617 binds = "capillary" if dt_sig < dt_cfl else "CFL"
618 t_pore = d / U
619 nst = t_pore / min(dt_sig, dt_cfl)
620 print(f" {d_um:>8.0f} {cells:>8d} {Ca:>8.0e} {U:>10.3e} {dt_sig:>12.3e}"
621 f" {dt_cfl:>12.3e} {binds:>10} {nst:>18.3e}")
622 print("\n And the same read-off from the solver itself on the static-droplet case:")
623 n, R = 48, 12.0
624 s = pf.Solver(n, n, n)
625 s.set_rho(1000.0)
626 s.set_mu(1.0)
627 s.set_advection(False)
628 s.set_pressure_geometry(np.full((n, n, n), 10.0, order="F"))
629 s.set_pressure_chebyshev(True, 400, 1e-12)
630 s.enable_vof()
631 s.set_vof(sphere_fractions((n, n, n), R, (n / 2 + 0.13, n / 2 + 0.27, n / 2 + 0.11)))
632 s.set_property_model("rho", "linear", "C", [1.0, 999.0])
633 s.set_surface_tension(50.0)
634 s.set_dt(0.5 * s.capillary_dt())
635 U = 0.05
636 s.set_state(np.full((n, n, n), U, order="F"), np.zeros((n, n, n), order="F"),
637 np.zeros((n, n, n), order="F"))
638 lim = s.vof_step_limits()
639 print(f" uniform |u| = {U}: WY CFL dt {lim['cfl_dt']:.4g}, capillary dt "
640 f"{lim['capillary_dt']:.4g}, binding {lim['binding']:.4g} "
641 f"({'capillary' if lim['capillary_binds'] else 'CFL'})")
642
643
644# --------------------------------------------------------------------------- main
645ALL = {
646 "static": gate_static,
647 "wave": gate_wave,
648 "lamb": gate_lamb,
649 "hysing1": lambda: gate_hysing(1, 32 if QUICK else 64),
650 "hysing2": lambda: gate_hysing(2, 32 if QUICK else 64),
651 "falling": gate_falling,
652 "limits": gate_limits,
653}
654
655if __name__ == "__main__":
656 run = GATES or list(ALL)
657 for g in run:
658 if g not in ALL:
659 print(f"unknown gate {g!r}; known: {', '.join(ALL)}")
660 sys.exit(2)
661 for g in run:
662 ALL[g]()
663 print("\ndone.")
__init__(self, cells_per_length)
cylinder_fractions(shape, R, cx, cz, sub=32)
sphere_fractions(shape, R, c, sub=24)
static double mean(const std::vector< double > &a)