use super::lanes::Lanes;
use super::{Selected, form_one};
use crate::structures::numerics::culvert::Arithmetic;
use crate::structures::numerics::culvert::constants::UNSUBMERGED_LIMIT;
use crate::structures::tensor::culvert::{Bounds, TensorCulvertLaw};
use crate::structures::{CulvertClient, StructureError};
use numr::dtype::DType;
use numr::ops::{BinaryOps, CompareOps, ConditionalOps, ReduceOps};
use numr::runtime::Runtime;
use numr::tensor::Tensor;
use solvr::common::arithmetic::{ArithmeticResult, with_interval_arithmetic};
pub(crate) struct Bands<R: Runtime<DType = DType>> {
pub(crate) unsubmerged: Tensor<R>,
pub(crate) transition: Tensor<R>,
pub(crate) submerged: Tensor<R>,
}
pub(crate) struct InletDischarge<R: Runtime<DType = DType>> {
pub(crate) bounds: Bounds<R>,
pub(crate) zero: Tensor<R>,
pub(crate) bands: Bands<R>,
}
pub(crate) fn invert<R>(
law: &TensorCulvertLaw<R>,
rows: &Selected<R>,
lanes: &Lanes<R>,
) -> Result<InletDischarge<R>, StructureError>
where
R: Runtime<DType = DType>,
R::Client: CulvertClient<R>,
{
let barrel = &rows.barrel;
let inlet = &rows.inlet;
let c = &barrel.client;
let h = &lanes.headwater;
let active = &lanes.active;
let [low_lower, low_upper] = &inlet.low;
let [high_lower, high_upper] = &inlet.high;
let bands = Bands {
unsubmerged: c.mul(active, &c.le(h, low_upper)?)?,
transition: c.mul(active, &c.mul(&c.gt(h, low_lower)?, &c.lt(h, high_upper)?)?)?,
submerged: c.mul(active, &c.ge(h, high_lower)?)?,
};
let start = with_interval_arithmetic(c, DType::F64, "culvert inlet start", |a| {
let d = a.point(barrel.open_rise())?;
let slope_term = a.bounds(&inlet.slope_term[0], &inlet.slope_term[1])?;
Ok(a.mul(&d, &slope_term)?.lower().clone())
})?;
let form_one_free = c.mul(&inlet.form_one, &bands.unsubmerged)?;
let zero = c.mul(&form_one_free, &c.le(h, &start)?)?;
let solve = c.mul(&form_one_free, &c.sub(&barrel.one, &zero)?)?;
let solved = if any(c, &solve)? {
form_one::invert(rows, h, &solve)?
} else {
[
barrel.constant(f64::INFINITY)?,
barrel.constant(f64::NEG_INFINITY)?,
]
};
let closed = Closed {
rows,
headwater: h,
bands: &bands,
form_one: &solved,
};
let [lower, upper] =
with_interval_arithmetic(c, DType::F64, "culvert inlet inversion", |a| closed.hull(a))?;
let flowing = c.mul(active, &c.sub(&barrel.one, &zero)?)?;
let empty = c.mul(&flowing, &c.gt(&lower, &upper)?)?;
if let Some(lane) = super::first(&empty)? {
return Err(StructureError::numerical(
"culvert inlet inversion found no band for the headwater. Report this lane",
)
.at(law.elements[lane]));
}
let mut lower = c.where_cond(&flowing, &lower, &barrel.zero)?;
let mut upper = c.where_cond(&flowing, &upper, &barrel.zero)?;
if inlet.faced {
let [scaled_low, scaled_high] =
with_interval_arithmetic(c, DType::F64, "culvert inlet face blockage", |a| {
let one = a.point(&barrel.one)?;
let blocked = a.point(&inlet.face)?;
let open = a.sub(&one, &blocked)?;
let q = a.bounds(&lower, &upper)?;
let scaled = a.mul(&q, &open)?;
Ok([scaled.lower().clone(), scaled.upper().clone()])
})?;
let has_face = c.gt(&inlet.face, &barrel.zero)?;
lower = c.where_cond(&has_face, &scaled_low, &lower)?;
upper = c.where_cond(&has_face, &scaled_high, &upper)?;
}
Ok(InletDischarge {
bounds: [lower, upper],
zero,
bands,
})
}
struct Closed<'o, R: Runtime<DType = DType>> {
rows: &'o Selected<R>,
headwater: &'o Tensor<R>,
bands: &'o Bands<R>,
form_one: &'o Bounds<R>,
}
impl<R> Closed<'_, R>
where
R: Runtime<DType = DType>,
R::Client: CulvertClient<R>,
{
fn hull(&self, a: &mut Arithmetic<'_, R>) -> ArithmeticResult<Bounds<R>> {
let barrel = &self.rows.barrel;
let inlet = &self.rows.inlet;
let c = a.client();
let enter = |a: &mut Arithmetic<'_, R>, [lower, upper]: &Bounds<R>| a.bounds(lower, upper);
let d = a.point(barrel.open_rise())?;
let scale = enter(a, &inlet.scale)?;
let slope_term = enter(a, &inlet.slope_term)?;
let harmless =
|mask: &Tensor<R>, value: &Tensor<R>| c.where_cond(mask, self.headwater, value);
let k = a.point(&inlet.k)?;
let dk = a.mul(&d, &k)?;
let form_two = c.mul(
&self.bands.unsubmerged,
&c.sub(&barrel.one, &inlet.form_one)?,
)?;
let h_two = a.point(&harmless(&form_two, dk.upper())?)?;
let ln_h = a.log_positive(&h_two)?;
let ln_dk = a.log_positive(&dk)?;
let ratio = a.sub(&ln_h, &ln_dk)?;
let ln_x = a.div_positive(&ratio, &inlet.m)?;
let x_two = super::exp(a, &ln_x)?;
let q_two = a.mul(&x_two, &scale)?;
let low = enter(a, &inlet.low)?;
let high = enter(a, &inlet.high)?;
let h_mid = a.point(self.headwater)?;
let past = a.sub(&h_mid, &low)?;
let rise = a.sub(&high, &low)?;
let two = a.point(&barrel.constant(2.0)?)?;
let span = a.mul(&two, &rise)?;
let fraction = a.div_positive_interval(&past, &span)?;
let limit = a.point(&barrel.constant(UNSUBMERGED_LIMIT)?)?;
let x_mid = a.add(&limit, &fraction)?;
let x_mid = a.bounds(
&c.maximum(x_mid.lower(), &barrel.zero)?,
&c.maximum(x_mid.upper(), &barrel.zero)?,
)?;
let q_mid = a.mul(&x_mid, &scale)?;
let h_full = a.point(self.headwater)?;
let relative = a.div_positive(&h_full, barrel.open_rise())?;
let y = a.point(&inlet.y)?;
let offset = a.add(&y, &slope_term)?;
let excess = a.sub(&relative, &offset)?;
let square = a.div_positive(&excess, &inlet.c)?;
let square = a.bounds(
&c.maximum(square.lower(), &barrel.zero)?,
&c.maximum(square.upper(), &barrel.zero)?,
)?;
let x_full = a.sqrt_nonnegative(&square)?;
let q_full = a.mul(&x_full, &scale)?;
let free = [
c.where_cond(&inlet.form_one, &self.form_one[0], q_two.lower())?,
c.where_cond(&inlet.form_one, &self.form_one[1], q_two.upper())?,
];
let mut lower = barrel.constant(f64::INFINITY)?;
let mut upper = barrel.constant(f64::NEG_INFINITY)?;
for (mask, [low, high]) in [
(&self.bands.unsubmerged, free),
(
&self.bands.transition,
[q_mid.lower().clone(), q_mid.upper().clone()],
),
(
&self.bands.submerged,
[q_full.lower().clone(), q_full.upper().clone()],
),
] {
lower = c.where_cond(mask, &c.minimum(&lower, &low)?, &lower)?;
upper = c.where_cond(mask, &c.maximum(&upper, &high)?, &upper)?;
}
Ok([lower, upper])
}
}
pub(crate) fn any<R>(c: &R::Client, mask: &Tensor<R>) -> Result<bool, StructureError>
where
R: Runtime<DType = DType>,
R::Client: CulvertClient<R>,
{
Ok(c.sum(mask, &[0], false)?.item::<f64>()? != 0.0)
}