If speed and memory is a concern, I suggest you use scipy
routines to save and load your data.
I have found that saving and loading a state u
via converting u
into a numpy
array, and using scipy.io
routines to save and load the data is much faster than using <<
and xml
files.
Going via scipy appears to be 30 times faster, while consuming only 10% of the memory, necessary for the xml files. These numbers hold for a scalar function on a 512x512 UnitSquareMesh for pw. linear 'CG' functions. The speedup increases with more DOFs.
The code for both procedures is given below. The speed test can be found and inspected on my github account .
from dolfin import UnitSquare, FunctionSpace, Expression, Function, File, project
from scipy.io import loadmat, savemat
mesh = UnitSquareMesh(512, 512)
V = FunctionSpace(mesh, "CG", 2)
u = Expression('x[0]*x[0]')
ufun = project(u, V)
# generic way as Jan Blechta has pointed out
File('saved_u.xml') << ufun
u_old = Function(V, 'saved_u.xml')
# conversions and scipy io functions
uvec = ufun.vector().array().reshape(len(ufun.vector()),1)
savemat('saved_u', { 'uvec': uvec }, oned_as='column')
uvec_old = loadmat('saved_u')['uvec']
u_old = Function(V)
u_old.vector().set_local(uvec_old[:,0])