Error reading a 3D cylindrical mesh imported from Gmsh

I created a 3D cylindrical mesh in Gmsh (Version 4.11.1). I exported it as “Version 4 ASCII”. I did not check the “Save all elements” and “Save parametric coordinates” options while exporting. I usually export all my 3D meshes like that and all of them worked good so far.

I created the cylindrical mesh by extruding an annulus as given in the following Gmsh script:

// Gmsh project created on Mon Feb 17 01:33:47 2025
SetFactory("OpenCASCADE");
//+
Point(1) = {0.0, 0.0, 0.0, 1.0};
//+
Point(2) = {0.0, 0.5, 0.0, 1.0};
//+
Point(3) = {0.0, -0.5, 0.0, 1.0};
//+
Point(4) = {0.0, 0.0, 0.5, 1.0};
//+
Point(5) = {0.0, 0.0, -0.5, 1.0};
//+
Point(6) = {0.0, 1.0, 0.0, 1.0};
//+
Point(7) = {0.0, -1.0, 0.0, 1.0};
//+
Point(8) = {0.0, 0.0, 1.0, 1.0};
//+
Point(9) = {0.0, 0.0, -1.0, 1.0};
//+
Circle(1) = {3, 1, 4};
//+
Circle(2) = {4, 1, 2};
//+
Circle(3) = {2, 1, 5};
//+
Circle(4) = {5, 1, 3};
//+
Circle(5) = {7, 1, 8};
//+
Circle(6) = {8, 1, 6};
//+
Circle(7) = {6, 1, 9};
//+
Circle(8) = {9, 1, 7};
//+
Curve Loop(1) = {6, 7, 8, 5};
//+
Curve Loop(2) = {2, 3, 4, 1};
//+
Plane Surface(1) = {1, 2};
//+
Extrude {10, 0, 0} {
  Curve{7}; Curve{6}; Curve{5}; Curve{8}; Curve{4}; Curve{1}; Curve{2}; Curve{3}; Surface{1};
}
//+
Physical Curve("leftinner", 1) = {2, 1, 4, 3};
//+
Physical Curve("leftouter", 2) = {8, 5, 6, 7};
//+
Physical Curve("rightouter", 4) = {15, 16, 11, 13};
//+
Physical Curve("rightinner", 3) = {21, 19, 24, 23};
//+
Physical Surface("rightcellsurface", 5) = {18};
//+
Physical Surface("leftcellsurface", 6) = {1};
//+
Physical Surface("outercellsurface", 8) = {3, 2, 5, 4};
//+
Physical Surface("innercellsurface", 7) = {8, 7, 6, 9};
//+
Physical Volume("cellvolume", 9) = {1};

I imported it to FEniCS (legacy version and DOLFIN version: 2019.2.0.13.dev0), to calculate area, volume and length of curves, as follows:

from dolfin import *     
import meshio


# Optimization options for the form compiler
parameters["form_compiler"]["cpp_optimize"] = True
ffc_options = {"optimize": True, \
               "eliminate_zeros": True, \
               "precompute_basis_const": True, \
               "precompute_ip_const": True}

    
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]})    
    return out_mesh

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

tetra_mesh = create_mesh(msh, "tetra", True)
triangle_mesh = create_mesh(msh, "triangle", True)
line_mesh = create_mesh(msh, "line", True)
meshio.write("mesh.xdmf", tetra_mesh)
meshio.write("surface.xdmf", triangle_mesh)
meshio.write("mf.xdmf", line_mesh) 

mesh = Mesh()
xdmf = XDMFFile(mesh.mpi_comm(),"mesh.xdmf")
xdmf.read(mesh)
mvc = MeshValueCollection("size_t", mesh, mesh.topology().dim())
with XDMFFile("mesh.xdmf") as infile:
   infile.read(mvc, "name_to_read")
cf = cpp.mesh.MeshFunctionSizet(mesh, mvc)
xdmf.close()

mvc = MeshValueCollection("size_t", mesh, mesh.topology().dim()-1)
with XDMFFile("surface.xdmf") as infile:
   infile.read(mvc, "name_to_read")                               # This line gives the error
sf = cpp.mesh.MeshFunctionSizet(mesh, mvc)
xdmf.close()

mvc = MeshValueCollection("size_t", mesh, mesh.topology().dim()-2)
with XDMFFile("mf.xdmf") as infile:
    infile.read(mvc, "name_to_read")
mf = cpp.mesh.MeshFunctionSizet(mesh, mvc)

def assemble_edge(tag):
    mv = MeshView.create(mf, tag)
    return assemble(Constant(1.0)*dx(domain=mv))   # Line measure


