The XDMFFile does not store a new mesh geometry at each time-step if not explicitly told to do so (as this is very expensive).
Here is a 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)
mesh.name = "initial_mesh"
xdmf = dolfinx.io.XDMFFile(mesh.comm, "output.xdmf", "w")
xdmf.write_mesh(mesh)
def f(x):
return x[0]**2, x[1]**2
V = dolfinx.fem.functionspace(mesh, ("Lagrange", 1, (2, )))
u = dolfinx.fem.Function(V)
u.interpolate(f)
xdmf.write_function(
u, 0.0, mesh_xpath=f"/Xdmf/Domain/Grid[@Name='{mesh.name}']")
# Perturb mesh
u.x.array[:] -= 1
mesh.geometry.x[:, :] += 0.1
mesh.name = "perturbed_mesh"
xdmf.write_mesh(mesh)
xdmf.write_function(
u, 1.0, mesh_xpath=f"/Xdmf/Domain/Grid[@Name='{mesh.name}']")
xdmf.close()