use super::March;
use super::coordinates::{area, coordinate};
use super::integrand::{ProfileParams, shape, slopes};
use super::jet::{self, Term};
use super::trig::{fill, negate};
use super::zone::{self, Depths, depths};
use crate::structures::numerics::culvert::{Arithmetic, BarrelClient, choose};
use crate::structures::tensor::culvert::{TensorBarrelContext, TensorOutletContext};
use numr::dtype::DType;
use numr::ops::{BinaryOps, CompareOps, ConditionalOps, UnaryOps};
use numr::runtime::Runtime;
use numr::tensor::Tensor;
use solvr::common::arithmetic::{ArithmeticResult, Interval};
pub(crate) struct HeadSlopes<R: Runtime<DType = DType>> {
pub(crate) gain: Tensor<R>,
pub(crate) tail: Tensor<R>,
pub(crate) known: Tensor<R>,
}
pub(crate) struct SlopeOperands<'o, R: Runtime<DType = DType>> {
pub(crate) barrel: &'o TensorBarrelContext<R>,
pub(crate) outlet: &'o TensorOutletContext<R>,
pub(crate) march: &'o March<R>,
pub(crate) discharge: &'o Tensor<R>,
pub(crate) tailwater: &'o Tensor<R>,
}
struct Section<R: Runtime<DType = DType>> {
area: Interval<R>,
froude: Term<R>,
friction: Term<R>,
valid: Tensor<R>,
}
fn section<R>(
a: &mut Arithmetic<'_, R>,
barrel: &TensorBarrelContext<R>,
params: &ProfileParams<R>,
depth: &Interval<R>,
) -> ArithmeticResult<Section<R>>
where
R: Runtime<DType = DType>,
R::Client: BarrelClient<R>,
{
let c = a.client();
let rise = &barrel.rise;
let outline = params.outline();
let v = coordinate(a, &outline, rise, depth)?;
let probe = shape(a, &outline, &v, 1)?;
let unit = jet::coefficient(a, &probe.area, 0, rise)?;
let valid = c.gt(unit.lower(), &barrel.zero)?;
let half = a.point(&barrel.half_rise)?;
let harmless = coordinate(a, &outline, rise, &half)?;
let v = choose(a, &valid, &v, &harmless)?;
let jets = shape(a, &outline, &v, 1)?;
let values = slopes(a, params, &jets)?;
Ok(Section {
area: area(a, &outline, rise, &v)?,
froude: jet::coefficient(a, &values.froude, 0, rise)?,
friction: jet::coefficient(a, &values.friction, 0, rise)?,
valid,
})
}
fn quotient<R>(
a: &mut Arithmetic<'_, R>,
x: &Interval<R>,
y: &Interval<R>,
) -> ArithmeticResult<(Interval<R>, Tensor<R>)>
where
R: Runtime<DType = DType>,
R::Client: BarrelClient<R>,
{
let c = a.client();
let like = y.lower();
let positive = c.gt(like, &fill(like, 0.0)?)?;
let one = fill(like, 1.0)?;
let safe = a.bounds(
&c.where_cond(&positive, y.lower(), &one)?,
&c.where_cond(&positive, y.upper(), &one)?,
)?;
Ok((a.div_positive_interval(x, &safe)?, positive))
}
fn magnitude<R>(a: &mut Arithmetic<'_, R>, x: &Interval<R>) -> ArithmeticResult<Interval<R>>
where
R: Runtime<DType = DType>,
R::Client: BarrelClient<R>,
{
let c = a.client();
let zero = fill(x.lower(), 0.0)?;
let upper = c.maximum(&c.abs(x.lower())?, &c.abs(x.upper())?)?;
let above = c.maximum(x.lower(), &zero)?;
let below = c.maximum(&c.neg(x.upper())?, &zero)?;
a.bounds(&c.maximum(&above, &below)?, &upper)
}
fn upper_point<R>(a: &mut Arithmetic<'_, R>, upper: &Tensor<R>) -> ArithmeticResult<Interval<R>>
where
R: Runtime<DType = DType>,
R::Client: BarrelClient<R>,
{
a.bounds(upper, upper)
}
struct Bound<R: Runtime<DType = DType>> {
gain: Interval<R>,
tail: Interval<R>,
known: Tensor<R>,
}
struct Flow<R: Runtime<DType = DType>> {
kinetic: Interval<R>,
ke: Interval<R>,
slope: Interval<R>,
rate: Interval<R>,
}
fn free_bound<R>(
a: &mut Arithmetic<'_, R>,
operands: &SlopeOperands<'_, R>,
flow: &Flow<R>,
free: &Tensor<R>,
) -> ArithmeticResult<Bound<R>>
where
R: Runtime<DType = DType>,
R::Client: BarrelClient<R>,
{
let SlopeOperands {
barrel,
march,
tailwater,
..
} = *operands;
let c = a.client();
let classes = &march.classes;
let params = &classes.spec.params;
let zero = &barrel.zero;
let one = a.point(&barrel.one)?;
let two = a.point(&barrel.constant(2.0)?)?;
let Depths {
end: y_e,
start: y_s,
low: y_lo,
monotone,
peak,
} = depths(a, operands, free)?;
let end = section(a, barrel, params, &y_e)?;
let start = section(a, barrel, params, &y_s)?;
let low = section(a, barrel, params, &y_lo)?;
let rising = c.gt(&classes.spec.sign, zero)?;
let crown = &classes.falling;
let subcritical = a.sub(&one, &end.froude)?;
let scaled = a.mul(&flow.ke, &end.froude)?;
let (excess, sub_ok) = quotient(a, &scaled, &subcritical)?;
let d_e = a.sub(&one, &excess)?;
let end_square = a.mul(&end.area, &end.area)?;
let (velocity, end_ok) = quotient(a, &flow.kinetic, &end_square)?;
let deep_area = a.bounds(
&c.minimum(start.area.lower(), low.area.lower())?,
&c.minimum(start.area.upper(), low.area.upper())?,
)?;
let deep_square = a.mul(&deep_area, &deep_area)?;
let (deep, deep_ok) = quotient(a, &flow.kinetic, &deep_square)?;
let lean = a.mul(&flow.slope, &low.froude)?;
let crown_ok = c.ge(low.friction.lower(), lean.upper())?;
let falling_ok = c.mul(&deep_ok, &c.maximum(&monotone, &crown_ok)?)?;
let falling_low = c.where_cond(&falling_ok, deep.lower(), zero)?;
let omega_low = c.where_cond(&rising, velocity.lower(), &falling_low)?;
let climb = a.sub(&y_e, &y_s)?;
let target = a.bounds(&classes.target[0], &classes.target[1])?;
let uphill = a.point(&c.maximum(&operands.outlet.slope, zero)?)?;
let drop = a.mul(&uphill, &target)?;
let reach = a.add(&climb, &drop)?;
let twice = a.mul(&two, &reach)?;
let rise_high = a.add(&velocity, &twice)?;
let sigma_e = a.sub(&end.friction, &flow.slope)?;
let sigma_s = a.sub(&start.friction, &flow.slope)?;
let gap_e = magnitude(a, &sigma_e)?;
let gap_s = magnitude(a, &sigma_s)?;
let (ratio, ratio_ok) = quotient(a, &gap_e, &gap_s)?;
let mut widest = c.maximum(ratio.upper(), &barrel.one)?;
let mut steepest = c.maximum(start.friction.upper(), end.friction.upper())?;
let mut zone_ok = barrel.one.clone();
if let Some(arcs) = &operands.outlet.arcs {
let zone = zone::falling(a, barrel, params, &arcs.arced, [&y_e, &y_s], &peak, &gap_e)?;
let touches = &zone.touches;
widest = c.where_cond(touches, &c.maximum(&widest, &zone.ratio)?, &widest)?;
let most = c.maximum(&steepest, &zone.friction)?;
steepest = c.where_cond(touches, &most, &steepest)?;
zone_ok = zone.known;
}
let widest = upper_point(a, &widest)?;
let steepest = upper_point(a, &steepest)?;
let span = a.mul(&two, &target)?;
let spread = a.mul(&span, &widest)?;
let friction = a.mul(&spread, &steepest)?;
let tw = a.point(tailwater)?;
let d = a.point(&barrel.rise)?;
let above = a.sub(&tw, &d)?;
let full = a.add(&flow.rate, &flow.slope)?;
let lift = a.mul(&above, &full)?;
let lifted = a.mul(&gap_e, &lift)?;
let doubled = a.mul(&two, &lifted)?;
let rate_square = a.mul(&flow.rate, &flow.rate)?;
let (shift, shift_ok) = quotient(a, &doubled, &rate_square)?;
let nothing = a.point(zero)?;
let shift = choose(a, crown, &shift, ¬hing)?;
let shifted = a.add(&velocity, &shift)?;
let fall_high = a.add(&shifted, &friction)?;
let fall_ok = c.mul(
&ratio_ok,
&c.maximum(&c.sub(&barrel.one, crown)?, &shift_ok)?,
)?;
let fall_ok = match &operands.outlet.arcs {
None => fall_ok,
Some(_) => c.mul(&fall_ok, &zone_ok)?,
};
let high_ok = c.where_cond(&rising, &monotone, &fall_ok)?;
let omega_high = c.where_cond(&rising, rise_high.upper(), fall_high.upper())?;
let omega_high = c.where_cond(&high_ok, &omega_high, &omega_low)?;
let omega = a.bounds(&omega_low, &c.maximum(&omega_high, &omega_low)?)?;
let product = a.mul(&d_e, &omega)?;
let entry = a.mul(&flow.ke, &velocity)?;
let (entry, _) = quotient(a, &entry, &subcritical)?;
let gain = a.add(&product, &entry)?;
let sign_ok = c.maximum(&c.ge(d_e.lower(), zero)?, &high_ok)?;
let unit = upper_point(a, &barrel.one)?;
let bounded = c.where_cond(
&ratio_ok,
&c.minimum(ratio.upper(), &barrel.one)?,
&barrel.one,
)?;
let share = c.where_cond(&monotone, &bounded, ratio.upper())?;
let share_ok = c.maximum(&monotone, &ratio_ok)?;
let share = upper_point(a, &share)?;
let start_sub = a.sub(&unit, &start.froude)?;
let start_sub = magnitude(a, &start_sub)?;
let outlet_z = a.mul(&start_sub, &share)?;
let decline = negate(a, &flow.rate)?;
let (crown_z, crown_ok) = quotient(a, &gap_e, &decline)?;
let z = choose(a, crown, &crown_z, &outlet_z)?;
let z_ok = c.where_cond(crown, &crown_ok, &share_ok)?;
let scale = magnitude(a, &d_e)?;
let tail = a.mul(&scale, &z)?;
let known = c.mul(
&c.mul(&c.mul(free, &sub_ok)?, &c.mul(&end_ok, &sign_ok)?)?,
&z_ok,
)?;
let sections = c.mul(&c.mul(&end.valid, &start.valid)?, &low.valid)?;
let known = c.mul(&known, §ions)?;
Ok(Bound { gain, tail, known })
}
pub(crate) fn head_slopes<R>(
a: &mut Arithmetic<'_, R>,
operands: &SlopeOperands<'_, R>,
) -> ArithmeticResult<HeadSlopes<R>>
where
R: Runtime<DType = DType>,
R::Client: BarrelClient<R>,
{
let viewed = SlopeOperands {
barrel: operands.outlet.view(operands.barrel),
..*operands
};
let operands = &viewed;
let SlopeOperands {
barrel,
outlet,
march,
discharge,
tailwater,
} = *operands;
let c = a.client();
let classes = &march.classes;
let reach = &march.reach;
let zero = &barrel.zero;
let one = &barrel.one;
let not = |mask: &Tensor<R>| c.sub(one, mask);
let wet = c.gt(discharge, zero)?;
let q = a.point(&c.where_cond(&wet, discharge, one)?)?;
let square = a.mul(&q, &q)?;
let flow = Flow {
kinetic: a.div_positive(&square, &outlet.gravity)?,
ke: a.bounds(&outlet.entrance[0], &outlet.entrance[1])?,
slope: a.point(&outlet.slope)?,
rate: a.bounds(&classes.rate[0], &classes.rate[1])?,
};
let unit = a.point(one)?;
let loss = a.add(&unit, &flow.ke)?;
let [inlet_low, inlet_high] = &march.headwater.inlet;
let finite = c.mul(&c.eq(inlet_low, inlet_low)?, &c.eq(inlet_high, inlet_high)?)?;
let free = c.mul(
&c.mul(&reach.solve, ¬(&reach.open)?)?,
&c.mul(&finite, ¬(&classes.tie)?)?,
)?;
let free_bound = free_bound(a, operands, &flow, &free)?;
let critical = c.mul(&c.maximum(&classes.critical_now, &reach.critical)?, &wet)?;
let critical_depth = a.bounds(&classes.critical[0], &classes.critical[1])?;
let outline = outlet.outline(barrel);
let v_c = coordinate(a, &outline, &barrel.rise, &critical_depth)?;
let critical_area = area(a, &outline, &barrel.rise, &v_c)?;
let critical_square = a.mul(&critical_area, &critical_area)?;
let (critical_gain, critical_ok) = quotient(a, &flow.kinetic, &critical_square)?;
let full_area = a.bounds(&barrel.full_area[0], &barrel.full_area[1])?;
let full_square = a.mul(&full_area, &full_area)?;
let lossy = a.mul(&loss, &flow.kinetic)?;
let (full_gain, full_ok) = quotient(a, &lossy, &full_square)?;
let crown = c.mul(&reach.crown_full, &wet)?;
let entire = c.mul(&classes.full_inlet, &wet)?;
let [yc_low, yc_high] = &classes.critical;
let h0 = a.bounds(
&c.maximum(tailwater, yc_low)?,
&c.maximum(tailwater, yc_high)?,
)?;
let start = section(a, barrel, &classes.spec.params, &h0)?;
let start_sub = a.sub(&unit, &start.froude)?;
let start_sub = magnitude(a, &start_sub)?;
let sigma = a.sub(&start.friction, &flow.slope)?;
let sigma = magnitude(a, &sigma)?;
let rate = magnitude(a, &flow.rate)?;
let product = a.mul(&rate, &start_sub)?;
let (crown_tail, crown_ok) = quotient(a, &product, &sigma)?;
let crown_ok = c.mul(&crown_ok, &start.valid)?;
let pick = |mask: &Tensor<R>, on: &Tensor<R>, off: &Tensor<R>| c.where_cond(mask, on, off);
let mut gain = pick(&free, free_bound.gain.lower(), zero)?;
let mut tail = pick(&free, free_bound.tail.upper(), zero)?;
let mut known = free_bound.known;
gain = pick(&entire, full_gain.lower(), &gain)?;
tail = pick(&entire, one, &tail)?;
known = pick(&entire, &full_ok, &known)?;
gain = pick(&crown, full_gain.lower(), &gain)?;
tail = pick(&crown, crown_tail.upper(), &tail)?;
known = pick(&crown, &c.mul(&full_ok, &crown_ok)?, &known)?;
gain = pick(&critical, critical_gain.lower(), &gain)?;
tail = pick(&critical, zero, &tail)?;
known = pick(&critical, &critical_ok, &known)?;
Ok(HeadSlopes {
gain,
tail,
known: c.mul(&known, &wet)?,
})
}