# Loading xdmf data back in

**URL:** <https://fenicsproject.discourse.group/t/loading-xdmf-data-back-in/1925>\
**Category:** I/O\
**Created:** [November 19, 2019, 1:45am UTC](https://fenicsproject.discourse.group/t/loading-xdmf-data-back-in/1925 "2019-11-19T01:45:48Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![gideonsimpson](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/gideonsimpson/32/561_2.png) [@gideonsimpson](https://fenicsproject.discourse.group/u/gideonsimpson)\
**Post date:** [November 19, 2019, 1:45am UTC](https://fenicsproject.discourse.group/t/loading-xdmf-data-back-in/1925/1 "2019-11-19T01:45:48Z")

</div>

This may not strictly be a fenics question, but suppose I run a time dependent problem, of the sort:

```auto
output = XDMFFile("out.xdmf")

for n in range(nmax):
# computational steps
    output.write(u,t)
output.close()

```

If I then read this in with paraview, it’s all as expected. But I was wondering how to read the data back in with python. I saw the post [here](https://fenicsproject.discourse.group/t/access-function-in-time-dependent-xdmffile/359/6), but I don’t quite see how to adapt it to what I’m doing.

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [November 19, 2019, 7:47am UTC](https://fenicsproject.discourse.group/t/loading-xdmf-data-back-in/1925/2 "2019-11-19T07:47:39Z")

</div>

You should use `write_checkpoint` and `read_checkpoint` as described in the previous post to read things back into FEniCS.

---

<div class="post-metadata">

**Author:** ![gideonsimpson](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/gideonsimpson/32/561_2.png) [@gideonsimpson](https://fenicsproject.discourse.group/u/gideonsimpson)\
**Post date:** [November 19, 2019, 1:46pm UTC](https://fenicsproject.discourse.group/t/loading-xdmf-data-back-in/1925/3 "2019-11-19T13:46:27Z")

</div>

Two follow up questions:

1. If I am fine reading the data in with Paraview (or some similar program), is there any reason I should stop using `write`?
2. I am still struggling to get the suggested approach working. Here is what a minimal example:

```auto
nx = ny = 8
mesh = UnitSquareMesh(nx, ny)
V = FunctionSpace(mesh, 'P', 1)

alpha = 3; beta = 1.2
f = Expression('1 + x[0]*x[0] + alpha*x[1]*x[1] + beta*t', degree=2, alpha=alpha, beta=beta, t=0)

f_out = XDMFFile("test.xdmf")

for j in range(5):
    t = float(j/5)
    f.t = t
    f_out.write_checkpoint(project(f,V), "f",t)

f_out.close()

f1 = Function(V)
f_in = XDMFFile("test.xdmf")

f_in.read_checkpoint(f1,"f",0) # this works fine
f_in.read_checkpoint(f1,"f",1) # this throws an error

```

The error that I get is:

```auto
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-48-ab851acfbb36> in <module>
----> 1 f_in.read_checkpoint(f1,"f",1)

RuntimeError: 

*** -------------------------------------------------------------------------
*** DOLFIN encountered an error. If you are not able to resolve this issue
*** using the information listed below, you can ask for help at
***
*** fenics-support@googlegroups.com
***
*** Remember to include the error message listed below and, if possible,
*** include a *minimal* running example to reproduce the error.
***
*** -------------------------------------------------------------------------
*** Error: Unable to reading data from XDMF file.
*** Reason: Storage format "" is unknown.
*** Where: This error was encountered inside XDMFFile.cpp.
*** Process: 0
*** 
*** DOLFIN version: 2019.1.0
*** Git changeset:  
*** -------------------------------------------------------------------------

```

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [November 19, 2019, 1:59pm UTC](https://fenicsproject.discourse.group/t/loading-xdmf-data-back-in/1925/4 "2019-11-19T13:59:49Z")

</div>

`write` saves the data at the vertices, efficiently projecting the solution to a CG-1 function space. `write_checkpoint` saves the data with the appropriate function space, and can therefore be read into dolfin again, to the appropriate function space.  
You are not saving multiple time steps to your file as you have not changed the append kwarg in write\_checkpoint (see: [https://fenicsproject.org/docs/dolfin/latest/cpp/de/d71/classdolfin\_1\_1XDMFFile.html#a9ef9031aec7bfec8f289d5210a1c811c](https://fenicsproject.org/docs/dolfin/latest/cpp/de/d71/classdolfin_1_1XDMFFile.html#a9ef9031aec7bfec8f289d5210a1c811c)). The following works:

```auto
from dolfin import *
nx = ny = 8 
mesh = UnitSquareMesh(nx, ny) 
V = FunctionSpace(mesh, 'P', 1) 
    
alpha = 3; beta = 1.2 
f = Expression('1 + x[0]*x[0] + alpha*x[1]*x[1] + beta*t', degree=2, alpha=alpha, beta=beta, t=0) 
 
f_out = XDMFFile("test.xdmf") 
f_out.write_checkpoint(project(f, V), "f", 0, XDMFFile.Encoding.HDF5, False) # Not appending to file
for j in range(1,5): 
    t = float(j/5) 
    f.t = t 
f_out.write_checkpoint(project(f,V), "f",t, XDMFFile.Encoding.HDF5, True) #appending to file
    
f_out.close() 
     
f1 = Function(V) 
f_in = XDMFFile("test.xdmf") 
   
f_in.read_checkpoint(f1,"f",0) 
f_in.read_checkpoint(f1,"f",1)   

```

---

<div class="post-metadata">

**Author:** ![gideonsimpson](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/gideonsimpson/32/561_2.png) [@gideonsimpson](https://fenicsproject.discourse.group/u/gideonsimpson)\
**Post date:** [November 20, 2019, 2:36am UTC](https://fenicsproject.discourse.group/t/loading-xdmf-data-back-in/1925/5 "2019-11-20T02:36:10Z")

</div>

Yes, that now works. One question: output in generated with `write_checkpoint` now doesn’t seem to be readable by Paraview. Is this to be expected, or is there some way to make it compatible with both?

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [November 20, 2019, 6:18am UTC](https://fenicsproject.discourse.group/t/loading-xdmf-data-back-in/1925/6 "2019-11-20T06:18:36Z")

</div>

then your paraview is out of data. You Need to use Paraview \>=5.5.0

---

<div class="post-metadata">

**Author:** ![RemDelaporteMathurin](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/remdelaportemathurin/32/4543_2.png) [@RemDelaporteMathurin](https://fenicsproject.discourse.group/u/RemDelaporteMathurin)\
**Post date:** [November 20, 2019, 11:23am UTC](https://fenicsproject.discourse.group/t/loading-xdmf-data-back-in/1925/7 "2019-11-20T11:23:31Z")

</div>

is there a way with the method `read_checkpoint` to read the actual time of the i\_th time step ? let’s say for example you want to recreate the exact same xdmf file ?

```python
from fenics import *

file_in = XDMFFile("in.xdmf")
file_out = XDMFFile("out.xdmf")

mesh = Mesh()
file_in.read(mesh)

V = FunctionSpace(mesh, "P", 1)
u = Function(V)

for i in range(0, 10):
    file_in.read_checkpoint(u, "u", i)
    t = i # ?????
    file_out.write_checkpoint(u, "u", i)
    export_file.write_checkpoint(u, "u", t)

```

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [November 20, 2019, 12:19pm UTC](https://fenicsproject.discourse.group/t/loading-xdmf-data-back-in/1925/8 "2019-11-20T12:19:25Z")

</div>

As you can see from the documentation: [https://fenicsproject.org/docs/dolfin/latest/cpp/de/d71/classdolfin\_1\_1XDMFFile.html#aa86a07b92188a2da32d1a0e128106c91](https://fenicsproject.org/docs/dolfin/latest/cpp/de/d71/classdolfin_1_1XDMFFile.html#aa86a07b92188a2da32d1a0e128106c91)  
That is not possible, as read\_checpoint only takes in a counter.

---

<div class="post-metadata">

**Author:** ![gideonsimpson](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/gideonsimpson/32/561_2.png) [@gideonsimpson](https://fenicsproject.discourse.group/u/gideonsimpson)\
**Post date:** [November 20, 2019, 5:06pm UTC](https://fenicsproject.discourse.group/t/loading-xdmf-data-back-in/1925/9 "2019-11-20T17:06:04Z")

</div>

I’m using 5.7.0, and I’m seeing that some of the fields are listed as `partial`. The consequence seems to be that I don’t have the field at every time at which I performed a `write_checkpoint`. See attached screen capture.

![31%20PM](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/1X/7c194808686ada12881826eff4e7274be4dca69a.png)

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [November 20, 2019, 6:10pm UTC](https://fenicsproject.discourse.group/t/loading-xdmf-data-back-in/1925/10 "2019-11-20T18:10:34Z")

</div>

Are you using the append functionality. I see none of the issues with the following code:

```auto
from dolfin import *

nx = ny = 8
mesh = UnitSquareMesh(nx, ny)
V = FunctionSpace(mesh, 'P', 1)

alpha = 3; beta = 1.2
f = Expression('1 + x[0]*x[0] + alpha*x[1]*x[1] + beta*t', degree=2, alpha=alpha, beta=beta, t=0)
f_out = XDMFFile("test.xdmf")

f_out.write_checkpoint(project(f,V), "f", 0.0, XDMFFile.Encoding.HDF5, False)

for j in range(1,5):
    t = float(j/5)
    f.t = t
   
    f_out.write_checkpoint(project(f,V), "f", t, XDMFFile.Encoding.HDF5, True)

f_out.close()

f1 = Function(V)
f_in = XDMFFile("test.xdmf")

f_in.read_checkpoint(f1,"f",0) 
f_in.read_checkpoint(f1,"f",1) 
f_in.close()

```

---

<div class="post-metadata">

**Author:** ![gideonsimpson](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/gideonsimpson/32/561_2.png) [@gideonsimpson](https://fenicsproject.discourse.group/u/gideonsimpson)\
**Post date:** [November 20, 2019, 9:03pm UTC](https://fenicsproject.discourse.group/t/loading-xdmf-data-back-in/1925/11 "2019-11-20T21:03:40Z")

</div>

Yeah, my mistake is I was appending to an existing file. Once I put `False` in the first call, it cleared up.

---

<div class="post-metadata">

**Author:** ![a516095046](https://avatars.discourse-cdn.com/v4/letter/a/e36b37/32.png) [@a516095046](https://fenicsproject.discourse.group/u/a516095046)\
**Post date:** [February 16, 2021, 4:50am UTC](https://fenicsproject.discourse.group/t/loading-xdmf-data-back-in/1925/12 "2021-02-16T04:50:41Z")

</div>

Hello Jorgen, is there still any way to read the data back in when they were already written using `write` instead of `write_checkpoint`?

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [February 16, 2021, 9:05am UTC](https://fenicsproject.discourse.group/t/loading-xdmf-data-back-in/1925/13 "2021-02-16T09:05:40Z")

</div>

No, you cannot read in data written with `XDMFFile.write`.

---

<div class="post-metadata">

**Author:** ![Ben\_Bird](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/ben_bird/32/3204_2.png) [@Ben\_Bird](https://fenicsproject.discourse.group/u/Ben_Bird)\
**Post date:** [February 7, 2022, 11:13am UTC](https://fenicsproject.discourse.group/t/loading-xdmf-data-back-in/1925/14 "2022-02-07T11:13:31Z")

</div>

Hi there,  
I have a problem relating to this topic. My aim is to get a time-averaged velocity field for the vortex shedding behind a cylinder. I have run a 2D Navier-Stokes simulation in FEniCS until a quasi-steady shedding regime has been established. Once established, I saved the data into an XDMF file using write\_checkpoint for 30 shedding cycles. I’ve then used Matlab to get an average of the ‘vector’ dataset in the hdf5 files over the 30 shedding cycles. I saved this new average ‘vector’ dataset into a separate xdmf file using the same domain data (dofs, mesh geom, mesh topology etc.) that was in the original xdmf file created by initial simulation. All of the groups, datasets and function have the same names as those prescribed in the ‘write\_checkpoint’ function, however, I am having trouble loading this time-averaged velocity field back into FEniCS as a function using read\_checkpoint. Any help with this topic is much appreciated, is it even possible??  
Thanks,  
Ben

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [February 7, 2022, 11:37am UTC](https://fenicsproject.discourse.group/t/loading-xdmf-data-back-in/1925/15 "2022-02-07T11:37:17Z")

</div>

You would need to make a minimal example, reproducing your error for anyone to be of any guidance. What error do you get once you use `read_checkpoint`?

---

<div class="post-metadata">

**Author:** ![Ben\_Bird](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/ben_bird/32/3204_2.png) [@Ben\_Bird](https://fenicsproject.discourse.group/u/Ben_Bird)\
**Post date:** [February 7, 2022, 2:14pm UTC](https://fenicsproject.discourse.group/t/loading-xdmf-data-back-in/1925/16 "2022-02-07T14:14:30Z")

</div>

Hi Jorgen,  
Thanks for the swift response. I’ve put together a minimal example, although it is probably easiest if I attach my mesh file and corresponding mean velocity file, since I have already done the Matlab manipulation mentioned above (is this possible). Here is the what I run:

```auto
from dolfin import *
import numpy as np

mesh = Mesh()

with XDMFFile("hex_mesh_bigdom.xdmf") as infile:
    infile.read(mesh)
    
V = VectorFunctionSpace(mesh, 'P', 2)
Q = FunctionSpace(mesh, 'P', 1)

f1 = Function(V)

f_in = XDMFFile('hex_velocity_MEAN_u.xdmf')

umean = f_in.read_checkpoint(f1, "u_mean", 0)

```

However, I get the following:

```auto
>>> type(umean)
<class 'NoneType'>

```

Even though everything in the xdmf and h5 files containing the mean velocity field has the same format as the files produced when running the original ‘write\_checkpoint’ command.

Happy to send the input files over to see if you can reproduce this error.

Thanks,  
Ben

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [February 7, 2022, 2:54pm UTC](https://fenicsproject.discourse.group/t/loading-xdmf-data-back-in/1925/17 "2022-02-07T14:54:05Z")

</div>

I would suggest you put them in a public location (for instance github gist) or google drive and share the link here.  
Please supply both the original velocity file, and the manipulated file.

---

<div class="post-metadata">

**Author:** ![Ben\_Bird](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/ben_bird/32/3204_2.png) [@Ben\_Bird](https://fenicsproject.discourse.group/u/Ben_Bird)\
**Post date:** [February 7, 2022, 3:36pm UTC](https://fenicsproject.discourse.group/t/loading-xdmf-data-back-in/1925/18 "2022-02-07T15:36:50Z")

</div>

The mesh, original velocity and time average velocity files in the link below:

[https://drive.google.com/drive/folders/1s2ItHUF2Y4SaQDQs5KK-Kxt9J3XB8Lix?usp=sharing](https://drive.google.com/drive/folders/1s2ItHUF2Y4SaQDQs5KK-Kxt9J3XB8Lix?usp=sharing)

It it worth noting the data was only saved every 22 time steps in the original data, this just helped save on memory/simulation time.

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [February 7, 2022, 4:24pm UTC](https://fenicsproject.discourse.group/t/loading-xdmf-data-back-in/1925/19 "2022-02-07T16:24:58Z")

</div>

> [@Ben\_Bird](#):
>
> `umean = f_in.read_checkpoint(f1, "u_mean", 0)`

You should note that you read data into `f1` here, thus you can check the output of `f1` pre and post reading the data:

```python
print(f1.vector().get_local())
f_in.read_checkpoint(f1, "u_mean", 0)
print(f1.vector().get_local())

```

As you can see, the data is successfully read in

---

<div class="post-metadata">

**Author:** ![Ben\_Bird](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/ben_bird/32/3204_2.png) [@Ben\_Bird](https://fenicsproject.discourse.group/u/Ben_Bird)\
**Post date:** [February 7, 2022, 4:41pm UTC](https://fenicsproject.discourse.group/t/loading-xdmf-data-back-in/1925/20 "2022-02-07T16:41:12Z")

</div>

Perfect, thank you for your help. I thought it would be reading the function into the variable umean. All works well know.

[Next page](https://fenicsproject.discourse.group/t/loading-xdmf-data-back-in/1925.md?page=2)
