Equivalent for SubMesh and BoundaryMesh in dolfinx

Hi all,

SubMesh and BoundaryMesh seem to be unavailable in dolfinx. Is there an equivalent functionality? Consider the following minimal working example:

from dolfinx import FunctionSpace, Function, BoxMesh
import numpy as np
from mpi4py import MPI

class MyExpression:
  def __init__(self):
    pass

  def eval(self, x):
    return np.full(x.shape[1], x[0])

mesh = BoxMesh(MPI.COMM_WORLD, [np.array([0.0, 0.0, 0.0]), np.array([1,1,1])], [5,5,5])
R = FunctionSpace(mesh, ("Lagrange",1))
f = Function(R)
f_initial=MyExpression()
f.interpolate(f_initial.eval)

# === Old Dolfin Strategy to create Submesh. Not available in DolfinX
# is_inside = lambda y: f(y)>0.5
# subdomain = AutoSubDomain(inside_function=is_inside)
# submesh = SubMesh(mesh, subdomain)
# File('submesh.xml') << mysubmesh

# === Old Dolfin Strategy to create Boundarymesh from Submesh. Not available in DolfinX
# bsubmesh = BoundaryMesh(mysubmesh,'exterior')
# File('bsubmesh.xml') << bsubmesh

These classes are not implemented in dolfin-x. However, depending on you actual usecase of these classes, there might be other ways of doing the same operations. Therefore, I would suggest you make a minimal example of what you would use the BoundaryMesh or SubMesh for, and then you might get specific feedback for your usecase.

Dokken, thanks for clarification! I noticed that pyvista allows to extract and save submeshes via the functions threshold() and save_meshio(). This is exactly what I was looking for.

Hello,
The MWE of SubMesh and BoundaryMesh can be found below. Can you suggest me of mapping alternative ?

import dolfinx 
domain=dolfinx.mesh.create_unit_cube(MPI.COMM_WORLD,2,2,2)
# Extracting submesh of left faces in legacy-dolfin
dim=3
bdim = dim-1
bmesh = BoundaryMesh(mesh, "exterior")
mapping = bmesh.entity_map(bdim)
part_of_bot = MeshFunction("size_t", bmesh, bdim)
for cell in cells(bmesh):
    m=mapping[cell.index()]
    curr_facet_normal = Facet(mesh, m).normal()   
    if near(curr_facet_normal.x(), -1.0):  # On bot boundary-left
        part_of_bot[cell] = 1   
mesh_r = SubMesh(bmesh, part_of_bot, 1)

Attempt made for getting SubMesh and BoundaryMesh using dolfinx.

tdim = domain.topology.dim
fdim = tdim - 1
facets = dolfinx.mesh.exterior_facet_indices(domain.topology)
# stuck in getting equivalent to 
# (mapping = bmesh.entity_map(bdim)  of legacy-dolfin and entities_to_geometry works no more in dolfinx as I tried)
def left(x):
    return np.isclose(x[0], 0)
def right(x):
    return np.isclose(x[0], 2)

facets_left = dolfinx.mesh.locate_entities_boundary(domain, dim=(tdim - 1),
                                       marker=left)
facets_right = dolfinx.mesh.locate_entities_boundary(domain, dim=(tdim - 1),
                                       marker=right)

Can you help me getting the equivalent of SubMesh and BoundaryMesh. I don’t find the implementation using pyvista. Any help is appreciated.