flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_vof_blocks_physical_units.py
Go to the documentation of this file.
1#!/usr/bin/env python
2"""Gate: block VoF with CSF gives the same flow in a physical domain as in cell units.
3
4Why this test exists. Two unit bugs were reachable only under `Solver(cells, extent=...)` with the
5block container (`enable_vof_blocks` + `enable_vof_block_csf`), and nothing ran that combination:
6 1. step() handed its INTERNAL dt to the physical-unit block advector, which converted it again;
7 with tRef pinned by the first set_dt the first step threw the Weymouth-Yue cap.
8 2. enable_vof_block_csf() copied the internal sigma into the container before the first set_dt
9 pinned tRef; the block CSF then ran 1/tRef^2 (~1e9 here) too strong.
10In cell units tRef = 1, so both are invisible there (flow ac09b68).
11
12The case: one block-marker bubble (d = 10 h) in a periodic 16^3 box, the case-A groups of the
13bubble-swarm validation (Ar 29.9, Bo 2, density and viscosity ratio 0.1), buoyancy with the mixture
14weight removed, the volume-mean u_x held at 0. It runs twice: in cell units (dt = 1, rho_l = 1), and
15in a physical box (h = 1e-4 m, rho_l = 1000 kg/m^3, dt = 2.5e-5 s) whose every input is the cell-unit
16value times its unit scale. After STEPS steps u, v, w, p (converted with `unit_scales`) and C must
17match to <= 1e-12 relative to each field's maximum. set_dt comes after enable_vof_block_csf on
18purpose: that is the drivers' order (tests/study/case_a) and the one that exposed bug 2.
19
20Run: OMP_NUM_THREADS=4 OMP_PROC_BIND=false PYTHONPATH=<build> \
21 python tests/python/test_vof_blocks_physical_units.py
22Exit 0 pass, 1 fail, 77 skipped (no module).
23"""
24import math
25import sys
26
27import numpy as np
28
29N, D_CELLS, STEPS, RTOL = 16, 10.0, 30, 1e-12
30AR, BO, RHO_RATIO, MU_RATIO = 29.9, 2.0, 0.1, 0.1
31# Cell units: rho_l = 1, dt = 1; g makes dt = 1 about a quarter of the capillary limit.
32G_CELL = 6.0e-5
33SIGMA_CELL = (1.0 - RHO_RATIO) * G_CELL * D_CELLS**2 / BO
34MU_CELL = math.sqrt((1.0 - RHO_RATIO) * G_CELL * D_CELLS**3) / AR
35# The physical realisation: deliberately not powers of two.
36H, RHO_REF, T_REF = 1.0e-4, 1000.0, 2.5e-5
37
38
39def build(pf, phys):
40 """The same case; `phys` scales every input from cell units to SI."""
41 sl = H if phys else 1.0 # length
42 sr = RHO_REF if phys else 1.0 # density
43 st = T_REF if phys else 1.0 # time
44 rho_l, rho_g = sr, RHO_RATIO * sr
45 mu_l = MU_CELL * sr * sl * sl / st
46 sigma = SIGMA_CELL * sr * sl**3 / (st * st)
47 g = G_CELL * sl / (st * st)
48 s = pf.Solver((N, N, N), extent=(N * H,) * 3) if phys else pf.Solver(N, N, N)
49 # pre-flip default (Stokes) kept; NS physics: CHANGELOG "advection default"
50 s.set_advection(False)
51 s.set_rho(rho_l)
52 s.set_mu(mu_l)
53 s.set_pressure_geometry(np.full((N, N, N), 1.0, order="F"))
54 s.enable_vof()
55 s.set_vof(np.zeros((N, N, N), order="F"))
56 s.set_property_model("rho", "linear", "C", [rho_l, rho_g - rho_l])
57 s.set_property_model("mu", "linear", "C", [mu_l, MU_RATIO * mu_l - mu_l])
58 s.set_surface_tension(sigma)
59 s.diagnostics.set_pressure_vcycle_precision("fp64") # FP32 weights are not scale-covariant
60 s.set_pressure_pcg(True, 800, 1e-10)
61 c = 0.5 * N # grid coordinates, as enable_vof_blocks takes
62 s.enable_vof_blocks([(c, c, c, 0.5 * D_CELLS)])
63 s.enable_vof_block_csf()
64 alpha = float(np.mean(s.get_vof()))
65 rho_av = rho_l + (rho_g - rho_l) * alpha
66 s.set_property_model("force_x", "linear", "C", [-(rho_l - rho_av) * g, -(rho_g - rho_l) * g])
67 s.set_superficial_velocity(True, "x", 0.0)
68 s.update_properties()
69 s.set_dt(st) # the first set_dt pins tRef (after the CSF)
70 return s
71
72
73def fields(s):
74 us = s.unit_scales
75 vel = us["velocity_to_internal"]
76 return {"u": s.get_u() * vel[0], "v": s.get_v() * vel[1], "w": s.get_w() * vel[2],
77 "p": s.get_p() * us["pressure_to_internal"], "C": s.get_vof()}
78
79
80def main():
81 try:
82 import peclet.flow as pf
83 except ImportError:
84 print("SKIP: peclet.flow not importable (set PYTHONPATH to the build tree)")
85 return 77
86 ref, phy = build(pf, False), build(pf, True)
87 us = phy.unit_scales
88 try:
89 assert not us["identity"], "the extent did not give a physical domain"
90 for k, v in (("h_ref", H), ("rho_ref", RHO_REF), ("t_ref", T_REF)):
91 assert abs(us[k] / v - 1.0) <= 1e-14, f"unit_scales[{k!r}] = {us[k]!r}, expected {v}"
92 cap = ref.vof_step_limits()["capillary_dt"]
93 assert cap >= 2.0, f"dt = 1 is not well inside the capillary limit ({cap:.3f})"
94 for n in range(STEPS):
95 ref.step()
96 try:
97 phy.step()
98 except RuntimeError as e:
99 raise AssertionError(f"physical run threw at step {n + 1}: {e}") from e
100 a, b = fields(ref), fields(phy)
101 worst = 0.0
102 for k in a:
103 scale = float(np.max(np.abs(a[k])))
104 err = float(np.max(np.abs(b[k] - a[k]))) / scale
105 print(f" {k}: max |cell| {scale:.4e} physical vs cell {err:.2e}")
106 assert err <= RTOL, f"{k}: physical vs cell units differ by {err:.3e} > {RTOL:g}"
107 worst = max(worst, err)
108 assert float(np.max(np.abs(a["u"]))) > 1e-4, "the bubble did not move: the test is vacuous"
109 except AssertionError as e:
110 print(f"FAIL: {e}")
111 return 1
112 print(f"OK: block VoF + CSF in a physical domain == cell units over {STEPS} steps "
113 f"(worst {worst:.2e}, capillary dt {cap:.2f})")
114 return 0
115
116
117if __name__ == "__main__":
118 sys.exit(main())