use crate::structures::{StructureError, StructureInputErrorKind};
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct PumpCurveKnot {
pub head_m: f64,
pub discharge_m3_per_s: f64,
}
pub(crate) type PumpCurveIdentity = (String, Vec<(u64, u64)>);
#[derive(Clone, Debug, PartialEq)]
pub struct PumpCurve {
knots: Vec<PumpCurveKnot>,
provenance: String,
}
impl PumpCurve {
pub fn new(knots: Vec<PumpCurveKnot>, provenance: String) -> Result<Self, StructureError> {
crate::structures::definition::text(&provenance, "pump curve provenance")?;
if knots.len() < 2 {
return Err(StructureError::input(
StructureInputErrorKind::Definition,
"pump reference curve requires at least two explicit knots",
));
}
for (index, knot) in knots.iter().enumerate() {
if !knot.head_m.is_finite()
|| !knot.discharge_m3_per_s.is_finite()
|| knot.discharge_m3_per_s < 0.0
{
return Err(StructureError::input(
StructureInputErrorKind::Definition,
format!(
"pump knot={index},value={knot:?}: supply finite head and nonnegative finite discharge"
),
));
}
}
for (index, pair) in knots.windows(2).enumerate() {
let width = pair[1].head_m - pair[0].head_m;
if !width.is_finite()
|| width <= 0.0
|| pair[1].discharge_m3_per_s > pair[0].discharge_m3_per_s
{
return Err(StructureError::input(
StructureInputErrorKind::Definition,
format!(
"pump segment={index},width={width}: require increasing representable head and nonincreasing discharge"
),
));
}
}
if !knots.iter().any(|k| k.discharge_m3_per_s > 0.0) {
return Err(StructureError::input(
StructureInputErrorKind::Definition,
"pump curve requires at least one positive discharge knot",
));
}
Ok(Self { knots, provenance })
}
pub fn knots(&self) -> &[PumpCurveKnot] {
&self.knots
}
pub fn provenance(&self) -> &str {
&self.provenance
}
pub(crate) fn key(&self) -> PumpCurveIdentity {
(
self.provenance.clone(),
self.knots
.iter()
.map(|k| (k.head_m.to_bits(), k.discharge_m3_per_s.to_bits()))
.collect(),
)
}
}