Skip to content

Gridded rain and a moving rain cell - #13

Merged
hgopalan merged 2 commits into
developmentfrom
claude/gridded-rain
Nov 23, 2026
Merged

hgopalan merged 2 commits into
developmentfrom
claude/gridded-rain

Conversation

@hgopalan

@hgopalan hgopalan commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Related to #11

Summary

#11 added rain that is the same in every cell. This PR adds two kinds of rain that change from place to place:

  • Gridded rain (flume.rain.type = gridded): a time series of rain grids, for example from a radar or a weather model. Each grid is an ESRI ASCII file of the rate in mm/h, on its own cells. An index file lists the grids with their times.
  • Moving rain cell (flume.rain.type = moving_cell): a storm cell whose rate falls off as a Gaussian from a centre that moves at a constant velocity. It is analytic, so tests can check a field that varies in space and time against a closed form.

Both use the forcing interface of #11. The solver asks for the depth of rain over a step, and the providers return the integral of the rate over each cell and over the step. A closed domain therefore receives the integral of the input over the domain and over the run to round-off, whatever the grid sizes and the steps are.

Each grid is mapped to the solver's cells by the mean over each cell (the area each grid cell shares with the solver cell). A grid that does not cover the domain stops the run. Grids are read again when a step needs them. The run keeps only the grids a later step can still use, plus one grid at a time that only the current step uses, so a long series is not held in memory.

Runs with none, constant or hyetograph rain are bit-identical to development: their code path is unchanged.

Theory

Gridded rain, space. Entry $k$ of the index is a time $T_k$ and a grid of rates $g_{k,m}$ (m/s) on cells $A_m$. On a solver cell $\Omega_{ij}$ of area $|\Omega_{ij}|$ the mapped rate is

$$M_{k,ij} = \frac{1}{|\Omega_{ij}|} \sum_m |\Omega_{ij} \cap A_m|\, g_{k,m} .$$

Why: the sum of $M_{k,ij},|\Omega_{ij}|$ over the domain is the integral of the grid over the domain. The cell edges are computed in grid units by one formula, so neighbouring cells share each edge bit for bit and the overlaps along a row add up to the row's width. An edge within 16 epsilons of a whole grid unit is set to that whole number, so a solver edge that lies on a grid edge does not reach a sliver of the next grid cell (a NODATA cell there would otherwise stop the run).

Gridded rain, time. The depth of a step from $t^n$ to $t^{n+1}$ is

$$\Delta h_{ij} = \sum_k w_k\, M_{k,ij}, \qquad w_k = W_k(t^{n+1}) - W_k(t^{n}),$$

where $W_k(t)$ is the integral up to $t$ of the weight of entry $k$ in the rate: 1 from $T_k$ to $T_{k+1}$ (step), or the hat function that is 1 at $T_k$ and 0 at $T_{k\pm1}$ (linear). Before $T_0$ the first grid holds, and after $T_N$ the last. Why a difference: the end of one step is the start of the next, so the weights of all the steps add up to those of the whole run.

Moving cell. With peak rate $P$, standard deviation $\sigma$, centre $\mathbf{c}_0$ at $t = 0$ and velocity $\mathbf{v}$,

$$r(\mathbf{x}, t) = P \exp\left(-\frac{|\mathbf{x} - \mathbf{c}(t)|^2}{2\sigma^2}\right), \qquad \mathbf{c}(t) = \mathbf{c}_0 + \mathbf{v}\,t .$$

The mean over the cell $[x_a, x_b] \times [y_a, y_b]$ at one time is exact:

$$\frac{P}{|\Omega_{ij}|}\,\frac{\pi\sigma^2}{2}\left[\mathrm{erf}\,\frac{x_b - c_x}{\sqrt{2}\,\sigma} - \mathrm{erf}\,\frac{x_a - c_x}{\sqrt{2}\,\sigma}\right]\left[\mathrm{erf}\,\frac{y_b - c_y}{\sqrt{2}\,\sigma} - \mathrm{erf}\,\frac{y_a - c_y}{\sqrt{2}\,\sigma}\right].$$

Each difference is formed from $\mathrm{erfc}$ on the side away from the centre, so a cell far from the centre keeps its digits. The time integral uses the 4-point Gauss-Legendre rule on $n = \max(1, \lceil 4|\mathbf{v}|,\Delta t/\sigma \rceil)$ sub-intervals, so the centre moves at most $\sigma/4$ in each. A cell at rest needs one sub-interval and is exact. A cell of peak 0 delivers no rain and skips the work. A step that would need more than $10^4$ sub-intervals (the centre moves more than $2500\sigma$) stops the run with a message naming flume.rain.cell.sigma and the step controls.

