Skip to main content

diffsol_la/
error.rs

1use faer::sparse::CreationError;
2use thiserror::Error;
3
4/// Error type for the diffsol linear algebra crate (`diffsol-la`).
5///
6/// This wraps the errors that can occur in the linear algebra layer: matrix
7/// operations, linear solvers, and (optionally) CUDA. It is re-exported by the
8/// `diffsol` crate and can be converted into `diffsol`'s top-level error type.
9#[derive(Error, Debug, Clone)]
10pub enum LaError {
11    #[error("Linear solver error: {0}")]
12    LinearSolverError(#[from] LinearSolverError),
13    #[error("Matrix error: {0}")]
14    MatrixError(#[from] MatrixError),
15    #[cfg(feature = "cuda")]
16    #[error("Cuda error: {0}")]
17    CudaError(#[from] CudaError),
18    #[error("Error: {0}")]
19    Other(String),
20}
21
22/// Possible errors that can occur when solving a linear problem
23#[derive(Error, Debug, Clone)]
24pub enum LinearSolverError {
25    #[error("LU not initialized")]
26    LuNotInitialized,
27    #[error("LU solve failed")]
28    LuSolveFailed,
29    #[error("Linear solver not setup")]
30    LinearSolverNotSetup,
31    #[error("Linear solver matrix not square")]
32    LinearSolverMatrixNotSquare,
33    #[error("Linear solver matrix not compatible with vector")]
34    LinearSolverMatrixVectorNotCompatible,
35    #[error("KLU failed to analyze")]
36    KluFailedToAnalyze,
37    #[error("KLU failed to factorize")]
38    KluFailedToFactorize,
39    #[error("Error: {0}")]
40    Other(String),
41}
42
43/// Possible errors for matrix operations
44#[derive(Error, Debug, Clone)]
45pub enum MatrixError {
46    #[error("Failed to create matrix from triplets: {0}")]
47    FailedToCreateMatrixFromTriplets(#[from] CreationError),
48    #[error("Cannot union matrices with different shapes")]
49    UnionIncompatibleShapes,
50    #[error("Cannot create a matrix with zero rows or columns")]
51    MatrixShapeError,
52    #[error("Index out of bounds")]
53    IndexOutOfBounds,
54    #[error("Error: {0}")]
55    Other(String),
56}
57
58#[cfg(feature = "cuda")]
59#[derive(Error, Debug, Clone)]
60pub enum CudaError {
61    #[error("Failed to allocate memory on GPU")]
62    CudaMemoryAllocationError,
63    #[error("Failed to initialize CUDA: {0}")]
64    CudaInitializationError(String),
65    #[error("Cuda error: {0}")]
66    Other(String),
67}
68
69#[cfg(feature = "cuda")]
70#[macro_export]
71macro_rules! cuda_error {
72    ($variant:ident) => {
73        $crate::error::LaError::from($crate::error::CudaError::$variant)
74    };
75    ($variant:ident, $($arg:tt)*) => {
76        $crate::error::LaError::from($crate::error::CudaError::$variant($($arg)*))
77    };
78}
79
80#[macro_export]
81macro_rules! linear_solver_error {
82    ($variant:ident) => {
83        $crate::error::LaError::from($crate::error::LinearSolverError::$variant)
84    };
85    ($variant:ident, $($arg:tt)*) => {
86        $crate::error::LaError::from($crate::error::LinearSolverError::$variant($($arg)*))
87    };
88}
89
90#[macro_export]
91macro_rules! matrix_error {
92    ($variant:ident) => {
93        $crate::error::LaError::from($crate::error::MatrixError::$variant)
94    };
95    ($variant:ident, $($arg:tt)*) => {
96        $crate::error::LaError::from($crate::error::MatrixError::$variant($($arg)*))
97    };
98}
99
100#[macro_export]
101macro_rules! la_other_error {
102    ($msg:expr) => {
103        $crate::error::LaError::Other($msg.to_string())
104    };
105}