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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
//! Capability declarations checked against identity, documentation, and admission.
//!
//! Each component appends its declaration to `declarations` and adds one module of
//! admission checks.

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

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

#[path = "component_capabilities/solver.rs"]
mod solver;

#[path = "component_capabilities/channel_fixture.rs"]
mod channel_fixture;

#[path = "component_capabilities/channel_variants.rs"]
mod channel_variants;

#[path = "component_capabilities/channel.rs"]
mod channel;

#[allow(dead_code)]
#[path = "level_pool/fixture.rs"]
mod level_pool_fixture;

#[allow(dead_code)]
#[path = "coupled_level_pools/fixture.rs"]
mod coupled_pool_fixture;

#[allow(dead_code)]
#[path = "coupled_pool_inflows/inflow_fixture.rs"]
mod inflow_fixture;

#[allow(dead_code)]
#[path = "channel_pools/reference.rs"]
mod reference;

#[allow(dead_code)]
#[path = "channel_pools/fixture.rs"]
mod channel_pool_fixture;

#[path = "component_capabilities/pools_fixture.rs"]
mod pools_fixture;

#[path = "component_capabilities/pools_variants.rs"]
mod pools_variants;

#[path = "component_capabilities/pools.rs"]
mod pools;

#[allow(dead_code)]
#[path = "land_sources/fixture.rs"]
mod land_fixture;

#[allow(dead_code)]
#[path = "nonlinear_reservoir/fixture.rs"]
mod reservoir_fixture;

#[allow(dead_code)]
#[path = "subcatchment/fixture.rs"]
mod subcatchment_fixture;

#[allow(dead_code)]
#[path = "component_capabilities/runoff_pool_support.rs"]
mod runoff_pools;

#[path = "component_capabilities/runoff_fixture.rs"]
mod runoff_fixture;

#[path = "component_capabilities/runoff_variants.rs"]
mod runoff_variants;

#[path = "component_capabilities/runoff.rs"]
mod runoff;

#[allow(dead_code)]
#[path = "controlled_gate_support/fixture.rs"]
mod gate_fixture;

#[allow(dead_code)]
#[path = "controlled_pump_support/fixture.rs"]
mod pump_fixture;

#[path = "component_capabilities/structures_fixture.rs"]
mod structures_fixture;

#[path = "component_capabilities/structures_variants.rs"]
mod structures_variants;

#[path = "component_capabilities/structures.rs"]
mod structures;

#[allow(dead_code)]
#[path = "component_capabilities/surface_channel_support.rs"]
mod surface_channel_support;

#[path = "component_capabilities/surface_channels_fixture.rs"]
mod surface_channels_fixture;

#[path = "component_capabilities/surface_channels_variants.rs"]
mod surface_channels_variants;

#[path = "component_capabilities/surface_channels.rs"]
mod surface_channels;

#[path = "component_capabilities/surface_channel_ports.rs"]
mod surface_channel_ports;

#[path = "component_capabilities/surface_channel_pools.rs"]
mod surface_channel_pools;

#[path = "component_capabilities/surface_channel_runoff.rs"]
mod surface_channel_runoff;

#[path = "component_capabilities/culvert_barrels.rs"]
mod culvert_barrels;

#[path = "component_capabilities/culvert_inlets.rs"]
mod culvert_inlets;

#[path = "component_capabilities/culvert_outlets.rs"]
mod culvert_outlets;

#[path = "component_capabilities/culverts.rs"]
mod culverts;

#[path = "component_capabilities/drainage.rs"]
mod drainage;
#[path = "component_capabilities/native_pipe.rs"]
mod native_pipe;

#[path = "component_capabilities/declarations.rs"]
mod declarations;
#[path = "component_capabilities/surface_channel_drainage.rs"]
mod surface_channel_drainage;
#[path = "component_capabilities/surface_drainage.rs"]
mod surface_drainage;
#[path = "component_capabilities/surface_f32.rs"]
mod surface_f32;

use declarations::declarations;
use hydr::capability::{ComponentId, RegimeScope, RuntimeKind};
use std::collections::BTreeSet;
use std::path::{Path, PathBuf};

fn root() -> &'static Path {
    Path::new(env!("CARGO_MANIFEST_DIR"))
}

fn is_kebab(id: &str) -> bool {
    !id.is_empty()
        && !id.starts_with('-')
        && !id.ends_with('-')
        && !id.contains("--")
        && id
            .chars()
            .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-')
}

