Read meshtag from xdmf created by <mesh generation with gmsh>

Hello, my dolfinx version is 0.8.0

import dolfinx

print(dolfinx.__version__)

I use gmsh to create a 2D model. And save the model in xdmf file. But the facets_tags can’t be read. Here is a example based on dolfinx 0.8.0 demo ‘Mesh generation with gmsh’

from mpi4py import MPI

# +
from dolfinx.io import XDMFFile, gmshio

try:
    import gmsh  # type: ignore
except ImportError:
    import sys

    print("This demo requires gmsh to be installed")
    sys.exit(0)
# -

# ##  Gmsh model builders
#
# The following functions add Gmsh meshes to a 'model'.

# +


def gmsh_sphere(model: gmsh.model, name: str) -> gmsh.model:
    """Create a Gmsh model of a sphere.

    Args:
        model: Gmsh model to add the mesh to.
        name: Name (identifier) of the mesh to add.

    Returns:
        Gmsh model with a sphere mesh added.

    """
    model.add(name)
    model.setCurrent(name)
    sphere = model.occ.addSphere(0, 0, 0, 1, tag=1)

    # Synchronize OpenCascade representation with gmsh model
    model.occ.synchronize()

    # Add physical marker for cells. It is important to call this
    # function after OpenCascade synchronization
    model.add_physical_group(dim=3, tags=[sphere])

    # Generate the mesh
    model.mesh.generate(dim=3)
    return model


# +


def create_mesh(comm: MPI.Comm, model: gmsh.model, name: str, filename: str, mode: str):
    """Create a DOLFINx from a Gmsh model and output to file.

    Args:
        comm: MPI communicator top create the mesh on.
        model: Gmsh model.
        name: Name (identifier) of the mesh to add.
        filename: XDMF filename.
        mode: XDMF file mode. "w" (write) or "a" (append).

    """
    msh, ct, ft = gmshio.model_to_mesh(model, comm, rank=0)
    msh.name = name
    ct.name = f"{msh.name}_cells"
    ft.name = f"{msh.name}_facets"
    with XDMFFile(msh.comm, filename, mode) as file:
        msh.topology.create_connectivity(2, 3)
        file.write_mesh(msh)
        file.write_meshtags(
            ct, msh.geometry, geometry_xpath=f"/Xdmf/Domain/Grid[@Name='{msh.name}']/Geometry"
        )
        file.write_meshtags(
            ft, msh.geometry, geometry_xpath=f"/Xdmf/Domain/Grid[@Name='{msh.name}']/Geometry"
        )

gmsh.initialize()
gmsh.option.setNumber("General.Terminal", 0)

# Create model
model = gmsh.model()

model = gmsh_sphere(model, "Sphere")
model.setCurrent("Sphere")
create_mesh(MPI.COMM_SELF, model, "sphere", f"out_gmsh/mesh_text.xdmf", "w")


from dolfinx import io

with io.XDMFFile(MPI.COMM_WORLD, "out_gmsh/mesh_text.xdmf", "r") as file:
    msh2 = file.read_mesh(name="sphere")
    msh2_cell_tag = file.read_meshtags(msh2, name='sphere_cells')
    msh2_facet_tag = file.read_meshtags(msh2, name='sphere_facets')

and the result is

Traceback (most recent call last):
  File "/usr/lib/python3.10/code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "/home/jtr/pycharm-2022.2.4/plugins/python/helpers/pydev/_pydev_bundle/pydev_umd.py", line 198, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "/home/jtr/pycharm-2022.2.4/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/home/jtr/fenicsdemo/fenicsx/gmsh/text.py", line 130, in <module>
    msh2_facet_tag = file.read_meshtags(msh2, name='sphere_facets')
  File "/usr/lib/petscdir/petsc-complex/lib/python3/dist-packages/dolfinx/io/utils.py", line 280, in read_meshtags
    mt = super().read_meshtags(mesh._cpp_object, name, xpath)
RuntimeError: Mesh entities of dimension 2have not been created.

You need a msh2.topology.create_connectivity(msh2.topology.dim - 1, msh2.topology.dim) before the final line in the code.

Yes, it’s looks like working well in the example.
Thank you , I will try this way in my own code.