What is the latest API for mesh refinement in DOLFINx?

Hi all,

I just noticed several API’s are changed in DOLFINx recently, and then some of my codes cannot work anymore.
The mesh refinement scheme (Dolfinx: Mesh refinement - #4 by dokken) worked normally previouly. I try to update some API’s by myself, but I cannot find the correct API’s for the remaining part.

MWE (modified from Dolfinx: Mesh refinement - #4 by dokken):

import numpy as np
from dolfinx.generation import RectangleMesh # new API
from dolfinx.io import XDMFFile
from dolfinx.mesh import MeshTags, refine, locate_entities # new API
from dolfinx.cpp.mesh import GhostMode, CellType
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 = RectangleMesh(MPI.COMM_WORLD,
                     [np.array([-W/2, -H/2, 0]), np.array([W/2, H/2, 0])],
                     [n_W, n_H],
                     CellType.triangle, GhostMode.none)


def inside_delta(xs):
    out = []
    for i in np.arange(xs.shape[1]):
        x, y = xs[0, i], xs[1, i]
        if W/2 - abs(x) <= 30*delta or H/2 - abs(y) <= 30*delta:
            out.append(True)
        else:
            out.append(False)
    return np.asarray(out)


def mark_all(xs):
    return np.ones(xs.shape[1])


# boundary layer
boundaries = [(1, lambda x: inside_delta(x))]

for _ in np.arange(5):
    refine_indices, refine_markers = [], []
    dim = mesh.topology.dim
    for (marker, locator) in boundaries:
        facets = locate_entities(mesh, dim, locator)
        refine_indices.append(facets)
        refine_markers.append(np.full(len(facets), marker))

    # Only markers should be int8
    refine_indices = np.array(np.hstack(refine_indices), dtype=np.int32)
    refine_markers = np.array(np.hstack(refine_markers), dtype=np.int8)
    # Indices sent into meshtags should be sorted
    sort = np.argsort(refine_indices)
    refine_tag = MeshTags(
        mesh, dim, refine_indices[sort], refine_markers[sort])

    out_tag = MeshTags(
        mesh, dim, refine_indices[sort], np.asarray(refine_markers[sort], dtype=np.int32))

    with XDMFFile(mesh.mpi_comm(), f"mesh_{_}.xdmf", "w") as xdmf: # this should be changed
        xdmf.write_mesh(mesh)
        xdmf.write_meshtags(out_tag)

    mesh.topology.create_entities(1)
    mesh = refine(mesh, refine_tag) # this should be changed

with XDMFFile(mesh.mpi_comm(), f"mesh_{_+1}.xdmf", "w") as xdmf:
    xdmf.write_mesh(mesh)

More specifically,
(1) What is the correct input for the mesh = refine(mesh, refine_tag) now? According to the description of the source code ( dolfinx/mesh.py at main · FEniCS/dolfinx (github.com)), refine_tag should be _cpp.mesh.MeshTags_int8. But the error message indicates refine_tag should be numpy.int32:

Traceback (most recent call last):
  File "/shared/test1.py", line 68, in <module>
    mesh = refine(mesh, refine_tag)
  File "/usr/local/dolfinx-real/lib/python3.8/dist-packages/dolfinx/mesh.py", line 147, in refine
    mesh_refined = _cpp.refinement.refine(mesh, cell_markers, redistribute)
TypeError: refine(): incompatible function arguments. The following argument types are supported:
    1. (mesh: dolfinx.cpp.mesh.Mesh, redistribute: bool = True) -> dolfinx.cpp.mesh.Mesh
    2. (mesh: dolfinx.cpp.mesh.Mesh, edges: numpy.ndarray[numpy.int32], redistribute: bool = True) -> dolfinx.cpp.mesh.Mesh

(2) What is the new api to replace the mesh.mpi_comm()?

Thanks in advance!

If have pushed a fix for this at: Fix refinement and add tests for sub refinement by jorgensd · Pull Request #1861 · FEniCS/dolfinx · GitHub

In particular, you should send in a set of mesh edges (mesh.topology.dim -2 for 3D, mesh.topology.dim-1 for 2D) to the refinement algorithm, as showed in the test linked to below:

mesh.comm is the new API

1 Like

FYI: Fixed in main: Fix refinement and add tests for sub refinement (#1861) · FEniCS/dolfinx@695212a · GitHub

2 Likes

Thanks very much @dokken! Could you please update the docker image ( dolfinx/dolfinx - Docker Image | Docker Hub) about this issue? It seem the docker image is not updated yet.

Or is there a way to modify the .cpp files? I can only find the .h and .py files in my docker container.

It will be updated between 2 and 4 am tomorrow, as the images are built nightly.

1 Like