hydr 0.2.0

Hydrology and hydraulic simulation on numr runtimes
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//! Native runoff attached to the surface/channel coordinator.
//!
//! Three impervious subcatchments own the three surface cells, so the plan suppresses their
//! rain. The north outlet drains into the middle cell of a walled reach. The south outlet drains
//! into a pond. The east outlet drains into surface cell 0, which no link or structure touches.
//! A weir joins the pond to surface cell 1, but the pond never reaches its crest. The run
//! prints each interval's runoff transfers and the composite balance, then resumes from a
//! checkpoint package.

#[allow(dead_code)]
#[path = "../tests/support/runtime.rs"]
mod runtime;

use hydr::channel::{
    ChannelBoundary, ChannelConfig, ChannelGeometry, ChannelInitialState, ChannelReachDefinition,
    RectangularChannelCell,
};
use hydr::coupling::{
    CoupledPoolSupport, StructureScenario, SurfaceChannelAttachment, SurfaceChannelBatch,
    SurfaceChannelConfig, SurfaceChannelForcingInterval, SurfaceChannelFreeOutlet,
    SurfaceChannelInitialState, SurfaceChannelNode, SurfaceChannelOperationOwner,
    SurfaceChannelReach, SurfaceChannelReachForcing, SurfaceChannelRunoff,
    SurfaceChannelRunoffDestination, SurfaceChannelRunoffOutlet, SurfaceChannelRunoffShare,
    SurfaceChannelSetup, SurfaceChannelStructure,
};
use hydr::grid::CartesianGrid;
use hydr::model::{CellState, Domain, FlowState, SpatialReference};
use hydr::runoff::{
    CellRainAssignment, Contribution, ContributionKind, PartitionAssertion, PoolRainSupport,
    SubcatchmentConfig, SubcatchmentContributionInput, SubcatchmentDefinition,
    SubcatchmentGeometry, SubcatchmentInput, SubcatchmentParameters, SubcatchmentRouting,
    SubcatchmentState, SupportAtom, SupportPlanInput, SurfaceCellSupport,
};
use hydr::soil::{LandModel, SoilState};
use hydr::solver::{
    BoundaryConditions, ForcingInterval, ForcingSchedule, InitialState, SolverConfig, SolverSetup,
};
use hydr::storage::{
    LevelPoolConfig, LevelPoolCurve, LevelPoolElement, LevelPoolKnot, LevelPoolState,
};
use hydr::structures::{
    DirectionPolicy, StructureCondition, StructureElement, StructureLaw, StructureOperatingPoint,
    SuppressedRectangularWeirParameters, WeirApplicability,
};

type Batch = SurfaceChannelBatch<crate::runtime::Runtime>;
type Error = Box<dyn std::error::Error>;

const DATUM: &str = "Example datum";
/// Plan area of every surface cell, reach cell, subcatchment, and the pond, in m².
const AREA: f64 = 4.0;
/// Rain on every subcatchment, in m/s.
const RAIN: f64 = 2e-5;

/// One impervious subcatchment without depression storage.
fn definition(id: &str) -> Result<SubcatchmentDefinition, Error> {
    Ok(SubcatchmentDefinition {
        id: id.into(),
        parameters: SubcatchmentParameters::new(
            SubcatchmentGeometry {
                total_area_m2: AREA,
                width_m: 2.0,
                slope: 1.0 / 64.0,
                impervious_fraction: 1.0,
                zero_storage_impervious_fraction: 1.0,
                impervious_manning: 1.0 / 32.0,
                pervious_manning: 1.0 / 32.0,
                impervious_depression_m: 0.0,
                pervious_depression_m: 0.0,
            },
            None,
            SubcatchmentRouting::Direct,
        )?,
    })
}

