How to adapt mesh markers to refined mesh?

4 months ago by
I'm using Fenics 2017.2.0 version and have problems with adapting mesh function to refined mesh. I want to solve Poisson problem on domain with two subdomains (circle inside square). Ordinary mesh is to coarse near the circle boundary so I want to locally refine it.
After that I adapt mesh markers but it seems like they are not adapted and that mesh markers defining class koef are the same as they are in the original (nonrefined) mesh.

from fenics import *
from mshr import *
import matplotlib.pyplot as plt

# Mesh 
domain = Rectangle(Point(0, 0), Point(1, 1))
patch = Circle(Point(0.5, 0.5), R)
domain.set_subdomain(1, patch)
mesh0 = generate_mesh(domain, 20)
markers0 = MeshFunction('size_t', mesh0, 2,

$ Mesh refinement
tol = 0.02
cell_markers = MeshFunction("bool", mesh0, 2)
origin = Point(0.5, 0.5)
for cell in cells(mesh0):
	p = cell.midpoint()
	if R - tol <= p.distance(origin) <= R + tol:
		cell_markers[cell] = True
		cell_markers[cell] = False

mesh = refine(mesh0, cell_markers)

# Update markers
markers = adapt(markers0, mesh)

# Function space
V = FunctionSpace(mesh, "P", 1)

# Mesh function
class koef(Expression):
    def __init__(self, mesh, **kwargs):
        self.markers = markers
    def eval_cell(self, values, x, cell):
        if markers[cell.index] == 0:
            values[0] = 1
            values[0] = 10

kappa = koef(mesh, degree=0)
A = interpolate(kappa, V)​

Using very coarse mesh at the beggining we get something like this:

and using finer mesh we get something like this:

In both cases class koef does not see updated mesh subdomain markers to refined mesh.

Community: FEniCS Project

1 Answer

4 months ago by
Hi Dario,

Try replacing the following lines in your code
mesh = refine(mesh0, cell_markers)

# Update markers
markers = adapt(markers0, mesh)
with the following lines, and check out if this does what you want done.
    print("Size of cell_markers before refinement. %d \n" % cell_markers.size())
    adapt(cell_markers, mesh.child())    
    mesh = mesh.child()
    cell_markers = cell_markers.child()
    print("Size of cell_markers after refinement. %d \n" % cell_markers.size())            
Thanks for your answer. Unfortunately this report error in adapt function which I don't know how to resolve.

   adapt(cell_markers, mesh.child())    
  File "/usr/lib/python3/dist-packages/dolfin/cpp/", line 4372, in adapt
    val = _fem.adapt(*args)
TypeError: in method 'adapt', argument 1 of type 'dolfin::ErrorControl const &'
written 4 months ago by Dario  
Please login to add an answer/comment or follow this question.

Similar posts:
Search »