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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
//! Types and constants for handling force.

use super::measurement::*;

/// Number of POUNDS force in a Newton
pub const POUNDS_PER_NEWTON: f64 = 0.224809;
/// Number of POUNDALS in a Newton.  A poundal is the force necessary to
/// accelerate 1 pound-mass at 1 foot per second per second.
pub const POUNDALS_PER_NEWTON: f64 = 7.2330;
/// Number of KILOPONDS in a Newton
pub const KILOPONDS_PER_NEWTON: f64 = 1.0 / 9.80665;
/// Number of DYNES in a Newton
pub const DYNES_PER_NEWTON: f64 = 1e5;

/// The `Force` struct can be used to deal with force in a common way.
///
/// #Example
///
/// ```
/// use measurements::Force;
/// use measurements::Mass;
/// use measurements::Acceleration;
///
/// let metric_ton = Mass::from_metric_tons(1.0);
/// let gravity = Acceleration::from_meters_per_second_per_second(9.81);
/// let force: Force = metric_ton * gravity; // F=ma
/// println!(
///     "One metric ton exerts a force of {} due to gravity",
///     force);
/// ```
#[derive(Copy, Clone, Debug)]
pub struct Force {
    newtons: f64,
}

impl Force {
    /// Create a Force from a floating point value in Newtons
    pub fn from_newtons(newtons: f64) -> Self {
        Force { newtons: newtons }
    }

    /// Create a Force from a floating point value in Micronewtons
    pub fn from_micronewtons(micronewtons: f64) -> Self {
        Self::from_newtons(micronewtons / 1e6)
    }

    /// Create a Force from a floating point value in Millinewtons
    pub fn from_millinewtons(millinewtons: f64) -> Self {
        Self::from_newtons(millinewtons / 1e3)
    }

    /// Create a Force from a floating point value in pounds
    pub fn from_pounds(pounds: f64) -> Self {
        Self::from_newtons(pounds / POUNDS_PER_NEWTON)
    }

    /// Create a Force from a floating point value in poundals
    pub fn from_poundals(poundals: f64) -> Self {
        Self::from_newtons(poundals / POUNDALS_PER_NEWTON)
    }

    /// Create a Force from a floating point value in kiloponds
    pub fn from_kiloponds(kiloponds: f64) -> Self {
        Self::from_newtons(kiloponds / KILOPONDS_PER_NEWTON)
    }

    /// Create a Force from a floating point value in Dynes
    pub fn from_dynes(dynes: f64) -> Self {
        Self::from_newtons(dynes / DYNES_PER_NEWTON)
    }

    /// Convert this Force into a floating point value in Micronewtons
    pub fn as_micronewtons(&self) -> f64 {
        self.newtons * 1e6
    }

    /// Convert this Force into a floating point value in Milliewtons
    pub fn as_millinewtons(&self) -> f64 {
        self.newtons * 1e3
    }

    /// Convert this Force into a floating point value in Newtons
    pub fn as_newtons(&self) -> f64 {
        self.newtons
    }

    /// Convert this Force into a floating point value in pound-force (lb.f)
    pub fn as_pounds(&self) -> f64 {
        self.newtons * POUNDS_PER_NEWTON
    }

    /// Convert this Force into a floating point value in poundals
    pub fn as_poundals(&self) -> f64 {
        self.newtons * POUNDALS_PER_NEWTON
    }

    /// Convert this Force into a floating point value in kiloponds
    pub fn as_kiloponds(&self) -> f64 {
        self.newtons * KILOPONDS_PER_NEWTON
    }

    /// Convert this Force into a floating point value in dynes
    pub fn as_dynes(&self) -> f64 {
        self.newtons * DYNES_PER_NEWTON
    }
}

impl Measurement for Force {
    fn as_base_units(&self) -> f64 {
        self.newtons
    }

    fn from_base_units(units: f64) -> Self {
        Self::from_newtons(units)
    }

    fn get_base_units_name(&self) -> &'static str {
        "N"
    }

    fn get_appropriate_units(&self) -> (&'static str, f64) {
        // Smallest to largest
        let list = [
            ("nN", 1e-9),
            ("\u{00B5}N", 1e-6),
            ("mN", 1e-3),
            ("N", 1e0),
            ("kN", 1e3),
            ("MN", 1e6),
            ("GN", 1e9),
            ("TN", 1e12),
        ];
        self.pick_appropriate_units(&list)
    }
}