fn rust_sources(directory: &Path, found: &mut Vec<PathBuf>) -> std::io::Result<()> {
    for entry in std::fs::read_dir(directory)? {
        let path = entry?.path();
        if path.is_dir() {
            rust_sources(&path, found)?;
        } else if path.extension().is_some_and(|extension| extension == "rs") {
            found.push(path);
        }
    }
    Ok(())
}

/// Reads every Rust source under `tests/` and `crates/`, with its path.
fn repository_sources() -> std::io::Result<Vec<(PathBuf, String)>> {
    let mut files = Vec::new();
    rust_sources(&root().join("tests"), &mut files)?;
    rust_sources(&root().join("crates"), &mut files)?;
    files
        .into_iter()
        .map(|path| std::fs::read_to_string(&path).map(|source| (path, source)))
        .collect()
}

/// One definition of an evidence function, with the attributes that gate it.
struct Definition<'a> {
    path: &'a Path,
    /// Attribute and comment lines directly above `fn`, plus the file's inner attributes.
    gates: Vec<&'a str>,
}

/// Finds each `fn name(` definition and the attribute lines that gate it.
///
/// A gate counts only on the function's own attribute block or as a file inner attribute.
fn definitions_of<'a>(sources: &'a [(PathBuf, String)], name: &str) -> Vec<Definition<'a>> {
    let needle = format!("fn {name}(");
    let mut found = Vec::new();
    for (path, source) in sources {
        let lines = source.lines().collect::<Vec<_>>();
        let inner = lines
            .iter()
            .copied()
            .filter(|line| line.trim_start().starts_with("#!["));
        for (index, line) in lines.iter().enumerate() {
            let signature = line.trim_start();
            let signature = signature.strip_prefix("pub ").unwrap_or(signature);
            if !signature.starts_with(&needle) {
                continue;
            }
            let mut gates = inner.clone().collect::<Vec<_>>();
            for above in lines[..index].iter().rev() {
                let above = above.trim();
                if above.is_empty() || above.ends_with(['}', ';', '{']) {
                    break;
                }
                gates.push(above);
            }
            found.push(Definition { path, gates });
        }
    }
    found
}

/// Returns the feature gate the runtime's evidence needs: `Some` for a GPU, `None` for CPU.
fn required_gate(runtime: RuntimeKind) -> Result<Option<&'static str>, String> {
    match runtime {
        RuntimeKind::Cpu => Ok(None),
        RuntimeKind::Cuda => Ok(Some("feature = \"cuda\"")),
        RuntimeKind::Wgpu => Ok(Some("feature = \"wgpu\"")),
        other => Err(format!(
            "runtime `{}` has no evidence gate rule. Add one to `required_gate`",
            other.id()
        )),
    }
}

#[test]
fn evidence_gates_come_from_the_attribute_block_only() {
    let source = "fn helper() {}\n\n#[cfg(feature = \"cuda\")]\n#[test]\nfn gated() -> Result {\n}\n\n#[test]\npub fn plain() {\n    let _ = \"#[cfg(feature = \\\"wgpu\\\")]\";\n}\n";
    let sources = vec![(PathBuf::from("synthetic.rs"), source.to_string())];
    let gated = definitions_of(&sources, "gated");
    assert_eq!(gated.len(), 1);
    assert!(gated[0].gates.contains(&"#[cfg(feature = \"cuda\")]"));
    assert!(gated[0].gates.contains(&"#[test]"));
    let plain = definitions_of(&sources, "plain");
    assert_eq!(plain.len(), 1);
    assert_eq!(plain[0].gates, ["#[test]"]);
    assert!(definitions_of(&sources, "missing").is_empty());
    let inner = vec![(
        PathBuf::from("inner.rs"),
        "#![cfg(feature = \"wgpu\")]\nuse x;\n\n#[test]\nfn whole() {}\n".to_string(),
    )];
    let whole = definitions_of(&inner, "whole");
    assert_eq!(whole.len(), 1);
    assert!(whole[0].gates.contains(&"#![cfg(feature = \"wgpu\")]"));
}

