Mesh refinement: adapt vs refine

See my modifications of your code:

from dolfin import *
import matplotlib.pyplot as plt


n_elem = 1
n_ref =  2
mesh = UnitSquareMesh(n_elem,n_elem,"crossed")
Praf = Point(0.20981,0.10987652,0.0)


fig, axs = plt.subplots(nrows=n_ref+1, ncols=1, constrained_layout=True)

plt.sca(axs[0])
plot(mesh)
plt.scatter(Praf.x(), Praf.y())


for i_ref in range(n_ref):
    print("-------------------------------------")
    print("Refinement "+str(i_ref))
    print("-------------------------------------")
    print(" ")
    markers = MeshFunction("bool", mesh, mesh.topology().dim(), False)
    for cell in cells(mesh):
        markers[cell] = cell.contains(Praf)
        if markers[cell]:
            print(cell.get_vertex_coordinates()) 
    print("")

    mesh = refine(mesh,markers)

    plt.sca(axs[i_ref+1])
    plt.scatter(Praf.x(), Praf.y())
    plot(mesh)


plt.savefig("meshes.png")

Which gives me this:

meshes

The key change here was

markers = MeshFunction("bool", mesh, mesh.topology().dim(), False)

A MeshFunction requires the topology dimension over which it’s defined. By providing False as the third argument, you were essentially creating a vertex function (False is interpreted as 0 which corresponds to vertex topology). This is one of the many reasons I recommend dolfinx over old dolfin since it employs explicit interfaces where possible.

1 Like