This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

How to save FEniCS results without HDF5 support

0 votes

I'm working my way through the tutorial with Jupyter notebooks on MS Azure and have made my way to ft09_reaction_system.py. In order to run this as documented, one must save the results of the previous example, FENICS ft08_navier_stokes_cylinder.py as a TimeSeries. This doesn't work (nor does TimeSeriesHDF5, nor does HDF5File), because the version of FEniCS available through anaconda (which is what you get on Azure) was compiled without HDF5 support, as described here and here. On trying to find a workaround for this, I realized that, without HDF5 support, there is no obvious way to save the results of any FEniCS computation. At least, if there is, I failed to find it after hunting through the Programmer's Reference.

So, that's my question? Is there any way to save results to disk without HDF5 support?

Example notebook (with problematic code commented out): https://notebooks.azure.com/run/lavery-waterloo/starting?dest=/notebooks/FENICS%20ft08_navier_stokes_cylinder.ipynb

Thanks for any help.

Note: as a result of further work on this, the link above no longer leads to an illustration of the problem -- instead, it is now a working notebook.

asked May 1, 2017 by lavery FEniCS Novice (350 points)
edited May 16, 2017 by lavery

As far as I know it is impossibe, as vtu is used only for postprocessing. Have you checked xdmf format? It might help, but I am not sure. Best regards!

xmdf depends on HDF5, apparently.

1 Answer

0 votes

I now have a solution/workaround, simply saving the data from a fenics Vector as a numpy array.

I thought at first I would be able to solve the problem by saving data in FEniCS XML files, like this:

File('navier_stokes_cylinder/timepoint.xml.gz') << u_.vector()

This indeed works, and inspection of the XML file shows that it contains the right sort of information. However, I couldn't figure out any way to read the file back into a FEniCS Vector. In particular,

vector = Vector('navier_stokes_cylinder/timepoint.xml.gz')

just throws an exception. I tried several variations, but was unable to come up with anything that worked. Obviously I'm missing something here, since it seems unlikely that the developers created a way to save Vector's in XML files without providing any way to read those files, but I couldn't find it.

Eventually I wrote my own minimal TimeSeries class, which can be seen here. The next problem I ran into was copying numerical data into and out of Vectors. The data can be retrieved as a numpy array using u.vector().array(). (Here u is a Function.) That was easy, It took me a good long while to figure out that if I had a numpy array data, I could copy it back into a Function using a slice assignment:

u.vector()[:] = data

Obviously there are all sorts of potential problems with the order of the elements, but I assumed that if I created a FunctionSpace on exactly the same mesh in exactly the same way, the dofs would correspond, and that seems to work, or at least to give plausible results.

At any rate, here are my versions of the ft08 and ft09 demo problems from the tutorial.

answered May 16, 2017 by lavery FEniCS Novice (350 points)
edited May 16, 2017 by lavery

Hi,

if I created a FunctionSpace on exactly the same mesh in exactly the
same way, the dofs would correspond, and that seems to work

this is very fragile. I would be very careful...

You can save XDMFFile without HDF5, with ASCII "encoding" like this

import dolfin as d

mesh = d.UnitIntervalMesh(10)
V = d.FunctionSpace(mesh, "CG", 1)
f = d.Function(V)

with d.XDMFFile("f.xdmf") as save_file:
    save_file.write(f, d.XDMFFile.Encoding_ASCII)

The difference is, that XDMF file, which is just readable XML file, doesn't point to
additional .h5 HDF5 file. Raw data are included into XDMF file itself. It is a little bug, that XDMFFile doesn't default to ASCII when there is no HDF5 and you have to set it explicitly...

Anyway, this is only for writing. The only FEniCS way to read Function is via HDF5 interface (to my best knowledge).
But I am working on this issue and XDMF read/write in ASCII/HDF5 will hopefully work in 2 months ;)

this is very fragile. I would be very careful...

Yes. I'm not terribly concerned about it right now, though. I did it this way only because that's the way the tutorial is written. I don't expect to use this two-stage solution strategy for my real problems.

But I am working on this issue and XDMF read/write in ASCII/HDF5 will hopefully work in 2 months ;)

Great. I look forward to that. Thanks.

...