Skip to main content

tpt_sci_ode/
error.rs

1use thiserror::Error;
2
3/// Errors raised while building or solving an [`OdeProblem`](crate::OdeProblem).
4#[derive(Debug, Error)]
5pub enum OdeError {
6    /// The problem definition was invalid (e.g. empty initial state).
7    #[error("invalid ODE problem: {0}")]
8    Invalid(String),
9
10    /// The nonlinear solver (Newton) failed to converge within the iteration
11    /// budget, or the linear solve was singular. Usually a sign that the step
12    /// size is too large for the current state; the integrator retries with a
13    /// smaller step before surfacing this error.
14    #[error("nonlinear solve failed to converge at t = {t} (last residual {residual:.3e})")]
15    Newton {
16        /// Time at which the failure occurred.
17        t: f64,
18        /// Magnitude of the residual on the final Newton iteration.
19        residual: f64,
20    },
21
22    /// The step-size controller drove the step below the smallest representable
23    /// increment (e.g. at a discontinuity or a very stiff region), so the
24    /// requested `t_final` cannot be reached.
25    #[error("step size collapsed below machine precision at t = {t}")]
26    StepTooSmall {
27        /// Time at which the step collapsed.
28        t: f64,
29    },
30
31    /// The integrator exceeded its maximum step count (a safety rail against
32    /// runaway step contraction); indicates the problem is far stiffer than the
33    /// configured method can handle at the requested tolerances.
34    #[error("maximum number of steps ({max_steps}) exceeded before reaching t = {t_final}")]
35    MaxSteps {
36        /// Target time that was not reached.
37        t_final: f64,
38        /// Configured step ceiling.
39        max_steps: usize,
40    },
41}
42
43impl OdeError {
44    pub(crate) fn invalid(msg: impl Into<String>) -> Self {
45        OdeError::Invalid(msg.into())
46    }
47}