Skip to main content

hydr_core/capability/
boundary.rs

1//! Endpoints and the boundary kinds each one admits.
2
3/// Place where a component meets a boundary or another component.
4#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5#[non_exhaustive]
6pub enum EndpointId {
7    /// Face on the exterior of a 2D grid.
8    ExteriorFace,
9    /// Left end of a channel reach.
10    ReachLeft,
11    /// Right end of a channel reach.
12    ReachRight,
13    /// Connection between two pools.
14    PoolConnection,
15    /// End of a hydraulic structure.
16    StructureEndpoint,
17    /// Outlet of a pool or subcatchment.
18    Outlet,
19    /// Lateral exchange along a reach or pool.
20    Lateral,
21    /// Active cell of a 2D grid that exchanges water with another component.
22    SurfaceCell,
23}
24
25impl EndpointId {
26    /// Returns the stable kebab-case id. Persisted decisions store it, so it never changes.
27    pub fn id(&self) -> &'static str {
28        match self {
29            Self::ExteriorFace => "exterior-face",
30            Self::ReachLeft => "reach-left",
31            Self::ReachRight => "reach-right",
32            Self::PoolConnection => "pool-connection",
33            Self::StructureEndpoint => "structure-endpoint",
34            Self::Outlet => "outlet",
35            Self::Lateral => "lateral",
36            Self::SurfaceCell => "surface-cell",
37        }
38    }
39}
40
41/// Boundary condition or link that an endpoint accepts.
42#[derive(Clone, Copy, Debug, PartialEq, Eq)]
43#[non_exhaustive]
44pub enum BoundaryKind {
45    /// Closed wall with zero normal flux.
46    Wall,
47    /// Port that exchanges flux with another component.
48    FluxPort,
49    /// Prescribed water-surface stage.
50    Stage,
51    /// Prescribed discharge.
52    Discharge,
53    /// Exterior state supplied outside the model.
54    Exterior,
55    /// Transmissive outflow that copies the interior state.
56    Transmissive,
57    /// Reservoir at rest with a prescribed stage.
58    QuiescentReservoir,
59    /// Link to a pool connection.
60    PoolConnection,
61    /// Submerged orifice.
62    SubmergedOrifice,
63    /// Rectangular weir without end contractions.
64    SuppressedRectangularWeir,
65    /// Stopped pump that isolates its two sides.
66    PumpStoppedIsolated,
67    /// Running pump.
68    PumpRunning,
69    /// Gate held at a fixed opening.
70    GateFixedOpening,
71    /// Gate whose opening follows a prescribed trajectory over time.
72    GatePrescribedOpening,
73    /// Gate whose opening follows an actuator driven by operating controls.
74    GateActuatedOpening,
75    /// Pump whose operation follows controls that sample endpoint stages.
76    PumpSampledControl,
77    /// Prescribed inflow rate.
78    PrescribedInflowRate,
79    /// Prescribed inflow volume over an interval.
80    PrescribedInflowVolume,
81    /// Runoff outlet that receives no feedback from downstream.
82    RunoffOutletNoFeedback,
83    /// HDS-5 culvert under inlet or outlet control.
84    Culvert,
85}
86
87impl BoundaryKind {
88    /// Returns the stable kebab-case id. Persisted decisions store it, so it never changes.
89    pub fn id(&self) -> &'static str {
90        match self {
91            Self::Wall => "wall",
92            Self::FluxPort => "flux-port",
93            Self::Stage => "stage",
94            Self::Discharge => "discharge",
95            Self::Exterior => "exterior",
96            Self::Transmissive => "transmissive",
97            Self::QuiescentReservoir => "quiescent-reservoir",
98            Self::PoolConnection => "pool-connection",
99            Self::SubmergedOrifice => "submerged-orifice",
100            Self::SuppressedRectangularWeir => "suppressed-rectangular-weir",
101            Self::PumpStoppedIsolated => "pump-stopped-isolated",
102            Self::PumpRunning => "pump-running",
103            Self::GateFixedOpening => "gate-fixed-opening",
104            Self::GatePrescribedOpening => "gate-prescribed-opening",
105            Self::GateActuatedOpening => "gate-actuated-opening",
106            Self::PumpSampledControl => "pump-sampled-control",
107            Self::PrescribedInflowRate => "prescribed-inflow-rate",
108            Self::PrescribedInflowVolume => "prescribed-inflow-volume",
109            Self::RunoffOutletNoFeedback => "runoff-outlet-no-feedback",
110            Self::Culvert => "culvert",
111        }
112    }
113}
114
115/// Endpoint and the boundary kinds that admission accepts there.
116#[derive(Clone, Copy, Debug, PartialEq, Eq)]
117pub struct Endpoint {
118    /// Endpoint identity.
119    pub id: EndpointId,
120    /// Boundary kinds accepted at the endpoint.
121    pub admitted: &'static [BoundaryKind],
122}
123
124impl Endpoint {
125    /// Returns `true` when the endpoint admits `kind`.
126    pub fn admits(&self, kind: BoundaryKind) -> bool {
127        self.admitted.contains(&kind)
128    }
129}
130
131#[cfg(test)]
132mod tests {
133    use super::*;
134    use crate::capability::id_check::assert_ids;
135
136    #[test]
137    fn endpoint_ids_are_unique_kebab_case() {
138        let all = [
139            EndpointId::ExteriorFace,
140            EndpointId::ReachLeft,
141            EndpointId::ReachRight,
142            EndpointId::PoolConnection,
143            EndpointId::StructureEndpoint,
144            EndpointId::Outlet,
145            EndpointId::Lateral,
146            EndpointId::SurfaceCell,
147        ];
148        assert_ids(&all.map(|e| e.id()));
149    }
150
151    #[test]
152    fn boundary_ids_are_unique_kebab_case() {
153        let all = [
154            BoundaryKind::Wall,
155            BoundaryKind::FluxPort,
156            BoundaryKind::Stage,
157            BoundaryKind::Discharge,
158            BoundaryKind::Exterior,
159            BoundaryKind::Transmissive,
160            BoundaryKind::QuiescentReservoir,
161            BoundaryKind::PoolConnection,
162            BoundaryKind::SubmergedOrifice,
163            BoundaryKind::SuppressedRectangularWeir,
164            BoundaryKind::PumpStoppedIsolated,
165            BoundaryKind::PumpRunning,
166            BoundaryKind::GateFixedOpening,
167            BoundaryKind::GatePrescribedOpening,
168            BoundaryKind::GateActuatedOpening,
169            BoundaryKind::PumpSampledControl,
170            BoundaryKind::PrescribedInflowRate,
171            BoundaryKind::PrescribedInflowVolume,
172            BoundaryKind::RunoffOutletNoFeedback,
173            BoundaryKind::Culvert,
174        ];
175        assert_ids(&all.map(|b| b.id()));
176    }
177
178    #[test]
179    fn controlled_structure_boundary_ids_are_frozen() {
180        for (kind, id) in [
181            (
182                BoundaryKind::GatePrescribedOpening,
183                "gate-prescribed-opening",
184            ),
185            (BoundaryKind::GateActuatedOpening, "gate-actuated-opening"),
186            (BoundaryKind::PumpSampledControl, "pump-sampled-control"),
187        ] {
188            assert_eq!(kind.id(), id);
189        }
190    }
191
192    #[test]
193    fn endpoint_admits_only_listed_kinds() {
194        let end = Endpoint {
195            id: EndpointId::ReachLeft,
196            admitted: &[BoundaryKind::Wall, BoundaryKind::Stage],
197        };
198        assert!(end.admits(BoundaryKind::Wall));
199        assert!(end.admits(BoundaryKind::Stage));
200        assert!(!end.admits(BoundaryKind::Discharge));
201    }
202}