How to convert msh file to xdmf and load it into dolfin-x?

Hi everyone,
I am trying to load this mesh file (.msh) into DOLFIN-x which is generated by GMSH:

but I have a problem how to write it in xdmf?

Either save it as XDMF as described here: Transitioning from mesh.xml to mesh.xdmf, from dolfin-convert to meshio
or load it directly using and:


Thanks for reply dokken,
Now I am trying to follow this tutorial as you mentioned:

and it is working for triangle cell type properly.

test.msh file:

is converted to test.xdmf file:


Is this method expandable on quadrilateral elements?

Yes, just change the celltype to “quad”.

1 Like

Thank you again dokken. It works very well for quad cell type too. But I have a problem in loading it to dolfin-x. this is my code:

import meshio
msh ="test.msh")
for cell in msh.cells:
    quad_cells =
for key in msh.cell_data_dict["gmsh:physical"].keys():
    if key == "quad":
        quad_data = msh.cell_data_dict["gmsh:physical"][key]
mesh =meshio.Mesh(points=msh.points,
                           cells=[("quad", quad_cells)],

import dolfinx
from import XDMFFile

with XDMFFile(MPI.COMM_WORLD, "test.xdmf", "w") as file:

but it leads to an error:

RuntimeError                              Traceback (most recent call last)
<ipython-input-24-951cea9a4399> in <module>
     16 with XDMFFile(MPI.COMM_WORLD, "test.xdmf", "w") as file:
---> 17     file.read_mesh(mesh)

/usr/local/dolfinx-real/lib/python3.8/dist-packages/dolfinx/ in read_mesh(self, ghost_mode, name, xpath)
     45     def read_mesh(self, ghost_mode=cpp.mesh.GhostMode.shared_facet, name="mesh", xpath="/Xdmf/Domain"):
     46         # Read mesh data from file
---> 47         cell_type = super().read_cell_type(name, xpath)
     48         cells = super().read_topology_data(name, xpath)
     49         x = super().read_geometry_data(name, xpath)

RuntimeError: <Grid> with name 'mesh' not found.

Use the following as meshio creates meshes with name “Grid”, while dolfinx expect the mesh by default to be named “mesh”

with, filename, "r") as xdmf:
       mesh = xdmf.read_mesh(name="Grid")

Thanks for your time dear dokken,
your kind guidance helped to solve the problem.