How can I plot quantities from mixedspace using pyvista

Hello,
I am having trouble accessing quantities from a subspace of a mixedspace, i.e. I want to plot u1_h in the example below. I tried as follows, but it gave me an error that the data lengths do not match:


import pyvista
from ufl import FiniteElement, MixedElement, split
from dolfinx import mesh, plot
from dolfinx.fem import FunctionSpace, Function
from mpi4py import MPI

msh = mesh.create_interval(MPI.COMM_WORLD, 10, points=(0, 1))
CG1_elem = FiniteElement("CG", msh.ufl_cell(), 1)
ME_elem = MixedElement([CG1_elem, CG1_elem])
ME = FunctionSpace(msh, ME_elem)
u_h = Function(ME)
u1_h, u2_h = split(u_h)
# Dummy function space for pyvista grid
W_dummy = FunctionSpace(msh, ("CG", 1))
cells, types, x = plot.create_vtk_mesh(W_dummy)
grid = pyvista.UnstructuredGrid(cells, types, x)
grid.point_data["u1_h"] = u_h.sub(0).x.array.real

You Need to collapse each suv component of the space:

u_h = Function(ME)
u1_h =u_h.sub(0).collapse()
u2_h = u_h.sub(1).collapse()
2 Likes