How to write multiple functions into one file with dolfinx.io.VTKFile?

Consider the following minimal reproducible example:

from mpi4py import MPI

import dolfinx

mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 10, 10, dolfinx.cpp.mesh.CellType.triangle)
V = dolfinx.fem.functionspace(mesh, ("Lagrange", 1))
u = dolfinx.fem.Function(V, name="u")
v = dolfinx.fem.Function(V, name="v")

with dolfinx.io.VTKFile(mesh.comm, "test.pvd", "w") as file_vtk:
    file_vtk.write_mesh(mesh)
    file_vtk.write_function([u, v], t=0.0)

Next time you post, please ensure that you make the code reproducible.
The code you have posted is not standalone, and contains typos:

as VTKFFile is not a dolfinx object.

2 Likes