single_element_grid/
single_element_grid.rs

1use ndelement::types::ReferenceCellType;
2use ndgrid::grid::local_grid::SingleElementGridBuilder;
3use ndgrid::traits::{Builder, Entity, Geometry, Grid, Point, Topology};
4
5/// Creating a (serial) single element grid
6///
7/// In a single element grid, the same finite element will be used to represent the geometry
8/// of each cell. For example, a grid of bilinear quadrilaterals can be created by using a degree 1
9/// element on a quadrilateral
10fn main() {
11    // When creating the grid builder, we give the physical/geometric dimension (3) and the cell type
12    // and degree of the element
13    let mut b = SingleElementGridBuilder::<f64>::new(3, (ReferenceCellType::Quadrilateral, 1));
14    // Add six points with ids 0 to 5
15    b.add_point(0, &[0.0, 0.0, 0.0]);
16    b.add_point(1, &[1.0, 0.0, 0.0]);
17    b.add_point(2, &[2.0, 0.0, 0.2]);
18    b.add_point(3, &[0.0, 1.0, 0.0]);
19    b.add_point(4, &[1.0, 1.0, -0.2]);
20    b.add_point(5, &[2.0, 1.0, 0.0]);
21    // Add two cells
22    b.add_cell(0, &[0, 1, 3, 4]);
23    b.add_cell(1, &[1, 2, 4, 5]);
24    // Create the grid
25    let grid = b.create_grid();
26
27    // Print the coordinates or each point in the mesh
28    let mut coords = vec![0.0; grid.geometry_dim()];
29    for point in grid.entity_iter(ReferenceCellType::Point) {
30        point.geometry().points().collect::<Vec<_>>()[0].coords(coords.as_mut_slice());
31        println!("point {}: {:#?}", point.local_index(), coords);
32    }
33
34    // Print the vertices of each cell
35    for cell in grid.entity_iter(ReferenceCellType::Quadrilateral) {
36        println!(
37            "cell {}: {:?} ",
38            cell.local_index(),
39            cell.topology()
40                .sub_entity_iter(ReferenceCellType::Point)
41                .collect::<Vec<_>>()
42        );
43    }
44}