Mesh generation in FEniCS

FEniCS comes with built-in mesh generation that allows relatively complex domains to be defined and meshed using simple Python code. The meshing capabilities of FEniCS are handled by the (optional) FEniCS component mshr and the actual meshing is performed by CGAL or Tetgen. The interface is inspired by that of Netgen in that domains are defined in terms of Constructive Solid Geometry (CSG).

For example, here’s how to create a mesh of the Death Star in FEniCS:

Death Star

mshr is the work by my student Benjamin Kehlet who has done a great job with designing an intuitive interface and handling corner cases, cleanups of geometries and various other issues that must be handled to deliver a smooth user experience. A feature that he recently added is the ability to define subdomains as part of domains and meshes. The following code taken from the upcoming FEniCS tutorial illustrates how to define subdomains in a mesh, here for a 2D cross-section of an iron cylinder with a coppper wire wound 10 times around the cylinder:

The following plot shows a detail of the mesh. The circles representing cross-sections of the copper wire are clearly visible, as is the outline of the iron cylinder.

Magnetostatics mesh

The subdomain information is carried over to the FEniCS mesh and can be used to define variational problems, set boundary conditions etc. Here’s a code snippet illustrating how to extract the subdomain data and use it for defining measures for the different subdomains:

Finally, calling solve(a == L) in FEniCS gives us the solution. The plots below show the (z-component of the) magnetic vector potential and the magnetic field.

Magnetostatics potential

Magnetostatics magnetic field

Recent Posts

Recent Comments


logg Written by:

Anders Logg is Professor of Computational Mathematics at Chalmers University of Technology. His research interests are adaptive finite element methods, high-level automating software systems for solution of PDE, domain-specific languages and compilers in scientific computing, and applications in biomedicine, general relativity, and architecture. Logg is head of the unit for Computational Mathematics and excellence profile leader within Chalmers Area of Advance the Built Environment. He is director of the Swedish Network for Mathematics in Industry and member of the EMS Applied Mathematics Committee and the Swedish National Committee for Mathematics. He is co-founder and a core developer of the FEniCS Project, a leading open-source software for automated solution of PDE. Logg works part-time as scientific advisor to Fraunhofer-Chalmers Centre and Simula Research Laboratory.


  1. Dante
    June 8, 2016

    It looks great.
    I’ve already try generate some 2D geometry and I have one question:
    How I can generate a structured mesh?
    For example, I want create 2D rectangular mesh with rectangular holes.

    • June 8, 2016

      There’s no problem doing rectangular holes but I don’t think it’s possible (either because it’s not available in the meshing backends or we don’t have an interface for it) to guarantee that the mesh is structured.

Leave a Reply

Your email address will not be published. Required fields are marked *