flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
vof_sucking.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2"""WO-P23 rung P2 — the SUCKING INTERFACE (Welch & Wilson, JCP 160:662, 2000).
3
4A planar interface with SATURATED VAPOUR on one side and SUPERHEATED LIQUID on the other. All the
5latent heat comes from the liquid, so this is the first rung on which the LIQUID half of the
6one-sided gradient fit, the per-phase k(C) / rho c_p(C) closures and the consistent rho c_p T
7transport are all live at once (rung P1's liquid was saturated and pinned, so none of them entered).
8
9GEOMETRY. Vapour occupies x < X(t) against a wall held at T_sat; liquid occupies x > X(t) and is
10superheated to T_inf far away. The interface moves INTO the liquid and the liquid is pushed AWAY
11from it at u_l = mdot (1/rho_v - 1/rho_l), so the domain needs an OUTLET at the liquid end: the net
12vapour production is a real volume flux that has to leave. (WO-P01's closed-column trick with a
13balancing sink cannot be used here — the sink removes VOLUME from cells whose colour is 1, so the
14colour drops there, and over a run this long the required removal is many cells' worth.)
15
16WHY THE OUTLET IS SOUND AT HIGH DENSITY RATIO. `applyBoundaryOpenness` re-imposes the literal 1.0
17at a boundary face instead of the variable-density coefficient `open * rho0/rho_f` (WO-R2 item 1).
18Here the outlet sits in PURE LIQUID, so choosing the reference density `set_rho(rho_l)` makes
19`rho0/rho_f == 1` exactly at that face and the overwrite is the correct coefficient — the defect is
20side-stepped by construction rather than tolerated. Reported with every run.
21
22THE EXACT SOLUTION (derived in the docstring of `exact_b`, checked against Welch & Wilson):
23
24 X(t) = 2 b sqrt(alpha_l t) , b = Ja exp(-g^2) / (sqrt(pi) erfc(g)) , g = b rho_v/rho_l
25 Ja = rho_l c_pl (T_inf - T_sat) / (rho_v h_lv)
26 T(x,t) = T_inf - (T_inf - T_sat) erfc(s)/erfc(g), s = x/(2 sqrt(alpha_l t)) - b(1 - rho_v/rho_l)
27
28Usage:
29 PYTHONPATH=$PWD/build_cuda python tests/study/vof_sucking.py [--ratio 10] [--ns 64,128,256]
30
31`--mu-ratio R` (default: none = the constant mu 1e-3) adds the closure mu = mu_l (R + (1 - R) C),
32mu_v/mu_l = R; `--viscous-stress auto|full|laplacian` then selects the form (default: the solver's
33AUTO, no setter call). Review S2 (validation/REVIEW_FULLSTRESS.md): phase change makes div u^n =
34mdot [1/rho] != 0, outside the full stress's §4.2/§7 premises, so it is gated at R = 0.1.
35"""
36import argparse
37import math
38import sys
39
40import numpy as np
41
42import peclet.flow as pf
43
44
45def exact_b(ja, rr):
46 """Solve b = Ja exp(-g^2)/(sqrt(pi) erfc(g)), g = b*rr, rr = rho_v/rho_l.
47
48 Derivation. The vapour is isothermal at T_sat (it is bounded by a wall at T_sat), so it is at
49 rest and mdot = rho_v Xdot. The liquid moves at the uniform u_l = Xdot (1 - rho_v/rho_l), so in
50 the Galilean frame x' = x - integral(u_l) the liquid temperature obeys the plain heat equation
51 and is an erfc in x'/(2 sqrt(alpha_l t)). Matching T(X) = T_sat fixes the amplitude and the
52 Stefan condition mdot h_lv = k_l dT/dx|_X gives the fixed point above.
53 """
54 b = ja / math.sqrt(math.pi)
55 for _ in range(200):
56 g = b * rr
57 b = ja * math.exp(-g * g) / (math.sqrt(math.pi) * math.erfc(g))
58 return b
59
60
61def run(n, ratio, ja, x0p=0.10, xep=0.25, alpha_l=1.0, cfl=0.2, fo=0.5, nt=4,
62 kratio=None, rcpratio=None, consistent=True, plane=True, quad=False, muscl=False,
63 verbose=True, energy_order=1, deposit=None, mu_ratio=None, stress=None):
64 """One resolution. The PHYSICAL problem is fixed (domain length 1, alpha_l = 1, the vapour
65 layer growing from x0p to xep of the domain) and only h = 1/n changes. `nt` is the transverse
66 extent: the problem is 1-D so 4 is enough physically, but it decides how far the pressure
67 multigrid can coarsen (see the findings)."""
68 ny = nz = nt
69 rr = 1.0 / ratio # rho_v / rho_l
70 b = exact_b(ja, rr)
71 t0 = (x0p / (2 * b)) ** 2 / alpha_l
72 te = (xep / (2 * b)) ** 2 / alpha_l
73
74 # solver (cell) units: h = 1, so alpha in cells^2/s is alpha_l * n^2
75 al = alpha_l * n * n
76 rho_l, rho_v = 1.0, rr
77 cpl = 1.0
78 k_l = al * rho_l * cpl # alpha_l = k/(rho c_p)
79 rcp_l = rho_l * cpl
80 rcp_v = rcp_l / (rcpratio if rcpratio else ratio) # default: c_p equal, so rcp ~ rho
81 k_v = k_l / (kratio if kratio else ratio)
82 dT = 1.0
83 h_lv = rho_l * cpl * dT / (rho_v * ja)
84
85 X0 = x0p * n
86 s = pf.Solver(n, ny, nz)
87 s.set_rho(rho_l) # reference density = the OUTLET phase; see the module docstring
88 s.set_mu(1e-3)
89 s.set_advection(False)
90 s.set_domain_bc("-x", "wall") # -x wall (the vapour side)
91 s.set_domain_bc("+x", "outflow") # +x outflow (the liquid leaves)
92 s.set_pressure_geometry(np.full((n, ny, nz), 1.0, order="F"))
93 s.enable_vof()
94 c = np.zeros((n, ny, nz), order="F")
95 for i in range(n):
96 c[i, :, :] = min(1.0, max(0.0, (i + 1) - X0)) # liquid on the HIGH side
97 s.set_vof(c)
98 s.set_property_model("rho", "linear", "C", [rho_v, rho_l - rho_v])
99 if mu_ratio is not None: # review S2: a mu jump at the interface
100 s.set_property_model("mu", "linear", "C", [1e-3 * mu_ratio, 1e-3 * (1.0 - mu_ratio)])
101 if stress is not None:
102 s.set_viscous_stress(stress)
103 s.set_pressure_fcg(True, 4000, 1e-10) # AFTER the rho closure (CLAUDE.md)
104
105 # temperature: T_sat = 0 at the wall, the similarity profile in the liquid
106 t = np.zeros((n, ny, nz), order="F")
107 for i in range(n):
108 x = (i + 0.5) / n
109 if x > x0p:
110 sv = x / (2 * math.sqrt(alpha_l * t0)) - b * (1.0 - rr)
111 t[i, :, :] = dT - dT * math.erfc(sv) / math.erfc(b * rr)
112 # The far-field Dirichlet is the EXACT profile value at x = 1, refreshed every step. The domain
113 # is finite and the similarity solution is not: at t_end the liquid thermal layer reaches
114 # erfc(s) ~ 8e-3 at the outlet, so a fixed T = T_inf there is a 0.86 % TRUNCATION error that
115 # does not converge under grid refinement (measured: |T - T_exact| flat at 0.838 % / 0.834 % at
116 # N = 64 / 128 with the fixed value). Imposing the exact far-field value removes a
117 # domain-truncation error, not a discretisation error.
118 def farT(tt):
119 sv = 1.0 / (2 * math.sqrt(alpha_l * tt)) - b * (1.0 - rr)
120 return dT - dT * math.erfc(sv) / math.erfc(b * rr)
121
122 s.add_scalar("T", al, "koren", 60)
123 s.set_scalar_bc("T", "-x", "dirichlet", 0.0) # saturated vapour against the wall
124 s.set_scalar_bc("T", "+x", "dirichlet", farT(t0)) # superheated far field (exact, refreshed per step)
125 s.set_field("T", t)
126 s.enable_phase_change(rho_v, rho_l, h_lv)
127 s.set_phase_change_thermal(True, "T", 0.0, k_v, k_l, 0.0)
128 s.diagnostics.set_phase_change_plane_dirichlet(plane)
129 s.diagnostics.set_phase_change_quadratic_fit(quad)
130 if consistent:
131 s.set_phase_change_energy(True, rcp_v, rcp_l)
132 s.diagnostics.set_phase_change_energy_muscl(muscl)
133 # WO-P3g: the second-order interfacial energy operator (default 1 = the shipped scheme, bitwise)
134 if energy_order != 1:
135 s.diagnostics.set_phase_change_energy_order(energy_order)
136 if deposit is not None:
137 s.diagnostics.set_phase_change_deposit_fallback(bool(deposit))
138
139 # initial liquid velocity (the projection would find it anyway; this removes a startup transient)
140 Xdot = b * math.sqrt(alpha_l / t0) * n # cells/s
141 ul = Xdot * (1.0 - rr)
142 u = np.zeros((n, ny, nz), order="F")
143 u[int(X0) + 2:, :, :] = ul
144 s.set_field("u", u)
145
146 # dt from a FIXED diffusive number Fo = alpha dt/h^2 (so dt ~ h^2 and the backward-Euler /
147 # explicit-mdot time error cannot contaminate a first- or second-order SPATIAL statement — the
148 # same protocol as the P1 Stefan ladder), capped by the interface-local Courant number.
149 tcur, nsteps, itmax, capped = t0, 0, 0, 0
150 while tcur < te:
151 Xd = b * math.sqrt(alpha_l / tcur) * n
152 dt = min(cfl / max(Xd * (1.0 - rr), 1e-30), fo / al)
153 dt = min(dt, (te - tcur))
154 s.set_dt(dt)
155 s.set_scalar_bc("T", "+x", "dirichlet", farT(tcur + dt))
156 s.step()
157 tcur += dt
158 nsteps += 1
159 it = s.diagnostics.last_pressure_iterations()
160 itmax = max(itmax, it)
161 if it >= 4000:
162 capped += 1
163 layer = n - s.get_vof().sum() / (ny * nz) # vapour thickness in cells
164 exact = 2 * b * math.sqrt(alpha_l * te) * n
165 d = s.diagnostics.phase_change_diagnostics()
166 # temperature profile error against the similarity solution, over the LIQUID
167 tnum = s.get_field("T")[:, 0, 0]
168 terr, tn = 0.0, 0
169 for i in range(n):
170 x = (i + 0.5) / n
171 xg = 2 * b * math.sqrt(alpha_l * te)
172 if x > xg + 2.0 / n:
173 sv = x / (2 * math.sqrt(alpha_l * te)) - b * (1.0 - rr)
174 ex = dT - dT * math.erfc(sv) / math.erfc(b * rr)
175 terr = max(terr, abs(tnum[i] - ex))
176 tn += 1
177 if verbose:
178 print(f" N = {n:4d} steps {nsteps:5d} b {b:.6f} layer {layer:9.5f} cells "
179 f"(exact {exact:9.5f}) rel {100*(layer-exact)/exact:+8.4f} %")
180 print(f" |T - T_exact|_inf over the liquid = {terr:.4e} ({100*terr/dT:.3f} % of "
181 f"dT, {tn} cells) C in [{d['min_C']:.2e}, {d['max_C']:.6f}]")
182 print(f" pressure iters max {itmax}/4000 (capped steps {capped}), "
183 f"band_div {d['band_div']:.3e}, T in [{d['T_min']:.4e}, {d['T_max']:.6f}], "
184 f"unresolved {d['unresolved']:.1e}, fallback {d['fallback_cells']}")
185 return abs((layer - exact) / exact), terr / dT, capped, itmax
186
187
188def main():
189 ap = argparse.ArgumentParser()
190 ap.add_argument("--ratio", type=float, default=10.0)
191 ap.add_argument("--ja", type=float, default=1.0)
192 ap.add_argument("--ns", type=str, default="64,128,256")
193 ap.add_argument("--kratio", type=float, default=None)
194 ap.add_argument("--rcpratio", type=float, default=None)
195 ap.add_argument("--no-consistent", action="store_true")
196 ap.add_argument("--no-plane", action="store_true")
197 ap.add_argument("--no-quad", action="store_true")
198 ap.add_argument("--fo", type=float, default=0.5)
199 ap.add_argument("--muscl", action="store_true")
200 ap.add_argument("--nt", type=int, default=4)
201 ap.add_argument("--mu-ratio", type=float, default=None)
202 ap.add_argument("--viscous-stress", type=str, default=None)
203 ap.add_argument("--energy-order", type=int, default=1,
204 help="WO-P3g: 2 = the second-order interfacial energy operator")
205 ap.add_argument("--deposit-fallback", type=int, default=None,
206 help="WO-P3f open item 6: the 5^3 divergence-source deposit fallback")
207 a = ap.parse_args()
208 ns = [int(x) for x in a.ns.split(",")]
209 print(f"P2 sucking interface: rho_l/rho_v = {a.ratio:g}, Ja = {a.ja:g}, "
210 f"k ratio {a.kratio or a.ratio:g}, rho*c_p ratio {a.rcpratio or a.ratio:g}, "
211 f"consistent energy {not a.no_consistent}, plane Dirichlet {not a.no_plane}, "
212 f"quadratic fit {not a.no_quad}, Fo {a.fo:g}, WO-P3g energy order {a.energy_order}, "
213 f"deposit fallback {a.deposit_fallback}, mu ratio {a.mu_ratio}, viscous stress "
214 f"{a.viscous_stress or 'solver default'}")
215 errs, terrs = [], []
216 for n in ns:
217 e, te_, capped, itmax = run(n, a.ratio, a.ja, kratio=a.kratio, rcpratio=a.rcpratio,
218 consistent=not a.no_consistent, plane=not a.no_plane,
219 quad=not a.no_quad, fo=a.fo, muscl=a.muscl, nt=a.nt,
220 energy_order=a.energy_order, deposit=a.deposit_fallback,
221 mu_ratio=a.mu_ratio, stress=a.viscous_stress)
222 errs.append(e)
223 terrs.append(te_)
224 if capped:
225 print(f" *** {capped} CAPPED pressure solves at N = {n}: rule 3b, run INVALID")
226 for x, y, na, nb in zip(errs[:-1], errs[1:], ns[:-1], ns[1:]):
227 print(f" observed order {na} -> {nb}: {math.log2(x/y):.3f}")
228 if len(errs) > 2:
229 print(f" observed order {ns[0]} -> {ns[-1]} (fit): "
230 f"{math.log2(errs[0]/errs[-1])/math.log2(ns[-1]/ns[0]):.3f}")
231 print(f" GATE: order >= 1.4 and |T - T_exact| <= 1 % of dT at N = {ns[-1]} "
232 f"(got {100*terrs[-1]:.3f} %)")
233 return 0
234
235
236if __name__ == "__main__":
237 sys.exit(main())
exact_b(ja, rr)
run(n, ratio, ja, x0p=0.10, xep=0.25, alpha_l=1.0, cfl=0.2, fo=0.5, nt=4, kratio=None, rcpratio=None, consistent=True, plane=True, quad=False, muscl=False, verbose=True, energy_order=1, deposit=None, mu_ratio=None, stress=None)