Tutorial – Import 2D geometry in FreeFem through freeCAD and GMSH

When i first approached to freeFEM all my attempts in importing a geometry from a CAD file ended in a fail. Then after some attempts i found this procedure working. Hope it can help.


You will need:

  • freeCAD for geometry creation
  • Gmsh for mesh creation
  • freeFEM++

Sketch creation in FreeCAD

First step is the creation of a sketch. in this case i created some circles, each one will rapresent a different region in freeFEM++. When the geometry is ready perform a fusion of all the sketches and export in .BREP format.

Import in GMSH

Open Gmsh and import your geometry using the merge command.

If you go in tools and then in visibility you should see curves and points labels. These are called “Elementary entities“, first we need to create all our elementary surfaces. To do so go in left panel under “Elementary entities “, open “add” tab and select “plane surface”. To define a surface we need to select its boundaries, see the three pictures below:

Note that even if i want to group the 7 small circles in an unique region i need to create 7 different surfaces.

FreeFEM require the definition of “Pysical groups” as boundaries and regions. So you need first to create some “Pysical curves” selecting “Physical groups” in left panel, then “add” and finally “curve”. Select one of your boundaries and press “e” to confirm, as in figure below. Once you have finished you should see a number near each curve, as in figure. This number is the same used in freeFEM to define boundaries.

Now we need to create the mesh, basic steps consists in expanding “mesh” in left panel and then click on 1D and 2D in order. This will create the mesh as in pitcure.

Finally wee need to create “Pysical surfaces“, FreeFEM will recognize them as different regions. To do so go in “Physical group” tab, select “Add” and then “Surface”. Select all the surfaces you want to be part of the same region as i did in figure below, then press “e” and go on with another selection.

Now each region should be labeld, the number will be the same in freeFEM:

region labels

If not, go in tools -> options -> mesh, select “physical group tag” and “2D element labels”, you can do the same for boundaries going in Geometry tab (instead of mesh)

Now you can export your mesh as “.msh” file, version 2 ASCII format.

Import in FreeFEM++

To import the mesh in freeFEM first we need to load “gmsh” library, then we will use gmshload(“mesh_name.msh”) command, as follow:

load "gmsh";
mesh Th = gmshload("mesh_name.msh");