"""What a form needs to know about every setting of a case file.
``axqua schema --json`` prints this table, and the case editor of the QGIS plugin builds
its forms from it. The plugin cannot import aXqua, so it cannot ask the dataclasses in
:mod:`axqua.config` itself, and a second, hand-written list of settings in the plugin
would be out of date with the first new field.
The table is **derived**, so it cannot miss a setting: the blocks and keys are those of
a completely written default case (``config_to_dict``), the kind of each value comes
from the annotation of its dataclass field, and its explanation is the comment that
stands with the field in the source. On top of that, :data:`CURATED` gives the settings
a modeler meets first a plain label, a unit and, for a file, what kind of file it is.
Those are the **essential** settings, which a form shows at once. Every other setting is
complete but stays out of the way until it is asked for.
**Pages, frames and what is required** (the user's corrections of 2026-10-16). The
editor shows the settings on :data:`PAGES`: one page per subject, each with one or more
frames (a block, or the part of a block that belongs to the subject: the lattice of the
OpenFOAM model is set on the page *Mesh configuration*). :data:`REQUIRED` names what a
minimal case cannot do without, and :data:`REQUIRED_WHEN` what an option makes
necessary, as conditions the editor evaluates while the form is filled in: the editor
marks both in the purple of the aXqua icon. :data:`SWITCHES` are the pages whose block
exists only when the case asks for it (simulating with OpenFOAM, calibrating).
"""
from __future__ import annotations
import dataclasses
import inspect
import re
import tempfile
import types
import typing
from pathlib import Path
from typing import Any
from axqua.core.boundaries import HYDROLOGY_OUTLETS, RIVER_OUTLETS, outlet_level
__all__ = ["BLOCKS", "CURATED", "KINDS", "PAGES", "REQUIRED", "REQUIRED_WHEN", "SWITCHES",
"block_classes", "build", "field_kinds", "pages"]
#: What a value can be, as far as a form is concerned. ``multichoice`` is a list of
#: entries out of a fixed set, which a form shows as check boxes.
KINDS = ("text", "int", "number", "bool", "choice", "multichoice", "file", "folder",
"vector", "raster", "table", "yaml")
#: The blocks of a case file in the order of the workflow: ``(key, title, docs label)``.
#: The label is the section of the page *aXqua case setup* that explains the block.
BLOCKS: tuple[tuple[str, str, str], ...] = (
("project", "Project settings", "case-project"),
("geodata", "Geodata", "case-geodata"),
("boundaries", "Liquid boundaries", "case-boundaries"),
("gain_lose", "Exchange with a porous body", "case-boundaries"),
("initialization", "Initial conditions", "case-initialization"),
("drying", "Drying of thin films", "case-initialization"),
("mesh", "Mesh configuration", "case-mesh"),
("friction", "Wall roughness", "case-roughness"),
("hydrodynamics", "Hydraulic simulation", "case-hydraulics"),
("hydrology", "Hydrology (rainfall-runoff)", "case-hydrology"),
("morphodynamics", "Morphodynamic simulation", "case-morphodynamics"),
("fine_sediment", "Fine sediment over a fixed bed", "case-morphodynamics"),
("dem_of_difference", "DEM of difference", "case-morphodynamics"),
("heat", "Water temperature", "case-hydraulics"),
("telemac", "TELEMAC", "case-hydraulics"),
("openfoam", "OpenFOAM", "case-hydraulics"),
("structures", "Structures", "case-geodata"),
("surfaces", "Surfaces from CAD", "case-surfaces"),
("ground_truth", "Measurements", "help-calibration"),
("calibration", "Calibration and validation", "help-calibration"),
("postproc", "Post-processing", "help-postprocessing"),
)
#: Why the drying of thin films matters, shown as the information of its frame.
DRYING_INFO = (
"On a coarse bed, water thinner than the grain roughness does not flow over the "
"bed: it stands within the grains and in nature drains into the ground. A 2D "
"model has no ground below the bed, so such a film stays where it is and makes the "
"wetted area look much larger than it is. On the Isar case, water shallower than "
"1 cm covered 13 % of the wetted area and held 0.16 % of the water. Removing thin "
"films takes away water that is both shallower than the film depth and slower than "
"the film velocity, and returns it where the porous body gives water back, so that "
"no water is lost. It needs the exchange with a porous body (page Liquid "
"boundaries).")
#: How the lattice of the OpenFOAM model follows the mesh of the case.
OPENFOAM_MESH_INFO = (
"The OpenFOAM model is built on a lattice of columns. With a factor, its cell size "
"follows the edge length in the channel above: 3 makes the lattice three times "
"coarser than the TELEMAC mesh, so a change of the TELEMAC mesh carries over. "
"Without a factor, the cell size is used as it is.")
#: What the frames of the Hydrology page explain.
RAIN_INFO = (
"Rain falls on every node of the catchment, one series for all of it. Ask the "
"national meteorological agency first: its gauges and radar resolve the minutes and "
"kilometers of a storm, and its design rainfall is what practice expects. The data "
"wizard on the tab Simulations > Hydrology imports a record, builds a design storm "
"or downloads a past event from a reanalysis, which underestimates short "
"intensities.")
INFILTRATION_INFO = (
"Green-Ampt as revised by de Vos et al. (2024): before ponding all rain "
"infiltrates; once the soil cannot take it, the capacity follows the explicit "
"solution of Sadeghi et al. (2024), and ponded water keeps infiltrating after the "
"rain. A soil is its conductivity Ks, its suction head and its water contents at "
"saturation and at the start; a texture class gives all but the last from Rawls et "
"al. (1983). The water content at the start decides most of the runoff of a storm.")
WEATHER_INFO = (
"Wind acts on water deeper than its threshold depth, so on ponds and lakes, not on "
"the sheet flow of a hillslope. Evaporation is taken from the surface water, at the "
"FAO-56 reference evapotranspiration times the factor of open water; the soil gives "
"no water back in this model. Both matter little during a storm and more in the "
"hours after it.")
OUTLET_INFO = (
"The outline is the drainage divide and so a wall; water leaves through the outflow "
"lines of the page Liquid boundaries. normal_depth holds the level at each outflow "
"line that its section needs for the discharge (Manning's normal flow, or a "
"measured rating curve). free prescribes nothing and holds only where the flow "
"leaves supercritical. The level of normal_depth depends on the roughness of the "
"outlet: 20 % on Manning's n is about 12 % on the depth, fading upstream over about "
"depth / slope, so put the outlet a little downstream of where results are read. "
"A field, a plot or a terrace is no catchment: where the terrain falls away beyond "
"its edge, overfall lets the water leave as over a brink, and none comes back.")
#: What the frames of the Water temperature page explain.
HEAT_INFO = (
"The unsteady simulation carries the water temperature from the inflow lines "
"through the reach and exchanges heat with the sun, the sky and the air. The "
"hydrograph of the page Liquid boundaries then needs dates and a temperature column "
"for every inflow line. The weather table needs the air temperature, the humidity "
"and the global radiation of a station near the reach.")
SHADE_INFO = (
"Trees, valley sides and buildings keep the direct sun from the water, and part of "
"the sky as well. On the Sarine, the shade reduced the error of a thermal model by "
"about a fifth (Dorthe et al. 2025). It is computed from a surface model, whose "
"trees are opaque and the same in every season.")
BED_HEAT_INFO = (
"The bed takes up heat by day and returns it at night, which dampens the daily "
"variation of shallow water. It is one layer of sediment that exchanges heat by "
"conduction. Its thickness and diffusivity stand for pore water flow as well, so "
"they are calibration parameters rather than material constants. Water that an "
"exchange with a porous body returns has a constant temperature, or the mean river "
"temperature of the last hours, which also holds in another season.")
HEAT_CALIBRATION_INFO = (
"The temperature is calibrated after the flow, with the mean, the highest and the "
"lowest temperature of every logger over a window of the unsteady simulation. One "
"run carries many parameter sets, because the temperature does not act on the flow. "
"Run the sensitivity first: a parameter whose whole range moves the temperatures by "
"less than the error of the loggers cannot be calibrated with them.")
#: The pages of the editor, in the order of the workflow:
#: ``(key, title, docs label, ((block or block/group, frame title, information), ...))``.
#: ``openfoam/mesh`` is the group *mesh* of the block ``openfoam``, which belongs on
#: the page of the mesh; the rest of the block stays on its own page.
PAGES: tuple[tuple[str, str, str, tuple[tuple[str, str, str], ...]], ...] = (
("project", "Project settings", "case-project", (("project", "", ""),)),
("geodata", "Geodata", "case-geodata", (("geodata", "", ""),)),
("boundaries", "Liquid boundaries", "case-boundaries", (
("boundaries", "", ""), ("gain_lose", "Exchange with a porous body", ""))),
("initialization", "Initial conditions", "case-initialization", (
("initialization", "", ""), ("drying", "Drying of thin films", DRYING_INFO))),
("mesh", "Mesh configuration", "case-mesh", (
("mesh", "TELEMAC mesh", ""),
("openfoam/mesh", "OpenFOAM lattice", OPENFOAM_MESH_INFO))),
("friction", "Wall roughness", "case-roughness", (("friction", "", ""),)),
("hydrodynamics", "Hydraulic simulation", "case-hydraulics", (
("hydrodynamics", "", ""),
("heat", "Water temperature", HEAT_INFO),
("heat/shade", "Water temperature: shade", SHADE_INFO),
("heat/bed and groundwater", "Water temperature: bed and groundwater",
BED_HEAT_INFO),
("heat/measurements and calibration",
"Water temperature: measurements and calibration", HEAT_CALIBRATION_INFO))),
("hydrology", "Hydrology", "case-hydrology", (
("hydrology/rain", "Rain", RAIN_INFO),
("hydrology/infiltration", "Infiltration (Green-Ampt)", INFILTRATION_INFO),
("hydrology/wind and sun", "Wind and sun", WEATHER_INFO),
("hydrology/outlet and run", "Outlet and run", OUTLET_INFO))),
("morphodynamics", "Morphodynamic simulation", "case-morphodynamics", (
("morphodynamics", "Sediment transport with TELEMAC and GAIA", ""),
("fine_sediment", "Fine sediment over a fixed bed with OpenFOAM", ""),
("dem_of_difference", "DEM of difference", ""))),
("telemac", "TELEMAC", "case-hydraulics", (("telemac", "", ""),)),
("openfoam", "OpenFOAM", "case-hydraulics", (
("openfoam", "", ""), ("structures", "Structures", ""),
("surfaces", "Surfaces from CAD", ""))),
("calibration", "Calibration and validation", "help-calibration", (
("ground_truth", "Measurements", ""),
("calibration", "Calibration and validation", ""))),
("postproc", "Post-processing", "help-postprocessing", (("postproc", "", ""),)),
)
#: Pages whose block exists only when the case asks for it: a switch at the top of the
#: page, and the entry written when it is switched on (an empty block is not written).
SWITCHES: dict[str, dict[str, Any]] = {
"telemac": {"label": "Simulate with TELEMAC", "seed": {"solver": "telemac2d"}},
"openfoam": {"label": "Simulate with OpenFOAM", "seed": {"mode": "vof"}},
"calibration": {"label": "Calibrate and validate this case",
"seed": {"calibration_quantities": ["WATER DEPTH",
"SCALAR VELOCITY"]}},
"hydrology": {"label": "Hydrology mode: rainfall-runoff over a catchment "
"(TELEMAC only; no channel mesh, no centerline)",
"seed": {"enabled": True}},
}
#: Frames that show the switch of a page of another block at their top, as one more
#: check box for the same switch: ``(page key, frame title) -> block``. The hydrology
#: mode changes how the mesh is built, so it is switched where the mesh is set up too.
FRAME_SWITCHES: dict[tuple[str, str], str] = {
("mesh", "TELEMAC mesh"): "hydrology",
}
#: A short line a frame shows above its settings, for what a user has to know before
#: entering a value there (``(page, frame title)``; the information of a frame, by
#: contrast, opens on request).
FRAME_NOTES: dict[tuple[str, str], str] = {
# geodata.Dataset.mesh_zones: the field of the layer wins, these are fallbacks
("mesh", "TELEMAC mesh"): (
"The edge length of a mesh zone in its GeoPackage layer (field Max Edge Length "
"(m)) wins; the edge lengths here apply only to zones without one. The factor "
"on all edge lengths applies to both."),
}
#: Settings a switch stands for, which get no row of their own.
HIDDEN = frozenset({"hydrology.enabled"})
#: The entries a minimal case cannot do without.
REQUIRED = frozenset({
"project.name", "project.crs_epsg", "geodata.dem_initial", "geodata.boundary",
"boundaries.liquid_boundaries", "boundaries.prescribed_flowrate",
})
#: The outlets of a hydrology case that hold the level ``boundaries.prescribed_elevation`` (a lake, a reservoir), and those that do not.
_FIXED_LEVEL = [c for c in HYDROLOGY_OUTLETS if outlet_level(c) == "fixed"]
_OTHER_LEVEL = [c for c in HYDROLOGY_OUTLETS if outlet_level(c) != "fixed"]
#: The outlet of a hydrology case is a level (a lake, a reservoir).
_HYDROLOGY_LEVEL = {"all": [{"block": "hydrology"},
{"key": "hydrology.outlet_condition", "in": _FIXED_LEVEL}]}
#: Entries that are not needed while a condition holds: ``key -> conditions`` (of which
#: one suffices), in the form of :data:`REQUIRED_WHEN`. A hydrology case is driven by
#: its rain, so it needs no discharge, and a closed catchment no outflow line; it
#: needs an outflow level only when its own outlet is one.
#: The case was imported from a TELEMAC steering file (``project.imported_from``), for
#: post-processing only: it has no geodata and no boundary values of its own.
_IMPORTED = {"key": "project.imported_from", "set": True}
REQUIRED_UNLESS: dict[str, list[dict[str, Any]]] = {
"boundaries.prescribed_flowrate": [{"block": "hydrology"}, _IMPORTED],
"boundaries.liquid_boundaries": [{"block": "hydrology"}, _IMPORTED],
"geodata.dem_initial": [_IMPORTED],
"geodata.boundary": [_IMPORTED],
"boundaries.prescribed_elevation": [
{"all": [{"block": "hydrology"},
{"key": "hydrology.outlet_condition", "in": _OTHER_LEVEL}]}],
}
#: The water temperature is switched off.
_HEAT_OFF = {"key": "heat.enabled", "in": [False]}
#: The settings of the water temperature that only its switch governs.
_HEAT_SETTINGS = (
"weather", "weather_utc_offset", "inflow_utc_offset", "wind_height", "albedo",
"emissivity", "wind_factor", "min_depth", "surface_model", "shade_distance",
"shade_resolution", "shade_sectors", "bed_exchange", "groundwater_from_river",
"initial_temperature", "forcing_step", "statistics_from", "observations",
"observation_points", "observations_utc_offset", "observation_error", "model_error",
"prior_samples", "calibrate", "sets_per_run")
#: Entries that have no effect while a condition holds, which a form grays out:
#: ``key -> conditions`` as in :data:`REQUIRED_WHEN`. A catchment mesh has no channel,
#: and calibration runs that do not start from the steady result take the case's time.
DISABLED_WHEN: dict[str, list[dict[str, Any]]] = {
**{key: [{"block": "hydrology"}] for key in (
"geodata.channel_centerline", "mesh.channel_anisotropy",
"initialization.prewet_depth")},
"calibration.hotstart_duration": [{"key": "calibration.hotstart", "in": [False]}],
# the water temperature: nothing of it acts while it is off, the layer of the bed
# only with its exchange, and the returning water has a constant temperature or
# the river's (the hours of whose mean then count)
**{f"heat.{name}": [_HEAT_OFF] for name in _HEAT_SETTINGS},
**{f"heat.{name}": [_HEAT_OFF, {"key": "heat.bed_exchange", "in": [False]}]
for name in ("bed_thickness", "bed_density", "bed_heat_capacity", "bed_diffusivity",
"bed_deep_temperature", "bed_initial_temperature")},
"heat.groundwater_temperature": [_HEAT_OFF,
{"key": "heat.groundwater_from_river", "in": [True]}],
"heat.groundwater_window": [_HEAT_OFF,
{"key": "heat.groundwater_from_river", "in": [False]}],
}
#: Entries an option makes necessary: ``key -> conditions``, of which one suffices. A
#: condition is ``{"key": ..., "in": [values]}`` (an unset entry counts with its
#: default), ``{"key": ..., "set": true}`` (the entry has a value, also one without a
#: row of its own, such as ``project.imported_from``), ``{"block": ...}`` (the block is
#: switched on) or ``{"all": [conditions]}`` (every one of them holds). A plugin older
#: than the ``set`` form reads it as not holding, which keeps a requirement. A plugin older than the ``all`` form reads it as not
#: holding: in this table that drops a requirement, in :data:`REQUIRED_UNLESS` it keeps
#: one. No released plugin is affected: 1.0.2 and earlier read no ``required_unless``
#: at all, and the plugin that brought it brought ``all`` as well.
REQUIRED_WHEN: dict[str, list[dict[str, Any]]] = {
"boundaries.prescribed_elevation": [
{"key": "boundaries.outflow_condition",
"in": [c for c in RIVER_OUTLETS if outlet_level(c) == "fixed"]},
_HYDROLOGY_LEVEL],
"boundaries.inflow": [{"key": "hydrodynamics.regime", "in": ["unsteady"]}],
"gain_lose.zone": [{"key": "gain_lose.enabled", "in": [True]}],
"geodata.dem_target": [{"key": "dem_of_difference.enabled", "in": [True]}],
"morphodynamics.sediment_classes": [{"key": "morphodynamics.enabled", "in": [True]}],
"fine_sediment.grain_size": [{"key": "fine_sediment.enabled", "in": [True]}],
"fine_sediment.inflow_concentration": [{"key": "fine_sediment.enabled",
"in": [True]}],
"openfoam.mode": [{"block": "openfoam"}],
"openfoam.end_time": [{"block": "openfoam"}],
"hydrology.rainfall": [{"block": "hydrology"}],
"heat.weather": [{"key": "heat.enabled", "in": [True]}],
"hydrology.weather": [{"key": "hydrology.wind", "in": [True]},
{"key": "hydrology.evaporation",
"in": ["et0", "penman", "series"]}],
"ground_truth.sources": [{"block": "calibration"}],
"calibration.parameters": [{"block": "calibration"}],
"calibration.calibration_quantities": [{"block": "calibration"}],
}
def _c(label: str, kind: str = "", unit: str = "", choices: tuple = (),
help: str = "", advanced: bool = False) -> dict[str, Any]: # noqa: A002 - the column's name
"""A curated setting. *advanced* gives it a label and a unit without putting it on the form of every case: it is offered under *Add setting...* of its frame."""
return {"label": label, "kind": kind, "unit": unit, "choices": choices, "help": help,
"advanced": advanced}
#: The settings a form shows first, with what a modeler calls them.
CURATED: dict[str, dict[str, Any]] = {
"project.name": _c("Name of the case", help="Used in titles and file headers."),
"project.crs_epsg": _c("Coordinate reference system (EPSG code)", "int",
help="A projected system with coordinates in meters, for "
"example 25832 for ETRS89 / UTM zone 32N."),
"project.sim_dir": _c("Folder for results", "folder",
help="Everything aXqua produces is written into this folder. "
"A path relative to the case file is recommended."),
"geodata.dem_initial": _c("Terrain model", "raster",
help="Digital elevation model with the river bed."),
"geodata.boundary": _c("Model outline", "vector",
help="One polygon around the area to be simulated."),
"geodata.mesh_zones": _c("Mesh zones", "vector",
help="Polygons named channel, floodplain or refinement, "
"each with its edge length."),
"geodata.channel_centerline": _c("Channel centerline", "vector",
help="Line along the channel. Mesh cells in the "
"channel are stretched along it."),
"geodata.roughness_zones": _c("Roughness zones", "vector",
help="Polygons with a whole-number Zone ID."),
"geodata.roughness_table": _c("Roughness table", "table",
help="Table with the columns zone_id and ks."),
"geodata.dem_target": _c("Second terrain model", "raster",
help="A later survey, for the bed change between the two."),
"geodata.structures": _c("Structures", "vector",
help="Dams, weirs, walls and buildings with their crest "
"level or height."),
"geodata.control_sections": _c("Control sections", "vector",
help="Lines across which the discharge is reported."),
"boundaries.liquid_boundaries": _c("Inflow and outflow lines", "vector",
help="Lines on the model outline with the type "
"inflow or outflow."),
"boundaries.prescribed_flowrate": _c("Discharge", "number", "m3/s",
help="Total inflow of the steady simulation."),
"boundaries.outflow_condition": _c(
"Water level at the outflow", "choice",
choices=RIVER_OUTLETS,
help="elevation: the fixed level entered below. stage_discharge: from a rating "
"curve at the simulated discharge. free: nothing is prescribed."),
"boundaries.prescribed_elevation": _c("Outflow water level", "number", "m a.s.l."),
"boundaries.stage_discharge": _c("Rating curve", "table",
help="Table of discharge and water level at the "
"outflow. Computed from the terrain if empty."),
"boundaries.rating_method": _c(
"Rating curve from the terrain", "choice", choices=("trapezoid", "section"),
help="section uses the real cross section at the outflow line and is the "
"better choice for a natural channel."),
"boundaries.inflow": _c("Hydrograph", "table",
help="Discharge over time. Only needed for an unsteady "
"simulation."),
"hydrodynamics.wet_depth": _c(
"Minimum water depth", "number", unit="m",
help="Water shallower than this is not treated as surface flow: on a bed "
"with a Nikuradse roughness of 0.05-0.5 m, water 5 mm deep stands "
"inside the grain roughness rather than flowing over it. The reports "
"of aXqua and the map styles of the plugin both use this value, so "
"the picture and the report agree. Empty: 0.01 m, and 0.001 m in "
"hydrology mode, whose sheet flow is millimeters deep."),
"postproc.velocity_scale_max": _c(
"Velocity scale maximum", "number", unit="m/s",
help="Upper end of the velocity color scale in every figure of this case. "
"Above about 5 m/s a depth-averaged river result is usually a "
"wetting/drying artifact in a nearly dry cell, and one such node sets "
"the scale and flattens the whole map. A chute or a fishway runs "
"faster and wants a higher value."),
"initialization.prewet_depth": _c(
"Water depth at the start", "number", "m",
help="Starts the simulation with water in the channel, which saves the time "
"the flow needs to fill a dry bed. Leave empty for a dry start."),
"mesh.channel_size": _c("Edge length in the channel", "number", "m"),
"mesh.floodplain_size": _c("Edge length on the floodplain", "number", "m"),
"mesh.refinement_size": _c("Edge length in refinement zones", "number", "m"),
"mesh.size_scale": _c("Factor on all edge lengths", "number", "-",
help="2 doubles every edge length, which gives a mesh with "
"about a quarter of the cells. Useful for a first run."),
"mesh.size_scale_3d": _c("Factor on all edge lengths in 3D", "number", "-",
help="The horizontal mesh of the TELEMAC-3D runs. Empty: "
"the mesh of the 2D model. A 3D run costs much more "
"per cell, so a coarser mesh than in 2D is often the "
"affordable choice."),
"mesh.channel_anisotropy": _c("Length to width of channel cells", "number", "-"),
"friction.roughness_law": _c(
"Roughness law", "int",
help="TELEMAC number of the law: 5 is Nikuradse (ks in meters), 3 Strickler, "
"4 Manning."),
"friction.boundary_law": _c("Roughness law at closed boundaries", "int"),
"friction.boundary_coefficient": _c("Roughness at closed boundaries", "number"),
"hydrodynamics.duration": _c("Simulated time", "number", "s"),
"hydrodynamics.turbulence_model": _c(
"Turbulence model", help="auto selects the model that suits the cell size."),
"hydrodynamics.desired_courant": _c("Courant number", "number", "-"),
"hydrodynamics.flux_tolerance": _c(
"Tolerance of the discharge balance", "number", "-",
help="The simulation is steady when inflow and outflow differ by less than "
"this share."),
"telemac.solver": _c("Solver", "choice", choices=("telemac2d", "telemac3d")),
"telemac.n_processors": _c("Processor cores", "int"),
"gain_lose.enabled": _c("Model the exchange", "bool"),
"gain_lose.zone": _c("Porous body", "vector",
help="Polygon of the gravel bar the water passes through."),
"gain_lose.faces": _c("Where the exchange happens", "choice",
choices=("water-table", "lines")),
"morphodynamics.enabled": _c("Simulate sediment transport", "bool"),
"dem_of_difference.enabled": _c("Compute the bed change", "bool"),
"fine_sediment.enabled": _c("Simulate the fine sediment", "bool"),
"fine_sediment.grain_size": _c("Grain size", "number", "m"),
"fine_sediment.fall_velocity": _c(
"Settling velocity", "number", "m/s",
help="Leave empty to have it computed from the grain size."),
"fine_sediment.inflow_concentration": _c(
"Concentration at the inflow", "text", "kg/m3",
help="One number for all inflow lines, or one per line, such as "
"{inlet-1: 0.02, inlet-2: 0}. 20 mg/l are 0.02 kg/m3."),
"fine_sediment.pick_up": _c(
"Pick-up from the bed", "choice", choices=("suspended", "bedload", "off"),
help="suspended and bedload assume a bed that consists of the fine sediment. "
"off: the bed only takes sediment up."),
"fine_sediment.iterations": _c("Iterations at most", "int"),
"fine_sediment.period": _c(
"Period", "number", "s",
help="Time over which the computed rates act. Leave empty to get rates only."),
"fine_sediment.layer_thickness": _c("Thickness of the bed layer", "number", "m"),
"fine_sediment.porosity": _c("Pore share of the bed layer", "number", "-"),
"fine_sediment.initial_fraction": _c("Share of fines at the start", "number", "-"),
"heat.enabled": _c("Simulate the water temperature in the unsteady run", "bool"),
"heat.weather": _c(
"Weather series", "table",
help="Time, air temperature, humidity and global radiation; wind, air pressure "
"and cloud cover where the table has them. The columns of MeteoSwiss and "
"the DWD are recognized."),
"heat.weather_utc_offset": _c(
"Clock of the weather table: offset from UTC", "number", "h",
help="0 for MeteoSwiss and the DWD, 1 for Central European Time. A time stamp with "
"a zone of its own (+01:00) is read with that zone."),
"heat.inflow_utc_offset": _c(
"Clock of the hydrograph: offset from UTC", "number", "h",
help="The clock of the dates in boundaries.inflow. A time stamp with a zone of its "
"own (+01:00) is read with that zone."),
"heat.wind_height": _c("Height of the wind measurement", "number", "m", advanced=True),
"heat.albedo": _c("Albedo of the water surface", "number", "-", advanced=True),
"heat.emissivity": _c("Emissivity of the water surface", "number", "-", advanced=True),
"heat.min_depth": _c("Depth below which water exchanges heat as if this deep",
"number", "m", advanced=True),
"heat.forcing_step": _c("Time step of the forcing", "number", "s", advanced=True),
"heat.surface_model": _c(
"Surface model", "raster",
help="Terrain with vegetation and buildings, for the shade. Empty: no shade."),
"heat.shade_distance": _c("Distance from which something can shade", "number", "m",
advanced=True),
"heat.shade_resolution": _c("Cell size the surface model is read at", "number", "m",
advanced=True),
"heat.shade_sectors": _c("Directions of the horizon", "int", advanced=True),
"heat.bed_density": _c("Bulk density of the sediment layer", "number", "kg/m3",
advanced=True),
"heat.bed_heat_capacity": _c("Specific heat of the sediment layer", "number", "J/kg/K",
advanced=True),
"heat.bed_deep_temperature": _c("Temperature below the sediment layer", "number",
"deg C", advanced=True),
"heat.bed_initial_temperature": _c("Temperature of the sediment layer at the start",
"number", "deg C", advanced=True),
"heat.bed_exchange": _c("Exchange heat with the bed", "bool"),
"heat.wind_factor": _c("Factor on the wind function", "number", "-"),
"heat.bed_thickness": _c("Thickness of the sediment layer", "number", "m"),
"heat.bed_diffusivity": _c("Thermal diffusivity of the sediment", "number", "m2/s"),
"heat.groundwater_temperature": _c("Temperature of returning groundwater", "number", "deg C"),
"heat.groundwater_from_river": _c(
"Returning groundwater follows the river", "bool",
help="The returning water takes the mean inflow temperature of the last hours "
"instead of a constant, so that it changes with the season."),
"heat.groundwater_window": _c("Hours of the river mean", "number", "h"),
"heat.statistics_from": _c(
"Start of the window of the statistics", "number", "h",
help="Hours after the start of the run. Leave out the hours in which the water of "
"the start is still in the reach."),
"heat.observations": _c(
"Measured water temperatures", "table",
help="Logger, time and temperature. Several sensors of a logger at the same time "
"are averaged; leave out a sensor that the sun or the air reaches."),
"heat.observation_points": _c("Positions of the loggers", "vector"),
"heat.observations_utc_offset": _c("Clock of the loggers: offset from UTC", "number", "h"),
"heat.observation_error": _c("Error of a measured temperature", "number", "deg C",
help="As a standard deviation."),
"heat.model_error": _c("Error of the model", "number", "deg C",
help="What no parameter set reproduces, as a standard deviation; "
"combined with the error of the loggers. The initial design "
"reports the value its best set needs."),
"heat.prior_samples": _c("Prior samples of the temperature calibration", "int",
help="Random parameter sets the surrogate is evaluated on; "
"the posterior is the accepted part of them."),
"heat.calibrate": _c(
"Parameters to calibrate", "yaml",
help="wind_factor, bed_diffusivity or groundwater_temperature, each with its "
"smallest and largest value, for example wind_factor: [0.5, 2.0]."),
"heat.sets_per_run": _c("Parameter sets in one run", "int"),
"heat.initial_temperature": _c(
"Water temperature at the start", "number", "deg C",
help="Empty: the mean of the inflows at the start."),
"ground_truth.sources": _c("Measurement files", "yaml",
help="One entry per file: category, kind and positions."),
"ground_truth.targets": _c("Calibration target workbook", "table"),
"calibration.parameters": _c("Calibration parameters", "yaml",
help="One entry per parameter: name, min and max."),
"calibration.calibration_quantities": _c("Calibration targets", "yaml"),
"calibration.init_runs": _c(
"Simulations before the surrogate model", "int",
help="Empty: 10 per calibration parameter, rounded up to a power of two "
"(Loeppky et al. 2009; an even Sobol design), for example 64 for four "
"parameters."),
"calibration.max_runs": _c(
"Simulations in total", "int",
help="Empty: no limit, the active learning runs until it converges; without "
"that option, the simulations before the surrogate model plus 50 "
"iterations of active learning."),
"calibration.until_converged": _c(
"Run the active learning until it converges", "bool",
help="On by default. The active learning runs at least 25 iterations and then "
"continues in steps of five until, over the last five, neither the "
"logarithm of the model evidence nor the relative entropy varies by more "
"than 1; a warning says when the two show no sign of leveling off. A value "
"in Simulations in total limits it. Off: exactly Simulations in total."),
"calibration.hotstart": _c(
"Start each simulation from the steady result", "bool",
help="Each simulation of the calibration continues from the converged steady "
"simulation of the case instead of starting dry again, so the flow only "
"settles to the roughness being tested."),
"calibration.hotstart_duration": _c(
"Simulated time of each simulation", "number", "s",
help="Leave empty to use 1.2 times the residence time of the reach: the "
"volume of water in the steady result divided by the discharge. When the "
"calibration varies the outflow level, the volume of the reach filled to "
"the highest outflow level of the range, plus the 600 s over which the "
"level rises."),
"calibration.gp_mean": _c(
"Mean of the surrogate model", "choice",
choices=("constant", "linear", "hydraulic"),
help="hydraulic: the surrogate model starts from a trend that follows how each "
"parameter acts on the flow (the friction law of a roughness zone; ks and "
"Cmu of the OpenFOAM model through the logarithmic layer), and learns only "
"the deviation from it. It needs fewer simulations for the same accuracy. "
"constant: no trend, as in HydroBayesCal up to 1.13. Needs HydroBayesCal "
"1.14.0 or newer; older releases ignore it."),
"calibration.prior_samples": _c(
"Prior samples for the posterior", "int",
help="Parameter sets drawn from the ranges at which the surrogate model is "
"evaluated; HydroBayesCal keeps each with a probability equal to its "
"likelihood divided by the largest one. A calibration against many "
"measurements keeps few of them, so raise it when the log reports few "
"accepted samples (Inn KB15: 60,000). Default 5,000."),
"calibration.structural_error": _c(
"Model error at points the simulated flow cannot reach", "choice",
choices=("none", "unreachable"),
help="unreachable: after the initial simulations, a point whose measured value "
"lies outside the range of all simulated values gets that distance added "
"to its error, for depth and velocity separately. The model cannot "
"reproduce such a point with any roughness in the ranges, so it should not "
"decide the calibration. The errors of a survey and calibration target that the best "
"initial simulation misses by more than their errors are then widened to "
"that misfit. none: every point keeps its measurement error."),
"calibration.outflow_offset": _c(
"Range of the outflow level offset", "yaml", "m",
help="[min, max]: the calibration varies the outflow level by an offset in this "
"range, added to the level of the rating at the discharge of every "
"situation. For a water level held up by a control that the terrain model "
"does not resolve or that lies downstream of the model. Leave empty to "
"keep the level of the rating."),
"calibration.outflow_slope": _c(
"Range of the energy slope at the outlet", "yaml", "-",
help="[min, max], for example [1.0e-5, 2.0e-3]: the calibration varies the "
"energy slope in the rating of the outflow section (rating_method: "
"section) in this range, in place of the bed slope of the reach, so that "
"every situation gets the level of one calibrated stage-discharge curve at "
"its discharge. For a water level held up by a control downstream of the "
"model (a backwater). Not together with the offset of the outflow level. "
"Leave empty to keep the level of the rating."),
"openfoam.mode": _c("Free surface", "choice", choices=("vof", "rigid-lid")),
"openfoam.outflow_boundary": _c(
"Outflow boundary", "choice", choices=("level", "rating-curve"),
help="level: one water level for the whole run. rating-curve: the level that "
"the stage-discharge relation gives for the discharge that leaves the "
"model, at every time step (boundary condition of the BAW)."),
"openfoam.cell_size_factor": _c(
"Cell size relative to the channel edge length", "number", "-",
help="3 makes the lattice three times coarser than the TELEMAC mesh in the "
"channel. Leave empty to use the cell size below."),
"openfoam.cell_size": _c("Cell size", "number", "m"),
"openfoam.n_layers": _c("Layers in the vertical", "int"),
"openfoam.end_time": _c("Simulated time", "number", "s",
help="End of the OpenFOAM run, counted from its start."),
"openfoam.n_processors": _c("Processor cores", "int"),
"hydrodynamics.regime": _c(
"Steady or unsteady", "choice", choices=("steady", "unsteady"),
help="unsteady runs the hydrograph given on the page Liquid boundaries."),
"drying.film_infiltration": _c("Remove thin films", "bool"),
"hydrology.rainfall": _c(
"Rain series", "table",
help="A table of time and rain (CSV or Excel), or the file the data wizard "
"writes. The model's time zero is the start of the series."),
"hydrology.rainfall_units": _c(
"Rain values are", "choice", choices=("mm/h", "mm"),
help="mm/h: intensities. mm: depths per interval."),
"hydrology.rainfall_stamp": _c(
"A time stamp is the", "choice", choices=("end", "start"),
help="Weather services stamp the end of the interval: the rain of 12:00 to "
"13:00 is reported at 13:00."),
"hydrology.rainfall_start": _c(
"Time zero", "text",
help="Date and time of the start of the simulation for a series with dates, "
"e.g. 2024-06-02T14:00. Empty: the first interval."),
"hydrology.infiltration": _c("Infiltration", "choice", choices=("green-ampt", "none"),
help="none: all rain runs off."),
"hydrology.green_ampt": _c(
"Green-Ampt solution", "choice", choices=("sadeghi", "exact", "travert"),
help="sadeghi: de Vos et al. (2024), the explicit solution of Sadeghi et al. "
"with Chu's ponding time. exact: Ks (1 + a/F) on the infiltrated depth. "
"travert: TELEMAC's own equations, for comparison."),
"hydrology.soil_zones": _c("Soil zones", "vector",
help="Polygons with a whole-number Zone ID. Empty: one "
"soil for the whole catchment."),
"hydrology.soil_table": _c(
"Soil table", "table",
help="Columns zone_id, texture, ks_mm_h, suction_m, theta_s, theta_i; what a "
"row leaves empty comes from its texture (Rawls et al., 1983). With "
"sediment transport also sand, silt, clay (or fraction_1, fraction_2, ...), "
"layer_thickness, mud_erosion_shear, partheniades and mud_concentration: "
"what each zone can lose to erosion."),
"hydrology.soil_texture": _c(
"Soil texture (whole catchment)", "choice",
choices=("sand", "loamy sand", "sandy loam", "loam", "silt loam",
"sandy clay loam", "clay loam", "silty clay loam", "sandy clay",
"silty clay", "clay"),
help="Used where there is no soil table."),
"hydrology.initial_water_content": _c(
"Water content at the start", "number", "m3/m3",
help="Volumetric soil moisture before the storm, for zones without their own "
"value. The data wizard fetches the ERA5-Land value of the event."),
"hydrology.sealed_surfaces": _c("Sealed surfaces", "vector",
help="Roofs and roads: no infiltration there."),
"hydrology.weather": _c(
"Weather series", "table",
help="Time and any of wind_speed_10m, wind_direction_10m, shortwave_radiation, "
"temperature, relative_humidity, et0, evaporation."),
"hydrology.wind": _c("Wind", "bool", help="Apply the wind of the weather series."),
"hydrology.evaporation": _c(
"Evaporation", "choice", choices=("none", "et0", "penman", "series"),
help="et0: the et0 column times the factor of open water. penman: FAO-56 from "
"radiation, temperature, humidity and wind. series: the evaporation "
"column."),
"hydrology.outlet_condition": _c(
"Outlet", "choice", choices=HYDROLOGY_OUTLETS,
help="normal_depth: the level of normal flow through each outflow section. "
"free: nothing prescribed; only for a supercritical outlet. elevation: "
"the fixed level of the page Liquid boundaries. overfall: no water is "
"held at the outflow lines, for the lower edge of a field over which the "
"terrain falls away."),
"hydrology.drain_time": _c("Simulated time after the rain", "number", "s"),
"hydrology.printout_period": _c("Interval of the result maps", "number", "s"),
"hydrology.kernel": _c(
"Numerical kernel", "choice", choices=("finite-volumes", "finite-elements"),
help="finite-volumes: TELEMAC's HLLC scheme with the hydrostatic "
"reconstruction of Chen and Noelle, made for water shallower than the "
"steps of the bed. finite-elements: thin films drain slower."),
"morphodynamics.sediment_classes": _c(
"Sediment classes", "yaml",
help="One entry per grain size class, for example name, diameter (m) and "
"share of the bed."),
"postproc.telemac_maps": _c(
"Maps of a TELEMAC result", "multichoice",
help="The layers that Load results adds in QGIS."),
"postproc.map_style": _c(
"Map style of the water depth", "choice",
help="Velocity arrows or streamlines that the plugin draws over the water "
"depth or the velocity magnitude of a loaded TELEMAC result. Empty: plain "
"depth and velocity maps."),
"postproc.openfoam_outputs": _c(
"OpenFOAM results for", "multichoice",
help="paraview: the file case.foam. visit: the controlDict of the case, which "
"VisIt opens. vtk: a conversion after every run, which QGIS can read too "
"and which can double the size of the case."),
"postproc.scenes": _c(
"VisIt figures", "multichoice",
help="The predefined figures of three-dimensional results that axqua postproc "
"renders with VisIt."),
}
#: Settings whose choices are a constant of :mod:`axqua.config` that the loader checks against, so that the form and the loader cannot disagree.
CONFIG_CHOICES: dict[str, str] = {
"postproc.telemac_maps": "TELEMAC_MAPS",
"postproc.map_style": "MAP_STYLES",
"postproc.openfoam_outputs": "OPENFOAM_OUTPUTS",
"postproc.scenes": "VISIT_SCENES",
}
#: Choices of settings that are not essential, where the code accepts a fixed set only.
CHOICES: dict[str, tuple[str, ...]] = {
"initialization.prewet_mode": ("normal-depth", "constant"),
"gain_lose.mode": ("off", "region", "fortran"),
"gain_lose.losing_region": ("patch", "line"),
"gain_lose.water_table": ("off", "phreatic"),
"hydrology.wind_formula": ("flather", "wu", "constant"),
}
#: Keys of the ``project`` block, which are fields of :class:`~axqua.config.Config`
#: itself and not of a block class.
_PROJECT = {"name": "text", "crs_epsg": "int", "sim_dir": "folder",
"preprocessing_dir": "folder", "model_dir": "folder",
"postprocessing_dir": "folder", "calibration_dir": "folder"}
_RASTER = re.compile(r"(^|_)dem(_|$)|raster|dem_of_difference|^ortho")
_TABLE = re.compile(r"table|stage_discharge|^inflow$|measurements|targets|series|csv")
_FOLDER = re.compile(r"(_dir|_folder|_root)$|^case_dir$")
def _file_kind(name: str) -> str:
if _FOLDER.search(name):
return "folder"
if _RASTER.search(name):
return "raster"
if _TABLE.search(name):
return "table"
return "vector"
def _kind(annotation: Any, default: Any, name: str) -> str:
"""The kind of a value, from the annotation of its field."""
options = typing.get_args(annotation) if (
typing.get_origin(annotation) in (typing.Union, types.UnionType)) else (annotation,)
options = [option for option in options if option is not type(None)]
if any(option is Path for option in options):
return _file_kind(name)
if len(options) == 1:
only = options[0]
if only is bool:
return "bool"
if only is int:
return "int"
if only is float:
return "number"
if only is str:
return "text"
if options and all(option in (int, float, str) for option in options):
# "auto" or a number: whatever is typed is kept as it is written
return "text" if str in options else "number"
if isinstance(default, bool):
return "bool"
return "yaml" # a list, a table or a nested block
#: A ``# ---- name ----`` heading in the source of a dataclass, which groups the fields below it.
_HEADING = re.compile(r"^\s*#\s*-{3,}\s*(.+?)\s*-{3,}\s*$")
def _comments(cls: type) -> dict[str, str]:
"""The comment that stands with each field in the source of a dataclass."""
try:
lines = inspect.getsource(cls).splitlines()
except (OSError, TypeError):
return {}
out: dict[str, str] = {}
pending: list[str] = []
field_line = re.compile(r"^\s{4}([a-z_][a-z0-9_]*)\s*:\s*[^=#]+(?:=[^#]*)?(?:#\s*(.*))?$")
for line in lines:
stripped = line.strip()
if _HEADING.match(line): # a group heading, not an explanation
pending = []
continue
if stripped.startswith("#"):
pending.append(stripped.lstrip("#").lstrip(":").strip()) # also #: lines
continue
match = field_line.match(line)
if match:
parts = pending + ([match.group(2)] if match.group(2) else [])
text = " ".join(part for part in parts if part)
if text:
out[match.group(1)] = re.sub(r"\s+", " ", text)
pending = []
return out
def _groups(cls: type) -> dict[str, str]:
"""The ``# ---- name ----`` heading each field of a dataclass stands under."""
try:
lines = inspect.getsource(cls).splitlines()
except (OSError, TypeError):
return {}
field_line = re.compile(r"^\s{4}([a-z_][a-z0-9_]*)\s*:")
out: dict[str, str] = {}
current = ""
for line in lines:
found = _HEADING.match(line)
if found:
current = found.group(1).strip().lower()
continue
match = field_line.match(line)
if match and current:
out[match.group(1)] = current
return out
def _frames() -> dict[str, tuple[str, str]]:
"""``{block or block/group: (page key, frame title)}``."""
return {block: (key, title) for key, _t, _l, frames in PAGES
for block, title, _info in frames}
def _label(name: str) -> str:
words = name.replace("_", " ").strip()
return words[:1].upper() + words[1:]
def _json(value: Any) -> Any:
if isinstance(value, Path):
return str(value)
if isinstance(value, dict):
return {str(k): _json(v) for k, v in value.items()}
if isinstance(value, (list, tuple)):
return [_json(v) for v in value]
return value
def _default_case():
"""A case with nothing but what the loader insists on, and all of it written out."""
from axqua.config import config_to_dict, load_config
with tempfile.TemporaryDirectory() as folder:
path = Path(folder) / "default.axq-case"
path.write_text("project: {name: case, crs_epsg: 25832}\n"
"geodata: {dem_initial: dem.tif, boundary: outline.gpkg}\n",
encoding="utf-8")
cfg = load_config(path)
cfg.declared_blocks = frozenset() # so that every block is written
return cfg, config_to_dict(cfg, base=Path(folder), portable=True)
[docs]
def build() -> list[dict[str, Any]]:
"""The sections of a case file with their settings, as plain data."""
from axqua import config as config_module
cfg, written = _default_case()
sections = []
listed = {block for block, _title, _label_ in BLOCKS}
order = list(BLOCKS) + [(block, _label(block), "help-case-setup")
for block in written if block not in listed]
for block, title, label in order:
values = written.get(block)
if not isinstance(values, dict):
continue
owner = getattr(cfg, block, None)
cls = type(owner) if dataclasses.is_dataclass(owner) else None
hints = typing.get_type_hints(cls) if cls is not None else {}
comments = _comments(cls) if cls is not None else {}
groups = _groups(cls) if cls is not None else {}
frames = _frames()
names = list(values)
if block == "project":
names = ["name", "crs_epsg", "sim_dir"] + [n for n in values
if n not in ("name", "crs_epsg")]
fields = []
for name in names:
key = f"{block}.{name}"
curated = CURATED.get(key, {})
default = None if name in ("dem_initial", "boundary") else values.get(name)
if block == "project":
kind = _PROJECT.get(name, "text")
default = values.get(name) if name != "sim_dir" else "axqua-case"
else:
kind = _kind(hints.get(name), default, name)
kind = curated.get("kind") or ("choice" if key in CHOICES else kind)
page, frame = frames.get(f"{block}/{groups.get(name, '')}",
frames.get(block, (block, "")))
required = key in REQUIRED
when = [dict(condition) for condition in REQUIRED_WHEN.get(key, ())]
unless = [dict(condition) for condition in REQUIRED_UNLESS.get(key, ())]
fields.append({
"key": key, "block": block, "name": name,
"label": curated.get("label") or _label(name),
"kind": kind,
"unit": curated.get("unit", ""),
"help": curated.get("help") or comments.get(name, ""),
"choices": list(curated.get("choices")
or (getattr(config_module, CONFIG_CHOICES[key])
if key in CONFIG_CHOICES else ())
or CHOICES.get(key, ())),
# a required entry is always on the form; an advanced curated one is
# offered under Add setting... with its label
"essential": (key in CURATED and not curated.get("advanced")) or required
or bool(when),
"default": _json(default),
"required": required,
"required_when": when,
"required_unless": unless,
"disabled_when": [dict(c) for c in DISABLED_WHEN.get(key, ())],
"hidden": key in HIDDEN,
"page": page,
"frame": frame,
})
sections.append({"block": block, "title": title, "label": label,
"fields": fields})
return sections
[docs]
def pages(sections: list[dict[str, Any]] | None = None) -> list[dict[str, Any]]:
"""The pages of the editor with their frames and, in each, the keys it shows.
*sections* is what :func:`build` returned, for a caller that has it already.
Every setting of :func:`build` is on exactly one frame; a block that no page names
gets a page of its own at the end.
"""
sections = build() if sections is None else sections
fields = [item for section in sections for item in section["fields"]]
out = []
known = set()
for key, title, label, frames in PAGES:
page = {"key": key, "title": title, "label": label,
"switch": dict(SWITCHES.get(key, {})), "frames": []}
for _block, frame_title, info in frames:
keys = [item["key"] for item in fields
if item["page"] == key and item["frame"] == frame_title
and not item.get("hidden")]
if keys:
frame = {"title": frame_title, "info": info, "keys": keys}
note = FRAME_NOTES.get((key, frame_title))
if note:
frame["note"] = note
block = FRAME_SWITCHES.get((key, frame_title))
if block:
frame["switch"] = {"block": block, **SWITCHES[block]}
page["frames"].append(frame)
known.update(keys)
out.append(page)
known.update(item["key"] for item in fields if item.get("hidden"))
for section in sections:
rest = [item["key"] for item in section["fields"] if item["key"] not in known]
if rest:
out.append({"key": section["block"], "title": section["title"],
"label": section["label"], "switch": {},
"frames": [{"title": "", "info": "", "keys": rest}]})
return out
[docs]
def block_classes() -> dict[str, str]:
"""``{class name in lower case: block}``. The messages of the loader name a class
(``MeshConfig: unknown config keys``), and a user can act on a block."""
cfg, written = _default_case()
out = {}
for block in written:
owner = getattr(cfg, block, None)
if dataclasses.is_dataclass(owner):
out[type(owner).__name__.lower()] = block
return out
[docs]
def field_kinds() -> dict[str, str]:
"""``{dotted key: kind}`` for every setting, which is what a check needs."""
return {item["key"]: item["kind"] for section in build() for item in section["fields"]}