Changes

  1. Conservative raster mapping (Source/FLUME_Raster.cpp:478)

    • sample_conservative: the mean of a raster over each cell. A NODATA_value cell that shares area with a solver cell is counted and stops the run, as for the other rasters.
    • snap_whole: the 16-epsilon snap of an edge to a whole raster unit. holding_cell now uses it too (same formula as before, bit-identical).
  2. Gridded rain (Source/FLUME_RainField.H:66, Source/FLUME_RainField.cpp:193)

    • read_rain_index: parsed on the I/O rank, then broadcast. Every line is checked (two items, a finite time, times strictly increasing; # starts a comment). A relative grid path is taken from the index file's directory.
    • GriddedRain: reads and maps every grid at start-up to check it (covers the domain, no NODATA read, no rate below 0 in the part read) and keeps its largest mapped rate for the step bound. During the run each grid is read again when a step needs it. Grids that no later step can use are dropped; a grid only the current step uses is read into a temporary and freed at once. Grids whose largest rate is 0 are not read during the run.
    • entry_weights: the time weights above.
  3. Moving cell (Source/FLUME_RainField.H:111, Source/FLUME_RainField.cpp:311)

    • MovingCellRain: the exact cell mean and the Gauss-Legendre time rule. Centre positions are formed in double precision relative to flume.prob_lo and passed to the device.
  4. Provider selection (Source/FLUME_Forcing.cpp:225, Source/FLUME_Flood.cpp:147)

    • make_forcing now takes the geometry, boxes and distribution of the run.
  5. Inputs (Source/FLUME_Inputs.cpp:462)

Input Default Effect
flume.rain.type none Adds gridded and moving_cell. microphysics still stops the run (reserved).
flume.rain.file required with hyetograph or gridded With gridded: the index file, lines of a time (s) and a grid path.
flume.rain.interpolation step Now also with gridded.
flume.rain.cell.peak_rate required with moving_cell Rate at the centre, mm/h; finite, ≥ 0.
flume.rain.cell.sigma required with moving_cell Standard deviation, m; finite, > 0.
flume.rain.cell.center required with moving_cell Centre at t = 0, m; two finite values.
flume.rain.cell.velocity 0 0 Velocity of the centre, m/s; two finite values.

Each flume.rain.cell.* key without moving_cell, and flume.rain.interpolation with moving_cell, stops the run with a message naming the key.

  1. Docs and tests
    • In the page layout of Restructure the documentation into user guide, theory and verification #12: Inputs.rst (input rows), InputFiles.rst ("Gridded rain index files"), theory/SourceTerms.rst ("Rain that varies in space"), TestCatalog.rst (the new tests), Verification.rst (the measured differences). README.md and the index page list gridded rain and the moving cell as available.
    • Tests/RainField/: decks, synthetic grids and index files with the script that writes them, bad files, check_rain_field.py, RainFieldTests.cmake and a README.md. The reserved-type abort tests in Tests/Rain/RainTests.cmake now cover microphysics only.

Ghost cells and NaNs: both providers write valid cells only; the rain source reads valid cells only. A raster value is checked for NODATA and for a negative rate before it enters a sum.

Results

Closed, flat, dry domain of 24 × 16 cells of 2 m in 6 boxes, against a reference computed independently from the files (areas in metres, time split at the index times; error functions and adaptive Simpson quadrature for the cell):

Case Largest difference
Every cell, one step, grids of 3.7 × 2.9 m (no shared edges), linear in time 9.9e-16 of the largest depth
Every cell, one step, grid of 1.3 m 4.3e-16
Water in the domain to 1200 s, three grids, step and linear 7.1e-16 of the water at step 679 (3 epsilons)
Every cell, 1 m grid whose edges fall on solver edges, NODATA ring outside the domain 17 epsilons of the largest depth
Moving cell at rest, every cell 5.8e-16
Moving cell, 29 m in one step (20 sub-intervals), every cell 1.1e-13 (1.3e-2 with one sub-interval)
Moving cell leaving the domain, water to 1200 s 1.4e-16

Cost: runs without gridded or cell rain do the same work as before. A gridded run reads each grid twice (once at start-up, once when used). It keeps the grids a later step can use plus one temporary grid. A moving cell evaluates $4n$ error-function products per cell per step.

Test(s)

Built with -j 2, tests run with ctest -j 2 and at most 2 MPI ranks (tests that need 4 ranks were not run locally).

Build Result
Release 385/385 passed (again after merging development with #12)
Debug (FLUME_TESTING=ON, warnings as errors) 384/385; rain_plane_kinematic_wave_order2 times out, as on development in Debug (not changed here)
Single precision 380/380 before the last review fixes; the 69 rain tests 69/69 after them

CPU runs in the style of GPU CI (amrex.the_arena_init_size=0 amrex.init_snan=1 fab.do_initval=1 amrex.fpe_trap_invalid=1, 2 ranks), with one box per rank (fabarray.mfiter_tile_size=1024 1024 1024) and again with 4 × 4 tiles: the linear, volume, aligned-grid, cell and cell-volume decks all pass check_rain_field.py.

make -C Docs/sphinx_doc html with -W, codespell and git diff --check are clean.

CTest Checks
rain_gridded_cells_linear_np1, _np2, rain_gridded_cells_fine every cell equal to the rain of its area, 16 epsilons
rain_gridded_volume_step, rain_gridded_volume_linear_shifted water = integral of the input over domain and time, every plotfile
rain_gridded_dry_burst_no_stop_time a grid ahead of the run's time sets the step of a dry domain
rain_gridded_cells_aligned_nodata_ring grid edges on solver edges, NODATA just outside the domain: no abort, every cell within 32 epsilons
rain_cell_at_rest_np1, _np2, rain_cell_fast_step, rain_cell_volume the cell against the closed form
rain_cell_zero_peak_long_step a cell of peak 0 with a step of $10^7$ s runs (no sub-interval cap)
abort_rain_gridded_* each bad index line (including an infinite time), missing grid, grid that does not cover the domain, negative rate and NODATA
abort_rain_cell_* each missing or out-of-range cell key; a cell key or flume.rain.file with the wrong type; a step that needs more than $10^4$ sub-intervals
abort_rain_type_microphysics the reserved name still stops the run

Each test was shown to fail on a one-line change to the code (all 21 caught):

Change Caught by
unweighted raster mean; edge not shifted by the domain origin gridded cell tests
left linear weight; step weight clamp; rain scaled by 0.999; linear range one entry short gridded cell and volume tests
one sub-interval; erfc difference reversed; centre not moving cell tests
grid path from the working directory gridded tests
negative-rate check off; look-ahead of one entry abort_rain_gridded_negative, rain_gridded_dry_burst_no_stop_time
sigma > 0 as >= 0; velocity key allowed without its type abort_rain_cell_*
edge snap removed rain_gridded_cells_aligned_nodata_ring
zero-peak return removed; sub-interval cap removed rain_cell_zero_peak_long_step, abort_rain_cell_step_too_long
center message, cell key and file checks with another type, infinite-time check the matching abort_rain_* tests

Variations from the plan

  • The file format is ESRI ASCII grids listed in an index file, which the existing raster reader already handles. A later replay will write the same format.
  • Space is mapped exactly (cell means); time uses 4-point Gauss-Legendre for the moving cell and the exact weights for grids.

Not in this PR

  • Grids in other formats (NetCDF, GeoTIFF).
  • Rain from ERF's microphysics (name reserved).
  • A rain cell that changes size or strength in time.
  • A shorter look-ahead for the step bound: as in Prescribed rain: constant rate and hyetograph #11, the bound uses the largest rate over the remaining look-ahead interval.

🤖 Generated with Claude Code

hgopalan and others added 2 commits November 22, 2026 18:48
Adds flume.rain.type = gridded (ESRI ASCII rain grids listed with their
times in an index file, mapped to the solver cells by exact cell means and
weighted in time by differences of cumulative step or linear weights) and
flume.rain.type = moving_cell (a Gaussian cell moving at constant velocity:
exact erf cell means, 4-point Gauss-Legendre in time on sub-intervals of at
most sigma/4 of travel). Both return the depth of a step, so a closed
domain receives the integral of the input to round-off.

A grid that does not cover the domain, a NODATA cell read, a negative rate
read, a bad index line and every out-of-range cell key stop the run with a
message naming the input. Raster edges within 16 epsilons of a whole raster
unit are snapped to it (also in holding_cell, bit-identical there).

Ghost cells and NaNs: both providers write valid cells only and the rain
source reads valid cells only; raster values are checked for NODATA and
negative rates before they enter a sum.

Runs with none, constant or hyetograph rain are bit-identical.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The rain documentation of this branch moves to the new pages: inputs in
Inputs.rst, the index file format in InputFiles.rst, the model in
theory/SourceTerms.rst (Rain that varies in space), the tests in
TestCatalog.rst and the measured errors in Verification.rst. Model.rst
stays deleted as on development. README and the index page list gridded
rain and the moving cell as available.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@hgopalan
hgopalan merged commit 245342a into development Nov 23, 2026
10 of 11 checks passed
@hgopalan
hgopalan deleted the claude/gridded-rain branch November 23, 2026 03:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant