I want to refine my mesh (multiple times) based on the location of the cell. The reason is, that I need to have a fine mesh towards the boundary of the domain. Since my problem is complex, I am using dolfinx.
However, I cannot get it to run. There are some examples for dolfin
but I did not spot one for dolfinx
. My code so far is. I would really appreciate any help on that.
import dolfinx
import numpy as np
from dolfinx import RectangleMesh
from dolfinx.fem import locate_dofs_geometrical, locate_dofs_topological
from dolfinx.io import XDMFFile
from dolfinx.mesh import locate_entities_boundary, refine
W = 380e-6 # width
H = 180e-6 # height
d_ele = 20e-6
delta = 1e-7
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, dolfinx.cpp.mesh.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)),
(0, lambda x: mark_all(x))]
refine_indices, refine_markers = [], []
dim = mesh.topology.dim
for (marker, locator) in boundaries:
facets = dolfinx.mesh.locate_entities(mesh, dim, locator)
refine_indices.append(facets)
refine_markers.append(np.full(len(facets), marker))
refine_tag = dolfinx.MeshTags(mesh, dim,
np.array(np.hstack(refine_indices), dtype=np.int8),
np.array(np.hstack(refine_markers), dtype=np.int8))
mesh.topology.create_entities(1)
mesh = refine(mesh, refine_tag)
# in order to export it you have to change dtype=np.int8 to dtype=np.int32
with XDMFFile(mesh.mpi_comm(), "mesh.xdmf", "w") as xdmf:
xdmf.write_mesh(mesh)