hydr-structures 0.3.0

Hydraulic structures for the hydr simulation crates
Documentation
//! End classification of the normal domain `[2^-30, top]` in `s = y/D`, before any solver call.
//!
//! `top` is `s_N`: `0.93818` for a circle, the crown `1` for a box and the certified
//! rising-limb top for an arc barrel. `f` is the residual enclosure, `kappa` the row slope
//! floor and `s_K` the upper bound of the row's conveyance peak enclosure, `1` for a box.
//!
//! | Rows | Status | Enclosure in `s` |
//! | --- | --- | --- |
//! | `S <= 0` | `NonPositiveSlope` | none |
//! | `Q = 0` | `Dry` | `[0, 0]` |
//! | `f(floor).lower > 0` | `BelowFloor` | `[0, floor]` |
//! | `f(floor)` holds a positive value and a nonpositive lower end | `BelowFloor` | `[0, floor - f(floor).lower / kappa]` |
//! | `f(floor) <= 0 <= f(top)` | `Solved` | from the solver |
//! | `f(floor) <= 0`, `f(top)` holds a negative value and a nonnegative upper end | `NearCapacity` | `[top - f(top).upper / kappa, s_K]` |
//! | `f(top).upper < 0`, circle or arc target not above `3 ln K_max` | `NearCapacity` | `[s_N, s_K]` |
//! | `f(top).upper < 0`, box, or circle or arc target above `3 ln K_max` | `AboveCapacity` | none |
//!
//! A `NearCapacity` row holds its rising-limb root in the enclosure when the root exists.
//! A circle or arc barrel has one up to the peak `K_max`. A box has one up to the crown limit
//! `K(D^-)`. `ln K_max` holds a row's log peak enclosure.

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};

/// Classifies every row from the residual at both domain ends and the circle peak.
///
/// Returns the row classes with the slope floor of each row's solver domain.
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,
    )?;

    // 3 ln k_max - target encloses the circle and arc residual at the conveyance peak.
    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)?;

    // A rising-limb root above s_N lies below the peak. A box root lies at or below the crown,
    // which is the box peak bound.
    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,
    ))
}

/// The domain top and slope floor of each bracketed row: `s_N` and `kappa_n`, or on an arc row
/// or a floored circle row the first rising-limb knot `s_j` certified above the root, with
/// `kappa_j`. A circle or box row without a floor keeps `s_N` and `kappa_n`.
///
/// `r_n(s_j).lower > 0` places the root below `s_j`, so `[2^-30, s_j]` still brackets it.
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,
    };
    // From the highest knot down, so the lowest knot above the root decides.
    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))
}