use crate::structures::numerics::culvert::constants::{CRITICAL_FLOOR, FLOOR_CRITICAL_TOP};
use crate::structures::numerics::culvert::{
Arithmetic, BarrelClient, BarrelDepths, floor, section,
};
use crate::structures::tensor::codes;
use crate::structures::tensor::culvert::{FloorRows, TensorBarrelContext};
use numr::dtype::DType;
use numr::ops::{BinaryOps, CompareOps, ConditionalOps};
use numr::runtime::Runtime;
use numr::tensor::Tensor;
use solvr::common::arithmetic::{ArithmeticResult, Interval};
pub(super) struct CriticalHead<R: Runtime<DType = DType>> {
pub(super) head: Interval<R>,
pub(super) valid: Tensor<R>,
}
pub(super) fn critical_head<R>(
a: &mut Arithmetic<'_, R>,
barrel: &TensorBarrelContext<R>,
unit: &TensorBarrelContext<R>,
depths: &BarrelDepths<R>,
) -> ArithmeticResult<CriticalHead<R>>
where
R: Runtime<DType = DType>,
R::Client: BarrelClient<R>,
{
let c = a.client();
let zero = &barrel.zero;
let code = |value: f64| barrel.constant(value);
let status = &depths.status;
let valid = c.add(
&c.add(
&c.eq(status, &code(codes::CRITICAL_DRY)?)?,
&c.eq(status, &code(codes::CRITICAL_SOLVED)?)?,
)?,
&c.eq(status, &code(codes::CRITICAL_BELOW_FLOOR)?)?,
)?;
let lower = c.where_cond(&valid, &depths.lower, zero)?;
let upper = c.where_cond(&valid, &depths.upper, zero)?;
let low = a.point(&lower)?;
let high = a.point(&upper)?;
let rise = barrel.open_rise();
let s_low = a.div_positive(&low, rise)?.lower().clone();
let s_high = c.minimum(a.div_positive(&high, rise)?.upper(), &barrel.one)?;
let bottom = unit_head(a, barrel, unit, &s_low)?;
let top = unit_head(a, barrel, unit, &s_high)?;
let mut lower = c.where_cond(&c.gt(&s_low, zero)?, bottom.lower(), zero)?;
let mut upper = c.where_cond(&c.gt(&s_high, zero)?, top.upper(), zero)?;
if let Some(rows) = barrel.floor.as_deref() {
let open = c.sub(&rows.floored, &rows.boxed)?;
let [open_lower, open_upper] = open_heads(a, barrel, rows, [&s_low, &s_high])?;
lower = c.where_cond(&open, &open_lower, &lower)?;
upper = c.where_cond(&open, &open_upper, &upper)?;
}
let unit_bounds = a.bounds(&lower, &upper)?;
let d = a.point(rise)?;
let head = a.mul(&d, &unit_bounds)?;
Ok(CriticalHead { head, valid })
}
fn open_heads<R>(
a: &mut Arithmetic<'_, R>,
barrel: &TensorBarrelContext<R>,
rows: &FloorRows<R>,
[low, high]: [&Tensor<R>; 2],
) -> ArithmeticResult<[Tensor<R>; 2]>
where
R: Runtime<DType = DType>,
R::Client: BarrelClient<R>,
{
let c = a.client();
let zero = &barrel.zero;
let floor_value = barrel.constant(CRITICAL_FLOOR)?;
let top = barrel.constant(FLOOR_CRITICAL_TOP)?;
let clamp = |s: &Tensor<R>| c.minimum(&c.maximum(s, &floor_value)?, &top);
let bottom = open_head(a, barrel, rows, &clamp(low)?)?;
let crest = open_head(a, barrel, rows, &clamp(high)?)?;
let shallow = c.lt(low, &floor_value)?;
let lower = c.where_cond(&shallow, low, bottom.lower())?;
let upper = c.where_cond(&c.gt(high, zero)?, crest.upper(), zero)?;
Ok([lower, upper])
}
fn open_head<R>(
a: &mut Arithmetic<'_, R>,
barrel: &TensorBarrelContext<R>,
rows: &FloorRows<R>,
s: &Tensor<R>,
) -> ArithmeticResult<Interval<R>>
where
R: Runtime<DType = DType>,
R::Client: BarrelClient<R>,
{
let framed = barrel.frame.as_ref().map_or(barrel, |frame| &frame.context);
let Some(open) = framed.floor.as_deref() else {
return Err(numr::error::Error::InvalidArgument {
arg: "barrel",
reason: "the barrel frame lost its floor rows".into(),
}
.into());
};
let geometry = floor::at_fraction(a, barrel, rows, s)?.geometry;
let two = a.point(&barrel.constant(2.0)?)?;
let width = a.mul(&two, &geometry.top_width)?;
let depth = a.div_positive_interval(&geometry.area, &width)?;
let scaled = a.div_positive(&depth, &open.top)?;
let point = a.point(s)?;
a.add(&point, &scaled)
}
pub(crate) fn section_unit_head<R>(
a: &mut Arithmetic<'_, R>,
barrel: &TensorBarrelContext<R>,
unit: &TensorBarrelContext<R>,
s: &Tensor<R>,
) -> ArithmeticResult<Interval<R>>
where
R: Runtime<DType = DType>,
R::Client: BarrelClient<R>,
{
let base = unit_head(a, barrel, unit, s)?;
let Some(rows) = barrel.floor.as_deref() else {
return Ok(base);
};
let c = a.client();
let open = c.sub(&rows.floored, &rows.boxed)?;
let framed = open_head(a, barrel, rows, s)?;
super::super::choose(a, &open, &framed, &base)
}
pub(crate) fn unit_head<R>(
a: &mut Arithmetic<'_, R>,
barrel: &TensorBarrelContext<R>,
unit: &TensorBarrelContext<R>,
s: &Tensor<R>,
) -> ArithmeticResult<Interval<R>>
where
R: Runtime<DType = DType>,
R::Client: BarrelClient<R>,
{
let c = a.client();
let half = barrel.constant(0.5)?;
let wet = c.gt(s, &barrel.zero)?;
let curved = c.sub(&barrel.one, &barrel.boxed)?;
let curved_rows = c.mul(&wet, &curved)?;
let fraction = c.where_cond(&curved_rows, s, &half)?;
let geometry = section::at_depth(a, unit, &fraction)?.geometry;
let two = a.point(&barrel.constant(2.0)?)?;
let width = a.mul(&two, &geometry.top_width)?;
let depth = a.div_positive_interval(&geometry.area, &width)?;
let point = a.point(&fraction)?;
let section_head = a.add(&point, &depth)?;
let box_fraction = a.point(s)?;
let three_halves = a.point(&barrel.constant(1.5)?)?;
let rectangle = a.mul(&three_halves, &box_fraction)?;
super::super::choose(a, &curved, §ion_head, &rectangle)
}