Unable to read .msh mesh file which contains only rectangular elements

By running gmsh -2 on this file and then opening the resulting msh file in gmsh, you will observe that there are no volume elements in the mesh.
With the following modifications:

SetFactory("OpenCASCADE");
//+
Point(1) = {0, -0.1, 0, 1.0};
//+
Point(2) = {0, 1, 0, 1.0};
//+
Point(3) = {2, 1, 0, 1.0};
//+
Point(4) = {2, 0, 0, 1.0};
//+
Recursive Delete {
  Point{1}; 
}
//+
Point(5) = {0, 0, 0, 1.0};
//+
Line(1) = {2, 5};
//+
Line(2) = {5, 4};
//+
Line(3) = {4, 3};
//+
Line(4) = {3, 2};
//+
Curve Loop(1) = {4, 1, 2, 3};
//+
Plane Surface(1) = {1};
//+
Transfinite Surface {1} = {2, 3, 4, 5};
//+
Transfinite Curve {1, 3} = 20 Using Progression 1;
//+
Transfinite Curve {4, 2} = 40 Using Progression 1;
//+
Recombine Surface {1};
//+
Physical Curve("inlet", 5) = {1};
//+
Physical Curve("outlet", 6) = {3};
//+
Physical Curve("top", 7) = {4};
//+
Physical Curve("bottom", 8) = {2};
//+
Physical Surface(9) = {1};

you can convert your mesh with the following commands

import numpy
import meshio
mesh_from_file = meshio.read("transf_gmsh.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


line_mesh = create_mesh(mesh_from_file, "quad", prune_z=True)
meshio.write("mesh_quad.xdmf", line_mesh)
line_mesh = create_mesh(mesh_from_file, "line", prune_z=True)
meshio.write("facet_mesh.xdmf", line_mesh)

but as I said, you should use FEniCSx if you want to work with quad meshes

2 Likes