flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
vof_collocated.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2"""VoF rung V8 (WO-T) — the staggered/collocated physics battery, both columns.
3
4The collocated (`SolverColocated`) path is the **ABC approximate projection**: average the cell
5velocities onto a MAC face field, project THAT exactly, correct the cell field. Rung V8 adds
6
7 * variable density — the face coefficient ``c_f = o_f rho0/rho_f`` and ``projectCorrectVar`` on the
8 FACE field, with the CELL correction the AVERAGE OF THE TWO FACE CORRECTIONS of each axis;
9 * forces as FACE accelerations ``a_f = dt (f_f - (P(i)-P(i-s)))/rho_f`` added after
10 ``centerToFace``, the cell taking the average of the two faces' total increment (Basilisk
11 ``centered.h``; Popinet JCP 2009 §3);
12 * colour advection from ``uf_/vf_/wf_``, the field the approximate projection makes exactly
13 divergence-free.
14
15What this script measures, staggered vs collocated:
16
17 G3 static droplet with the COMPUTED curvature — the spurious capillary number
18 ``Ca = mu max|u| / sigma`` at D/dx = 16 (and 8, 24).
19 G4 Hysing case 1 (density ratio 10) — max rise velocity and y_c(3). The collocated column runs
20 WITHOUT momentum consistency (`enable_vof_momentum` is staggered-only at this rung — the
21 collocated construction needs Favre face states), so the honest staggered comparison is the
22 one with it OFF; both staggered columns are printed.
23 G5 capillary wave at 32 cells/lambda — the frequency against the exact viscous two-fluid mode.
24
25Every gate records the pressure iteration count against its cap (preamble rule 3b: a capped solve
26makes the run INVALID) and, on the collocated grid, BOTH the cell max|u| and the FACE max|uf|: the
27cell field carries the approximate projection's invisible checkerboard, which `centerToFace`
28annihilates and the projection therefore cannot remove.
29
30Usage: PYTHONPATH=<build> python tests/study/vof_collocated.py [static|hysing1|wave] [--quick]
31"""
32import math
33import os
34import sys
35import time
36
37import numpy as np
38
39import peclet.flow as pf
40
41sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
42from vof_capillary_references import wave_mode # noqa: E402
43from vof_surface_tension import (HYSING, Scale, Solve, cylinder_fractions, # noqa: E402
44 maxvel, sphere_fractions, zero_crossings)
45
46QUICK = "--quick" in sys.argv
47GATES = [a for a in sys.argv[1:] if not a.startswith("--")]
48
49GRIDS = (("staggered ", pf.Solver), ("collocated", pf.SolverColocated))
50
51
53 return max(np.abs(s.get_uf()).max(), np.abs(s.get_vf()).max(), np.abs(s.get_wf()).max())
54
55
56# --------------------------------------------------------------------------- G3 static droplet
58 print("\n" + "=" * 100)
59 print("G3 STATIC DROPLET, computed curvature — Ca = mu max|u| / sigma, both grids")
60 print("=" * 100)
61 print(" The balanced-force identity is exact on both grids (the ctest's T2: the FACE field")
62 print(" stays at machine zero with a constant kappa). What is left is the curvature error.")
63 print(" On the collocated grid the CELL number additionally carries the invisible")
64 print(" checkerboard, so both are printed.\n")
65 rungs = [(16, 4.0), (32, 8.0)] if QUICK else [(16, 4.0), (32, 8.0), (48, 12.0)]
66 for n, R in rungs:
67 row = {}
68 for label, cls in GRIDS:
69 s = cls(n, n, n)
70 s.set_rho(1.0)
71 s.set_mu(0.1)
72 s.set_advection(False)
73 s.set_pressure_geometry(np.full((n, n, n), 10.0, order="F"))
74 s.set_pressure_chebyshev(True, 500, 1e-14)
75 s.enable_vof()
76 s.set_vof(sphere_fractions((n, n, n), R, (n / 2 + 0.13, n / 2 + 0.27, n / 2 + 0.11)))
77 s.set_property_model("rho", "linear", "C", [1.0, 0.0])
78 s.set_surface_tension(1.0)
79 s.set_dt(0.5 * s.capillary_dt())
80 h = Solve(500)
81 for _ in range(60):
82 s.step()
83 h.sample(s)
84 ca = 0.1 * maxvel(s) / 1.0
85 caf = 0.1 * maxfacevel(s) / 1.0
86 row[label] = (ca, caf, h)
87 print(f" D/dx = {2*R:5.1f} {label} Ca(cell) = {ca:.3e} Ca(face) = {caf:.3e}")
88 print(f" {h}")
89 st, co = row["staggered "][0], row["collocated"][0]
90 print(f" D/dx = {2*R:5.1f} collocated / staggered (cell Ca) = {co/st:.2f} x\n")
91
92
93# --------------------------------------------------------------------------- G5 capillary wave
95 print("\n" + "=" * 100)
96 print("G5 CAPILLARY WAVE, 32 cells/lambda — omega against the EXACT viscous two-fluid mode")
97 print("=" * 100)
98 rungs = [(32, 0.005)] if QUICK else [(32, 0.005), (64, 0.005)]
99 for nx, nu in rungs:
100 nz, ny, rho, sigma = nx, 4, 1.0, 1.0
101 lam = float(nx)
102 k = 2 * math.pi / lam
103 a0 = lam / 100.0
104 omega_th = math.sqrt(sigma * k ** 3 / (2 * rho))
105 s_ex, _ = wave_mode(k, nu, sigma, rho, H=nz / 2.0)
106 omega_ex, gamma_ex = s_ex.imag, -s_ex.real
107 for label, cls in GRIDS:
108 s = cls(nx, ny, nz)
109 s.set_rho(rho)
110 s.set_mu(nu * rho)
111 s.set_advection(False)
112 s.set_domain_bc("-z", "wall", (0, 0, 0))
113 s.set_domain_bc("+z", "wall", (0, 0, 0))
114 s.set_pressure_geometry(np.full((nx, ny, nz), 10.0, order="F"))
115 s.set_pressure_chebyshev(True, 500, 1e-11)
116 s.enable_vof()
117 xe = np.arange(nx + 1)
118 zi = nz / 2.0 + a0 * (np.sin(k * xe[1:]) - np.sin(k * xe[:-1])) / (k * 1.0)
119 C = np.zeros((nx, ny, nz))
120 for kk in range(nz):
121 C[:, :, kk] = np.clip(zi - kk, 0.0, 1.0)[:, None]
122 s.set_vof(np.asfortranarray(C))
123 s.set_property_model("rho", "linear", "C", [rho, 0.0])
124 s.set_surface_tension(sigma)
125 dt = 0.5 * s.capillary_dt()
126 s.set_dt(dt)
127 nsteps = int(2.5 * (2 * math.pi / omega_th) / dt)
128 h = Solve(500)
129 t, amp = [], []
130 for i in range(nsteps):
131 s.step()
132 h.sample(s)
133 col = s.get_vof().sum(axis=2)[:, 0]
134 t.append((i + 1) * dt)
135 amp.append(float(np.dot(col - nz / 2.0,
136 np.cos(k * (np.arange(nx) + 0.5))) * 2.0 / nx))
137 t, amp = np.array(t), np.array(amp)
138 zc = zero_crossings(t, amp)
139 omega_num = math.pi / np.mean(np.diff(zc)) if len(zc) >= 3 else float("nan")
140 pk = [i for i in range(1, len(amp) - 1)
141 if abs(amp[i]) > abs(amp[i - 1]) and abs(amp[i]) > abs(amp[i + 1])]
142 gamma_num = (-math.log(abs(amp[pk[-1]]) / abs(amp[pk[0]])) / (t[pk[-1]] - t[pk[0]])
143 if len(pk) >= 2 else float("nan"))
144 print(f" nx = {nx} {label} omega {omega_num:.5f} vs inviscid {omega_th:.5f} "
145 f"({100*(omega_num/omega_th-1):+.2f} %) vs EXACT viscous {omega_ex:.5f} "
146 f"({100*(omega_num/omega_ex-1):+.2f} %)")
147 print(f" gamma {gamma_num:.3e} vs exact {gamma_ex:.3e} "
148 f"({100*(gamma_num/gamma_ex-1):+.1f} %); {h}")
149
150
151# --------------------------------------------------------------------------- G4 Hysing case 1
152def gate_hysing1(nx=64):
153 p = HYSING[1]
154 print("\n" + "=" * 100)
155 print(f"G4 HYSING RISING BUBBLE case 1 (ratio 10), nx = {nx} — both grids")
156 print("=" * 100)
157 print(" `enable_vof_momentum` is STAGGERED-ONLY at this rung (the collocated construction")
158 print(" needs Favre face states, AMR-Wind's pattern), so the collocated column runs without")
159 print(" it and the honest staggered comparison is the momentum-consistency-OFF one. Both")
160 print(" staggered columns are printed; V2b is worth ~14 % here.\n")
161 Lx = 1.0
162 nz, ny = 2 * nx, 4
163 sc = Scale(nx / Lx)
164 R = sc.len_to_cells(0.25)
165 runs = [("staggered (V2b ON )", pf.Solver, True),
166 ("staggered (V2b OFF)", pf.Solver, False),
167 ("COLLOCATED (V2b n/a)", pf.SolverColocated, False)]
168 out = {}
169 for label, cls, vmom in runs:
170 s = cls(nx, ny, nz)
171 s.set_rho(p["rho1"])
172 s.set_mu(sc.mu(p["mu1"]))
173 s.set_advection(False) # the pre-flip default (Stokes) kept; NS physics: CHANGELOG "advection default"
174 s.set_domain_bc("-z", "wall", (0, 0, 0))
175 s.set_domain_bc("+z", "wall", (0, 0, 0))
176 s.set_pressure_geometry(np.full((nx, ny, nz), 10.0, order="F"))
177 s.set_pressure_chebyshev(True, 600, 1e-12)
178 s.enable_vof()
179 C = 1.0 - cylinder_fractions((nx, ny, nz), R, nx / 2.0, sc.len_to_cells(0.5))
180 s.set_vof(np.asfortranarray(C))
181 s.set_property_model("rho", "linear", "C", [p["rho2"], p["rho1"] - p["rho2"]])
182 s.set_property_model("mu", "linear", "C",
183 [sc.mu(p["mu2"]), sc.mu(p["mu1"] - p["mu2"])])
184 s.set_surface_tension(sc.sigma(p["sigma"]))
185 if vmom:
186 s.enable_vof_momentum(p["rho2"], p["rho1"])
187 s.set_property_model("force_z", "linear", "C",
188 [-sc.bodyforce(p["rho2"] * p["g"]),
189 -sc.bodyforce((p["rho1"] - p["rho2"]) * p["g"])])
190 T = 3.0
191 dt = 0.5 * s.capillary_dt()
192 s.set_dt(dt)
193 zs = (np.arange(nz) + 0.5) / sc.s
194 h = Solve(600)
195 vmax, tvmax, yc, t, i = 0.0, 0.0, 0.0, 0.0, 0
196 t0 = time.time()
197 while t < T:
198 if i % 10 == 0:
199 L = s.vof_step_limits()
200 dt = 0.4 * min(L["cfl_dt"], L["capillary_dt"])
201 s.set_dt(dt)
202 t += dt
203 i += 1
204 s.step()
205 h.sample(s)
206 B = 1.0 - s.get_vof()
207 vb = B.sum()
208 yc = float((B.sum(axis=(0, 1)) * zs).sum() / vb)
209 vrise = float((B * s.get_w()).sum() / vb) / sc.s
210 if vrise > vmax:
211 vmax, tvmax = vrise, t
212 out[label] = (vmax, tvmax, yc)
213 print(f" {label} v_rise max {vmax:.4f} at t = {tvmax:.3f} y_c(3) = {yc:.4f} "
214 f"({i} steps, {time.time()-t0:.0f} s)")
215 print(f" {h}")
216 print(f"\n published reference (Hysing 2009): v_rise max {p['ref_vmax']:.4f} at "
217 f"t = {p['ref_tvmax']:.4f}, y_c(3) = {p['ref_yc']:.4f}")
218 a = out["staggered (V2b OFF)"]
219 b = out["COLLOCATED (V2b n/a)"]
220 print(f" collocated vs staggered (both V2b off): v_rise {100*(b[0]/a[0]-1):+.2f} %, "
221 f"y_c(3) {100*(b[2]/a[2]-1):+.2f} %")
222
223
224ALL = {"static": gate_static, "wave": gate_wave,
225 "hysing1": lambda: gate_hysing1(32 if QUICK else 64)}
226
227if __name__ == "__main__":
228 run = GATES or list(ALL)
229 for g in run:
230 if g not in ALL:
231 print(f"unknown gate {g!r}; known: {', '.join(ALL)}")
232 sys.exit(2)
233 for g in run:
234 ALL[g]()
235 print("\ndone.")