Best strategy for importing external meshes

Good morning,

I was wondering what the best strategy is for importing external meshes (2d/3d) into FEniCS. I have browsed various forums and found the following options:

(i) Create mesh in gmsh and use dolfin-convert to convert into xml file that can be imported into fenics.
(ii) Create mesh in gmsh and use meshio to import mesh into fenics
(iii) Create mesh in salome and import into fenics, via steps (i) or (ii).

I have also read that dolfin-convert is no longer maintained so it is not advisable. I have read somewhere that gmsh is okay if you used version 2 and not the latest version. I have also read that Salome is okay but it will not store geometric tags when you import mesh into FEniCS. Thus, I am not sure what to believe.

I have been successful in creating meshes with gmsh but I am having a hard time importing them to FeniCS. I have tried various meshio examples, but I have not been successful. Thus, I am wondering if there is a reliable strategy in place for importing 2d/3d meshes.

Thank you for your attention.

1 Like

I am also looking at Transitioning from mesh.xml to mesh.xdmf, from dolfin-convert to meshio - #2 by tianyikillua to see if I can work something out.

Hi @mechapple,

You can export from gmsh to .msh, then use the script I posted here to convert to XDMF, and subsequently load the XDMF file into your FEniCS script.


1 Like

I have shown how to import meshes from gmsh using meshio in:

1 Like

Thank you. For a 2d mesh, do I simply modify your script and remove the tetra section? or will your script work for both 2d (triangular) and 3d (tetrahedral) meshes.

EDIT: I figured it out. Thanks.

Hi @conpierce8,

I tried your solution and I got the following error:

KeyError                                  Traceback (most recent call last)
<ipython-input-5-3ab8b8b1a31e> in <module>
      5 msh =".msh")
----> 7 tet_data = msh.cell_data_dict["gmsh:physical"]["tetra"]
      8 meshio.write(mesh_name+".xdmf",
      9     meshio.Mesh(points=msh.points,

KeyError: 'gmsh:physical'

Is there a specific version of meshio or gmsh that I should be using?

EDIT: I fixed the error by creating physical entities in my gmsh model.

One method, which I am using extensively, is Salome export as med to gmsh to xml to h5. You need to install Salome and import CAD or generate your CAD file in there. By using Salome you can also select and give names to boundaries. Then after exporting, in FEniCS you see these names as numbers.

Have a look at Back Matter in this book
in Appendix A.3 Complicated Geometries in FEniCS

For any specific step, you find difficult, simply ask a question here, I will help.

If you want to have the book, write me an email, we find a solution as well.

Best, Emek