Mesh refinement not working?

I’m using a simple 1D mesh which I refine in a specific zone using the recommended marking and refine method:

size = 4e-3
nb_cells_init = 4000
mesh = IntervalMesh(nb_cells_init,0, size)

Adding at least 100 cells in the first 50e-8m:

nb_cells = 100
distance = 50e-8
while count < nb_cells and nb_cells > 0:
    count = 0
    cell_markers = MeshFunction("bool", mesh, mesh.topology().dim())
    cell_markers.set_all(False)
        for cell in cells(mesh):
                if cell.midpoint().x() < distance:
                    count = count+1
                    cell_markers_left[cell] = True
        if count == 0:
            for cell in cells(mesh):
                if cell.midpoint().x() < size/nb_cells_init:
                    cell_markers[cell]=True
    mesh = refine(mesh, cell_markers)
print("Nb of cells in the mesh:" + str(len(mesh.cells())))

The print allows me to check that the mesh has more cells than initially.

In postprocessing, I use a XDMF/h5 file to save my solution. This file is initialized with

xdmf_sol = XDMFFFile(mesh.mpi_comm(), "solution.xdmf")

and updated at each timestep:

xdmf_sol.write(solution, t)

However when I check the output file with Paraview, the solution is only plotted over the points of the unrefined mesh.

I printed mesh.coordinates() in order to check that there are some added points and noticed that they are put at the end of the array:

[[0.00000000e+00]
[1.00000000e-06]
[2.00000000e-06]

[3.37890625e-07]
[3.30078125e-07]
[3.33984375e-07]]

Using mesh.order() does not change this.

In the h5 file, the mesh is refined and the solution matches the refined mesh.

What am I missing here? Is it just a Paraview issue ?

You need to supply a minimal working example for anyone to help you with this particular issue.

My apologies, I edited my question

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