hydr_structures/structures/culvert/outlet/
output.rs1use crate::structures::StructureError;
4use crate::structures::numerics::culvert::outlet::OutletHeadwater;
5use crate::structures::numerics::culvert::outlet::jump::OutletJump;
6use crate::structures::tensor::codes;
7use numr::dtype::DType;
8use numr::runtime::Runtime;
9use numr::tensor::Tensor;
10
11#[derive(Clone, Copy, Debug, PartialEq, Eq)]
13pub enum OutletRegime {
14 NoFlow,
16 Full,
18 PartlyFull,
20 InletCritical,
24 Unresolved,
26}
27
28impl OutletRegime {
29 fn from_code(code: f64) -> Option<Self> {
30 [
31 (codes::OUTLET_NO_FLOW, Self::NoFlow),
32 (codes::OUTLET_FULL, Self::Full),
33 (codes::OUTLET_PARTLY_FULL, Self::PartlyFull),
34 (codes::OUTLET_INLET_CRITICAL, Self::InletCritical),
35 (codes::OUTLET_UNRESOLVED, Self::Unresolved),
36 ]
37 .into_iter()
38 .find_map(|(value, regime)| (value == code).then_some(regime))
39 }
40}
41
42pub struct CulvertOutletHeadwater<R: Runtime<DType = DType>> {
45 status: Tensor<R>,
46 inlet: [Tensor<R>; 2],
47 headwater: [Tensor<R>; 2],
48}
49
50#[derive(Clone, Copy, Debug, PartialEq)]
52pub struct CulvertOutletHeadwaterRow {
53 pub regime: OutletRegime,
55 pub inlet_head_m: (f64, f64),
58 pub headwater_m: (f64, f64),
61}
62
63fn regime_error(code: f64, row: usize) -> StructureError {
64 StructureError::numerical(format!(
65 "outlet regime code={code}: report this row, the code is not an outlet regime"
66 ))
67 .at(row)
68}
69
70impl<R: Runtime<DType = DType>> CulvertOutletHeadwater<R> {
71 pub(crate) fn from_headwater(headwater: OutletHeadwater<R>) -> Self {
72 Self {
73 status: headwater.status,
74 inlet: headwater.inlet,
75 headwater: headwater.headwater,
76 }
77 }
78
79 pub fn headwater_m(&self) -> (&Tensor<R>, &Tensor<R>) {
81 (&self.headwater[0], &self.headwater[1])
82 }
83
84 pub fn inlet_head_m(&self) -> (&Tensor<R>, &Tensor<R>) {
86 (&self.inlet[0], &self.inlet[1])
87 }
88
89 pub fn project(&self) -> Result<Vec<CulvertOutletHeadwaterRow>, StructureError> {
91 let status = self.status.try_to_vec::<f64>()?;
92 let inlet = [
93 self.inlet[0].try_to_vec::<f64>()?,
94 self.inlet[1].try_to_vec::<f64>()?,
95 ];
96 let headwater = [
97 self.headwater[0].try_to_vec::<f64>()?,
98 self.headwater[1].try_to_vec::<f64>()?,
99 ];
100 status
101 .iter()
102 .enumerate()
103 .map(|(row, &code)| {
104 let regime =
105 OutletRegime::from_code(code).ok_or_else(|| regime_error(code, row))?;
106 let at = |values: &[Vec<f64>; 2]| -> (f64, f64) {
107 (
108 values[0].get(row).copied().unwrap_or(f64::NAN),
109 values[1].get(row).copied().unwrap_or(f64::NAN),
110 )
111 };
112 Ok(CulvertOutletHeadwaterRow {
113 regime,
114 inlet_head_m: at(&inlet),
115 headwater_m: at(&headwater),
116 })
117 })
118 .collect()
119 }
120}
121
122#[derive(Clone, Copy, Debug, PartialEq, Eq)]
124pub enum JumpStatus {
125 NotSteep,
127 Supercritical,
129 InBarrel,
131 Unresolved,
134}
135
136impl JumpStatus {
137 fn from_code(code: f64) -> Option<Self> {
138 [
139 (codes::JUMP_NOT_STEEP, Self::NotSteep),
140 (codes::JUMP_SUPERCRITICAL, Self::Supercritical),
141 (codes::JUMP_IN_BARREL, Self::InBarrel),
142 (codes::JUMP_UNRESOLVED, Self::Unresolved),
143 ]
144 .into_iter()
145 .find_map(|(value, status)| (value == code).then_some(status))
146 }
147}
148
149pub struct CulvertOutletJump<R: Runtime<DType = DType>> {
153 headwater: CulvertOutletHeadwater<R>,
154 status: Tensor<R>,
155 position: [Tensor<R>; 2],
156 upstream: [Tensor<R>; 2],
157 downstream: [Tensor<R>; 2],
158 outlet: [Tensor<R>; 2],
159}
160
161#[derive(Clone, Copy, Debug, PartialEq)]
163pub struct CulvertOutletJumpRow {
164 pub status: JumpStatus,
166 pub position_m: (f64, f64),
168 pub upstream_depth_m: (f64, f64),
170 pub downstream_depth_m: (f64, f64),
172 pub outlet_depth_m: (f64, f64),
174}
175
176impl<R: Runtime<DType = DType>> CulvertOutletJump<R> {
177 pub(crate) fn from_jump(jump: OutletJump<R>, headwater: CulvertOutletHeadwater<R>) -> Self {
178 Self {
179 headwater,
180 status: jump.status,
181 position: jump.position,
182 upstream: jump.upstream,
183 downstream: jump.downstream,
184 outlet: jump.outlet,
185 }
186 }
187
188 pub fn headwater(&self) -> &CulvertOutletHeadwater<R> {
190 &self.headwater
191 }
192
193 pub fn position_m(&self) -> (&Tensor<R>, &Tensor<R>) {
195 (&self.position[0], &self.position[1])
196 }
197
198 pub fn project(&self) -> Result<Vec<CulvertOutletJumpRow>, StructureError> {
200 let status = self.status.try_to_vec::<f64>()?;
201 let read = |pair: &[Tensor<R>; 2]| -> Result<[Vec<f64>; 2], StructureError> {
202 Ok([pair[0].try_to_vec::<f64>()?, pair[1].try_to_vec::<f64>()?])
203 };
204 let position = read(&self.position)?;
205 let upstream = read(&self.upstream)?;
206 let downstream = read(&self.downstream)?;
207 let outlet = read(&self.outlet)?;
208 status
209 .iter()
210 .enumerate()
211 .map(|(row, &code)| {
212 let status = JumpStatus::from_code(code).ok_or_else(|| {
213 StructureError::numerical(format!(
214 "jump status code={code}: report this row, the code is not a jump status"
215 ))
216 .at(row)
217 })?;
218 let at = |values: &[Vec<f64>; 2]| -> (f64, f64) {
219 (
220 values[0].get(row).copied().unwrap_or(f64::NAN),
221 values[1].get(row).copied().unwrap_or(f64::NAN),
222 )
223 };
224 Ok(CulvertOutletJumpRow {
225 status,
226 position_m: at(&position),
227 upstream_depth_m: at(&upstream),
228 downstream_depth_m: at(&downstream),
229 outlet_depth_m: at(&outlet),
230 })
231 })
232 .collect()
233 }
234}