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

Cell neighbours

+1 vote

Hello,
For each cell of a 2D mesh is it possible to have the list of the cells that have a common
edge with it ?

Thanks

asked Jun 12, 2014 by micdup FEniCS User (1,120 points)

1 Answer

+3 votes

Hi, consider

from dolfin import *

mesh = UnitSquareMesh(2, 2)

# Init facet-cell connectivity
tdim = mesh.topology().dim()
mesh.init(tdim - 1, tdim)

# For every cell, build a list of cells that are connected to its facets
# but are not the iterated cell
cell_neighbors = {cell.index(): sum((filter(lambda ci: ci != cell.index(),
                                            facet.entities(tdim))
                                    for facet in facets(cell)), [])
                for cell in cells(mesh)}

print cell_neighbors 
answered Jun 12, 2014 by MiroK FEniCS Expert (80,920 points)

Beautiful !
Thanks a lot!

...