Output multiple mesh-changed result into one single visualization file

If it is possible to write multiple mesh-changed result into a single file of XDMF/VTX in FEniCSx?

I want to achieve the similar one as what I do in FEniCS: I change my mesh in time (e.g. adaptive mesh refinement or AMR) and output time dependent Function on that changed mesh to a single XDMF file as following:

# Pseudo code demonstrates functionality in FEniCS
mesh0 = Mesh()           # initial mesh
XDMFFile(mesh_path).read(mesh0)

xdmf = XDMFFile(path)

# Time iteration
while t < t_end: 

	# Do AMR
	mesh = refine(mesh0, material)

	# Dump time dependent mesh-changed result `f`
	f = Function(FunctionSpace(mesh))
	xdmf.write(f, t)

	t += dt

Consider the following MWE (based on the same logic as Update mesh over time - #2 by dokken):

from mpi4py import MPI
import dolfinx


mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 10, 10)
mesh.name = "InitialMesh"
V = dolfinx.fem.FunctionSpace(mesh, ("Lagrange", 1))
u = dolfinx.fem.Function(V)
u.interpolate(lambda x: x[0] * x[1])
u.name = "f"

xdmf = dolfinx.io.XDMFFile(MPI.COMM_WORLD, "functions.xdmf", "w")
xdmf.write_mesh(mesh)
xdmf.write_function(u, mesh_xpath=f"/Xdmf/Domain/Grid[@Name='{mesh.name}']")

mesh.topology.create_connectivity(1, 2)
r_mesh = dolfinx.mesh.refine(mesh)
r_mesh.name = "Refined"
Vr = dolfinx.fem.FunctionSpace(r_mesh, ("Lagrange", 1))
ur = dolfinx.fem.Function(Vr)
ur.interpolate(lambda x: x[0] * x[1])
ur.name = "f"

xdmf.write_mesh(r_mesh)
xdmf.write_function(
    ur, t=1, mesh_xpath=f"/Xdmf/Domain/Grid[@Name='{r_mesh.name}']")
xdmf.close()
1 Like