hydr 0.3.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
//! Actual runtime execution and arithmetic admission through public components.

#[path = "runtime_backends/channel_boundaries.rs"]
mod channel_boundaries;
#[path = "runtime_backends/channel_pools.rs"]
mod channel_pools;
#[path = "runtime_backends/channel_reservoir.rs"]
mod channel_reservoir;
#[path = "runtime_backends/channel_tensor_forcing.rs"]
mod channel_tensor_forcing;
#[path = "runtime_backends/channels.rs"]
mod channels;
#[path = "runtime_backends/control_fixture.rs"]
mod control_fixture;
#[path = "runtime_backends/controlled_gates.rs"]
mod controlled_gates;
#[path = "runtime_backends/controlled_pumps.rs"]
mod controlled_pumps;
#[path = "runtime_backends/coupled_level_pool_culverts.rs"]
mod coupled_level_pool_culverts;
#[path = "coupled_level_pools/fixture.rs"]
mod coupled_pool_fixture;
#[allow(dead_code)]
#[path = "culvert_barrels/critical_rows.rs"]
mod critical_rows;
#[path = "cross_sections/profiles.rs"]
mod cross_section_profiles;
#[path = "runtime_backends/cross_sections.rs"]
mod cross_sections;
#[path = "runtime_backends/culvert_barrels.rs"]
mod culvert_barrels;
#[allow(dead_code)]
#[path = "culvert_inlets/lanes.rs"]
mod culvert_inlet_lanes;
#[path = "runtime_backends/culvert_inlets.rs"]
mod culvert_inlets;
#[allow(dead_code)]
#[path = "culvert_barrels/lanes.rs"]
mod culvert_lanes;
#[allow(dead_code)]
#[path = "culvert_laws/lanes.rs"]
mod culvert_law_lanes;
#[path = "runtime_backends/culvert_laws.rs"]
mod culvert_laws;
#[allow(dead_code)]
#[path = "culvert_outlets/lanes.rs"]
mod culvert_outlet_lanes;
#[path = "runtime_backends/culvert_outlets.rs"]
mod culvert_outlets;
#[allow(dead_code)]
#[path = "culvert_shapes/backend_lanes.rs"]
mod culvert_shapes;
#[allow(dead_code)]
#[path = "surface_channels/culverts_fixture.rs"]
mod culverts_fixture;
#[allow(dead_code)]
#[path = "culvert_barrels/dense_rows.rs"]
mod dense_rows;
#[path = "runtime_backends/device_checks.rs"]
mod device_checks;
#[path = "runtime_backends/gate_actuators.rs"]
mod gate_actuators;
#[path = "coupled_pool_inflows/inflow_fixture.rs"]
mod inflow_fixture;
#[path = "runtime_backends/inflows.rs"]
mod inflows;
#[allow(dead_code)]
#[path = "culverts/inlet_rows.rs"]
mod inlet_rows;
#[path = "runtime_backends/interval_volumes.rs"]
mod interval_volumes;
#[path = "runtime_backends/level_pool.rs"]
mod level_pool;
#[allow(dead_code)]
#[path = "culvert_barrels/normal_rows.rs"]
mod normal_rows;
#[path = "runtime_backends/operating_controls.rs"]
mod operating_controls;
#[allow(dead_code)]
#[path = "culverts/outlet_rows.rs"]
mod outlet_rows;
#[allow(dead_code, unused_imports)]
#[path = "surface_channels/pools_fixture.rs"]
mod pools_fixture;
#[allow(dead_code)]
#[path = "culvert_barrels/reference_rows.rs"]
mod reference_rows;
#[path = "runtime_backends/reservoir.rs"]
mod reservoir;
#[allow(dead_code)]
#[path = "surface_channels/runoff_fixture.rs"]
mod runoff_fixture;
#[path = "runtime_backends/runoff_pools.rs"]
mod runoff_pools;
#[path = "support/runtime.rs"]
mod runtime;
#[path = "runtime_backends/structure_laws.rs"]
mod structure_laws;
#[path = "runtime_backends/subcatchment.rs"]
mod subcatchment;
#[allow(dead_code)]
#[path = "culvert_laws/support.rs"]
mod support;
#[path = "runtime_backends/surface_channel_culverts.rs"]
mod surface_channel_culverts;
#[path = "runtime_backends/surface_channel_pools.rs"]
mod surface_channel_pools;
#[path = "runtime_backends/surface_channel_runoff.rs"]
mod surface_channel_runoff;
#[path = "runtime_backends/surface_channels.rs"]
mod surface_channels;
#[path = "runtime_backends/surveyed_endpoints.rs"]
mod surveyed_endpoints;
#[path = "runtime_backends/triangular_channels.rs"]
mod triangular_channels;

