# How to access neighbor nodes and elements (fenicsx)

**URL:** <https://fenicsproject.discourse.group/t/how-to-access-neighbor-nodes-and-elements-fenicsx/10104>\
**Category:** General\
**Created:** [January 6, 2023, 1:57pm UTC](https://fenicsproject.discourse.group/t/how-to-access-neighbor-nodes-and-elements-fenicsx/10104 "2023-01-06T13:57:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hamedZaker](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/hamedzaker/32/4563_2.png) [@hamedZaker](https://fenicsproject.discourse.group/u/hamedZaker)\
**Post date:** [January 6, 2023, 1:57pm UTC](https://fenicsproject.discourse.group/t/how-to-access-neighbor-nodes-and-elements-fenicsx/10104/1 "2023-01-06T13:57:42Z")

</div>

I saw some old questions on how to access neighbors but they don’t seem working for fenicsx. I simply want to see the indices of adjacent vertices around one particular node. Is there a way to do it?

This is one example which does not work for if I change from dolfin to dolfinx (mainly because of vetices command whose equivalent I did not find for fenicsx) (found here: [https://fenicsproject.org/qa/3145/how-to-get-a-node-neighbourhood/](https://fenicsproject.org/qa/3145/how-to-get-a-node-neighbourhood/))

```auto
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

```

Thank you

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [January 6, 2023, 2:10pm UTC](https://fenicsproject.discourse.group/t/how-to-access-neighbor-nodes-and-elements-fenicsx/10104/2 "2023-01-06T14:10:25Z")

</div>

Your question is quite vague, please follow the guidelines of [Read before posting: How do I get my question answered?](https://fenicsproject.discourse.group/t/read-before-posting-how-do-i-get-my-question-answered/21)  
Please also refer to the previous posts,

> [@hamedZaker](#):
>
> I saw some old questions on how to access neighbors but they don’t seem working for fenicsx.

and explain what is not working with them for your application.

---

<div class="post-metadata">

**Author:** ![hamedZaker](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/hamedzaker/32/4563_2.png) [@hamedZaker](https://fenicsproject.discourse.group/u/hamedZaker)\
**Post date:** [January 6, 2023, 2:26pm UTC](https://fenicsproject.discourse.group/t/how-to-access-neighbor-nodes-and-elements-fenicsx/10104/3 "2023-01-06T14:26:30Z")

</div>

I added some explanations. I hope it is more clear now. Thank you

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [January 6, 2023, 2:52pm UTC](https://fenicsproject.discourse.group/t/how-to-access-neighbor-nodes-and-elements-fenicsx/10104/4 "2023-01-06T14:52:42Z")

</div>

The vertex indices can be access by calling

```python
vertex_map = mesh.topology.index_map(0)
vertex_indices = np.arange(vertex_map.size_local + vertex_map.num_ghosts)
mesh.topology.create_connectivity(0, 1)
mesh.topology.create_connectivity(1, 0)
vertex_to_edge = mesh.topology.connectivity(0, 1)
edge_to_vertex = mesh.topology.connectivity(1, 0)
for vertex in vertex_indices:
      adjacent_edges = vertex_to_edge.links(vertex)
      for edge in adjacent_edges:
            adjacent_vertices = edge_to_vertex.links(edge)
            # Add these to neighbourhood
            #....

```

---

<div class="post-metadata">

**Author:** ![hamedZaker](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/hamedzaker/32/4563_2.png) [@hamedZaker](https://fenicsproject.discourse.group/u/hamedZaker)\
**Post date:** [January 6, 2023, 3:11pm UTC](https://fenicsproject.discourse.group/t/how-to-access-neighbor-nodes-and-elements-fenicsx/10104/5 "2023-01-06T15:11:19Z")

</div>

Thank you very much! It seems what I was looking for. But I had some errors. This modified code works for me:

```auto
domain = mesh.create_unit_square(MPI.COMM_WORLD, 10, 10)
vertex_map = domain.topology.index_map(0)
vertex_indices = np.arange(vertex_map.size_local + vertex_map.num_ghosts)
domain.topology.create_connectivity(0, 1)
domain.topology.create_connectivity(1, 0)
vertex_to_edge = domain.topology.connectivity(0, 1)
edge_to_vertex = domain.topology.connectivity(1, 0)

    for vertex in vertex_indices:
        adjacent_edges = vertex_to_edge.links(vertex)
        neighborhood = []
        for edge in adjacent_edges:
                adjacent_vertices = edge_to_vertex.links(edge)
                neighborhood.append(adjacent_vertices)
        print(neighborhood)

```
