Getting markers for cells

Hi, I have a mesh with different subdomains. I wanted to know how I can find the subdomain marker for each cell or how I can find which cell belongs to which subdomain ? Is it possible to Iterate through all cell and find the marker value for each cell ?


It is not clear what version of FEniCS you are using and there is no MWE.

For DOLFINx you can check, among others,

For DOLFINx, the marking is performed over the edges belonging to a desired cell criterion. Maybe @dokken can explain further.

For Legacy DOLFIN you can check, among others,

Here, the markers are stored as booleans in a MeshFunction. Hence, you can access the markers by iterating over the cells:

Taken from

from dolfin import *
import matplotlib.pyplot as plt

n_elem = 1
n_ref = 2
mesh = UnitSquareMesh(n_elem, n_elem, "crossed")
Praf = Point(0.20981, 0.10987652, 0.0)

fig, axs = plt.subplots(nrows=n_ref+1, ncols=1, constrained_layout=True)[0])
plt.scatter(Praf.x(), Praf.y())

for i_ref in range(n_ref):
    print("Refinement "+str(i_ref))
    print(" ")
    markers = MeshFunction("bool", mesh, mesh.topology().dim(), False)
    for cell in cells(mesh):
        markers[cell] = cell.contains(Praf)
        if markers[cell]: