hydr-structures 0.2.0

Hydraulic structures for the hydr simulation crates
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
//! Culvert head sensitivity: a bound on `|dQ/dH| / |Q|` per metre of either head.
//!
//! `|dQ/dH| / Q = 1 / E` for the governing control, with `E = Q dHW/dQ` at the root.
//!
//! | Control | `E` or bound |
//! | --- | --- |
//! | inlet, form 2 unsubmerged | `E = M HW`, since `HW = D K X^M` |
//! | inlet, form 1 unsubmerged | `E >= M D K X^M`, since the critical head rises with `Q` |
//! | inlet, transition | `E = 2 (HW_4 - HW_3.5) X` |
//! | inlet, submerged | `E = 2 D c X^2` |
//! | outlet, drowned full barrel | `Q = A sqrt(2 g dH / k)`, so the bound is `1 / (2 dH)` |
//! | outlet, other | `max(1, \|dHW/dTW\|) / E` from one march, see below |
//!
//! Inlet bounds take the lower end of the discharge enclosure for `X`, and the smallest `E`
//! over the bands a lane may lie in. The inlet ignores the tailwater.
//!
//! The other outlet lanes run one coarse march at the reported discharge and tailwater.
//! [`head_slopes`](outlet::head_slopes) bounds `E` below and `|dHW/dTW|` above in closed form
//! from its class and depths. `dQ/dHW = 1 / (dHW/dQ)` and `dQ/dTW = -(dHW/dTW) / (dHW/dQ)` at
//! fixed other head, so `max(1, |dHW/dTW|) / E` bounds both heads. A tie takes the larger of
//! both controls. Lanes without flow carry zero.
//!
//! Every bound is capped at `2^256` per metre, and `E` is floored at `2^-256` m, so products
//! with volumes and heads stay finite. A lane the march leaves without a slope bound takes the
//! cap: an open, tie or unresolved row, or one whose divisors are not certified positive.

mod closed;
#[cfg(test)]
mod fixture;

use super::Selected;
use super::inlet::{InletDischarge, any};
use super::lanes::Lanes;
use super::outlet::OutletDischarge;
use crate::structures::numerics::culvert::outlet::{self, Accuracy};
use crate::structures::numerics::culvert::{Arithmetic, RowNames};
use crate::structures::tensor::culvert::TensorCulvertLaw;
use crate::structures::{CulvertClient, StructureError};
use closed::{full_sensitivity, inlet_sensitivity};
use numr::dtype::DType;
use numr::ops::{BinaryOps, ConditionalOps};
use numr::runtime::{Runtime, RuntimeClient};
use numr::tensor::Tensor;
use solvr::common::arithmetic::{ArithmeticResult, Interval, with_interval_arithmetic};

/// Smallest `E`, `2^-256` m, so every bound stays at or below [`CAP`].
const E_FLOOR: f64 = f64::from_bits(0x2ff0_0000_0000_0000);

/// Largest bound, `2^256` per metre. Products with volumes and heads stay finite.
const CAP: f64 = f64::from_bits(0x4ff0_0000_0000_0000);

/// 0 or 1 masks of the governing control of each flowing lane.
pub(crate) struct Governs<R: Runtime<DType = DType>> {
    pub(crate) inlet: Tensor<R>,
    pub(crate) outlet: Tensor<R>,
    pub(crate) tie: Tensor<R>,
}

/// Bounds the head sensitivity of every lane. `point` is the per-barrel discharge.
///
/// Also returns the outlet profile bounds of each head alone, zero off outlet profile lanes.
pub(crate) fn bound<R>(
    law: &TensorCulvertLaw<R>,
    rows: &Selected<R>,
    lanes: &Lanes<R>,
    inlet: &InletDischarge<R>,
    outlet_discharge: &OutletDischarge<R>,
    governs: &Governs<R>,
    point: &Tensor<R>,
) -> Result<(Tensor<R>, Profile<R>), StructureError>
where
    R: Runtime<DType = DType>,
    R::Client: CulvertClient<R>,
{
    let barrel = &rows.barrel;
    let c = &barrel.client;
    let one = &barrel.one;
    let inlet_side = c.maximum(&governs.inlet, &governs.tie)?;
    let outlet_side = c.maximum(&governs.outlet, &governs.tie)?;
    let full = c.mul(&outlet_side, &outlet_discharge.full)?;
    let profile_side = c.mul(&outlet_side, &c.sub(one, &outlet_discharge.full)?)?;
    let [inlet_bound, full_bound] =
        with_interval_arithmetic(c, DType::F64, "culvert inlet sensitivity", |a| {
            Ok([
                inlet_sensitivity(a, rows, lanes, inlet, &inlet_side)?,
                full_sensitivity(a, rows, lanes, &full)?,
            ])
        })?;
    let profile = if any(c, &profile_side)? {
        profile_sensitivity(law, rows, lanes, &profile_side, point)?
    } else {
        Profile {
            bound: barrel.zero.clone(),
            head: barrel.zero.clone(),
            tail: barrel.zero.clone(),
        }
    };
    let outlet_bound = c.where_cond(&full, &full_bound, &profile.bound)?;
    let combined = c.where_cond(
        &governs.inlet,
        &inlet_bound,
        &c.where_cond(
            &governs.outlet,
            &outlet_bound,
            &c.maximum(&inlet_bound, &outlet_bound)?,
        )?,
    )?;
    let flowing = c.maximum(&inlet_side, &governs.outlet)?;
    Ok((c.where_cond(&flowing, &combined, &barrel.zero)?, profile))
}

