Save solution and load it in the next iteration

Hi! I have the next problem: I need to save the solution at each iteration to some file and in the next iteration load solution from the previous iteration. Is it possible? I know, that I can simply assign result from the previous iteration to current function, but this option does not suit me. Thank you advance. The example below.

from dolfin import *

mesh = Mesh("...some_file")
V = FunctionSpace(mesh, 'Lagrange', 1)
...

for i in range(10):
    u0 = load_solution()

    # Solve equation with initial conditions u0, store solution to the u function
    ...

    save_solution(u)


def save_solution(u):
    # Save solution to some file
    ...


def load_solution():
    # Load solution from some file and return it. u0 - Function
    ...
    return u0
2 Likes

You should be able to do this with HDF5 files:

from dolfin import *

# Define a (nonzero) function `f`:
mesh = UnitSquareMesh(10,10)
V = FunctionSpace(mesh,"CG",1)
x = SpatialCoordinate(mesh)
f = project(x[0]*x[1],V)

# Write `f` to a file:
fFile = HDF5File(MPI.comm_world,"f.h5","w")
fFile.write(f,"/f")
fFile.close()

# Read the contents of the file back into a new function, `f2`:
f2 = Function(V)
fFile = HDF5File(MPI.comm_world,"f.h5","r")
fFile.read(f2,"/f")
fFile.close()

# Check the difference between the functions:
print(assemble(((f-f2)**2)*dx))
5 Likes

You could Also use XDMFFile.write_checkpoint and read_checkpoint.

1 Like