Read mesh from XDMF file (write_checkpoint)

To use read, you need to use write. However, you can combine the two as follows:

from fenics import *
mesh = UnitSquareMesh(10, 10)

V = FunctionSpace(mesh, 'P', 1)

u = Function(V)
u.vector()[:] = 2
with XDMFFile("out.xdmf") as outfile:
    outfile.write(mesh)
    outfile.write_checkpoint(u, "u", 0, append=True)

mesh2 = Mesh()
u2 = Function(V)
with XDMFFile("out.xdmf") as infile:
    infile.read(mesh2)
    infile.read_checkpoint(u2, "u")
print(u2.vector().get_local())
print(mesh.num_cells(), mesh2.num_cells())
1 Like