Skip to main content

conspire/math/integrate/error/
mod.rs

1#[cfg(test)]
2mod test;
3
4use crate::math::{Scalar, Style, StyledError, assert::AssertionError, styled_error};
5
6/// Possible errors encountered when integrating.
7pub enum IntegrationError {
8    InconsistentInitialConditions,
9    InitialTimeNotLessThanFinalTime,
10    Intermediate(String),
11    LengthTimeLessThanTwo,
12    MinimumStepSizeReached(Scalar, String),
13    MinimumStepSizeUpstream(Scalar, String, String),
14    TimeStepNotSet(Scalar, Scalar, String),
15    Upstream(String, String),
16}
17
18impl From<String> for IntegrationError {
19    fn from(error: String) -> Self {
20        Self::Intermediate(error)
21    }
22}
23
24impl StyledError for IntegrationError {
25    fn message(&self, style: &Style) -> String {
26        let (h, c) = (style.headline, style.frame);
27        match self {
28            Self::InconsistentInitialConditions => {
29                format!("{h}The initial condition z_0 is not consistent with g(t_0, y_0).")
30            }
31            Self::InitialTimeNotLessThanFinalTime => {
32                format!("{h}The initial time must precede the final time.")
33            }
34            Self::Intermediate(message) => message.to_string(),
35            Self::LengthTimeLessThanTwo => {
36                format!("{h}The time must contain at least two entries.")
37            }
38            Self::MinimumStepSizeReached(dt_min, integrator) => {
39                format!(
40                    "{h}Minimum time step ({dt_min:?}) reached.{c}\n\
41                    In integrator: {integrator}."
42                )
43            }
44            Self::MinimumStepSizeUpstream(dt_min, error, integrator) => {
45                format!(
46                    "{error}{c}\n\
47                    Causing error: {h}Minimum time step ({dt_min:?}) reached.{c}\n\
48                    In integrator: {integrator}."
49                )
50            }
51            Self::TimeStepNotSet(t0, tf, integrator) => {
52                format!(
53                    "{h}A positive time step must be set within [{t0:?}, {tf:?}].{c}\n\
54                    In integrator: {integrator}."
55                )
56            }
57            Self::Upstream(error, integrator) => {
58                format!(
59                    "{error}{c}\n\
60                    In integrator: {integrator}."
61                )
62            }
63        }
64    }
65}
66
67styled_error!(IntegrationError);
68
69impl From<IntegrationError> for String {
70    fn from(error: IntegrationError) -> Self {
71        format!("{}", error)
72    }
73}
74
75impl From<IntegrationError> for AssertionError {
76    fn from(error: IntegrationError) -> Self {
77        AssertionError {
78            message: error.to_string(),
79        }
80    }
81}