mod closed;
#[cfg(test)]
mod fixture;
use super::Selected;
use super::inlet::{InletDischarge, any};
use super::lanes::Lanes;
use super::outlet::OutletDischarge;
use crate::structures::numerics::culvert::outlet::{self, Accuracy};
use crate::structures::numerics::culvert::{Arithmetic, RowNames};
use crate::structures::tensor::culvert::TensorCulvertLaw;
use crate::structures::{CulvertClient, StructureError};
use closed::{full_sensitivity, inlet_sensitivity};
use numr::dtype::DType;
use numr::ops::{BinaryOps, ConditionalOps};
use numr::runtime::{Runtime, RuntimeClient};
use numr::tensor::Tensor;
use solvr::common::arithmetic::{ArithmeticResult, Interval, with_interval_arithmetic};
const E_FLOOR: f64 = f64::from_bits(0x2ff0_0000_0000_0000);
const CAP: f64 = f64::from_bits(0x4ff0_0000_0000_0000);
pub(crate) struct Governs<R: Runtime<DType = DType>> {
pub(crate) inlet: Tensor<R>,
pub(crate) outlet: Tensor<R>,
pub(crate) tie: Tensor<R>,
}
pub(crate) fn bound<R>(
law: &TensorCulvertLaw<R>,
rows: &Selected<R>,
lanes: &Lanes<R>,
inlet: &InletDischarge<R>,
outlet_discharge: &OutletDischarge<R>,
governs: &Governs<R>,
point: &Tensor<R>,
) -> Result<(Tensor<R>, Profile<R>), StructureError>
where
R: Runtime<DType = DType>,
R::Client: CulvertClient<R>,
{
let barrel = &rows.barrel;
let c = &barrel.client;
let one = &barrel.one;
let inlet_side = c.maximum(&governs.inlet, &governs.tie)?;
let outlet_side = c.maximum(&governs.outlet, &governs.tie)?;
let full = c.mul(&outlet_side, &outlet_discharge.full)?;
let profile_side = c.mul(&outlet_side, &c.sub(one, &outlet_discharge.full)?)?;
let [inlet_bound, full_bound] =
with_interval_arithmetic(c, DType::F64, "culvert inlet sensitivity", |a| {
Ok([
inlet_sensitivity(a, rows, lanes, inlet, &inlet_side)?,
full_sensitivity(a, rows, lanes, &full)?,
])
})?;
let profile = if any(c, &profile_side)? {
profile_sensitivity(law, rows, lanes, &profile_side, point)?
} else {
Profile {
bound: barrel.zero.clone(),
head: barrel.zero.clone(),
tail: barrel.zero.clone(),
}
};
let outlet_bound = c.where_cond(&full, &full_bound, &profile.bound)?;
let combined = c.where_cond(
&governs.inlet,
&inlet_bound,
&c.where_cond(
&governs.outlet,
&outlet_bound,
&c.maximum(&inlet_bound, &outlet_bound)?,
)?,
)?;
let flowing = c.maximum(&inlet_side, &governs.outlet)?;
Ok((c.where_cond(&flowing, &combined, &barrel.zero)?, profile))
}
fn reciprocal<R>(a: &mut Arithmetic<'_, R>, e: &Tensor<R>) -> ArithmeticResult<Tensor<R>>
where
R: Runtime<DType = DType>,
R::Client: CulvertClient<R>,
{
let c = a.client();
let floor = Tensor::<R>::full_scalar(e.shape(), DType::F64, E_FLOOR, c.device())?;
let safe = c.maximum(e, &floor)?;
let ones = Tensor::<R>::ones(e.shape(), DType::F64, c.device())?;
let unit = a.point(&ones)?;
Ok(a.div_positive(&unit, &safe)?.upper().clone())
}
fn profile_sensitivity<R>(
law: &TensorCulvertLaw<R>,
rows: &Selected<R>,
lanes: &Lanes<R>,
wanted: &Tensor<R>,
point: &Tensor<R>,
) -> Result<Profile<R>, StructureError>
where
R: Runtime<DType = DType>,
R::Client: CulvertClient<R>,
{
let barrel = &rows.barrel;
let c = &barrel.client;
let q = c.where_cond(wanted, point, &barrel.rise)?;
let tailwater = &lanes.tailwater;
let march = outlet::march(
RowNames::mapped(&law.barrels, &lanes.rows),
barrel,
&rows.unit,
&rows.outlet,
&q,
tailwater,
Accuracy::Coarse,
)?;
let operands = outlet::SlopeOperands {
barrel,
outlet: &rows.outlet,
march: &march,
discharge: &q,
tailwater,
};
let [bound, head, tail] =
with_interval_arithmetic(c, DType::F64, "culvert outlet sensitivity", |a| {
let slopes = outlet::head_slopes(a, &operands)?;
let c = a.client();
let one = &barrel.one;
let cap = barrel.constant(CAP)?;
let gain = c.where_cond(&slopes.known, &slopes.gain, one)?;
let gain = c.maximum(&gain, &barrel.constant(E_FLOOR)?)?;
let tail = c.where_cond(&slopes.known, &slopes.tail, one)?;
let wide = a.point(&c.maximum(&tail, one)?)?;
let unit = a.point(one)?;
let alone = a.point(&tail)?;
let ratios = [
a.div_positive(&wide, &gain)?,
a.div_positive(&unit, &gain)?,
a.div_positive(&alone, &gain)?,
];
let c = a.client();
let capped = |ratio: &Interval<R>| -> ArithmeticResult<Tensor<R>> {
Ok(c.where_cond(&slopes.known, &c.minimum(ratio.upper(), &cap)?, &cap)?)
};
Ok([
capped(&ratios[0])?,
capped(&ratios[1])?,
capped(&ratios[2])?,
])
})?;
let keep = |value: &Tensor<R>| c.where_cond(wanted, value, &barrel.zero);
Ok(Profile {
bound: keep(&bound)?,
head: keep(&head)?,
tail: keep(&tail)?,
})
}
pub(crate) struct Profile<R: Runtime<DType = DType>> {
pub(crate) bound: Tensor<R>,
pub(crate) head: Tensor<R>,
pub(crate) tail: Tensor<R>,
}
#[cfg(test)]
mod tests {
use super::fixture::{Spec, certified, level, pairs, weak};
use super::{CAP, E_FLOOR};
use crate::structures::numerics::sensitivity::fixture::{TestResult, grid};
use crate::structures::{CulvertBarrel, CulvertInlet};
#[test]
fn floor_and_cap_are_the_stated_powers_of_two() {
assert_eq!(E_FLOOR, 2.0_f64.powi(-256));
assert_eq!(CAP, 2.0_f64.powi(256));
}
#[test]
fn mild_partly_full_circle_bound_holds_over_a_head_grid() -> TestResult {
let spec = Spec {
barrel: CulvertBarrel::circular(1.2)?,
length_m: 60.0,
manning_n: 0.013,
inverts: [0.12, 0.0],
};
let cases = pairs(
&grid(0.3, 1.3, 2),
&grid(0.02, 1.15, 2),
&[0.0, 0.12, 1.2, 1.32],
);
assert!(certified(spec, CulvertInlet::hds5(1)?, &cases)? > cases.len() / 4);
Ok(())
}
#[test]
fn adverse_partly_full_box_bound_holds_over_a_head_grid() -> TestResult {
let spec = Spec {
barrel: CulvertBarrel::rectangular_box(2.0, 1.0)?,
length_m: 30.0,
manning_n: 0.015,
inverts: [0.0, 0.3],
};
let cases = pairs(
&grid(0.4, 1.6, 2),
&grid(0.32, 1.25, 2),
&[0.0, 0.3, 1.0, 1.3],
);
assert!(certified(spec, CulvertInlet::hds5(9)?, &cases)? > cases.len() / 4);
Ok(())
}
#[test]
fn zero_slope_partly_full_box_bound_holds_over_a_head_grid() -> TestResult {
let spec = level(CulvertBarrel::rectangular_box(2.0, 1.0)?, (100.0, 0.03));
let cases = pairs(&grid(0.1, 1.4, 3), &grid(0.02, 0.95, 3), &[0.0, 1.0]);
assert!(certified(spec, CulvertInlet::hds5(9)?, &cases)? > cases.len() / 4);
Ok(())
}
#[test]
fn steep_drowned_barrels_bound_holds_over_a_head_grid() -> TestResult {
for (barrel, rise, code) in [
(CulvertBarrel::rectangular_box(2.0, 1.5)?, 1.5, 9),
(CulvertBarrel::circular(1.2)?, 1.2, 1),
] {
let spec = Spec {
barrel,
length_m: 40.0,
manning_n: 0.013,
inverts: [0.8, 0.0],
};
let cases = pairs(
&grid(rise + 1.0, rise + 3.0, 8),
&grid(rise + 0.02, rise + 0.8, 8),
&[0.0, 0.8, rise, rise + 0.8],
);
assert!(certified(spec, CulvertInlet::hds5(code)?, &cases)? > cases.len() / 4);
}
Ok(())
}
#[test]
fn mild_arc_barrels_bound_holds_over_a_head_grid() -> TestResult {
for (barrel, code) in [
(
CulvertBarrel::horizontal_ellipse(1.524, 0.9652, 0.419_475_92)?,
30,
),
(
CulvertBarrel::pipe_arch(1.8542, 1.397, 0.4572, 1.938_528)?,
36,
),
(CulvertBarrel::arch(3.048, 1.6002)?, 45),
] {
let rise = barrel.rise_m();
let spec = Spec {
barrel,
length_m: 60.0,
manning_n: 0.024,
inverts: [0.12, 0.0],
};
let cases = pairs(
&grid(0.3, rise + 0.6, 2),
&grid(0.02, rise + 0.4, 2),
&[0.0, 0.12, rise, rise + 0.12],
);
assert!(certified(spec, CulvertInlet::hds5(code)?, &cases)? > cases.len() / 4);
}
Ok(())
}
#[test]
fn steep_box_bound_holds_where_the_barrel_primes() -> TestResult {
let spec = Spec {
barrel: CulvertBarrel::rectangular_box(2.0, 1.5)?,
length_m: 40.0,
manning_n: 0.013,
inverts: [0.8, 0.0],
};
let cases = pairs(
&grid(4.5, 9.5, 10),
&grid(0.02, 1.4, 6),
&[0.0, 0.8, 1.5, 2.3],
);
assert!(certified(spec, weak()?, &cases)? > cases.len() / 2);
Ok(())
}
#[test]
fn floored_and_face_blocked_culverts_bound_holds_over_head_grid() -> TestResult {
use super::fixture::{certified_with_condition, culvert};
use crate::structures::{
BlockageFraction, CulvertBlockage, CulvertSediment, DirectionPolicy, StructureBlockage,
StructureCondition,
};
let spec = Spec {
barrel: CulvertBarrel::circular(1.2)?,
length_m: 100.0,
manning_n: 0.024,
inverts: [0.08, 0.0],
};
let law = culvert(spec, CulvertInlet::hds5(1)?, false)?;
let condition = StructureCondition::Blocked(StructureBlockage::culvert(
CulvertBlockage {
face_a: BlockageFraction::new(0.25)?,
face_b: BlockageFraction::NONE,
sediment: Some(CulvertSediment {
depth_m: 0.24,
bed_manning_n: 0.030,
}),
closed_barrels: 0,
},
"Sediment and debris survey".into(),
)?);
let cases = pairs(
&grid(0.5, 2.2, 3),
&grid(0.3, 1.5, 3),
&[0.24, 0.32, 1.2, 1.28],
);
let outlet = certified_with_condition(law, DirectionPolicy::AtoBOnly, condition, &cases)?;
assert!(outlet > 0, "expected outlet-controlled lanes");
let box_spec = Spec {
barrel: CulvertBarrel::rectangular_box(2.0, 1.5)?,
length_m: 30.0,
manning_n: 0.012,
inverts: [0.06, 0.0],
};
let box_law = culvert(box_spec, CulvertInlet::hds5(9)?, false)?;
let box_cond = StructureCondition::Blocked(StructureBlockage::culvert(
CulvertBlockage {
face_a: BlockageFraction::new(0.25)?,
face_b: BlockageFraction::NONE,
sediment: Some(CulvertSediment {
depth_m: 0.45,
bed_manning_n: 0.030,
}),
closed_barrels: 0,
},
"Sediment survey".into(),
)?);
let box_cases = pairs(
&grid(0.7, 2.4, 3),
&grid(0.5, 1.8, 3),
&[0.45, 0.51, 1.5, 1.56],
);
certified_with_condition(box_law, DirectionPolicy::AtoBOnly, box_cond, &box_cases)?;
Ok(())
}
}