Error importing from GMSH

Hi everyone,

I’m trying to import a 3D beam mesh I created using GMSH following Dokken’s tutorial.

However, when I run this code:

from dolfinx.io import gmshio
from dolfinx.fem.petsc import LinearProblem
from mpi4py import MPI

gmsh_model_rank = 0
mesh_comm = MPI.COMM_WORLD
#mesh = gmshio.read_from_msh('MeshingGMSH/MyTest2.msh',mesh_comm, gdim=3)
domain, cell_markers, facet_markers = gmshio.model_to_mesh('MeshingGMSH/MyTest2.msh', mesh_comm, gmsh_model_rank, gdim=3)
print(facet_markers)

I get the following error:
AttributeError: 'str' object has no attribute 'mesh'

Upon inspecting the .msh file, it seems that there is, in fact, a mesh.

Any advice on this?

Thanks in advance!

Model to mesh takes in a GMSH model object, not a file name.

Read from msh takes in a msh file name.

Hi Dokken,

Thanks for the swift reply.

Upon using read_from_mesh I can now access the mesh, but I get a segmentation fault that causes the program to crash:

from dolfinx.io import gmshio
from dolfinx.fem.petsc import LinearProblem
from mpi4py import MPI

gmsh_model_rank = 0
mesh_comm = MPI.COMM_WORLD
mesh, cell_tags, facet_tags = gmshio.read_from_msh('MeshingGMSH/MyTest2.msh',mesh_comm, gdim=3)
Info    : Reading 'MeshingGMSH/MyTest2.msh'...
Info    : 45 entities
Info    : 156 nodes
Info    : 876 elements
Info    : Done reading 'MeshingGMSH/MyTest2.msh'
[1]    6412 segmentation fault (core dumped)

What could be causing this?

Thanks in advance!

Without having the mesh file, i cannot help you further

Hi Dokken,

Please find attached the .geo file that was used to generate the mesh using gmsh MyTest2.geo -3 -save_all

// Im going to define a beam, mesh it and then insert a new surface in the middle, following Tutorials 1 and 15 of the GMSH tutorial page

l = 5;
w = 1;
h = 1;
lc = 0.5;

// POINTS
Point(1) = {0,0,0,lc};
Point(2) = {0,0,l,lc};
Point(3) = {w,0,0,lc};
//Point(4) = {0,h,0,lc};
//Point(5) = {w,h,0,lc};
//Point(6) = {0,h,l,lc};
Point(7) = {w,0,l,lc};
//Point(8) = {w,h,l,lc};
Point(9) = {0,0.5,l,lc}; //Point for inserting
Point(10) = {w,0.5,l,lc}; //Point for inserting
Point(11) = {0,0.5,0,lc}; //Point for inserting
Point(12) = {w,0.5,0,lc}; //Point for inserting


// LINES
Line(1) = {1,11};
Line(2) = {1,2};
Line(3) = {2,9};
Line(4) = {11,9};
Line(5) = {9,10};
Line(6) = {11,12};
// Im going to define a beam, mesh it and then insert a new surface in the middle, following Tutorials 1 and 15 of the GMSH tutorial page

l = 5;
w = 1;
h = 1;
lc = 0.5;

// POINTS
Point(1) = {0,0,0,lc};
Point(2) = {0,0,l,lc};
Point(3) = {w,0,0,lc};
//Point(4) = {0,h,0,lc};
//Point(5) = {w,h,0,lc};
//Point(6) = {0,h,l,lc};
Point(7) = {w,0,l,lc};
//Point(8) = {w,h,l,lc};
Point(9) = {0,0.5,l,lc}; //Point for inserting
Point(10) = {w,0.5,l,lc}; //Point for inserting
Point(11) = {0,0.5,0,lc}; //Point for inserting
Point(12) = {w,0.5,0,lc}; //Point for inserting


// LINES
Line(1) = {1,11};
Line(2) = {1,2};
Line(3) = {2,9};
Line(4) = {11,9};
Line(5) = {9,10};
Line(6) = {11,12};
Line(7) = {12,10};
Line(8) = {12,3};
Line(9) = {10,7};
Line(10) = {7,2};
Line(11) = {3,1};
Line(12) = {3,7};



//LOOPS
Curve Loop(1) = {2,3,-4,-1};
Curve Loop(2) = {3,5,9,10};
Curve Loop(3) = {1,6,8,11};
Curve Loop(4) = {12,-9,-7,8};
Curve Loop(5) = {6,7,-5,-4};
Curve Loop(6) = {-11,12,10,-2};




// SURFACES
Plane Surface(1) = {1};
Plane Surface(2) = {2};
Plane Surface(3) = {3};
Plane Surface(4) = {4};
Plane Surface(5) = {5};
Plane Surface(6) = {6};

Physical Surface('Internal Surface') = {5};
Physical Surface('Left') = {3};



// SURFACE LOOP
Surface Loop(1) = {1,2,5,6,3,4};
Volume(1) = {1};
Physical Volume('Volume') = {1};

Extrude {0,0.5,0} { Surface{5}; }
Line(7) = {12,10};
Line(8) = {12,3};
Line(9) = {10,7};
Line(10) = {7,2};
Line(11) = {3,1};
Line(12) = {3,7};



//LOOPS
Curve Loop(1) = {2,3,-4,-1};
Curve Loop(2) = {3,5,9,10};
Curve Loop(3) = {1,6,8,11};
Curve Loop(4) = {12,-9,-7,8};
Curve Loop(5) = {6,7,-5,-4};
Curve Loop(6) = {-11,12,10,-2};




// SURFACES
Plane Surface(1) = {1};
Plane Surface(2) = {2};
Plane Surface(3) = {3};
Plane Surface(4) = {4};
Plane Surface(5) = {5};
Plane Surface(6) = {6};

Physical Surface('Internal Surface') = {5};
Physical Surface('Left') = {3};



// SURFACE LOOP
Surface Loop(1) = {1,2,5,6,3,4};
Volume(1) = {1};
Physical Volume('Volume') = {1};

Extrude {0,0.5,0} { Surface{5}; } 

Thanks again for your help :slight_smile:

Please remove -save_all from the call. This causes major issues for gmsh when trying to read in the mesh to DOLFINx, as you would read in nodes that are not part of the mesh.

1 Like

Hi Dokken,

Now it works like a charm!

Thank you