Is it possible to find the adjacent DOFs connected by edges?
In my case, I have a marked domain with a Function
of 0 and 1 (Fig a). I try to find DOFs on the exterior layer of a marked region (Fig b). I pick all those points by looping all DOFs with 0 and at least 1 adjacent DOF with 1.
To do so, I made a specific case where P1
element is used. Dim 0 vertex and Dim 1 edge connectivity are created, and I examine all connected vertices based on the current vertex. This would be insufficient in other cases, e.g. high order element and mixed element. Any advice for improvement?
MWE:
from mpi4py import MPI
from dolfinx import fem, mesh
import numpy as np
def mark(x):
dist = np.sqrt((x[0] - 0.5)**2 + (x[1] - 0.5)**2)
values = np.ones_like(dist, dtype=np.int32)
values[dist <= 0.25] = 0
return values
comm = MPI.COMM_WORLD
_mesh = mesh.create_unit_square(comm, 64, 64, mesh.CellType.quadrilateral)
V = fem.functionspace(_mesh, ("CG", 1))
f = fem.Function(V)
f.interpolate(mark)
geometry = _mesh.geometry
topology = _mesh.topology
num_vertices = topology.index_map(0).size_local + topology.index_map(0).num_ghosts
vertices = []
topology.create_entities(1)
topology.create_connectivity(0, 1)
vertex2edge = topology.connectivity(0, 1)
edge2vertex = topology.connectivity(1, 0)
for vertex in range(num_vertices):
adjacent_vertices = []
edges = vertex2edge.links(vertex)
for edge in edges:
found_vertices = edge2vertex.links(edge)
adjacent_vertices.append(list(found_vertices))
adjacent_vertices = np.unique(adjacent_vertices)
adjacent_vertices_excluded = np.setdiff1d(adjacent_vertices, vertex)
if (f.x.array[vertex] == 0) and np.any(f.x.array[adjacent_vertices_excluded] == 1):
vertices.append(vertex)
# Validation
import matplotlib.pyplot as plt
plt.figure()
for vertex in vertices:
x, y, z = geometry.x[vertex, :]
plt.plot(x, y, 'bo')
plt.gca().set_aspect(1)