airplane 0.1.7

A library for easy airplane-related calculations and visualizations, including weight and balance assessments and graphical representations.A Rust library for easy airplane-related calculations and visualizations, including weight and balance assessments and graphical representations.
Documentation
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
use crate::types::{FuelType, VolumeType};

const AVGAS_FUEL_DENSITY_KG_LITER: f64 = 0.72;
const MOGAS_FUEL_DENSITY_KG_LITER: f64 = 0.74;

const LITERS_IN_GALLON: f64 = 378541.0 / 100000.0;

#[derive(Clone)]
pub enum LeverArm {
    Meter(f64),
}

impl LeverArm {
    pub fn meter(&self) -> f64 {
        match self {
            LeverArm::Meter(m) => *m,
        }
    }
}

#[derive(Debug, Clone)]
pub enum Volume {
    Liter(f64),
    Gallon(f64),
}

impl Volume {
    pub fn to_liter(&self) -> f64 {
        match self {
            Volume::Liter(v) => *v,
            Volume::Gallon(v) => *v * LITERS_IN_GALLON,
        }
    }

    pub fn to_gallon(&self) -> f64 {
        match self {
            Volume::Liter(v) => *v / LITERS_IN_GALLON,
            Volume::Gallon(v) => *v,
        }
    }

    pub fn to_string(&self) -> String {
        match self {
            Volume::Liter(v) => format!("{:.2}L", v),
            Volume::Gallon(v) => format!("{:.2}gal", v),
        }
    }
}

#[derive(Debug, Clone)]
pub enum Mass {
    Kilo(f64),
    Avgas(Volume),
    Mogas(Volume),
}

impl Mass {
    pub fn kilo(&self) -> f64 {
        match self {
            Mass::Kilo(kg) => *kg,
            Mass::Avgas(l) => match l {
                Volume::Liter(l) => l * AVGAS_FUEL_DENSITY_KG_LITER,
                Volume::Gallon(g) => g * LITERS_IN_GALLON * AVGAS_FUEL_DENSITY_KG_LITER,
            },
            Mass::Mogas(l) => match l {
                Volume::Liter(l) => l * MOGAS_FUEL_DENSITY_KG_LITER,
                Volume::Gallon(g) => g * LITERS_IN_GALLON * MOGAS_FUEL_DENSITY_KG_LITER,
            },
        }
    }

    pub fn to_avgas(&self) -> Mass {
        let liter = self.kilo() / AVGAS_FUEL_DENSITY_KG_LITER;
        Mass::Avgas(Volume::Liter(liter))
    }

    pub fn to_mogas(&self) -> Mass {
        let liter = self.kilo() / MOGAS_FUEL_DENSITY_KG_LITER;
        Mass::Mogas(Volume::Liter(liter))
    }

    pub fn unit(&self) -> String {
        match self {
            Mass::Kilo(_) => "kg".to_string(),
            Mass::Avgas(l) => match l {
                Volume::Liter(_) => format!("{:.2}kg/L", AVGAS_FUEL_DENSITY_KG_LITER),
                Volume::Gallon(_) => format!(
                    "{:.2}kg/gal",
                    AVGAS_FUEL_DENSITY_KG_LITER * LITERS_IN_GALLON
                ),
            },
            Mass::Mogas(l) => match l {
                Volume::Liter(_) => format!("{:.2}kg/L", MOGAS_FUEL_DENSITY_KG_LITER),
                Volume::Gallon(_) => format!(
                    "{:.2}kg/gal",
                    MOGAS_FUEL_DENSITY_KG_LITER * LITERS_IN_GALLON
                ),
            },
        }
    }
}

#[derive(Clone)]
pub struct Moment {
    name: String,
    lever_arm: LeverArm,
    mass: Mass,
}

impl Moment {
    pub fn new(name: String, lever_arm: LeverArm, mass: Mass) -> Moment {
        Moment {
            name,
            lever_arm,
            mass,
        }
    }

    pub fn lever_arm(&self) -> &LeverArm {
        &self.lever_arm
    }

    pub fn mass(&self) -> &Mass {
        &self.mass
    }

    pub fn total(&self) -> MassMoment {
        MassMoment::KgM(self.mass.kilo() * self.lever_arm.meter())
    }

    pub fn name(&self) -> &String {
        &self.name
    }
}

pub enum MassMoment {
    KgM(f64),
}

impl MassMoment {
    pub fn kgm(&self) -> f64 {
        match self {
            MassMoment::KgM(kgm) => *kgm,
        }
    }
}

/// Positive numbers represent reference aft of datum.
pub enum CenterOfGravity {
    Meter(f64),
    Millimeter(f64),
}

impl CenterOfGravity {
    pub fn meter(&self) -> f64 {
        match self {
            CenterOfGravity::Meter(m) => *m,
            CenterOfGravity::Millimeter(mm) => mm / 1000.0,
        }
    }
}

pub struct Limits {
    minimum_weight: Mass,
    mtow: Mass,
    forward_cg_limit: CenterOfGravity,
    rearward_cg_limit: CenterOfGravity,
}

