flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_vof_momentum_rho_names.py
Go to the documentation of this file.
1#!/usr/bin/env python
2"""Gate: enable_vof_momentum takes (rho_c0, rho_c1); the former keywords rho_gas / rho_liquid work.
3
4The pair is (density at C = 0, density at C = 1), whichever phase that is. The old names assumed
5C = 1 marks the liquid and misled callers whose C = 1 is the gas (the Q6 runs). `rho_c0=`/`rho_c1=`
6is the canonical spelling (suite/docs/NAMING.md section 0.1); `rho_gas=`/`rho_liquid=` stay as an
7overload for this release.
8
9Checks, on a 12^3 periodic drop moving through a density-ratio-10 fluid for a few steps:
10 1. positional, rho_c0=/rho_c1= and rho_gas=/rho_liquid= give bitwise identical u, v, w, p, C;
11 2. mixed keywords (rho_c0=, rho_liquid=) raise TypeError;
12 3. a pair swapped against the registered 'rho' closure raises, and the message names rho_c0 and
13 rho_c1 and says SWAPPED.
14
15Run: OMP_NUM_THREADS=4 OMP_PROC_BIND=false PYTHONPATH=<build> \
16 python tests/python/test_vof_momentum_rho_names.py
17Exit 0 pass, 1 fail, 77 skipped (no module).
18"""
19import sys
20
21import numpy as np
22
23N, STEPS = 12, 5
24RHO_C0, RHO_C1 = 1.0, 10.0
25CALLS = {
26 "positional": lambda s: s.enable_vof_momentum(RHO_C0, RHO_C1),
27 "canonical keywords": lambda s: s.enable_vof_momentum(rho_c0=RHO_C0, rho_c1=RHO_C1),
28 "former keywords": lambda s: s.enable_vof_momentum(rho_gas=RHO_C0, rho_liquid=RHO_C1),
29}
30
31
32def build(pf, enable):
33 s = pf.Solver(N, N, N)
34 s.set_rho(RHO_C1)
35 s.set_mu(0.05)
36 s.set_dt(0.2)
37 s.set_advection(False)
38 s.set_pressure_geometry(np.full((N, N, N), 1.0, order="F"))
39 s.set_pressure_pcg(True, 800, 1e-10)
40 s.enable_vof()
41 x = (np.arange(N) + 0.5)[:, None, None]
42 y = (np.arange(N) + 0.5)[None, :, None]
43 z = (np.arange(N) + 0.5)[None, None, :]
44 r2 = (x - 0.5 * N) ** 2 + (y - 0.5 * N) ** 2 + (z - 0.5 * N) ** 2
45 s.set_vof(np.asfortranarray(np.clip(0.5 + 3.0 - np.sqrt(r2), 0.0, 1.0)))
46 s.set_property_model("rho", "linear", "C", [RHO_C0, RHO_C1 - RHO_C0])
47 if enable is not None:
48 enable(s)
49 one = np.ones((N, N, N), order="F")
50 s.set_state(0.5 * one, 0.3 * one, 0.0 * one)
51 return s
52
53
54def run(pf, enable):
55 s = build(pf, enable)
56 for _ in range(STEPS):
57 s.step()
58 assert s.vof_momentum_enabled()
59 return {"u": s.get_u(), "v": s.get_v(), "w": s.get_w(), "p": s.get_p(), "C": s.get_vof()}
60
61
62def main():
63 try:
64 import peclet.flow as pf
65 except ImportError:
66 print("SKIP: peclet.flow not importable (set PYTHONPATH to the build tree)")
67 return 77
68 try:
69 ref = run(pf, CALLS["positional"])
70 assert float(np.max(np.abs(ref["u"] - 0.5))) > 0.0 or float(np.max(np.abs(ref["p"]))) > 0.0, \
71 "the fields did not evolve: the test is vacuous"
72 for name in ("canonical keywords", "former keywords"):
73 got = run(pf, CALLS[name])
74 for k in ref:
75 assert np.array_equal(ref[k], got[k]), f"{name}: field {k} differs from positional"
76 print(f" {name}: bitwise identical to positional")
77 for bad in (lambda s: s.enable_vof_momentum(rho_c0=RHO_C0, rho_liquid=RHO_C1),
78 lambda s: s.enable_vof_momentum(rho_gas=RHO_C0, rho_c1=RHO_C1)):
79 try:
80 bad(build(pf, None))
81 except TypeError:
82 pass
83 else:
84 raise AssertionError("mixed keywords did not raise TypeError")
85 print(" mixed keywords: TypeError")
86 try:
87 build(pf, lambda s: s.enable_vof_momentum(RHO_C1, RHO_C0))
88 except RuntimeError as e:
89 msg = str(e)
90 assert "rho_c0" in msg and "rho_c1" in msg and "SWAPPED" in msg, f"message: {msg}"
91 assert "rho_gas" not in msg and "rho_liquid" not in msg, f"message: {msg}"
92 else:
93 raise AssertionError("a swapped pair against the 'rho' closure did not raise")
94 print(" swapped pair: error names rho_c0 / rho_c1")
95 except AssertionError as e:
96 print(f"FAIL: {e}")
97 return 1
98 print("OK: enable_vof_momentum(rho_c0, rho_c1) == positional == former keywords")
99 return 0
100
101
102if __name__ == "__main__":
103 sys.exit(main())