flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
prof.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2"""Profile the bubble-column step from a checkpoint (the VoF step-performance harness, WO-0).
3
4The case is the bubble column of peclet-examples (128 x 96 x 64, 16 VoF block markers, rho/mu
5ratio 0.02, set_superficial_velocity), built by that repo's run_peclet.build() and restarted from
6ckpt_t43.npz. It runs W warm-up steps (not timed), then N timed steps with the driver's per-step
7protocol (dt = 0.25 min(cfl_dt, capillary_dt) from vof_step_limits()). See
8doc/vof_step_performance_design.md (sec. 7 WO-0, sec. 8 G-BIT / G-NUM / G-PERF).
9
10Usage:
11 PYTHONPATH=<flow build> OMP_NUM_THREADS=8 OMP_PROC_BIND=false \\
12 python tests/study/vof_perf/prof.py N [--pcg [--rtol R]] [--timing] [--dump out.npz]
13 [--warm W] [--flux device|python|none] [--levels L] [--bottom MODE] [--fixdt DT]
14 [--ckpt PATH] [--case-dir DIR] [--dummy N] [--viscous-stress MODE]
15
16 --ckpt checkpoint (default ~/Codes/bubble_column_perf/ckpt_t43.npz)
17 --case-dir directory holding run_peclet.py + case.py (default:
18 ~/Codes/peclet-examples/benchmarks/bubble-column/scripts)
19 --pcg MG-PCG pressure with cap 800 and relative tolerance --rtol (default 1e-10)
20 --cheb Chebyshev pressure (cap --cheb-maxit, default 120) at --rtol; prints the
21 solver's num_pressure_chebyshev_restarts when the build has it (D3, §5.13)
22 --dump save u, v, w, p, C, the timed steps' dt and pressure iterations (iters) and every
23 VoF block's colour array (col<id>) -- compare two dumps with cmp.py (G-BIT item 2)
24 --timing per-stage breakdown from diagnostics.vof_timing()
25 --levels L pressure multigrid depth; --bottom MODE = set_pressure_bottom(MODE) (E1)
26 --bottom-solver ENGINE diagnostics.set_pressure_bottom_solver(ENGINE): auto | direct |
27 algebraic (§13: the A/B of the two bottom engines on one build)
28 --fcg flexible CG pressure (set_pressure_fcg, cap 800) at --rtol
29 --vcycle-precision MODE diagnostics.set_pressure_vcycle_precision(MODE): auto | fp64 | fp32
30 (doc/vof_projection_cost_design.md §4: the FP32 V-cycle A/B on one build)
31 --initial-guess MODE diagnostics.set_pressure_initial_guess(MODE, --guess-history H):
32 auto | zero | projected, H = 1 | 2 (default 2) (the same note, §5)
33 --viscous-stress MODE set_viscous_stress(MODE): laplacian | full (absent: the build default)
34 --div with --dump: also store each timed step's max_open_divergence_projected() (div),
35 read after the step (G-NUM item 3)
36 --stats with --dump: also store every timed step's measured block statistics (st_volume,
37 st_area, st_centroid, st_velocity, st_moments; WO-9's gate, C2 §5.10)
38"""
39import os
40import sys
41import time
42
43import numpy as np
44
45A = list(sys.argv)
46
47
48def arg(name, default, cast=str):
49 return cast(A[A.index(name) + 1]) if name in A else default
50
51
52CASE_DIR = arg("--case-dir", os.path.expanduser(
53 "~/Codes/peclet-examples/benchmarks/bubble-column/scripts"))
54CKPT = arg("--ckpt", os.path.expanduser(
55 "~/Codes/bubble_column_perf/ckpt_t43.npz"))
56sys.path.insert(0, CASE_DIR)
57import run_peclet as rp # noqa: E402
58
59N = int(A[1])
60TIMING = "--timing" in A
61FLUX = arg("--flux", "python")
62DUMP = arg("--dump", None)
63WARM = arg("--warm", 5, int)
64FIXDT = arg("--fixdt", None, float)
65RTOL = arg("--rtol", 1e-10, float)
66
67step, t, blocks, vel, pres, series = rp.load_ckpt(CKPT)
68t_b = time.time()
69if "--dummy" in A:
70 import peclet.flow as _pf
71 _dummy = _pf.Solver(arg("--dummy", 17, int), 13, 11)
72 _dummy.set_advection(False)
73 _dummy.set_pressure_geometry(np.full((arg("--dummy", 17, int), 13, 11), 10.0, order="F"))
74if "--levels" in A:
75 _L = arg("--levels", 4, int)
76 _orig = rp.pf.Solver
77
78 class _Shim:
79 def __new__(cls, *a, **k):
80 o = _orig(*a, **k)
81 o.set_pressure_multigrid(True, _L)
82 return o
83 rp.pf.Solver = _Shim
84if not hasattr(rp.pf.Solver, "set_superficial_velocity") and hasattr(rp.pf.Solver, "set_bulk_velocity"):
85 rp.pf.Solver.set_superficial_velocity = rp.pf.Solver.set_bulk_velocity # pre-rename build
86s = rp.build(blocks)
87if "--viscous-stress" in A: # laplacian | full (set_viscous_stress; doc/full_viscous_stress.md)
88 s.set_viscous_stress(arg("--viscous-stress", "laplacian"))
89for c, a in enumerate(vel):
90 s.set_velocity(c, a)
91s.set_field("p", pres)
92s.diagnostics.set_vof_step_parity(step)
93# run_peclet.build() now enables the device constraint itself; the other modes switch it off
94for _name in ("set_superficial_velocity", "set_bulk_velocity"):
95 if hasattr(s, _name):
96 getattr(s, _name)(FLUX == "device", "x", 0.0)
97 break
98if "--pcg" in A:
99 s.set_pressure_pcg(True, 800, RTOL)
100if "--fcg" in A: # the flexible CG (set_pressure_fcg), cap 800, at --rtol
101 s.set_pressure_fcg(True, 800, RTOL)
102if "--cheb" in A: # the variable-density default driver, solver default cap 120, at --rtol
103 s.set_pressure_chebyshev(True, arg("--cheb-maxit", 120, int), RTOL)
104if "--bottom" in A:
105 s.set_pressure_bottom(arg("--bottom", "auto"))
106if "--bottom-solver" in A:
107 s.diagnostics.set_pressure_bottom_solver(arg("--bottom-solver", "auto"))
108if "--vcycle-precision" in A:
109 s.diagnostics.set_pressure_vcycle_precision(arg("--vcycle-precision", "fp64"))
110if "--initial-guess" in A:
111 s.diagnostics.set_pressure_initial_guess(arg("--initial-guess", "zero"),
112 arg("--guess-history", 2, int))
113DIV = "--div" in A
114divs = []
115STATS = "--stats" in A
116stats = {k: [] for k in ("volume", "area", "centroid", "velocity", "moments")}
117print(f"build {time.time()-t_b:.2f} s", flush=True)
118
119S = rp.S
120dtsafe = 0.25
121iters = []
122T = {"limits": 0.0, "step": 0.0, "flux": 0.0}
123
124
125def one(timed):
126 global t
127 t0 = time.perf_counter()
128 if FIXDT:
129 dt = FIXDT
130 else:
131 L = s.vof_step_limits()
132 dt = min(dtsafe * L["cfl_dt"], dtsafe * L["capillary_dt"])
133 s.set_dt(dt)
134 t1 = time.perf_counter()
135 s.step()
136 t2 = time.perf_counter()
137 if FLUX == "python":
138 u = s.get_u()
139 s.set_velocity(0, np.asfortranarray(u - float(u.mean())))
140 t3 = time.perf_counter()
141 if timed:
142 T["limits"] += t1 - t0
143 T["step"] += t2 - t1
144 T["flux"] += t3 - t2
145 iters.append(s.diagnostics.last_pressure_iterations())
146 if DIV:
147 divs.append(s.max_open_divergence_projected())
148 if STATS:
149 st = s.diagnostics.vof_block_stats()
150 for k in stats:
151 stats[k].append([b[k] for b in st])
152 t += dt
153 return dt
154
155
156for _ in range(WARM):
157 one(False)
158if TIMING:
159 s.diagnostics.set_vof_timing(True)
160w0 = time.perf_counter()
161dts = [one(True) for _ in range(N)]
162wall = time.perf_counter() - w0
163print(f"steps {N} wall {wall:.3f} s {1000*wall/N:.2f} ms/step dt mean {np.mean(dts):.3e}")
164print("per-step ms: " + " ".join(f"{k} {1000*v/N:.2f}" for k, v in T.items()))
165print(f"pressure iters mean {np.mean(iters):.2f} min {min(iters)} max {max(iters)}")
166if "--cheb" in A and hasattr(s.diagnostics, "num_pressure_chebyshev_restarts"):
167 print(f"chebyshev restarts {s.diagnostics.num_pressure_chebyshev_restarts}")
168if TIMING:
169 v = s.diagnostics.vof_timing()
170 n = v["steps"]
171 print("vof_timing per step ms (steps=%d):" % n)
172 for k, x in v.items():
173 if k in ("steps", "k_sweeps", "kc_calls"):
174 continue
175 if x:
176 print(f" {k:22s} {1000*x/n:8.3f}")
177if DUMP:
178 cols = {f"col{b['id']}": np.asarray(s.vof_block_color(b["id"]))
179 for b in s.diagnostics.vof_block_stats()}
180 np.savez(DUMP, u=s.get_u(), v=s.get_v(), w=s.get_w(), p=s.get_field("p"), C=s.get_field("C"),
181 dts=np.array(dts), iters=np.array(iters, dtype=np.int64), **cols,
182 **({"div": np.array(divs)} if DIV else {}),
183 **({f"st_{k}": np.array(v) for k, v in stats.items()} if STATS else {}))
184 print("dumped", DUMP, f"({len(cols)} block colours)")
__new__(cls, *a, **k)
Definition prof.py:79
_orig
Definition prof.py:76
arg(name, default, cast=str)
Definition prof.py:48
one(timed)
Definition prof.py:125