/// Each runtime claim names exactly one test, gated by the claimed runtime's feature.
///
/// CUDA evidence carries `cfg(feature = "cuda")` and WGPU evidence `cfg(feature = "wgpu")`.
/// CPU evidence carries neither, so a default build runs it.
#[test]
fn runtime_claims_name_existing_tests() -> Result<(), Box<dyn std::error::Error>> {
    let sources = repository_sources()?;
    let gpu_gates = [RuntimeKind::Cuda, RuntimeKind::Wgpu]
        .into_iter()
        .map(required_gate)
        .collect::<Result<Vec<_>, _>>()?;
    let mut errors = Vec::new();
    for entry in declarations() {
        let id = entry.capabilities.component.id();
        for claim in entry.capabilities.runtimes {
            let (runtime, evidence) = (claim.runtime.id(), claim.evidence);
            let definitions = definitions_of(&sources, evidence);
            let [definition] = definitions.as_slice() else {
                errors.push(format!(
                    "`{id}` {runtime} evidence `{evidence}` has {} definitions. Define it once under tests/ or crates/",
                    definitions.len()
                ));
                continue;
            };
            let place = definition
                .path
                .strip_prefix(root())
                .unwrap_or(definition.path);
            let place = place.display();
            let has = |gate: &str| definition.gates.iter().any(|line| line.contains(gate));
            if !definition.gates.contains(&"#[test]") {
                errors.push(format!(
                    "`{id}` {runtime} evidence `{evidence}` in {place} is not a #[test]. Name a test function"
                ));
            }
            match required_gate(claim.runtime)? {
                Some(gate) if !has(gate) => errors.push(format!(
                    "`{id}` {runtime} evidence `{evidence}` in {place} lacks #[cfg({gate})]. Gate the test on its runtime feature"
                )),
                None if gpu_gates.iter().flatten().any(|gate| has(gate)) => {
                    errors.push(format!(
                        "`{id}` {runtime} evidence `{evidence}` in {place} carries a GPU feature gate. Name an ungated CPU test"
                    ))
                }
                _ => {}
            }
        }
    }
    assert!(errors.is_empty(), "{}", errors.join("\n"));
    Ok(())
}

#[test]
fn declarations_are_unique_and_identified() -> Result<(), Box<dyn std::error::Error>> {
    let declared = declarations();
    let mut components = BTreeSet::new();
    for entry in &declared {
        let id = entry.capabilities.component.id();
        assert!(components.insert(id), "`{id}` is declared twice");
    }
    for entry in &declared {
        let capabilities = entry.capabilities;
        let id = capabilities.component.id();
        assert_eq!(capabilities.contract, entry.contract, "`{id}` contract");
        assert_eq!(
            capabilities.snapshot_schema, entry.snapshot_schema,
            "`{id}` snapshot schema"
        );
        let mut ids = BTreeSet::new();
        for record in capabilities
            .unsupported
            .iter()
            .map(|u| (u.id, u.doc))
            .chain(capabilities.limits.iter().map(|l| (l.id, l.doc)))
        {
            assert!(
                is_kebab(record.0),
                "`{id}`: `{}` is not kebab-case",
                record.0
            );
            assert!(ids.insert(record.0), "`{id}`: `{}` appears twice", record.0);
            assert!(
                root().join(record.1).is_file(),
                "`{id}`: `{}` names missing doc `{}`",
                record.0,
                record.1
            );
        }
        for option in capabilities.options {
            assert!(is_kebab(option.id), "`{id}`: option `{}`", option.id);
            assert!(
                ids.insert(option.id),
                "`{id}`: `{}` appears twice",
                option.id
            );
            let mut values = BTreeSet::new();
            for value in option.values {
                assert!(is_kebab(value), "`{id}` `{}`: value `{value}`", option.id);
                assert!(
                    values.insert(*value),
                    "`{id}` `{}`: `{value}` twice",
                    option.id
                );
            }
            if let Some(default) = option.default {
                assert!(
                    values.contains(default),
                    "`{id}` `{}`: default `{default}` is not a value",
                    option.id
                );
            }
        }
        for linked in capabilities.nested.iter().chain(capabilities.attachable_to) {
            assert!(
                components.contains(linked.id()),
                "`{id}` links undeclared component `{}`",
                linked.id()
            );
        }
        let find = |component: &hydr::capability::ComponentId| {
            declared
                .iter()
                .map(|d| d.capabilities)
                .find(|c| c.component == *component)
                .ok_or(format!("`{id}` links undeclared `{}`", component.id()))
        };
        // A parent runs its nested components, so it needs their arithmetic too.
        for nested in capabilities.nested {
            let child = find(nested)?;
            for requirement in child.arithmetic {
                assert!(
                    capabilities.arithmetic.contains(requirement),
                    "`{id}` omits `{}` arithmetic `{}`",
                    nested.id(),
                    requirement.id()
                );
            }
            assert!(
                child.attachable_to.contains(&capabilities.component),
                "`{}` omits attachment to `{id}`",
                nested.id()
            );
        }
        for parent in capabilities.attachable_to {
            assert!(
                find(parent)?.nested.contains(&capabilities.component),
                "`{}` does not nest `{id}`",
                parent.id()
            );
        }
        for regime in capabilities.regimes {
            if let RegimeScope::Boundary(kind) = regime.scope {
                assert!(
                    capabilities.endpoints.iter().any(|e| e.admits(kind)),
                    "`{id}` claims a regime at unadmitted boundary `{}`",
                    kind.id()
                );
            }
        }
    }
    Ok(())
}