/// The modelled source `name` over its own atom.
fn source(name: &str) -> Contribution {
    Contribution {
        id: format!("{name}-source"),
        input_identity: format!("{name}-input"),
        provenance: "Synthetic rainfall over one subcatchment".into(),
        atoms: vec![name.into()],
        includes: Vec::new(),
        kind: ContributionKind::ModelledRunoff {
            definition_id: format!("{name}-model"),
            outlet_id: format!("{name}-outlet"),
        },
    }
}

/// Surface cell `cell` owned by the source `name`.
fn owned_cell(cell: usize, name: &str) -> SurfaceCellSupport {
    SurfaceCellSupport {
        cell,
        atoms: vec![name.into()],
        rain: CellRainAssignment::Suppressed {
            selected_contribution_id: format!("{name}-source"),
        },
    }
}

/// The outlet of the source `name` into `destination`, free below 3 m.
fn outlet(name: &str, destination: SurfaceChannelRunoffDestination) -> SurfaceChannelRunoffOutlet {
    SurfaceChannelRunoffOutlet {
        outlet_id: format!("{name}-outlet"),
        destination,
        free_outlet: SurfaceChannelFreeOutlet {
            invert_elevation_m: 3.0,
            evidence: "The outfall sits above the highest receiving water".into(),
        },
    }
}

/// Runoff of every subcatchment: north into reach cell 1, south into the pond, east into
/// surface cell 0.
fn runoff() -> Result<SurfaceChannelRunoff, Error> {
    let atom = |id: &str| SupportAtom {
        id: id.into(),
        area_m2: AREA,
    };
    Ok(SurfaceChannelRunoff {
        support: SupportPlanInput {
            partition: PartitionAssertion {
                identity: "example-partition".into(),
                covered_support: "every surface cell and the pond".into(),
                provenance: "Complete disjoint synthetic partition".into(),
                complete_disjoint: true,
            },
            surface_input_identity: "surface-input".into(),
            atoms: vec![
                atom("north"),
                atom("south"),
                atom("east"),
                atom("pond-surface"),
            ],
            contributions: vec![source("north"), source("south"), source("east")],
            replacements: Vec::new(),
            surface_cells: vec![
                owned_cell(0, "north"),
                owned_cell(1, "south"),
                owned_cell(2, "east"),
            ],
            boundaries: Vec::new(),
            pool_rain: vec![PoolRainSupport {
                pool_id: "pond".into(),
                atoms: vec!["pond-surface".into()],
                rain: CellRainAssignment::Direct,
            }],
        },
        definitions: vec![
            definition("east-model")?,
            definition("north-model")?,
            definition("south-model")?,
        ],
        config: SubcatchmentConfig::default(),
        outlets: vec![
            outlet(
                "north",
                SurfaceChannelRunoffDestination::ChannelCells {
                    reach_id: "reach".into(),
                    cells: vec![SurfaceChannelRunoffShare {
                        index: 1,
                        weight: 1.0,
                    }],
                },
            ),
            outlet(
                "south",
                SurfaceChannelRunoffDestination::Pool {
                    pool_id: "pond".into(),
                },
            ),
            outlet(
                "east",
                SurfaceChannelRunoffDestination::SurfaceCells {
                    cells: vec![SurfaceChannelRunoffShare {
                        index: 0,
                        weight: 1.0,
                    }],
                },
            ),
        ],
    })
}

/// A weir from the pond to surface cell 1 with its crest at 1 m.
fn spill() -> Result<SurfaceChannelStructure, Error> {
    let pond = SurfaceChannelNode::Pool("pond".into());
    let cell = SurfaceChannelNode::SurfaceCell(1);
    let attachment = |node: SurfaceChannelNode| SurfaceChannelAttachment {
        node,
        bed_elevation_m: 0.0,
        physical_connection_assertion: "The weir opens onto this node".into(),
    };
    Ok(SurfaceChannelStructure {
        element: StructureElement {
            id: "spill".into(),
            endpoint_a: pond.identity(),
            endpoint_b: cell.identity(),
            datum: DATUM.into(),
            direction: DirectionPolicy::Bidirectional,
            law: StructureLaw::SuppressedRectangularWeir(SuppressedRectangularWeirParameters::new(
                1.0,
                1.0,
                1.7,
                WeirApplicability {
                    coefficient_provenance: "Declared weir coefficient".into(),
                    free_nappe_and_approach_assertion: "Suppressed crest, quiescent approach"
                        .into(),
                    submergence_correction_assertion: "Villemonte correction adopted".into(),
                    symmetric_reversal: true,
                },
            )?),
            condition: StructureCondition::AsBuilt,
        },
        endpoint_a: attachment(pond),
        endpoint_b: attachment(cell),
        scenario: StructureScenario {
            id: "spill-as-built".into(),
            evidence: "As-built geometry, no blockage".into(),
        },
        operation: SurfaceChannelOperationOwner::Manual(StructureOperatingPoint::Passive),
    })
}

