Skip to main content

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 parametric coordinates for a point (optional)
29    fn add_point_parametric_coords(&mut self, _id: usize, _entity_dim: usize, _coords: &[Self::T]);
30
31    /// Add a cell to the grid
32    fn add_cell(&mut self, id: usize, cell_data: Self::CellData<'_>);
33
34    /// Add a cell to the grid
35    fn add_cell_from_nodes_and_type(
36        &mut self,
37        id: usize,
38        nodes: &[usize],
39        cell_type: Self::EntityDescriptor,
40        cell_degree: usize,
41    );
42
43    /// Create the grid
44    fn create_grid(&self) -> Self::Grid;
45
46    /// Number of points
47    fn point_count(&self) -> usize;
48
49    /// Number of cells
50    fn cell_count(&self) -> usize;
51
52    /// Get the insertion ids of each point
53    fn point_indices_to_ids(&self) -> &[usize];
54
55    /// Get the insertion ids of each cell
56    fn cell_indices_to_ids(&self) -> &[usize];
57
58    /// Get the indices of the points of a cell
59    fn cell_points(&self, index: usize) -> &[usize];
60
61    /// Get the indices of the points of a cell
62    fn cell_vertices(&self, index: usize) -> &[usize];
63
64    /// Get the coordinates of a point
65    fn point(&self, index: usize) -> &[Self::T];
66
67    /// Get all points
68    fn points(&self) -> &[Self::T];
69
70    /// Get parametric coordinates for a point, if available
71    fn point_parametric_coords(&self, _index: usize) -> Option<(usize, &[Self::T])>;
72
73    /// Get the type of a cell
74    fn cell_type(&self, index: usize) -> Self::EntityDescriptor;
75
76    /// Get the degree of a cell's geometry
77    fn cell_degree(&self, index: usize) -> usize;
78
79    /// Geometric dimension
80    fn gdim(&self) -> usize;
81
82    /// Topoligical dimension
83    fn tdim(&self) -> usize;
84
85    /// Number of points in a cell with the given type and degree
86    fn npts(&self, cell_type: Self::EntityDescriptor, degree: usize) -> usize;
87}
88
89/// Trait for building a geometry
90///
91/// This trait is usually not called by the user. It provides
92/// an interface to building the geometry information of the grid.
93pub(crate) trait GeometryBuilder: Builder {
94    /// Grid geometry type
95    type GridGeometry;
96
97    /// Create geometry
98    fn create_geometry(
99        &self,
100        point_ids: &[usize],
101        coordinates: &[Self::T],
102        cell_points: &[usize],
103        cell_types: &[Self::EntityDescriptor],
104        cell_degrees: &[usize],
105    ) -> Self::GridGeometry;
106}
107
108/// Trait for building a topology
109///
110/// This trait is usually not called by the user. It provides
111/// an interface to building the topology information of the grid.
112pub(crate) trait TopologyBuilder: Builder {
113    /// Grid topology type
114    type GridTopology;
115
116    /// Create topology
117    fn create_topology(
118        &self,
119        vertex_ids: Vec<usize>,
120        cell_ids: Vec<usize>,
121        cells: &[usize],
122        cell_types: &[Self::EntityDescriptor],
123    ) -> Self::GridTopology;
124
125    /// Extract the cell vertices from the cell points
126    fn extract_vertices(
127        &self,
128        cell_points: &[usize],
129        cell_types: &[Self::EntityDescriptor],
130        cell_degrees: &[usize],
131    ) -> Vec<usize>;
132}
133
134/// Trait for building a grid from topology and geometry
135///
136/// This trait is usually not called by the user. It provides
137/// an interface to building the grid from a given topology and Geometry.
138pub(crate) trait GridBuilder: Builder + GeometryBuilder + TopologyBuilder {
139    /// Create topology
140    fn create_grid_from_topology_geometry(
141        &self,
142        topology: <Self as TopologyBuilder>::GridTopology,
143        geometry: <Self as GeometryBuilder>::GridGeometry,
144    ) -> <Self as Builder>::Grid;
145}
146
147/// MPI parallelized grid builder
148#[cfg(feature = "mpi")]
149pub trait ParallelBuilder: Builder {
150    /// Parallel grid type
151    type ParallelGrid<'a, C: Communicator + 'a>: ParallelGrid<C = C>
152    where
153        Self: 'a;
154
155    /// Create a parallel grid (call from root)
156    fn create_parallel_grid_root<'a, C: Communicator>(
157        &self,
158        comm: &'a C,
159        partitioner: GraphPartitioner,
160    ) -> Self::ParallelGrid<'a, C>;
161
162    /// Create a parallel grid (call from other processes)
163    fn create_parallel_grid<'a, C: Communicator>(
164        &self,
165        comm: &'a C,
166        root_rank: i32,
167    ) -> Self::ParallelGrid<'a, C>;
168}