Dolfinx: Mesh refinement

The posted fixed example by @dokken no longer works, because the dolfinx API has changed.

The following code works with newer dolfinx versions (was tested with dolfinx 0.5.1)

import numpy as np
import pyvista
from dolfinx.mesh import CellType, create_rectangle, locate_entities, refine
from dolfinx.plot import create_vtk_mesh
from mpi4py import MPI

W = 380e-6  # width
H = 180e-6  # height

d_ele = 20e-6
delta = 1e-6


n_W = int(np.ceil(W / d_ele))  # number of elements along W
n_H = int(np.ceil(H / d_ele))  # number of elements along H

# Create mesh
mesh = create_rectangle(
    MPI.COMM_WORLD,
    points=((-W / 2, -H / 2), (W / 2, H / 2)),
    n=(n_W, n_H),
    cell_type=CellType.triangle,
)


def inside_delta(xs):
    # refine around the boundary
    return np.logical_or(
        W / 2 - abs(xs[0, :]) <= 30 * delta,
        H / 2 - abs(xs[1, :]) <= 30 * delta,
    )


for _ in np.arange(5):
    dim = mesh.topology.dim
    edges = locate_entities(mesh, dim - 1, inside_delta)
    mesh.topology.create_entities(1)
    mesh = refine(mesh, edges, redistribute=False)

    vtkdata = create_vtk_mesh(mesh, mesh.topology.dim)
    plotter = pyvista.Plotter()
    grid = pyvista.UnstructuredGrid(*vtkdata)
    actor = plotter.add_mesh(grid, show_edges=True)
    plotter.view_xy()
    plotter.show()

and it generates plots of the refined mesh

4 Likes