use crate::structures::StructureError;
use crate::structures::numerics::culvert::outlet::OutletHeadwater;
use crate::structures::numerics::culvert::outlet::jump::OutletJump;
use crate::structures::tensor::codes;
use numr::dtype::DType;
use numr::runtime::Runtime;
use numr::tensor::Tensor;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum OutletRegime {
NoFlow,
Full,
PartlyFull,
InletCritical,
Unresolved,
}
impl OutletRegime {
fn from_code(code: f64) -> Option<Self> {
[
(codes::OUTLET_NO_FLOW, Self::NoFlow),
(codes::OUTLET_FULL, Self::Full),
(codes::OUTLET_PARTLY_FULL, Self::PartlyFull),
(codes::OUTLET_INLET_CRITICAL, Self::InletCritical),
(codes::OUTLET_UNRESOLVED, Self::Unresolved),
]
.into_iter()
.find_map(|(value, regime)| (value == code).then_some(regime))
}
}
pub struct CulvertOutletHeadwater<R: Runtime<DType = DType>> {
status: Tensor<R>,
inlet: [Tensor<R>; 2],
headwater: [Tensor<R>; 2],
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct CulvertOutletHeadwaterRow {
pub regime: OutletRegime,
pub inlet_head_m: (f64, f64),
pub headwater_m: (f64, f64),
}
fn regime_error(code: f64, row: usize) -> StructureError {
StructureError::numerical(format!(
"outlet regime code={code}: report this row, the code is not an outlet regime"
))
.at(row)
}
impl<R: Runtime<DType = DType>> CulvertOutletHeadwater<R> {
pub(crate) fn from_headwater(headwater: OutletHeadwater<R>) -> Self {
Self {
status: headwater.status,
inlet: headwater.inlet,
headwater: headwater.headwater,
}
}
pub fn headwater_m(&self) -> (&Tensor<R>, &Tensor<R>) {
(&self.headwater[0], &self.headwater[1])
}
pub fn inlet_head_m(&self) -> (&Tensor<R>, &Tensor<R>) {
(&self.inlet[0], &self.inlet[1])
}
pub fn project(&self) -> Result<Vec<CulvertOutletHeadwaterRow>, StructureError> {
let status = self.status.try_to_vec::<f64>()?;
let inlet = [
self.inlet[0].try_to_vec::<f64>()?,
self.inlet[1].try_to_vec::<f64>()?,
];
let headwater = [
self.headwater[0].try_to_vec::<f64>()?,
self.headwater[1].try_to_vec::<f64>()?,
];
status
.iter()
.enumerate()
.map(|(row, &code)| {
let regime =
OutletRegime::from_code(code).ok_or_else(|| regime_error(code, row))?;
let at = |values: &[Vec<f64>; 2]| -> (f64, f64) {
(
values[0].get(row).copied().unwrap_or(f64::NAN),
values[1].get(row).copied().unwrap_or(f64::NAN),
)
};
Ok(CulvertOutletHeadwaterRow {
regime,
inlet_head_m: at(&inlet),
headwater_m: at(&headwater),
})
})
.collect()
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum JumpStatus {
NotSteep,
Supercritical,
InBarrel,
Unresolved,
}
impl JumpStatus {
fn from_code(code: f64) -> Option<Self> {
[
(codes::JUMP_NOT_STEEP, Self::NotSteep),
(codes::JUMP_SUPERCRITICAL, Self::Supercritical),
(codes::JUMP_IN_BARREL, Self::InBarrel),
(codes::JUMP_UNRESOLVED, Self::Unresolved),
]
.into_iter()
.find_map(|(value, status)| (value == code).then_some(status))
}
}
pub struct CulvertOutletJump<R: Runtime<DType = DType>> {
headwater: CulvertOutletHeadwater<R>,
status: Tensor<R>,
position: [Tensor<R>; 2],
upstream: [Tensor<R>; 2],
downstream: [Tensor<R>; 2],
outlet: [Tensor<R>; 2],
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct CulvertOutletJumpRow {
pub status: JumpStatus,
pub position_m: (f64, f64),
pub upstream_depth_m: (f64, f64),
pub downstream_depth_m: (f64, f64),
pub outlet_depth_m: (f64, f64),
}
impl<R: Runtime<DType = DType>> CulvertOutletJump<R> {
pub(crate) fn from_jump(jump: OutletJump<R>, headwater: CulvertOutletHeadwater<R>) -> Self {
Self {
headwater,
status: jump.status,
position: jump.position,
upstream: jump.upstream,
downstream: jump.downstream,
outlet: jump.outlet,
}
}
pub fn headwater(&self) -> &CulvertOutletHeadwater<R> {
&self.headwater
}
pub fn position_m(&self) -> (&Tensor<R>, &Tensor<R>) {
(&self.position[0], &self.position[1])
}
pub fn project(&self) -> Result<Vec<CulvertOutletJumpRow>, StructureError> {
let status = self.status.try_to_vec::<f64>()?;
let read = |pair: &[Tensor<R>; 2]| -> Result<[Vec<f64>; 2], StructureError> {
Ok([pair[0].try_to_vec::<f64>()?, pair[1].try_to_vec::<f64>()?])
};
let position = read(&self.position)?;
let upstream = read(&self.upstream)?;
let downstream = read(&self.downstream)?;
let outlet = read(&self.outlet)?;
status
.iter()
.enumerate()
.map(|(row, &code)| {
let status = JumpStatus::from_code(code).ok_or_else(|| {
StructureError::numerical(format!(
"jump status code={code}: report this row, the code is not a jump status"
))
.at(row)
})?;
let at = |values: &[Vec<f64>; 2]| -> (f64, f64) {
(
values[0].get(row).copied().unwrap_or(f64::NAN),
values[1].get(row).copied().unwrap_or(f64::NAN),
)
};
Ok(CulvertOutletJumpRow {
status,
position_m: at(&position),
upstream_depth_m: at(&upstream),
downstream_depth_m: at(&downstream),
outlet_depth_m: at(&outlet),
})
})
.collect()
}
}