pub(crate) mod anchors;
pub(crate) mod bands;
pub(crate) mod head;
mod power;
use super::{BarrelClient, BarrelDepths, RowNames, critical};
use crate::structures::StructureError;
use crate::structures::tensor::culvert::{Bounds, TensorBarrelContext, TensorInletContext};
use numr::dtype::DType;
use numr::ops::{BinaryOps, ConditionalOps};
use numr::runtime::Runtime;
use numr::tensor::Tensor;
use solvr::common::arithmetic::with_interval_arithmetic;
use solvr::optimize::CertifiedBisectionAlgorithms;
pub(crate) struct InletHeadwater<R: Runtime<DType = DType>> {
pub(crate) status: Tensor<R>,
pub(crate) lower: Tensor<R>,
pub(crate) upper: Tensor<R>,
}
pub(crate) fn evaluate<R>(
names: RowNames<'_, R>,
barrel: &TensorBarrelContext<R>,
unit: &TensorBarrelContext<R>,
inlet: &TensorInletContext<R>,
discharge: &Tensor<R>,
) -> Result<InletHeadwater<R>, StructureError>
where
R: Runtime<DType = DType>,
R::Client: BarrelClient<R> + CertifiedBisectionAlgorithms<R>,
{
let c = &barrel.client;
let flows = unblocked(barrel, inlet, discharge)?;
let bands = bands::classify(inlet, barrel, &flows)?;
let needs = c.mul(&inlet.form_one, &bands.unsubmerged)?;
let solve = |end: &Tensor<R>| {
let flow = c.where_cond(&needs, end, &barrel.zero)?;
critical::solve(names, barrel, unit, &flow, &inlet.gravity)
};
let [low, high] = &flows;
let critical = if inlet.faced {
let below = solve(low)?;
let above = solve(high)?;
BarrelDepths {
status: c.maximum(&below.status, &above.status)?,
lower: below.lower,
upper: above.upper,
}
} else {
solve(low)?
};
let operands = bands::Operands {
barrel,
unit,
inlet,
discharge: &flows,
bands: &bands,
critical: &critical,
};
let headwater = with_interval_arithmetic(c, DType::F64, "culvert inlet headwater", |a| {
bands::headwater(a, &operands)
})?;
Ok(InletHeadwater {
status: bands::status(barrel, &bands)?,
lower: headwater.lower().clone(),
upper: headwater.upper().clone(),
})
}
fn unblocked<R>(
barrel: &TensorBarrelContext<R>,
inlet: &TensorInletContext<R>,
discharge: &Tensor<R>,
) -> Result<Bounds<R>, StructureError>
where
R: Runtime<DType = DType>,
R::Client: BarrelClient<R>,
{
if !inlet.faced {
return Ok([discharge.clone(), discharge.clone()]);
}
let c = &barrel.client;
let flows = with_interval_arithmetic(c, DType::F64, "culvert inlet face blockage", |a| {
let q = a.point(discharge)?;
let one = a.point(&barrel.one)?;
let blocked = a.point(&inlet.face)?;
let open = a.sub(&one, &blocked)?;
let flow = a.div_positive_interval(&q, &open)?;
Ok([flow.lower().clone(), flow.upper().clone()])
})?;
Ok(flows)
}