Plotting Partial Meshes

For problems with multiple subdomains, is it possible to plot solutions only over particular subdomains? In particular, I am working on a problem where there is a “background” mesh with objects inside of it, and I only care about the deformation of the objects. Would there be ways to plot the deformed state of particular subdomains (objects) without plotting others (background)?

I am currently plotting things in-line using pyvista as shown in the Hyperelasticity example but would like to eventually work with something external so that I can create animations of the time-varying solutions.

Any help would be greatly appreciated!

Are you using DOLFINx or DOLFIN?

Sorry for not including that. I am working with DOLFINx in the Jupyter lab environment.

You can map data from a bigger mesh to a smaller mesh, using the submeshes:

import dolfinx
from mpi4py import MPI
import numpy as np
mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 10, 10)
cells = np.array([6,8,12], dtype=np.int32)
V = dolfinx.fem.FunctionSpace(mesh, ("DG", 2))
u = dolfinx.fem.Function(V)
u.interpolate(lambda x: x[0],cells=np.arange(12, dtype=np.int32))

sub_mesh = dolfinx.mesh.create_submesh(mesh, mesh.topology.dim, cells)
V_sub = dolfinx.fem.FunctionSpace(sub_mesh[0], ("DG", 2))
v = dolfinx.fem.Function(V_sub)
for i, c in enumerate(cells):
    parent_dofs = V.dofmap.cell_dofs(c)
    sub_dofs = V_sub.dofmap.cell_dofs(i)
    assert V.dofmap.bs == V_sub.dofmap.bs
    for dof, sub_dof in zip(parent_dofs, sub_dofs):
        for j in range(V.dofmap.bs):
            v.x.array[sub_dof*V.dofmap.bs + j] = u.x.array[dof*V.dofmap.bs + j]

with dolfinx.io.VTXWriter(mesh.comm, "u.bp", u) as vtx:
    vtx.write(0.0)
with dolfinx.io.VTXWriter(mesh.comm, "v.bp", v) as vtx:
    vtx.write(0.0)

You can use pyvista to create gifs, as for instance shown in:
https://jorgensd.github.io/fenics22-tutorial/heat_eq.html

1 Like