/// `1 / E` upper bounds, with `E` floored at [`E_FLOOR`].
fn reciprocal<R>(a: &mut Arithmetic<'_, R>, e: &Tensor<R>) -> ArithmeticResult<Tensor<R>>
where
    R: Runtime<DType = DType>,
    R::Client: CulvertClient<R>,
{
    let c = a.client();
    let floor = Tensor::<R>::full_scalar(e.shape(), DType::F64, E_FLOOR, c.device())?;
    let safe = c.maximum(e, &floor)?;
    let ones = Tensor::<R>::ones(e.shape(), DType::F64, c.device())?;
    let unit = a.point(&ones)?;
    Ok(a.div_positive(&unit, &safe)?.upper().clone())
}

/// Profile bounds on the `wanted` lanes from one march, zero elsewhere.
fn profile_sensitivity<R>(
    law: &TensorCulvertLaw<R>,
    rows: &Selected<R>,
    lanes: &Lanes<R>,
    wanted: &Tensor<R>,
    point: &Tensor<R>,
) -> Result<Profile<R>, StructureError>
where
    R: Runtime<DType = DType>,
    R::Client: CulvertClient<R>,
{
    let barrel = &rows.barrel;
    let c = &barrel.client;
    let q = c.where_cond(wanted, point, &barrel.rise)?;
    let tailwater = &lanes.tailwater;
    // The bounds read the class and depth enclosures, which a coarse march certifies too.
    let march = outlet::march(
        RowNames::mapped(&law.barrels, &lanes.rows),
        barrel,
        &rows.unit,
        &rows.outlet,
        &q,
        tailwater,
        Accuracy::Coarse,
    )?;
    let operands = outlet::SlopeOperands {
        barrel,
        outlet: &rows.outlet,
        march: &march,
        discharge: &q,
        tailwater,
    };
    let [bound, head, tail] =
        with_interval_arithmetic(c, DType::F64, "culvert outlet sensitivity", |a| {
            let slopes = outlet::head_slopes(a, &operands)?;
            let c = a.client();
            let one = &barrel.one;
            let cap = barrel.constant(CAP)?;
            let gain = c.where_cond(&slopes.known, &slopes.gain, one)?;
            let gain = c.maximum(&gain, &barrel.constant(E_FLOOR)?)?;
            let tail = c.where_cond(&slopes.known, &slopes.tail, one)?;
            let wide = a.point(&c.maximum(&tail, one)?)?;
            let unit = a.point(one)?;
            let alone = a.point(&tail)?;
            let ratios = [
                a.div_positive(&wide, &gain)?,
                a.div_positive(&unit, &gain)?,
                a.div_positive(&alone, &gain)?,
            ];
            let c = a.client();
            let capped = |ratio: &Interval<R>| -> ArithmeticResult<Tensor<R>> {
                Ok(c.where_cond(&slopes.known, &c.minimum(ratio.upper(), &cap)?, &cap)?)
            };
            Ok([
                capped(&ratios[0])?,
                capped(&ratios[1])?,
                capped(&ratios[2])?,
            ])
        })?;
    let keep = |value: &Tensor<R>| c.where_cond(wanted, value, &barrel.zero);
    Ok(Profile {
        bound: keep(&bound)?,
        head: keep(&head)?,
        tail: keep(&tail)?,
    })
}

/// Outlet profile sensitivities on the wanted lanes, each per metre and zero elsewhere.
pub(crate) struct Profile<R: Runtime<DType = DType>> {
    /// `max(1, |dHW/dTW|) / E`, a bound for either head.
    pub(crate) bound: Tensor<R>,
    /// `1 / E`, the bound for the headwater alone.
    pub(crate) head: Tensor<R>,
    /// `|dHW/dTW| / E`, the bound for the tailwater alone.
    pub(crate) tail: Tensor<R>,
}

#[cfg(test)]
mod tests {
    use super::fixture::{Spec, certified, level, pairs, weak};
    use super::{CAP, E_FLOOR};
    use crate::structures::numerics::sensitivity::fixture::{TestResult, grid};
    use crate::structures::{CulvertBarrel, CulvertInlet};

    #[test]
    fn floor_and_cap_are_the_stated_powers_of_two() {
        assert_eq!(E_FLOOR, 2.0_f64.powi(-256));
        assert_eq!(CAP, 2.0_f64.powi(256));
    }

    #[test]
    fn mild_partly_full_circle_bound_holds_over_a_head_grid() -> TestResult {
        // Fall 0.12 m over 60 m. Both heads stay below the 1.2 m crown. Two heads per side
        // hold both outlet classes: profiles falling and rising toward normal depth.
        let spec = Spec {
            barrel: CulvertBarrel::circular(1.2)?,
            length_m: 60.0,
            manning_n: 0.013,
            inverts: [0.12, 0.0],
        };
        let cases = pairs(
            &grid(0.3, 1.3, 2),
            &grid(0.02, 1.15, 2),
            &[0.0, 0.12, 1.2, 1.32],
        );
        assert!(certified(spec, CulvertInlet::hds5(1)?, &cases)? > cases.len() / 4);
        Ok(())
    }

    #[test]
    fn adverse_partly_full_box_bound_holds_over_a_head_grid() -> TestResult {
        // The barrel rises 0.3 m toward its outlet. Every outlet profile rises to the crown. Two
        // heads per side hold it, a headwater on each side of the inlet crown, and both flow
        // directions.
        let spec = Spec {
            barrel: CulvertBarrel::rectangular_box(2.0, 1.0)?,
            length_m: 30.0,
            manning_n: 0.015,
            inverts: [0.0, 0.3],
        };
        let cases = pairs(
            &grid(0.4, 1.6, 2),
            &grid(0.32, 1.25, 2),
            &[0.0, 0.3, 1.0, 1.3],
        );
        assert!(certified(spec, CulvertInlet::hds5(9)?, &cases)? > cases.len() / 4);
        Ok(())
    }

    #[test]
    fn zero_slope_partly_full_box_bound_holds_over_a_head_grid() -> TestResult {
        // Every outlet profile rises to the crown. Three heads per side hold it, a headwater on
        // each side of the crown, and both flow directions. Two would keep no headwater above
        // the crown.
        let spec = level(CulvertBarrel::rectangular_box(2.0, 1.0)?, (100.0, 0.03));
        let cases = pairs(&grid(0.1, 1.4, 3), &grid(0.02, 0.95, 3), &[0.0, 1.0]);
        assert!(certified(spec, CulvertInlet::hds5(9)?, &cases)? > cases.len() / 4);
        Ok(())
    }

    #[test]
    fn steep_drowned_barrels_bound_holds_over_a_head_grid() -> TestResult {
        // Slope 0.02 with the outlet above the crown: full, then free from the crown.
        for (barrel, rise, code) in [
            (CulvertBarrel::rectangular_box(2.0, 1.5)?, 1.5, 9),
            (CulvertBarrel::circular(1.2)?, 1.2, 1),
        ] {
            let spec = Spec {
                barrel,
                length_m: 40.0,
                manning_n: 0.013,
                inverts: [0.8, 0.0],
            };
            let cases = pairs(
                &grid(rise + 1.0, rise + 3.0, 8),
                &grid(rise + 0.02, rise + 0.8, 8),
                &[0.0, 0.8, rise, rise + 0.8],
            );
            assert!(certified(spec, CulvertInlet::hds5(code)?, &cases)? > cases.len() / 4);
        }
        Ok(())
    }

    #[test]
    fn mild_arc_barrels_bound_holds_over_a_head_grid() -> TestResult {
        // Fall 0.12 m over 60 m. The heads span free and full barrels, both flow directions and
        // both controls on a horizontal ellipse, a pipe-arch and a flat-floor arch.
        for (barrel, code) in [
            (
                CulvertBarrel::horizontal_ellipse(1.524, 0.9652, 0.419_475_92)?,
                30,
            ),
            (
                CulvertBarrel::pipe_arch(1.8542, 1.397, 0.4572, 1.938_528)?,
                36,
            ),
            (CulvertBarrel::arch(3.048, 1.6002)?, 45),
        ] {
            let rise = barrel.rise_m();
            let spec = Spec {
                barrel,
                length_m: 60.0,
                manning_n: 0.024,
                inverts: [0.12, 0.0],
            };
            let cases = pairs(
                &grid(0.3, rise + 0.6, 2),
                &grid(0.02, rise + 0.4, 2),
                &[0.0, 0.12, rise, rise + 0.12],
            );
            assert!(certified(spec, CulvertInlet::hds5(code)?, &cases)? > cases.len() / 4);
        }
        Ok(())
    }

    #[test]
    fn steep_box_bound_holds_where_the_barrel_primes() -> TestResult {
        // A free outfall below the 1.5 m crown. The donor heads span critical depth at the crown
        // and the priming discharge, where the free-surface friction at the crown passes the
        // slope and the barrel runs full from the crown.
        let spec = Spec {
            barrel: CulvertBarrel::rectangular_box(2.0, 1.5)?,
            length_m: 40.0,
            manning_n: 0.013,
            inverts: [0.8, 0.0],
        };
        let cases = pairs(
            &grid(4.5, 9.5, 10),
            &grid(0.02, 1.4, 6),
            &[0.0, 0.8, 1.5, 2.3],
        );
        assert!(certified(spec, weak()?, &cases)? > cases.len() / 2);
        Ok(())
    }

    #[test]
    fn floored_and_face_blocked_culverts_bound_holds_over_head_grid() -> TestResult {
        use super::fixture::{certified_with_condition, culvert};
        use crate::structures::{
            BlockageFraction, CulvertBlockage, CulvertSediment, DirectionPolicy, StructureBlockage,
            StructureCondition,
        };
        // Sedimented circle (h = 0.24 m, bed n = 0.030) with 25% entrance face blockage on a long
        // rough barrel so both inlet and outlet control appear.
        let spec = Spec {
            barrel: CulvertBarrel::circular(1.2)?,
            length_m: 100.0,
            manning_n: 0.024,
            inverts: [0.08, 0.0],
        };
        let law = culvert(spec, CulvertInlet::hds5(1)?, false)?;
        let condition = StructureCondition::Blocked(StructureBlockage::culvert(
            CulvertBlockage {
                face_a: BlockageFraction::new(0.25)?,
                face_b: BlockageFraction::NONE,
                sediment: Some(CulvertSediment {
                    depth_m: 0.24,
                    bed_manning_n: 0.030,
                }),
                closed_barrels: 0,
            },
            "Sediment and debris survey".into(),
        )?);
        let cases = pairs(
            &grid(0.5, 2.2, 3),
            &grid(0.3, 1.5, 3),
            &[0.24, 0.32, 1.2, 1.28],
        );
        let outlet = certified_with_condition(law, DirectionPolicy::AtoBOnly, condition, &cases)?;
        assert!(outlet > 0, "expected outlet-controlled lanes");

        // Sedimented box with open-bottom floor replaced by sediment, inlet + outlet regimes.
        let box_spec = Spec {
            barrel: CulvertBarrel::rectangular_box(2.0, 1.5)?,
            length_m: 30.0,
            manning_n: 0.012,
            inverts: [0.06, 0.0],
        };
        let box_law = culvert(box_spec, CulvertInlet::hds5(9)?, false)?;
        let box_cond = StructureCondition::Blocked(StructureBlockage::culvert(
            CulvertBlockage {
                face_a: BlockageFraction::new(0.25)?,
                face_b: BlockageFraction::NONE,
                sediment: Some(CulvertSediment {
                    depth_m: 0.45,
                    bed_manning_n: 0.030,
                }),
                closed_barrels: 0,
            },
            "Sediment survey".into(),
        )?);
        let box_cases = pairs(
            &grid(0.7, 2.4, 3),
            &grid(0.5, 1.8, 3),
            &[0.45, 0.51, 1.5, 1.56],
        );
        certified_with_condition(box_law, DirectionPolicy::AtoBOnly, box_cond, &box_cases)?;
        Ok(())
    }
}