Import mesh from Gmsh

Hello,
I am trying to import to Fenics a mesh generated with Gmsh. I have taken the simple mesh “t1.msh” of the documentation of Gmsh (“t1.geo”). Following a previous discussion in this forum

https://fenicsproject.discourse.group/t/transitioning-from-mesh-xml-to-mesh-xdmf-from-dolfin-convert-to-meshio/412/174

I have arrived to this code (which use meshio)

msh = meshio.read("t1.msh")

def create_mesh(mesh, cell_type, prune_z=False):
    cells = mesh.get_cells_type(cell_type)
    cell_data = mesh.get_cell_data("gmsh:physical", cell_type)
    out_mesh = meshio.Mesh(points=mesh.points, cells={
                           cell_type: cells}, cell_data={"name_to_read": [cell_data]})
    if prune_z:
        out_mesh.prune_z_0()
    return out_mesh

mesh2d = create_mesh(msh, "triangle")

However there are some errors when trying to use the mesh “mesh2d”. Precisely, when I try to plot the mesh in Fenics, I find the message “‘Mesh’ object has no attribute ‘ufl_domain’”, while when I try to define a finite element space the message error is “‘Mesh’ object has no attribute ‘ufl_cell’”.

It seems like the imported mesh is incomplete. I would like to ask if this is a fault of my “.msh” file or if there is something missing in the code above. Thanks!

This only creates a meshio mesh, not a mesh for usage in legacy dolfin. You would have to save this mesh to xdmf and then reload it, as done in Transitioning from mesh.xml to mesh.xdmf, from dolfin-convert to meshio - #176 by dokken

Thank you very much!
Now I have completed the code as shown below (just for completeness).
But I have still a small question. The geometric dimension of the “mesh2d” which is created with this code is 3, so it is a triangular mesh immersed in R^3. How can I do to obtain a mesh of geometric dimension 2? I tried using “prune_z=True” when calling “create_mesh”:

mesh2d = create_mesh(msh, "triangle",prune_z=True)

but this gives the error “AttributeError: ‘Mesh’ object has no attribute ‘prune_z_0’”

My complete code:

msh = meshio.read("t1.msh")

def create_mesh(mesh, cell_type, prune_z=False):
    cells = mesh.get_cells_type(cell_type)
    cell_data = mesh.get_cell_data("gmsh:physical", cell_type)
    out_mesh = meshio.Mesh(points=mesh.points, cells={
                           cell_type: cells}, cell_data={"name_to_read": [cell_data]})
    if prune_z:
        out_mesh.prune_z_0()
    return out_mesh

mesh2d = create_mesh(msh, "triangle")
meshio.write("mesh.xdmf", mesh_out)
mesh2d = Mesh()
with XDMFFile("mesh.xdmf") as infile:
    infile.read(mesh2d)

There are plenty of posts regarding this already, see

for updated meshio syntax in the create_mesh function.

I couldn’t find the right post to follow. Now all is ok! Thank you!

Hello @dokken
How can I export mesh from gmsh to FEnCSx?. Previously, in FEniCS, I exported through terminal with this command

dolfin-convert mesh.msh mesh.xml

I also generated two additional archives with boundary_markers and physical_groups, and I want to replicate this in FeniCSx. Is there another way to do it?

Please look at the examples:
https://docs.fenicsproject.org/dolfinx/v0.7.3/python/demos/demo_gmsh.html
or
https://jsdokken.com/dolfinx-tutorial/chapter1/membrane.html
or
https://jsdokken.com/dolfinx-tutorial/chapter3/subdomains.html#subdomains-defined-from-external-mesh-data