# Mesh refinement not working?

**URL:** https://fenicsproject.discourse.group/t/mesh-refinement-not-working/3199
**Category:** mesh
**Created:** [May 14, 2020, 10:40am UTC](https://fenicsproject.discourse.group/t/mesh-refinement-not-working/3199 "2020-05-14T10:40:00Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![mackenzie](https://avatars.discourse-cdn.com/v4/letter/m/3da27b/32.png) [@mackenzie](https://fenicsproject.discourse.group/u/mackenzie)
#### Post date: [May 14, 2020, 10:40am UTC](https://fenicsproject.discourse.group/t/mesh-refinement-not-working/3199/1 "2020-05-14T10:40:00Z")

</div>

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 ?

---

<div class="post-metadata">

### Author: ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)
#### Post date: [May 14, 2020, 10:59am UTC](https://fenicsproject.discourse.group/t/mesh-refinement-not-working/3199/2 "2020-05-14T10:59:45Z")

</div>

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

---

<div class="post-metadata">

### Author: ![mackenzie](https://avatars.discourse-cdn.com/v4/letter/m/3da27b/32.png) [@mackenzie](https://fenicsproject.discourse.group/u/mackenzie)
#### Post date: [May 14, 2020, 12:23pm UTC](https://fenicsproject.discourse.group/t/mesh-refinement-not-working/3199/3 "2020-05-14T12:23:55Z")

</div>

My apologies, I edited my question

---

<div class="post-metadata">

### Author: ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)
#### Post date: [May 14, 2020, 4:08pm UTC](https://fenicsproject.discourse.group/t/mesh-refinement-not-working/3199/4 "2020-05-14T16:08:57Z")

</div>

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:

```auto
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).

 ![prepost](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/7/75bd8819eaa48b7c5074f98f0f42b8b7292681a3.png)
