This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

How to get a node Neighbourhood?

+1 vote

I need to compute the Neighbourhood of all nodes in a mesh?

just that.

asked Apr 1, 2014 by ljofre FEniCS Novice (720 points)

1 Answer

+3 votes
 
Best answer

You could do it like this:

from dolfin import *
import numpy as np

mesh = UnitCubeMesh(4,4,4)

# Init vertex-edge connectivity
mesh.init(0,1)

for v in vertices(mesh):
    idx = v.index()
    neighborhood = [Edge(mesh, i).entities(0) for i in v.entities(1)]
    neighborhood = np.array(neighborhood).flatten()

    # Remove own index from neighborhood
    neighborhood = neighborhood[np.where(neighborhood != idx)[0]]

    print "Vertex %d neighborhood: " %idx, neighborhood
answered Apr 1, 2014 by Øyvind Evju FEniCS Expert (17,700 points)
selected Apr 1, 2014 by ljofre

but if I want only one node neighborhod?

in this way:

neighborhood(mesh, nodeId)

because I cant use this form

vertices(mesh)[nodeid]

thanks

v = Vertex(mesh, nodeId)
...