flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
vof_cutcell.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2"""WO-Q (rung V5a) — VoF transport in cut cells: the gate battery, in Python.
3
4Every gate prints its numbers; nothing is summarised into an adjective. The three that matter:
5
6 G2 CONSERVATION THROUGH A PACKING (kinematic). The single-phase solver is run to a Stokes steady
7 state on a periodic sphere array, that PROJECTED velocity is frozen, and a liquid slab is
8 advected by `advect_vof` alone. The conserved functional of the openness-weighted scheme is
9 `sum eps_eff * C` (`src/vof/cutcell.hpp` rule 1) and its floor is the projection's own
10 `max_open_divergence()` — both are reported, together with the raw `sum eps * C`, the colour
11 in solid cells (0 by construction), the boundedness in UNCUT fluid cells and the clipped
12 volume.
13
14 G4 COUPLED DRAINING. The same packing at density ratio 10 with a ZERO-MEAN buoyancy force,
15 `enable_vof` + the LinearMix closures, optionally `enable_vof_momentum`. Gates: no NaN, the
16 colour drift against the projection floor, zero colour in solid cells, `max|u|` bounded, and
17 the pressure iteration count against its cap (a capped solve makes a run INVALID).
18
19 G5 THE 90-DEGREE NEUTRAL FILL. A liquid cap on a flat SDF wall at a HALF-INTEGER z, so the wall
20 cells are genuinely cut, with surface tension. The apparent contact angle is measured from the
21 cap height on the axis and the contact radius on the first fluid plane
22 (`theta = 2 atan(h/a)`, the spherical-cap relation), and the Young-Laplace jump against
23 `2 sigma / R`.
24
25Usage:
26 PYTHONPATH=$PWD/build_cuda python tests/study/vof_cutcell.py [g2 g4 g5 ...]
27"""
28import math
29import sys
30
31import numpy as np
32
33import peclet.flow as pf
34
35PRESS_MAXIT = 300
36
37
38def sphere_array_sdf(n, centres, radius):
39 """Periodic array of spheres; > 0 in fluid (flow's SDF convention), cell centres at i + 1/2."""
40 ax = (np.arange(n) + 0.5)[:, None, None]
41 ay = (np.arange(n) + 0.5)[None, :, None]
42 az = (np.arange(n) + 0.5)[None, None, :]
43 best = np.full((n, n, n), 1e30)
44 for cx, cy, cz in centres:
45 for px in (-n, 0, n):
46 for py in (-n, 0, n):
47 for pz in (-n, 0, n):
48 d = np.sqrt((ax - (cx + px)) ** 2 + (ay - (cy + py)) ** 2
49 + (az - (cz + pz)) ** 2) - radius
50 best = np.minimum(best, d)
51 return np.asfortranarray(best)
52
53
54DEFAULT_CENTRES = [(6.0, 7.0, 8.0), (20.0, 9.0, 23.0), (11.0, 24.0, 19.0), (26.0, 22.0, 6.0)]
55
56
57def packing(n):
58 f = n / 32.0
59 return sphere_array_sdf(n, [(a * f, b * f, c * f) for a, b, c in DEFAULT_CENTRES], 6.0 * f)
60
61
62def stokes_state(n, steps=60, mu=0.2, force=(2e-3, 1e-3, 5e-4)):
63 s = pf.Solver(n, n, n)
64 s.set_rho(1.0)
65 s.set_mu(mu)
66 s.set_dt(1.0)
67 s.set_advection(False)
68 s.set_body_force(force)
69 s.set_solid(packing(n), cutcell_pressure=True)
70 it = 0
71 for _ in range(steps):
72 s.step()
73 it = max(it, s.diagnostics.last_pressure_iterations())
74 return s, it
75
76
77# --------------------------------------------------------------------------------------- G2
78def g2(n=48, nadv=500, cfl=0.2):
79 s, it = stokes_state(n)
80 div = s.max_open_divergence()
81 s.enable_vof()
82 c0 = np.zeros((n, n, n), order="F")
83 c0[:, :, : n // 2] = 1.0
84 s.set_vof(c0)
85 d0 = s.diagnostics.vof_diagnostics()
86 dt = cfl / s.vof_max_courant()
87 clip = 0.0
88 mn, mx = 1e30, -1e30
89 solid = 0.0
90 for _ in range(nadv):
91 s.advect_vof(dt)
92 d = s.diagnostics.vof_diagnostics()
93 clip += d["clipped_volume"]
94 mn = min(mn, d["min_fluid"])
95 mx = max(mx, d["max_fluid"])
96 solid = max(solid, abs(d["solid_sum"]))
97 d1 = s.diagnostics.vof_diagnostics()
98 drift = (d1["volume"] - d0["volume"]) / d0["volume"]
99 raw = (d1["raw_volume"] - d0["raw_volume"]) / d0["raw_volume"]
100 print(f"G2 packing {n}^3, {nadv} kinematic steps at interface CFL {cfl} (dt = {dt:.5g})")
101 print(f" max|div(open u)| {div:.3e} pressure iters {it}")
102 print(f" sum eps_eff C {d0['volume']:.15e} -> {d1['volume']:.15e} drift {drift:.3e}")
103 print(f" sum eps C {d0['raw_volume']:.15e} -> {d1['raw_volume']:.15e} drift {raw:.3e}")
104 print(f" solid cells {d1['solid_cells']}, cut cells {d1['cut_cells']}, "
105 f"clamped faces last step {d1['clamped_faces']}")
106 print(f" max|sum C over solid cells| {solid:.3e} (must be exactly 0)")
107 print(f" min/max C over UNCUT fluid cells {mn:.3e} / {mx:.17g}")
108 print(f" clipped liquid volume over the run {clip:.6e} "
109 f"({clip / d0['volume']:.3e} of the liquid volume)")
110 ok = abs(drift) <= 1e-11 and solid == 0.0 and mn >= -1e-12 and mx <= 1 + 1e-12
111 print(f" G2 {'PASS' if ok else 'FAIL'}")
112 return ok
113
114
115# --------------------------------------------------------------------------------------- G4
116def g4(n=48, steps=400, ratio=10.0, grav=2e-3, momentum=True):
117 s = pf.Solver(n, n, n)
118 s.set_rho(1.0)
119 s.set_mu(0.05)
120 s.set_dt(0.5)
121 s.set_advection(False) # the pre-flip default (Stokes) kept; NS physics: CHANGELOG "advection default"
122 s.set_solid(packing(n), cutcell_pressure=True)
123 s.enable_vof()
124 c0 = np.zeros((n, n, n), order="F")
125 c0[:, :, 3 * n // 4:] = 1.0 # a liquid layer resting on top of the packing
126 s.set_vof(c0)
127 liq = 0.25
128 rho_mean = liq * ratio + (1.0 - liq)
129 s.set_property_model("rho", "linear", "C", [1.0, ratio - 1.0])
130 s.set_property_model("mu", "linear", "C", [0.05, 0.45])
131 # ZERO-MEAN buoyancy: a periodic box has no way to absorb a net body force, so `-g rho` would
132 # accelerate the whole fluid without bound and there would be no state to measure a drift in.
133 s.set_property_model("force_z", "linear", "rho", [grav * rho_mean, -grav])
134 if momentum:
135 s.enable_vof_momentum(1.0, ratio)
136 v0 = s.diagnostics.vof_diagnostics()["volume"]
137 maxit, capped, clip = 0, 0, 0.0
138 for _ in range(steps):
139 s.step()
140 it = s.diagnostics.last_pressure_iterations()
141 maxit = max(maxit, it)
142 capped += it >= PRESS_MAXIT
143 clip += s.diagnostics.vof_diagnostics()["clipped_volume"]
144 d1 = s.diagnostics.vof_diagnostics()
145 umax = max(abs(np.asarray(g())).max() for g in (s.get_u, s.get_v, s.get_w))
146 drift = (d1["volume"] - v0) / v0
147 print(f"G4 packing {n}^3 draining, ratio {ratio}, {steps} coupled steps, "
148 f"momentum consistency {'ON' if momentum else 'off'}")
149 print(f" colour drift {drift:.3e} ({drift / steps:.3e} per step); "
150 f"max|div(open u)| {s.max_open_divergence():.3e}")
151 print(f" pressure iters max {maxit} (cap {PRESS_MAXIT}, capped steps {capped})")
152 print(f" min/max C over uncut fluid {d1['min_fluid']:.3e} / {d1['max_fluid']:.17g}; "
153 f"clipped volume total {clip:.3e}")
154 print(f" max|u| {umax:.4e}; sum C over solid cells {d1['solid_sum']:.3e}")
155 ok = (not math.isnan(umax) and capped == 0 and abs(drift) / steps <= 1e-10
156 and d1["solid_sum"] == 0.0)
157 print(f" G4 {'PASS' if ok else 'FAIL (a capped solve makes the run INVALID)'}")
158 return ok
159
160
161# --------------------------------------------------------------------------------------- G5
162def g5(diam=24, steps=200, sigma=1.0, mu=0.05, cfl=0.5):
163 nx = ny = int(1.7 * diam)
164 nz = int(1.2 * diam)
165 zw = 3.5 # HALF-INTEGER: the wall cells are genuinely cut
166 r = 0.5 * diam
167 z = (np.arange(nz) + 0.5)[None, None, :]
168 sdf = np.asfortranarray(np.broadcast_to(z - zw, (nx, ny, nz)).astype(float).copy())
169 cx, cy = nx * 0.5, ny * 0.5
170 # 4^3 midpoint subsampling of the FLUID part of each cell (C is a fraction of the fluid volume)
171 sub = (np.arange(4) + 0.5) / 4.0
172 c0 = np.zeros((nx, ny, nz))
173 for a in sub:
174 for b in sub:
175 for c in sub:
176 px = (np.arange(nx) + a)[:, None, None]
177 py = (np.arange(ny) + b)[None, :, None]
178 pz = (np.arange(nz) + c)[None, None, :]
179 fluid = pz >= zw
180 inside = ((px - cx) ** 2 + (py - cy) ** 2 + (pz - zw) ** 2 < r * r) & fluid
181 c0 += np.where(inside, 1.0, 0.0)
182 tot = np.zeros((nx, ny, nz))
183 for c in sub:
184 pz = (np.arange(nz) + c)[None, None, :]
185 tot += np.broadcast_to(np.where(pz >= zw, 16.0, 0.0), (nx, ny, nz))
186 c0 = np.asfortranarray(np.where(tot > 0, c0 / np.maximum(tot, 1e-30), 0.0))
187
188 s = pf.Solver(nx, ny, nz)
189 s.set_rho(1.0)
190 s.set_mu(mu)
191 s.set_advection(False)
192 s.set_solid(sdf, cutcell_pressure=True)
193 s.enable_vof()
194 s.set_vof(c0)
195 s.set_surface_tension(sigma)
196 dts = s.capillary_dt()
197 s.set_dt(cfl * dts)
198 v0 = s.diagnostics.vof_diagnostics()["volume"]
199 maxit, capped = 0, 0
200 for _ in range(steps):
201 s.step()
202 it = s.diagnostics.last_pressure_iterations()
203 maxit = max(maxit, it)
204 capped += it >= PRESS_MAXIT
205 cc = np.asarray(s.get_vof())
206 d1 = s.diagnostics.vof_diagnostics()
207 z0 = int(math.ceil(zw))
208 # h: the liquid column on the axis, counting only the FLUID part of the cut wall cell
209 frac = np.clip(np.arange(nz) + 1 - zw, 0.0, 1.0)
210 h = float((cc[nx // 2, ny // 2, :] * frac).sum())
211 a = math.sqrt(float(cc[:, :, z0].sum()) / math.pi)
212 theta = math.degrees(2.0 * math.atan2(h, a))
213 rc = (a * a + h * h) / (2.0 * h)
214 p = np.asarray(s.get_field("p"))
215 px = (np.arange(nx) + 0.5)[:, None, None] - cx
216 py = (np.arange(ny) + 0.5)[None, :, None] - cy
217 pz = (np.arange(nz) + 0.5)[None, None, :] - zw
218 rr = np.sqrt(px ** 2 + py ** 2 + pz ** 2)
219 inner = (rr < 0.55 * r) & (pz > 0.5)
220 outer = (rr > 1.5 * r) & (rr < 1.8 * r) & (pz > 0.5)
221 dp = float(p[inner].mean() - p[outer].mean())
222 umax_open = max(abs(np.asarray(g())[:, :, z0 + 1:]).max() for g in (s.get_u, s.get_v, s.get_w))
223 umax_all = max(abs(np.asarray(g())).max() for g in (s.get_u, s.get_v, s.get_w))
224 print(f"G5 liquid cap on a CUT flat wall, D/dx = {diam}, {nx}x{ny}x{nz}, {steps} steps at "
225 f"{cfl} dt_sigma = {cfl * dts:.5g}")
226 print(f" volume {v0:.6f} -> {d1['volume']:.6f} (drift {(d1['volume'] - v0) / v0:.3e}); "
227 f"analytic hemisphere {2 * math.pi * r ** 3 / 3:.6f}")
228 print(f" h {h:.4f} a {a:.4f} -> apparent theta {theta:.3f} deg (target 90)")
229 print(f" cap sphere radius {rc:.4f} (target {r}); Young-Laplace dP {dp:.6f} vs "
230 f"2 sigma/R {2 * sigma / rc:.6f} (rel {abs(dp - 2 * sigma / rc) / (2 * sigma / rc):.3e})")
231 print(f" spurious currents: max|u| in the OPEN fluid {umax_open:.3e} -> "
232 f"Ca = mu max|u|/sigma = {mu * umax_open / sigma:.3e}; "
233 f"max|u| including the wall band {umax_all:.3e}")
234 print(f" (V4 free-droplet reference at the same D/dx: Ca 1.4e-5 ... 2.6e-5)")
235 print(f" pressure iters max {maxit} (capped steps {capped}); "
236 f"sum C over solid cells {d1['solid_sum']:.3e}")
237 ok = (abs(theta - 90.0) <= 3.0
238 and abs(dp - 2 * sigma / rc) / (2 * sigma / rc) <= 0.01
239 and d1["solid_sum"] == 0.0 and capped == 0)
240 print(f" G5 {'PASS' if ok else 'FAIL'}")
241 return ok
242
243
244GATES = {"g2": g2, "g4": g4, "g5": g5}
245
246if __name__ == "__main__":
247 names = [a for a in sys.argv[1:] if not a.startswith("-")] or list(GATES)
248 quick = "--quick" in sys.argv
249 results = {}
250 for nm in names:
251 fn = GATES[nm]
252 if quick and nm == "g2":
253 results[nm] = fn(n=32, nadv=200)
254 elif quick and nm == "g4":
255 results[nm] = fn(n=32, steps=150)
256 elif quick and nm == "g5":
257 results[nm] = fn(diam=16, steps=100)
258 else:
259 results[nm] = fn()
260 print()
261 print("summary: " + " ".join(f"{k}={'PASS' if v else 'FAIL'}" for k, v in results.items()))
262 sys.exit(0 if all(results.values()) else 1)
sphere_array_sdf(n, centres, radius)
g2(n=48, nadv=500, cfl=0.2)
g5(diam=24, steps=200, sigma=1.0, mu=0.05, cfl=0.5)
stokes_state(n, steps=60, mu=0.2, force=(2e-3, 1e-3, 5e-4))
g4(n=48, steps=400, ratio=10.0, grav=2e-3, momentum=True)
static double mean(const std::vector< double > &a)