impl Limits {
    pub fn new(
        minimum_weight: Mass,
        mtow: Mass,
        forward_cg_limit: CenterOfGravity,
        rearward_cg_limit: CenterOfGravity,
    ) -> Limits {
        Limits {
            minimum_weight,
            mtow,
            forward_cg_limit,
            rearward_cg_limit,
        }
    }

    pub fn minimum_weight(&self) -> &Mass {
        &self.minimum_weight
    }

    pub fn mtow(&self) -> &Mass {
        &self.mtow
    }

    pub fn forward_cg_limit(&self) -> &CenterOfGravity {
        &self.forward_cg_limit
    }

    pub fn rearward_cg_limit(&self) -> &CenterOfGravity {
        &self.rearward_cg_limit
    }
}

pub struct Airplane {
    callsign: String,
    moments: Vec<Moment>,
    limits: Limits,
    fuel_consumption_trip: Volume,
}

impl Airplane {
    pub fn new(
        callsign: String,
        moments: Vec<Moment>,
        limits: Limits,
        fuel_consumption_trip: Volume,
    ) -> Airplane {
        Airplane {
            callsign,
            moments,
            limits,
            fuel_consumption_trip,
        }
    }

    pub fn limits(&self) -> &Limits {
        &self.limits
    }

    fn center_of_gravity(&self) -> CenterOfGravity {
        let kg_mass = self.total_mass().kilo();
        let kgm_moment = self.total_mass_moment().kgm();

        CenterOfGravity::Meter(kgm_moment / kg_mass)
    }

    pub fn add_max_fuel_within_limits(
        &mut self,
        name: String,
        arm: LeverArm,
        fuel: FuelType,
        volume: VolumeType,
        max_volume: Option<Volume>,
    ) -> &Moment {
        let cg_limit = if arm.meter().ge(&0.5) {
            self.limits().rearward_cg_limit().meter()
        } else {
            self.limits().forward_cg_limit().meter()
        };

        let kg_max_mass: f64 = (cg_limit * self.total_mass().kilo()
            - self.total_mass_moment().kgm())
            / (arm.meter() - cg_limit);

        let max_mass = Mass::Kilo(
            if kg_max_mass + self.total_mass().kilo() >= self.limits().mtow().kilo() {
                self.limits().mtow().kilo() - self.total_mass().kilo()
            } else {
                kg_max_mass
            },
        );
        let max_mass = match fuel {
            FuelType::Mogas => max_mass.to_mogas(),
            FuelType::Avgas => max_mass.to_avgas(),
        };

        let limited_max_mass = match max_volume {
            Some(max_volume) => match max_mass {
                Mass::Avgas(v) => {
                    let volume_liter = if v.to_liter() > max_volume.to_liter() {
                        max_volume.to_liter()
                    } else {
                        v.to_liter()
                    };
                    match volume {
                        VolumeType::Liter => Mass::Avgas(Volume::Liter(volume_liter)),
                        VolumeType::Gallon => {
                            Mass::Avgas(Volume::Gallon(Volume::Liter(volume_liter).to_gallon()))
                        }
                    }
                }
                Mass::Mogas(v) => {
                    let volume_liter = if v.to_liter() > max_volume.to_liter() {
                        max_volume.to_liter()
                    } else {
                        v.to_liter()
                    };
                    match volume {
                        VolumeType::Liter => Mass::Mogas(Volume::Liter(volume_liter)),
                        VolumeType::Gallon => {
                            Mass::Mogas(Volume::Gallon(Volume::Liter(volume_liter).to_gallon()))
                        }
                    }
                }
                _ => max_mass,
            },
            None => max_mass,
        };

        let moment = Moment::new(name, arm, limited_max_mass);
        self.moments.push(moment);
        self.moments.last().unwrap()
    }

    pub fn total_mass_moment(&self) -> MassMoment {
        MassMoment::KgM(self.moments.iter().map(|m| m.total().kgm()).sum())
    }

    pub fn total_mass(&self) -> Mass {
        Mass::Kilo(self.moments.iter().map(|m| m.mass.kilo()).sum())
    }

    pub fn total_mass_moment_landing(&self) -> MassMoment {
        let fuel_moment = self.moments.last().expect("should be present");
        let mass_moment_without_fuel = self.total_mass_moment().kgm() - fuel_moment.total().kgm();

        let mass = match fuel_moment.mass() {
            Mass::Mogas(v) => Mass::Mogas(Volume::Liter(
                v.to_liter() - self.fuel_consumption_trip.to_liter(),
            )),
            Mass::Avgas(v) => Mass::Avgas(Volume::Liter(
                v.to_liter() - self.fuel_consumption_trip.to_liter(),
            )),
            _ => panic!("should be fuel"),
        };

        let fuel_moment = Moment::new("Fuel".to_string(), fuel_moment.lever_arm().clone(), mass);

        MassMoment::KgM(mass_moment_without_fuel + fuel_moment.total().kgm())
    }

