Tag one entity twice in a mesh

Hello,
Given a square mesh in two dimensions, I can successfully generate it assign a tag (say, 1) to its left edge (A). Now I want to tag also the whole boundary (left, right, top and bottom edges) (B) with a different tag (say 2).

Is it possible to do this and export the mesh to XDMF file? I have read that XDMF format does not allow for the same entity to appear in two separate tagged objects, A and B in this case.

Of course, I can provide a minimal working example if needed.

Thank you

The follow example shows how you would do this, by creating to separate meshtags:

from mpi4py import MPI
import numpy as np
import dolfinx


mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 10, 10)
tdim = mesh.topology.dim
mesh.topology.create_connectivity(tdim, tdim - 1)
mesh.topology.create_connectivity(tdim - 1, tdim)


num_facets_local = mesh.topology.index_map(tdim - 1).size_local + mesh.topology.index_map(tdim - 1).num_ghosts
values = np.full(num_facets_local, -1, dtype=np.int32)
values[dolfinx.mesh.exterior_facet_indices(mesh.topology)] = 1000
idx = np.flatnonzero(values != -1)
ft0 = dolfinx.mesh.meshtags(mesh, tdim - 1, idx, values[idx])
ft0.name = "exterior_facets"
values2 = np.full(num_facets_local, -1, dtype=np.int32)
values2[dolfinx.mesh.locate_entities_boundary(mesh, tdim - 1, lambda x: np.isclose(x[0], 0.0))] = 2000
idx2 = np.flatnonzero(values2 != -1)
ft1 = dolfinx.mesh.meshtags(mesh, tdim - 1, idx2, values2[idx2])
ft1.name = "left_boundary_facets"
with dolfinx.io.XDMFFile(MPI.COMM_WORLD, "mwe.xdmf", "w") as xdmf:
    xdmf.write_mesh(mesh)
    xdmf.write_meshtags(ft0, mesh.geometry)
    xdmf.write_meshtags(ft1, mesh.geometry)



with dolfinx.io.XDMFFile(MPI.COMM_WORLD, "mwe.xdmf", "r") as xdmf:
    mesh2 = xdmf.read_mesh()
    mesh2.topology.create_connectivity(tdim, tdim - 1)
    ft0_2 = xdmf.read_meshtags(mesh2, name="left_boundary_facets")
    ft1_2 = xdmf.read_meshtags(mesh2, name="exterior_facets")

    print(ft0_2.values,
          ft1_2.values)

Quite sure you’ll need to create and export two different meshtag objects. A meshtag object is little more than one array of facet id’s and one array of associated tags (and some logic and functionaliy wrapped around).

EDIT: Of course Jorgen beat me to it and supplied a nice MWE :wink:

1 Like