mod assemble;
mod bands;
mod classes;
mod coordinates;
mod depth;
mod integrand;
mod jet;
pub(crate) mod jump;
mod junctions;
mod levels;
mod partition;
mod quadrature;
mod screen;
mod slope;
mod trig;
mod trig_ends;
mod zone;
pub(crate) use bands::{ArcShape, Outline};
pub(crate) use levels::Accuracy;
pub(crate) use slope::{SlopeOperands, head_slopes};
use super::{Arithmetic, BarrelClient, BarrelDepths, RowNames, critical, normal};
use crate::structures::tensor::culvert::{Bounds, TensorBarrelContext, TensorOutletContext};
use crate::structures::{CulvertBarrelShape, CulvertOutletRow, StructureError};
use numr::dtype::DType;
use numr::ops::ConditionalOps;
use numr::runtime::Runtime;
use numr::tensor::Tensor;
use solvr::common::arithmetic::{ArithmeticResult, Interval, with_interval_arithmetic};
use solvr::integrate::CertifiedSimpsonAlgorithms;
use solvr::optimize::CertifiedBisectionAlgorithms;
pub(crate) struct OutletHeadwater<R: Runtime<DType = DType>> {
pub(crate) status: Tensor<R>,
pub(crate) inlet: Bounds<R>,
pub(crate) headwater: Bounds<R>,
}
pub(crate) struct March<R: Runtime<DType = DType>> {
pub(crate) headwater: OutletHeadwater<R>,
pub(crate) classes: classes::Classes<R>,
pub(crate) reach: assemble::Reach<R>,
pub(crate) profile: partition::Profile<R>,
pub(crate) normal: BarrelDepths<R>,
}
pub(crate) fn prepare<R>(
barrel: &TensorBarrelContext<R>,
rows: &[CulvertOutletRow],
) -> Result<TensorOutletContext<R>, StructureError>
where
R: Runtime<DType = DType>,
R::Client: BarrelClient<R>,
{
let device = &barrel.device;
let m = rows.len();
let column = |f: &dyn Fn(&CulvertOutletRow) -> f64| -> numr::error::Result<Tensor<R>> {
let values: Vec<f64> = rows.iter().map(f).collect();
Tensor::<R>::from_slice(&values, &[m], device)
};
let open_rows = rows
.iter()
.any(|r| r.barrel.floor().is_some() && r.barrel.shape() != CulvertBarrelShape::Box);
let entrance = column(&|r| r.entrance_loss)?;
let face = column(&|r| r.face_blockage.blocked_share())?;
let c = &barrel.client;
let ends = |x: &Interval<R>| -> Bounds<R> { [x.lower().clone(), x.upper().clone()] };
let (beta, bed, entrance, arcs) =
with_interval_arithmetic(c, DType::F64, "culvert outlet shape", |a| {
let span = a.point(&barrel.span)?;
let ratio = a.div_positive(&span, barrel.open_rise())?;
let beta = ends(&ratio);
let bed = match barrel.floor.as_deref() {
Some(rows) => {
let weight = a.bounds(&rows.weight[0], &rows.weight[1])?;
let weighted = a.mul(&weight, &ratio)?;
let [lower, upper] = ends(&weighted);
[
c.where_cond(&rows.boxed, &lower, &beta[0])?,
c.where_cond(&rows.boxed, &upper, &beta[1])?,
]
}
None => beta.clone(),
};
let base = match &barrel.unit_arcs {
Some(rows) => Some(ArcShape::new(a, rows)?),
None => None,
};
let arcs = if open_rows {
Some(ArcShape::with_floors(a, barrel, base)?)
} else {
base
};
Ok((
beta,
bed,
ends(&face_loss(a, barrel, &entrance, &face)?),
arcs,
))
})?;
Ok(TensorOutletContext {
length: column(&|r| r.length_m)?,
manning: column(&|r| r.manning_n)?,
entrance,
slope: column(&|r| r.slope)?,
gravity: column(&|r| r.gravity_m_per_s2)?,
beta,
bed,
arcs,
open: barrel.march_view()?.map(Box::new),
})
}
fn face_loss<R>(
a: &mut Arithmetic<'_, R>,
barrel: &TensorBarrelContext<R>,
entrance: &Tensor<R>,
face: &Tensor<R>,
) -> ArithmeticResult<Interval<R>>
where
R: Runtime<DType = DType>,
R::Client: BarrelClient<R>,
{
let one = a.point(&barrel.one)?;
let blocked = a.point(face)?;
let open = a.sub(&one, &blocked)?;
let square = a.mul(&open, &open)?;
let ke = a.point(entrance)?;
let loss = a.div_positive_interval(&ke, &square)?;
let ratio = a.div_positive_interval(&blocked, &open)?;
let expansion = a.mul(&ratio, &ratio)?;
a.add(&loss, &expansion)
}
pub(crate) fn march<R>(
names: RowNames<'_, R>,
barrel: &TensorBarrelContext<R>,
unit: &TensorBarrelContext<R>,
outlet: &TensorOutletContext<R>,
discharge: &Tensor<R>,
tailwater: &Tensor<R>,
accuracy: Accuracy,
) -> Result<March<R>, StructureError>
where
R: Runtime<DType = DType>,
R::Client: BarrelClient<R> + CertifiedBisectionAlgorithms<R> + CertifiedSimpsonAlgorithms<R>,
{
let c = &barrel.client;
let rtol = accuracy.root_tolerance();
let critical = critical::solve_to(names, barrel, unit, discharge, &outlet.gravity, rtol)?;
let normal = normal::solve_to(
names,
barrel,
unit,
[discharge, &outlet.manning, &outlet.slope],
rtol,
)?;
let barrel = outlet.view(barrel);
let operands = classes::Operands {
barrel,
outlet,
discharge,
tailwater,
critical: &critical,
normal: &normal,
};
let classes = with_interval_arithmetic(c, DType::F64, "culvert outlet classes", |a| {
classes::classify(a, &operands)
})?;
let profile = partition::build(c, classes.spec.clone(), accuracy)?;
let reach = with_interval_arithmetic(c, DType::F64, "culvert outlet reach", |a| {
assemble::reach(a, barrel, &classes, &profile)
})?;
let rows: Vec<i64> = (0..barrel.width())
.map(|row| i64::try_from(row).unwrap_or(i64::MAX))
.collect();
let rows = Tensor::<R>::from_slice(&rows, &[barrel.width()], &barrel.device)?;
let inlet = depth::depth_at(
c,
&profile,
&rows,
&classes.target,
&reach.solve,
accuracy.depth_tolerance(),
)?;
let parts = assemble::Parts {
barrel,
outlet,
classes: &classes,
reach: &reach,
inlet: &inlet,
};
let assembled = with_interval_arithmetic(c, DType::F64, "culvert outlet headwater", |a| {
assemble::assemble(a, &parts)
})?;
Ok(March {
headwater: OutletHeadwater {
status: assembled.status,
inlet: assembled.inlet,
headwater: assembled.headwater,
},
classes,
reach,
profile,
normal,
})
}