How to convert quadrilateral msh file to xdmf files in dolfinx?

Hello everyone, I obtained a quadrilateral mesh using Gmsh (trapezoid.msh), and then tried to convert it with the following code:

import meshio
import numpy as np
from mpi4py import MPI
from dolfinx.io import gmshio

proc = MPI.COMM_WORLD.rank
mesh, cell_markers, facet_markers = gmshio.read_from_msh("trapezoid.msh", MPI.COMM_WORLD, gdim=2)
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)
    points = mesh.points[:, :2] if prune_z else mesh.points
    out_mesh = meshio.Mesh(points=points, cells={cell_type: cells}, cell_data={"name_to_read": [cell_data.astype(np.int32)]})
    return out_mesh
if proc == 0:
    # Read in mesh
    msh = meshio.read("trapezoid.msh")
    # Create and save one file for the mesh, and one file for the facets
    quad_mesh = create_mesh(msh, "quadrilateral", prune_z=True)
    line_mesh = create_mesh(msh, "line", prune_z=True)
    meshio.write("mesh.xdmf", quad_mesh)
    meshio.write("mt.xdmf", line_mesh)
MPI.COMM_WORLD.barrier()

However, an error occurred:

Info    : Reading 'trapezoid.msh'...
Info    : 220463 nodes
Info    : 244288 elements
Info    : Done reading 'trapezoid.msh'

Traceback (most recent call last):
  File "/home/dyfluid/Desktop/dolfinx/main.py", line 19, in <module>
    quad_mesh = create_mesh(msh, "quadrilateral", prune_z=True)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/dyfluid/Desktop/dolfinx/main.py", line 10, in create_mesh
    cells = mesh.get_cells_type(cell_type)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/dyfluid/anaconda3/envs/fenicsx-env/lib/python3.11/site-packages/meshio/_mesh.py", line 245, in get_cells_type
    return np.empty((0, num_nodes_per_cell[cell_type]), dtype=int)
                        ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^
KeyError: 'quadrilateral'

and I am unsure how to handle it. I hope to get some help. Thank you.

This already reads in the mesh, it’s unclear to me why you would need the rest of the code, considering that dolfinx has XDMFFile to write the mesh and its mesh tags out.

Dear, are you saying that calculations can be performed without converting to an XDMF file? I’m just following the tutorial in the link(https://jsdokken.com/dolfinx-tutorial/chapter3/subdomains.html#) below and hoping to convert the quadrilateral mesh to XDMF as well. (I only know that XDMF is a suitable format for reading meshes, such as for visualization in ParaView.) Could you help me?

To address the error mentioned above, I changed the keyword from “quadrilateral” to “quad”, and it seems to be running now. However, I encountered an error related to the lack of “h5py”. I am using a conda environment and installed h5py with the following command:

pip3 install h5py

The code runs normally and generates mesh file. When I visualize the mesh using Paraview, the mesh looks normal, but why does the color bar show numerical values?
a4c871618a957c6ef6bcc5203169ca7

You can simply write the results here to xdmf with

with dolfinx.io.XDMFFile(mesh.comm, "filename.xdmf", "w") as xdmf:
    xdmf.write_mesh(mesh)
    xdmf.write_meshtags(cell_markers, mesh.geometry)
    xdmf.write_meshtags(facet_markers, mesh.geometry)

This is because you have marked your mesh in gmsh with some value. Without the mesh or context, it is hard to give any further guidance.

Thank you for providing the simpler method and helpful explanation. As shown in the figure, I did indeed make markings in Gmsh because it is necessary to determine the numbering of boundary conditions when writing the variational formula:

L = f * v * dx - g * v * ds(561)

Will these markings affect the calculation results? If they do not, I will ignore this issue.
38b4d481ea5cb5911d4060c61eab7eb

The markings should do what you expect them to do, i.e. restrict integrals to those facets marked with 561.

Thanks for your reply.