Skip to main content

hydr_structures/structures/culvert/inlet/
definition.rs

1//! HDS-5 inlet-control definitions: equation form, coefficients and the mitered flag.
2
3use super::codes::{Unadmitted, lookup};
4use crate::structures::{CulvertBarrelShape, StructureError};
5
6/// Unsubmerged HDS-5 inlet-control equation, with `X = Ku Q / (A D^0.5)` in foot units.
7#[derive(Clone, Copy, Debug, PartialEq, Eq)]
8pub enum CulvertInletForm {
9    /// `HW/D = Hc/D + K X^M + Ks S`, with critical head `Hc = dc + A(dc) / (2 T(dc))`.
10    One,
11    /// `HW/D = K X^M`. The form has no slope term.
12    Two,
13}
14
15/// Admitted HDS-5 inlet: form, coefficients `K`, `M`, `c`, `Y` and the mitered flag.
16///
17/// The submerged equation is `HW/D = c X^2 + Y + Ks S`. `Ks` is `-0.5`, or `+0.7` for an inlet
18/// mitered to the slope.
19#[derive(Clone, Copy, Debug, PartialEq)]
20pub struct CulvertInlet {
21    code: Option<u8>,
22    shape: Option<CulvertBarrelShape>,
23    form: CulvertInletForm,
24    k: f64,
25    m: f64,
26    c: f64,
27    y: f64,
28    mitered: bool,
29}
30
31impl CulvertInlet {
32    /// Admits the HDS-5 Table A.1 or A.2 inlet `code`, numbered as in SWMM `culvert.c`.
33    ///
34    /// | Codes | Barrel shape |
35    /// | --- | --- |
36    /// | 1 to 8 | circular, code 5 mitered |
37    /// | 9 to 26 | concrete box |
38    /// | 30 to 32 | horizontal ellipse |
39    /// | 33 to 35 | vertical ellipse |
40    /// | 36 to 44 | pipe-arch, code 37 mitered |
41    /// | 45 to 47 | flat-floor arch, code 46 mitered |
42    ///
43    /// Rejects codes 27 to 29 (corrugated metal box, which no barrel shape models), codes 48 to
44    /// 57 (tapered inlets, which need throat and face geometry) and every other code.
45    pub fn hds5(code: u8) -> Result<Self, StructureError> {
46        let entry = lookup(code).map_err(|reason| {
47            let action = "an HDS-5 code from 1 to 26 or 30 to 47 for the barrel shape, or explicit coefficients";
48            StructureError::definition(match reason {
49                Unadmitted::MetalBox => format!(
50                    "inlet code={code}: HDS-5 codes 27 to 29 are corrugated metal box inlets, and no barrel shape models a corrugated metal box. Supply {action}"
51                ),
52                Unadmitted::Tapered => format!(
53                    "inlet code={code}: HDS-5 codes 48 to 57 are tapered inlets, which need throat and face geometry. Supply {action}"
54                ),
55                Unadmitted::Unknown => format!("inlet code={code}: supply {action}"),
56            })
57        })?;
58        Ok(Self {
59            code: Some(code),
60            shape: Some(entry.shape),
61            form: entry.form,
62            k: entry.k,
63            m: entry.m,
64            c: entry.c,
65            y: entry.y,
66            mitered: entry.mitered,
67        })
68    }
69
70    /// Admits explicit coefficients for any barrel shape.
71    ///
72    /// Rejects a nonfinite or nonpositive `K`, `M` or `c`, and a nonfinite `Y`. Each of these
73    /// keeps the headwater rising within its band.
74    pub fn explicit(
75        form: CulvertInletForm,
76        [k, m, c, y]: [f64; 4],
77        mitered: bool,
78    ) -> Result<Self, StructureError> {
79        let fields = format!("form={form:?},k={k:?},m={m:?},c={c:?},y={y:?}");
80        for (name, value) in [("K", k), ("M", m), ("c", c)] {
81            if !(value.is_finite() && value > 0.0) {
82                return Err(StructureError::definition(format!(
83                    "{fields}: supply a finite positive inlet coefficient {name}"
84                )));
85            }
86        }
87        if !y.is_finite() {
88            return Err(StructureError::definition(format!(
89                "{fields}: supply a finite inlet coefficient Y"
90            )));
91        }
92        Ok(Self {
93            code: None,
94            shape: None,
95            form,
96            k,
97            m,
98            c,
99            y,
100            mitered,
101        })
102    }
103
104    /// HDS-5 code, or `None` for explicit coefficients.
105    pub fn code(&self) -> Option<u8> {
106        self.code
107    }
108
109    /// Barrel shape a coded inlet requires, or `None` for explicit coefficients.
110    pub fn barrel_shape(&self) -> Option<CulvertBarrelShape> {
111        self.shape
112    }
113
114    /// Unsubmerged equation form.
115    pub fn form(&self) -> CulvertInletForm {
116        self.form
117    }
118
119    /// Coefficients `[K, M, c, Y]`.
120    pub fn coefficients(&self) -> [f64; 4] {
121        [self.k, self.m, self.c, self.y]
122    }
123
124    /// Whether the inlet is mitered to the slope, so `Ks = +0.7`.
125    pub fn mitered(&self) -> bool {
126        self.mitered
127    }
128
129    /// Names the inlet in rejection messages.
130    pub(crate) fn describe(&self) -> String {
131        let coefficients = format!(
132            "form={:?},k={:?},m={:?},c={:?},y={:?},mitered={}",
133            self.form, self.k, self.m, self.c, self.y, self.mitered
134        );
135        match self.code {
136            Some(code) => format!("inlet code={code},{coefficients}"),
137            None => format!("explicit inlet {coefficients}"),
138        }
139    }
140}
141
142#[cfg(test)]
143mod tests {
144    use super::*;
145    use crate::structures::StructureInputErrorKind;
146
147    fn message(result: Result<CulvertInlet, StructureError>) -> Option<String> {
148        match result {
149            Err(StructureError::InvalidInput {
150                kind: StructureInputErrorKind::Definition,
151                context,
152                element: None,
153            }) => Some(context),
154            _ => None,
155        }
156    }
157
158    #[test]
159    fn codes_carry_their_shape_form_and_mitered_flag() -> Result<(), StructureError> {
160        let mitered = CulvertInlet::hds5(5)?;
161        assert!(mitered.mitered());
162        assert_eq!(mitered.barrel_shape(), Some(CulvertBarrelShape::Circular));
163        assert_eq!(mitered.coefficients(), [0.0210, 1.33, 0.0463, 0.75]);
164        let boxed = CulvertInlet::hds5(26)?;
165        assert_eq!(boxed.form(), CulvertInletForm::Two);
166        assert_eq!(boxed.barrel_shape(), Some(CulvertBarrelShape::Box));
167        assert_eq!(boxed.code(), Some(26));
168        let unmitered = (1..=26)
169            .chain(30..=47)
170            .filter(|&code| ![5, 37, 46].contains(&code));
171        for code in unmitered {
172            assert!(!CulvertInlet::hds5(code)?.mitered(), "code {code}");
173        }
174        let shapes = [
175            (30..=32, CulvertBarrelShape::HorizontalEllipse),
176            (33..=35, CulvertBarrelShape::VerticalEllipse),
177            (36..=44, CulvertBarrelShape::PipeArch),
178            (45..=47, CulvertBarrelShape::Arch),
179        ];
180        for (codes, shape) in shapes {
181            for code in codes {
182                let inlet = CulvertInlet::hds5(code)?;
183                assert_eq!(inlet.barrel_shape(), Some(shape), "code {code}");
184                assert_eq!(inlet.form(), CulvertInletForm::One, "code {code}");
185                assert_eq!(inlet.code(), Some(code));
186            }
187        }
188        for code in [37, 46] {
189            assert!(CulvertInlet::hds5(code)?.mitered(), "code {code}");
190        }
191        // Table A.2 Y of chart 34 scale 3, not the 0.53 of section A.3.1.
192        assert_eq!(CulvertInlet::hds5(38)?.coefficients()[3], 0.57);
193        Ok(())
194    }
195
196    #[test]
197    fn admission_names_the_value_and_action() {
198        let cases = [
199            (
200                CulvertInlet::hds5(0),
201                "inlet code=0: supply an HDS-5 code from 1 to 26 or 30 to 47 for the barrel shape",
202            ),
203            (
204                CulvertInlet::hds5(58),
205                "inlet code=58: supply an HDS-5 code from 1 to 26 or 30 to 47",
206            ),
207            (
208                CulvertInlet::hds5(27),
209                "inlet code=27: HDS-5 codes 27 to 29 are corrugated metal box inlets",
210            ),
211            (
212                CulvertInlet::hds5(29),
213                "no barrel shape models a corrugated metal box. Supply an HDS-5 code from 1 to 26 or 30 to 47",
214            ),
215            (
216                CulvertInlet::hds5(48),
217                "inlet code=48: HDS-5 codes 48 to 57 are tapered inlets, which need throat and face geometry",
218            ),
219            (
220                CulvertInlet::hds5(57),
221                "inlet code=57: HDS-5 codes 48 to 57 are tapered inlets",
222            ),
223            (
224                CulvertInlet::explicit(CulvertInletForm::One, [0.0, 2.0, 0.04, 0.7], false),
225                "k=0.0,m=2.0,c=0.04,y=0.7: supply a finite positive inlet coefficient K",
226            ),
227            (
228                CulvertInlet::explicit(CulvertInletForm::Two, [0.5, f64::NAN, 0.04, 0.7], false),
229                "m=NaN,c=0.04,y=0.7: supply a finite positive inlet coefficient M",
230            ),
231            (
232                CulvertInlet::explicit(CulvertInletForm::Two, [0.5, 0.6, -0.04, 0.7], true),
233                "supply a finite positive inlet coefficient c",
234            ),
235            (
236                CulvertInlet::explicit(
237                    CulvertInletForm::One,
238                    [0.5, 0.6, 0.04, f64::INFINITY],
239                    false,
240                ),
241                "y=inf: supply a finite inlet coefficient Y",
242            ),
243        ];
244        for (result, expected) in cases {
245            let text = message(result);
246            assert!(
247                text.as_deref().is_some_and(|t| t.contains(expected)),
248                "{text:?} lacks {expected:?}"
249            );
250        }
251    }
252}