How to save workspace variables in FEniCS

12 months ago by
Hello, I'm using a solver very similar to the navier-stokes example in the tutorial:, with a few modifications. One of the changes is that I am importing a very large mesh.

I would like to save the workspace variables in FEniCS - the reason is because it takes a really long time to get to the beginning of the time loop. So i would like to save all the workspace variables just before the time loop, then load the workspace variables later in a different script to just run the time loop and not have to wait for the assembly of the bilinear forms all over again.

This is very easy to do in MATLAB - I'm looking for the equivalent of save file.mat and load file.mat.

I tried doing this in Spyder, but Spyder does not save the bilinear or the linear forms (because they are never explicitly stored in the variable explorer).

Hope to hear from someone soon. Thank you,

Community: FEniCS Project

2 Answers

12 months ago by

Not sure if this will help: When I tried finding something similar in Python to saving .mat workspace data in MATLAB, the best I found was pickle, , or object serialization. I've had mixed luck with being able to pickle complicated Python objects, and I don't yet know how pickle friendly fenics is. Also I haven't pickled anything in parallel.

Edit: Perhaps a better answer: I implemented checkpoint/restarting in my own code entirely using HDF5. I'd be surprised if you're trying to save something that can't be written/read to/from HDF5.

That link is 404.  Also, see the above answer for saving np.array to .mat.
written 12 months ago by pf4d  
I fixed the link. The hyperlink had caught the ")" at the end, so I changed my format.
written 12 months ago by Alexander G. Zimmerman  
Thank you for replying - i read about pickle, but i thought that it wasn't suitable for saving a lot of variables, i think there is also a package called 'dill'

I'm gonna try the HDF5  approach, and will update this post with the results, thank you again,
written 12 months ago by Sophia Wright  

In case it helps, here's a snippet from my code:

# Write checkpoint/restart files
with fenics.HDF5File(fenics.mpi_comm_world(), restart_filename, "w") as h5:
    h5.write(mesh, "mesh")
    h5.write(w_n, "w_n")
if fenics.dolfin.MPI.rank(fenics.mpi_comm_world()) is 0:
    with h5py.File(restart_filename, "r+") as h5:
        h5.create_dataset("t", data=current_time)

The relevant part is that I used fenics.HDF5File for saving the solution function w_n, but I used h5py to add another dataset to the same HDF5 file. Then when I restart, I do

with h5py.File(restart_filename, "r") as h5:
    current_time = h5['t'].value

w_n = fenics.Function(W)
mesh = fenics.Mesh()
with fenics.HDF5File(mesh.mpi_comm(), restart_filename, 'r') as h5:, "mesh", True), "w_n")


Best of luck!

written 12 months ago by Alexander G. Zimmerman  
That worked! Sorry I took so long to check and reply - thank you so much for your help!
written 11 months ago by Sophia Wright  
No worries; and you're welcome :) Glad to help.
written 11 months ago by Alexander G. Zimmerman  
12 months ago by
You can save numpy data with numpy.savetxt() and numpy.loadtxt().  You can save matlab files too :

I think you want to use HDF5 to save your tensors, as they save in parallel :

Note that you probably do not want to store the sparse assembled matrix -- the assembly of bilinear forms is rather fast -- but saving the mesh and any projected or interpolated expressions will save time.

Thank you for replying, I'm gonna try this soon
written 12 months ago by Sophia Wright  
the HDF5 approach worked - i marked Alexander's reply as the answer so anyone else that needs to do something similar can use the code in his reply, but your reply is correct too - thank you so much for your help
written 11 months ago by Sophia Wright  
If I have helped, that's what matters.  You might check this site for extra help too:
written 11 months ago by pf4d  
i'll look it up - thank you!
written 11 months ago by Sophia Wright  
Please login to add an answer/comment or follow this question.

Similar posts:
Search »