use hydr::soil::{
    ModifiedGreenAmptBatch, ModifiedGreenAmptParameters, NativeSoilInput, NativeSoilState,
};
type Result<T = ()> = std::result::Result<T, Box<dyn std::error::Error>>;

#[cfg(feature = "wgpu")]
use device_checks::{f64_rejection, wgpu_client};
#[cfg(feature = "cuda")]
use device_checks::{same_bits, same_states_bounded_data};

macro_rules! dry_soil_execution {
    ($runtime:ty, $client:expr) => {{
        let parameters = ModifiedGreenAmptParameters::from_native(0.4, 1e-5, 0.3)?;
        let initial = NativeSoilState {
            imd: 0.2,
            f_ft: 0.01,
            fu_ft: 0.02,
            saturated: true,
            t_s: 5.0,
        };
        let batch = ModifiedGreenAmptBatch::<$runtime>::from_native_states(
            $client,
            &[parameters],
            &[initial],
        )?;
        let trial = batch.trial_native(
            60.0,
            &[NativeSoilInput {
                incoming_rate_ft_per_s: 0.0,
                ponded_depth_ft: 0.0,
            }],
        )?;
        assert_eq!(trial.outputs()?[0].native_rate_ft_per_s, 0.0);
        assert_eq!(
            trial.into_next().native_states()?,
            vec![NativeSoilState {
                t_s: -55.0,
                ..initial
            }]
        );
        assert_eq!(batch.native_states()?, vec![initial]);
    }};
}

#[test]
fn selected_cpu_client_executes_soil_trial_without_mutating_accepted_state() -> Result {
    dry_soil_execution!(runtime::Runtime, runtime::client());
    Ok(())
}

#[cfg(feature = "cuda")]
#[test]
fn selected_cuda_client_executes_soil_trial_without_mutating_accepted_state() -> Result {
    use numr::runtime::cuda::{CudaClient, CudaDevice, CudaRuntime};
    let client = CudaClient::new(CudaDevice::new(0))?;
    dry_soil_execution!(CudaRuntime, client);
    Ok(())
}

/// Still water at depth one on a closed 2 x 2 grid, with the expected cells.
fn still_water_case() -> Result<(
    hydr::solver::SolverSetup,
    hydr::solver::InitialState,
    Vec<hydr::model::CellState>,
)> {
    use hydr::grid::CartesianGrid;
    use hydr::model::{CellState, Domain, FlowState, SpatialReference};
    use hydr::soil::{LandModel, SoilState};
    use hydr::solver::{
        BoundaryConditions, ForcingInterval, ForcingSchedule, InitialState, SolverConfig,
        SolverSetup,
    };

    let grid = CartesianGrid::new(2, 2, 1.0, 1.0)?;
    let count = grid.cell_count();
    let reference = SpatialReference::new(
        "synthetic metric plane".into(),
        "synthetic datum".into(),
        [0.0, 0.0],
    )?;
    let domain = Domain::new(
        grid,
        vec![0.0; count],
        vec![0.0; count],
        vec![true; count],
        reference,
    )?;
    let cells = vec![
        CellState {
            h: 1.0,
            qx: 0.0,
            qy: 0.0
        };
        count
    ];
    let flow = FlowState::new(&domain, cells.clone())?;
    let boundaries = BoundaryConditions::impermeable_walls();
    let forcing = ForcingSchedule::new(
        &domain,
        &boundaries,
        vec![ForcingInterval {
            start: 0.0,
            end: 2.0,
            rain: vec![0.0; count],
            potential_evaporation: vec![0.0; count],
            withdrawal: vec![0.0; count],
            boundaries: vec![],
        }],
    )?;
    let land = LandModel::impervious(&domain);
    let soil = SoilState::initialized(&land)?;
    let setup = SolverSetup {
        domain,
        land,
        boundaries,
        forcing,
        config: SolverConfig::default(),
    };
    let initial = InitialState {
        time: 0.0,
        flow,
        soil,
    };
    Ok((setup, initial, cells))
}

