Skip to main content

kcl_ezpz/
error.rs

1use faer::{
2    linalg::svd::SvdError,
3    sparse::{CreationError, FaerError, linalg::LuError},
4};
5
6use crate::Id;
7
8/// Errors from parsing and executing ezpz's textual representation.
9#[derive(thiserror::Error, Debug)]
10#[cfg_attr(not(feature = "unstable-exhaustive"), non_exhaustive)]
11pub enum TextualError {
12    /// No initial guess was given for this label.
13    #[error("No guess was given for point {label}")]
14    MissingGuess {
15        /// The entity that didn't have any guesses
16        label: String,
17    },
18    /// No initial guess was given for this label.
19    #[error("You gave a guess for points which weren't defined: {labels:?}")]
20    UnusedGuesses {
21        /// The entities you gave guesses for which weren't defined.
22        labels: Vec<String>,
23    },
24    /// You referred to an entity that was never defined.
25    #[error("You referred to the point {label} but it was never defined")]
26    UndefinedPoint {
27        /// The undefined point.
28        label: String,
29    },
30}
31
32/// Errors that could occur when running the core Newton-Gauss solve.
33#[derive(thiserror::Error, Debug)]
34#[cfg_attr(not(feature = "unstable-exhaustive"), non_exhaustive)]
35pub enum NonLinearSystemError {
36    /// ID was not found.
37    #[error("ID {0} not found")]
38    NotFound(Id),
39    /// There should be exactly 1 guess per variable, but you supplied the wrong number.
40    #[error(
41        "There should be exactly 1 guess per variable, but you supplied {labels} variables and must {guesses} guesses"
42    )]
43    WrongNumberGuesses {
44        /// How many variables/labels were given.
45        labels: usize,
46        /// How many guesses were given.
47        guesses: usize,
48    },
49    /// Constraint references a variable that doesn't appear in the initial guesses.
50    #[error(
51        "Constraint {constraint_id} references variable {variable} but no such variable appears in your initial guesses."
52    )]
53    MissingGuess {
54        /// Which constraint ID referenced a missing variable.
55        constraint_id: usize,
56        /// Which variable was missing.
57        variable: Id,
58    },
59    /// Faer: could not create a matrix.
60    #[error("Could not create matrix: {error}")]
61    FaerMatrix {
62        /// Underlying error.
63        #[from]
64        error: CreationError,
65    },
66    /// Faer: general error.
67    #[error("Something went wrong in faer: {error}")]
68    Faer {
69        /// Underlying error.
70        #[from]
71        error: FaerError,
72    },
73    /// Faer: could not solve the matrix in the Newton-Gauss loop.
74    #[error("Something went wrong doing matrix solves in faer: {error}")]
75    FaerSolve {
76        /// Underlying error.
77        #[from]
78        error: LuError,
79    },
80    /// Faer: could not decompose Jacobian.
81    #[error("Something went wrong doing SVD in faer")]
82    FaerSvd(SvdError),
83    /// Solver did not find a solution within the allowed number of iterations.
84    /// Consider raising the iterations?
85    #[error("Could not find a solution in the allowed number of iterations")]
86    DidNotConverge,
87    /// You provided an empty constraint system.
88    #[error("Cannot solve an empty system")]
89    EmptySystemNotAllowed,
90}