differential_equations/
error.rs

1//! NumericalMethod Trait for Differential Equation NumericalMethods
2
3use crate::traits::{Real, State};
4use std::fmt::{Debug, Display};
5
6/// Error for Differential Equation NumericalMethods
7///
8/// # Variants
9/// * `BadInput` - NumericalMethod input was bad.
10/// * `MaxSteps` - NumericalMethod reached maximum steps.
11/// * `StepSize` - NumericalMethod terminated due to step size converging too small of a value.
12/// * `Stiffness` - NumericalMethod terminated due to stiffness.
13///
14#[derive(PartialEq, Clone)]
15pub enum Error<T, V>
16where
17    T: Real,
18    V: State<T>,
19{
20    /// NumericalMethod input was bad
21    BadInput {
22        msg: String, // if input is bad, return this with reason
23    },
24    MaxSteps {
25        // If the solver reaches the maximum number of steps
26        t: T, // Time at which the solver reached maximum steps
27        y: V, // Solution at time t
28    },
29    StepSize {
30        t: T, // Time at which step size became too small
31        y: V, // Solution at time t
32    },
33    Stiffness {
34        t: T, // Time at which stiffness was detected
35        y: V, // Solution at time t
36    },
37}
38
39impl<T, V> Display for Error<T, V>
40where
41    T: Real + Display,
42    V: State<T> + Display,
43{
44    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
45        match self {
46            Self::BadInput { msg } => write!(f, "Bad Input: {}", msg),
47            Self::MaxSteps { t, y } => {
48                write!(f, "Maximum steps reached at (t, y) = ({}, {})", t, y)
49            }
50            Self::StepSize { t, y } => write!(f, "Step size too small at (t, y) = ({}, {})", t, y),
51            Self::Stiffness { t, y } => write!(f, "Stiffness detected at (t, y) = ({}, {})", t, y),
52        }
53    }
54}
55
56impl<T, V> Debug for Error<T, V>
57where
58    T: Real + Debug,
59    V: State<T> + Debug,
60{
61    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
62        match self {
63            Self::BadInput { msg } => write!(f, "Bad Input: {}", msg),
64            Self::MaxSteps { t, y } => {
65                write!(f, "Maximum steps reached at (t, y) = ({:?}, {:?})", t, y)
66            }
67            Self::StepSize { t, y } => {
68                write!(f, "Step size too small at (t, y) = ({:?}, {:?})", t, y)
69            }
70            Self::Stiffness { t, y } => {
71                write!(f, "Stiffness detected at (t, y) = ({:?}, {:?})", t, y)
72            }
73        }
74    }
75}
76
77impl<T, V> std::error::Error for Error<T, V>
78where
79    T: Real + Debug + Display,
80    V: State<T> + Debug + Display,
81{
82}