use crate::structures::{
BlockageFraction, CulvertApplicability, CulvertBarrel, CulvertBarrelShape, CulvertBlockage,
CulvertFloor, CulvertInlet, CulvertInletRow, CulvertOutletRow, StructureError,
};
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct CulvertEnd {
inlet: CulvertInlet,
entrance_loss: f64,
}
impl CulvertEnd {
pub fn new(inlet: CulvertInlet, entrance_loss: f64) -> Result<Self, StructureError> {
if !(entrance_loss.is_finite() && entrance_loss >= 0.0) {
return Err(StructureError::definition(format!(
"{},entrance_loss={entrance_loss:?}: supply a finite nonnegative entrance loss coefficient",
inlet.describe()
)));
}
Ok(Self {
inlet,
entrance_loss,
})
}
pub fn inlet(&self) -> CulvertInlet {
self.inlet
}
pub fn entrance_loss(&self) -> f64 {
self.entrance_loss
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct CulvertAlignment {
pub length_m: f64,
pub invert_a_m: f64,
pub invert_b_m: f64,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct CulvertEnds {
pub a: Option<CulvertEnd>,
pub b: Option<CulvertEnd>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct CulvertParameters {
barrel: CulvertBarrel,
alignment: CulvertAlignment,
manning_n: f64,
floor_manning_n: Option<f64>,
barrel_count: u32,
ends: CulvertEnds,
gravity_m_per_s2: f64,
applicability: CulvertApplicability,
}
impl CulvertParameters {
pub fn new(
barrel: CulvertBarrel,
alignment: CulvertAlignment,
manning_n: f64,
barrel_count: u32,
ends: CulvertEnds,
gravity_m_per_s2: f64,
applicability: CulvertApplicability,
) -> Result<Self, StructureError> {
applicability.check()?;
if let Some(floor) = barrel.floor() {
return Err(StructureError::definition(format!(
"sediment_depth_m={:?},roughness_ratio={:?}: CulvertParameters holds the unblocked barrel. Supply CulvertBarrel::base, set an open floor with CulvertParameters::with_floor_manning_n, or declare sediment with StructureBlockage::culvert",
floor.sediment_depth_m, floor.roughness_ratio
)));
}
let CulvertAlignment {
length_m,
invert_a_m,
invert_b_m,
} = alignment;
let fields = format!(
"length_m={length_m:?},invert_a_m={invert_a_m:?},invert_b_m={invert_b_m:?},manning_n={manning_n:?},barrel_count={barrel_count},gravity_m_per_s2={gravity_m_per_s2:?}"
);
if !(invert_a_m.is_finite() && invert_b_m.is_finite()) {
return Err(StructureError::definition(format!(
"{fields}: supply finite invert elevations on the structure datum"
)));
}
if barrel_count == 0 {
return Err(StructureError::definition(format!(
"{fields}: supply at least one barrel"
)));
}
if ends.a.is_none() && ends.b.is_none() {
return Err(StructureError::definition(format!(
"{fields}: supply the culvert end of at least one flow direction"
)));
}
let parameters = Self {
barrel,
alignment,
manning_n,
floor_manning_n: None,
barrel_count,
ends,
gravity_m_per_s2,
applicability,
};
for side in [CulvertSide::A, CulvertSide::B] {
if let Some((inlet, outlet)) = parameters.rows(side) {
outlet.check()?;
inlet.check()?;
}
}
Ok(parameters)
}
pub fn with_floor_manning_n(mut self, floor_manning_n: f64) -> Result<Self, StructureError> {
if !matches!(
self.barrel.shape(),
CulvertBarrelShape::Box | CulvertBarrelShape::Arch
) {
return Err(StructureError::definition(format!(
"floor_manning_n={floor_manning_n:?},barrel_shape={:?}: only a box or flat-floor arch barrel has its own floor. Declare sediment through StructureBlockage::culvert instead",
self.barrel.shape()
)));
}
if !(floor_manning_n.is_finite() && floor_manning_n > 0.0) {
return Err(StructureError::definition(format!(
"floor_manning_n={floor_manning_n:?}: supply a finite positive floor Manning n in s/m^(1/3)"
)));
}
self.barrel.with_floor(CulvertFloor {
sediment_depth_m: 0.0,
roughness_ratio: floor_manning_n / self.manning_n,
})?;
self.floor_manning_n = Some(floor_manning_n);
Ok(self)
}
pub fn barrel(&self) -> CulvertBarrel {
self.barrel
}
pub fn alignment(&self) -> CulvertAlignment {
self.alignment
}
pub fn manning_n(&self) -> f64 {
self.manning_n
}
pub fn floor_manning_n(&self) -> Option<f64> {
self.floor_manning_n
}
pub fn barrel_count(&self) -> u32 {
self.barrel_count
}
pub fn ends(&self) -> CulvertEnds {
self.ends
}
pub fn gravity_m_per_s2(&self) -> f64 {
self.gravity_m_per_s2
}
pub fn applicability(&self) -> &CulvertApplicability {
&self.applicability
}
pub fn forward_rows(&self) -> Option<(CulvertInletRow, CulvertOutletRow)> {
self.rows(CulvertSide::A)
}
pub fn reverse_rows(&self) -> Option<(CulvertInletRow, CulvertOutletRow)> {
self.rows(CulvertSide::B)
}
pub(crate) fn rows(&self, side: CulvertSide) -> Option<(CulvertInletRow, CulvertOutletRow)> {
let CulvertAlignment {
length_m,
invert_a_m,
invert_b_m,
} = self.alignment;
let (end, fall) = match side {
CulvertSide::A => (self.ends.a?, invert_a_m - invert_b_m),
CulvertSide::B => (self.ends.b?, invert_b_m - invert_a_m),
};
let barrel = match self.floor_manning_n {
Some(floor_n) => self
.barrel
.with_floor(CulvertFloor {
sediment_depth_m: 0.0,
roughness_ratio: floor_n / self.manning_n,
})
.unwrap_or(self.barrel),
None => self.barrel,
};
let slope = fall / length_m;
Some((
CulvertInletRow {
barrel,
inlet: end.inlet,
slope,
gravity_m_per_s2: self.gravity_m_per_s2,
face_blockage: BlockageFraction::NONE,
},
CulvertOutletRow {
barrel,
length_m,
manning_n: self.manning_n,
entrance_loss: end.entrance_loss,
slope,
gravity_m_per_s2: self.gravity_m_per_s2,
face_blockage: BlockageFraction::NONE,
},
))
}
pub(crate) fn effective_barrel(
&self,
blockage: Option<&CulvertBlockage>,
) -> Result<CulvertBarrel, StructureError> {
if let Some(sediment) = blockage.and_then(|b| b.sediment) {
if !(sediment.bed_manning_n.is_finite() && sediment.bed_manning_n > 0.0) {
return Err(StructureError::definition(format!(
"bed_manning_n={:?}: supply a finite positive sediment bed Manning n in s/m^(1/3)",
sediment.bed_manning_n
)));
}
return self.barrel.with_floor(CulvertFloor {
sediment_depth_m: sediment.depth_m,
roughness_ratio: sediment.bed_manning_n / self.manning_n,
});
}
if let Some(floor_n) = self.floor_manning_n {
return self.barrel.with_floor(CulvertFloor {
sediment_depth_m: 0.0,
roughness_ratio: floor_n / self.manning_n,
});
}
Ok(self.barrel)
}
pub(crate) fn effective_rows(
&self,
side: CulvertSide,
blockage: Option<&CulvertBlockage>,
) -> Result<Option<(CulvertInletRow, CulvertOutletRow)>, StructureError> {
let Some((mut inlet, mut outlet)) = self.rows(side) else {
return Ok(None);
};
let barrel = self.effective_barrel(blockage)?;
let face = match (side, blockage) {
(CulvertSide::A, Some(b)) => b.face_a,
(CulvertSide::B, Some(b)) => b.face_b,
(_, None) => BlockageFraction::NONE,
};
inlet.barrel = barrel;
inlet.face_blockage = face;
outlet.barrel = barrel;
outlet.face_blockage = face;
outlet.check()?;
inlet.check()?;
Ok(Some((inlet, outlet)))
}
pub(crate) fn invert(&self, side: CulvertSide) -> f64 {
match side {
CulvertSide::A => self.alignment.invert_a_m,
CulvertSide::B => self.alignment.invert_b_m,
}
}
pub(crate) fn bed_elevation(
&self,
side: CulvertSide,
blockage: Option<&CulvertBlockage>,
) -> Result<f64, StructureError> {
let barrel = self.effective_barrel(blockage)?;
let depth = barrel.floor().map_or(0.0, |floor| floor.sediment_depth_m);
let invert = self.invert(side);
let bed = if depth == 0.0 { invert } else { invert + depth };
if !bed.is_finite() {
return Err(StructureError::definition(format!(
"invert_m={invert:?},sediment_depth_m={depth:?}: bed elevation overflows F64. Supply finite inverts with room for the sediment depth"
)));
}
Ok(bed)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum CulvertSide {
A,
B,
}
#[cfg(test)]
mod tests {
use super::*;
use crate::structures::StructureInputErrorKind;
fn applicability() -> CulvertApplicability {
CulvertApplicability {
coefficient_provenance: "HDS-5 Table A.1".into(),
barrel_condition_assertion: "Clean barrel".into(),
}
}
fn end(code: u8) -> Result<CulvertEnd, StructureError> {
CulvertEnd::new(CulvertInlet::hds5(code)?, 0.5)
}
fn culvert(
alignment: CulvertAlignment,
manning_n: f64,
count: u32,
ends: CulvertEnds,
) -> Result<CulvertParameters, StructureError> {
CulvertParameters::new(
CulvertBarrel::circular(1.2)?,
alignment,
manning_n,
count,
ends,
9.81,
applicability(),
)
}
fn message(result: Result<CulvertParameters, StructureError>) -> Option<String> {
match result {
Err(StructureError::InvalidInput {
kind: StructureInputErrorKind::Definition,
context,
element: None,
}) => Some(context),
_ => None,
}
}
#[test]
fn rows_carry_opposite_slopes_and_their_own_end() -> Result<(), StructureError> {
let alignment = CulvertAlignment {
length_m: 40.0,
invert_a_m: 10.3,
invert_b_m: 10.1,
};
let parameters = culvert(
alignment,
0.012,
2,
CulvertEnds {
a: Some(end(1)?),
b: Some(CulvertEnd::new(CulvertInlet::hds5(3)?, 0.2)?),
},
)?;
let forward = parameters.rows(CulvertSide::A);
let reverse = parameters.rows(CulvertSide::B);
let (Some((inlet_a, outlet_a)), Some((inlet_b, outlet_b))) = (forward, reverse) else {
return Err(StructureError::definition("both ends exist"));
};
assert_eq!(inlet_a.slope.to_bits(), (-inlet_b.slope).to_bits());
assert_eq!(outlet_a.slope.to_bits(), inlet_a.slope.to_bits());
assert_eq!(inlet_a.inlet.code(), Some(1));
assert_eq!(inlet_b.inlet.code(), Some(3));
assert_eq!((outlet_a.entrance_loss, outlet_b.entrance_loss), (0.5, 0.2));
assert_eq!(parameters.invert(CulvertSide::B), 10.1);
Ok(())
}
#[test]
fn admission_names_the_value_and_action() -> Result<(), StructureError> {
let level = CulvertAlignment {
length_m: 20.0,
invert_a_m: 0.0,
invert_b_m: 0.0,
};
let one = CulvertEnds {
a: Some(end(1)?),
b: None,
};
let cases = [
(
culvert(level, 0.013, 0, one),
"barrel_count=0,gravity_m_per_s2=9.81: supply at least one barrel",
),
(
culvert(level, 0.013, 1, CulvertEnds { a: None, b: None }),
"supply the culvert end of at least one flow direction",
),
(
culvert(
CulvertAlignment {
invert_b_m: f64::NAN,
..level
},
0.013,
1,
one,
),
"invert_b_m=NaN",
),
(
culvert(level, -0.013, 1, one),
"manning_n=-0.013,entrance_loss=0.5,slope=0.0,gravity_m_per_s2=9.81: supply a finite positive Manning n",
),
(
culvert(
CulvertAlignment {
invert_b_m: 0.7,
..level
},
0.013,
1,
one,
),
"Supply an adverse fall of at most rise/2",
),
(
culvert(
level,
0.013,
1,
CulvertEnds {
a: Some(end(9)?),
b: None,
},
),
"the code is for Box barrels",
),
];
for (result, expected) in cases {
let text = message(result);
assert!(
text.as_deref().is_some_and(|t| t.contains(expected)),
"{text:?} lacks {expected:?}"
);
}
let text = match CulvertEnd::new(CulvertInlet::hds5(1)?, -0.1) {
Err(StructureError::InvalidInput { context, .. }) => context,
Ok(_) => String::new(),
Err(other) => other.to_string(),
};
assert!(text.contains("entrance_loss=-0.1: supply a finite nonnegative"));
Ok(())
}
}