Transfer meshtags to submesh in dolfinx

And here is a similar version transfering facet tags from a parent mesh to a submesh of the same co-dimension:

from IPython import embed
import dolfinx
import ufl
from mpi4py import MPI
import numpy as np


def facets(x):
    return np.isclose(x[0], 0)


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

left_facets = dolfinx.mesh.locate_entities_boundary(mesh, fdim, facets)


def some_facets(x):
    return (x[1] <= 0.51) & np.isclose(x[0], 0)


def some_cells(x):
    return (x[0] <= 0.51)


some_marked_facets = dolfinx.mesh.locate_entities_boundary(
    mesh, fdim, some_facets)
values = np.full_like(some_marked_facets, 1, dtype=np.int32)

facet_tags = dolfinx.mesh.meshtags(mesh, fdim, some_marked_facets, values)
c_to_f = mesh.topology.connectivity(tdim, fdim)
with dolfinx.io.XDMFFile(mesh.comm, "facet.xdmf", "w") as xdmf:
    xdmf.write_mesh(mesh)
    xdmf.write_meshtags(facet_tags)


f_map = mesh.topology.index_map(fdim)
all_facets = f_map.size_local + f_map.num_ghosts
# Create array with zeros for all facets that are not marked
all_values = np.zeros(all_facets, dtype=np.int32)
all_values[facet_tags.indices] = facet_tags.values


submesh, entity_map, _, _ = dolfinx.mesh.create_submesh(
    mesh, tdim, dolfinx.mesh.locate_entities(mesh, mesh.topology.dim, some_cells))

submesh.topology.create_entities(fdim)
subf_map = submesh.topology.index_map(fdim)
submesh.topology.create_connectivity(tdim, fdim)
c_to_f_sub = submesh.topology.connectivity(tdim, fdim)
num_sub_facets = subf_map.size_local + subf_map.size_global
sub_values = np.empty(num_sub_facets, dtype=np.int32)
for i, entity in enumerate(entity_map):
    parent_facets = c_to_f.links(entity)
    child_facets = c_to_f_sub.links(i)
    for child, parent in zip(child_facets, parent_facets):
        sub_values[child] = all_values[parent]
sub_meshtag = dolfinx.mesh.meshtags(submesh, submesh.topology.dim-1, np.arange(
    num_sub_facets, dtype=np.int32), sub_values)

with dolfinx.io.XDMFFile(mesh.comm, "sub_tag.xdmf", "w") as xdmf:
    xdmf.write_mesh(submesh)
    submesh.topology.create_connectivity(fdim, tdim)
    xdmf.write_meshtags(sub_meshtag)

1 Like