Skip to main content

hpr_forensics/synthetic/
designs.rs

1//! The public designs the synthetic flights fly, each with its RocketPy example's own parachutes.
2//!
3//! The ten are every design under `validation/designs/` that is a published example and can fly:
4//! RocketPy's examples, rebuilt as HPR designs from `validation/oracles/rocketpy/rocket_mass.py`
5//! (`validation/designs/README.md`). The others there have no motor (the MIL-HDBK-762 sample, the
6//! two wind-tunnel models, the range's Basic Finner) or are invented (the two synthetic rockets).
7//! The designs leave their
8//! parachutes out, since RocketPy gives only their drag area; the drag areas, triggers and lags
9//! below are each example's own, as `rocket_mass.py` records them, at the lines given.
10
11use std::f64::consts::PI;
12
13use serde::Serialize;
14
15use super::SyntheticError;
16
17/// The inch, m.
18const INCH_M: f64 = 0.0254;
19/// The foot, m.
20const FOOT_M: f64 = 0.3048;
21
22/// What opens a parachute.
23#[derive(Debug, Clone, Copy, PartialEq, Serialize)]
24pub enum Opens {
25    /// At apogee.
26    Apogee,
27    /// Descending through this height above the launch site, m, or at apogee if apogee is lower.
28    Below(f64),
29}
30
31/// One parachute, by its drag area.
32#[derive(Debug, Clone, Copy, PartialEq, Serialize)]
33pub struct Chute {
34    /// The example's name for it.
35    pub name: &'static str,
36    /// Its drag area `C_D S`, m².
37    pub cd_s_m2: f64,
38    /// When it opens.
39    pub opens: Opens,
40    /// The delay from its trigger to its opening, s.
41    pub lag_s: f64,
42}
43
44/// A design the flights draw from.
45#[derive(Debug, Clone, Copy, PartialEq, Serialize)]
46pub struct Design {
47    /// Its file's stem under `validation/designs/`.
48    pub name: &'static str,
49    /// The configuration that flies.
50    pub configuration: &'static str,
51    /// Its parachutes in the order they open; each releases the one before, as RocketPy's
52    /// parachutes replace one another.
53    pub chutes: &'static [Chute],
54}
55
56/// RocketPy's Calisto, both examples: a 1 m² drogue at apogee and a 10 m² main at 800 m, each
57/// after 1.5 s (`rocket_mass.py:204-207`, `:250-253`).
58const CALISTO: &[Chute] = &[
59    Chute {
60        name: "Drogue",
61        cd_s_m2: 1.0,
62        opens: Opens::Apogee,
63        lag_s: 1.5,
64    },
65    Chute {
66        name: "Main",
67        cd_s_m2: 10.0,
68        opens: Opens::Below(800.0),
69        lag_s: 1.5,
70    },
71];
72
73/// The ten designs, each equally likely in a draw.
74pub const DESIGNS: [Design; 10] = [
75    Design {
76        name: "rocketpy-bella-lui",
77        configuration: "example",
78        // `rocket_mass.py:305-306`.
79        chutes: &[Chute {
80            name: "Drogue",
81            cd_s_m2: PI / 4.0,
82            opens: Opens::Apogee,
83            lag_s: 1.0,
84        }],
85    },
86    Design {
87        name: "rocketpy-calisto-getting-started-motor-at-minus-1.255",
88        configuration: "example",
89        chutes: CALISTO,
90    },
91    Design {
92        name: "rocketpy-calisto-tests-motor-at-minus-1.373",
93        configuration: "example",
94        chutes: CALISTO,
95    },
96    Design {
97        name: "rocketpy-cavour",
98        configuration: "example",
99        // The example has no parachute (`rocket_mass.py:482-485`): a flight whose recovery failed.
100        chutes: &[],
101    },
102    Design {
103        name: "rocketpy-genesis",
104        configuration: "example",
105        // `rocket_mass.py:582-585`.
106        chutes: &[
107            Chute {
108                name: "Drogue",
109                cd_s_m2: 0.285_005_285_533_666,
110                opens: Opens::Apogee,
111                lag_s: 1.0,
112            },
113            Chute {
114                name: "Main",
115                cd_s_m2: 1.1,
116                opens: Opens::Below(870.0),
117                lag_s: 1.0,
118            },
119        ],
120    },
121    Design {
122        name: "rocketpy-juno-iii",
123        configuration: "example",
124        // `rocket_mass.py:476-477`.
125        chutes: &[Chute {
126            name: "Drogue",
127            cd_s_m2: 0.885,
128            opens: Opens::Apogee,
129            lag_s: 0.5,
130        }],
131    },
132    Design {
133        name: "rocketpy-lince",
134        configuration: "example",
135        // `rocket_mass.py:639-640`: one main, at apogee, with no lag.
136        chutes: &[Chute {
137            name: "Main",
138            cd_s_m2: 3.9,
139            opens: Opens::Apogee,
140            lag_s: 0.0,
141        }],
142    },
143    Design {
144        name: "rocketpy-ndrt-2020-nose-to-tail",
145        configuration: "example",
146        // `rocket_mass.py:360-365`: a 24 in drogue of C_D 1.5 and a 120 in main of C_D 2.2.
147        chutes: &[
148            Chute {
149                name: "Drogue",
150                cd_s_m2: 1.5 * PI * (24.0 * INCH_M) * (24.0 * INCH_M) / 4.0,
151                opens: Opens::Apogee,
152                lag_s: 1.0,
153            },
154            Chute {
155                name: "Main",
156                cd_s_m2: 2.2 * PI * (120.0 * INCH_M) * (120.0 * INCH_M) / 4.0,
157                opens: Opens::Below(167.64),
158                lag_s: 1.0,
159            },
160        ],
161    },
162    Design {
163        name: "rocketpy-prometheus-2022-generic-motor",
164        configuration: "example",
165        // `rocket_mass.py:688-689`, with RocketPy's default lag of none.
166        chutes: &[
167            Chute {
168                name: "Drogue",
169                cd_s_m2: 1.6 * PI * FOOT_M * FOOT_M,
170                opens: Opens::Apogee,
171                lag_s: 0.0,
172            },
173            Chute {
174                name: "Main",
175                cd_s_m2: 2.2 * PI * (3.0 * FOOT_M) * (3.0 * FOOT_M),
176                opens: Opens::Below(457.2),
177                lag_s: 0.0,
178            },
179        ],
180    },
181    Design {
182        name: "rocketpy-valetudo",
183        configuration: "example",
184        // `rocket_mass.py:417-418`.
185        chutes: &[Chute {
186            name: "Drogue",
187            cd_s_m2: 0.349 * 1.3,
188            opens: Opens::Apogee,
189            lag_s: 1.0,
190        }],
191    },
192];
193
194/// The text of each design's file, in [`DESIGNS`]'s order, as the caller reads it from
195/// `validation/designs/<name>.json`. The library does no I/O, and a published crate can't embed a
196/// file outside its own folder, so the files come from whoever flies the flights.
197#[derive(Debug, Clone, PartialEq, Eq)]
198pub struct DesignFiles(Vec<String>);
199
200impl DesignFiles {
201    /// The files' text, `files[k]` that of `DESIGNS[k]`.
202    ///
203    /// # Errors
204    ///
205    /// [`SyntheticError::Invalid`] unless there is one file for each design.
206    pub fn new(files: Vec<String>) -> Result<Self, SyntheticError> {
207        if files.len() == DESIGNS.len() {
208            Ok(Self(files))
209        } else {
210            Err(SyntheticError::Invalid {
211                what: "the design files aren't one for each of the ten designs",
212            })
213        }
214    }
215
216    /// The file of design `design`, an index into [`DESIGNS`].
217    pub fn json(&self, design: usize) -> Option<&str> {
218        self.0.get(design).map(String::as_str)
219    }
220
221    /// The files as the repository holds them, for the tests.
222    #[cfg(test)]
223    pub(crate) fn committed() -> Self {
224        macro_rules! files {
225            ($($name:literal),* $(,)?) => {
226                vec![$(include_str!(concat!("../../../../validation/designs/", $name, ".json")).to_owned()),*]
227            };
228        }
229        Self(files![
230            "rocketpy-bella-lui",
231            "rocketpy-calisto-getting-started-motor-at-minus-1.255",
232            "rocketpy-calisto-tests-motor-at-minus-1.373",
233            "rocketpy-cavour",
234            "rocketpy-genesis",
235            "rocketpy-juno-iii",
236            "rocketpy-lince",
237            "rocketpy-ndrt-2020-nose-to-tail",
238            "rocketpy-prometheus-2022-generic-motor",
239            "rocketpy-valetudo",
240        ])
241    }
242}
243
244#[cfg(test)]
245mod tests {
246    use super::*;
247
248    #[test]
249    fn the_committed_files_are_the_designs_in_order() {
250        let files = DesignFiles::committed();
251        for (k, design) in DESIGNS.iter().enumerate() {
252            let rocket: serde_json::Value = serde_json::from_str(files.json(k).unwrap()).unwrap();
253            // Each file names its example: `rocketpy-cavour.json` is "RocketPy example: cavour".
254            let example = design.name.trim_start_matches("rocketpy-");
255            assert_eq!(
256                rocket["name"],
257                format!("RocketPy example: {example}"),
258                "{}",
259                design.name
260            );
261        }
262        assert!(matches!(
263            DesignFiles::new(vec![String::new(); 9]),
264            Err(SyntheticError::Invalid {
265                what: "the design files aren't one for each of the ten designs"
266            })
267        ));
268        assert!(files.json(DESIGNS.len()).is_none());
269    }
270}