Built-in meshes in FEniCS

Built-in meshes in FEniCS

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, nx denotes the number of intervals as input argument, and the total number of vertices is, therefore (nx+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(nx, ny, "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")
Here, we have not mentioned the orientation of the diagonals, so the default orientation is taken as right. We can also change the orientation of the diagonals as follows:
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 nx and ny 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(nx, ny, nz)

Here, nx, ny, and nz 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), nx, ny, nz)

Here, nx , ny and nz 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.

Leave a Reply