Cette page a été traduite automatiquement de l’anglais et peut contenir des erreurs. Original en anglais
Code source de axqua.calibration
"""Stage 5 - calibration points CSV and HydroBayesCal config emit.
Turns the hydraulic measurements into the calibration-points CSV that
HydroBayesCal reads (``id, x, y, z, <QTY>_DATA, <QTY>_ERROR`` columns) and emits
a ``config_Telemac.py`` wiring the produced case to the calibration parameters
and BAL settings declared in the axqua config.
"""
from __future__ import annotations
import logging
from collections.abc import Callable, Mapping
from pathlib import Path
import numpy as np
import pandas as pd
from axqua.config import Config
from axqua import ground_truth
log = logging.getLogger("axqua")
#: Warnings about the size of the initial design; the job copies them into its results.
design_log = logging.getLogger("axqua.calibration_design")
#: Initial runs per calibration parameter: the sample size n = 10 d that Loeppky, Sacks
#: and Welch (2009, Technometrics 51(4)) recommend for the initial design of a computer
#: experiment that a Gaussian process is fitted to. HydroBayesCal applies the same rule
#: (``surrogate.initial_design.recommended_init_runs``).
RUNS_PER_PARAMETER = 10
#: The smallest initial design, HydroBayesCal's floor: below it a Gaussian process over
#: more than one parameter is no surrogate worth the name.
MINIMUM_INITIAL_RUNS = 16
[docs]
def recommended_initial_runs(n_parameters: int) -> int:
"""The initial runs a calibration of *n_parameters* needs.
Ten per parameter (:data:`RUNS_PER_PARAMETER`), at least
:data:`MINIMUM_INITIAL_RUNS`, rounded up to the next power of two: the initial design
is a Sobol sequence, whose points are evenly spread over the parameter ranges only
at 2**m points (the balance property of a Sobol sequence). Four parameters: 40 runs,
so 64.
"""
floor = max(RUNS_PER_PARAMETER * max(int(n_parameters), 1), MINIMUM_INITIAL_RUNS)
return 1 << (floor - 1).bit_length()
[docs]
def default_iterations() -> int:
"""Iterations of active learning of a calibration that runs a fixed number of them.
That is a calibration with ``until_converged: false`` and no ``max_runs``. Twice
:data:`axqua.calibration_report.CONVERGENCE_MINIMUM`: the plateau of the evidence
counts only after that many iterations, and the number leaves as many again for a
plateau that comes later. With ``until_converged`` (the default), the active
learning has no such number: it runs until the plateau holds.
"""
from axqua.calibration_report import CONVERGENCE_MINIMUM
return 2 * CONVERGENCE_MINIMUM
[docs]
def run_budget(calibration, n_parameters: int) -> tuple[int, int | None, int]:
"""``(init_runs, max_runs, first block)`` of a calibration of *n_parameters*.
``init_runs`` is the case's, or :func:`recommended_initial_runs` where the case sets
none. ``max_runs`` is the case's; where the case sets none, it is None (no limit)
when the active learning runs until it converges (``until_converged``, the user's
rule of 2026-11-23: enough iterations for the evidence and the relative entropy to
converge, not a number fixed beforehand), and ``init_runs`` plus
:func:`default_iterations` otherwise. The first block of HydroBayesCal's staged
initial design is the case's ``init_runs_min``, or the recommended size: the design
grows beyond it only where HydroBayesCal finds it insufficient, and never stops
below the rule.
"""
recommended = recommended_initial_runs(n_parameters)
init_runs = int(calibration.init_runs) if calibration.init_runs is not None else recommended
if calibration.max_runs is not None:
max_runs: int | None = int(calibration.max_runs)
elif getattr(calibration, "until_converged", True):
max_runs = None
else:
max_runs = init_runs + default_iterations()
first = calibration.init_runs_min
first = min(int(first) if first is not None else recommended, init_runs)
return init_runs, max_runs, first
[docs]
def pass_ceiling(init_runs: int, max_runs: int | None) -> int:
"""The ``max_runs`` a HydroBayesCal configuration states, which needs a number.
The case's limit, or without one the initial runs plus the
:data:`~axqua.calibration_report.CONVERGENCE_MINIMUM` iterations that the first
pass of active learning runs (:func:`axqua.bayescal._launch_in_passes`).
"""
if max_runs is not None:
return int(max_runs)
from axqua.calibration_report import CONVERGENCE_MINIMUM
return int(init_runs) + CONVERGENCE_MINIMUM
[docs]
def check_run_budget(calibration, n_parameters: int) -> tuple[int, int | None, int]:
""":func:`run_budget`, with a warning where the case sets fewer runs than the rule."""
init_runs, max_runs, first = run_budget(calibration, n_parameters)
recommended = recommended_initial_runs(n_parameters)
if init_runs < recommended:
design_log.warning(
"calibration.init_runs = %d is below the %d initial runs that %d calibration "
"parameters need (%d per parameter, rounded up to a power of two for an even "
"Sobol design). Remove init_runs from the case file to use the rule, or "
"calibrate fewer parameters.", init_runs, recommended, n_parameters,
RUNS_PER_PARAMETER)
if max_runs is not None and max_runs <= init_runs:
from axqua.core.errors import ConfigError
raise ConfigError(
f"calibration.max_runs = {max_runs} leaves no run for the active learning "
f"after {init_runs} initial runs",
subject="calibration.max_runs",
remedy=f"Raise max_runs above {init_runs}, or remove it from the case file: "
"the active learning then runs until it converges.")
return init_runs, max_runs, first
def _scalar_velocity_with_error(df: pd.DataFrame) -> tuple[pd.Series, pd.Series | None]:
"""Speed from the velocity components, with the component errors propagated.
Shared by ``SCALAR VELOCITY`` (TELEMAC) and ``U_MAG`` (OpenFOAM) so the two
cannot drift apart: they are the same quantity under two solvers' names, and
the error propagation is the part that would be easy to get subtly different.
sigma_V = (1/V) * sqrt(sum_i (c_i * sigma_i)^2), over the horizontal components ``u`` and ``v`` (:func:`axqua.ground_truth.scalar_velocity`).
"""
vel = ground_truth.scalar_velocity(df)
terms = [(df[c].astype(float) * df[f"{c}_err"].astype(float)) ** 2
for c in ("u", "v") if c in df and f"{c}_err" in df]
err = None
if terms:
safe_v = vel.replace(0.0, np.nan)
err = (np.sqrt(sum(terms)) / safe_v).fillna(0.0)
return vel, err
def _resolve_quantity(df: pd.DataFrame, qty: str) -> tuple[pd.Series, pd.Series | None] | None:
"""Map a SELAFIN quantity name to (values, measured_error) from a tidy table.
Returns ``None`` when the tidy table lacks the columns to produce *qty*, so
callers can report missing quantities. ``measured_error`` is ``None`` when no
per-point error was measured (the caller falls back to the configured
fraction).
"""
q = qty.upper()
if q == "WATER DEPTH" and "h" in df:
return df["h"].astype(float), None
if q in ("TURBULENT ENERG.", "TURBULENT ENERGY") and "tke" in df:
return df["tke"].astype(float), None
if q in ("CUMUL BED EVOL", "BED EVOLUTION") and "dz" in df:
return df["dz"].astype(float), None
if q == "SCALAR VELOCITY" and "u_mag" in df:
err = df["u_mag_std"].astype(float) if "u_mag_std" in df else None
return df["u_mag"].astype(float), err
if q == "SCALAR VELOCITY" and any(c in df for c in ("u", "v")):
return _scalar_velocity_with_error(df)
# --- OpenFOAM field names (axqua.solvers.openfoam.calibration) ------------
# HydroBayesCal's OpenFOAM binding extracts U_x/U_y/U_z/U_MAG/TKE, not the
# SELAFIN names, and compares them case-SENSITIVELY - so the spelling the
# caller passes is the spelling that must reach the CSV header.
for name, comp in (("U_X", "u"), ("U_Y", "v"), ("U_Z", "w")):
if q == name and comp in df:
err = df[f"{comp}_err"].astype(float) if f"{comp}_err" in df else None
return df[comp].astype(float), err
if q == "U_MAG" and "u_mag" in df:
err = df["u_mag_std"].astype(float) if "u_mag_std" in df else None
return df["u_mag"].astype(float), err
if q == "U_MAG" and any(c in df for c in ("u", "v")):
return _scalar_velocity_with_error(df)
if q == "TKE" and "tke" in df:
return df["tke"].astype(float), None
if q == "VELOCITY U" and "u" in df:
return df["u"].astype(float), df["u_err"].astype(float) if "u_err" in df else None
if q == "VELOCITY V" and "v" in df:
return df["v"].astype(float), df["v_err"].astype(float) if "v_err" in df else None
return None
def _pick_table(tables: dict[str, pd.DataFrame], quantities: list[str]) -> pd.DataFrame:
"""Choose the tidy category whose columns satisfy all calibration targets."""
for df in (*([tables["hydraulics"]] if "hydraulics" in tables else []), *tables.values()):
if all(_resolve_quantity(df, q) is not None for q in quantities):
return df
available = {cat: [c for c in df.columns if c not in ("x", "y", "z")]
for cat, df in tables.items()}
raise ValueError(
f"no ground-truth category provides all calibration targets {quantities}. "
f"Available columns per category: {available}"
)
def _positional_errors(cfg, df, quantities) -> dict:
"""Per-quantity uncertainty induced by not knowing where the point is.
Returns ``{}`` unless the case declares a survey position quality, so every
existing config produces a byte-identical CSV. Never raises: a positional
error bar is an improvement to the error budget, and failing to compute one
must not cost the calibration its inputs.
"""
from axqua import positional_error as pe
# resolve_sigma's ValueError (an unknown quality name) is deliberately NOT
# caught: that is a typo in the config, and silently calibrating without the
# error bar the user asked for is the wrong response to it.
sigma, quality = pe.resolve_sigma(cfg)
if not sigma:
return {}
geometry = Path(cfg.model_path(cfg.geometry_slf))
results = Path(cfg.model_path("r2d.slf"))
if not (geometry.is_file() and results.is_file()):
log.warning(
"%s, but there is no converged 2D result to measure it against yet "
"(%s). The calibration CSV carries measurement error only; rebuild it "
"after the steady run to include the positional term.",
pe.describe(sigma, quality), results.name)
return {}
x = df["x"].to_numpy(float)
y = df["y"].to_numpy(float)
out = {}
for qty in quantities:
variable = _SELAFIN_VARIABLE.get(qty, qty)
try:
sampler = pe.telemac_sampler(geometry, results, variable)
except (KeyError, OSError) as exc:
log.debug("no positional error for %s: %s", qty, exc)
continue
spread = pe.sample_spread(sampler, x, y, sigma)
if np.any(spread > 0):
out[qty] = spread
log.info("%s -> %s positional error: median %.3f, max %.3f "
"(added in quadrature to the measurement error)",
pe.describe(sigma, quality), qty,
float(np.median(spread)), float(np.max(spread)))
return out
#: Calibration target -> the SELAFIN variable whose spatial variation carries
#: the positional uncertainty. A component velocity varies like the speed does.
_SELAFIN_VARIABLE = {
"U_x": "VELOCITY U", "U_y": "VELOCITY V", "U_z": "SCALAR VELOCITY",
"U_MAG": "SCALAR VELOCITY", "TKE": "SCALAR VELOCITY",
}
[docs]
def build_calibration_csv(cfg: Config, *,
floors: Mapping[str, float] | None = None,
path: Path | None = None,
z_resolver: "Callable[[pd.DataFrame], np.ndarray] | None" = None,
category: str | None = None,
point_filter: "Callable[[pd.DataFrame], np.ndarray] | None" = None,
) -> Path | None:
"""Write the HydroBayesCal calibration-points CSV from the tidy ground truth.
The tidy table is compiled from ``ground_truth.sources`` when given, otherwise
read from a user-authored ``ground_truth.measurements``. Returns ``None`` if neither
is configured.
*floors* clamps the per-quantity ``_ERROR`` column from below. This is a
correctness guard, not a nicety: HydroBayesCal builds each observation's
variance from this column, so a measured error that is zero (or a value near
zero multiplied by the relative fraction) gives that point a near-zero variance
and lets the least trustworthy measurement dominate the whole likelihood. The
default ``None`` leaves the existing behavior exactly as it was.
*path* overrides the output location, for a second CSV alongside the default
one (the OpenFOAM calibration targets the same points with different columns).
*z_resolver* replaces the ``z`` column. The default (``None``) writes the tidy
table's own ``z``, which is the **surveyed bed** - fine for a 2D calibration,
where ``z`` is ignored, and wrong for a 3D one, where it places the extraction
point on the bed and couples it to the survey's vertical accuracy. A solver
adapter supplies a resolver that places targets in the model's own column
instead (see :mod:`axqua.model_column`). It is a callback rather than a flag
because the two HydroBayesCal bindings disagree on what ``z`` means - OpenFOAM
wants an absolute elevation, TELEMAC-3D a height above the model bed - and that
knowledge belongs to the adapter, not here.
*category* selects one tidy tab by name instead of letting the quantities
decide. This is what lets a 2D and a 3D calibration read the *same* survey
under **different profile rules** - the 2D one depth-averaged, the 3D one
per-reading - by compiling them as separate categories (``hydraulics`` and
``hydraulics-3d``). They must not share a category: ``compile_ground_truth``
concatenates sources that do, which would silently hand both calibrations the
union of the two row sets.
*point_filter* returns, for the table of points, which rows to keep. A solver adapter that may not reach into the mesh of another solver is handed the rules of the case this way: the OpenFOAM calibration leaves out the points outside the 2D model and those near an inflow or outflow line (:mod:`axqua.openfoam_situations`).
"""
compile_ground_truth(cfg) # no-op when the table is user-supplied
if not cfg.ground_truth.sources and cfg.ground_truth.measurements is None \
and cfg.ground_truth.targets is None:
return None
if not Path(cfg.ground_truth_path).exists():
# e.g. a calibration-target template with only the parameters tab filled:
# no measurement points yet, but the parameter ranges still reach the HBC
# config via merged_parameters()
log.info("no ground-truth measurements compiled (yet); skipping the "
"calibration-points CSV")
return None
tables = ground_truth.read_tidy(cfg.ground_truth_path)
quantities = cfg.calibration.calibration_quantities
if category is not None:
if category not in tables:
raise ValueError(
f"ground-truth category {category!r} is not in "
f"{Path(cfg.ground_truth_path).name} (has: {', '.join(tables)}). "
"Add a ground_truth source with that category, or drop the "
"category= argument to pick the tab by its quantities.")
df = tables[category].reset_index(drop=True)
else:
df = _pick_table(tables, quantities).reset_index(drop=True)
if point_filter is not None:
keep = np.asarray(point_filter(df), dtype=bool)
if keep.shape != (len(df),):
raise ValueError(f"the point filter returned {keep.shape} for {len(df)} points")
df = df.loc[keep].reset_index(drop=True)
if z_resolver is not None:
z_column = np.asarray(z_resolver(df), dtype=float)
elif "z" in df:
z_column = df["z"].astype(float)
else:
z_column = 0.0
out = pd.DataFrame({
"id": np.arange(1, len(df) + 1),
"x": df["x"].astype(float),
"y": df["y"].astype(float),
"z": z_column,
})
from axqua.hbc import is_datum
frac = cfg.calibration.measurement_error
positional = _positional_errors(cfg, df, quantities)
for qty in quantities:
values, measured_err = _resolve_quantity(df, qty)
# a share of a level or a temperature is no error (10 % of 816 m is 82 m)
share = 0.0 if is_datum(qty) else frac
err = measured_err if measured_err is not None else values.abs() * share
floor = (floors or {}).get(qty)
if floor is not None:
err = err.clip(lower=float(floor))
extra = positional.get(qty)
if extra is not None:
# In quadrature: the position error and the instrument error are
# independent. Applied AFTER the floor, because the floor is a
# statement about the instrument and this is not.
err = np.sqrt(err.astype(float) ** 2 + np.asarray(extra) ** 2)
out[f"{qty}_DATA"] = values.round(6)
out[f"{qty}_ERROR"] = np.round(err, 6)
path = Path(path) if path is not None else cfg.calibration_path(cfg.calibration_csv)
path.parent.mkdir(parents=True, exist_ok=True)
out.to_csv(path, index=False)
return path
[docs]
def compile_ground_truth(cfg: Config) -> Path | None:
"""Compile configured raw ground-truth sources into the tidy table (if any)."""
return ground_truth.compile_ground_truth(cfg)
[docs]
def roughness_zone_parameters(cfg: Config) -> list:
"""``zone<N>`` calibration parameters declared by the roughness table.
A zone is a calibration parameter when it has a non-degenerate
``[ks_min, ks_max]`` prior and is not flagged ``calibration = False``. Writing
a genuine range is itself the declaration, so a table with bounds but no
``calibration`` column calibrates all of them rather than silently none; the
column is how you *opt a zone out*. Excluded zones are held at their nominal ks
- they still get a ``.tbl`` row and still influence the flow, they are just not
inferred.
Returns an empty list for the legacy single-``ks`` schema, which carries no
bounds; there the config's ``calibration.parameters`` remains the only source.
"""
from axqua.config import CalibrationParameter
from axqua.core.geodata import read_roughness_zones
if cfg.geodata.roughness_table is None:
return []
out = []
for zid, z in sorted(read_roughness_zones(cfg.geodata.roughness_table).items()):
if z.calibrate is False or z.ks_min is None or z.ks_max is None:
continue
if z.ks_max <= z.ks_min:
log.warning(" roughness zone %d flagged for calibration but its prior "
"is degenerate (ks_min=%g >= ks_max=%g) - held fixed at %g m",
zid, z.ks_min, z.ks_max, z.ks)
continue
out.append(CalibrationParameter(
name=f"zone{zid}", min=z.ks_min, max=z.ks_max,
comment=f"roughness zone {zid} ks [m] (Nikuradse)"))
return out
[docs]
def merged_parameters(cfg: Config) -> list:
"""Calibration parameters, merged from the three places they can be declared.
Precedence, later wins on a name collision: config ``calibration.parameters``
< the roughness table's ``zone<N>`` rows (``geodata.roughness_table`` with the
bounds schema) < the ``parameters`` tab of the filled calibration-target
template, and ``calibration.outflow_offset`` as the parameter
``f.OUTFLOW_OFFSET`` or ``calibration.outflow_slope`` as ``f.OUTFLOW_LOG_SLOPE``. The roughness table outranks the config because it is where the
zonation itself is defined - adding a zone there must not require editing the
parameter list in two files - while the spreadsheet stays the live override.
"""
from axqua import targets as targets_mod
from axqua.solvers.telemac import outflow_offset
by_name = {p.name: p for p in cfg.calibration.parameters}
for p in roughness_zone_parameters(cfg):
by_name[p.name] = p
for p in targets_mod.read_target_parameters(cfg):
by_name[p.name] = p
# The outflow level is declared by calibration.outflow_offset or outflow_slope only: the routine, the files a run rewrites and the ramp follow that setting, and a row of the parameter list or the template named neither (review of 2026-11-18).
reserved = {name.upper() for name in outflow_offset.NAMES}
rows = sorted(name for name in by_name if str(name).strip().upper() in reserved)
if rows:
from axqua.core.errors import ConfigError
raise ConfigError(
f"the parameter list names {', '.join(rows)}: the outflow level is calibrated "
"through calibration.outflow_offset or calibration.outflow_slope",
subject="calibration.parameters",
remedy="Remove the row and give the range as calibration.outflow_offset "
"(an offset in m) or calibration.outflow_slope (an energy slope).")
offset = outflow_offset.parameter(cfg)
if offset is not None:
by_name[offset.name] = offset
return list(by_name.values())
[docs]
def telemac_parameters(cfg: Config) -> list:
"""The declared parameters minus the ones that belong to another solver.
A case that calibrates both codes declares both sets in one
``calibration.parameters`` list, so this filter is the mirror image of
:func:`axqua.solvers.openfoam.calibration.openfoam_parameters`. Without it an
OpenFOAM ``ks`` / ``Cmu`` would be emitted into ``config_Telemac.py`` as though
it were a TELEMAC keyword, and HydroBayesCal would spend the whole campaign
rewriting a ``.cas`` line that does not exist.
"""
from axqua.solvers.openfoam.spec import OPENFOAM_PARAMETERS
keep, drop = [], []
for p in merged_parameters(cfg):
# a zone<N> roughness parameter is TELEMAC's and must survive: only the
# names that are exclusively OpenFOAM's are removed
if str(p.name).strip().lower() in OPENFOAM_PARAMETERS:
drop.append(p.name)
else:
keep.append(p)
if drop:
log.info("not TELEMAC calibration parameters, skipped: %s (they belong to "
"the OpenFOAM calibration)", ", ".join(sorted(drop)))
return keep
[docs]
def fortran_file_of(cfg: Config, names) -> str | None:
"""The file of the user Fortran folder HydroBayesCal rewrites for the ``f.``
parameters *names*: ``user_sl.f`` for the outflow level (offset or slope)
(:mod:`axqua.solvers.telemac.outflow_offset`), ``user_rain.f`` for a gain-lose
reach, the heat routine for its own parameters, else None."""
from axqua.solvers.telemac import heat, heat_calibration, outflow_offset
if outflow_offset.calibrated(names):
return outflow_offset.FILE
if any(heat_calibration.is_heat_parameter(name) for name in names):
return heat.ROUTINE
if cfg.gain_lose.active and cfg.gain_lose.implementation == "fortran":
return "user_rain.f"
return None
def _refuse_continued_keywords(cfg: Config, names: list[str], control_file: str) -> None:
"""Stop before a calibration of a keyword that the built steering file writes on several lines: HydroBayesCal rewrites the first line only (``update_gaia_class_line``), so the old values on the next ones would be read as keywords and every run would stop."""
import re
from axqua.solvers.telemac.steering import continued_keywords
files = {"telemac": cfg.model_path(control_file)}
if cfg.morphodynamics.enabled:
files["gaia"] = cfg.model_path(cfg.gaia_cas)
continued = {kind: continued_keywords(path) for kind, path in files.items()
if path.is_file()}
problems = []
for name in names:
text = str(name).strip()
if re.fullmatch(r"zone\d+", text, re.I) or text.startswith("f."):
continue
kind = "gaia" if text.lower().startswith("gaia") else "telemac"
keyword = re.sub(r"\s+\d+$", "", text[4:] if kind == "gaia" else text).strip().upper()
if keyword in continued.get(kind, set()):
problems.append(f"{name} ({files[kind].name})")
if problems:
raise ValueError(
"HydroBayesCal cannot calibrate a keyword written on more than one line "
f"of the steering file: {', '.join(problems)}. The values of these keywords "
"do not fit TELEMAC's 72 columns; calibrate fewer sediment classes or other "
"parameters")
[docs]
def emit_hbc_config(cfg: Config, calibration_csv: Path | None, *,
complete_bal_mode: bool = True, only_bal_mode: bool = False,
parameters: list | None = None,
budget: tuple | None = None,
measurement_share: float | None = None) -> Path:
"""Emit a HydroBayesCal ``config_Telemac.py`` for the produced case.
*complete_bal_mode* False runs the initial simulations only; *only_bal_mode* True
(with *complete_bal_mode*) continues from the saved initial simulations with the
active learning (HydroBayesCal's two execution switches). *parameters* are the
ones to calibrate (default :func:`telemac_parameters`; a calibration passes those of
its zone plan, :func:`axqua.calibration_zones.zone_plan`). *budget* ``(init_runs,
max_runs, adaptive)`` replaces the case's run counts, and the adaptive initial
design unless *adaptive* is None, for the passes of
:func:`axqua.bayescal._launch_in_passes`. *measurement_share* is HydroBayesCal's
``measurement_error``, the share of every measured value that it adds to the error of
the points file; None writes its default (:data:`axqua.bayescal.HBC_MEASUREMENT_SHARE`).
"""
from axqua.bayescal import HBC_MEASUREMENT_SHARE
# *measurement_share*: None keeps HydroBayesCal's own default, stated here
share = float(HBC_MEASUREMENT_SHARE if measurement_share is None else measurement_share)
if calibration_csv is not None and Path(calibration_csv).is_file():
from axqua.hbc import warn_zero_variance
# HydroBayesCal 1.16.0 refuses them when it sets up; say which and why first
warn_zero_variance(calibration_csv, cfg.calibration.calibration_quantities, share)
from axqua.solvers.telemac import heat
c = cfg.calibration
parameters = telemac_parameters(cfg) if parameters is None else list(parameters)
init_runs, max_runs, first = run_budget(c, len(parameters))
adaptive = None
if budget:
init_runs, max_runs, adaptive = budget
max_runs = pass_ceiling(init_runs, max_runs)
adaptive = bool(c.adaptive_init_runs) if adaptive is None else bool(adaptive)
first = min(first, int(init_runs))
params = [p.name for p in parameters]
ranges = [[p.min, p.max] for p in parameters]
# which case HydroBayesCal drives: the built steady 2D case by default, or the
# 3D steering file when calibrating vertical profiles (calibration.solver_name)
control_file = c.control_file or cfg.cas_file
_refuse_continued_keywords(cfg, params, control_file)
if c.results_base:
results_base = c.results_base
elif c.solver_name == "Telemac3d":
# HydroBayesCal sets '3D RESULT FILE' from this base and derives the
# companion 2D file as <base>_<id>_2d.slf, so the base must be the 3D one.
results_base = Path(cfg.results3d_slf).stem
else:
results_base = Path(cfg.results_slf).stem
# HydroBayesCal resolves this against <model_dir>/user_fortran/, and rewrites a
# line in it for any calibration parameter prefixed `f.` - which is how the
# gain-lose conductivity (`f.HMP_KF`) is calibrated. Only meaningful when a
# gain-lose reach actually generated the routine.
fortran_file = fortran_file_of(cfg, params)
def pylist(items, q=True):
if q:
return "[" + ", ".join(repr(str(i)) for i in items) + "]"
return "[" + ", ".join(str(i) for i in items) + "]"
# bal_telemac.py reads config.morphodynamic_simulation['gaia_cas'] /
# ['gaia_results_filename_base'] unconditionally, so always emit the block;
# the GAIA values are None when morphodynamics is off (hydraulic-only case).
if cfg.morphodynamics.enabled:
gaia_cas = repr(cfg.gaia_cas)
gaia_base = repr(cfg.morphodynamics.gaia_results_base)
else:
gaia_cas = gaia_base = "None"
morph = (
"morphodynamic_simulation = {\n"
f" 'gaia_cas': {gaia_cas},\n"
f" 'gaia_results_filename_base': {gaia_base},\n"
"}\n\n"
)
text = f'''"""HydroBayesCal TELEMAC config generated by axqua for case '{cfg.name}'.
Run a surrogate-assisted Bayesian calibration with:
python bal_telemac.py --config {cfg.hbc_config}
"""
import os
BASE_DIR = {str(cfg.model_dir)!r}
paths = {{
'case_template_dir': BASE_DIR,
'model_dir': BASE_DIR,
'res_dir': {str(cfg.calibration_dir)!r},
'calibration_pts_file_path': {(str(calibration_csv) if calibration_csv else "")!r},
}}
hydrodynamic_simulation = {{
'solver_name': {c.solver_name!r},
'n_processors': {cfg.telemac.n_processors},
'results_filename_base': {results_base!r},
'control_file': {control_file!r},
'friction_file': {cfg.friction_tbl!r},
'fortran_file': {fortran_file!r},
}}
{morph}calibration = {{
'parameters': {pylist(params)},
'param_values': {ranges},
'extraction_quantities': {pylist(c.extraction_quantities)},
'calibration_quantities': {pylist(c.calibration_quantities)},
'dict_output_name': 'extraction-data',
# HydroBayesCal adds this share of every measured value to its error, in
# quadrature with the error of the points file (0 for a temperature in deg C)
'measurement_error': {share!r},
}}
# WHEN to read the model outputs. "last" takes the converged frame; the stock
# default "mean_last" averages the final n_last frames, which on a run marching to
# steady state from a dry or pre-wetted start folds the residual transient into the
# values the surrogate is trained on.
# Emitting it here rather than relying on axqua's source patch of the staged
# driver: the patch rewrites a string literal in someone else's file, so it silently
# no-ops if that literal ever changes. This says it in the config, which is the
# interface the drivers actually document. (The patch is kept as well - hydrobayescal
# 1.4.3's multiflow driver ignores this block, and only honors it from 1.4.4 on.)
# n_last is unused while output_extraction_time is 'last'; 1 is the value that keeps
# the intent if anyone flips this back to 'mean_last'.
extraction = {{
'output_extraction_time': 'last',
'n_last': 1,
}}
sampling = {{
'init_runs': {init_runs},
'max_runs': {max_runs},
'parameter_distribution': 'uniform',
'parameter_sampling_method': {c.parameter_sampling_method!r},
# HydroBayesCal >= 1.5: grow the initial design in Sobol blocks and stop when
# it is measurably sufficient. init_runs is the ceiling and init_runs_min the
# first block, 10 runs per parameter rounded up to a power of two unless the case
# sets it (axqua.calibration.run_budget), so the design never stops below that.
'adaptive_init_runs': {adaptive},
'init_runs_min': {first},
# exploit the posterior until >1 well-separated mode is found, then explore
'bal_exploration_tradeoff': {c.bal_exploration_tradeoff!r},
'tp_selection_criteria': 'dkl',
'eval_steps': 1,
# prior samples the surrogate is predicted at (calibration.prior_samples); a sharp
# posterior keeps few of them, so raise it when few are accepted.
'prior_samples': {c.prior_samples},
# seed of the prior sample (HydroBayesCal after 1.15; older ones ignore it): a repeated calibration or a
# restart draws the same sample, so its posterior and next runs repeat as well
'random_seed': 1,
'mc_samples_al': 2000,
'mc_exploration': 1000,
'gp_library': {c.gp_library!r},
'gp_mean': {c.gp_mean!r},
# 'variables': one multi-output GPE per location, its outputs the calibration
# quantities (calibration.multitask_selection)
'multitask_selection': {c.multitask_selection!r},
}}
execution = {{
'complete_bal_mode': {complete_bal_mode},
'only_bal_mode': {only_bal_mode},
'delete_complex_outputs': True,
'validation': False,
'user_param_values': False,
}}
'''
# HydroBayesCal reads the result variables it has a list of, which the temperatures of a
# heat run are not in; the driver imports this file before it runs the model
own = [q for q in dict.fromkeys([*c.extraction_quantities, *c.calibration_quantities])
if q in heat.STATISTICS]
if own:
text += f'''
from hydroBayesCal.telemac import config_telemac as _known
_known.classification_tm_gaia_dict.update({{name: "telemac" for name in {own!r}}})
'''
path = cfg.calibration_path(cfg.hbc_config)
path.write_text(text)
return path