flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_no_float_operator_casts.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2"""QUALITY_PLAN G.6 guard: no operator-storage view narrows to `float` outside the allow-list.
3
4`MReal` (`mac_cutcell_mg.hpp`) is the operator storage type -- float unless the build defines
5`-DPECLET_FLOW_OPERATOR_DOUBLE` (an `option()` since G.6, was a raw, untested `-DCMAKE_CXX_FLAGS`).
6G.6 templated `IbmOverlayT`/`ibmFillEntry`/`ibmModifyStencil` (cut_cell_ibm.hpp) and the geometry
7build that feeds them (mac_ibm.hpp, flow_ibm.hpp) on `Real`/`mreal` so a double build carries the
8cut-cell overlay in double end to end; this test fails if a NEW hard `(float)` cast or `float`-typed
9operator view creeps back in outside the allow-list below, so the next one has to be typed on
10`mreal`/`MReal` (or exempted with a stated reason) instead.
11
12A hit is IGNORED when: the line is a pure comment (only prose, no code -- these mention "(float)" in
13plain English and are not casts); the whole FILE is allow-listed (an opt-in debug/forensics file
14whose float precision is unrelated to operator correctness, or a file whose ghost-projection overlay
15is a stated, deferred gap -- see the reasons below); or the line carries a `PRECISION-EXEMPT:`
16marker (a deliberate, reasoned exception -- `git grep PRECISION-EXEMPT src/` to read them).
17"""
18import pathlib
19import re
20import sys
21
22SRC = pathlib.Path(__file__).resolve().parents[2] / "src"
23
24# WO-D0 (doc/vof_projection_cost_design.md ยง12): also a `float` template ARGUMENT (`X<float>`,
25# `X<float, ...>`, `X<..., float>`) and a `float` template-parameter DEFAULT (`class R = float`,
26# `typename R = float`) -- the forms the FP32 bottom factor and the FP32 V-cycle data take.
27CAST = re.compile(
28 r"\‍(\s*float\s*\‍)|static_cast\s*<\s*float\s*>|View<\s*float\s*\*"
29 r"|<\s*float\s*[,>]|,\s*float\s*>"
30 r"|\b(?:class|typename)\s+\w+\s*=\s*float\b"
31)
32
33# Whole files exempt from this test, with the reason (also stated in the file's own header
34# comment as PRECISION-EXEMPT so a reader of the file sees it too, not just this list).
35FILE_EXEMPT = {
36 "ghost_projection_debug.hpp": (
37 "opt-in forensics (PECLET_FLOW_GP_DEBUG); never feeds a solve, precision is unrelated to "
38 "correctness"
39 ),
40}
41
42
43def is_comment_line(line: str) -> bool:
44 s = line.strip()
45 return s.startswith("//") or s.startswith("/*") or s.startswith("*")
46
47
48def main() -> int:
49 offenders = []
50 for path in sorted(SRC.rglob("*")):
51 if path.suffix not in (".hpp", ".cpp", ".h"):
52 continue
53 if path.name in FILE_EXEMPT:
54 continue
55 for lineno, line in enumerate(path.read_text().splitlines(), 1):
56 if not CAST.search(line):
57 continue
58 if is_comment_line(line):
59 continue
60 if "PRECISION-EXEMPT" in line:
61 continue
62 offenders.append(f"{path.relative_to(SRC.parent)}:{lineno}: {line.strip()}")
63 if offenders:
64 print(
65 "hard (float) casts / float-typed operator views found in src/ outside the "
66 "allow-list (QUALITY_PLAN G.6 -- type on mreal/MReal, or mark "
67 "`// PRECISION-EXEMPT: <reason>` if it is genuinely precision-independent):"
68 )
69 for o in offenders:
70 print(" " + o)
71 return 1
72 print("no un-exempted float operator casts in src/ [OK]")
73 return 0
74
75
76if __name__ == "__main__":
77 sys.exit(main())