# Cannot write function to XDMF with DG elements

**URL:** <https://fenicsproject.discourse.group/t/cannot-write-function-to-xdmf-with-dg-elements/12442>\
**Category:** General\
**Created:** [October 12, 2023, 2:24pm UTC](https://fenicsproject.discourse.group/t/cannot-write-function-to-xdmf-with-dg-elements/12442 "2023-10-12T14:24:48Z")\
**Posts on this page:** 19\
**Page:** 1

<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:** [October 12, 2023, 2:24pm UTC](https://fenicsproject.discourse.group/t/cannot-write-function-to-xdmf-with-dg-elements/12442/1 "2023-10-12T14:24:48Z")

</div>

Hi!

I’m trying to export a function from a DG function space to XDMF but obtain the following error:

```auto
  File "/home/remidm/festimx/mwe.py", line 14, in <module>
    xdmf_file.write_function(u)
  File "/home/remidm/miniconda3/envs/dolfinx/lib/python3.11/site-packages/dolfinx/io/utils.py", line 235, in write_function
    super().write_function(getattr(u, "_cpp_object", u), t, mesh_xpath)
RuntimeError: Function and Mesh dof layouts do not match. Maybe the Function needs to be interpolated?

```

Obtained with the following MWE:

```python
from dolfinx import mesh, fem
from mpi4py import MPI
from dolfinx.io import XDMFFile

msh = mesh.create_unit_square(MPI.COMM_WORLD, 8, 8)

V = fem.FunctionSpace(msh, ("DG", 1))

u = fem.Function(V)

xdmf_file = XDMFFile(MPI.COMM_WORLD, "u.xdmf", "w")
xdmf_file.write_mesh(msh)
xdmf_file.write_function(u)

```

Works just fine with CG elements.

This [used to work fine even with DG](https://fenicsproject.discourse.group/t/imposing-a-discontinuity-at-interface-using-dg-method/11780/2) so maybe it is a recent change?

Any idea?  
Thanks!

---

<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:** [October 12, 2023, 2:32pm UTC](https://fenicsproject.discourse.group/t/cannot-write-function-to-xdmf-with-dg-elements/12442/2 "2023-10-12T14:32:51Z")

</div>

> [@RemDelaporteMathurin](#):
>
> ```auto
> V = fem.FunctionSpace(msh, ("DG", 1))
> 
> u = fem.Function(V)
> 
> xdmf_file = XDMFFile(MPI.COMM_WORLD, "u.xdmf", "w")
> xdmf_file.write_mesh(msh)
> xdmf_file.write_function(u)
> 
> ```

This is an explicit change to highlight the limitations of the `XDMF` file format, as in comparison to VTX.

In DOLFINx\<0.7.0 any function was interpolated into the function space of the mesh geometry (N-th order Lagrange space, N usually 1).  
Now you have to interpolate this function into (“CG”, 1) explicitly to save it to xdmf, or use the `VTXWriter`, which supports arbitrary order “CG”/“DG” elements.

---

<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:** [October 12, 2023, 2:34pm UTC](https://fenicsproject.discourse.group/t/cannot-write-function-to-xdmf-with-dg-elements/12442/3 "2023-10-12T14:34:04Z")

</div>

Understood!  
I suppose we would need a write\_checkpoint method for this then?

---

<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:** [October 12, 2023, 2:37pm UTC](https://fenicsproject.discourse.group/t/cannot-write-function-to-xdmf-with-dg-elements/12442/4 "2023-10-12T14:37:54Z")

</div>

We are not aiming for long term support of XDMF, as it is no longer actively developed ([Xdmf / Xdmf · GitLab](https://gitlab.kitware.com/xdmf/xdmf), no commit in three years), and thus we are not aiming to add checkpointing to it.

I was working on an ADIOS2 based checkpoint/visualization code, but hit some speed-bumps, such as:

> <https://github.com/ornladios/ADIOS2/issues/3688>
>
> Currently there are some limitations to the VTK unstructured-grid reader in Para…view
> \> Unstructured Grid, vtu, is supported with ADIOS2 Local Arrays Variables only (https://adios2.readthedocs.io/en/latest/ecosystem/visualization.html)
> 
> This means that the data is written into blocks, making it challenging to read this data in again on a different number of processes.
> 
> A motivating example would be to write a mesh to file:
> 1. We write the mesh on N processes
> 2. We want to read the mesh in on M processes, with \`M!=N\`.
> To do so, I would use \`Global Array\` or a \`JoinedArray\` (https://adios2.readthedocs.io/en/stable/components/components.html)
> 
> This would be extremely beneficial for using VTX as a default mesh format.
> A reference implementation of how I want this to work in DOLFINx (previously using local arrays) can be found at: https://github.com/FEniCS/dolfinx/pull/2717

and thus I don’t have a good proposal for a format that can be both used as a checkpoint and be visualized with tools such as Paraview.

I’ve got checkpoint functionality at: [GitHub - jorgensd/adios4dolfinx: Interface of ADIOS2 for DOLFINx](https://github.com/jorgensd/adios4dolfinx)  
which I aim to make 0.7.0 compatible over the weekend.

---

<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:** [October 12, 2023, 2:39pm UTC](https://fenicsproject.discourse.group/t/cannot-write-function-to-xdmf-with-dg-elements/12442/5 "2023-10-12T14:39:54Z")

</div>

Gotcha. I’ll stick with VTXWriter for now then. Not sure how to read it with paraview though. Is there any demo available?

---

<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:** [October 12, 2023, 3:10pm UTC](https://fenicsproject.discourse.group/t/cannot-write-function-to-xdmf-with-dg-elements/12442/6 "2023-10-12T15:10:36Z")

</div>

VTXWriter created a .bp folder, that you can open in paraview. It should then Give you the option og fides or VTX (choose Vtx)

---

<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:** [October 12, 2023, 3:13pm UTC](https://fenicsproject.discourse.group/t/cannot-write-function-to-xdmf-with-dg-elements/12442/7 "2023-10-12T15:13:44Z")

</div>

Hm I have the folder but it’s not opening in paraview 5.11.1, just hangs forever.

This is how I export it, does it seem right?

```python
from dolfinx import mesh, fem
from mpi4py import MPI
from dolfinx.io import XDMFFile
from dolfinx.io import VTXWriter

msh = mesh.create_unit_square(MPI.COMM_WORLD, 8, 8)

V = fem.FunctionSpace(msh, ("DG", 1))

u = fem.Function(V)

V_CG1 = fem.FunctionSpace(msh, ("CG", 1))

u_cg = fem.Function(V_CG1)
u_cg.interpolate(u)

xdmf_file = XDMFFile(MPI.COMM_WORLD, "u.xdmf", "w")
xdmf_file.write_mesh(msh)
xdmf_file.write_function(u_cg)

with VTXWriter(msh.comm, "out.bp", [u]) as vtx:
    vtx.write(0.0)

```

---

<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:** [October 12, 2023, 4:20pm UTC](https://fenicsproject.discourse.group/t/cannot-write-function-to-xdmf-with-dg-elements/12442/8 "2023-10-12T16:20:01Z")

</div>

> [@RemDelaporteMathurin](#):
>
> ```auto
> with VTXWriter(msh.comm, "out.bp", [u]) as vtx:
> vtx.write(0.0)
> 
> ```

Try

```python
with VTXWriter(msh.comm, "out.bp", [u], engine="BP4") as vtx:
    vtx.write(0.0)

```

after removing the old directory. Paraview doesn’t support BP5 yet, Which is the default engine for adios2

---

<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:** [October 12, 2023, 4:38pm UTC](https://fenicsproject.discourse.group/t/cannot-write-function-to-xdmf-with-dg-elements/12442/9 "2023-10-12T16:38:25Z")

</div>

Progress: now Paraview doesn’t hang anymore but I have this error

```auto
ERROR: In vtkADIOS2CoreImageReader.cxx, line 648
vtkADIOS2CoreImageReader (000002A5F7ABD6D0): Can not use the dimension of array NumberOfEntities to set the dimension of image data. Its size is neither 2 nor 3

```

---

<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:** [October 12, 2023, 4:44pm UTC](https://fenicsproject.discourse.group/t/cannot-write-function-to-xdmf-with-dg-elements/12442/10 "2023-10-12T16:44:27Z")

</div>

There should be another reader there (that is not image data). Do you Get the option: ADIOS2VTXReader  
([paraview.simple.ADIOS2VTXReader — ParaView/Python 5.8.0-RC3-1-g56631fdd9a documentation](https://kitware.github.io/paraview-docs/v5.8.0/python/paraview.simple.ADIOS2VTXReader.html))

---

<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:** [October 12, 2023, 4:48pm UTC](https://fenicsproject.discourse.group/t/cannot-write-function-to-xdmf-with-dg-elements/12442/11 "2023-10-12T16:48:41Z")

</div>

Hm, I wasn’t offered the choice of reader.

This is the list of readers I find when going in OpenFile :

 ![image](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/c/c69e1b9d3e0e4cc2eb2a2e649b97912d765314a0.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:** [October 12, 2023, 5:01pm UTC](https://fenicsproject.discourse.group/t/cannot-write-function-to-xdmf-with-dg-elements/12442/12 "2023-10-12T17:01:28Z")

</div>

That is strange. How did you install paraview and what kind of system are you using?

---

<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:** [October 12, 2023, 5:08pm UTC](https://fenicsproject.discourse.group/t/cannot-write-function-to-xdmf-with-dg-elements/12442/13 "2023-10-12T17:08:49Z")

</div>

I’m on Windows, I installed it on the [Paraview download page](https://www.paraview.org/download/) (v5.11.1)

---

<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:** [October 12, 2023, 5:31pm UTC](https://fenicsproject.discourse.group/t/cannot-write-function-to-xdmf-with-dg-elements/12442/14 "2023-10-12T17:31:53Z")

</div>

I would Ask on the paraview discourse:

> **[ParaView](https://discourse.paraview.org/)**
>
> A community place to discuss ParaView and related software

as I dont have a Windows computer to test this on

---

<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:** [October 12, 2023, 5:50pm UTC](https://fenicsproject.discourse.group/t/cannot-write-function-to-xdmf-with-dg-elements/12442/15 "2023-10-12T17:50:26Z")

</div>

Will do, I have the same bug in paraview 5.11.2

---

<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:** [October 12, 2023, 6:01pm UTC](https://fenicsproject.discourse.group/t/cannot-write-function-to-xdmf-with-dg-elements/12442/16 "2023-10-12T18:01:39Z")

</div>

@dokken It seems the ADIOS2VTX reader has been removed from the docs of Paraview after 5.8

This is 5.11:  
[https://kitware.github.io/paraview-docs/v5.11.0/python/paraview.servermanager\_proxies.html](https://kitware.github.io/paraview-docs/v5.11.0/python/paraview.servermanager_proxies.html)

---

<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:** [October 12, 2023, 8:28pm UTC](https://fenicsproject.discourse.group/t/cannot-write-function-to-xdmf-with-dg-elements/12442/17 "2023-10-12T20:28:13Z")

</div>

I’m using 5.11-r2, and as you can see from the screenshot below, I got access to `ADIOS2VTXReader`:

 ![image](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/e/e6595c3d1690857aaa87ae7f517582019777b0c1.png)  
This is running on a ubuntu 20.04 machine, using the binary download from the Paraview webpage.

---

<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:** [October 12, 2023, 11:02pm UTC](https://fenicsproject.discourse.group/t/cannot-write-function-to-xdmf-with-dg-elements/12442/18 "2023-10-12T23:02:15Z")

</div>

It would seem it has to do with[needing the MPI version](https://discourse.paraview.org/t/cannot-open-bp4-file/13033/5?u=remdelaportemathurin)

Is this the MPI version?

---

<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:** [October 13, 2023, 2:00am UTC](https://fenicsproject.discourse.group/t/cannot-write-function-to-xdmf-with-dg-elements/12442/19 "2023-10-13T02:00:50Z")

</div>

Yes. As adios2 features alot of MPI functionality.
