flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
d1_tolerance.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2"""D1 -- the pressure-tolerance study of doc/vof_step_performance_design.md sec. 5.12 (WO-0 starts
3it, WO-13 analyses it). An instrument, not a gate: it records, it does not judge.
4
5For every pressure-driver rtol in --rtols (default 1e-10 [reference], 1e-9, 1e-8, 1e-7, 1e-6; the
6momentum solve follows the pressure driver's rtol by default) it runs up to three cases and writes
7one JSON per (case, rtol) into --out:
8
9 static tests/study/vof_surface_tension.py's static droplet (all resolution rungs, 60 steps
10 each): max|u| per rung -> static_rtol<R>.json
11 hysing Hysing case 1 through the BLOCK path (tests/study/vof_blocks_ns.py run_hysing, nx 64,
12 T = 3): peak rise velocity, its time, final centroid, volume drift -> hysing_rtol<R>.json
13 column the bubble column from ckpt_t43, --column-steps steps (default 2000), MG-PCG at the
14 rtol, device flux constraint (the production protocol of run_peclet.py): per step dt,
15 pressure iterations, momentum residual and time (the Python API exposes no momentum
16 iteration count), max_open_divergence_projected(), every marker's volume and
17 x-velocity; every 50 steps run_peclet.sample() (gas volume, drift = rise speed)
18 -> column_rtol<R>.json
19
20A (case, rtol) whose JSON already exists is skipped, so re-running resumes.
21
22Usage:
23 PYTHONPATH=<flow build> OMP_NUM_THREADS=8 OMP_PROC_BIND=false \\
24 python tests/study/vof_perf/d1_tolerance.py --out DIR [--cases static,hysing,column]
25 [--rtols 1e-10,1e-9,1e-8,1e-7,1e-6] [--column-steps 2000] [--ckpt PATH] [--case-dir DIR]
26 [--vcycle-precision auto|fp64|fp32] (the column's V-cycle precision, §4 of
27 doc/vof_projection_cost_design.md)
28 [--initial-guess auto|zero|projected [--guess-history 1|2]] (the column's
29 pressure initial guess, §5 of the same note)
30"""
31import json
32import os
33import sys
34import time
35
36import numpy as np
37
38A = list(sys.argv)
39
40
41def arg(name, default, cast=str):
42 return cast(A[A.index(name) + 1]) if name in A else default
43
44
45OUT = arg("--out", "d1")
46CASES = arg("--cases", "static,hysing,column").split(",")
47RTOLS = [float(r) for r in arg("--rtols", "1e-10,1e-9,1e-8,1e-7,1e-6").split(",")]
48NCOL = arg("--column-steps", 2000, int)
49CASE_DIR = arg("--case-dir", os.path.expanduser(
50 "~/Codes/peclet-examples/benchmarks/bubble-column/scripts"))
51CKPT = arg("--ckpt", os.path.expanduser(
52 "~/Codes/bubble_column_perf/ckpt_t43.npz"))
53# doc/vof_projection_cost_design.md §4: the V-cycle precision of the COLUMN case (its PCG is the
54# only Krylov solve here; the static and Hysing cases run Chebyshev, where the FP32 V-cycle never
55# applies, so they keep the build's default)
56VPREC = arg("--vcycle-precision", "")
57# §5: the column's pressure initial guess (the Chebyshev cases are not eligible)
58GUESS = arg("--initial-guess", "")
59GHIST = int(arg("--guess-history", "2"))
60
61# the two study scripts read sys.argv at import: hand them none of ours
62sys.argv = [sys.argv[0]]
63sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
64import vof_surface_tension as vst # noqa: E402
65import vof_blocks_ns as vbn # noqa: E402
66sys.path.insert(0, CASE_DIR)
67import run_peclet as rp # noqa: E402
68import peclet.flow as pf # noqa: E402
69
70
71def run_static(rtol):
72 vst.RTOL = rtol
73 vst.gate_static()
74 return {"rungs": list(vst.STATIC_RESULTS),
75 "max_u": max(r["max_u"] for r in vst.STATIC_RESULTS)}
76
77
78def run_hysing(rtol):
79 vbn.RTOL = rtol
80 r = vbn.run_hysing(64, True, 3.0, verbose=False)
81 return {"vmax": r["vmax"], "tvmax": r["tvmax"], "yc": r["yc"], "vdrift": r["vdrift"],
82 "steps": r["steps"], "p_iters_max": r["solve"].iters, "div_max": r["solve"].div,
83 "capped": not r["solve"].valid}
84
85
86def run_column(rtol):
87 step, t, blocks, vel, pres, series = rp.load_ckpt(CKPT)
88 s = rp.build(blocks)
89 for c, a in enumerate(vel):
90 s.set_velocity(c, a)
91 s.set_field("p", pres)
92 s.diagnostics.set_vof_step_parity(step)
93 s.set_pressure_pcg(True, 800, rtol)
94 if VPREC:
95 s.diagnostics.set_pressure_vcycle_precision(VPREC)
96 if GUESS:
97 s.diagnostics.set_pressure_initial_guess(GUESS, GHIST)
98 v0 = np.array([b["volume"] for b in s.diagnostics.vof_block_stats()])
99 rec = {k: [] for k in ("t", "dt", "p_iters", "mom_residual", "mom_time", "step_time",
100 "div_proj", "vol", "ux")}
101 samples = [rp.sample(s, t)]
102 for i in range(NCOL):
103 L = s.vof_step_limits()
104 dt = min(0.25 * L["cfl_dt"], 0.25 * L["capillary_dt"])
105 s.set_dt(dt)
106 s.step()
107 t += dt
108 d = s.diagnostics
109 tm = d.last_step_timers()
110 st = d.vof_block_stats()
111 rec["t"].append(t)
112 rec["dt"].append(dt)
113 rec["p_iters"].append(d.last_pressure_iterations())
114 rec["mom_residual"].append(d.last_momentum_residual())
115 rec["mom_time"].append(tm["momentum"])
116 rec["step_time"].append(tm["step"])
117 rec["div_proj"].append(s.max_open_divergence_projected())
118 rec["vol"].append([b["volume"] for b in st])
119 rec["ux"].append([b["velocity"][0] / rp.S for b in st])
120 if (i + 1) % 50 == 0:
121 samples.append(rp.sample(s, t))
122 print(f" column rtol {rtol:g}: step {i+1}/{NCOL} p_iters {rec['p_iters'][-1]} "
123 f"div {rec['div_proj'][-1]:.2e}", flush=True)
124 vol = np.array(rec["vol"])
125 ux = np.array(rec["ux"])
126 half = NCOL // 2
127 rec.update({
128 "vol0": v0.tolist(),
129 "bubble_vol_drift_max": float(np.max(np.abs(vol / v0 - 1.0))),
130 "total_vol_drift_max": float(np.max(np.abs(vol.sum(axis=1) / v0.sum() - 1.0))),
131 "rise_mean_last_half": float(-np.mean(ux[half:])), # + = towards -x (up), per marker
132 "drift_mean_last_half": float(np.mean([q["drift"] for q in samples[len(samples) // 2:]])),
133 "samples": samples})
134 return rec
135
136
137RUN = {"static": run_static, "hysing": run_hysing, "column": run_column}
138os.makedirs(OUT, exist_ok=True)
139print(f"D1: cases {CASES} rtols {RTOLS} module {pf.__file__}", flush=True)
140for case in CASES:
141 for rtol in RTOLS:
142 path = os.path.join(OUT, f"{case}_rtol{rtol:.0e}.json")
143 if os.path.exists(path):
144 print(f" {case} rtol {rtol:g}: exists, skipped", flush=True)
145 continue
146 t0 = time.time()
147 print(f" {case} rtol {rtol:g}: start", flush=True)
148 res = RUN[case](rtol)
149 res.update({"case": case, "rtol": rtol, "wall_s": time.time() - t0,
150 "loadavg": open("/proc/loadavg").read().strip(), "module": pf.__file__})
151 with open(path + ".tmp", "w") as f:
152 json.dump(res, f)
153 os.replace(path + ".tmp", path)
154 print(f" {case} rtol {rtol:g}: done in {res['wall_s']:.0f} s -> {path}", flush=True)
155print("D1 DONE", flush=True)
arg(name, default, cast=str)