Saving an xdmf file - dolfinx v 0.7.0

Suppose I run the Navier Stokes equation given here: Implementation — FEniCSx tutorial (jsdokken.com)

Then I have the answer u_n as :
Coefficient(FunctionSpace(Mesh(blocked element (Basix element (P, triangle, 1, gll_warped, unset, False), (2,)), 0), VectorElement(FiniteElement('Lagrange', triangle, 2), dim=2)), 0)

And its mesh is given by:
mesh = <dolfinx.mesh.Mesh at 0x7f56de19f5e0>

Now I was going to save the value of u_n . I have tried multiple ways and the closest I could get was:

from dolfinx import io

with io.XDMFFile(mesh.comm, "u_n.xdmf", "w") as xdmf_file:
    xdmf_file.write_mesh(mesh)
    xdmf_file.write_function(u_n)

But this still gives me the following error:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[5], line 5
      3 with io.XDMFFile(mesh.comm, "u_n.xdmf", "w") as xdmf:
      4     xdmf.write_mesh(mesh)
----> 5     xdmf.write_function(u_n)

File /usr/local/dolfinx-real/lib/python3.10/dist-packages/dolfinx/io/utils.py:235, in XDMFFile.write_function(self, u, t, mesh_xpath)
    219 def write_function(self, u: Function, t: float = 0.0, mesh_xpath="/Xdmf/Domain/Grid[@GridType='Uniform'][1]"):
    220     """Write function to file for a given time.
    221 
    222     Note:
   (...)
    233 
    234     """
--> 235     super().write_function(getattr(u, "_cpp_object", u), t, mesh_xpath)

RuntimeError: Degree of output Function must be same as mesh degree. Maybe the Function needs to be interpolated?

Could you please help me out with this. I don’t need to strictly use this method. I simply want to save the u_n , p_n and mesh separately. And then load it to my notebook later.

I use dolfinx through a docker container and use jupyter notebook to run the codes.

Please search for error messages on the forum prior to posting questions that have already been answered.
See:

or for the tutorial up to date with the main branch see

1 Like

Hi @dokken Thank you for the response. I was trying so many things the past two days but I still have some issues with that.

My first observation

from dolfinx import io
with io.XDMFFile(mesh.comm, "u_n_test1.xdmf", "w") as xdmf:
    xdmf.write_mesh(mesh)
    xdmf.write_function(u_n)

Does give an error as I mentioned above, but it creates two files u_n_test1.xdmf as well as u_n_test1.h5

My second observation
With the changes suggested in the given links: I used this code and it doesn’t have an error.

with io.VTKFile(mesh.comm, "u_n_test1_from_vtk.xdmf", "w") as xdmf_file:
    xdmf_file.write_mesh(mesh)
    xdmf_file.write_function(u_n)


But now I only have u_n_test1_from_vtk.xdmf along with some .vtu files. (I don’t really understand what those .vtu files are…)

My main goal

Is to upload what I have saved externally, that is:
u_n = Coefficient(FunctionSpace(Mesh(blocked element (Basix element (P, triangle, 1, gll_warped, unset, False), (2,)), 1), VectorElement(FiniteElement('Lagrange', triangle, 2), dim=2)), 5)

and the
mesh =<dolfinx.mesh.Mesh at 0x7f39cef63ac0>

And the closest I could get is this:


with io.XDMFFile(MPI.COMM_WORLD, "u_n_test1_from_vtk.xdmf", "r") as xdmf_file_read:
    read_mesh = xdmf_file_read.read_mesh(mesh)
    read_u_h = xdmf_file_read.read_function(u_n)

And this gave me an error: Unable to open HDF5 file. File u_n_test1_from_vtk.h5 does not exist.

When I looked through the discourse for this error, I went down on a rabbit hole of about meshio but I don’t think that supports the mesh data we have in the Navier Stokes solver given in the tutorial: Implementation — FEniCSx tutorial (jsdokken.com)

Could you please give me a help if you have time. This problem really causing me a lot of issues…

This is expected, as the default format of XDMF is hdf5 (a binary format suitable for parallel computing).
If you want to store just a single file, you can change the encoding to dolfinx.io.XDMFFile.Encoding.ASCII (it is a keyword arg to the init function of XDMFFile).

You are using the wrong extension for the file format VTKFile as it should be PVD.

You need to use my code adios4dolfinx (GitHub - jorgensd/adios4dolfinx: Interface of ADIOS2 for DOLFINx) which is installable through pypi for function checkpointing.
See for instance: Parallel function saving and reading it back using PETSc or adios4dolfinx - #15 by dokken
for a description of how to use it.

1 Like

Hi @dokken thank you for the suggestion. I updated my docker image to the version 0.7.3:

DOLFINx version: 0.7.3 based on GIT commit: 25db9a743ef95a78f686f05e38607aa18281b0d6

And then I installed adios4dolfinx
Now to save the files externally, I followed both Parallel function saving and reading it back using PETSc or adios4dolfinx - dolfinx - FEniCS Project as well as checkpointing slides (jsdokken.com)

Quick reminder: I’m solving the Navier stokes solver in the FEniCSx tutorial: Test problem 1: Channel flow (Poiseuille flow) — FEniCSx tutorial (jsdokken.com)

Where
mesh = create_unit_square(MPI.COMM_WORLD, 10, 10)

And u_n is the solution constructed as:

v_cg2 = VectorElement("Lagrange", mesh.ufl_cell(), 2)
V = FunctionSpace(mesh, v_cg2)

u_n = Function(V)
u_n.name = "u_n"

The final u_n looks like:

u_n = Coefficient(FunctionSpace(Mesh(blocked element (Basix element (P, triangle, 1, gll_warped, unset, False), (2,)), 0), VectorElement(FiniteElement('Lagrange', triangle, 2), dim=2)), 0)

So, to save the file, I used:


import adios4dolfinx as adx
from pathlib import Path

checkpoint_file = Path("function_checkpoint.bp")
adx.write_mesh(mesh, checkpoint_file, engine="BP4")
adx.write_function(u_n, checkpoint_file, engine="BP4")

Now, to read the file I used:

checkpoint_file = Path("function_checkpoint.bp")

domain = adx.read_mesh(MPI.COMM_WORLD, checkpoint_file, "BP4", dolfinx.mesh.GhostMode.none)
v_cg2 = VectorElement("Lagrange", domain.ufl_cell(), 2)
V = FunctionSpace(domain, v_cg2)
u_new = Function(V)
u_new.name = "u_n"

adx.read_function(u_new, checkpoint_file, "BP4")

Please read carefully through the example:

I.e. the mesh and function must be saved to the same file:

   V = dolfinx.fem.functionspace(mesh, el)
    u = dolfinx.fem.Function(V)
    u.interpolate(f)
    adios4dolfinx.write_mesh(mesh, path_out, engine="BP4")
    adios4dolfinx.write_function(u, path_out, engine="BP4")
1 Like

Got it!
Sorry for asking those basic questions. With your help, I was able to work on it and I edited my previous question with the correct approach.

Thanks a lot and this was really helpful!!!