Error while importing gmsh mesh

Hi,
I’m trying to import a 3D mesh from gmsh and I’m having the following Error:

RuntimeError Traceback (most recent cal l last) Cell In[89], line 11 9 mvc_2d = MeshValueCollection(“size_t”, mesh, 2) 10 with XDMFFile(path + filename + “_triangle.xdmf”) as infile: —> 11 infile.read(mvc_2d, “name_to_read”) 13 mf_2d = cpp.mesh.MeshFunctionSizet(mesh, mvc_2d) 15 n = FacetNormal(mesh) RuntimeError:
*** -------------------------------------------------------------------- ----- ***
DOLFIN encountered an error. If you are not able to resolve this issue *** using the information listed below, you can ask for help at *** *** fenics-support@googlegroups.com *** *** Remember to include the error message listed below and, if possi ble, *** include a minimal running example to reproduce the error. ***
*** -------------------------------------------------------------------- ----- ***
Error: Unable to recognise cell type. *** Reason: Unknown value “”. *** Where: This error was encountered inside XDMFFile.cpp. *** Process: 0 *** *** DOLFIN version: 2019.1.0 ***

The code I’m using to import the mesh is the following:

from dolfin import * 
path=" "
filename="Design"
mesh = Mesh()
with XDMFFile(path + filename + "_tetra.xdmf") as infile:
    infile.read(mesh)
mvc_3d = MeshValueCollection("size_t", mesh, 3) 
with XDMFFile("path + filename + "_tetra.xdmf") as infile:
    infile.read(mvc_3d, "name_to_read")
mf_3d = cpp.mesh.MeshFunctionSizet(mesh, mvc_3d)

mvc_2d = MeshValueCollection("size_t", mesh, 2)
with XDMFFile("path + filename + "_triangle.xdmf") as infile:
    infile.read(mvc_2d, "name_to_read")
mf_2d = cpp.mesh.MeshFunctionSizet(mesh, mvc_2d)

n=FacetNormal(mesh)
mesh.num_cells()

I have tried to solve it by running the following line but it is not working:
pip install meshio lxml h5py

Thank you

First of all, please format all code with

```python
#add code here
```

Secondly, you would have to either supply the xdmf-file and the binary h5 file, a pure ascii xdmf file, or the original mesh file, and the functions that have been used to convert it to xdmf.

Also see:

I have tried the same mesh and code in other computer and it works. Could the error be related to the program version?
The code I’ve used to convert the mesh is the following:

from fenics import *
import meshio
path=" "
filename="Design"
msh=meshio.read(path + filename + ".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

tetra_mesh=create_mesh(msh, "tetra")
triangle_mesh=create_mesh(msh,"triangle")
meshio.write(path + filename + "_tetra.xdmf", tetra_mesh)

And my mesh is the following:

$MeshFormat
2.2 0 8
$EndMeshFormat
$PhysicalNames
4
2 13 “walls”
2 14 “inlet”
2 15 “outlet”
3 16 “volumen”
$EndPhysicalNames
$Nodes
14
1 0 0 0
2 1 0 0
3 1 1 0
4 0 1 0
5 0 0 1
6 1 0 1
7 1 1 1
8 0 1 1
9 0.5 0.5 1
10 1 0.5 0.5
11 0.5 0.5 0
12 0 0.5 0.5
13 0.5 1 0.5
14 0.5 0 0.5
$EndNodes
$Elements
48
1 2 2 13 1 5 9 6
2 2 2 13 1 8 9 5
3 2 2 13 1 6 9 7
4 2 2 13 1 7 9 8
5 2 2 15 2 2 10 3
6 2 2 15 2 6 10 2
7 2 2 15 2 3 10 7
8 2 2 15 2 7 10 6
9 2 2 13 3 2 11 1
10 2 2 13 3 1 11 4
11 2 2 13 3 3 11 2
12 2 2 13 3 4 11 3
13 2 2 14 4 4 12 1
14 2 2 14 4 1 12 5
15 2 2 14 4 8 12 4
16 2 2 14 4 5 12 8
17 2 2 13 5 4 13 3
18 2 2 13 5 3 13 7
19 2 2 13 5 8 13 4
20 2 2 13 5 7 13 8
21 2 2 13 6 1 14 2
22 2 2 13 6 5 14 1
23 2 2 13 6 2 14 6
24 2 2 13 6 6 14 5
25 4 2 16 1 10 11 14 13
26 4 2 16 1 9 14 12 13
27 4 2 16 1 14 9 10 13
28 4 2 16 1 11 12 14 13
29 4 2 16 1 12 5 1 14
30 4 2 16 1 1 11 4 12
31 4 2 16 1 1 11 14 2
32 4 2 16 1 8 5 12 9
33 4 2 16 1 5 14 9 6
34 4 2 16 1 9 6 10 7
35 4 2 16 1 10 6 14 2
36 4 2 16 1 4 11 3 13
37 4 2 16 1 4 8 12 13
38 4 2 16 1 9 8 7 13
39 4 2 16 1 10 7 3 13
40 4 2 16 1 3 10 11 2
41 4 2 16 1 12 11 4 13
42 4 2 16 1 3 11 10 13
43 4 2 16 1 1 11 12 14
44 4 2 16 1 14 12 5 9
45 4 2 16 1 14 9 6 10
46 4 2 16 1 9 12 8 13
47 4 2 16 1 7 10 9 13
48 4 2 16 1 10 14 11 2
$EndElements

Thank you

You are simply not saving the triangle mesh to an xdmf file, thus

cannot find the file.

Here is an MWE of working code (renaming the mesh file and removing your path variable):

from fenics import *
import meshio
filename="mesh"
msh=meshio.read(filename + ".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)
    points = mesh.points[:,:2] if prune_z else mesh.points
    out_mesh=meshio.Mesh(points=points, cells={
                         cell_type: cells}, cell_data={"name_to_read": [cell_data]})
    
    return out_mesh

tetra_mesh=create_mesh(msh, "tetra")
triangle_mesh=create_mesh(msh,"triangle")
meshio.write(filename + "_tetra.xdmf", tetra_mesh)
meshio.write(filename + "_triangle.xdmf", triangle_mesh)


mesh = Mesh()
with XDMFFile(filename + "_tetra.xdmf") as infile:
    infile.read(mesh)
mvc_3d = MeshValueCollection("size_t", mesh, 3) 
with XDMFFile(filename + "_tetra.xdmf") as infile:
    infile.read(mvc_3d, "name_to_read")
mf_3d = cpp.mesh.MeshFunctionSizet(mesh, mvc_3d)

mvc_2d = MeshValueCollection("size_t", mesh, 2)
with XDMFFile(filename + "_triangle.xdmf") as infile:
    infile.read(mvc_2d, "name_to_read")
mf_2d = cpp.mesh.MeshFunctionSizet(mesh, mvc_2d)

Than you very much, that was the problem. Now it’s working