Skip to main content

hydr_structures/structures/
held.rs

1//! Exact original held-head intervals and immutable signed-volume proposals.
2use numr::{dtype::DType, runtime::Runtime};
3
4use super::{
5    HeldHeadInterval, HeldHeadVolumeOutput, StructureError, StructureInputErrorKind,
6    StructureProposal,
7};
8use numr::tensor::Tensor;
9
10/// Opaque signed Q*dt proposals with original support and exact held interval.
11#[derive(Clone, Debug)]
12pub struct HeldHeadProposal<R: Runtime<DType = DType>> {
13    proposal: StructureProposal<R>,
14    interval: HeldHeadInterval,
15    volumes: Tensor<R>,
16}
17impl<R: Runtime<DType = DType>> StructureProposal<R> {
18    /// Multiply original signed discharge by one exact held duration, without storage advancement.
19    /// A trajectory proposal retains its sampled opening and never integrates prescribed motion.
20    pub fn held_volumes(
21        &self,
22        interval: HeldHeadInterval,
23    ) -> Result<HeldHeadProposal<R>, StructureError>
24    where
25        R::Client: numr::ops::BinaryOps<R>
26            + numr::ops::CompareOps<R>
27            + numr::ops::ReduceOps<R>
28            + numr::ops::ScalarOps<R>
29            + numr::ops::TypeConversionOps<R>
30            + numr::ops::UnaryOps<R>,
31    {
32        let dt = interval.end - interval.start;
33        if !interval.start.is_finite()
34            || interval.start < 0.0
35            || !interval.end.is_finite()
36            || !dt.is_finite()
37            || dt <= 0.0
38        {
39            return Err(StructureError::input(
40                StructureInputErrorKind::Definition,
41                format!(
42                    "held interval={interval:?},dt={dt}: supply finite nonnegative start and finite positive original duration"
43                ),
44            ));
45        }
46        let volumes = crate::structures::numerics::held::volumes(
47            &self.inner.context,
48            &self.inner.result.discharge,
49            dt,
50        )?;
51        Ok(HeldHeadProposal {
52            proposal: self.clone(),
53            interval,
54            volumes,
55        })
56    }
57}
58impl<R: Runtime<DType = DType>> HeldHeadProposal<R> {
59    /// Exact original elapsed interval, without implicit rescaling or trajectory integration.
60    pub fn interval(&self) -> HeldHeadInterval {
61        self.interval
62    }
63    /// Project original instantaneous proposals and their original signed held-volume products.
64    pub fn outputs(&self) -> Result<Vec<HeldHeadVolumeOutput>, StructureError> {
65        let output = self.proposal.outputs()?;
66        let volumes = super::projection::read(&self.volumes, output.len())?;
67        Ok(output
68            .into_iter()
69            .zip(volumes)
70            .map(|(proposal, volume_m3)| HeldHeadVolumeOutput {
71                interval: self.interval,
72                proposal,
73                volume_m3,
74            })
75            .collect())
76    }
77}