Skip to main content

hydr_structures/structures/culvert/inlet/
batch.rs

1//! Public inlet-control batch over compact `[m]` tensors.
2
3use super::{CulvertInletHeadwater, CulvertInletRow};
4use crate::structures::culvert::input::check_discharges;
5use crate::structures::numerics::culvert::RowNames;
6use crate::structures::numerics::culvert::inlet::{anchors, evaluate};
7use crate::structures::tensor::culvert::{TensorBarrelContext, TensorInletContext};
8use crate::structures::{CulvertBarrel, StructureError};
9use numr::dtype::DType;
10use numr::ops::{
11    CompareOps, CorrectlyRoundedOps, NextRepresentableOps, ScalarOps, TensorOps, TwoSumOps,
12};
13use numr::runtime::{Runtime, RuntimeClient};
14use numr::tensor::Tensor;
15use solvr::optimize::CertifiedBisectionAlgorithms;
16use std::fmt;
17use std::sync::Arc;
18
19/// HDS-5 inlet-control rows, one barrel and inlet each, with a certified headwater query.
20///
21/// The headwater is an enclosure: the exact `HW_i(Q)` at each represented discharge lies inside
22/// its `[lower, upper]` pair. Admission certifies that each row's headwater strictly rises with
23/// discharge, across the transition too.
24#[derive(Clone)]
25pub struct CulvertInletBatch<R: Runtime<DType = DType>> {
26    rows: Arc<[CulvertInletRow]>,
27    barrels: Arc<[CulvertBarrel]>,
28    barrel: Arc<TensorBarrelContext<R>>,
29    unit: Arc<TensorBarrelContext<R>>,
30    inlet: Arc<TensorInletContext<R>>,
31}
32
33impl<R: Runtime<DType = DType>> fmt::Debug for CulvertInletBatch<R> {
34    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35        f.debug_struct("CulvertInletBatch")
36            .field("rows", &self.rows)
37            .finish_non_exhaustive()
38    }
39}
40
41impl<R: Runtime<DType = DType>> CulvertInletBatch<R> {
42    /// Uploads the rows, encloses each transition anchor and admits each row.
43    ///
44    /// Rejects an empty row list, a nonfinite slope, a nonfinite or nonpositive gravity, and a
45    /// coded inlet on a barrel of another shape. Rejects a row whose submerged headwater at
46    /// `X = 4` does not certifiably exceed its unsubmerged headwater at `X = 3.5`, a form-1 row
47    /// whose critical depth at `X = 3.5` is not below the crown, and a row whose discharge at
48    /// `X = 3.5` lies below the normal F64 range. Each rejection names the row and its inlet.
49    /// Rejects a runtime without the barrel batch's F64 contracts.
50    pub fn new(client: R::Client, rows: Vec<CulvertInletRow>) -> Result<Self, StructureError>
51    where
52        R::Client: TensorOps<R>
53            + CompareOps<R>
54            + NextRepresentableOps<R>
55            + ScalarOps<R>
56            + TwoSumOps<R>
57            + CorrectlyRoundedOps<R>
58            + RuntimeClient<R>
59            + CertifiedBisectionAlgorithms<R>,
60    {
61        if rows.is_empty() {
62            return Err(StructureError::definition(
63                "rows=[]: supply at least one culvert inlet row",
64            ));
65        }
66        for (index, row) in rows.iter().enumerate() {
67            row.check().map_err(|error| error.at(index))?;
68        }
69        let barrels: Arc<[CulvertBarrel]> = rows.iter().map(|row| row.barrel).collect();
70        let barrel = TensorBarrelContext::new(client, &barrels)?;
71        let unit = barrel.unit()?;
72        let inlet = anchors::prepare(&barrels, &barrel, &unit, &rows)?;
73        Ok(Self {
74            rows: rows.into(),
75            barrels,
76            barrel: Arc::new(barrel),
77            unit: Arc::new(unit),
78            inlet: Arc::new(inlet),
79        })
80    }
81
82    /// Rows in order.
83    pub fn rows(&self) -> &[CulvertInletRow] {
84        &self.rows
85    }
86
87    /// Encloses the inlet-control headwater per row at one discharge per barrel.
88    ///
89    /// `discharge_m3_per_s` is an F64 `[m]` tensor of per-barrel discharge magnitudes on the
90    /// batch device. Rejects a negative or nonfinite discharge, naming its row.
91    /// Returns `Numerical` when a headwater bound overflows F64.
92    pub fn headwater(
93        &self,
94        discharge_m3_per_s: &Tensor<R>,
95    ) -> Result<CulvertInletHeadwater<R>, StructureError>
96    where
97        R::Client: TensorOps<R>
98            + CompareOps<R>
99            + NextRepresentableOps<R>
100            + ScalarOps<R>
101            + TwoSumOps<R>
102            + CorrectlyRoundedOps<R>
103            + RuntimeClient<R>
104            + CertifiedBisectionAlgorithms<R>,
105    {
106        let barrel = &*self.barrel;
107        check_discharges(barrel, "discharge_m3_per_s", discharge_m3_per_s)?;
108        let headwater = evaluate(
109            RowNames::new(&self.barrels),
110            barrel,
111            &self.unit,
112            &self.inlet,
113            discharge_m3_per_s,
114        )?;
115        Ok(CulvertInletHeadwater::from_headwater(headwater))
116    }
117}