Search code examples
pythonmeshgmshmeshio

Generating triangular elements with gmsh and read with python


I'm trying to generate a triangular mesh with gmsh and then read it with python, for the python part I'm using meshio. But my problem is that the mesh elements are always readead as lines, not triangles.

Here is my .geo

SetFactory("OpenCASCADE");
Circle(1) = {0, 0, 0, 1, 0, 2*Pi};
Curve Loop(1) = {1};
Plane Surface(1) = {1};
Physical Curve("circle", 2) = {1};

then I run:

gmsh -2 circle.geo

and get this mesh:

enter image description here

With this python code I read the mesh:

ELEMENT_TYPES = {
    "line": 2,
    "line3": 3,
    "triangle": 3,
    "quadrangle": 4,
    "tetrahedron": 4,
    "hexahedron": 8,
    "prism": 6,
    "pyramid": 5,
}

def read(self, mesh_file: str):
    msh = meshio.read(mesh_file)

    points = msh.points
    number_of_nodes = points.shape[0]
    point_index = range(number_of_nodes)
    mesh_nodes = dict(zip(point_index, points))

    elements = []
    for cell in msh.cells:
        cell_type = cell.type
        for idx, element_nodes in enumerate(cell.data, 1):

            element_nodes_coordinates = np.array([mesh_nodes[node] for node in element_nodes])

            element = Element(
                index=idx,
                type=cell_type,
                size=ELEMENT_TYPES[cell_type],
                nodes=element_nodes,
                nodes_coordinates=element_nodes_coordinates,
            )

            elements.append(element)

But I always get a mesh type of line (2 node element), I need a 3 node triangular mesh.


Solution

  • I'm pretty sure it's because you only added a "physical group" for the circle which is a 1D curve (not 2D disk), so you need to add the surface to a physical group as well or gmsh won't save the triangles in its output. Add this line and I think it'll work: Physical Surface("My surface") = {1};