Hello everyone,

I would like to store a sub-function from a mixed-function space into a vtk file. Considering my MWE

from dolfinx.mesh import create_unit_square
from dolfinx.cpp.mesh import CellType
from mpi4py import MPI
import os
import ufl  
import dolfinx 

mesh_1 = create_unit_square(comm, 8, 8, CellType.triangle)  

deg_cwe_u = 1
deg_cwe_p = 1
U_el = ufl.VectorElement("Lagrange", mesh_1.ufl_cell(), deg_cwe_u)
P_el = ufl.FiniteElement("Lagrange", mesh_1.ufl_cell(), deg_cwe_p)
W_cwe = dolfinx.fem.FunctionSpace(mesh_1, ufl.MixedElement([U_el, P_el]))    
up_cwe = dolfinx.fem.Function(W_cwe)
mesh_1_vtk = dolfinx.io.VTKFile(comm, f'{os.getcwd()}/x_interpolation_1.vtk', "w")    
_ua = up_cwe.split()[0]
mesh_1_vtk.write_function(_ua, 0.)     

I got the error:

RuntimeError: Cannot write sub-Functions to VTK file

As far as I can see, only interpolation into a new FunctionSpace helps:

utmp = dolfinx.fem.Function(dolfinx.fem.FunctionSpace(mesh_1, U_el))
mesh_1_vtk.write_function(utmp, 0.)        

But this is a bit time-consuming. Is there any other way to store the results directly in a VTK-file?

Any help is appreciated.

Use collapse on the function: https://github.com/FEniCS/dolfinx/blob/e4439ccca81b976d11c6f606d9c612afcf010a31/python/dolfinx/fem/function.py#L479-L483

should be
mesh_1_vtk.write_function(_ua.collapse(), 0.)