    pub fn total_mass_landing(&self) -> Mass {
        let fuel_moment = self.moments.last().expect("should be present");
        let mass_without_fuel = self.total_mass().kilo() - fuel_moment.mass().kilo();

        let mass = match fuel_moment.mass() {
            Mass::Mogas(v) => Mass::Mogas(Volume::Liter(
                v.to_liter() - self.fuel_consumption_trip.to_liter(),
            )),
            Mass::Avgas(v) => Mass::Avgas(Volume::Liter(
                v.to_liter() - self.fuel_consumption_trip.to_liter(),
            )),
            _ => panic!("should be fuel"),
        };

        Mass::Kilo(mass_without_fuel + mass.kilo())
    }
    pub fn within_limits(&self) -> bool {
        let cg = self.center_of_gravity().meter();
        self.total_mass().kilo() <= self.limits.mtow.kilo()
            && cg <= self.limits.rearward_cg_limit.meter()
            && cg >= self.limits.forward_cg_limit.meter()
    }

    pub fn callsign(&self) -> &String {
        &self.callsign
    }

    pub fn moments(&self) -> &Vec<Moment> {
        &self.moments
    }

    pub fn add_moment(&mut self, moment: Moment) {
        self.moments.push(moment);
    }
}

#[cfg(test)]
mod test {
    use super::*;

    fn airplane(within_limits: bool) -> Airplane {
        let pilot_mass = if within_limits {
            Mass::Kilo(80.0)
        } else {
            Mass::Kilo(95.0)
        };

        Airplane::new(
            String::from("PHDHA"),
            vec![
                Moment::new(
                    "test".to_string(),
                    LeverArm::Meter(0.4294),
                    Mass::Kilo(517.0),
                ),
                Moment::new("test".to_string(), LeverArm::Meter(0.515), pilot_mass),
                Moment::new("test".to_string(), LeverArm::Meter(0.515), Mass::Kilo(89.0)),
                Moment::new("test".to_string(), LeverArm::Meter(1.3), Mass::Kilo(5.0)),
                Moment::new(
                    "test".to_string(),
                    LeverArm::Meter(0.325),
                    Mass::Avgas(Volume::Liter(62.0)),
                ),
            ],
            Limits::new(
                Mass::Kilo(558.0),
                Mass::Kilo(750.0),
                CenterOfGravity::Millimeter(427.0),
                CenterOfGravity::Millimeter(523.0),
            ),
            Volume::Liter(17.0),
        )
    }

    fn calculate_maximum_mass() {
        let mut plane = Airplane::new(
            String::from("PHDHA"),
            vec![
                Moment::new("test".to_string(), LeverArm::Meter(2.0), Mass::Kilo(10.0)),
                Moment::new("test".to_string(), LeverArm::Meter(3.0), Mass::Kilo(5.0)),
            ],
            Limits::new(
                Mass::Kilo(10.0),
                Mass::Kilo(40.0),
                CenterOfGravity::Meter(1.0),
                CenterOfGravity::Meter(3.0),
            ),
            Volume::Liter(17.0),
        );

        assert_eq!(
            10.0,
            plane
                .add_max_fuel_within_limits(
                    "test".to_string(),
                    LeverArm::Meter(4.0),
                    FuelType::Avgas,
                    VolumeType::Liter,
                    None
                )
                .mass()
                .kilo()
        );
        assert!(plane.within_limits());
    }

    #[test]
    fn calculate_maximum_mass_mtow() {
        let mut plane = Airplane::new(
            String::from("PHDHA"),
            vec![
                Moment::new("test".to_string(), LeverArm::Meter(2.0), Mass::Kilo(10.0)),
                Moment::new("test".to_string(), LeverArm::Meter(3.0), Mass::Kilo(5.0)),
            ],
            Limits::new(
                Mass::Kilo(10.0),
                Mass::Kilo(24.0),
                CenterOfGravity::Meter(1.0),
                CenterOfGravity::Meter(3.0),
            ),
            Volume::Liter(17.0),
        );

        {
            let max_moment = plane.add_max_fuel_within_limits(
                "test".to_string(),
                LeverArm::Meter(4.0),
                FuelType::Avgas,
                VolumeType::Liter,
                None,
            );
            assert_eq!(9.0, max_moment.mass().kilo());
        }

        assert!(plane.within_limits());
    }

    #[test]
    fn calculate_kg_moment() {
        let m = Moment::new(
            "test".to_string(),
            LeverArm::Meter(0.4294),
            Mass::Kilo(517.0),
        );
        let MassMoment::KgM(kgm) = m.total();

        assert_eq!(517.0 * 0.4294, kgm);
    }

    #[test]
    fn calculate_cg() {
        assert_eq!(
            (((0.4294 * 517.0)
                + (0.515 * 80.0)
                + (0.515 * 89.0)
                + (1.3 * 5.0)
                + (0.325 * AVGAS_FUEL_DENSITY_KG_LITER * 62.0))
                / (517.0 + 80.0 + 89.0 + 5.0 + (62.0 * AVGAS_FUEL_DENSITY_KG_LITER))),
            airplane(true).center_of_gravity().meter()
        );
    }

    #[test]
    fn outside_of_limits() {
        assert!(!airplane(false).within_limits());
    }

    #[test]
    fn inside_of_limits() {
        assert!(airplane(true).within_limits());
    }
}