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 {              // If the solver reaches the maximum number of steps
25        t: T,               // Time at which the solver reached maximum steps
26        y: V,// Solution at time t
27    }, 
28    StepSize {
29        t: T,               // Time at which step size became too small
30        y: V,// Solution at time t
31    },
32    Stiffness {
33        t: T,               // Time at which stiffness was detected
34        y: V,// Solution at time t
35    },
36}
37
38impl<T, V> Display for Error<T, V>
39where
40    T: Real + Display,
41    V: State<T> + Display,
42{
43    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44        match self {
45            Self::BadInput { msg } => write!(f, "Bad Input: {}", msg),
46            Self::MaxSteps { t, y } => write!(f, "Maximum steps reached at (t, y) = ({}, {})", t, y),
47            Self::StepSize { t, y } => write!(f, "Step size too small at (t, y) = ({}, {})", t, y),
48            Self::Stiffness { t, y } => write!(f, "Stiffness detected at (t, y) = ({}, {})", t, y),
49        }
50    }
51}
52
53impl<T, V> Debug for Error<T, V>
54where
55    T: Real + Debug,
56    V: State<T> + Debug,
57{
58    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59        match self {
60            Self::BadInput { msg } => write!(f, "Bad Input: {}", msg),
61            Self::MaxSteps { t, y } => write!(f, "Maximum steps reached at (t, y) = ({:?}, {:?})", t, y),
62            Self::StepSize { t, y } => write!(f, "Step size too small at (t, y) = ({:?}, {:?})", t, y),
63            Self::Stiffness { t, y } => write!(f, "Stiffness detected at (t, y) = ({:?}, {:?})", t, y),
64        }
65    }
66}
67
68impl<T, V> std::error::Error for Error<T, V>
69where
70    T: Real + Debug + Display,
71    V: State<T> + Debug + Display,
72{}