1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
use super::measurement::*;
pub const PASCAL_ATMOSPHERE_FACTOR: f64 = 101_325.0;
pub const PASCAL_HECTOPASCAL_FACTOR: f64 = 100.0;
pub const PASCAL_KILOPASCAL_FACTOR: f64 = 1000.0;
pub const PASCAL_MILLIBAR_FACTOR: f64 = 100.0;
pub const PASCAL_BAR_FACTOR: f64 = 100_000.0;
pub const PASCAL_PSI_FACTOR: f64 = 6894.76;
#[derive(Copy, Clone, Debug)]
pub struct Pressure {
pascals: f64,
}
impl Pressure {
pub fn from_pascals(pascals: f64) -> Pressure {
Pressure { pascals: pascals }
}
pub fn from_hectopascals(hectopascals: f64) -> Pressure {
Self::from_pascals(hectopascals * PASCAL_HECTOPASCAL_FACTOR)
}
pub fn from_millibars(millibars: f64) -> Pressure {
Self::from_pascals(millibars * PASCAL_MILLIBAR_FACTOR)
}
pub fn from_kilopascals(kilopascals: f64) -> Pressure {
Self::from_pascals(kilopascals * PASCAL_KILOPASCAL_FACTOR)
}
pub fn from_psi(psi: f64) -> Pressure {
Self::from_pascals(psi * PASCAL_PSI_FACTOR)
}
pub fn from_bars(bars: f64) -> Pressure {
Self::from_pascals(bars * PASCAL_BAR_FACTOR)
}
pub fn from_atmospheres(atmospheres: f64) -> Pressure {
Self::from_pascals(atmospheres * PASCAL_ATMOSPHERE_FACTOR)
}
pub fn as_pascals(&self) -> f64 {
self.pascals
}
pub fn as_hectopascals(&self) -> f64 {
self.pascals / PASCAL_HECTOPASCAL_FACTOR
}
pub fn as_millibars(&self) -> f64 {
self.pascals / PASCAL_MILLIBAR_FACTOR
}
pub fn as_kilopascals(&self) -> f64 {
self.pascals / PASCAL_KILOPASCAL_FACTOR
}
pub fn as_psi(&self) -> f64 {
self.pascals / PASCAL_PSI_FACTOR
}
pub fn as_bars(&self) -> f64 {
self.pascals / PASCAL_BAR_FACTOR
}
pub fn as_atmospheres(&self) -> f64 {
self.pascals / PASCAL_ATMOSPHERE_FACTOR
}
}
impl Measurement for Pressure {
fn as_base_units(&self) -> f64 {
self.pascals
}
fn from_base_units(units: f64) -> Self {
Self::from_pascals(units)
}
fn get_base_units_name(&self) -> &'static str {
"Pa"
}
fn get_appropriate_units(&self) -> (&'static str, f64) {
let list = [
("mPa", 1e-3),
("Pa", 1e0),
("hPa", 1e2),
("kPa", 1e3),
("MPa", 1e6),
("GPa", 1e9),
("TPa", 1e12),
];
self.pick_appropriate_units(&list)
}
}
implement_measurement! { Pressure }
#[cfg(test)]
mod test {
use super::*;
use test_utils::assert_almost_eq;
#[test]
fn hectopascals() {
let t = Pressure::from_pascals(100.0);
let o = t.as_hectopascals();
assert_almost_eq(o, 1.0);
let t = Pressure::from_hectopascals(100.0);
let o = t.as_pascals();
assert_almost_eq(o, 10000.0);
}
#[test]
fn millibars() {
let t = Pressure::from_pascals(100.0);
let o = t.as_millibars();
assert_almost_eq(o, 1.0);
let t = Pressure::from_millibars(100.0);
let o = t.as_pascals();
assert_almost_eq(o, 10000.0);
}
#[test]
fn kilopascals() {
let t = Pressure::from_pascals(100.0);
let o = t.as_kilopascals();
assert_almost_eq(o, 0.1);
let t = Pressure::from_kilopascals(100.0);
let o = t.as_pascals();
assert_almost_eq(o, 100000.0);
}
#[test]
fn bars() {
let t = Pressure::from_pascals(100.0);
let o = t.as_bars();
assert_almost_eq(o, 0.001);
let t = Pressure::from_bars(100.0);
let o = t.as_pascals();
assert_almost_eq(o, 1e+7);
}
#[test]
fn atmospheres() {
let t = Pressure::from_pascals(100.0);
let o = t.as_atmospheres();
assert_almost_eq(o, 0.000986923);
let t = Pressure::from_atmospheres(100.0);
let o = t.as_pascals();
assert_almost_eq(o, 10132497.2616919);
}
#[test]
fn psi() {
let t = Pressure::from_pascals(100.0);
let o = t.as_psi();
assert_almost_eq(o, 0.0145038);
let t = Pressure::from_psi(100.0);
let o = t.as_pascals();
assert_almost_eq(o, 689476.9760513823);
}
#[test]
fn add() {
let a = Pressure::from_pascals(2.0);
let b = Pressure::from_pascals(4.0);
let c = a + b;
let d = b + a;
assert_almost_eq(c.as_pascals(), 6.0);
assert_eq!(c, d);
}
#[test]
fn sub() {
let a = Pressure::from_pascals(2.0);
let b = Pressure::from_pascals(4.0);
let c = a - b;
assert_almost_eq(c.as_pascals(), -2.0);
}
#[test]
fn mul() {
let a = Pressure::from_pascals(3.0);
let b = a * 2.0;
let c = 2.0 * a;
assert_almost_eq(b.as_pascals(), 6.0);
assert_eq!(b, c);
}
#[test]
fn div() {
let a = Pressure::from_pascals(2.0);
let b = Pressure::from_pascals(4.0);
let c = a / b;
let d = a / 2.0;
assert_almost_eq(c, 0.5);
assert_almost_eq(d.as_pascals(), 1.0);
}
#[test]
fn eq() {
let a = Pressure::from_pascals(2.0);
let b = Pressure::from_pascals(2.0);
assert_eq!(a == b, true);
}
#[test]
fn neq() {
let a = Pressure::from_pascals(2.0);
let b = Pressure::from_pascals(4.0);
assert_eq!(a == b, false);
}
#[test]
fn cmp() {
let a = Pressure::from_pascals(2.0);
let b = Pressure::from_pascals(4.0);
assert_eq!(a < b, true);
assert_eq!(a <= b, true);
assert_eq!(a > b, false);
assert_eq!(a >= b, false);
}
}