Opening a dolfin.File in 'append' mode

Asked by Maximilian Albert on 2013-02-19


when writing fields successively to a .pvd file (or even a .xdmf file with the latest dolfin), is there a way to open the file in 'append' mode to preserve previously saved data? For example, when I'm doing this:

   f = df.File('foo.pvd')
   f << u1
   f << u2
   f << u3
   f = df.File('foo.pvd')
   f << u4

then the fields u1, u2, u3 are lost and only u4 is present. Is there a way to modify this piece of code so that all four fields are present in the file? I tried some variations of

   f = df.File('foo.pvd', mode='a')

but unfortunately I couldn't get any of them to work.

Many thanks in advance!

P.S.: The snippet above is of course a bit pointless, but the use case is when re-opening the file happens during a different run of my code but I'd like to avoid overwriting existing data.

Question information

English Edit question
DOLFIN Edit question
No assignee Edit question
Solved by:
Maximilian Albert
Last query:
Last reply:
Chris Richardson (chris-bpi) said : #1

In theory, you should be able to open an HDF5File in append mode, like this:

F = HDF5File("data.h5","a")
F.write(my_mesh, "mesh_name")
F.write(my_vector, "vector_name")

At the moment, there is no F.close(), so you just have to destroy the object:

del F

to close the file, if you want to do that.

Maximilian Albert (cilix) said : #2

Thanks, Chris! I presume there is no way to make this work for other file types?

Also, I guess an F.close() method would be useful in the future since 'del F' doesn't guarantee that the object is actually garbage-collected.