I try to mark all the overlapped region from two meshes with different resolutions. Here I use collision related functions in dolfinx.geometry
and meshtag
, but the number of overlapped elements is much less than the expected. The following is the code and screenshot with DOLFINx v0.6.0:
from mpi4py import MPI
import ufl
import numpy as np
from dolfinx import fem, io, mesh, geometry
def mark_cells(msh, cell_index):
num_cells = msh.topology.index_map(
msh.topology.dim).size_local + msh.topology.index_map(
msh.topology.dim).num_ghosts
cells = np.arange(0, num_cells)
values = np.full(cells.shape, 0, dtype=np.int32)
values[cell_index] = np.full(len(cell_index), 1, dtype=np.int32)
cell_tag = mesh.meshtags(msh, msh.topology.dim, cells, values)
return cell_tag
mesh_big = mesh.create_unit_square(MPI.COMM_WORLD, 64, 64)
mesh_big.geometry.x[:, :2] -= 0.51
mesh_big.geometry.x[:, :2] *= 4
mesh_small = mesh.create_unit_square(MPI.COMM_WORLD, 1, 1)
mesh_small.geometry.x[:, :2] -= 0.5
bb_tree = geometry.BoundingBoxTree(mesh_big, mesh_big.topology.dim)
cell_candidates = geometry.compute_collisions(bb_tree, mesh_small.geometry.x)
colliding_cells = geometry.compute_colliding_cells(mesh_big, cell_candidates, mesh_small.geometry.x)
cell_indices = []
for i, point in enumerate(mesh_small.geometry.x):
if len(colliding_cells.links(i))>0:
cell_indices.append(colliding_cells.links(i)[0])
cell_tags = mark_cells(mesh_big, cell_indices)
with io.XDMFFile(MPI.COMM_WORLD, "cell_tags.xdmf", "w") as file:
file.write_mesh(mesh_big)
file.write_meshtags(cell_tags)
with io.XDMFFile(MPI.COMM_WORLD, "mesh_small.xdmf", "w") as file:
file.write_mesh(mesh_small)
# Validation with the area of the overlapped region
dxs = ufl.Measure("dx", domain=mesh_big, subdomain_data=cell_tags)
f = 1*dxs(1)
area = MPI.COMM_WORLD.allreduce(
fem.assemble_scalar(fem.form(f)), op=MPI.SUM)
print("Area:", area)