Problem for writing function with XDMFFile

Hello!

When running the following block of code in Colab, the resulting .xdmf file makes Paraview crash when opened:

domain = dolfinx.mesh.create_unit_cube(MPI.COMM_WORLD, 10, 10, 10)
V = fem.functionspace(domain, ("Lagrange", 1))
u = fem.Function(V)

with XDMFFile(MPI.COMM_WORLD, "foo1.xdmf", "w") as xdmf:
  xdmf.write_mesh(domain)
  xdmf.write_function(u)

If I try to open the same file with meshio using meshio.xdmf.read("foo1.xdmf") , I get the following error:

---------------------------------------------------------------------------
ReadError                                 Traceback (most recent call last)
<ipython-input-9-eb0f777a23e6> in <cell line: 1>()
----> 1 meshio.xdmf.read("foo1.xdmf")

2 frames
/usr/local/lib/python3.10/dist-packages/meshio/xdmf/main.py in read_xdmf3(self, root)
    240         grids = list(domain)
    241         if len(grids) != 1:
--> 242             raise ReadError("XDMF reader: Only supports one grid right now.")
    243         grid = grids[0]
    244         if grid.tag != "Grid":

ReadError: XDMF reader: Only supports one grid right now.

For importing packages, I run the following:

try:
    import gmsh
except ImportError:
    !wget "https://fem-on-colab.github.io/releases/gmsh-install.sh" -O "/tmp/gmsh-install.sh" && bash "/tmp/gmsh-install.sh"
    import gmsh

try:
    import dolfinx
except ImportError:
    !wget "https://fem-on-colab.github.io/releases/fenicsx-install-real.sh" -O "/tmp/fenicsx-install.sh" && bash "/tmp/fenicsx-install.sh"
    import dolfinx

!pip install meshio
import meshio

import ufl

from dolfinx.io import XDMFFile
from dolfinx import io, cpp, fem, mesh, plot
from mpi4py import MPI

I can’t seem to get where this issue is coming from. How can I fix this? Thanks in advance.

This is a limitation of meshio, and how it handles xdmf-files.

The XDMFFile in question looks like:

<?xml version="1.0"?>
<!DOCTYPE Xdmf SYSTEM "Xdmf.dtd" []>
<Xdmf Version="3.0" xmlns:xi="https://www.w3.org/2001/XInclude">
  <Domain>
    <Grid Name="mesh" GridType="Uniform">
      <Topology TopologyType="Tetrahedron" NumberOfElements="6000" NodesPerElement="4">
        <DataItem Dimensions="6000 4" NumberType="Int" Format="HDF">foo1.h5:/Mesh/mesh/topology</DataItem>
      </Topology>
      <Geometry GeometryType="XYZ">
        <DataItem Dimensions="1331 3" Format="HDF">foo1.h5:/Mesh/mesh/geometry</DataItem>
      </Geometry>
    </Grid>
    <Grid Name="f" GridType="Collection" CollectionType="Temporal">
      <Grid Name="f" GridType="Uniform">
        <xi:include xpointer="xpointer(/Xdmf/Domain/Grid[@GridType='Uniform'][1]/*[self::Topology or self::Geometry])" />
        <Time Value="0" />
        <Attribute Name="f" AttributeType="Scalar" Center="Node">
          <DataItem Dimensions="1331 1" Format="HDF">foo1.h5:/Function/f/0</DataItem>
        </Attribute>
      </Grid>
    </Grid>
  </Domain>
</Xdmf>

As you can see, there are two grids in this file, one for the mesh itself, and one for the function, which in turn uses x-pointers to get the geometry and topology from the actual mesh when visualizing.

1 Like