Skip to main content

hydr_structures/structures/pump/
curve.rs

1//! Closed monotone reference-speed pump curves with explicit provenance.
2
3use crate::structures::{StructureError, StructureInputErrorKind};
4
5/// One supplied quiescent endpoint-stage reference-curve knot.
6#[derive(Clone, Copy, Debug, PartialEq)]
7pub struct PumpCurveKnot {
8    /// Receiving-stage minus donor-stage head, in metres.
9    pub head_m: f64,
10    /// Nonnegative reference-speed discharge magnitude, in m³/s.
11    pub discharge_m3_per_s: f64,
12}
13
14pub(crate) type PumpCurveIdentity = (String, Vec<(u64, u64)>);
15
16/// Immutable single-valued bounded pump curve. No extrapolation or implicit shutoff extension applies.
17#[derive(Clone, Debug, PartialEq)]
18pub struct PumpCurve {
19    knots: Vec<PumpCurveKnot>,
20    provenance: String,
21}
22impl PumpCurve {
23    /// Check increasing finite heads, nonincreasing nonnegative flow, and explicit curve provenance.
24    pub fn new(knots: Vec<PumpCurveKnot>, provenance: String) -> Result<Self, StructureError> {
25        crate::structures::definition::text(&provenance, "pump curve provenance")?;
26        if knots.len() < 2 {
27            return Err(StructureError::input(
28                StructureInputErrorKind::Definition,
29                "pump reference curve requires at least two explicit knots",
30            ));
31        }
32        for (index, knot) in knots.iter().enumerate() {
33            if !knot.head_m.is_finite()
34                || !knot.discharge_m3_per_s.is_finite()
35                || knot.discharge_m3_per_s < 0.0
36            {
37                return Err(StructureError::input(
38                    StructureInputErrorKind::Definition,
39                    format!(
40                        "pump knot={index},value={knot:?}: supply finite head and nonnegative finite discharge"
41                    ),
42                ));
43            }
44        }
45        for (index, pair) in knots.windows(2).enumerate() {
46            let width = pair[1].head_m - pair[0].head_m;
47            if !width.is_finite()
48                || width <= 0.0
49                || pair[1].discharge_m3_per_s > pair[0].discharge_m3_per_s
50            {
51                return Err(StructureError::input(
52                    StructureInputErrorKind::Definition,
53                    format!(
54                        "pump segment={index},width={width}: require increasing representable head and nonincreasing discharge"
55                    ),
56                ));
57            }
58        }
59        if !knots.iter().any(|k| k.discharge_m3_per_s > 0.0) {
60            return Err(StructureError::input(
61                StructureInputErrorKind::Definition,
62                "pump curve requires at least one positive discharge knot",
63            ));
64        }
65        Ok(Self { knots, provenance })
66    }
67    /// Complete exact supplied reference knots.
68    pub fn knots(&self) -> &[PumpCurveKnot] {
69        &self.knots
70    }
71    /// Source/revision, SI units, reference speed, fluid applicability, and valid curve envelope evidence.
72    pub fn provenance(&self) -> &str {
73        &self.provenance
74    }
75    pub(crate) fn key(&self) -> PumpCurveIdentity {
76        (
77            self.provenance.clone(),
78            self.knots
79                .iter()
80                .map(|k| (k.head_m.to_bits(), k.discharge_m3_per_s.to_bits()))
81                .collect(),
82        )
83    }
84}