hydr-structures 0.1.0

Hydraulic structures for the hydr simulation crates
Documentation
//! Certified barrel geometry tensors and their explicit host projection.

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;

/// Stored endpoints of `interval`.
fn ends<R: Runtime<DType = DType>>(interval: &Interval<R>) -> [Tensor<R>; 2] {
    [interval.lower().clone(), interval.upper().clone()]
}

/// Flow state of one barrel row.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BarrelState {
    /// Depth zero. Every quantity is an exact zero, except `T = P = B` for a box and a
    /// flat-floor arch, and `T = P = b`, `P_n = rho^(3/2) b` above a floor.
    Dry,
    /// Free surface below the crown, `0 < y < D`, with `D` the open rise above a floor.
    Free,
    /// Full from the crown upward, `y >= D`. Depth above the crown is pressure head.
    Full,
}

/// Certified barrel geometry per row, as lower and upper F64 tensors of shape `[m]`.
///
/// Every exact value lies inside its `[lower, upper]` pair.
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],
}

/// One projected row of [`CulvertBarrelGeometry`], each quantity as `(lower, upper)`.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct CulvertBarrelGeometryRow {
    /// Flow state of the row.
    pub state: BarrelState,
    /// Flow area `A`, in square metres.
    pub area_m2: (f64, f64),
    /// Free-surface width `T`, in metres. Zero in the full state.
    pub top_width_m: (f64, f64),
    /// Wetted perimeter `P`, in metres: walls and bed, `P_w + b`.
    pub wetted_perimeter_m: (f64, f64),
    /// Wall perimeter `P_w`, in metres: `P` without the bed. Equal to `P` without a floor.
    pub wall_perimeter_m: (f64, f64),
    /// Wetted bed width `b`, in metres, at every depth of a barrel with a floor. Zero without one.
    pub floor_width_m: (f64, f64),
    /// Weighted perimeter `P_n = P_w + rho^(3/2) b`, in metres, with `rho` the bed-to-wall
    /// Manning ratio. Manning friction with the wall `n` and `P_n` equals HDS-5 composite
    /// roughness. Equal to `P` without a floor.
    pub weighted_perimeter_m: (f64, f64),
    /// Hydraulic radius `A / P`, in metres.
    pub hydraulic_radius_m: (f64, f64),
    /// Hydrostatic moment `integral from 0 to y of (y - eta) T(eta) d eta`, in cubic metres.
    /// In the full state it is `A_full (y - D/2)` for a circle or a box, and `M(D) + A_full (y - D)`
    /// for an arc barrel or a barrel with a floor, with `D` its open rise.
    pub hydrostatic_moment_m3: (f64, f64),
}

impl<R: Runtime<DType = DType>> CulvertBarrelGeometry<R> {
    /// Rows without a floor: the wall perimeter and the weighted perimeter are `P`.
    pub(crate) fn from_section(section: &SectionIntervals<R>) -> numr::error::Result<Self> {
        let g = &section.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),
        })
    }

    /// `section` on rows without a floor, and the open section `open` of the context's floored
    /// rows.
    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)?,
        })
    }

    /// Lower and upper flow area `A`, in square metres.
    pub fn area(&self) -> (&Tensor<R>, &Tensor<R>) {
        (&self.area[0], &self.area[1])
    }

    /// Lower and upper free-surface width `T`, in metres.
    pub fn top_width(&self) -> (&Tensor<R>, &Tensor<R>) {
        (&self.top_width[0], &self.top_width[1])
    }

    /// Lower and upper wetted perimeter `P`, in metres.
    pub fn wetted_perimeter(&self) -> (&Tensor<R>, &Tensor<R>) {
        (&self.perimeter[0], &self.perimeter[1])
    }

    /// Lower and upper wall perimeter `P_w`, in metres.
    pub fn wall_perimeter(&self) -> (&Tensor<R>, &Tensor<R>) {
        (&self.wall[0], &self.wall[1])
    }

    /// Lower and upper wetted bed width `b`, in metres. Zero on rows without a floor.
    pub fn floor_width(&self) -> (&Tensor<R>, &Tensor<R>) {
        (&self.floor[0], &self.floor[1])
    }

    /// Lower and upper weighted perimeter `P_n = P_w + rho^(3/2) b`, in metres.
    pub fn weighted_perimeter(&self) -> (&Tensor<R>, &Tensor<R>) {
        (&self.weighted[0], &self.weighted[1])
    }

    /// Lower and upper hydraulic radius `A / P`, in metres.
    pub fn hydraulic_radius(&self) -> (&Tensor<R>, &Tensor<R>) {
        (&self.radius[0], &self.radius[1])
    }

    /// Lower and upper hydrostatic moment, in cubic metres.
    pub fn hydrostatic_moment(&self) -> (&Tensor<R>, &Tensor<R>) {
        (&self.moment[0], &self.moment[1])
    }

    /// Copies every row to the host.
    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()
    }
}