Skip to content

Extracting the boundary of a 2D graph

Given a 2D finite element mesh, we wish to determine the boundary nodes and edges using simple graph operations.

Graph

Graph


Using networkx, the following code builds the graph and then extracts the boundary edges:

import networkx as nx
#Use a MultiGraph to allow parallel edges
G = nx.MultiGraph()
#Each element is a cycle
G.add_edges_from([(1,2),(2,6),(6,5),(5,1)])
G.add_edges_from([(2,3),(3,7),(7,6),(6,2)])
G.add_edges_from([(3,4),(4,8),(8,7),(7,3)])
G.add_edges_from([(5,6),(6,10),(10,9),(9,5)])
G.add_edges_from([(6,7),(7,11),(11,10),(10,6)])
G.add_edges_from([(7,8),(8,12),(12,11),(11,7)])
# Create a boundary graph
G_boundary = nx.Graph()
for v1,v2 in G.edges():
    '''
    G[a][b] will be a dict of length 1 if there is one edge and length 2 if
    an interior edge (two parallel edges for the 2D case)
    '''
    if len(G[v1][v2]) == 1:
            G_boundary.add_edge(v1,v2)
nx.dfs_preorder(G_boundary) # This will give us the directed boundary

yielding
boundary
To generalize to 3D, can we introduce the concept of faces?

Post a Comment

Your email is never published nor shared. Required fields are marked *