Skip to main content

outram_foam_basic_lib/mesh/
error.rs

1// SPDX-License-Identifier: GPL-3.0-only
2// Copyright (C) 2026 OUTRAM PARK contributors
3
4/// Errors produced by the mesh layer (mesh construction and validation).
5#[derive(Debug, Clone, PartialEq)]
6pub enum MeshError {
7    /// An array field in the mesh has the wrong length.
8    ///
9    /// For example, `owner` must have length `n_faces`; `neighbour` must have
10    /// length `n_internal_faces`; `cell_volumes` must have length `n_cells`.
11    ArrayLengthMismatch {
12        /// Name of the offending array (e.g. `"owner"`, `"cell_volumes"`).
13        array: &'static str,
14        expected: usize,
15        got: usize,
16    },
17
18    /// A boundary patch does not start immediately after the previous one,
19    /// leaving a gap or overlap in face coverage.
20    PatchStartMismatch {
21        /// Name of the offending patch.
22        name: String,
23        expected: usize,
24        got: usize,
25    },
26
27    /// The sum of all patch sizes does not equal the number of boundary faces.
28    PatchCoverageMismatch {
29        /// Total face count covered by all patches.
30        covered: usize,
31        /// Total face count in the mesh.
32        n_faces: usize,
33    },
34
35    /// `number_of_cells` was zero or negative when building a 1-D mesh.
36    NonPositiveCellCount { got: i64 },
37}
38
39impl std::fmt::Display for MeshError {
40    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41        match self {
42            MeshError::ArrayLengthMismatch {
43                array,
44                expected,
45                got,
46            } => write!(
47                f,
48                "mesh array '{array}': expected length {expected}, got {got}"
49            ),
50            MeshError::PatchStartMismatch {
51                name,
52                expected,
53                got,
54            } => write!(
55                f,
56                "patch '{name}': expected to start at face {expected}, starts at {got}"
57            ),
58            MeshError::PatchCoverageMismatch { covered, n_faces } => write!(
59                f,
60                "boundary patches cover {covered} faces but n_faces = {n_faces}"
61            ),
62            MeshError::NonPositiveCellCount { got } => {
63                write!(f, "number_of_cells must be ≥ 1, got {got}")
64            }
65        }
66    }
67}
68
69impl std::error::Error for MeshError {}