/// Components that a runtime without correctly rounded elementary functions cannot run.
///
/// Each calls numr `CorrectlyRoundedOps` or the host `correctly_rounded` functions, directly or
/// through a nested component, or needs a `StructureBatch` that does.
const CORRECTLY_ROUNDED_CALLERS: &[ComponentId] = &[
    // Surface Manning `cr_pow_scalar(depth, 7/3)`, and nested soil.
    ComponentId::SurfaceSolver,
    // Reach Manning `cr_pow_scalar(radius, 4/3)`.
    ComponentId::ChannelReachRectangular,
    // Star-depth `cr_log` / `cr_exp` and reach Manning.
    ComponentId::ChannelReachTriangular,
    // Surveyed Manning `cr_pow_scalar(radius, 4/3)`.
    ComponentId::ChannelReachSurveyed,
    // Green-Ampt Newton step `cr_log`.
    ComponentId::Soil,
    // Drainage `cr_pow_scalar` and the host SI coefficient `pow`.
    ComponentId::Reservoir,
    // Weir and orifice log-scale caches and `cr_exp` discharge.
    ComponentId::CoupledLevelPools,
    // Needs a `StructureBatch`, whose construction runs `cr_log` and the correctly rounded probe.
    ComponentId::ControlledGate,
    ComponentId::OperatingControl,
    ComponentId::GateActuator,
    ComponentId::ControlledPump,
    // Interval `acos` for circle angles and interval `log` in the critical and normal residuals.
    ComponentId::CulvertBarrel,
    // Interval `log` and `exp` for the inlet powers, and the barrel's critical depth.
    ComponentId::CulvertInlet,
    // Interval `acos`, `log` and `exp` in the profile integrand and the barrel depth solves.
    ComponentId::CulvertOutlet,
    // Inlet and outlet curves of each direction, and the form-1 critical-depth solve.
    ComponentId::Culvert,
    // Bank weir, host stall forecast `log`, and nested components.
    ComponentId::SurfaceChannels,
    // Nests `SurfaceSolver` / `SurfaceChannels`.
    ComponentId::SurfaceDrainage,
    ComponentId::SurfaceChannelDrainage,
    // Nested callers only.
    ComponentId::Subcatchment,
    ComponentId::ChannelPools,
    ComponentId::RunoffPools,
];

#[test]
fn correctly_rounded_elementary_is_declared_by_exactly_its_callers() {
    use hydr::capability::ArithmeticRequirement as A;
    for entry in declarations() {
        let capabilities = entry.capabilities;
        let id = capabilities.component.id();
        let declares = capabilities
            .arithmetic
            .contains(&A::CorrectlyRoundedElementary);
        let calls = CORRECTLY_ROUNDED_CALLERS.contains(&capabilities.component);
        assert_eq!(
            declares, calls,
            "`{id}`: declares correctly-rounded-elementary={declares}, calls it={calls}"
        );
        if declares {
            // The numr family needs IEEE basic arithmetic and fused multiply-add.
            for needed in [
                A::F64Ieee754Basic,
                A::NearestEvenGradualUnderflow,
                A::FusedMulAdd,
            ] {
                assert!(
                    capabilities.arithmetic.contains(&needed),
                    "`{id}` declares correctly-rounded-elementary without `{}`",
                    needed.id()
                );
            }
        }
    }
}

#[test]
fn reference_runtime_meets_correctly_rounded_elementary() {
    assert!(hydr_core::tensor::probe_correctly_rounded::<runtime::Runtime>().is_none());
}