differential_equations/
error.rs

1//! NumericalMethod Trait for Differential Equation NumericalMethods
2
3use crate::traits::Real;
4use nalgebra::SMatrix;
5use std::fmt::{Debug, Display};
6
7/// Error for Differential Equation NumericalMethods
8///
9/// # Variants
10/// * `BadInput` - NumericalMethod input was bad.
11/// * `MaxSteps` - NumericalMethod reached maximum steps.
12/// * `StepSize` - NumericalMethod terminated due to step size converging too small of a value.
13/// * `Stiffness` - NumericalMethod terminated due to stiffness.
14///
15#[derive(PartialEq, Clone)]
16pub enum Error<T, const R: usize, const C: usize>
17where
18    T: Real,
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: SMatrix<T, R, C>,// Solution at time t
27    }, 
28    StepSize {
29        t: T,               // Time at which step size became too small
30        y: SMatrix<T, R, C>,// Solution at time t
31    },
32    Stiffness {
33        t: T,               // Time at which stiffness was detected
34        y: SMatrix<T, R, C>,// Solution at time t
35    },
36}
37
38impl<T, const R: usize, const C: usize> Display for Error<T, R, C>
39where
40    T: Real + Display,
41{
42    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43        match self {
44            Self::BadInput { msg } => write!(f, "Bad Input: {}", msg),
45            Self::MaxSteps { t, y } => write!(f, "Maximum steps reached at (t, y) = ({}, {})", t, y),
46            Self::StepSize { t, y } => write!(f, "Step size too small at (t, y) = ({}, {})", t, y),
47            Self::Stiffness { t, y } => write!(f, "Stiffness detected at (t, y) = ({}, {})", t, y),
48        }
49    }
50}
51
52impl<T, const R: usize, const C: usize> Debug for Error<T, R, C>
53where
54    T: Real + Debug,
55{
56    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57        match self {
58            Self::BadInput { msg } => write!(f, "Bad Input: {}", msg),
59            Self::MaxSteps { t, y } => write!(f, "Maximum steps reached at (t, y) = ({}, {})", t, y),
60            Self::StepSize { t, y } => write!(f, "Step size too small at (t, y) = ({}, {})", t, y),
61            Self::Stiffness { t, y } => write!(f, "Stiffness detected at (t, y) = ({}, {})", t, y),
62        }
63    }
64}
65
66impl<T, const R: usize, const C: usize> std::error::Error for Error<T, R, C>
67where
68    T: Real + Debug,
69{}