implement_measurement! { Force }

#[cfg(test)]
mod test {
    use force::*;
    use test_utils::assert_almost_eq;

    #[test]
    pub fn newtons() {
        let i1 = Force::from_newtons(100.0);
        let r1 = i1.as_newtons();

        assert_almost_eq(r1, 100.0);
    }

    #[test]
    pub fn micronewtons() {
        let i1 = Force::from_newtons(100.0);
        let r1 = i1.as_micronewtons();

        let i2 = Force::from_micronewtons(100.0);
        let r2 = i2.as_newtons();

        assert_almost_eq(r1, 1e8);
        assert_almost_eq(r2, 1e-4);
    }

    #[test]
    pub fn millinewtons() {
        let i1 = Force::from_newtons(100.0);
        let r1 = i1.as_millinewtons();

        let i2 = Force::from_millinewtons(100.0);
        let r2 = i2.as_newtons();

        assert_almost_eq(r1, 1e5);
        assert_almost_eq(r2, 1e-1);
    }

    #[test]
    pub fn pounds() {
        let i1 = Force::from_newtons(100.0);
        let r1 = i1.as_pounds();

        let i2 = Force::from_pounds(100.0);
        let r2 = i2.as_newtons();

        assert_almost_eq(r1, 22.480886300718);
        assert_almost_eq(r2, 444.822);
    }

    #[test]
    pub fn poundals() {
        let i1 = Force::from_newtons(100.0);
        let r1 = i1.as_poundals();

        let i2 = Force::from_poundals(100.0);
        let r2 = i2.as_newtons();

        assert_almost_eq(r1, 723.3016238104);
        assert_almost_eq(r2, 13.8255);
    }

    #[test]
    pub fn kiloponds() {
        let i1 = Force::from_newtons(100.0);
        let r1 = i1.as_kiloponds();

        let i2 = Force::from_kiloponds(100.0);
        let r2 = i2.as_newtons();

        assert_almost_eq(r1, 10.1972);
        assert_almost_eq(r2, 980.665);
    }

    #[test]
    pub fn dynes() {
        let i1 = Force::from_newtons(100.0);
        let r1 = i1.as_dynes();

        let i2 = Force::from_dynes(100.0);
        let r2 = i2.as_newtons();

        assert_almost_eq(r1, 1e7);
        assert_almost_eq(r2, 0.001);
    }

    #[test]
    fn add() {
        let a = Force::from_newtons(2.0);
        let b = Force::from_newtons(4.0);
        let c = a + b;
        let d = b + a;
        assert_almost_eq(c.as_newtons(), 6.0);
        assert_eq!(c, d);
    }

    #[test]
    fn sub() {
        let a = Force::from_newtons(2.0);
        let b = Force::from_newtons(4.0);
        let c = a - b;
        assert_almost_eq(c.as_newtons(), -2.0);
    }

    #[test]
    fn mul() {
        let a = Force::from_newtons(3.0);
        let b = a * 2.0;
        let c = 2.0 * a;
        assert_almost_eq(b.as_newtons(), 6.0);
        assert_eq!(b, c);
    }

    #[test]
    fn div() {
        let a = Force::from_newtons(2.0);
        let b = Force::from_newtons(4.0);
        let c = a / b;
        let d = a / 2.0;
        assert_almost_eq(c, 0.5);
        assert_almost_eq(d.as_newtons(), 1.0);
    }

    #[test]
    fn eq() {
        let a = Force::from_newtons(2.0);
        let b = Force::from_newtons(2.0);
        assert_eq!(a == b, true);
    }

    #[test]
    fn neq() {
        let a = Force::from_newtons(2.0);
        let b = Force::from_newtons(4.0);
        assert_eq!(a == b, false);
    }

    #[test]
    fn cmp() {
        let a = Force::from_newtons(2.0);
        let b = Force::from_newtons(4.0);
        assert_eq!(a < b, true);
        assert_eq!(a <= b, true);
        assert_eq!(a > b, false);
        assert_eq!(a >= b, false);
    }

}