Skip to main content

epanet_rs/model/
units.rs

1//! Unit systems, flow/pressure units and the `UnitConversion` trait used throughout the crate.
2
3use std::str::FromStr;
4
5use crate::constants::*;
6use crate::model::options::SimulationOptions;
7use serde::{Deserialize, Serialize};
8
9// type aliases for internal units
10// no enforcement (for now), only for additional clarity
11pub type Ft = f64;
12pub type Cfs = f64;
13pub type Psi = f64;
14pub type Ft3 = f64;
15
16#[derive(Debug, PartialEq, Eq, Clone, Copy, Deserialize, Serialize, strum::Display)]
17pub enum FlowUnits {
18    // Imperial units
19    CFS,  // Cubic feet per second
20    GPM,  // Gallons per minute
21    MGD,  // Million gallons per day
22    IMGD, // Imperial million gallons per day
23    AFD,  // Acre-feet per day
24    // Metric units
25    LPS, // Liters per second
26    LPM, // Liters per minute
27    MLD, // Million liters per day
28    CMS, // Cubic meters per second
29    CMH, // Cubic meters per hour
30    CMD, // Cubic meters per day
31}
32
33/// FromStr implementation for FlowUnits
34impl FromStr for FlowUnits {
35    type Err = String;
36    fn from_str(s: &str) -> Result<Self, Self::Err> {
37        match s.to_uppercase().as_str() {
38            "CFS" => Ok(FlowUnits::CFS),
39            "GPM" => Ok(FlowUnits::GPM),
40            "MGD" => Ok(FlowUnits::MGD),
41            "IMGD" => Ok(FlowUnits::IMGD),
42            "AFD" => Ok(FlowUnits::AFD),
43            "LPS" => Ok(FlowUnits::LPS),
44            "LPM" => Ok(FlowUnits::LPM),
45            "MLD" => Ok(FlowUnits::MLD),
46            "CMS" => Ok(FlowUnits::CMS),
47            "CMH" => Ok(FlowUnits::CMH),
48            "CMD" => Ok(FlowUnits::CMD),
49            _ => Err(format!("Invalid flow unit: {}", s)),
50        }
51    }
52}
53
54#[derive(Debug, PartialEq, Eq, Clone, Copy, Deserialize, Serialize)]
55pub enum PressureUnits {
56    PSI,    // Pounds per square inch
57    KPA,    // Kilopascals
58    METERS, // Meters
59    FEET,   // Feet
60    BAR,    // Bar
61}
62impl FromStr for PressureUnits {
63    type Err = String;
64    fn from_str(s: &str) -> Result<Self, Self::Err> {
65        match s.to_uppercase().as_str() {
66            "PSI" => Ok(PressureUnits::PSI),
67            "KPA" => Ok(PressureUnits::KPA),
68            "METERS" => Ok(PressureUnits::METERS),
69            "FEET" => Ok(PressureUnits::FEET),
70            "BAR" => Ok(PressureUnits::BAR),
71            _ => Err(format!("Invalid pressure unit: {}", s)),
72        }
73    }
74}
75
76#[derive(Debug, PartialEq, Eq, Deserialize, Serialize, Clone)]
77pub enum UnitSystem {
78    US, // US Customary units
79    SI, // International System of Units (metric)
80}
81
82impl UnitSystem {
83    pub fn per_feet(&self) -> Ft {
84        match self {
85            UnitSystem::US => 1.0,
86            UnitSystem::SI => MperFT,
87        }
88    }
89    pub fn per_cubic_feet(&self) -> Ft3 {
90        match self {
91            UnitSystem::US => 1.0,
92            UnitSystem::SI => M3perFT3,
93        }
94    }
95    pub fn per_horsepower(&self) -> f64 {
96        match self {
97            UnitSystem::US => 1.0,
98            UnitSystem::SI => KWperHP,
99        }
100    }
101}
102
103impl FlowUnits {
104    // convert the flow units to standard units (US standard) and CFS
105    pub fn per_cfs(&self) -> Cfs {
106        match self {
107            FlowUnits::CFS => 1.0,
108            FlowUnits::GPM => GPMperCFS,
109            FlowUnits::MGD => MGDperCFS,
110            FlowUnits::IMGD => IMGDperCFS,
111            FlowUnits::AFD => AFDperCFS,
112            FlowUnits::LPS => LPSperCFS,
113            FlowUnits::LPM => LPMperCFS,
114            FlowUnits::MLD => MLDperCFS,
115            FlowUnits::CMS => CMSperCFS,
116            FlowUnits::CMH => CMHperCFS,
117            FlowUnits::CMD => CMDperCFS,
118        }
119    }
120}
121
122impl PressureUnits {
123    pub fn per_feet(&self) -> f64 {
124        match self {
125            PressureUnits::PSI => PSIperFT,
126            PressureUnits::KPA => KPAperFT,
127            PressureUnits::METERS => MperFT,
128            PressureUnits::FEET => 1.0,
129            PressureUnits::BAR => BARperFT,
130        }
131    }
132}
133
134pub trait UnitConversion {
135    // convert units to EPANET standard units (US standard) from the given units / unit system
136    fn convert_to_standard(&mut self, options: &SimulationOptions);
137    // convert units from EPANET standard units (US standard) to the given units / unit system
138    fn convert_from_standard(&mut self, options: &SimulationOptions);
139}