dolfinx.io.XDMFFile sensitive to functionspace definition

Dear all,

I understand that in fenicsx version 0.8.0, the function dolfinx.io.XDMFFile is sensitive to the functionspace definition. Here is my example code:

import dolfinx
import dolfinx.mesh as mesh

L = 1. # total length
d = L/10. # thickness
h = d/10. # size of a cell
my_domain = dolfinx.mesh.create_rectangle(comm=mpi4py.MPI.COMM_WORLD,
                            points=((0.0, -0.5*d), (L, 0.5*d)), n=(int(L/h), int(d/h)),
                            cell_type=dolfinx.mesh.CellType.triangle)
V = fem.functionspace(my_domain, ("DG", 0, (my_domain.geometry.dim, )))
u = dolfinx.fem.Function(V)
with dolfinx.io.XDMFFile(my_domain.comm, "out/solution.xdmf", "w") as file:
    file.write_mesh(my_domain)
    file.write_function(u)

If I change the functionspace definition to

V = fem.functionspace(my_domain, ("DG", 1, (my_domain.geometry.dim, )))

I got the following error message:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[7], line 14
     12 with dolfinx.io.XDMFFile(my_domain.comm, "out/solution.xdmf", "w") as file:
     13     file.write_mesh(my_domain)
---> 14     file.write_function(u)

File ~/anaconda3/envs/fenicsx-cism-2024/lib/python3.12/site-packages/dolfinx/io/utils.py:252, in XDMFFile.write_function(self, u, t, mesh_xpath)
    235 def write_function(
    236     self, u: Function, t: float = 0.0, mesh_xpath="/Xdmf/Domain/Grid[@GridType='Uniform'][1]"
    237 ):
    238     """Write function to file for a given time.
    239 
    240     Note:
   (...)
    250             XDMFFile.
    251     """
--> 252     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?

Alternatively, if I change it to

V = fem.functionspace(my_domain, ("Lagrange", 2, (my_domain.geometry.dim, )))

I get the following error message:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[6], line 14
     12 with dolfinx.io.XDMFFile(my_domain.comm, "out/solution.xdmf", "w") as file:
     13     file.write_mesh(my_domain)
---> 14     file.write_function(u)

File ~/anaconda3/envs/fenicsx-cism-2024/lib/python3.12/site-packages/dolfinx/io/utils.py:252, in XDMFFile.write_function(self, u, t, mesh_xpath)
    235 def write_function(
    236     self, u: Function, t: float = 0.0, mesh_xpath="/Xdmf/Domain/Grid[@GridType='Uniform'][1]"
    237 ):
    238     """Write function to file for a given time.
    239 
    240     Note:
   (...)
    250             XDMFFile.
    251     """
--> 252     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?

Note that changing the initial functionspace to “Lagrange” 1 does not generate any error.

Could someone give me some tips on this issue.
Many thanks in advance.

See:
http://jsdokken.com/FEniCS-workshop/src/outputting.html#outputting

In particular if you are using a Linear grid and want to use XDMFFile, you need to interpolate into Lagrange 1.

If you want to visualize higher order functions properly, use:

  • VTXWriter
    or
  • VTKFile
1 Like