Skip to main content

hpr_sim/
error.rs

1//! Errors from setting up or running a flight.
2
3use hpr_aero::AeroError;
4use hpr_atmos::AtmosError;
5use hpr_core::CoreError;
6use hpr_design::{DesignError, Finding, Severity};
7use thiserror::Error;
8
9use crate::integrator::{IntegrationError, SettingsError};
10
11/// An error setting up or running a flight.
12#[derive(Debug, Error)]
13#[non_exhaustive]
14pub enum SimError {
15    /// An input outside its domain, such as a negative rail length.
16    #[error("{what} is outside its domain: {value}")]
17    Domain {
18        /// What the value is.
19        what: &'static str,
20        /// The offending value.
21        value: f64,
22    },
23    /// A model doesn't cover the input, such as a flutter panel of an elliptical fin.
24    #[error("unsupported: {what}")]
25    Unsupported {
26        /// What isn't covered.
27        what: &'static str,
28    },
29    /// A separation or ejection that the design can't make, with the component (or stage) id it
30    /// names or runs through.
31    #[error("{what}: `{component}`")]
32    Parting {
33        /// Why it can't be made.
34        what: &'static str,
35        /// The component or stage.
36        component: String,
37    },
38    /// A mass shift that the design can't make, with the component id it names.
39    #[error("{what}: `{component}`")]
40    Shift {
41        /// Why it can't be made.
42        what: &'static str,
43        /// The component.
44        component: String,
45    },
46    /// A mass release that the design can't make, with the component id it names.
47    #[error("{what}: `{component}`")]
48    MassRelease {
49        /// Why it can't be made.
50        what: &'static str,
51        /// The component.
52        component: String,
53    },
54    /// A thrust or mass history given from outside ([`crate::histories`]) that the flight can't
55    /// take, and why.
56    #[error("the {history} history can't be flown: {why}")]
57    History {
58        /// Which history: `thrust` or `mass`.
59        history: &'static str,
60        /// Why it can't be flown.
61        why: &'static str,
62    },
63    /// The design's checks found errors, and the settings don't accept them: every finding,
64    /// errors and warnings, in the checks' order. The message counts and names only the errors.
65    #[error(
66        "the design has {} error finding(s); the first is {}",
67        error_count(.0),
68        first_error(.0)
69    )]
70    DesignChecks(Vec<Finding>),
71    /// The design can't be assembled.
72    #[error(transparent)]
73    Design(#[from] DesignError),
74    /// The aerodynamic models refused the design or a flow condition (for example Mach 5 or
75    /// faster, past the normal force's and the drag buildup's range).
76    #[error(transparent)]
77    Aero(#[from] AeroError),
78    /// The atmosphere or wind model refused a height.
79    #[error(transparent)]
80    Atmosphere(#[from] AtmosError),
81    /// A geodesy, gravity or numerics error.
82    #[error(transparent)]
83    Core(#[from] CoreError),
84    /// Bad integrator settings or initial state.
85    #[error(transparent)]
86    Settings(#[from] SettingsError),
87    /// The integration failed for a reason other than the step limit.
88    #[error("the integration failed")]
89    Integration(#[source] Box<IntegrationError<SimError>>),
90}
91
92/// How many findings are errors, for [`SimError::DesignChecks`]'s message.
93fn error_count(findings: &[Finding]) -> usize {
94    findings
95        .iter()
96        .filter(|finding| finding.severity() == Severity::Error)
97        .count()
98}
99
100/// The first finding that is an error, for [`SimError::DesignChecks`]'s message.
101fn first_error(findings: &[Finding]) -> String {
102    findings
103        .iter()
104        .find(|finding| finding.severity() == Severity::Error)
105        .map_or_else(|| "missing".to_owned(), |finding| format!("{finding:?}"))
106}
107
108#[cfg(test)]
109mod tests {
110    use super::*;
111
112    /// #46: a warning ahead of the error is neither counted nor named as the error.
113    #[test]
114    fn design_checks_message_counts_and_names_only_errors() {
115        let warning = Finding::MotorPastMountTop {
116            configuration: "c".to_owned(),
117            mount: "mount".to_owned(),
118            excess_m: 0.01,
119        };
120        let error = Finding::MotorWiderThanMount {
121            configuration: "c".to_owned(),
122            mount: "mount".to_owned(),
123            motor_diameter_m: 0.038,
124            mount_inner_diameter_m: 0.029,
125        };
126        let message = SimError::DesignChecks(vec![warning, error.clone()]).to_string();
127        assert_eq!(
128            message,
129            format!("the design has 1 error finding(s); the first is {error:?}")
130        );
131    }
132}