Skip to content

Commit bdd386a

Browse files
committed
refactor: clean up codegen — remove dead alias, stale comments, docstring violations
1 parent e199d47 commit bdd386a

3 files changed

Lines changed: 33 additions & 83 deletions

File tree

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,22 @@
1-
"""
2-
Generate Fortran parameter .fpp files into the CMake build directory.
1+
"""Generate Fortran parameter .fpp files into the CMake build directory.
32
43
Called by CMakeLists.txt at configure time:
54
python3 cmake_gen.py <cmake_binary_dir>
6-
7-
Writes generated_namelist.fpp and generated_decls.fpp into
8-
<cmake_binary_dir>/include/{pre_process,simulation,post_process}/
95
"""
106

117
import sys
128
from pathlib import Path
139

1410
sys.path.insert(0, str(Path(__file__).parent.parent.parent.parent))
1511

16-
from mfc.params.generators.fortran_gen import generate_decls_fpp, generate_namelist_fpp
17-
18-
_TARGETS = [
19-
("pre", "pre_process"),
20-
("sim", "simulation"),
21-
("post", "post_process"),
22-
]
23-
24-
25-
def main(build_dir: Path) -> None:
26-
for short, full in _TARGETS:
27-
out_dir = build_dir / "include" / full
28-
out_dir.mkdir(parents=True, exist_ok=True)
29-
(out_dir / "generated_namelist.fpp").write_text(generate_namelist_fpp(short))
30-
(out_dir / "generated_decls.fpp").write_text(generate_decls_fpp(short))
12+
from mfc.params.generators.fortran_gen import generate_decls_fpp, generate_namelist_fpp # noqa: E402
3113

14+
if len(sys.argv) != 2:
15+
sys.exit(f"Usage: {sys.argv[0]} <cmake_binary_dir>")
3216

33-
if __name__ == "__main__":
34-
if len(sys.argv) != 2:
35-
sys.exit(f"Usage: {sys.argv[0]} <cmake_binary_dir>")
36-
main(Path(sys.argv[1]))
17+
build_dir = Path(sys.argv[1])
18+
for short, full in [("pre", "pre_process"), ("sim", "simulation"), ("post", "post_process")]:
19+
out_dir = build_dir / "include" / full
20+
out_dir.mkdir(parents=True, exist_ok=True)
21+
(out_dir / "generated_namelist.fpp").write_text(generate_namelist_fpp(short))
22+
(out_dir / "generated_decls.fpp").write_text(generate_decls_fpp(short))
Lines changed: 12 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
"""
2-
Fortran parameter code generator.
3-
4-
Generates namelist fragments and simple scalar declaration fragments
5-
per target (pre/sim/post). Output consumed by generate.py.
6-
7-
Output format matches ffmt (the MFC Fortran formatter) so that
8-
./mfc.sh format is idempotent on these generated files.
9-
"""
1+
"""Fortran parameter code generator — namelist and scalar decl fragments per target."""
102

113
import re
124
from typing import List
@@ -17,17 +9,14 @@
179
from ..registry import REGISTRY
1810
from ..schema import ParamDef, ParamType
1911

20-
# ffmt collapses the two header lines into one and uses ASCII hyphen
21-
_HEADER = "! AUTO-GENERATED - do not edit directly. Regenerate: ./mfc.sh generate\n!\n"
12+
_HEADER = "! AUTO-GENERATED - do not edit directly. Regenerate: cmake reconfigure\n!\n"
2213

23-
# ffmt formats Fortran with max 130-char lines and 4-space continuation indent
2414
_MAX_LINE = 130
2515
_FIRST_PREFIX = "namelist /user_inputs/ "
2616
_CONT_PREFIX = " & "
27-
_CONT2_PREFIX = " & " # second-level continuation (inside Fypp #:if block)
17+
_CONT2_PREFIX = " & " # inside #:if block
2818

29-
# ffmt aligns '::' to a fixed column; widest type is character(LEN=path_len) = 23 chars
30-
_DECL_COL = 24 # pad type string to this width before '::'
19+
_DECL_COL = 24 # '::' column, matches ffmt alignment
3120

3221

3322
def get_namelist_var(param_name: str) -> str:
@@ -55,93 +44,69 @@ def fortran_type_decl(param: ParamDef) -> str:
5544

5645

5746
def _is_simple_scalar(name: str) -> bool:
58-
"""Return True if name has no '%' and no '(' - i.e. a plain simple variable."""
5947
return "%" not in name and "(" not in name
6048

6149

6250
def _vars_for_target(target: str) -> List[str]:
63-
"""Return sorted list of namelist variable names for the given target."""
6451
return sorted(v for v, ts in NAMELIST_VARS.items() if target in ts)
6552

6653

6754
def _pack_namelist(vars_list: List[str], first_prefix: str, cont_prefix: str, max_line: int) -> List[str]:
68-
"""
69-
Pack a list of variable names into Fortran namelist continuation lines.
70-
71-
Returns a list of lines WITHOUT trailing newlines.
72-
All lines except the last end with ', &'.
73-
"""
55+
"""Pack variable names into Fortran continuation lines; all but last end with ', &'."""
7456
if not vars_list:
7557
return []
76-
7758
lines: List[str] = []
7859
prefix = first_prefix
7960
current_vars: List[str] = []
8061
current_len = len(prefix)
81-
8262
for var in vars_list:
8363
additional = len(var) + (2 if current_vars else 0)
8464
if current_vars and current_len + additional + 3 > max_line:
85-
# Flush with continuation marker
8665
lines.append(prefix + ", ".join(current_vars) + ", &")
8766
prefix = cont_prefix
8867
current_vars = [var]
8968
current_len = len(cont_prefix) + len(var)
9069
else:
9170
current_vars.append(var)
9271
current_len += additional
93-
9472
if current_vars:
9573
lines.append(prefix + ", ".join(current_vars))
96-
9774
return lines
9875

9976

10077
def _format_namelist(vars_list: List[str]) -> str:
101-
"""Format vars as a Fortran namelist statement block (no trailing newline)."""
102-
lines = _pack_namelist(vars_list, _FIRST_PREFIX, _CONT_PREFIX, _MAX_LINE)
103-
return "\n".join(lines)
78+
return "\n".join(_pack_namelist(vars_list, _FIRST_PREFIX, _CONT_PREFIX, _MAX_LINE))
10479

10580

10681
def generate_namelist_fpp(target: str) -> str:
107-
"""Generate the namelist /user_inputs/ statement for a target."""
82+
"""Return the namelist /user_inputs/ statement for a target as a string."""
10883
assert target in ("pre", "sim", "post")
10984
all_vars = _vars_for_target(target)
11085

11186
if target != "sim":
11287
return _HEADER + _format_namelist(all_vars) + "\n"
11388

114-
# For sim: split into normal vars and case-opt-excluded vars
11589
normal = [v for v in all_vars if v not in CASE_OPT_EXCLUDE]
11690
opt = sorted(v for v in CASE_OPT_EXCLUDE if v in NAMELIST_VARS and "sim" in NAMELIST_VARS[v])
11791

118-
# Normal vars: last line gets ', &' since opt vars follow
11992
nl_lines = _pack_namelist(normal, _FIRST_PREFIX, _CONT_PREFIX, _MAX_LINE)
12093
nl_lines[-1] += ", &"
121-
122-
# Opt vars: pack using cont_prefix for first line, cont2_prefix for subsequent
12394
opt_lines = _pack_namelist(opt, _CONT_PREFIX, _CONT2_PREFIX, _MAX_LINE)
12495

125-
all_lines = [_HEADER.rstrip()] + nl_lines + ["#:if not MFC_CASE_OPTIMIZATION"] + opt_lines + ["#:endif"]
126-
return "\n".join(all_lines) + "\n"
96+
parts = [_HEADER.rstrip()] + nl_lines + ["#:if not MFC_CASE_OPTIMIZATION"] + opt_lines + ["#:endif"]
97+
return "\n".join(parts) + "\n"
12798

12899

129100
def generate_decls_fpp(target: str) -> str:
130-
"""Generate simple scalar Fortran variable declarations for a target.
131-
132-
Column-aligns '::' to match ffmt output (type padded to _DECL_COL chars).
133-
"""
101+
"""Return simple scalar Fortran declarations for a target as a string."""
134102
assert target in ("pre", "sim", "post")
135103
all_params = REGISTRY.all_params
136-
vars_for_target = _vars_for_target(target)
137104
lines = [_HEADER.rstrip()]
138-
for name in vars_for_target:
105+
for name in _vars_for_target(target):
139106
if not _is_simple_scalar(name):
140107
continue
141108
param = all_params.get(name)
142109
if param is None:
143110
continue
144-
type_str = fortran_type_decl(param)
145-
padded = type_str.ljust(_DECL_COL)
146-
lines.append(f"{padded}:: {name}")
111+
lines.append(f"{fortran_type_decl(param).ljust(_DECL_COL)}:: {name}")
147112
return "\n".join(lines) + "\n"

toolchain/mfc/params/namelist_targets.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@
5151
# --- Time ---
5252
"dt": {"sim"},
5353
"t_step_start": _ALL,
54-
"t_step_stop": {"sim", "post"},
55-
"t_step_save": {"sim", "post"},
54+
"t_step_stop": _SIM_POST,
55+
"t_step_save": _SIM_POST,
5656
"t_step_print": {"sim"},
5757
"t_step_old": {"pre", "sim"},
5858
"time_stepper": {"sim"},
59-
"t_stop": {"sim", "post"},
60-
"t_save": {"sim", "post"},
61-
"cfl_target": {"sim", "post"},
59+
"t_stop": _SIM_POST,
60+
"t_save": _SIM_POST,
61+
"cfl_target": _SIM_POST,
6262
"cfl_adap_dt": _ALL,
6363
"cfl_const_dt": _ALL,
6464
"n_start": _ALL,
@@ -86,14 +86,13 @@
8686
"muscl_eps": {"sim"},
8787
"recon_type": {"pre", "post"},
8888
"muscl_order": {"pre", "post"},
89-
"muscl_lim": set(),
9089
"int_comp": {"sim"},
9190
"ic_eps": {"sim"},
9291
"ic_beta": {"sim"},
9392
# --- Riemann solver ---
9493
"riemann_solver": {"sim"},
9594
"wave_speeds": {"sim"},
96-
"avg_state": {"sim", "post"},
95+
"avg_state": _SIM_POST,
9796
"low_Mach": {"sim"},
9897
# --- MHD ---
9998
"mhd": {"pre", "post"},
@@ -170,7 +169,7 @@
170169
"coefficient_of_restitution": {"sim"},
171170
"collision_time": {"sim"},
172171
"ib_coefficient_of_friction": {"sim"},
173-
"ib_state_wrt": {"sim", "post"},
172+
"ib_state_wrt": _SIM_POST,
174173
# --- Continuum damage ---
175174
"cont_damage": _ALL,
176175
"tau_star": {"sim"},
@@ -187,7 +186,7 @@
187186
"integral_wrt": {"sim"},
188187
"num_integrals": {"sim"},
189188
"integral": {"sim"},
190-
"fd_order": {"sim", "post"},
189+
"fd_order": _SIM_POST,
191190
# --- Acoustic sources (sim) ---
192191
"acoustic_source": {"sim"},
193192
"num_source": {"sim"},
@@ -216,7 +215,7 @@
216215
"precision": _ALL,
217216
"parallel_io": _ALL,
218217
"file_per_process": _ALL,
219-
"prim_vars_wrt": {"sim", "post"},
218+
"prim_vars_wrt": _SIM_POST,
220219
"cons_vars_wrt": {"post"},
221220
"run_time_info": {"sim"},
222221
"fft_wrt": _ALL,
@@ -245,8 +244,8 @@
245244
"cf_wrt": {"post"},
246245
"chem_wrt_T": {"post"},
247246
"chem_wrt_Y": {"post"},
248-
"alt_soundspeed": {"sim", "post"},
249-
"mixture_err": {"sim", "post"},
247+
"alt_soundspeed": _SIM_POST,
248+
"mixture_err": _SIM_POST,
250249
"flux_lim": {"post"},
251250
"sim_data": {"post"},
252251
"alpha_rho_e_wrt": {"post"},

0 commit comments

Comments
 (0)