flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
mg_precond_fp32cmp.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2"""G-D2 (b), (c) of doc/vof_projection_cost_design.md §13: the FP32 V-cycle against the FP64 one.
3
4Reads the dense preconditioners `mg_dense_precond` writes for the same configuration with the
5double operator (`M_double_*`, today's FP64 V-cycle) and with `--precision fp32` (`M_vf32_*`, the
6FP32 V-cycle of §4), and checks per configuration:
7 (b) no new indefiniteness: lambda_min(sym(M_fp32)) on the mean-free subspace
8 >= lambda_min(sym(M_fp64)) - 1e-3 * lambda_max(sym(M_fp64));
9 (c) ||M_fp32 - M_fp64||_F / ||M_fp64||_F <= 1e-4 at ratio <= 1e3, <= 1e-3 at 1e4.
10The mean-free subspace is spanned by an orthonormal basis of 1^perp (every configuration here is
11one connected fluid component), so the exact null direction of M does not enter lambda_min.
12
13Usage: python mg_precond_fp32cmp.py <dir> [more dirs...]; last line G-D2 PASS|FAIL, exit 0 iff PASS.
14"""
15import glob
16import os
17import re
18import sys
19
20import numpy as np
21
22MAGIC = 0x50434D47444E5331
23
24
25def load(path):
26 with open(path, "rb") as f:
27 hdr = np.fromfile(f, dtype=np.int64, count=3)
28 if hdr[0] != MAGIC:
29 raise ValueError(f"{path}: bad magic {hdr[0]:x}")
30 rows, cols = int(hdr[1]), int(hdr[2])
31 a = np.fromfile(f, dtype=np.float64, count=rows * cols)
32 return a.reshape(rows, cols)
33
34
36 n = M.shape[0]
37 # orthonormal basis of 1^perp: the last n-1 columns of a Householder-completed basis
38 Q, _ = np.linalg.qr(np.column_stack([np.ones(n), np.eye(n)[:, : n - 1]]))
39 B = Q[:, 1:]
40 S = 0.5 * (M + M.T)
41 return np.linalg.eigvalsh(B.T @ S @ B)
42
43
44def main(dirs):
45 ok_all = True
46 rows = 0
47 for d in dirs:
48 for p64 in sorted(glob.glob(os.path.join(d, "M_double_*.bin"))):
49 p32 = p64.replace("M_double_", "M_vf32_")
50 if not os.path.exists(p32):
51 continue
52 m = re.search(r"M_double_(\w+?)_n(\d+)_L(\d+)_r([0-9e+.-]+)\.bin", os.path.basename(p64))
53 geom, n, lev, ratio = m.group(1), int(m.group(2)), int(m.group(3)), float(m.group(4))
54 M64, M32 = load(p64), load(p32)
55 fro = np.linalg.norm(M32 - M64) / np.linalg.norm(M64)
56 e64, e32 = meanfree_eigs(M64), meanfree_eigs(M32)
57 lmin64, lmax64, lmin32 = e64[0], e64[-1], e32[0]
58 tol_c = 1e-4 if ratio <= 1e3 else 1e-3
59 ok_b = lmin32 >= lmin64 - 1e-3 * lmax64
60 ok_c = fro <= tol_c
61 ok_all = ok_all and ok_b and ok_c
62 rows += 1
63 print(f"{geom:8s} n {n:2d} L {lev} ratio {ratio:8.0e} (c) |dM|/|M| {fro:.3e} "
64 f"(<= {tol_c:.0e}) {'ok' if ok_c else 'FAIL'} (b) lmin32 {lmin32:+.6e} "
65 f"lmin64 {lmin64:+.6e} lmax64 {lmax64:.4e} {'ok' if ok_b else 'FAIL'}")
66 if rows == 0:
67 ok_all = False
68 print("no M_double_* / M_vf32_* pairs found")
69 print(f"G-D2 {'PASS' if ok_all else 'FAIL'} ({rows} configurations)")
70 return 0 if ok_all else 1
71
72
73if __name__ == "__main__":
74 sys.exit(main(sys.argv[1:]))