1use std::str::FromStr;
4
5use crate::constants::*;
6use crate::model::options::SimulationOptions;
7use serde::{Deserialize, Serialize};
8
9pub 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 CFS, GPM, MGD, IMGD, AFD, LPS, LPM, MLD, CMS, CMH, CMD, }
32
33impl 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, KPA, METERS, FEET, BAR, }
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, SI, }
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 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 fn convert_to_standard(&mut self, options: &SimulationOptions);
137 fn convert_from_standard(&mut self, options: &SimulationOptions);
139}