macro_rules! still_water_execution {
    ($runtime:ty, $client:expr) => {{
        use hydr::solver::Solver;

        let (setup, initial, cells) = still_water_case()?;
        let mut solver = Solver::<$runtime>::new($client, setup, initial)?;
        assert_eq!(solver.advance_to(0.5, 1_000)?.accepted_time, 0.5);
        let checkpoint = solver.snapshot();
        let checkpoint_data = checkpoint.data()?;
        assert_eq!(solver.state()?.cells(), cells.as_slice());
        assert_eq!(solver.advance_to(1.0, 1_000)?.accepted_time, 1.0);
        let continued = solver.snapshot().data()?;
        let continued_state = solver.state()?;
        assert_eq!(continued_state.cells(), cells.as_slice());
        assert_eq!(solver.ledger().storage, 4.0);
        assert_eq!(solver.ledger().residual, 0.0);
        solver.restore(&checkpoint)?;
        assert_eq!(solver.snapshot().data()?, checkpoint_data);
        assert_eq!(solver.advance_to(1.0, 1_000)?.accepted_time, 1.0);
        assert_eq!(solver.snapshot().data()?, continued);
        assert_eq!(solver.state()?, continued_state);
        assert_eq!(solver.ledger().storage, 4.0);
        assert_eq!(solver.ledger().residual, 0.0);
    }};
}

#[test]
fn selected_cpu_client_preserves_still_water_and_trusted_restart() -> Result {
    still_water_execution!(runtime::Runtime, runtime::client());
    Ok(())
}

#[cfg(feature = "cuda")]
#[test]
fn selected_cuda_client_preserves_still_water_and_trusted_restart() -> Result {
    use numr::runtime::cuda::{CudaClient, CudaDevice, CudaRuntime};
    still_water_execution!(CudaRuntime, CudaClient::new(CudaDevice::new(0))?);
    Ok(())
}

macro_rules! coupled_pool_execution {
    ($runtime:ty, $client:expr) => {{
        use hydr::coupling::CoupledLevelPoolBatch;

        let (setup, initial) = coupled_pool_fixture::orifice_case([4.0, 2.0])?;
        let batch = CoupledLevelPoolBatch::<$runtime>::new($client, setup, initial)?;
        let original = batch.snapshot_data()?;
        let mut next = batch.trial_to(0.25)?.into_next();
        assert_eq!(batch.snapshot_data()?, original);
        assert_eq!(next.time(), 0.25);
        let checkpoint = next.snapshot();
        let checkpoint_data = checkpoint.data()?;
        next = next.trial_to(0.5)?.into_next();
        let continued = next.snapshot_data()?;
        let volumes = next.pools().states()?;
        coupled_pool_fixture::near(volumes.iter().map(|pool| pool.volume_m3).sum(), 6.0);
        assert!(volumes[0].volume_m3 < 4.0);
        assert!(volumes[1].volume_m3 > 2.0);
        next.restore(&checkpoint)?;
        assert_eq!(next.snapshot_data()?, checkpoint_data);
        next = next.trial_to(0.5)?.into_next();
        assert_eq!(next.snapshot_data()?, continued);
    }};
}

#[test]
fn selected_cpu_client_executes_coupled_pool_exchange_and_trusted_restart() -> Result {
    coupled_pool_execution!(runtime::Runtime, runtime::client());
    Ok(())
}

#[cfg(feature = "cuda")]
#[test]
fn selected_cuda_client_executes_coupled_pool_exchange_and_trusted_restart() -> Result {
    use numr::runtime::cuda::{CudaClient, CudaDevice, CudaRuntime};
    coupled_pool_execution!(CudaRuntime, CudaClient::new(CudaDevice::new(0))?);
    Ok(())
}

