Write out parts of a mesh -- from xdmf file

I have a complicated mesh that comes with cell tags to mark the regions.

How can I write out a partial mesh for the separate regions for expection in, say, paraview? Preferably using the capabilities of dolfinx.io

Here is a screenshot of the mesh (already clipped to see some of the inner parts)

meshtags

And here is the code that I use to convert the mesh from .nas to .xdmf and then to read it into dolfinx.

import meshio
import dolfinx
from mpi4py import MPI

def create_mesh(mesh, cell_type, prune_z=False):
    cells = mesh.get_cells_type(cell_type)
    cell_data = mesh.get_cell_data("nastran:ref", cell_type)
    out_mesh = meshio.Mesh(points=mesh.points, cells={cell_type: cells},
                           cell_data={"Region": [cell_data]})
    return out_mesh

msh = meshio.read("FuMO_mesh.nas")
tetra_mesh = create_mesh(msh, "tetra", True)
meshio.write("tetra_mesh.xdmf", tetra_mesh)
print('saved tetra mesh')

filename = 'tetra_mesh.xdmf'

with dolfinx.io.XDMFFile(MPI.COMM_WORLD, filename, "r") as xdmf:
    mesh = xdmf.read_mesh(name="Grid")
    cell_tags = xdmf.read_meshtags(mesh, name="Grid")
1 Like

Just found out that the paraview’s “extract cells that satisfy a threshold criterion” does the job – however, in a rather manual way. Also, the submeshes will be quite helpful for me for testing.

dolfinx.mesh.create_submesh should do that job.