Sturgeon Deep Dive · Architecture · How it is built and how to change it
Architecture.
How Sturgeon is put together — the layers, the seams between them, and the regions, reaches and blocks every model is built from — and, above all, where you can change the math and do it your own way: in the browser with no code, in a region’s settings, or as a plug-in of your own, judged against the live model.
The architecture belongs to no one place. One engine runs every region, and a region is data. Three run today — the San Francisco Bay–Delta, the Rhine–Meuse delta in the Netherlands, and a deliberately plain test river, the Colorado below Glen Canyon Dam — and on this page they appear only as examples.
The layers, and what each owns
Everything lives in one repository, FischerDeltaModelReturns ↗. Each layer has one job, and the published files are the only thing that passes from the model runner to your screen.
Level 0, the whole system. Data in on the left, the screen on the right; the published files are the only thing that passes between. The sources are shown by kind; each region picks its own of each kind, and the names are examples from the Bay–Delta and the Rhine–Meuse. The teal loop is the what-if: only the owner’s key sends one to the runner. The badge marks the box that the next level opens.
The math library (fdmr-math ↗, Rust): the equations, each written and tested once, with nothing about any place in it — Fischer’s dispersion, flood-wave routing, hydraulic geometry, the Saint-Venant network solver, PSS-78 salinity, decay and the fate of what is spilled, certainty.
The compute engine (fdmr-compute ↗, Rust): moves parcels and flow particles over the published fields, in river reaches, channel networks and meshes, and hands them across the joins between reaches one for one. It is built twice: to WebAssembly (fdmr-wasm ↗), where it runs in your browser in a background worker, and to Python (fdmr-py ↗), so the model runner calls exactly the same code. The bytes of the published format are fdmr-format ↗.
The model runner (fdmr_runner ↗, Python): the cycle, for every region in turn — fetch, calibrate the rivers nightly, run each reach that needs a run of its own, publish, keep every prediction in the ledger, and rerun the model nightly as of five days before.
The adapters (fdmr_adapters ↗): one per data source, each turning what it reads into SI units and naming its source. Every region picks from the same set, by kind: gauges, forecasts, operations, ocean models, satellites and maps. For example, the Bay–Delta reads USGS, California’s CDEC and NOAA; the Rhine–Meuse reads Rijkswaterstaat and the Dutch map service PDOK; the test region reads USGS alone.
The plug-ins (fdmr_plugins ↗): the model kinds and the maths that run them — river routing, the tidal network and the drivers that build it, the mesh fields and the join between meshes, salt transport — and the wrappers for the external models a reach can run on, such as DWR’s DSM2 and NOAA’s FVCOM and ROMS forecasts; and the register of maths, registry.py ↗.
The store and the ground: fdmr_store ↗ keeps every fetched value with its source and the time it was fetched, so an outage degrades the forecast instead of stopping it; fdmr_geo ↗ measures distances on the ground, once.
The published format (format.md ↗): one index.json per cycle saying what is published and how certain it is, and one compact binary .fdf file per reach per day. It is the only versioned interface; a reader refuses a major version it does not know rather than misread it.
The viewer (ui ↗, Svelte and MapLibre): the map only draws. Its worker moves particles on the flows it is given and computes nothing else about the water, and it never needs to know which region it is drawing.
The regions (regions ↗): data only — each region’s reaches, gauges, handoffs, settings, sources and guard words in its own regions/<id>/ files. Three today: sf-bay-delta, rhine-meuse and test-colorado.
Level 1, the layers. The Rust maths are written once and built twice: for Python, where the runner calls them, and to WebAssembly, where your browser’s worker runs the same code. The published files are the one interface between the two sides.
The seams
seams.toml ↗ lists every module and what it may use, and check_seams.py ↗ fails the build if any module reaches past its list, if a data-only folder holds code, or if engine code names a place. That last rule is the code’s own statement that the architecture belongs to no region. Every region lists its own guard words in its region.toml, and none of them may appear anywhere in the engine: for example Sacramento, Chipps and Golden Gate in the Bay–Delta; Lobith, Waal and Nieuwe Waterweg in the Rhine–Meuse; Lees Ferry and Glen Canyon in the test region. Data sources are named only through their adapters or the region’s own files. Each new region adds its own words, so the check grows stricter with every region added.
A region: reaches, model kinds and handoffs
A region is data: regions/<id>/region.toml ↗ and the files beside it. It lists the region’s reaches, and each reach is of a model kind, the code’s name for a kind of water and the model that runs it. The kinds today are the register’s: river-routing, one-way rivers between gauges, often below a dam; 1d-tidal-network, tidal rivers and branching channels; 2d-field, a bay or lagoon on a mesh; coastal-field, the open coast and shelf past a bay; and canal-network, canals fed by a network’s exports. The specification sketches more to come, such as pools and flooded areas, and every kind writes the same published format.
Reaches are joined by handoffs (the region’s [[handoffs]]), each of a kind: at a node, a river’s parcels enter a network’s channel at a junction; on a reach, a tributary enters another river some way along it; split by flow, a river divides and each branch takes its measured share; along a line, a mesh’s edge cuts a network’s channels, both ways; shared mesh, two meshes meet edge for edge; and at an export, what a network’s pumps take enters a canal. The published format carries every handoff, so the browser hands parcels across them without knowing which region it is in.
Level 2, a region as a chain of reaches. The top is the architecture: every reach is one of a few model kinds (kind in region.toml), and every join between two reaches is one of a few kinds of handoff (the [[handoffs]]). Below it, the regions running today, as examples of the same chain: each uses only the kinds its water needs. The Waal is opened at the next level.
For example, the Bay–Delta’s chain uses every kind: fifteen rivers into the Delta’s network at nodes, the Delta into its export canals and, along a line, into the Bay, and the Bay into the coast by a shared mesh. The Rhine–Meuse uses two: six rivers, which split by measured flow at the Pannerdensche Kop and the IJsselkop, into a tidal network at three nodes, which meets the North Sea’s tide as a boundary. The test region uses one: a single river.
Where a reach’s flow comes from: our solver, or an external model
Within a kind, the source of the flow can be swapped. A reach names it in its region’s data (flow_source, or its choice of the kind’s flow maths), and our own solvers and established external models stand side by side in the register as that part’s implementations, each external one with the checks against stations it is judged by in every cycle. So a reach may be driven by an established external model, with ours run beside it as a shadow until it matches. (In the code, a driver is how a source’s inputs reach our solver: one reads an existing model’s own input files, another builds a network from open data.)
For example, in the Bay–Delta the Delta’s network is driven by DWR’s DSM2 (flow_source = "dsm2", fdmr_plugins/dsm2 ↗). Our own Saint-Venant solver reads DSM2’s inputs through its driver and runs beside HYDRO in every six-hourly cycle as the shadow (shadow.py ↗): compared at every check, published as shadow.json, never as the forecast. Which of the two publishes is one setting in that region’s files, flows in dsm2.toml, "hydro" or "ours"; it stays "hydro" until ours is within the bounds its switch allows.
In the Rhine–Meuse no open model of the tidal rivers can be had, so the same solver drives the network from the start: its driver, open_network ↗, builds the channels from open data, and it publishes every hour.
NOAA’s forecasts play the external role for the Bay–Delta’s bay and coast: its 2d-field reach takes its flow from SFBOFS and its coastal-field reach from WCOFS (flow_source = "noaa-ofs:…"), corrected and joined to the reaches beside them. No shadow of ours runs beside them yet: there is no two-dimensional solver of ours in the live model.
Rijkswaterstaat’s forecasts play a different role. They are inputs, not a reach’s model: forecasts of discharge at a river gauge and of the sea’s level at the coast, which the Rhine–Meuse’s rivers and tidal network run on past now, as the Bay–Delta’s rivers run on the River Forecast Center’s.
Blocks and the block contract
Inside a region, the model is built from blocks, one for each kind of water: a river reach, a tidal channel network, a pool, a gate, a bay, the shelf. Each block holds its own physics, and blocks meet at ports. The block contract, contract.md ↗, fixes what crosses a port, so any two blocks can be joined without either knowing how the other works inside — and someone can replace one block, or one law inside it, with their own while the rest still fits. The contract is the same for every block in every region.
A descriptor per block, regions/<id>/blocks/<block>.toml: its type and dimensions, one named law per process, every value with its source, and its ports. For example, the Rhine–Meuse’s Waal, waal.toml ↗, or the Bay–Delta’s Delta, delta.toml ↗.
Laws inside blocks: each process is a law, a registered plug-in of maths named once in the register with what it does, its cited source, its code and the standard problems it passes. A reach chooses among the registered laws for its kind; an unknown choice is refused.
Each port says what it joins, which side sets the level and which answers with the flow, its layers over the depth, and the quantities it carries, each from the registry quantities.toml ↗. Layer schemes that differ are joined by conservative remapping, so the total over depth is kept exactly.
A checker in CI, check_contract.py ↗: every law in the maths register and matching the region’s own choice, every value sourced, every joined pair agreeing on place, direction, roles and layers, and every reach and handoff carried by a block. It also writes each region’s blocks page: Bay–Delta blocks, Rhine–Meuse blocks.
Written to grow. The contract is a draft, 0.1; minor versions only add, nothing is redefined, and others’ additions go under x-<name>.
Level 3, one block. The example is the Waal, a river reach in the Rhine–Meuse, as its descriptor gives it; a Bay–Delta river, its Delta or its Bay has the same parts. One law per process, every value sourced, and ports that say who sets the level and who answers with the flow. A law can be swapped for another in the register without touching the blocks either side. Two checks in CI hold every block, in every region, to the contract.
How to change the math
There are three ways in, from no code at all to a law of your own, and one way every change is judged.
Changing the math, across the levels. Each route starts at a different level, from a what-if in the browser to a law of your own, and all four end the same way: judged against what was measured, and adopted, if at all, by a person.
In the browser, with no code: the model editor. Each published region has one; for example, the Bay–Delta editor and the Rhine–Meuse editor. The model is drawn as its lines, nodes and meshes, geographic or as a schematic, and every part that computes is a block. Pick one to swap the law it uses for a process for another the register allows, or to change any value its descriptor carries, each shown with its source; add an event, such as a dam release. Your edits stay in your own browser. Save as a what-if file downloads them as a specification; Propose as a change downloads a patch to the region’s data files, ready for a pull request. Only the owner’s key can send a what-if to the runner (whatif.py ↗), which runs it as its own file set beside the live forecast, so the public puts no load on the runner.
In a region’s settings. A reach picks another registered implementation of a part in its region.toml entry, maths = { part = "name" }. For example, every Rhine–Meuse river reach, and the Bay–Delta’s Putah Creek, picks wide-channel-uniform-flow for its channel shape where no field measurements are published, and the Rhine–Meuse’s tidal rivers pick our own flow solver. An unknown choice is refused, never swapped silently. A few settings live in a region’s own files beside it, such as the Bay–Delta’s flows in dsm2.toml. Every number the model is set with, where it is set and why, is in parameters.md ↗, written from the code by params_doc.py ↗.
In code: a plug-in of your own. Write the law against the block contract — in Rust in the math library if the browser needs it, or in Python in the plug-ins. Register it in registry.py ↗ under its kind and part, with what it does, its source and its code, and name the standard problems it passes (its tests: pure advection, a pulse recovering its dispersion coefficient, mass kept, a textbook value); then give the runner a dispatch for it. Cite every source by its ref: key in references.toml ↗. maths_doc.py ↗ writes maths.md ↗ from the register, and references.py ↗ checks every citation. CI (ci.yml ↗) runs the seams, the contract, the references, the generated documents, the Python and Rust tests, and the WebAssembly and viewer builds. Once registered, the law is open to every reach of its kind in every region.
How a change is judged. Every published prediction is kept in the prediction ledger (ledger.py ↗) and scored when its measurement arrives, against persistence — the last reading, held. A registered alternative can be tried first as a trial (trial.py ↗): a region’s rivers rerun as a hindcast with your maths, labelled, in a ledger of its own, and scored apart. Where a region has them, experiments (experiment.py ↗) rerun its past interventions with and without them; in the Bay–Delta, for example, barriers and gate changes, with our own salt transport beside DWR’s QUAL on the same flows. Each region’s validation page shows it all, for example Bay–Delta validation and Rhine–Meuse validation. Nothing is tuned to the scores: a person reads them, and adopting a change is done by hand, with its source.
Worked example: your own dispersion law
Say you are a hydrologist with a better estimate of a river’s longitudinal dispersion coefficient than Fischer’s 0.011 U2W2/(d u*). Dispersion is a part of every river reach, so here is the whole path, the same for a river in any region.
Level 4, one law. Fischer’s longitudinal dispersion, one of the registered choices for any river reach in any region (the Waal’s among them), down to its function: where its code is, where it is registered and cited, where a reach chooses it, and the textbook case it is tested against. The worked example below adds a law beside it.
Write it. Add your function beside fischer_k in dispersion.rs ↗, with a test against a published case.
Compute it.dispersion_field in fields.py ↗ gives each element’s K; add yours beside it, and call it from publish.py ↗, where the reach’s chosen dispersion maths is read. The browser needs no change: with any dispersion law but the default cross-channel shear, the reach publishes its K as the dispersion field, and the engine (river.rs ↗) spreads parcels by it.
Register and cite it. Add it to the register’s river-routingdispersion part with its tests, and its sources to references.toml; regenerate the documents.
Try it.python -m fdmr_runner.trial on any region, with --maths '*.dispersion=your-law', reruns that region’s rivers as a hindcast and scores it apart from the live model.
Adopt it. If it holds up, a reach chooses it in its region.toml entry, maths = { dispersion = "your-law" } — the Waal in regions/rhine-meuse, say, or the Sacramento in regions/sf-bay-delta — and its block descriptor names the same law; CI must pass.
Where the data comes from, and one cycle
Every value reaches the model through an adapter, into the local store, with its source and time. A region names the sources it reads in its own files (a gauge’s source, or the region’s gauge_source), so no source is assumed in the engine. A failed fetch is named in the cycle’s fetch status and the rest carry on; every input past its last reading is carried on in a named way — held, its own forecast, a release rule — and each cycle’s index.json says which, input by input. Every source cited anywhere is listed once in references.toml. The whole flow, in diagrams, is how-it-works.md ↗.
The runner works in cycles. Timers start cycle.sh ↗, which runs every region in turn the same way: fetch; nightly, calibrate; each reach that has a run of its own, after the reach it is joined to; publish, which routes the rivers, joins every reach and writes the files; and the checks. What runs in a region is what its files ask for. For example, every six hours the Bay–Delta runs DSM2, its canals, the Bay, the coast and the shadow; every hour the Rhine–Meuse runs its tidal network; the test region only fetches and publishes.
Time: one cycle. The top two rows are the architecture: the timers, and one cycle that is the same for every region. Below, what that cycle is in each region today, as examples: a region runs only the steps its reaches need. In the Bay–Delta, the shadow, greyed, is compared every cycle but never published, and never stops the cycle.
A new region is data, not code
Everything above is general; a region only fills it in. To build a new one, you write data: a regions/<id>/region.toml naming its reaches, with a model kind and a flow source for each, its gauges and the adapters that feed them, its handoffs and its guard words; a block descriptor for each reach; and the settings files its kinds need. You write code only where its water needs something the engine does not have yet — an adapter for a new data source, a new model kind or a new law — and that code is general too: it names no place, the seams check makes sure of it, and every region can use it from then on.
Three regions keep the engine honest. test-colorado ↗ is deliberately plain: one gauged river below Glen Canyon Dam, run with only the river model and national adapters — if the engine needs changing to run it, something regional has leaked in. rhine-meuse ↗ tested whether the first region had shaped the engine. Porting it found what was tied to the United States and met each as configuration: river lines from Rijkswaterstaat’s waterway network or OpenStreetMap, channels from sourced width, slope and roughness where no field surveys are published, and, with no open model of its tidal rivers to be had, a channel network built from open data and run on our own solver. And sf-bay-delta ↗, the largest, where the model began, is one region among them: an example of the architecture, not the architecture itself. The next region, a Mississippi or a Thames, starts as a new folder of data.