Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions toolchain/mfc/params/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,28 @@
from .schema import ParamDef, ParamType

# Index limits — sourced from Fortran compile-time constants (m_constants.fpp).
# These must stay in sync with Fortran; we error if the source can't be parsed.
# Falls back to the inline default when src/ is unavailable (e.g. Homebrew).
# Default must match src/common/m_constants.fpp — enforced by co-location.
_FC = get_fortran_constants()


def _fc(name: str) -> int:
"""Get a required Fortran constant, raising if unavailable."""
if name not in _FC:
raise RuntimeError(f"Fortran constant '{name}' not found in m_constants.fpp. Toolchain is out of sync with Fortran source.")
return _FC[name]
def _fc(name: str, default: int) -> int:
"""Get a Fortran constant, using the inline default when m_constants.fpp is unavailable."""
if _FC:
if name not in _FC:
raise RuntimeError(
f"Fortran constant '{name}' not found in m_constants.fpp. "
f"Toolchain is out of sync with Fortran source."
)
return _FC[name]
return default


NF = _fc("num_fluids_max") # fluid_pp
NPR = _fc("num_probes_max") # probe, acoustic, integral
NB = _fc("num_bc_patches_max") # patch_bc
NUM_PATCHES_MAX = _fc("num_patches_max") # patch_icpp (Fortran array bound)
NIB = _fc("num_ib_patches_max") # patch_ib (Fortran array bound)
NF = _fc("num_fluids_max", 10) # fluid_pp
NPR = _fc("num_probes_max", 10) # probe, acoustic, integral
NB = _fc("num_bc_patches_max", 10) # patch_bc
NUM_PATCHES_MAX = _fc("num_patches_max", 10) # patch_icpp (Fortran array bound)
NIB = _fc("num_ib_patches_max", 50000) # patch_ib (Fortran array bound)
# Enumeration limits for families not yet converted to IndexedFamily.
# These are smaller than the Fortran array bounds to keep the registry compact.
# The CONSTRAINTS dict below uses the Fortran constants for validation.
Expand Down
17 changes: 3 additions & 14 deletions toolchain/mfc/params/namelist_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,29 +492,18 @@ def get_fortran_constants() -> Dict[str, int]:
"""
Get Fortran compile-time constants from m_constants.fpp.

Cached after first call. Falls back to built-in defaults when the Fortran
source is unavailable (e.g. Homebrew installs where src/ is not shipped).
Cached after first call. Returns an empty dict when the Fortran source is
unavailable (e.g. Homebrew installs where src/ is not shipped); callers
supply their own inline defaults via _fc(name, default) in definitions.py.
"""
global _FORTRAN_CONSTANTS_CACHE # noqa: PLW0603
if _FORTRAN_CONSTANTS_CACHE is None:
root = get_mfc_root()
path = root / "src" / "common" / "m_constants.fpp"
_FORTRAN_CONSTANTS_CACHE = parse_fortran_constants(path)
if not _FORTRAN_CONSTANTS_CACHE:
_FORTRAN_CONSTANTS_CACHE = _FALLBACK_CONSTANTS.copy()
return _FORTRAN_CONSTANTS_CACHE


# Fallback values for when m_constants.fpp is not available at runtime.
# Keep in sync with src/common/m_constants.fpp.
_FALLBACK_CONSTANTS: Dict[str, int] = {
"num_fluids_max": 10,
"num_probes_max": 10,
"num_patches_max": 1000,
"num_bc_patches_max": 10,
}


def get_mfc_root() -> Path:
"""Get the MFC root directory from this file's location."""
# This file is at toolchain/mfc/params/namelist_parser.py
Expand Down
Loading