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

Dear FEniCSx community,

I am using dolfinx 0.7.2.

I am trying to save multiple functions into one file using dolfinx.io.VTKFile. The current code I have is the following:

with io.VTKFFile(comm, vtk_filepath, "w") as file_vtk:
   file_vtk.write_mesh(domain)
   u_f.name = "u"
   stress_vm_f.namem = "stress_vm"
   file_vtk.write_function(u_f)
   file_vtk.write_function(stress_vm)

It seems that only stress_vm is saved. Thanks in advance for any help!

Best regards,
Jorge

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