/// Setup and original state: a dry 3×1 surface, a still reach at 5 cm, an empty pond, and
/// 1 cm of runoff on each subcatchment.
fn parts() -> Result<(SurfaceChannelSetup, SurfaceChannelInitialState), Error> {
    let domain = Domain::new(
        CartesianGrid::new(3, 1, 2.0, 2.0)?,
        vec![0.0; 3],
        vec![0.0; 3],
        vec![true; 3],
        SpatialReference::new("Example projected CRS".into(), DATUM.into(), [0.0; 2])?,
    )?;
    let boundaries = BoundaryConditions::impermeable_walls();
    let forcing = ForcingSchedule::new(
        &domain,
        &boundaries,
        vec![ForcingInterval {
            start: 0.0,
            end: 1000.0,
            rain: vec![0.0; 3],
            potential_evaporation: vec![0.0; 3],
            withdrawal: vec![0.0; 3],
            boundaries: Vec::new(),
        }],
    )?;
    let land = LandModel::impervious(&domain);
    let soil = SoilState::initialized(&land)?;
    let dry = CellState {
        h: 0.0,
        qx: 0.0,
        qy: 0.0,
    };
    let flow = FlowState::new(&domain, vec![dry; 3])?;
    let cell = |index: usize| RectangularChannelCell {
        id: format!("cell-{index}"),
        length_m: 2.0,
        width_m: 2.0,
        bed_elevation_m: 0.0,
        maximum_depth_m: 4.0,
        manning_n: 0.0,
    };
    let knot = |volume_m3, stage_m| LevelPoolKnot { volume_m3, stage_m };
    let pond = LevelPoolElement {
        id: "pond".into(),
        curve: LevelPoolCurve::new(
            vec![knot(0.0, 0.0), knot(2.0 * AREA, 2.0)],
            DATUM.into(),
            "Constant-area pond".into(),
        )?,
        connected_pool_assertion: "One level water body".into(),
        storage_support_identity: "pond-basin".into(),
    };
    let setup = SurfaceChannelSetup {
        surface: SolverSetup {
            domain,
            land,
            boundaries,
            forcing,
            config: SolverConfig::default(),
        },
        reaches: vec![SurfaceChannelReach {
            id: "reach".into(),
            definition: ChannelReachDefinition {
                vertical_datum: DATUM.into(),
                provenance: "Three walled rectangular cells".into(),
                geometry: ChannelGeometry::Rectangular {
                    cells: (0..3).map(cell).collect(),
                },
                lateral_bindings: Vec::new(),
                config: ChannelConfig::default(),
            },
        }],
        links: Vec::new(),
        ports: Vec::new(),
        pools: vec![pond],
        pool_support: vec![CoupledPoolSupport {
            pool_id: "pond".into(),
            zero_volume_stage_m: 0.0,
            zero_volume_limit_assertion: "The pond empties at its curve origin".into(),
        }],
        pool_sources: None,
        pool_inflows: Vec::new(),
        storage: LevelPoolConfig::default(),
        structures: vec![spill()?],
        pool_ends: Vec::new(),
        operating: None,
        geometry_schedules: Vec::new(),
        pool_curve_schedules: Vec::new(),
        surface_bed_schedules: Vec::new(),
        channel_section_schedules: Vec::new(),
        runoff: Some(runoff()?),
        storage_exclusion_evidence: "The reach and the pond lie outside the surface".into(),
        config: SurfaceChannelConfig {
            head_absolute_m: 1e-12,
            head_relative: 1e-12,
            transfer_absolute_m3: 1e-12,
            transfer_relative: 1e-12,
            mass_absolute_m3: 1e-9,
            mass_relative: 1e-12,
            minimum_step_s: 1e-10,
            maximum_step_s: 1.0,
            iteration_limit: 64,
            retry_limit: 16,
            contraction_target: 0.5,
        },
    };
    let mut wet = SubcatchmentState::initialized(&definition("north-model")?.parameters)?;
    wet.depths_m[0] = 0.01;
    let initial = SurfaceChannelInitialState {
        time_s: 0.0,
        surface: InitialState {
            time: 0.0,
            flow,
            soil,
        },
        reaches: vec![(
            "reach".into(),
            ChannelInitialState {
                time_s: 0.0,
                volume_m3: vec![0.05 * AREA; 3],
                longitudinal_momentum_m4_per_s: vec![0.0; 3],
            },
        )],
        pools: vec![LevelPoolState { volume_m3: 0.0 }],
        runoff: vec![wet; 3],
    };
    Ok((setup, initial))
}

