Bug or strange behavior in XDMFFile checkpoint

Issue #1022 new
Neliju created an issue

Dear all,

I am facing a bug or something I don't understand when using the new write_checkpoint and read_checkpoint methods of XDMFFile.

More precisely, if I create a mixed space (u in P2 and p in P1 for example) and export the two subfunctions with write_checkpoint, if later on I import back the two subfunctions with read_checkpoint, the original functions are not restored correctly. The values of u and p seem to be permuted at the point (0,0). This is not happening without mixed functions. The problem is still there if one replaces P2×P1 by P1×P1. This is working fine if I use deepcopy=True.

See the attached file for a minimal working example (work fine if deepcopy is True on line 28).

Does anyone have an idea what is happening?

Configuration: FEniCS 2017.2, PETSc 3.8.0 and HDF5 1.8.18 compiled from sources on ubuntu 16.04.

Thanks and best regards,

Nelijuc

import os
from dolfin import *

# Create mesh
mesh = UnitSquareMesh(2, 2)

# Define space
P2 = FiniteElement("Lagrange", mesh.ufl_cell(), 2)
P1 = FiniteElement("Lagrange", mesh.ufl_cell(), 1)
W = FunctionSpace(mesh, P2*P1)

up = Function(W)
u,p = split(up)

# Define u = 1 and p = 0
up.interpolate(Constant((1,0)))

# Check that grad(u) = 0
norm = assemble(inner(grad(u),grad(u))*dx)
print("The norm of grad(u) is %f" % norm)

# Export to XDMF
try:
    os.remove("xdmf-up.xdmf")
except:
    pass
xdmf = XDMFFile("xdmf-up.xdmf")
uu,pp = up.split()
xdmf.write_checkpoint(uu, "u")
xdmf.write_checkpoint(pp, "p")
del xdmf

# Import from XDMF to u0 and p0
V = W.sub(0).collapse()
Q = W.sub(0).collapse()
u0 = Function(V)
p0 = Function(Q)
xdmf = XDMFFile("xdmf-up.xdmf")
xdmf.read_checkpoint(u0, "u")
xdmf.read_checkpoint(p0, "u")
del xdmf

# Now grad(u0) is no more zero
norm = assemble(inner(grad(u0),grad(u0))*dx)
print("The norm of grad(u0) is %f" % norm)

# Export u0 and p0 to see where is the problem
try:
    os.remove("xdmf-up0.xdmf")
except:
    pass
xdmf = XDMFFile("xdmf-up0.xdmf")
xdmf.write_checkpoint(u0, "u0")
xdmf.write_checkpoint(p0, "p0") 
del xdmf

Comments (3)

  1. Michal Habera

    I don't think it should be considered a bug.

    When you split in a shallow way, uu, pp = up.split() then uu.vector().size() == pp.vector().size(). Underlying data vector is the only one and this "big" vector is saved into XDMF file twice (for two pointers uu and pp). This should be forbidden.

    Please do uu, pp = up.split(deepcopy=True) and read into functions build on function spaces V = uu.function_space() and Q = pp.function_space().

    But I'll keep this as enhancement issue, as there is definitely some sanity work to be done.

  2. Log in to comment