macro_rules! coupled_pool_source_execution {
    ($runtime:ty, $client:expr) => {{
        use hydr::coupling::{CoupledLevelPoolBatch, CoupledLevelPoolForcingInterval};
        use hydr::storage::{LevelPoolSourcePolicy, LevelPoolSources};

        let (mut setup, initial) = coupled_pool_fixture::orifice_case([4.0, 2.0])?;
        let policies = vec![
            LevelPoolSourcePolicy {
                dry_basal_footprint_assertion:
                    "Synthetic first segment is the rain-receiving basal footprint".into(),
                bottom_loss_provenance: "Explicit zero bottom demand without inferred soil state"
                    .into(),
            };
            2
        ];
        let forcing = CoupledLevelPoolForcingInterval {
            start_s: 0.0,
            end_s: 1.0,
            sources: Some(vec![
                LevelPoolSources {
                    rain_rate_m_per_s: 0.25,
                    potential_evaporation_rate_m_per_s: 0.0,
                    bottom_loss_rate_m3_per_s: 0.0,
                };
                2
            ]),
            inflows: vec![],
        };
        setup.source_policies = Some(policies);
        let batch = CoupledLevelPoolBatch::<$runtime>::new($client, setup, initial)?;
        let original = batch.snapshot_data()?;
        let mut next = batch.trial_with_forcing_to(0.25, &forcing)?.into_next();
        assert_eq!(batch.snapshot_data()?, original);
        assert_eq!(next.time(), 0.25);
        let dt: f64 = 0.25;
        let transfer = dt * 0.5 * ((dt * dt + 8.0).sqrt() - dt);
        let states = next.pools().states()?;
        coupled_pool_fixture::near(states[0].volume_m3, 4.0 + 0.25 * dt - transfer);
        coupled_pool_fixture::near(states[1].volume_m3, 2.0 + 0.25 * dt + transfer);
        let checkpoint = next.snapshot();
        let checkpoint_data = checkpoint.data()?;
        next = next.trial_with_forcing_to(0.5, &forcing)?.into_next();
        let continued = next.snapshot_data()?;
        let totals = next.pools().totals()?;
        coupled_pool_fixture::near(totals.volume_m3, 6.25);
        coupled_pool_fixture::near(
            totals
                .sources
                .ok_or("Missing runtime source counters")?
                .cumulative_rain_volume_m3,
            0.25,
        );
        next.restore(&checkpoint)?;
        assert_eq!(next.snapshot_data()?, checkpoint_data);
        next = next.trial_with_forcing_to(0.5, &forcing)?.into_next();
        assert_eq!(next.snapshot_data()?, continued);
    }};
}

#[test]
fn selected_cpu_client_executes_coupled_sources_and_trusted_restart() -> Result {
    coupled_pool_source_execution!(runtime::Runtime, runtime::client());
    Ok(())
}

#[cfg(feature = "cuda")]
#[test]
fn selected_cuda_client_executes_coupled_sources_and_trusted_restart() -> Result {
    use numr::runtime::cuda::{CudaClient, CudaDevice, CudaRuntime};
    coupled_pool_source_execution!(CudaRuntime, CudaClient::new(CudaDevice::new(0))?);
    Ok(())
}

#[cfg(feature = "wgpu")]
#[test]
fn selected_wgpu_client_rejects_f64_profile() -> Result {
    use numr::dtype::DType;
    use numr::runtime::Runtime;
    use numr::runtime::wgpu::{WgpuClient, WgpuDevice, WgpuRuntime};
    assert!(!WgpuRuntime::computes_float_dtype(DType::F64));
    let client = WgpuClient::new(WgpuDevice::new(0))?;
    let parameters = ModifiedGreenAmptParameters::from_native(0.4, 1e-5, 0.3)?;
    let error = ModifiedGreenAmptBatch::<WgpuRuntime>::initialized(client, &[parameters])
        .err()
        .ok_or("WGPU admitted unsupported F64 profile")?;
    assert!(error.to_string().contains("F64"));
    Ok(())
}

#[cfg(feature = "wgpu")]
#[test]
fn selected_wgpu_rejects_surface_solver_f64_without_fallback() -> Result {
    use hydr::solver::{EngineError, Solver};
    use numr::runtime::wgpu::WgpuRuntime;
    let (setup, initial, _) = still_water_case()?;
    let error = f64_rejection(
        Solver::<WgpuRuntime>::new(wgpu_client()?, setup, initial),
        "surface solver",
    )?;
    let EngineError::Rejected { operation, .. } = error else {
        return Err(format!("Unexpected WGPU surface rejection: {error}").into());
    };
    assert_eq!(operation, "arithmetic environment");
    Ok(())
}

#[cfg(feature = "wgpu")]
#[test]
fn selected_wgpu_rejects_coupled_level_pools_f64_without_fallback() -> Result {
    use hydr::coupling::{CoupledLevelPoolBatch, CoupledLevelPoolError};
    use numr::runtime::wgpu::WgpuRuntime;
    let (setup, initial) = coupled_pool_fixture::orifice_case([4.0, 2.0])?;
    let error = f64_rejection(
        CoupledLevelPoolBatch::<WgpuRuntime>::new(wgpu_client()?, setup, initial),
        "coupled level pool",
    )?;
    assert!(
        matches!(
            error,
            CoupledLevelPoolError::Pool(_) | CoupledLevelPoolError::Structure(_)
        ),
        "Unexpected WGPU coupled pool rejection: {error}"
    );
    Ok(())
}