Skip to main content

diffsol_nl/
error.rs

1use diffsol_la::error::LaError;
2use thiserror::Error;
3
4/// Error type for the diffsol non-linear solver crate (`diffsol-nl`).
5///
6/// This wraps the errors that can occur in the non-linear solver layer, as well as
7/// the linear-algebra errors produced by the underlying [`diffsol_la`] linear solvers.
8/// It is re-exported by the `diffsol` crate and can be converted into `diffsol`'s
9/// top-level error type.
10#[derive(Error, Debug, Clone)]
11pub enum NlError {
12    #[error("Non-linear solver error: {0}")]
13    NonLinearSolverError(#[from] NonLinearSolverError),
14    #[error("Linear algebra error: {0}")]
15    LaError(#[from] LaError),
16    #[error("Error: {0}")]
17    Other(String),
18}
19
20/// Possible errors that can occur when solving a non-linear problem
21#[derive(Error, Debug, Clone)]
22pub enum NonLinearSolverError {
23    #[error("Initial condition solver did not converge")]
24    InitialConditionDidNotConverge,
25    #[error("Newton iterations did not converge, maximum iterations reached")]
26    NewtonMaxIterations,
27    #[error("Newton iteration diverged")]
28    NewtonDiverged,
29    #[error("Newton linesearch failed to find a suitable step in max iterations")]
30    LinesearchFailedMaxIterations,
31    #[error("Newton linesearch failed, minimum step size reached")]
32    LinesearchFailedMinStep,
33    #[error("LU solve failed")]
34    LuSolveFailed,
35    #[error("Jacobian not reset before calling solve")]
36    JacobianNotReset,
37    #[error("State has wrong length: expected {expected}, got {found}")]
38    WrongStateLength { expected: usize, found: usize },
39    #[error("Error: {0}")]
40    Other(String),
41}
42
43#[macro_export]
44macro_rules! non_linear_solver_error {
45    ($variant:ident) => {
46        $crate::error::NlError::from($crate::error::NonLinearSolverError::$variant)
47    };
48    ($variant:ident, $($arg:tt)*) => {
49        $crate::error::NlError::from($crate::error::NonLinearSolverError::$variant($($arg)*))
50    };
51}