Mesh refinement not working?

You need to make your example reproducable. As you have ignored defining several variables, I cannot run your code.
I have created a proper minimal working example to illustrate how this should work:

from dolfin import *
size = 0.1
nb_cells_init = 10

mesh = IntervalMesh(10,0, 0.1)
x = SpatialCoordinate(mesh)
V = FunctionSpace(mesh, "CG", 1)
u = project(x[0], V)
xdmf_sol = XDMFFile("solution_pre.xdmf")
xdmf_sol.write(u, 0)

print("Nb of cells in the mesh:" + str(len(mesh.cells())))

cell_markers = MeshFunction("bool", mesh, mesh.topology().dim())
cell_markers.set_all(False)
for cell in cells(mesh):
    if cell.midpoint().x() < 0.05:
        cell_markers[cell] = True
mesh = refine(mesh, cell_markers)
print("Nb of cells in the mesh:" + str(len(mesh.cells())))
x = SpatialCoordinate(mesh)
V = FunctionSpace(mesh, "CG", 1)
u = project(x[0], V)

xdmf_sol = XDMFFile("solution_post.xdmf")
xdmf_sol.write(u, 0)

This produces two paraview files, and I have attached an image of the points where the solution is saved (refined left, unrefined right).

1 Like