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

flag elements on a boundary

0 votes

This post described how to flag cells on the entire boundary; if I'd like to flag all cells that lie on a certain part of the boundary (say, $x=1$ on a unit square), is there an easy way to do so as well?

asked Dec 19, 2013 by jlchan FEniCS Novice (390 points)

1 Answer

+1 vote
 
Best answer

Hi, you need to modify the criteria that was used to identify the boundary cells in the post you referred to. Here are two examples of such logic for the unit square an x=1 as the boundary

from dolfin import *

mesh = UnitSquareMesh(10, 10)

# a cell with at least one vertex on boundary belongs to boundary
boundary_cells = [cell for cell in cells(mesh)\
            if any([near(vertex.x(0), 1) for vertex in vertices(cell)])]

# a cell with at least one facet with midpoint on boundary belongs to boundary
#boundary_cells = [cell for cell in cells(mesh)\
#            if any([near(facet.midpoint().x(), 1) for facet in facets(cell)])]

all_cells = CellFunction("size_t", mesh, 0)
for cell in boundary_cells:
    all_cells[cell] = 1

plot(all_cells, interactive=True)
answered Dec 19, 2013 by MiroK FEniCS Expert (80,920 points)
selected Dec 19, 2013 by jlchan

MiroK, thanks! Good to know you can iterate over vertices/facets in such a manner.

...