# Built-in meshes in FEniCS

- Post by: Anurag Gupta
- October 20, 2021
- No Comment

In FEniCS, we can either work with the inbuilt meshes or we can also import the mesh file generated in another pre-processing tool as a xdmf format. In order to create inbuilt meshes, firstly, `dolfin `

module is imported as:

```
from dolfin import *
import matplotlib.pyplot as plt
```

`Matplotlib`

library is used to display the mesh plots.

The different types of inbuilt meshes in FEniCS are:

1 **Unit Interval mesh:** This is a mesh generated over the unit interval [0,1]. The general syntax of this type of mesh is:

`mesh = UnitIntervalMesh(nx)`

Here, n_{x} denotes the number of intervals as input argument, and the total number of vertices is, therefore (n_{x}+1).

The implementation is as follows:

```
mesh = UnitIntervalMesh(5)
print("Plotting a UnitIntervalMesh")
plt.figure()
plot(mesh, title="Unit interval")
```

2. **Unit Square mesh:** This is a mesh generated on the unit square [0,1]×[0,1]. The general syntax of this type of mesh is:

`mesh = UnitSquareMesh(n`_{x}, n_{y}, "diagonal direction")

Here, the first two arguments represent the number of cells in the horizontal and vertical directions**. The third argument is an optional one** which represents the direction of the diagonals. This argument is either “right”, “left”, “right/left”, “left/right”, or “crossed”. This argument is optional and we can, therefore, also ignore this argument. If this argument is ignored, then the default direction of diagonal is taken as “right”. The implementation is as follows:

```
mesh = UnitSquareMesh(5, 5)
print("Plotting a UnitSquareMesh")
plt.figure()
plot(mesh, title="Unit square")
```

```
mesh = UnitSquareMesh(5, 5, "left")
print("Plotting a UnitSquareMesh")
plot(mesh, title="Unit square (left)")
```

```
mesh = UnitSquareMesh(5, 5, "crossed")
print("Plotting a UnitSquareMesh")
plot(mesh, title="Unit square (crossed)")
```

```
mesh = UnitSquareMesh(5, 5, "right/left")
print("Plotting a UnitSquareMesh")
plt.figure()
plot(mesh, title="Unit square (right/left)")
```

3**. Rectangle** **mesh:** This creates a mesh of a 2D rectangle spanned by two points (opposing corners) of the rectangle. The general syntax of this type of mesh is:

`mesh = RectangleMesh(Point("coordinates of first corner point"), Point("coordinates of second corner point), nx, ny, "diagonal direction")`

Here, the n_{x} and n_{y }represent the number of cells in the horizontal and vertical directions and the direction of the diagonals is given as a final optional argument (“left”, “right”, “left/right”, or “crossed”). The implementation is as follows:

```
mesh = RectangleMesh(Point(0.0, 0.0), Point(10.0, 5), 5, 5)
print("Plotting a RectangleMesh")
plt.figure()
plot(mesh, title="Rectangle")
```

```
mesh = RectangleMesh(Point(-3.0, 2.0), Point(7.0, 6.0), 5, 5, "left/right")
print("Plotting a RectangleMesh")
plt.figure()
plot(mesh, title="Rectangle (left/right)")
```

4. **Unit Cube mesh:** This is used to make a mesh of the 3D unit cube [0,1]×[0,1]×[0,1]. The general syntax of this type of mesh is:

`mesh = UnitCubeMesh(n`_{x}, n_{y}, n_{z})

Here, n_{x}, n_{y,} and n_{z} represent the number of cells in the x-, y- and z-directions respectively. The implementation is as follows:

```
mesh = UnitCubeMesh(5, 5, 5)
print("Plotting a UnitCubeMesh")
plt.figure()
plot(mesh, title="Unit cube")
```

5. **Box mesh:** This is used to make a mesh on a rectangular prism in 3D. The prism is specified by two points (opposing corners) of the prism. The general syntax of this type of mesh is:

`mesh = BoxMesh(Point("coordinates of first corner point"), Point("coordinates of second corner point), n`_{x}, n_{y}, n_{z})

Here, n_{x} , n_{y} and n_{z} represents the number of divisions in the x , y and z directions respectively. The implementation is as follows:

```
mesh = BoxMesh(Point(0.0, 0.0, 0.0), Point(7, 4.0, 2.0), 5, 5, 5)
print("Plotting a BoxMesh")
plt.figure()
plot(mesh, title="Box")
plt.show()
```

Some information related to the mesh can be obtained using the following commands:

mesh.coordinates( ):returns the coordinates of the vertices of the mesh as a numpy array.

mesh.num_cells( ):returns the no. of cells (or elements) in the mesh.

mesh.num_vertices( ):returns the no. of vertices in the mesh.

str(mesh):returns the mesh description.

**Categories:**FEniCS, Finite element method

**Tagged:**fem, FEniCS, finite element