use super::Selected;
use super::inlet::{InletDischarge, any};
use super::lanes::Lanes;
use super::outlet_root::{self, Refine};
use super::warm;
use crate::structures::tensor::TensorStructureContext;
use crate::structures::tensor::culvert::CulvertHint;
use crate::structures::tensor::culvert::{Bounds, TensorCulvertLaw};
use crate::structures::{CulvertClient, StructureError};
use numr::dtype::DType;
use numr::ops::{BinaryOps, CompareOps, ConditionalOps, IndexingOps, ScalarOps};
use numr::runtime::Runtime;
use numr::tensor::Tensor;
use solvr::common::arithmetic::with_interval_arithmetic;
pub(crate) struct OutletDischarge<R: Runtime<DType = DType>> {
pub(crate) bounds: Bounds<R>,
pub(crate) solved: Tensor<R>,
pub(crate) full: Tensor<R>,
pub(crate) level: Tensor<R>,
pub(crate) dead: Tensor<R>,
pub(crate) searched: Tensor<R>,
}
pub(crate) fn invert<R>(
law: &TensorCulvertLaw<R>,
context: &TensorStructureContext<R>,
rows: &Selected<R>,
lanes: &Lanes<R>,
inlet: &InletDischarge<R>,
hint: Option<&CulvertHint<R>>,
) -> Result<OutletDischarge<R>, StructureError>
where
R: Runtime<DType = DType>,
R::Client: CulvertClient<R>,
{
let barrel = &rows.barrel;
let outlet_rows = &rows.outlet;
let view = outlet_rows.view(barrel);
let c = &barrel.client;
let one = &barrel.one;
let flowing = c.mul(&lanes.active, &c.sub(one, &inlet.zero)?)?;
let drowned = c.mul(&flowing, &c.ge(&lanes.tailwater, &view.rise)?)?;
let (closed, full_head, still) =
with_interval_arithmetic(c, DType::F64, "culvert outlet closed form", |a| {
let c = a.client();
let n = a.point(&outlet_rows.manning)?;
let length = a.point(&outlet_rows.length)?;
let ke = a.bounds(&outlet_rows.entrance[0], &outlet_rows.entrance[1])?;
let g = a.point(&outlet_rows.gravity)?;
let s = a.point(&outlet_rows.slope)?;
let area = a.bounds(&view.full_area[0], &view.full_area[1])?;
let radius = a.bounds(&view.full_radius[0], &view.full_radius[1])?;
let four = a.point(&barrel.constant(4.0)?)?;
let third = a.div_positive(&four, &barrel.constant(3.0)?)?;
let ln_radius = a.log_positive(&radius)?;
let scaled = a.mul(&third, &ln_radius)?;
let power = super::exp(a, &scaled)?;
let n_square = a.mul(&n, &n)?;
let rough = a.mul(&n_square, &length)?;
let per_head = a.div_positive_interval(&rough, &power)?;
let two = a.point(&barrel.constant(2.0)?)?;
let two_g = a.mul(&two, &g)?;
let friction = a.mul(&two_g, &per_head)?;
let unit = a.point(one)?;
let entry = a.add(&unit, &ke)?;
let k = a.add(&entry, &friction)?;
let gap = a.point(&c.where_cond(&drowned, &lanes.gap, one)?)?;
let driving = a.mul(&two_g, &gap)?;
let square = a.div_positive_interval(&driving, &k)?;
let velocity = a.sqrt_nonnegative(&square)?;
let discharge = a.mul(&area, &velocity)?;
let tw = a.point(&lanes.tailwater)?;
let drop = a.mul(&s, &length)?;
let level = a.sub(&tw, &drop)?;
let loss = a.mul(&per_head, &square)?;
let head = a.add(&level, &loss)?;
Ok((
[discharge.lower().clone(), discharge.upper().clone()],
head.lower().clone(),
[
c.maximum(level.lower(), &barrel.zero)?,
c.maximum(level.upper(), &barrel.zero)?,
],
))
})?;
let full = c.mul(&drowned, &c.ge(&full_head, &view.rise)?)?;
let open = c.mul(&flowing, &c.sub(one, &full)?)?;
let dead = c.mul(&open, &c.gt(&still[0], &lanes.headwater)?)?;
let level = c.mul(
&c.mul(&open, &c.sub(one, &dead)?)?,
&c.ge(&still[1], &lanes.headwater)?,
)?;
let candidates = c.mul(&c.mul(&open, &c.sub(one, &level)?)?, &c.sub(one, &dead)?)?;
let infinity = barrel.constant(f64::INFINITY)?;
let mut bounds = [
c.where_cond(&full, &closed[0], &infinity)?,
c.where_cond(&full, &closed[1], &infinity)?,
];
let mut solved = full.clone();
let mut searched = barrel.zero.clone();
let mut cold = candidates.clone();
if hint.is_some() {
let window = warm::window(barrel, lanes, &candidates, &inlet.bounds[1], hint)?;
if any(c, &window.warm)? {
let found = outlet_root::solve_warm(law, rows, lanes, &window)?;
let take = found.enclosed;
bounds = [
c.where_cond(&take, &found.bounds[0], &bounds[0])?,
c.where_cond(&take, &found.bounds[1], &bounds[1])?,
];
solved = c.maximum(&solved, &take)?;
cold = c.mul(&candidates, &c.sub(one, &take)?)?;
searched = take;
}
}
let doubled = c.mul_scalar(&inlet.bounds[1], 2.0)?;
let (search, ceiling) = if any(c, &cold)? {
let probe = c.where_cond(&cold, &inlet.bounds[1], &barrel.zero)?;
let lane: Vec<i64> = (0..barrel.width())
.map(|index| i64::try_from(index).unwrap_or(i64::MAX))
.collect();
let owner = Tensor::<R>::from_slice(&lane, &[barrel.width()], &barrel.device)?;
let [lower, upper] = outlet_root::residual(
law,
rows,
lanes,
&barrel.zero,
&owner,
&probe,
Refine::Uncertain,
)?;
let inlet_side = c.lt(&upper, &barrel.zero)?;
let outlet_side = c.gt(&lower, &barrel.zero)?;
(
c.mul(&cold, &c.sub(one, &inlet_side)?)?,
c.where_cond(&outlet_side, &inlet.bounds[1], &doubled)?,
)
} else {
(cold, doubled)
};
if any(c, &search)? {
let found = outlet_root::solve(law, rows, lanes, &search, &ceiling)?;
let unresolved = c.mul(
&search,
&c.sub(one, &c.maximum(&found.enclosed, &found.absent)?)?,
)?;
if let Some(lane) = super::first(&unresolved)? {
return Err(StructureError::numerical(
"culvert outlet headwater near the inlet discharge is unresolved or does not separate from the donor head, so the governing control is undetermined. The outlet march leaves a steep barrel unresolved where its critical depth reaches the crown. Lower the heads or supply a mild barrel",
)
.at(law.elements[lane]));
}
let take = c.mul(&search, &found.enclosed)?;
bounds = [
c.where_cond(&take, &found.bounds[0], &bounds[0])?,
c.where_cond(&take, &found.bounds[1], &bounds[1])?,
];
solved = c.maximum(&solved, &take)?;
searched = c.maximum(&searched, &take)?;
}
let solved_upper = c.where_cond(&solved, &bounds[1], &barrel.zero)?;
context.finite(
&c.index_put(&context.zero, 0, &law.lanes, &solved_upper)?,
"culvert outlet discharge enclosure",
)?;
Ok(OutletDischarge {
bounds,
solved,
full,
level,
dead,
searched,
})
}