Skip to content

Commit 8acdfef

Browse files
committed
Switch to Pascals for pressure. Keep it SI!
1 parent 84f14ab commit 8acdfef

File tree

1 file changed

+43
-30
lines changed

1 file changed

+43
-30
lines changed

src/pressure.rs

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::measurement::*;
2+
use super::*;
23

34
/// The `Pressure` struct can be used to deal with presssures in a common way.
45
/// Common metric and imperial units are supported.
@@ -14,86 +15,98 @@ use super::measurement::*;
1415
/// ```
1516
#[derive(Copy, Clone, Debug)]
1617
pub struct Pressure {
17-
millibars: f64,
18+
pascals: f64,
1819
}
1920

2021
impl Pressure {
22+
pub fn from_pascals(pascals: f64) -> Pressure {
23+
Pressure { pascals: pascals }
24+
}
25+
2126
pub fn from_hectopascals(hectopascals: f64) -> Pressure {
22-
Self::from_millibars(hectopascals)
27+
Self::from_pascals(hectopascals * 100.0)
2328
}
2429

25-
pub fn from_pascals(pascals: f64) -> Pressure {
26-
Self::from_millibars(pascals / 100.0)
30+
pub fn from_millibars(millibars: f64) -> Pressure {
31+
Self::from_pascals(millibars * 100.0)
2732
}
2833

2934
pub fn from_kilopascals(kilopascals: f64) -> Pressure {
30-
Self::from_millibars(kilopascals * 10.0)
35+
Self::from_pascals(kilopascals * 1000.0)
3136
}
3237

33-
pub fn from_millibars(millibars: f64) -> Pressure {
34-
Pressure { millibars: millibars }
38+
pub fn from_psi(psi: f64) -> Pressure {
39+
Self::from_pascals(psi * 6894.76)
3540
}
3641

3742
pub fn from_bars(bars: f64) -> Pressure {
38-
Self::from_millibars(bars * 1000.0)
43+
Self::from_pascals(bars * 100_000.0)
3944
}
4045

41-
pub fn from_psi(psi: f64) -> Pressure {
42-
Self::from_millibars(psi / 0.0145038)
46+
pub fn from_atmospheres(atmospheres: f64) -> Pressure {
47+
Self::from_pascals(atmospheres * 101_325.0)
4348
}
4449

45-
pub fn from_atmospheres(atmospheres: f64) -> Pressure {
46-
Self::from_millibars(atmospheres * 1013.25)
50+
pub fn as_pascals(&self) -> f64 {
51+
self.pascals
4752
}
4853

4954
pub fn as_hectopascals(&self) -> f64 {
50-
self.millibars
55+
self.pascals / 100.0
5156
}
5257

53-
pub fn as_pascals(&self) -> f64 {
54-
self.millibars * 100.0
58+
pub fn as_millibars(&self) -> f64 {
59+
self.pascals / 100.0
5560
}
5661

5762
pub fn as_kilopascals(&self) -> f64 {
58-
self.millibars / 10.0
63+
self.pascals / 1000.0
5964
}
6065

61-
pub fn as_millibars(&self) -> f64 {
62-
self.millibars
66+
pub fn as_psi(&self) -> f64 {
67+
self.pascals / 6894.76
6368
}
6469

6570
pub fn as_bars(&self) -> f64 {
66-
self.millibars / 1000.0
71+
self.pascals / 100_000.0
6772
}
6873

69-
pub fn as_psi(&self) -> f64 {
70-
self.millibars * 0.0145038
74+
pub fn as_atmospheres(&self) -> f64 {
75+
self.pascals / 101_325.0
7176
}
77+
}
7278

73-
pub fn as_atmospheres(&self) -> f64 {
74-
self.millibars / 1013.25
79+
/// Pressure * Area = Force
80+
impl ::std::ops::Mul<Area> for Pressure {
81+
type Output = Force;
82+
83+
fn mul(self, rhs: Area) -> Force {
84+
Force::from_newtons(rhs.as_square_meters() * self.as_pascals())
7585
}
7686
}
7787

7888
impl Measurement for Pressure {
7989
fn get_base_units(&self) -> f64 {
80-
self.millibars
90+
self.pascals
8191
}
8292

8393
fn from_base_units(units: f64) -> Self {
84-
Self::from_millibars(units)
94+
Self::from_pascals(units)
8595
}
8696

8797
fn get_base_units_name(&self) -> &'static str {
88-
"mbar"
98+
"Pa"
8999
}
90100

91101
fn get_appropriate_units(&self) -> (&'static str, f64) {
92102
let list = [
93-
("millibars", 1e0),
94-
("bar", 1e3),
95-
("thousand bar", 1e6),
96-
("million bar", 1e9),
103+
("mPa", 1e-3),
104+
("Pa", 1e0),
105+
("hPa", 1e2),
106+
("kPa", 1e3),
107+
("MPa", 1e6),
108+
("GPa", 1e9),
109+
("TPa", 1e12),
97110
];
98111
self.pick_appropriate_units(&list)
99112
}

0 commit comments

Comments
 (0)