use crate::structures::numerics::culvert::{Arithmetic, BarrelClient, choose, floor, section};
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(crate) fn target<R>(
a: &mut Arithmetic<'_, R>,
barrel: &TensorBarrelContext<R>,
discharge: &Tensor<R>,
gravity: &Tensor<R>,
) -> ArithmeticResult<Interval<R>>
where
R: Runtime<DType = DType>,
R::Client: BarrelClient<R>,
{
let c = a.client();
let wet = c.gt(discharge, &barrel.zero)?;
let q = a.point(&c.where_cond(&wet, discharge, &barrel.one)?)?;
let ln_q = a.log_positive(&q)?;
let g = a.point(gravity)?;
let ln_g = a.log_positive(&g)?;
let d = a.point(barrel.open_rise())?;
let ln_d = a.log_positive(&d)?;
let b = a.point(&barrel.span)?;
let ln_b = a.log_positive(&b)?;
let two = a.point(&barrel.constant(2.0)?)?;
let three = a.point(&barrel.constant(3.0)?)?;
let five = a.point(&barrel.constant(5.0)?)?;
let twice_q = a.mul(&two, &ln_q)?;
let flow = a.sub(&twice_q, &ln_g)?;
let circle = a.mul(&five, &ln_d)?;
let circle = match &barrel.floor {
Some(rows) => {
let open = c.sub(&rows.floored, &rows.boxed)?;
let ln_scale = floor::log_scale(a, barrel)?;
let framed = a.mul(&five, &ln_scale)?;
choose(a, &open, &framed, &circle)?
}
None => circle,
};
let twice_b = a.mul(&two, &ln_b)?;
let thrice_d = a.mul(&three, &ln_d)?;
let rectangle = a.add(&twice_b, &thrice_d)?;
let scale = choose(a, &barrel.boxed, &rectangle, &circle)?;
a.sub(&flow, &scale)
}
pub(crate) fn residual<R>(
a: &mut Arithmetic<'_, R>,
barrel: &TensorBarrelContext<R>,
unit: &TensorBarrelContext<R>,
target: &Interval<R>,
fraction: &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 three = a.point(&barrel.constant(3.0)?)?;
let circle_fraction = c.where_cond(&barrel.boxed, &half, fraction)?;
let geometry = section::at_depth(a, unit, &circle_fraction)?.geometry;
let ln_area = a.log_positive(&geometry.area)?;
let ln_top = a.log_positive(&geometry.top_width)?;
let thrice_area = a.mul(&three, &ln_area)?;
let circle = a.sub(&thrice_area, &ln_top)?;
let box_fraction = c.where_cond(&barrel.boxed, fraction, &half)?;
let s = a.point(&box_fraction)?;
let ln_s = a.log_positive(&s)?;
let rectangle = a.mul(&three, &ln_s)?;
let shape = choose(a, &barrel.boxed, &rectangle, &circle)?;
let shape = match &barrel.floor {
Some(rows) => {
let open = floor::at_fraction(a, barrel, rows, fraction)?;
let ln_area = a.log_positive(&open.geometry.area)?;
let ln_top = a.log_positive(&open.geometry.top_width)?;
let thrice_area = a.mul(&three, &ln_area)?;
let framed = a.sub(&thrice_area, &ln_top)?;
choose(a, &open.rows, &framed, &shape)?
}
None => shape,
};
a.sub(&shape, target)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::structures::numerics::culvert::constants::CRITICAL_FLOOR;
use crate::structures::{CulvertBarrel, StructureError};
use numr::runtime::cpu::{CpuDevice, CpuRuntime};
use solvr::common::arithmetic::with_interval_arithmetic;
fn ends(
barrels: Vec<CulvertBarrel>,
discharges: &[f64],
) -> Result<[[Vec<f64>; 2]; 2], StructureError> {
let device = CpuDevice::new();
let client = CpuRuntime::default_client(&device);
let barrel = TensorBarrelContext::<CpuRuntime>::new(client.clone(), &barrels)?;
let unit = barrel.unit()?;
let discharge = Tensor::<CpuRuntime>::from_slice(discharges, &[discharges.len()], &device)?;
let gravity = barrel.constant(9.81)?;
let floor = barrel.constant(CRITICAL_FLOOR)?;
let [bottom, top] = with_interval_arithmetic(&client, DType::F64, "ends", |a| {
let row_target = target(a, &barrel, &discharge, &gravity)?;
let bottom = residual(a, &barrel, &unit, &row_target, &floor)?;
let top = residual(a, &barrel, &unit, &row_target, &barrel.critical_top)?;
Ok([bottom, top])
})?;
let pair = |i: &Interval<CpuRuntime>| -> Result<[Vec<f64>; 2], StructureError> {
Ok([
i.lower().try_to_vec::<f64>()?,
i.upper().try_to_vec::<f64>()?,
])
};
Ok([pair(&bottom)?, pair(&top)?])
}
#[test]
fn residual_signs_bracket_interior_roots_and_flag_the_ends() -> Result<(), StructureError> {
let circle = CulvertBarrel::circular(1.0)?;
let rectangle = CulvertBarrel::rectangular_box(2.5, 0.8)?;
let barrels = vec![circle, rectangle, circle, rectangle, rectangle];
let discharges = [0.77, 2.8, 1e-30, 1e-30, 6.0];
let [[bottom_lower, bottom_upper], [top_lower, top_upper]] = ends(barrels, &discharges)?;
for row in 0..2 {
assert!(bottom_upper[row] < 0.0 && 0.0 < top_lower[row], "row={row}");
}
for (row, lower) in bottom_lower.iter().enumerate().take(4).skip(2) {
assert!(*lower > 0.0, "row={row}");
}
assert!(top_upper[4] < 0.0);
for row in 0..discharges.len() {
assert!(bottom_lower[row] <= bottom_upper[row] && top_lower[row] <= top_upper[row]);
assert!(
bottom_upper[row] < top_lower[row],
"row={row}: the residual must rise"
);
}
Ok(())
}
}