Skip to main content

linkcell/
error.rs

1//! Recoverable failures from a neighbour search.
2
3use std::fmt;
4
5/// Why a search could not run.
6#[derive(Clone, Copy, Debug, Eq, PartialEq)]
7pub enum Error {
8    /// `k == 0`.
9    ZeroK,
10    /// A box length is not strictly positive, or H is singular.
11    BadBox,
12    /// `n == 0` or the point list is empty. Not a buffer-size mismatch.
13    Empty,
14    /// Caller `out` length is not `n * k`.
15    BufferSize,
16    /// `n * k` does not fit a slice (`usize` or `isize`).
17    Overflow,
18    /// `mask` is `Some` and its length is not `n`.
19    MaskLen,
20    /// Linked-cell mesh would overflow or exceed the bin cap.
21    TooManyCells,
22    /// `cutoff` is not finite and strictly positive.
23    BadCutoff,
24    /// Periodic image stencil exceeds the 2^24 resource cap.
25    TooManyImages,
26    /// An axis is not periodic and `libvesin.so` is not loaded.
27    Partial,
28    /// The device is not the CPU and `libvesin.so` is not loaded.
29    Device,
30    /// A point has more neighbours than the requested pad.
31    Padded,
32    /// A coordinate is not finite.
33    NonFinite,
34    /// The part count is zero, or the balance bound is below one.
35    BadPartition,
36    /// The cell graph is not a square CSR of that grid.
37    BadGraph,
38    /// A device curve split was requested and no device is loaded.
39    NoDevice,
40    /// The device curve split disagreed with the host weights.
41    DeviceMismatch,
42    /// Scotch support was not compiled in.
43    ScotchDisabled,
44    /// The Scotch library rejected the graph.
45    ScotchFailed,
46}
47
48impl fmt::Display for Error {
49    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50        match self {
51            Error::ZeroK => write!(f, "k must be at least 1"),
52            Error::BadBox => write!(f, "singular or non-positive cell"),
53            Error::Empty => write!(f, "no points"),
54            Error::BufferSize => write!(f, "out buffer length must be n * k"),
55            Error::Overflow => write!(f, "n * k overflows"),
56            Error::MaskLen => write!(f, "mask length must be n"),
57            Error::TooManyCells => write!(f, "linked-cell mesh is too fine"),
58            Error::BadCutoff => write!(f, "cutoff must be finite and positive"),
59            Error::TooManyImages => write!(f, "periodic image count exceeds 2^24"),
60            Error::Partial => write!(
61                f,
62                "partial periodicity needs libvesin.so on the loader path"
63            ),
64            Error::Device => write!(f, "this device needs libvesin.so on the loader path"),
65            Error::Padded => write!(f, "a point has more neighbours than padded_to"),
66            Error::NonFinite => write!(f, "a coordinate is not finite"),
67            Error::BadPartition => {
68                write!(f, "part count must be positive and balance at least 1")
69            }
70            Error::BadGraph => write!(f, "cell graph is not a square CSR"),
71            Error::NoDevice => write!(f, "CUDA curve split is not available"),
72            Error::DeviceMismatch => {
73                write!(f, "device curve split disagreed with the host weights")
74            }
75            Error::ScotchDisabled => write!(f, "Scotch support is compiled out"),
76            Error::ScotchFailed => write!(f, "Scotch returned an error"),
77        }
78    }
79}
80
81impl std::error::Error for Error {}
82
83impl From<minimage::Error> for Error {
84    fn from(err: minimage::Error) -> Self {
85        match err {
86            minimage::Error::BadBox => Error::BadBox,
87            minimage::Error::BufferSize => Error::BufferSize,
88            minimage::Error::Empty => Error::Empty,
89        }
90    }
91}