How to get neigbor of cell


242
views
0
9 months ago by
I would like to get neighbor cell for each cell
for cell in cells(mesh):
   # How to loop over face neighbors of cell​

I want to get an iterator to the neighbor cells.

Community: FEniCS Project

1 Answer


2
9 months ago by
Hi

consider the following MWE to obtain this functionality

from dolfin import *

# Set-up mesh and connectivity
mesh = UnitSquareMesh(3,3)
tdim = mesh.topology().dim()
mesh.init(tdim - 1, tdim)

# Empty dict for storing results
cell_neighbors = {}

# Loop over cells
for cell in cells(mesh):
    neighbors = []
    for facet in facets(cell):
        # Filtered list of neighbor cells
        neighbor = filter(lambda ci: ci != cell.index(), facet.entities(tdim))
        neighbors.extend(neighbor)    
    # Collect in dict
    cell_neighbors[cell.index()] = neighbors
    
print cell_neighbors​


Alternatively, you can opt for the one-line solution

cell_neighbors = {cell.index(): sum((filter(lambda ci: ci != cell.index(),
                                            facet.entities(tdim))
                                    for facet in facets(cell)), [])
                                    for cell in cells(mesh)}


Hope this helps!

This gives cell indices. Is it possible to get an iterator ? I want to use this to flag cells for refinement
        flag = CellFunction("bool", mesh)
        for cell in cells(mesh):
            i = cell.index()
            if np.abs(Cmean[i]) > 15.0 and cell.h() > Hmin:
                flag[cell] = True
                # TODO: loop over neighbours of cell and set their flag = True​
I need an iterator to the neighbor cell to set its flag to True.
written 9 months ago by Praveen C  

How about this?

flag = CellFunction("bool", mesh)

for cell in cells(mesh):
    neighbors = []
    for facet in facets(cell):
        # Filtered list of neighbor cells
        neighbor = filter(lambda ci: ci != cell.index(), facet.entities(tdim))
        if neighbor: 
            # Make CellEntity
            neighbor_cell = Cell(mesh, neighbor[0])
            flag[neighbor_cell] = True
written 9 months ago by Jakob Maljaars  
Please login to add an answer/comment or follow this question.

Similar posts:
Search »