use crate::structures::StructureError;
use crate::structures::numerics::culvert::BarrelClient;
use crate::structures::numerics::culvert::floor::FloorSection;
use crate::structures::numerics::culvert::section::SectionIntervals;
use crate::structures::tensor::codes;
use crate::structures::tensor::culvert::TensorBarrelContext;
use numr::dtype::DType;
use numr::ops::ConditionalOps;
use numr::runtime::Runtime;
use numr::tensor::Tensor;
use solvr::common::arithmetic::Interval;
fn ends<R: Runtime<DType = DType>>(interval: &Interval<R>) -> [Tensor<R>; 2] {
[interval.lower().clone(), interval.upper().clone()]
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BarrelState {
Dry,
Free,
Full,
}
pub struct CulvertBarrelGeometry<R: Runtime<DType = DType>> {
state: Tensor<R>,
area: [Tensor<R>; 2],
top_width: [Tensor<R>; 2],
perimeter: [Tensor<R>; 2],
wall: [Tensor<R>; 2],
floor: [Tensor<R>; 2],
weighted: [Tensor<R>; 2],
radius: [Tensor<R>; 2],
moment: [Tensor<R>; 2],
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct CulvertBarrelGeometryRow {
pub state: BarrelState,
pub area_m2: (f64, f64),
pub top_width_m: (f64, f64),
pub wetted_perimeter_m: (f64, f64),
pub wall_perimeter_m: (f64, f64),
pub floor_width_m: (f64, f64),
pub weighted_perimeter_m: (f64, f64),
pub hydraulic_radius_m: (f64, f64),
pub hydrostatic_moment_m3: (f64, f64),
}
impl<R: Runtime<DType = DType>> CulvertBarrelGeometry<R> {
pub(crate) fn from_section(section: &SectionIntervals<R>) -> numr::error::Result<Self> {
let g = §ion.geometry;
let like = g.area.lower();
let zero = Tensor::<R>::zeros(like.shape(), DType::F64, like.device())?;
let perimeter = ends(&g.perimeter);
Ok(Self {
state: section.state.clone(),
area: ends(&g.area),
top_width: ends(&g.top_width),
wall: perimeter.clone(),
floor: [zero.clone(), zero],
weighted: perimeter.clone(),
perimeter,
radius: ends(&g.radius),
moment: ends(&g.moment),
})
}
pub(crate) fn combine(
context: &TensorBarrelContext<R>,
section: &SectionIntervals<R>,
open: Option<FloorSection<R>>,
) -> numr::error::Result<Self>
where
R::Client: BarrelClient<R>,
{
let base = Self::from_section(section)?;
let (Some(open), Some(rows)) = (open, &context.floor) else {
return Ok(base);
};
let c = &context.client;
let floored = &rows.floored;
let g = &open.geometry;
let pick = |interval: &Interval<R>, [lower, upper]: &[Tensor<R>; 2]| {
Ok::<_, numr::error::Error>([
c.where_cond(floored, interval.lower(), lower)?,
c.where_cond(floored, interval.upper(), upper)?,
])
};
Ok(Self {
state: c.where_cond(floored, &open.state, &base.state)?,
area: pick(&g.area, &base.area)?,
top_width: pick(&g.top_width, &base.top_width)?,
perimeter: pick(&g.perimeter, &base.perimeter)?,
wall: pick(&g.wall, &base.wall)?,
floor: pick(&g.bed, &base.floor)?,
weighted: pick(&g.weighted, &base.weighted)?,
radius: pick(&g.radius, &base.radius)?,
moment: pick(&g.moment, &base.moment)?,
})
}
pub fn area(&self) -> (&Tensor<R>, &Tensor<R>) {
(&self.area[0], &self.area[1])
}
pub fn top_width(&self) -> (&Tensor<R>, &Tensor<R>) {
(&self.top_width[0], &self.top_width[1])
}
pub fn wetted_perimeter(&self) -> (&Tensor<R>, &Tensor<R>) {
(&self.perimeter[0], &self.perimeter[1])
}
pub fn wall_perimeter(&self) -> (&Tensor<R>, &Tensor<R>) {
(&self.wall[0], &self.wall[1])
}
pub fn floor_width(&self) -> (&Tensor<R>, &Tensor<R>) {
(&self.floor[0], &self.floor[1])
}
pub fn weighted_perimeter(&self) -> (&Tensor<R>, &Tensor<R>) {
(&self.weighted[0], &self.weighted[1])
}
pub fn hydraulic_radius(&self) -> (&Tensor<R>, &Tensor<R>) {
(&self.radius[0], &self.radius[1])
}
pub fn hydrostatic_moment(&self) -> (&Tensor<R>, &Tensor<R>) {
(&self.moment[0], &self.moment[1])
}
pub fn project(&self) -> Result<Vec<CulvertBarrelGeometryRow>, StructureError> {
let read =
|t: &Tensor<R>| -> Result<Vec<f64>, StructureError> { Ok(t.try_to_vec::<f64>()?) };
let pair = |[lower, upper]: &[Tensor<R>; 2]| -> Result<Vec<(f64, f64)>, StructureError> {
Ok(read(lower)?.into_iter().zip(read(upper)?).collect())
};
let states = read(&self.state)?;
let area = pair(&self.area)?;
let top_width = pair(&self.top_width)?;
let perimeter = pair(&self.perimeter)?;
let wall = pair(&self.wall)?;
let floor = pair(&self.floor)?;
let weighted = pair(&self.weighted)?;
let radius = pair(&self.radius)?;
let moment = pair(&self.moment)?;
states
.iter()
.enumerate()
.map(|(row, &code)| {
let state = if code == codes::BARREL_DRY {
BarrelState::Dry
} else if code == codes::BARREL_FREE {
BarrelState::Free
} else if code == codes::BARREL_FULL {
BarrelState::Full
} else {
return Err(StructureError::numerical(format!(
"barrel state code={code}: report this row, the code is not a barrel state"
))
.at(row));
};
Ok(CulvertBarrelGeometryRow {
state,
area_m2: area[row],
top_width_m: top_width[row],
wetted_perimeter_m: perimeter[row],
wall_perimeter_m: wall[row],
floor_width_m: floor[row],
weighted_perimeter_m: weighted[row],
hydraulic_radius_m: radius[row],
hydrostatic_moment_m3: moment[row],
})
})
.collect()
}
}