hydr_structures/structures/
held.rs1use numr::{dtype::DType, runtime::Runtime};
3
4use super::{
5 HeldHeadInterval, HeldHeadVolumeOutput, StructureError, StructureInputErrorKind,
6 StructureProposal,
7};
8use numr::tensor::Tensor;
9
10#[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 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 pub fn interval(&self) -> HeldHeadInterval {
61 self.interval
62 }
63 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}