dx_custom = Measure("ds", domain=mesh, subdomain_data=sf)  # Area measure
dv_custom = Measure("dx", domain=mesh, subdomain_data=cf)  # Volume measure


print(f"Volume = {assemble(Constant(1.0)*dv_custom(9))}")            
print(f"leftouterboundary = {assemble_edge(2)}")    
print(f"leftinnerboundary = {assemble_edge(1)}")       
print(f"leftcellsurface = {assemble(Constant(1.0)*dx_custom(6))}")      
print(f"rightouterboundary = {assemble_edge(4)}")     
print(f"rightinnerboundary = {assemble_edge(3)}")        
print(f"rightcellsurface = {assemble(Constant(1.0)*dx_custom(5))}")     
print(f"outercellsurface = {assemble(Constant(1.0)*dx_custom(8))}")       
print(f"innercellsurface = {assemble(Constant(1.0)*dx_custom(7))}")    

This generates the error

line 39, in <module>
    infile.read(mvc, "name_to_read")                               # This line gives the error
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
***
***     https://fenicsproject.discourse.group/
***
*** Remember to include the error message listed below and, if possible,
*** include a *minimal* running example to reproduce the error.
***
*** -------------------------------------------------------------------------
*** Error:   Unable to find entity in map.
*** Reason:  Error reading MeshValueCollection.
*** Where:   This error was encountered inside HDF5File.cpp.
*** Process: 0
***
*** DOLFIN version: 2019.2.0.13.dev0
*** Git changeset:  ubuntu
*** -------------------------------------------------------------------------

I have previously imported and read 3D meshes successfully using the same code. I think the problem comes from extruding the surfaces and lines. I checked some of the solutions in this forum and tried some of them. But none of them worked.

I am not sure if I am creating the mesh wrong or am I importing it incorrectly.

Please advise.

Thanks in advance!

The issue is that Extrude does something weird with the vertex numbering.

Consider the following code:

import meshio
import numpy as np

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


tetra_cells = mesh.get_cells_type("tetra")
points = mesh.points

triangle_cells = mesh.get_cells_type("triangle")
tetra_indices = np.unique(tetra_cells.flatten())
triangle_indices = np.unique(triangle_cells.flatten())
print(tetra_indices)
print(triangle_indices)

which loads your mesh after it has been generated with gmsh -3 mesh.geo.
It yields:

[  0   1   2   3   4   5   6   7  16  17  18  19  20  21  22  23  24  25
  26  27  28  29  30  31 112 113 114 115 116 117 118 119 120 121 122 123
 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
 178 179 180 181 182 183 184 185 186 187 188 189 190 191 272 273 274 275
 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
 348 349 350 351]
[  0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17
  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35
  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53
  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71
  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89
  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107
 108 109 110 111 130 140 150 151 170 180 190 191 192 193 194 195 196 197
 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
 270 271]

Meaning that there are several nodes in your mesh.points that are not part of your mesh. (i.e. the first list is not contiguous).

Secondly, there are vertices in the facets that are not part of the 3D mesh, as 7,8,9,10,…15 is not part of the tetrahedral mesh.

@dokken Thanks for the explanation. That makes sense.
So how do I import and read it in FEniCS then? What changes do I need to make to the code that I posted in my original post?

This is an issue with how you use GMSH (i never use the geo files).
For instance, with your mesh, when you call mesh 3D, you get the volume and surfaces:


As you can see, they are not conforming to eachother.
This means that you have tagged the wrong surfaces within gmsh.

You can for instance check this by only adding a single physical surface, say

Physical Surface("faces", 10) = {1};

and then run

import meshio
import numpy as np

mesh = meshio.read("mwe_gmsh.msh")

tetra_cells = mesh.get_cells_type("tetra")

triangle_cells = mesh.get_cells_type("triangle")
tetra_indices = np.unique(tetra_cells.flatten())
triangle_indices = np.unique(triangle_cells.flatten())
print(np.isin(triangle_indices, tetra_indices).all())

You then see that all triangles are represented in your mesh.

However, if you change it to:

Physical Surface("faces", 10) = {1,2};

you will get a “False” as output, meaning that this surfaces is not in the mesh.
As you can see by inspecting the mesh, only the following surfaces are actually part of the mesh


and therefore you can add individual groups for:

Physical Surface("faces", 10) = {1,10,11,12,13,14,15,16,17,18};

I’ve just added a single one to illustrate the point.

Please note that this is related to how gmsh operates, and questions as to why gmsh has duplicate surfaces, my answer is I do not know.

1 Like

@dokken Thanks. That works.