use super::residual::{residual, target};
use crate::structures::numerics::culvert::constants::CRITICAL_FLOOR;
use crate::structures::numerics::culvert::roots::{self, Ends, Signs};
use crate::structures::numerics::culvert::{Arithmetic, BarrelClient};
use crate::structures::tensor::codes;
use crate::structures::tensor::culvert::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) fn classify<R>(
a: &mut Arithmetic<'_, R>,
barrel: &TensorBarrelContext<R>,
unit: &TensorBarrelContext<R>,
discharge: &Tensor<R>,
roughness: &Tensor<R>,
slope: &Tensor<R>,
) -> ArithmeticResult<(Ends<R>, Tensor<R>)>
where
R: Runtime<DType = DType>,
R::Client: BarrelClient<R>,
{
let floor = barrel.constant(CRITICAL_FLOOR)?;
let top = &barrel.normal_top;
let row_target = target(a, barrel, discharge, roughness, slope)?;
let bottom = residual(a, barrel, unit, &row_target, &floor)?;
let crown = residual(a, barrel, unit, &row_target, top)?;
let c = a.client();
let one = &barrel.one;
let zero = &barrel.zero;
let sloped = c.gt(slope, zero)?;
let flat = c.sub(one, &sloped)?;
let wet = c.mul(&sloped, &c.gt(discharge, zero)?)?;
let Signs {
below,
straddle,
solved,
near_top,
above,
past,
start,
} = roots::classify(
a,
barrel,
&wet,
[&bottom, &crown],
top,
&barrel.normal_slope,
)?;
let [log_lower, log_upper] = &barrel.log_peak;
let log_peak = a.bounds(log_lower, log_upper)?;
let three = a.point(&barrel.constant(3.0)?)?;
let thrice_peak = a.mul(&three, &log_peak)?;
let at_peak = a.sub(&thrice_peak, &row_target)?;
let beyond = c.lt(at_peak.upper(), zero)?;
let over = c.maximum(&barrel.boxed, &beyond)?;
let above_capacity = c.mul(&above, &over)?;
let below_peak = c.sub(&above, &above_capacity)?;
let near_capacity = c.add(&near_top, &below_peak)?;
let capacity_top = &barrel.conveyance_peak[1];
let mut lower = zero.clone();
let mut upper = c.where_cond(&below, &floor, zero)?;
upper = c.where_cond(&straddle, &past, &upper)?;
lower = c.where_cond(&near_top, &start, &lower)?;
upper = c.where_cond(&near_capacity, capacity_top, &upper)?;
lower = c.where_cond(&below_peak, top, &lower)?;
lower = c.where_cond(&solved, &floor, &lower)?;
upper = c.where_cond(&solved, top, &upper)?;
let code = |value: f64| barrel.constant(value);
let below_floor = code(codes::NORMAL_BELOW_FLOOR)?;
let mut status = code(codes::NORMAL_DRY)?;
status = c.where_cond(&below, &below_floor, &status)?;
status = c.where_cond(&straddle, &below_floor, &status)?;
status = c.where_cond(&near_capacity, &code(codes::NORMAL_NEAR_CAPACITY)?, &status)?;
status = c.where_cond(
&above_capacity,
&code(codes::NORMAL_ABOVE_CAPACITY)?,
&status,
)?;
status = c.where_cond(&solved, &code(codes::NORMAL_SOLVED)?, &status)?;
status = c.where_cond(&flat, &code(codes::NORMAL_NON_POSITIVE_SLOPE)?, &status)?;
let (limb_top, limb_slope) = limb(a, barrel, unit, &row_target)?;
Ok((
Ends {
target: [row_target.lower().clone(), row_target.upper().clone()],
status,
enclosed: c.sub(&c.sub(one, &above_capacity)?, &flat)?,
solver_top: c.where_cond(&solved, &limb_top, &floor)?,
solved,
fraction: [lower, upper],
},
limb_slope,
))
}
fn limb<R>(
a: &mut Arithmetic<'_, R>,
barrel: &TensorBarrelContext<R>,
unit: &TensorBarrelContext<R>,
row_target: &Interval<R>,
) -> ArithmeticResult<(Tensor<R>, Tensor<R>)>
where
R: Runtime<DType = DType>,
R::Client: BarrelClient<R>,
{
let mut top = barrel.normal_top.clone();
let mut slope = barrel.normal_slope.clone();
let Some(knots) = &barrel.limb else {
return Ok((top, slope));
};
let c = a.client();
let arced = match &barrel.arcs {
Some(arcs) => arcs.arced.clone(),
None => barrel.zero.clone(),
};
let open = match &barrel.floor {
Some(rows) => c.maximum(&arced, &c.sub(&rows.floored, &rows.boxed)?)?,
None => arced,
};
for [knot, floor] in knots.iter().rev() {
let value = residual(a, barrel, unit, row_target, knot)?;
let above = c.mul(&open, &c.gt(value.lower(), &barrel.zero)?)?;
top = c.where_cond(&above, knot, &top)?;
slope = c.where_cond(&above, floor, &slope)?;
}
Ok((top, slope))
}