/// Forcing over `[start, end]` with walls at both reach ends and rain on every subcatchment.
fn forcing(start: f64, end: f64) -> SurfaceChannelForcingInterval {
    let record = |name: &str| SubcatchmentContributionInput {
        contribution_id: format!("{name}-source"),
        input_identity: format!("{name}-input"),
        start,
        end,
        input: SubcatchmentInput {
            rain_rate_m_per_s: RAIN,
            potential_evaporation_m_per_s: 0.0,
            external_runon_volumes_m3: [0.0; 3],
        },
    };
    SurfaceChannelForcingInterval {
        start_s: start,
        end_s: end,
        reaches: vec![SurfaceChannelReachForcing {
            reach_id: "reach".into(),
            left: Some(ChannelBoundary::Wall),
            right: Some(ChannelBoundary::Wall),
            lateral_inputs: Vec::new(),
        }],
        pool_sources: None,
        pool_inflows: Vec::new(),
        runoff: vec![record("north"), record("south"), record("east")],
    }
}

/// One accepted interval of 0.25 s.
fn advance(batch: &Batch) -> Result<Batch, Error> {
    let end = batch.time() + 0.25;
    Ok(batch
        .trial_to(end, &forcing(batch.time(), end))?
        .into_next())
}

fn main() -> Result<(), Error> {
    let (setup, initial) = parts()?;
    let mut batch = Batch::new(crate::runtime::client(), setup, initial)?;
    for _ in 0..4 {
        batch = advance(&batch)?;
        let step = batch.last_step()?.ok_or("an accepted interval")?;
        for transfer in &step.runoff {
            println!(
                "t={} outlet={} target={} volume_m3={}",
                step.end_s,
                transfer.outlet_id,
                transfer.target.identity(),
                transfer.volume_m3
            );
        }
        let balance = batch.ledger()?;
        println!(
            "t={} runoff_storage_m3={} runoff_delivered_m3={} residual_m3={} bound_m3={}",
            batch.time(),
            balance.runoff_storage_m3,
            balance.runoff_delivered_m3,
            balance.residual_m3,
            balance.bound_m3
        );
    }
    let data = batch.snapshot_data()?;
    let resumed = Batch::resume(
        crate::runtime::client(),
        &data,
        data.surface.setup.forcing.clone(),
    )?;
    let next = advance(&batch)?;
    let repeated = advance(&resumed)?;
    if next.snapshot_data()? != repeated.snapshot_data()? {
        return Err("the resumed run differs from the uninterrupted run".into());
    }
    println!("resumed run matches at t={}", repeated.time());
    Ok(())
}