Cette page a été traduite automatiquement de l’anglais et peut contenir des erreurs. Original en anglais
Code source de axqua.solvers.telemac.backend
"""The TELEMAC backend: what the job system calls, adapting what already exists.
Deliberately thin. Every piece of work here is a function the case-template scripts have
been calling for months - ``pipeline.run``, ``run_solver_streaming``,
``analyze_flux_convergence``, ``run_mesh_convergence``, ``run_single_flow_calibration``.
The backend adds no hydraulics and no numerics; it maps a *verb plus a capability* onto
the right one of them and passes the job's event sink and cancel token down.
That thinness is the point. ``cases/*/initial_run.py`` and a submitted ``steady`` job must
drive the same code, because the standalone scripts remain the supported path (plan §29)
and a second implementation would drift the moment one of them was fixed.
This module is loaded **only** through ``BackendSpec.load()``. It imports gmsh, numpy and
the rest transitively, so it must never be imported by ``spec.py`` or by the package
``__init__`` - ``tests/test_capabilities.py`` asserts exactly that from a subprocess.
"""
from __future__ import annotations
import logging
from pathlib import Path
from typing import Any
from axqua.core import errors
from axqua.core.capabilities import Capability
from axqua.core.registry import BaseBackend
from axqua.jobs.results import STYLE_DEPTH, STYLE_HIGH_WATER, STYLE_VELOCITY
log = logging.getLogger("axqua.solvers.telemac.backend")
#: Which ``.cas`` each 3D variant runs, and what its result is called. Kept here rather
#: than re-derived, because ``spec.py`` already names the same three files and the two
#: must not drift.
#: What a case in hydrology mode does not run: a steady state, the unsteady and 3D runs that start from one, the studies that compare steady runs and the calibration that perturbs one. A file left from when the case was a river case (``steady2d.cas``, ``r2d.slf``, ``unsteady2d.cas``) would otherwise run a model that is no longer the case.
_NOT_IN_HYDROLOGY_MODE = frozenset({
Capability.STEADY2D, Capability.UNSTEADY2D, Capability.UNSTEADY3D, Capability.STEADY3D,
Capability.MESH_CONVERGENCE, Capability.VERTICAL_CONVERGENCE, Capability.CALIBRATION})
def _refuse_in_hydrology_mode(cfg, capability) -> None:
"""A :class:`~axqua.core.errors.ConfigError` for a capability of :data:`_NOT_IN_HYDROLOGY_MODE` in a case in hydrology mode."""
if getattr(cfg, "hydrology_mode", False) and capability in _NOT_IN_HYDROLOGY_MODE:
raise errors.ConfigError(
f"a case in hydrology mode has no {capability.value} simulation",
subject="hydrology.enabled",
remedy="Run the storm with the hydrology run (axqua submit <case> --kind "
"hydrology), or switch the hydrology mode off and build the case again.")
_3D_VARIANTS = {
"hydrostatic": ("hotstart3d_hydrostatic.cas", "r3d-hydrostatic.slf"),
"hydrodyn": ("hotstart3d_hydrodyn.cas", None),
"unsteady": ("unsteady3d.cas", None),
}
[docs]
def result_layers(cfg, three_d=None) -> list[dict]:
"""The map layers of every 2D result of the case that exists, as entries of a result manifest: ``{name, path, kind, style, variable, extra}``.
A job records them (:meth:`TelemacBackend.export_qgis_results`), and ``axqua export
--list`` names them per result, so that the plugin puts a result of the case on the
map without a job (a case imported from a steering file has none). *three_d* is the
case the 3D runs work in (:func:`axqua.solvers.telemac.threed.three_d_case`).
"""
from axqua.solvers.telemac import threed
c3 = three_d if three_d is not None else threed.three_d_case(cfg, prepare=False)
post = getattr(cfg, "postproc", None)
# Which maps the case asks for (postproc.telemac_maps), and the predefined map style
# the plugin applies to the depth layer (postproc.map_style); an empty list is a
# choice: no map.
maps = getattr(post, "telemac_maps", None)
maps = set(("depth", "velocity") if maps is None else maps)
style = str(getattr(post, "map_style", "") or "")
out: list[dict] = []
# Results, in the order a user wants them: the thing that was computed first. The
# last column is the dataset the velocity is styled by. A 2D result prints the
# scalar velocity; the depth-averaged companion of a 3D run carries the two
# components only, which QGIS reads as one vector group named VELOCITY. The
# hydrostatic 3D run keeps its results under names of its own.
for name, label, velocity, case in (
(getattr(cfg, "results_slf", None), "TELEMAC result (steady)",
"SCALAR VELOCITY", cfg),
(getattr(cfg, "results_unsteady_slf", None), "TELEMAC result (unsteady)",
"SCALAR VELOCITY", cfg),
(getattr(cfg, "results_hydrology_slf", None), "TELEMAC result (hydrology)",
"SCALAR VELOCITY", cfg),
(getattr(cfg, "results2d_from_3d_slf", None),
"TELEMAC-3D result (depth-averaged)", "VELOCITY", c3),
(threed.HYDROSTATIC_RESULT_2D,
"TELEMAC-3D result, hydrostatic (depth-averaged)", "VELOCITY", c3),
):
if not name:
continue
path = case.model_path(name)
if not path.exists():
continue
if "depth" in maps:
out.append({"name": label, "path": str(path), "kind": "mesh",
"style": STYLE_DEPTH, "variable": "WATER DEPTH",
"extra": {"map_style": style} if style else {}})
# The same file carries velocity; a second entry lets the plugin add two
# differently-styled layers over one dataset without reading it twice.
if "velocity" in maps:
out.append({"name": f"{label} - velocity", "path": str(path), "kind": "mesh",
"style": STYLE_VELOCITY, "variable": velocity, "extra": {}})
if "free-surface" in maps:
out.append({"name": f"{label} - free surface", "path": str(path),
"kind": "mesh", "style": "", "variable": "FREE SURFACE",
"extra": {}})
return out
[docs]
class TelemacBackend(BaseBackend):
"""TELEMAC-2D / TELEMAC-3D, with GAIA where the case asks for it."""
name = "telemac"
# -- environment --------------------------------------------------------------
[docs]
def check_environment(self, cfg) -> bool:
"""True when the TELEMAC environment can actually be entered.
Must not raise (the protocol says so), because this is also called from
``axqua status --check-env`` where a broken install is information, not a
crash.
"""
from axqua.env import TelemacRuntime
try:
TelemacRuntime(cfg.telemac).check_available()
except Exception as exc: # noqa: BLE001 - report, never raise
log.debug("telemac unavailable: %s: %s", type(exc).__name__, exc)
return False
return True
# -- build --------------------------------------------------------------------
[docs]
def build(self, cfg, capability: Capability | None = None, *, ctx: Any = None,
**kwargs):
capability = capability or Capability.STEADY2D
if capability is not Capability.STEADY2D:
# the build of the case itself is the build of STEADY2D, in either mode
_refuse_in_hydrology_mode(cfg, capability)
if capability in (Capability.UNSTEADY2D, Capability.UNSTEADY3D):
return self._build_unsteady(cfg, ctx=ctx, **kwargs)
if capability in (Capability.STEADY3D,):
return self._build_3d(cfg, ctx=ctx, **kwargs)
# a hydrology case is built by the same pipeline (it knows the mode)
return self._build_case(cfg, ctx=ctx, **kwargs)
def _build_case(self, cfg, *, ctx: Any = None, discharge: float | None = None,
validate_env: bool = True, dry_run: bool = False, **_ignored):
from axqua.solvers.telemac import pipeline
from axqua.workflow import prepare_steady_inputs
prepare_steady_inputs(cfg, discharge)
if ctx is not None:
ctx.check_cancelled()
# log_to_file=False: the job already owns runner.log, and a second logfile in
# model_dir would split the narration of one run across two places.
artifacts = pipeline.run(cfg, validate_env=validate_env, dry_run=dry_run,
log_to_file=False)
if ctx is not None:
for name in ("geometry_slf", "boundary_cli", "cas_file", "friction_tbl",
"hydrology_cas"):
path = getattr(artifacts, name, None)
if path:
ctx.record(Path(path).name, path, kind="file")
return artifacts
def _build_3d(self, cfg, *, ctx: Any = None, hydrostatic_steps: int | None = None,
n_levels: int | None = None, **_ignored):
from axqua.solvers.telemac.threed import build_3d_cases, three_d_case
cfg = three_d_case(cfg)
kwargs: dict[str, Any] = {}
if hydrostatic_steps is not None:
kwargs["hydrostatic_steps"] = hydrostatic_steps
if n_levels is not None:
kwargs["n_levels"] = n_levels
return build_3d_cases(cfg, **kwargs)
def _build_unsteady(self, cfg, *, ctx: Any = None, mode_3d: bool = False,
control_sections: bool = True, **_ignored):
from axqua.solvers.telemac.unsteady import (build_unsteady_3d_case,
build_unsteady_case)
if mode_3d:
from axqua.solvers.telemac.threed import three_d_case
return build_unsteady_3d_case(three_d_case(cfg))
return build_unsteady_case(cfg, control_sections=control_sections)
# -- run ----------------------------------------------------------------------
[docs]
def run(self, cfg, capability: Capability | None = None, *, ctx: Any = None,
**kwargs):
capability = capability or Capability.STEADY2D
_refuse_in_hydrology_mode(cfg, capability)
if capability is Capability.STEADY3D:
return self._run_3d(cfg, ctx=ctx, **kwargs)
if capability in (Capability.UNSTEADY2D, Capability.UNSTEADY3D):
return self._run_unsteady(cfg, ctx=ctx, **kwargs)
if capability is Capability.HYDROLOGY:
return self._run_hydrology(cfg, ctx=ctx, **kwargs)
return self._run_steady(cfg, ctx=ctx, **kwargs)
def _run_hydrology(self, cfg, *, ctx: Any = None, ncsize: int | None = None,
**_ignored):
from axqua.solvers.telemac.steering import cas_duration
if not cfg.hydrology_mode:
raise errors.ConfigError(
"the case is not in hydrology mode", subject="hydrology.enabled",
remedy="Switch the hydrology mode on in the case settings and build "
"the case again.")
return self._launch(cfg, ctx=ctx, cas_file=cfg.hydrology_cas_file,
ncsize=ncsize, solver=None, name="telemac-hydrology",
duration=cas_duration(cfg.model_path(cfg.hydrology_cas_file)))
def _run_steady(self, cfg, *, ctx: Any = None, ncsize: int | None = None,
cas_file: str | None = None, **_ignored):
return self._launch(cfg, ctx=ctx, cas_file=cas_file or cfg.cas_file,
ncsize=ncsize, solver=None, name="telemac2d")
def _run_3d(self, cfg, *, ctx: Any = None, variant: str = "hydrostatic",
ncsize: int | None = None, **_ignored):
if variant not in _3D_VARIANTS:
raise errors.ConfigError(
f"unknown 3D variant {variant!r}",
subject="options.variant",
remedy="Use one of: " + ", ".join(_3D_VARIANTS),
)
cas_name = _3D_VARIANTS[variant][0]
from axqua.solvers.telemac.threed import three_d_case
cfg = three_d_case(cfg, prepare=False)
if not cfg.model_path(cas_name).exists():
raise errors.ConfigError(
f"{cas_name} has not been written yet",
subject="options.variant",
remedy="Submit a build-3d job first (or run add3d.py).",
)
# TELEMAC-3D has no DURATION keyword, so the progress bar spans the fixed step
# count instead - which is why the duration is computed rather than read.
duration = self._three_d_duration(cfg, cas_name)
return self._launch(cfg, ctx=ctx, cas_file=cas_name, ncsize=ncsize,
solver="telemac3d", name=f"telemac3d-{variant}",
duration=duration)
def _run_unsteady(self, cfg, *, ctx: Any = None, mode_3d: bool = False,
control_sections: bool = True, ncsize: int | None = None,
run: bool = True, calibrated_heat: bool = False, **_ignored):
"""The hydrograph run. In 2D the case is written first, from the case file as it is now, so the run continues from the steady result and a changed hydrograph needs no new mesh; the 3D case is the one of the build-3d job. With *run* false the 2D case is written and not started."""
if mode_3d:
if cfg.heat.enabled:
raise errors.ConfigError(
"the water temperature is simulated with TELEMAC-2D only",
subject="heat.enabled", remedy="Run the unsteady case in 2D.")
if not run:
return None
from axqua.solvers.telemac.threed import UNSTEADY3D_CAS, three_d_case
return self._launch(three_d_case(cfg, prepare=False), ctx=ctx,
cas_file=UNSTEADY3D_CAS, ncsize=ncsize,
solver="telemac3d", name="telemac-unsteady")
from axqua import bayescal
from axqua.solvers.telemac.unsteady import build_unsteady_case
# before the case is written, or the files of an interrupted calibration would be
# restored over it
if bayescal.restore_as_built(cfg):
log.warning("restored the built case: a calibration had been interrupted and had "
"left its last tested parameter set in %s", cfg.model_dir)
if calibrated_heat and cfg.heat.enabled:
from axqua.solvers.telemac import heat_calibration
heat_calibration.use_calibrated(cfg)
try:
setup = build_unsteady_case(cfg, control_sections=control_sections)
except ValueError as exc: # no hydrograph, or a constant one
raise errors.ConfigError(
str(exc), subject="boundaries.inflow",
remedy="Check the table named in boundaries.inflow.") from exc
if ctx is not None:
ctx.record(setup.cas.name, setup.cas, kind="file")
if not run:
return setup
proc = self._launch(cfg, ctx=ctx, cas_file=cfg.unsteady_cas_file, ncsize=ncsize,
solver=None, name="telemac-unsteady", duration=setup.duration)
if cfg.heat.enabled:
self._compare_temperatures(cfg, ctx)
return proc
@staticmethod
def _compare_temperatures(cfg, ctx: Any) -> None:
"""Compare a heat run with the loggers of the case; a failure is a warning, never a failed run."""
from axqua.solvers.telemac import heat_results
try:
found = heat_results.compare(cfg)
except Exception as exc: # noqa: BLE001 - the simulation itself is fine
log.warning("the simulated water temperature was not compared with the loggers: %s",
exc)
return
if found is not None and ctx is not None:
for name in ("scores.csv", "scores-reference.csv", "series.csv"):
ctx.record(f"Water temperature - {name}", found["folder"] / name, kind="table")
if found["figure"] is not None:
ctx.record("Water temperature - comparison with the loggers", found["figure"],
kind="file")
def _launch(self, cfg, *, ctx: Any, cas_file: str, ncsize: int | None,
solver: str | None, name: str, duration: float | None = None):
from axqua.env import TelemacRuntime
from axqua.workflow import run_solver_streaming
cas = cfg.model_path(cas_file)
if not cas.exists():
raise errors.ConfigError(
f"no built case at {cas}",
subject="model_dir",
remedy="Submit a preprocessing job first, or point the job at the case "
"workspace that holds the build.",
)
from axqua import bayescal
# A calibration that was cancelled leaves the last parameter set it tested in
# the friction table and the steering file of this case.
if bayescal.restore_as_built(cfg):
log.warning("restored the built case: a calibration had been interrupted "
"and had left its last tested parameter set in %s", cas.parent)
runtime = TelemacRuntime(cfg.telemac)
sink = _sink_for(ctx, name)
proc = run_solver_streaming(
runtime, cfg, cas_file=cas_file, solver=solver, ncsize=ncsize,
duration=duration,
# A detached job has no terminal to draw a bar on, and echoing the listing
# to a dead stdout is pure waste; the sink still receives every line.
show_progress=ctx is None,
sink=sink,
should_stop=ctx.should_stop if ctx is not None else None,
)
if ctx is not None:
ctx.check_cancelled()
if proc.returncode != 0:
from axqua.workflow import LIMIT_STOP_REMEDY
# set when TELEMAC-2D stopped itself on its limits, which it reports as a
# correct end with exit code 0 (workflow._fail_on_limit_stop)
stopped = getattr(proc, "stop_reason", "")
raise errors.SolverError(
stopped
or f"{solver or cfg.telemac.solver} exited with code {proc.returncode}",
subject=str(cas),
remedy=(LIMIT_STOP_REMEDY if stopped else
f"Read the listing next to {cas.parent} for the first error."),
component=solver or cfg.telemac.solver,
return_code=proc.returncode,
listing=str(cas.with_suffix(".sortie")),
)
return proc
@staticmethod
def _three_d_duration(cfg, cas_name: str) -> float | None:
"""Read TIME STEP x NUMBER OF TIME STEPS out of a 3D steering file.
TELEMAC-3D genuinely has no ``DURATION``, so the only honest span for a progress
bar is the product of the two keywords the file does carry.
"""
try:
text = cfg.model_path(cas_name).read_text(encoding="utf-8", errors="replace")
except OSError:
return None
step = steps = None
for line in text.splitlines():
if ":" not in line or line.lstrip().startswith("/"):
continue
key, _, value = line.partition(":")
key = key.strip().upper()
try:
if key == "TIME STEP":
step = float(value.strip())
elif key == "NUMBER OF TIME STEPS":
steps = int(float(value.strip()))
except ValueError:
continue
return step * steps if (step and steps) else None
# -- study --------------------------------------------------------------------
[docs]
def study(self, cfg, capability: Capability | None = None, *, ctx: Any = None,
**kwargs):
_refuse_in_hydrology_mode(cfg, capability)
if capability is Capability.MESH_CONVERGENCE:
return self._mesh_convergence(cfg, ctx=ctx, **kwargs)
if capability is Capability.VERTICAL_CONVERGENCE:
return self._vertical_convergence(cfg, ctx=ctx, **kwargs)
if capability is Capability.CALIBRATION:
return self._calibration(cfg, ctx=ctx, **kwargs)
raise errors.ConfigError(
f"telemac has no study for {capability}",
subject="kind",
)
def _mesh_convergence(self, cfg, *, ctx: Any = None, discharge: float | None = None,
refinement_ratio: float = 1.3, tolerance: float = 0.05,
n_coarser: int = 1, n_finer: int = 2,
max_extra_levels: int = 3, auto_extend: bool = False,
initial_depth: float | None = 0.5, ncsize: int | None = None,
answers: dict | None = None, **_ignored):
from axqua.convergence import ratio_levels, run_mesh_convergence
from axqua.workflow import prepare_steady_inputs
q = prepare_steady_inputs(cfg, discharge, prewet_depth=initial_depth)
levels = ratio_levels(cfg, ratio=refinement_ratio, n_coarser=n_coarser,
n_finer=n_finer)
base = Path(cfg.postprocessing_dir) / "mesh-convergence"
report = run_mesh_convergence(
cfg, discharge=q, levels=levels, tolerance=tolerance, base_dir=base,
n_processors=ncsize, extend_ratio=refinement_ratio,
max_extra_levels=max_extra_levels, auto_extend=auto_extend,
# Never the study's own stdin prompt: a detached job must answer from its
# own record, and every answer is logged (see jobs.interaction).
ask=(ctx.ask if ctx is not None else None),
on_level=_level_progress(ctx),
)
if ctx is not None:
ctx.progress(levels_done=len(report.levels), levels_total=len(report.levels),
converged=bool(getattr(report, "converged", False)))
_write_study_report(report, base, "mesh-convergence", ctx, readme=True)
return report
def _vertical_convergence(self, cfg, *, ctx: Any = None,
layer_counts: list[int] | None = None,
tolerance: float = 0.05, ncsize: int | None = None,
**_ignored):
from axqua.solvers.telemac.threed import three_d_case
from axqua.vertical_convergence import run_vertical_convergence
# the layers are studied on the horizontal mesh of the 3D model; the report
# stays with the case's other studies
base = Path(cfg.postprocessing_dir) / "vertical-convergence"
cfg = three_d_case(cfg)
kwargs: dict[str, Any] = {"tolerance": tolerance}
if layer_counts:
kwargs["counts"] = list(layer_counts)
if ncsize:
kwargs["n_processors"] = ncsize
report = run_vertical_convergence(cfg, base_dir=base,
on_level=_level_progress(ctx), **kwargs)
_write_study_report(report, base, "vertical-convergence", ctx)
return report
def _calibration(self, cfg, *, ctx: Any = None, flows: list | None = None,
launch_mode: str = "run", force: bool = False,
prepare_only: bool = False, resume: bool = False,
extend: bool = False,
calibration_quantities: list | None = None,
extraction_quantities: list | None = None,
vel_err_floor: float | None = None,
depth_err_floor: float | None = None,
validate: bool = False, situation: str = "",
compare_only: bool = False, ncsize: int | None = None,
heat: bool = False, sensitivity: bool = False,
design_only: bool = False, **_ignored):
from axqua import bayescal
if validate:
return self._validation(cfg, ctx=ctx, situation=situation,
compare_only=compare_only, ncsize=ncsize)
if heat:
from axqua.solvers.telemac import heat_calibration
def ensemble(c, cas, duration):
self._launch(c, ctx=ctx, cas_file=cas, ncsize=ncsize, solver=None,
name="telemac-heat-ensemble", duration=duration)
if sensitivity:
return heat_calibration.sensitivity(cfg, launch=ensemble)
with _calibration_progress(cfg, ctx, folder=heat_calibration.FOLDER):
code = heat_calibration.run(cfg, prepare_only=prepare_only, resume=resume,
extend=extend, design_only=design_only,
launch=ensemble)
elif flows:
specs = [bayescal.FlowSpec(**f) for f in flows]
code = bayescal.run_multiflow_calibration(cfg, specs,
launch_mode=launch_mode,
force=force)
else:
kwargs: dict[str, Any] = {"prepare_only": prepare_only, "resume": resume,
"extend": extend}
if calibration_quantities:
kwargs["calibration_quantities"] = tuple(calibration_quantities)
if extraction_quantities:
kwargs["extraction_quantities"] = tuple(extraction_quantities)
if vel_err_floor is not None:
kwargs["vel_err_floor"] = vel_err_floor
if depth_err_floor is not None:
kwargs["depth_err_floor"] = depth_err_floor
if bayescal.calibration_situations(cfg):
# measurements at several discharges (calibration.situations): one set
# of parameters against all of them, HydroBayesCal's multi-flow driver
with _calibration_progress(cfg, ctx, folder=bayescal.MULTIFLOW):
code = bayescal.run_situation_calibration(cfg, force=force, **kwargs)
else:
with _calibration_progress(cfg, ctx):
code = bayescal.run_single_flow_calibration(cfg, **kwargs)
if code != 0:
raise errors.SolverError(
f"the calibration driver exited with code {code}",
subject=str(cfg.calibration_dir),
remedy="Read the HydroBayesCal output in the calibration directory.",
component="hydrobayescal",
return_code=code,
)
if ctx is not None and not prepare_only and not flows and not heat:
_record_calibration_report(cfg, ctx)
return code
def _validation(self, cfg, *, ctx: Any = None, situation: str = "",
compare_only: bool = False, ncsize: int | None = None):
"""The calibrated model at another discharge, against what was measured then.
Runs in the model folder under names of its own, so neither the built case nor
the result of the steady run is touched.
"""
import numpy as np
from axqua import validation
from axqua.solvers.telemac import sortie
from axqua.solvers.telemac.flux_convergence import (net_imbalance, net_window,
relative_imbalance)
chosen = validation.situation_named(cfg, situation or None)
setup = None
balance = None
if not compare_only:
calibrated = validation.calibrated_parameters(cfg)
log.info("validation %s with the %s of %s", chosen.name,
calibrated.estimate, calibrated.source.parent.parent.parent.name)
setup = validation.build(cfg, chosen, calibrated.as_dict())
self._launch(cfg, ctx=ctx, cas_file=setup.cas.name, ncsize=ncsize,
solver=None, name="telemac2d",
duration=chosen.duration or validation.run_duration(setup))
listing = sortie.latest_sortie(cfg.model_dir, setup.cas.name)
if listing is not None:
history = sortie.read_sortie(listing)
measured = net_imbalance(history.time, history.gross_in,
history.gross_out)
if measured is None:
tail = relative_imbalance(history.gross_in, history.gross_out)[-10:]
measured = ((float(np.nanmean(tail)),) * 2) if tail.size else None
if measured is not None:
balance, swing = measured
window = net_window(history.time)
setup.notes.append(
"Discharge balance at the end of the validation run: over the "
f"last {window:.0f} s, the volume in the reach changed at a rate "
f"of {100 * balance:.3f} % of the inflow, and inflow and outflow "
f"differed by {100 * swing:.2f} % from one printout to the next "
"(unsteady flow).")
report = validation.evaluate(cfg, chosen, setup)
if balance is not None and balance > float(cfg.hydrodynamics.flux_tolerance):
report.notes.append(
"The validation run had not reached a steady state: over its last "
f"{window:.0f} s, the volume in the reach still changed at a rate of "
f"{100 * balance:.2f} % of the inflow. Increase the duration of the "
"validation situation.")
if ctx is not None:
folder = Path(cfg.calibration_dir) / validation.FOLDER
tag = validation.slug(chosen.name)
ctx.record(f"Validation {chosen.name}: model against measurement",
folder / f"validation-{tag}.csv", kind="table")
ctx.record(f"Validation {chosen.name}: summary",
folder / f"validation-{tag}.json", kind="file")
ctx.record(f"Validation {chosen.name}: figure",
folder / f"validation-{tag}.png", kind="figure")
if setup is not None:
ctx.record(f"Validation {chosen.name} (TELEMAC result)", setup.results,
kind="mesh", style=STYLE_DEPTH, variable="WATER DEPTH")
for note in report.notes + (setup.notes if setup else []):
log.info("validation %s: %s", chosen.name, note)
return report
# -- after the run ------------------------------------------------------------
[docs]
def postprocess(self, cfg, ctx: Any = None):
"""Flux convergence, wetting and section reports - what ``initial_run.py`` does.
Only for the kinds where those reports mean something: running a flux analysis
after a mesh-convergence study would read the last level's listing and report it
as if it were the case's own.
"""
from axqua.jobs.model import JobKind
kind = getattr(getattr(ctx, "spec", None), "kind", None)
if kind is JobKind.HYDROLOGY_RUN:
return self._hydrology_report(cfg, ctx)
if getattr(cfg, "hydrology_mode", False):
return None # a catchment has no steady flux balance to judge
if kind not in (JobKind.STEADY_RUN, JobKind.UNSTEADY_RUN, JobKind.PREPROCESSING):
return None
if not any(Path(cfg.model_dir).glob("*.sortie")):
return None
from axqua.solvers.telemac.flux_convergence import analyze_flux_convergence
fc = analyze_flux_convergence(cfg)
if ctx is not None:
ctx.progress(flux_imbalance=getattr(fc, "final_imbalance", None))
for name in ("extracted-fluxes.csv", "flux-convergence.png",
"convergence-rate.csv", "convergence-rate.png"):
ctx.record(name, cfg.model_path(name), kind="figure"
if name.endswith(".png") else "table")
self._wetting_reports(cfg, ctx)
return fc
@staticmethod
def _hydrology_report(cfg, ctx: Any):
"""Outlet hydrograph and water balance of a hydrology run."""
from axqua.hydrology import report
result = report.analyze(cfg)
if ctx is not None:
folder = Path(cfg.postprocessing_dir) / "hydrology"
ctx.record("Outlet hydrograph", folder / "hydrograph.png", kind="figure")
ctx.record("Outlet hydrograph (table)", folder / "hydrograph.csv",
kind="table")
ctx.record("Water balance", folder / "water-balance.csv", kind="table")
ctx.record("Volume through each liquid boundary",
folder / "boundary-volumes.csv", kind="table")
if (folder / "sediment-budget.csv").exists():
ctx.record("Sediment budget", folder / "sediment-budget.csv",
kind="table")
ctx.progress(flux_imbalance=result.relative_error)
return result
@staticmethod
def _wetting_reports(cfg, ctx: Any) -> None:
"""A balanced flux budget says nothing about *where* the water is.
So the wetted-extent report runs too - but it is genuinely optional, and a
failure here must not fail a job whose solver run succeeded.
"""
from axqua.workflow import report_wetting
try:
report_wetting(cfg)
except Exception as exc: # noqa: BLE001
log.debug("wetting report skipped: %s: %s", type(exc).__name__, exc)
return
if ctx is not None:
for name in ("wetting-report.csv", "outlet-profile.csv"):
ctx.record(name, cfg.model_path(name), kind="table")
_record_backwater(Path(cfg.model_dir), ctx)
[docs]
def extract_objective(self, cfg, ctx: Any = None) -> float | None:
"""The number this job was minimizing, when there is one.
A steady run's is the relative flux imbalance - the thing its own convergence
is judged on - which makes "how good was this run" answerable from the job
record without opening anything.
"""
summary = getattr(getattr(ctx, "manifest", None), "summary", {}) or {}
for key in ("flux_imbalance", "gci", "best_objective"):
if key in summary:
try:
return float(summary[key])
except (TypeError, ValueError):
return None
progress = getattr(getattr(ctx, "sink", None), "status", None)
imbalance = getattr(getattr(progress, "progress", None), "flux_imbalance", None)
return float(imbalance) if imbalance is not None else None
[docs]
def export_qgis_results(self, cfg, ctx: Any = None):
"""Describe what QGIS should open. **No conversion, and no PyQGIS.**
TELEMAC results are SELAFIN and QGIS reads SELAFIN natively through MDAL, so
converting them would cost a full copy of a multi-hundred-megabyte file to gain
nothing (plan §26). The export is therefore a *manifest*: paths plus a hint about
how to style each one.
"""
if ctx is None:
return []
written: list[Path] = []
from axqua.solvers.telemac import threed
# the 3D runs work in a folder of their own when the case gives them a mesh
# of their own (mesh.size_scale_3d)
c3 = threed.three_d_case(cfg, prepare=False)
for entry in result_layers(cfg, c3):
path = Path(entry["path"])
ctx.record(entry["name"], path, kind=entry["kind"], style=entry["style"],
variable=entry["variable"], **entry["extra"])
if path not in written:
written.append(path)
# The 3D results themselves are volumes. QGIS reads SELAFIN in 2D only, so they
# are listed as files and not as layers: offered as layers, each *Load results*
# ended with "r3d.slf did not load".
for name, label in ((getattr(cfg, "results3d_slf", None), "TELEMAC-3D result"),
(threed.HYDROSTATIC_RESULT_3D,
"TELEMAC-3D result, hydrostatic")):
if name:
ctx.record(f"{label} (3D file, not a map layer)", c3.model_path(name),
kind="file")
hydrology = cfg.model_path(getattr(cfg, "results_hydrology_slf", "r2d-hydrology.slf"))
if hydrology.exists():
ctx.record("TELEMAC result (hydrology) - infiltrated depth", hydrology,
kind="mesh", variable="INFILTRATED")
# TELEMAC's maxima over the run (MAXZ, MAXV in the printouts)
ctx.record("TELEMAC result (hydrology) - high water mark", hydrology,
kind="mesh", style=STYLE_HIGH_WATER, variable="HIGH WATER MARK")
ctx.record("TELEMAC result (hydrology) - highest velocity", hydrology,
kind="mesh", style=STYLE_VELOCITY, variable="HIGHEST VELOCITY")
# the suspended sediment of GAIA, which the hydrology run prints as tracers
m = getattr(cfg, "morphodynamics", None)
if m is not None:
for name in m.tracer_names:
ctx.record(f"TELEMAC result (hydrology) - {name.lower()} [g/l]",
hydrology, kind="mesh", variable=name)
warm = getattr(getattr(cfg, "heat", None), "enabled", False)
unsteady = cfg.model_path(cfg.results_unsteady_slf) if warm else None
if warm and unsteady.exists():
from axqua.core import selafin
from axqua.solvers.telemac import heat
labels = {"TEMPERATURE": "water temperature", "BED TEMPERATURE": "bed temperature",
**{name: f"{what} water temperature"
for name, what in heat.STATISTICS.items()}}
present = {str(name).strip() for name in selafin.SelafinFile(unsteady).var_names}
for name, label in labels.items():
if name in present:
ctx.record(f"TELEMAC result (unsteady) - {label} [deg C]", unsteady,
kind="mesh", variable=name)
# the bed of GAIA: how much it rose or fell, and the shear stress that moved it
m = getattr(cfg, "morphodynamics", None)
gaia = cfg.model_path(f"{m.gaia_results_base}.slf") if m is not None else None
if m is not None and m.enabled and gaia.exists():
ctx.record("GAIA result - bed change [m]", gaia, kind="mesh",
variable="CUMUL BED EVOL")
ctx.record("GAIA result - bed shear stress [N/m2]", gaia, kind="mesh",
variable="BED SHEAR STRESS")
ctx.record("Mesh geometry", cfg.model_path(cfg.geometry_slf), kind="mesh")
for name, kind in (("mesh-convergence.xlsx", "table"),
("vertical-convergence.xlsx", "table")):
for found in Path(cfg.postprocessing_dir).rglob(name):
ctx.record(found.name, found, kind=kind)
csv = Path(cfg.calibration_dir) / cfg.calibration_csv
ctx.record("Calibration points", csv, kind="table")
return written
BACKEND = TelemacBackend()
def _level_progress(ctx: Any):
"""What a convergence study calls after each of its runs; ``None`` without a job."""
from axqua.convergence import level_progress
return level_progress(ctx)
def _write_study_report(report, folder: Path, stem: str, ctx: Any, *,
readme: bool = False) -> None:
"""Write the report of a convergence study next to its levels, and record it
(:func:`axqua.convergence.write_study_report`, shared with OpenFOAM)."""
from axqua.convergence import write_study_report
write_study_report(report, folder, stem, ctx, readme=readme)
def _record_backwater(folder: Path, ctx) -> None:
"""Put the backwater reach (:mod:`axqua.solvers.telemac.backwater`) of *folder* into
the manifest, when the check wrote one."""
from axqua.solvers.telemac.backwater import FILE_STEM
figure = Path(folder) / f"{FILE_STEM}.png"
if figure.exists():
ctx.record("Backwater reach of the outflow level", figure, kind="figure")
ctx.record("Backwater reach (table)", figure.with_suffix(".csv"), kind="table")
def _record_calibration_report(cfg, ctx) -> None:
"""Put the calibration report (:mod:`axqua.calibration_report`) into the manifest."""
from axqua import bayescal
from axqua.calibration_report import FOLDER
root = Path(cfg.calibration_dir)
folder = (root / bayescal.MULTIFLOW if bayescal.calibration_situations(cfg)
else root) / FOLDER
_record_backwater(folder.parent, ctx)
ctx.record("Calibration: posterior of each parameter", folder / "posteriors.png",
kind="figure")
ctx.record("Calibration: roughness zones and their ks", folder / "roughness-zones.png",
kind="figure")
ctx.record("Calibration: evidence and relative entropy", folder / "evidence.png",
kind="figure")
ctx.record("Calibration: summary", folder / "calibration-report.json", kind="file")
ctx.record("Calibrated roughness zones", folder / "roughness-zones-calibrated.gpkg",
kind="vector", style="graduated", variable="ks_cm", label_field="label")
def _calibration_progress(cfg, ctx: Any, *, folder: str = ""):
"""Follow the HydroBayesCal log of a calibration and report its model runs.
Without a job context there is nobody to report to, and the driver's own output
is on the terminal anyway. The multi-flow driver works in its own *folder* (its
log is in the results folder below it, :func:`axqua.hbc.driver_logs`) and runs
each parameter set once per situation; the progress then names the situation at
work, its place among the situations and the stage (initial design or active
learning). The total is the ``max_runs`` the calibration recorded when it started
(:func:`axqua.bayescal.record_run_budget`); without a limit, the initial runs while
the initial design runs, and none during the active learning.
"""
from contextlib import nullcontext
if ctx is None:
return nullcontext()
import json
from axqua import bayescal, hbc
here = Path(cfg.calibration_dir) / folder
names: dict[str, str] = {}
if folder:
try:
from axqua.validation import slug
names = {slug(s.name): s.name for s in bayescal.calibration_situations(cfg)}
except Exception: # noqa: BLE001 - progress must not stop a run
names = {}
order = list(names)
def total(stage: str | None) -> int | None:
"""The planned runs: max_runs, or without a limit the initial design while it runs.
0 once the active learning of a calibration without a limit runs: the status
keeps a field that is reported as None, and the 64 of the initial design would
stay; the job list and ``axqua status`` show no total for 0.
"""
try:
budget = json.loads((here / bayescal.RUN_BUDGET).read_text())
except (OSError, ValueError):
return int(cfg.calibration.max_runs or 0) or None
if budget.get("max_runs") is not None:
return int(budget["max_runs"])
if stage == hbc.STAGE_DESIGN and budget.get("init_runs") is not None:
return int(budget["init_runs"])
return 0
def report(run: int, *, flow: str | None = None, stage: str | None = None) -> None:
fields = {"iteration": run, "max_iterations": total(stage), "stage": stage}
if flow is not None:
fields.update(flow=names.get(flow, flow), n_flows=len(order) or None,
flow_index=order.index(flow) + 1 if flow in order else None)
ctx.progress(**{key: value for key, value in fields.items() if value is not None})
return hbc.RunCounter(hbc.driver_logs(here), report)
def _sink_for(ctx: Any, name: str):
"""Tee the solver's own listing into its own rotating file, plus the job sink."""
if ctx is None:
return None
from axqua.jobs.events import TeeSink, TextLogSink
return TeeSink(ctx.sink, TextLogSink(ctx.solver_sink(name)))