Skip to content

Commit 303c53f

Browse files
committed
feat(params): support real(stp) declarations in Fortran codegen
1 parent a21bfbf commit 303c53f

4 files changed

Lines changed: 17 additions & 3 deletions

File tree

toolchain/mfc/params/definitions.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ def get_value_label(param_name: str, value: int) -> str:
489489
}
490490

491491

492-
def _r(name, ptype, tags=None, desc=None, hint=None, math=None, str_len=None):
492+
def _r(name, ptype, tags=None, desc=None, hint=None, math=None, str_len=None, storage_precision=False):
493493
"""Register a parameter with optional feature tags and description."""
494494
if hint is None:
495495
hint = _lookup_hint(name)
@@ -514,6 +514,7 @@ def _r(name, ptype, tags=None, desc=None, hint=None, math=None, str_len=None):
514514
hint=hint,
515515
math_symbol=math or "",
516516
str_len=str_len if str_len is not None else "name_len",
517+
storage_precision=storage_precision,
517518
)
518519
)
519520

toolchain/mfc/params/generators/fortran_gen.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
_FORTRAN_TYPES = {
2222
ParamType.INT: "integer",
2323
ParamType.ANALYTIC_INT: "integer",
24-
ParamType.REAL: "real(wp)",
25-
ParamType.ANALYTIC_REAL: "real(wp)",
2624
ParamType.LOG: "logical",
2725
}
2826

27+
_REAL_TYPES = (ParamType.REAL, ParamType.ANALYTIC_REAL)
28+
2929
_VALID_TARGETS = ("pre", "sim", "post")
3030

3131

@@ -46,6 +46,8 @@ def get_namelist_var(name: str) -> str:
4646
def fortran_type_decl(param: ParamDef) -> str:
4747
if param.param_type == ParamType.STR:
4848
return f"character(LEN={param.str_len})"
49+
if param.param_type in _REAL_TYPES:
50+
return f"real({'stp' if param.storage_precision else 'wp'})"
4951
return _FORTRAN_TYPES[param.param_type]
5052

5153

toolchain/mfc/params/schema.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class ParamDef:
6565
hint: str = "" # Constraint/usage hint for docs (e.g. "Used with grcbc_in")
6666
math_symbol: str = "" # LaTeX math symbol (Doxygen format, e.g. "\\f$\\gamma_k\\f$")
6767
str_len: str = "name_len" # For STR type: Fortran character length constant ("path_len" for case_dir)
68+
storage_precision: bool = False # For REAL types: declare as real(stp) instead of real(wp)
6869

6970
def __post_init__(self):
7071
# Validate name

toolchain/mfc/params_tests/test_fortran_gen.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ def test_fortran_type_real():
3333
assert fortran_type_decl(ParamDef(name="x", param_type=ParamType.REAL)) == "real(wp)"
3434

3535

36+
def test_fortran_type_real_storage_precision():
37+
from mfc.params.generators.fortran_gen import fortran_type_decl
38+
from mfc.params.schema import ParamDef, ParamType
39+
40+
p = ParamDef(name="x", param_type=ParamType.REAL, storage_precision=True)
41+
assert fortran_type_decl(p) == "real(stp)"
42+
a = ParamDef(name="x", param_type=ParamType.ANALYTIC_REAL, storage_precision=True)
43+
assert fortran_type_decl(a) == "real(stp)"
44+
45+
3646
def test_fortran_type_log():
3747
from mfc.params.generators.fortran_gen import fortran_type_decl
3848
from mfc.params.schema import ParamDef, ParamType

0 commit comments

Comments
 (0)