ndgrid/traits/
builder.rs

1//! Grid builder
2use crate::{traits::Grid, types::Scalar};
3#[cfg(feature = "mpi")]
4use crate::{traits::ParallelGrid, types::GraphPartitioner};
5#[cfg(feature = "mpi")]
6use mpi::traits::Communicator;
7use std::fmt::Debug;
8use std::hash::Hash;
9
10/// A builder is a factory that creates meshes.
11///
12/// After instantiation points and cells can be added.
13/// To build the actual grid call [Builder::create_grid].
14pub trait Builder {
15    /// Type used as identifier of different entity types
16    type EntityDescriptor: Debug + PartialEq + Eq + Clone + Copy + Hash;
17
18    /// The type of the grid that the builder creates
19    type Grid: Grid<EntityDescriptor = Self::EntityDescriptor>;
20    /// The floating point type used for coordinates
21    type T: Scalar;
22    /// The type of the data that is input to add a cell
23    type CellData<'a>;
24
25    /// Add a point to the grid
26    fn add_point(&mut self, id: usize, data: &[Self::T]);
27
28    /// Add a cell to the grid
29    fn add_cell(&mut self, id: usize, cell_data: Self::CellData<'_>);
30
31    /// Add a cell to the grid
32    fn add_cell_from_nodes_and_type(
33        &mut self,
34        id: usize,
35        nodes: &[usize],
36        cell_type: Self::EntityDescriptor,
37        cell_degree: usize,
38    );
39
40    /// Create the grid
41    fn create_grid(&self) -> Self::Grid;
42
43    /// Number of points
44    fn point_count(&self) -> usize;
45
46    /// Number of cells
47    fn cell_count(&self) -> usize;
48
49    /// Get the insertion ids of each point
50    fn point_indices_to_ids(&self) -> &[usize];
51
52    /// Get the insertion ids of each cell
53    fn cell_indices_to_ids(&self) -> &[usize];
54
55    /// Get the indices of the points of a cell
56    fn cell_points(&self, index: usize) -> &[usize];
57
58    /// Get the indices of the points of a cell
59    fn cell_vertices(&self, index: usize) -> &[usize];
60
61    /// Get the coordinates of a point
62    fn point(&self, index: usize) -> &[Self::T];
63
64    /// Get all points
65    fn points(&self) -> &[Self::T];
66
67    /// Get the type of a cell
68    fn cell_type(&self, index: usize) -> Self::EntityDescriptor;
69
70    /// Get the degree of a cell's geometry
71    fn cell_degree(&self, index: usize) -> usize;
72
73    /// Geometric dimension
74    fn gdim(&self) -> usize;
75
76    /// Topoligical dimension
77    fn tdim(&self) -> usize;
78
79    /// Number of points in a cell with the given type and degree
80    fn npts(&self, cell_type: Self::EntityDescriptor, degree: usize) -> usize;
81}
82
83/// Trait for building a geometry
84///
85/// This trait is usually not called by the user. It provides
86/// an interface to building the geometry information of the grid.
87pub(crate) trait GeometryBuilder: Builder {
88    /// Grid geometry type
89    type GridGeometry;
90
91    /// Create geometry
92    fn create_geometry(
93        &self,
94        point_ids: &[usize],
95        coordinates: &[Self::T],
96        cell_points: &[usize],
97        cell_types: &[Self::EntityDescriptor],
98        cell_degrees: &[usize],
99    ) -> Self::GridGeometry;
100}
101
102/// Trait for building a topology
103///
104/// This trait is usually not called by the user. It provides
105/// an interface to building the topology information of the grid.
106pub(crate) trait TopologyBuilder: Builder {
107    /// Grid topology type
108    type GridTopology;
109
110    /// Create topology
111    fn create_topology(
112        &self,
113        vertex_ids: Vec<usize>,
114        cell_ids: Vec<usize>,
115        cells: &[usize],
116        cell_types: &[Self::EntityDescriptor],
117    ) -> Self::GridTopology;
118
119    /// Extract the cell vertices from the cell points
120    fn extract_vertices(
121        &self,
122        cell_points: &[usize],
123        cell_types: &[Self::EntityDescriptor],
124        cell_degrees: &[usize],
125    ) -> Vec<usize>;
126}
127
128/// Trait for building a grid from topology and geometry
129///
130/// This trait is usually not called by the user. It provides
131/// an interface to building the grid from a given topology and Geometry.
132pub(crate) trait GridBuilder: Builder + GeometryBuilder + TopologyBuilder {
133    /// Create topology
134    fn create_grid_from_topology_geometry(
135        &self,
136        topology: <Self as TopologyBuilder>::GridTopology,
137        geometry: <Self as GeometryBuilder>::GridGeometry,
138    ) -> <Self as Builder>::Grid;
139}
140
141/// MPI parallelized grid builder
142#[cfg(feature = "mpi")]
143pub trait ParallelBuilder: Builder {
144    /// Parallel grid type
145    type ParallelGrid<'a, C: Communicator + 'a>: ParallelGrid<C = C>
146    where
147        Self: 'a;
148
149    /// Create a parallel grid (call from root)
150    fn create_parallel_grid_root<'a, C: Communicator>(
151        &self,
152        comm: &'a C,
153        partitioner: GraphPartitioner,
154    ) -> Self::ParallelGrid<'a, C>;
155
156    /// Create a parallel grid (call from other processes)
157    fn create_parallel_grid<'a, C: Communicator>(
158        &self,
159        comm: &'a C,
160        root_rank: i32,
161    ) -> Self::ParallelGrid<'a, C>;
162}