From DG0 to MeshFunction in parallel

Hello,

I am trying to pass cells index from a Function (DG0) onto a boolean MeshFunction in order to perform a selective refinement of the domain.

While in serial I could easily pass the index of the cells where the value of the Function surpasses a set threshold to the MeshFunction. However, the same procedure does not work in parallel due to the local indexing of the cells.

A mwe of the process i use in serial is reported below, but i don’t know how to achieve a similar result in parallel.

from dolfin import *
import numpy as np

mesh = UnitSquareMesh(10,10)

Vdg = FunctionSpace(mesh, 'DG', 0)
v = interpolate(Expression('(x[0] < 0.5) ? 1 : 0', degree = 0), Vdg)
cells_mark = np.argwhere(v.vector() >= 1)

mf = MeshFunction('bool', mesh, 2, False)
mf.array()[cells_mark] = True

mesh2=refine(mesh, mf)

Thanks in advance.