Skip to main content

hydr_structures/structures/geometry/
knot.rs

1//! Knots and parameter selectors for piecewise-linear and step structure geometry schedules.
2
3/// Continuous scalar geometry parameter of a structure that a schedule can vary linearly or by step.
4#[non_exhaustive]
5#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
6pub enum ScalarGeometryParameter {
7    /// Suppressed rectangular weir crest elevation, in metres.
8    WeirCrestElevation,
9    /// Suppressed rectangular weir crest length, in metres.
10    WeirCrestLength,
11    /// Suppressed rectangular weir blocked crest-length share in `[0, 1)`.
12    WeirBlockage,
13    /// Submerged orifice opening area, in m².
14    OrificeArea,
15    /// Submerged orifice sill elevation, in metres.
16    OrificeSillElevation,
17    /// Submerged orifice crown elevation, in metres.
18    OrificeCrownElevation,
19    /// Submerged orifice blocked area share in `[0, 1)`.
20    OrificeBlockage,
21    /// Hydrostatic rectangular gate aperture width, in metres.
22    GateWidth,
23    /// Hydrostatic rectangular gate sill elevation, in metres.
24    GateSillElevation,
25    /// Hydrostatic rectangular gate maximum opening, in metres.
26    GateMaximumOpening,
27    /// Hydrostatic rectangular gate blocked width share in `[0, 1)`.
28    GateWidthBlockage,
29    /// Hydrostatic rectangular gate blocked opening-range share in `[0, 1)`.
30    GateOpeningRangeBlockage,
31    /// Culvert entrance face A blocked share in `[0, 1)`.
32    CulvertFaceABlockage,
33    /// Culvert entrance face B blocked share in `[0, 1)`.
34    CulvertFaceBBlockage,
35}
36
37impl ScalarGeometryParameter {
38    /// Human-readable name for error messages.
39    pub(crate) fn name(self) -> &'static str {
40        match self {
41            Self::WeirCrestElevation => "WeirCrestElevation",
42            Self::WeirCrestLength => "WeirCrestLength",
43            Self::WeirBlockage => "WeirBlockage",
44            Self::OrificeArea => "OrificeArea",
45            Self::OrificeSillElevation => "OrificeSillElevation",
46            Self::OrificeCrownElevation => "OrificeCrownElevation",
47            Self::OrificeBlockage => "OrificeBlockage",
48            Self::GateWidth => "GateWidth",
49            Self::GateSillElevation => "GateSillElevation",
50            Self::GateMaximumOpening => "GateMaximumOpening",
51            Self::GateWidthBlockage => "GateWidthBlockage",
52            Self::GateOpeningRangeBlockage => "GateOpeningRangeBlockage",
53            Self::CulvertFaceABlockage => "CulvertFaceABlockage",
54            Self::CulvertFaceBBlockage => "CulvertFaceBBlockage",
55        }
56    }
57}
58
59/// One knot of a scalar geometry schedule.
60///
61/// `left` applies immediately before `time_s`, and `right` applies at and immediately after
62/// `time_s`. Equal `left` and `right` give a continuous piecewise-linear knot; distinct values
63/// give a step change at `time_s`.
64#[derive(Clone, Copy, Debug, PartialEq)]
65pub struct StructureGeometryKnot {
66    /// Event time in seconds.
67    pub time_s: f64,
68    /// Value immediately before `time_s`.
69    pub left: f64,
70    /// Value at and immediately after `time_s`.
71    pub right: f64,
72}
73
74impl StructureGeometryKnot {
75    /// A continuous knot where `left` and `right` both equal `value`.
76    pub const fn continuous(time_s: f64, value: f64) -> Self {
77        Self {
78            time_s,
79            left: value,
80            right: value,
81        }
82    }
83
84    /// A step knot jumping from `left` before `time_s` to `right` at and after `time_s`.
85    pub const fn step(time_s: f64, left: f64, right: f64) -> Self {
86        Self {
87            time_s,
88            left,
89            right,
90        }
91    }
92}
93
94/// One step knot of a discrete geometry schedule (`u32` closed barrels or `bool` closed state).
95#[derive(Clone, Copy, Debug, PartialEq)]
96pub struct DiscreteGeometryKnot<T: Copy> {
97    /// Event time in seconds.
98    pub time_s: f64,
99    /// Value before `time_s`.
100    pub left: T,
101    /// Value at and after `time_s`.
102    pub right: T,
103}
104
105impl<T: Copy> DiscreteGeometryKnot<T> {
106    /// A step knot jumping from `left` before `time_s` to `right` at and after `time_s`.
107    pub const fn step(time_s: f64, left: T, right: T) -> Self {
108        Self {
109            time_s,
110            left,
111            right,
112        }
113    }
114}
115
116/// Typed track of one geometry schedule on a single structure.
117#[non_exhaustive]
118#[derive(Clone, Debug, PartialEq)]
119pub enum StructureGeometryTrack {
120    /// Piecewise-linear scalar parameter (metres, m², or blocked share in `[0, 1)`).
121    Scalar {
122        /// Geometric parameter driven by this track.
123        parameter: ScalarGeometryParameter,
124        /// Strictly increasing knots in time.
125        knots: Vec<StructureGeometryKnot>,
126    },
127    /// Piecewise-linear uniform culvert sediment depth in metres, with a fixed bed Manning `n`.
128    ///
129    /// A depth of `0.0` clears sediment.
130    CulvertSediment {
131        /// Manning roughness `n_bed` of the sediment bed, in `s/m^(1/3)`.
132        bed_manning_n: f64,
133        /// Strictly increasing knots of sediment depth in metres.
134        knots: Vec<StructureGeometryKnot>,
135    },
136    /// Step schedule of closed culvert barrel counts `k` (`0 <= k < N`).
137    CulvertClosedBarrels {
138        /// Strictly increasing step knots of closed barrel counts.
139        knots: Vec<DiscreteGeometryKnot<u32>>,
140    },
141    /// Step schedule of `StructureCondition::Closed` (`true` = closed, `false` = open).
142    ///
143    /// Closure is always a step change because no linear path reaches blockage fraction 1.
144    Closed {
145        /// Strictly increasing step knots of closed state.
146        knots: Vec<DiscreteGeometryKnot<bool>>,
147    },
148}
149
150/// Discriminant identifying which parameter of a structure a track writes, for duplicate checks.
151#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
152pub(crate) enum TrackKey {
153    Scalar(u8),
154    CulvertSediment,
155    CulvertClosedBarrels,
156    Closed,
157}
158
159impl StructureGeometryTrack {
160    pub(crate) fn key(&self) -> TrackKey {
161        match self {
162            Self::Scalar { parameter, .. } => TrackKey::Scalar(*parameter as u8),
163            Self::CulvertSediment { .. } => TrackKey::CulvertSediment,
164            Self::CulvertClosedBarrels { .. } => TrackKey::CulvertClosedBarrels,
165            Self::Closed { .. } => TrackKey::Closed,
166        }
167    }
168
169    pub(crate) fn name(&self) -> &'static str {
170        match self {
171            Self::Scalar { parameter, .. } => parameter.name(),
172            Self::CulvertSediment { .. } => "CulvertSediment",
173            Self::CulvertClosedBarrels { .. } => "CulvertClosedBarrels",
174            Self::Closed { .. } => "Closed",
175        }
176    }
177
178    /// Knot times in ascending order.
179    pub fn knot_times(&self) -> Vec<f64> {
180        match self {
181            Self::Scalar { knots, .. } | Self::CulvertSediment { knots, .. } => {
182                knots.iter().map(|k| k.time_s).collect()
183            }
184            Self::CulvertClosedBarrels { knots } => knots.iter().map(|k| k.time_s).collect(),
185            Self::Closed { knots } => knots.iter().map(|k| k.time_s).collect(),
186        }
187    }
188}