Skip to main content

hydr_structures/structures/
batch.rs

1//! Immutable typed law batches and original supported-head proposals.
2use numr::{dtype::DType, runtime::Runtime};
3
4use super::{StructureElement, StructureError, StructureHeads, StructureProposal};
5use crate::structures::tensor::{TensorStructureContext, TensorStructureInput};
6use std::sync::Arc;
7
8/// Persistent checked law definitions without evolving component storage.
9#[derive(Clone, Debug)]
10pub struct StructureBatch<R: Runtime<DType = DType>> {
11    pub(super) context: Arc<TensorStructureContext<R>>,
12}
13impl<R: Runtime<DType = DType>> StructureBatch<R> {
14    /// Check explicit law geometry, applicability, identities, and ideal direction policies.
15    ///
16    /// Rejects a culvert law. Build a batch that holds one with [`StructureBatch::with_culverts`].
17    pub fn new(client: R::Client, elements: Vec<StructureElement>) -> Result<Self, StructureError>
18    where
19        R::Client: numr::ops::BinaryOps<R>
20            + numr::ops::CompareOps<R>
21            + numr::ops::CorrectlyRoundedOps<R>
22            + numr::ops::ReduceOps<R>
23            + numr::ops::ScalarOps<R>
24            + numr::ops::TypeConversionOps<R>
25            + numr::ops::UnaryOps<R>,
26    {
27        Ok(Self {
28            context: Arc::new(TensorStructureContext::new(client, elements)?),
29        })
30    }
31    /// Check every law, including culverts, and upload the culvert rows.
32    ///
33    /// A culvert needs certified roots and quadrature on the runtime, which
34    /// [`CulvertClient`](super::CulvertClient) names. [`StructureBatch::new`] rejects culverts.
35    /// Each culvert declares an end for each admitted direction and no other. Rejects an inlet
36    /// whose headwater does not certifiably rise across its transition, naming the element.
37    /// Rejects a runtime without the culvert barrel's F64 contracts.
38    pub fn with_culverts(
39        client: R::Client,
40        elements: Vec<StructureElement>,
41    ) -> Result<Self, StructureError>
42    where
43        R::Client: super::CulvertClient<R>,
44    {
45        Ok(Self {
46            context: Arc::new(TensorStructureContext::with_culverts(client, elements)?),
47        })
48    }
49    /// Immutable concrete definitions in original numerical order.
50    pub fn elements(&self) -> &[StructureElement] {
51        &self.context.elements
52    }
53    /// Declared and effective geometry of every element, in element order.
54    pub fn geometry(&self) -> Vec<super::StructureGeometryReport> {
55        let culverts = self
56            .context
57            .culvert
58            .as_deref()
59            .map_or(&[][..], |law| law.geometry());
60        super::blockage::reports(&self.context.elements, culverts)
61    }
62    /// Evaluate original supported heads without mutating definitions or earlier proposals.
63    pub fn propose(&self, heads: &[StructureHeads]) -> Result<StructureProposal<R>, StructureError>
64    where
65        R::Client: numr::ops::CumulativeOps<R>
66            + numr::ops::BinaryOps<R>
67            + numr::ops::CompareOps<R>
68            + numr::ops::ConditionalOps<R>
69            + numr::ops::CorrectlyRoundedOps<R>
70            + numr::ops::IndexingOps<R>
71            + numr::ops::ReduceOps<R>
72            + numr::ops::ScalarOps<R>
73            + numr::ops::ShapeOps<R>
74            + numr::ops::TwoSumOps<R>
75            + numr::ops::TypeConversionOps<R>
76            + numr::ops::UnaryOps<R>
77            + solvr::interpolate::Interp1dAlgorithms<R>,
78    {
79        if self.context.elements.iter().any(|element| {
80            matches!(
81                element.law,
82                super::StructureLaw::Pump(_) | super::StructureLaw::HydrostaticRectangularGate(_)
83            )
84        }) {
85            return Err(StructureError::input(
86                super::StructureInputErrorKind::OperatingPoint,
87                "device operations are missing. Use propose_with_operations with explicit identity-bound operations",
88            ));
89        }
90        let operations = self
91            .elements()
92            .iter()
93            .map(|e| super::StructureOperation {
94                structure_id: e.id.clone(),
95                operating_point: super::StructureOperatingPoint::Passive,
96            })
97            .collect::<Vec<_>>();
98        self.propose_with_operations(heads, &operations)
99    }
100    /// Evaluate complete identity-bound device operations without mutating earlier proposals.
101    pub fn propose_with_operations(
102        &self,
103        heads: &[StructureHeads],
104        operations: &[super::StructureOperation],
105    ) -> Result<StructureProposal<R>, StructureError>
106    where
107        R::Client: numr::ops::CumulativeOps<R>
108            + numr::ops::BinaryOps<R>
109            + numr::ops::CompareOps<R>
110            + numr::ops::ConditionalOps<R>
111            + numr::ops::CorrectlyRoundedOps<R>
112            + numr::ops::IndexingOps<R>
113            + numr::ops::ReduceOps<R>
114            + numr::ops::ScalarOps<R>
115            + numr::ops::ShapeOps<R>
116            + numr::ops::TwoSumOps<R>
117            + numr::ops::TypeConversionOps<R>
118            + numr::ops::UnaryOps<R>
119            + solvr::interpolate::Interp1dAlgorithms<R>,
120    {
121        let input = TensorStructureInput::with_operations(&self.context, heads, operations)?;
122        let result = crate::structures::numerics::proposal(&self.context, &input)?;
123        Ok(StructureProposal::new(
124            Arc::clone(&self.context),
125            heads.to_vec(),
126            operations.to_vec(),
127            result,
128        ))
129    }
130}