HDF5File should raise an exception when I try to read data into the wrong function space

Issue #861 new
Patrick Farrell created an issue

HDF5File.read does not do enough error checking. Consider the following code, which writes a mixed function and tries to read in a scalar function:

from dolfin import *

mesh = UnitSquareMesh(2, 2)

Ve = FiniteElement("CG", triangle, 1)
Ze = MixedElement([Ve, Ve, Ve])

V = FunctionSpace(mesh, Ve) # scalar function space
Z = FunctionSpace(mesh, Ze) # mixed  function space

# write a mixed function to HDF5
d = interpolate(Constant((1, 2, 3)), Z)
h5w = HDF5File(mpi_comm_world(), "bug.h5", "w")
h5w.write(d, "/solution")
del h5w

# read in a scalar function from HDF5
u = Function(V) # wrong space, should be Z!
h5r = HDF5File(mpi_comm_world(), "bug.h5", "r")
h5r.read(d, "/solution")

print "d.vector(): ", list(d.vector())

It prints [1.0, 2.0, 3.0, ...] instead of raising an exception.

Comments (3)

  1. Chris Richardson

    I'm not sure how we could expect to overcome this issue. The HDF5File saves a Function as a Dofmap and Vector, and can restore them to another Dofmap and Vector of the same dimension. We necessarily rely on users setting up the correct Mesh and FunctionSpace to read into. Otherwise, we will have to somehow store a "signature", which would be difficult to maintain backwards compatibility. The simplicity of dofmap/vector storage works well so far.

  2. Chris Richardson

    Also, I think there is something wrong with your example. You should have put h5r.read(u, "/solution") which does indeed raise an exception.

  3. Log in to comment