Meshio does not read Gmsh physical groups

Thank you @bhaveshshrimali for your detailed response. My ultimate goal is to use gmsh API/pygmsh to generate the mesh, and solve in FEniCS. Since I have little to no knowledge of any of them, I had to divide them and learn step by step.

As to your suggestions

  1. I tried the geo file. Your geo file opens OK in my Gmsh GUI 4.8.0. However I meshed the model, and exported to .msh. The same problems remain when I ran my python code: Meshio.read() can read ver2 msh file, but does not recognize physical groups; ver4 msh file cannot be properly read by Meshio.

  2. I also tried to run your python/pygmsh code. I had to install pygmsh and gmsh to the FEniCS container following this thread (Problem with installation of latest gmsh 4.6.0 via pip - #3 by Nexius). I could import meshio and pygmsh in container’s bash, but not directly in jupyter notebook (Jupyter did not find module of pygmsh). I did the following in Jupyter to be able to import gmsh and pygmsh. However there are errors about opencascade, see below.

import sys
sys.path.append('/usr/local/gmsh-4.6.0-Linux64-sdk/lib')    # have to add path manually to import gmsh
sys.path.append('/usr/local/gmsh-4.6.0-Linux64-sdk/bin')
import meshio
import gmsh
import pygmsh, os
import numpy as np

#geom = pygmsh.opencascade.Geometry()     # pygmsh has no opencascade attribute
geom = pygmsh.geo.Geometry()                      # pygmsh.geo.Geometry() works
b1 = geom.add_box([0.0, 0.0, 0.0], [1.0, 1.0, 1.0])     # error: add_box() missing 4 required positional arguments: 'y0', 'y1', 'z0', and 'z1'

It seems pygmsh (v7.1.8) has no .opencascade, but .geo attribute. ‘add_box’ needs 4 arguments. I have not yet started studying pygmsh/gmsh API but your code provides me a good point to start with.

At this point I will try to stick to 1), i.e. generate a mesh from Gmsh GUI with physical groups, and then use my python code to read (via meshio) and export to separate xdmf files. Any insights?