Skip to main content

hydr_structures/structures/culvert/barrel/
floor.rs

1//! Flat beds in barrels: sediment beds and the own floors of boxes and flat-floor arches.
2//!
3//! A bed at depth `h` leaves the open section above it, of rise `D' = D - h`. The admitted
4//! depth is `D - fl(D - h)`, so `D'`, `h` and `h + D' = D` are exact F64 values.
5
6use super::{CulvertBarrel, CulvertBarrelShape, FLOOR_FRACTION, OVERFLOW_MARGIN};
7use crate::structures::StructureError;
8
9/// Flat bed of a barrel: its depth above the invert and its Manning `n` against the walls'.
10///
11/// A sediment bed fills every barrel to one depth along its whole length. A box or a
12/// flat-floor arch also takes depth zero: its own floor with another roughness.
13#[derive(Clone, Copy, Debug, PartialEq)]
14pub struct CulvertFloor {
15    /// Bed depth `h` above the invert, in metres, with `0 <= h < D`.
16    pub sediment_depth_m: f64,
17    /// Bed-to-wall Manning ratio `rho = n_bed / n_wall`, dimensionless.
18    pub roughness_ratio: f64,
19}
20
21impl CulvertBarrel {
22    /// The barrel with `floor`, replacing any floor it had.
23    ///
24    /// Stores the depth `D - fl(D - h)`, which differs from `h` by at most half an F64 step of
25    /// `D - h`. Rejects a nonfinite or negative depth, a depth at or above the rise, which is a
26    /// closed barrel, and a zero depth on a barrel without a floor. Rejects a nonfinite or
27    /// nonpositive ratio, a ratio whose `rho^(3/2)` times the span leaves the F64 range, and an
28    /// open rise whose `2^-30` fraction is subnormal.
29    pub fn with_floor(self, floor: CulvertFloor) -> Result<Self, StructureError> {
30        let base = self.base();
31        let CulvertFloor {
32            sediment_depth_m: depth,
33            roughness_ratio: ratio,
34        } = floor;
35        let rise = base.rise_m;
36        if !depth.is_finite() {
37            return Err(StructureError::definition(format!(
38                "sediment_depth_m={depth:?}: supply a finite sediment depth in metres"
39            )));
40        }
41        if depth < 0.0 {
42            return Err(StructureError::definition(format!(
43                "sediment_depth_m={depth:?}: supply a nonnegative sediment depth in metres"
44            )));
45        }
46        if depth >= rise {
47            return Err(StructureError::definition(format!(
48                "sediment_depth_m={depth:?},rise_m={rise:?}: the barrel is full. Declare StructureCondition::Closed"
49            )));
50        }
51        let open = rise - depth;
52        // Sterbenz: rise - open is exact for open in [rise/2, rise], and depth >= rise/2 is
53        // itself exact, so this depth makes D - h exact.
54        let bed = rise - open;
55        if bed == 0.0
56            && !matches!(
57                base.shape,
58                CulvertBarrelShape::Box | CulvertBarrelShape::Arch
59            )
60        {
61            let what = shape_name(base.shape);
62            return Err(StructureError::definition(if depth == 0.0 {
63                format!(
64                    "sediment_depth_m=0: {what} has no floor. Supply a positive sediment depth or omit the floor"
65                )
66            } else {
67                format!(
68                    "sediment_depth_m={depth:?},rise_m={rise:?}: the sediment depth rounds to zero against the rise. Supply a larger sediment depth or omit the floor"
69                )
70            }));
71        }
72        if !(ratio.is_finite() && ratio > 0.0) {
73            return Err(StructureError::definition(format!(
74                "roughness_ratio={ratio:?}: supply a finite positive bed-to-wall Manning n ratio"
75            )));
76        }
77        let weight = ratio * ratio.sqrt();
78        if !(weight.is_normal() && (weight * base.span_m * OVERFLOW_MARGIN).is_finite()) {
79            return Err(StructureError::definition(format!(
80                "roughness_ratio={ratio:?},span_m={:?}: ratio^(3/2) times the span leaves the F64 range. Supply a ratio nearer 1",
81                base.span_m
82            )));
83        }
84        let smallest = f64::MIN_POSITIVE / FLOOR_FRACTION;
85        if open < smallest {
86            return Err(StructureError::definition(format!(
87                "sediment_depth_m={depth:?},rise_m={rise:?}: the open rise times 2^-30 is subnormal. Supply a smaller sediment depth or declare StructureCondition::Closed"
88            )));
89        }
90        Ok(Self {
91            floor: Some(CulvertFloor {
92                sediment_depth_m: bed,
93                roughness_ratio: ratio,
94            }),
95            ..base
96        })
97    }
98
99    /// The declared floor, with its admitted depth.
100    pub fn floor(&self) -> Option<CulvertFloor> {
101        self.floor
102    }
103
104    /// The barrel without its floor.
105    pub fn base(&self) -> CulvertBarrel {
106        Self {
107            floor: None,
108            ..*self
109        }
110    }
111
112    /// Rise of the open section above the bed, `D - h` in metres. `D` without a floor.
113    pub fn open_rise_m(&self) -> f64 {
114        match self.floor {
115            Some(floor) => self.rise_m - floor.sediment_depth_m,
116            None => self.rise_m,
117        }
118    }
119}
120
121fn shape_name(shape: CulvertBarrelShape) -> &'static str {
122    match shape {
123        CulvertBarrelShape::Circular => "a circular barrel",
124        CulvertBarrelShape::Box => "a box barrel",
125        CulvertBarrelShape::HorizontalEllipse => "a horizontal ellipse",
126        CulvertBarrelShape::VerticalEllipse => "a vertical ellipse",
127        CulvertBarrelShape::PipeArch => "a pipe-arch",
128        CulvertBarrelShape::Arch => "a flat-floor arch",
129    }
130}
131
132#[cfg(test)]
133mod tests {
134    use super::*;
135    use crate::structures::StructureInputErrorKind;
136
137    fn floor(sediment_depth_m: f64, roughness_ratio: f64) -> CulvertFloor {
138        CulvertFloor {
139            sediment_depth_m,
140            roughness_ratio,
141        }
142    }
143
144    fn message(result: Result<CulvertBarrel, StructureError>) -> Option<String> {
145        match result {
146            Err(StructureError::InvalidInput {
147                kind: StructureInputErrorKind::Definition,
148                context,
149                element: None,
150            }) => Some(context),
151            _ => None,
152        }
153    }
154
155    #[test]
156    fn every_floor_rule_rejects_with_its_values_and_action() -> Result<(), StructureError> {
157        let circle = CulvertBarrel::circular(1.2)?;
158        let pipe = CulvertBarrel::pipe_arch(1.8542, 1.397, 0.4572, 1.938_528)?;
159        let boxed = CulvertBarrel::rectangular_box(2.0, 1.5)?;
160        let cases = [
161            (
162                circle.with_floor(floor(1.2, 2.5)),
163                "sediment_depth_m=1.2,rise_m=1.2: the barrel is full. Declare StructureCondition::Closed",
164            ),
165            (circle.with_floor(floor(3.0, 2.5)), "the barrel is full"),
166            (
167                circle.with_floor(floor(0.0, 2.5)),
168                "sediment_depth_m=0: a circular barrel has no floor. Supply a positive sediment depth or omit the floor",
169            ),
170            (
171                pipe.with_floor(floor(-0.0, 2.5)),
172                "sediment_depth_m=0: a pipe-arch has no floor",
173            ),
174            (
175                circle.with_floor(floor(1e-300, 2.5)),
176                "the sediment depth rounds to zero against the rise",
177            ),
178            (
179                circle.with_floor(floor(f64::NAN, 2.5)),
180                "sediment_depth_m=NaN: supply a finite sediment depth in metres",
181            ),
182            (
183                circle.with_floor(floor(-0.1, 2.5)),
184                "sediment_depth_m=-0.1: supply a nonnegative sediment depth in metres",
185            ),
186            (
187                circle.with_floor(floor(0.2, 0.0)),
188                "roughness_ratio=0.0: supply a finite positive bed-to-wall Manning n ratio",
189            ),
190            (
191                boxed.with_floor(floor(0.0, f64::NAN)),
192                "roughness_ratio=NaN: supply a finite positive bed-to-wall Manning n ratio",
193            ),
194            (
195                boxed.with_floor(floor(0.0, 1e250)),
196                "ratio^(3/2) times the span leaves the F64 range",
197            ),
198            (
199                CulvertBarrel::circular(2f64.powi(-990))?
200                    .with_floor(floor(2f64.powi(-990) * 0.875, 1.0)),
201                "the open rise times 2^-30 is subnormal",
202            ),
203        ];
204        for (result, expected) in cases {
205            let text = message(result);
206            assert!(
207                text.as_deref().is_some_and(|t| t.contains(expected)),
208                "{text:?} lacks {expected:?}"
209            );
210        }
211        Ok(())
212    }
213
214    #[test]
215    fn admitted_floor_keeps_an_exact_open_rise() -> Result<(), StructureError> {
216        let circle = CulvertBarrel::circular(1.2)?;
217        let floored = circle.with_floor(floor(0.3, 2.5))?;
218        let Some(admitted) = floored.floor() else {
219            return Err(StructureError::definition("the floor was lost"));
220        };
221        let (bed, open) = (admitted.sediment_depth_m, floored.open_rise_m());
222        assert_eq!(bed + open, 1.2);
223        assert_eq!(1.2 - open, bed);
224        assert!((bed - 0.3).abs() <= 0.5 * (open.next_up() - open));
225        assert_eq!(floored.base(), circle);
226        assert_eq!(circle.open_rise_m(), 1.2);
227        // A box and an arch take their own floor at depth zero, and -0 admits as +0.
228        let boxed = CulvertBarrel::rectangular_box(2.0, 1.5)?.with_floor(floor(-0.0, 2.5))?;
229        let depth = boxed.floor().map(|f| f.sediment_depth_m.to_bits());
230        assert_eq!(depth, Some(0));
231        assert_eq!(boxed.open_rise_m(), 1.5);
232        CulvertBarrel::arch(3.048, 1.6002)?.with_floor(floor(0.0, 0.5))?;
233        // A new floor replaces the old one.
234        let again = floored.with_floor(floor(0.6, 1.0))?;
235        assert_eq!(again.floor().map(|f| f.sediment_depth_m), Some(0.6));
236        assert_eq!(again.base(), circle);
237        Ok(())
238    }
239}