I’ve been searching a while for how to write and read a function from XDMF/HDF5 files in dolfin-x, but haven’t found a suitable example.
Consider the following MWE which first writes a function to a file and then (fails) to read back in the function from the file:
from dolfinx import * from dolfinx.io import XDMFFile from dolfinx.cpp.mesh import CellType from mpi4py import MPI import numpy as np comm = MPI.COMM_WORLD mesh = BoxMesh(comm, [np.array([0.0, 0.0, 0.0]),np.array([2.0, 1.0, 1.0])], [12, 12, 12], CellType.tetrahedron, dolfinx.cpp.mesh.GhostMode.none) V = VectorFunctionSpace(mesh, ("DG", 0)) f = Function(V, name="fiber") # write function to file with XDMFFile(comm,'tmp.xdmf','w') as ofile: ofile.write_mesh(mesh) ofile.write_function(f) encoding=XDMFFile.Encoding.HDF5 f2 = Function(V) # read function from file with XDMFFile(comm,'tmp.xdmf','r',encoding=encoding) as ifile: #f2 = ifile.read_function(V,"fiber") ifile.read_checkpoint(f2,"fiber",1)
The writing of the function and the mesh work, but the reading back in to another function fails. The read_checkpoint and the read_function seem to have been removed in dolfin-x? Is there any alternative?
What I could do is reading with h5py:
import h5py with h5py.File('tmp.h5', 'r') as ifile: y = ifile['/Function/fiber/0']
however the output y is not a function and would have to be transformed/copied to a function (which could lack in parallel).
Hope someone has a nicer solution!