I am trying to load in a .xdmf and .h5 file that have solution data. My code runs without errors but when I print out the velocity field it is only zeros. I have looked at the solution in Paraview, and my solver worked, the solution is nonzero. How do I correctly read in a function space/solution from a .xdmf file?
Below is my code.
import numpy as np
from skimage import io
import skimage as sk
import scipy.ndimage as ndimage
from rdp import rdp
import os
import dolfinx
import dolfinx.io
import dolfinx.fem
from mpi4py import MPI
import sys
import h5py
comm = MPI.COMM_WORLD
img_fname = sys.argv[1] # File name of input image
solname_xdmf = sys.argv[2] # File name of solution (.xdmf file)
solname_h5 = sys.argv[3] # File name of solution (.h5 file)
def read_sol_file(comm, sol_fname_xdmf, sol_fname_h5):
with dolfinx.io.XDMFFile(comm, sol_fname_xdmf, "r") as infile:
# Read the mesh
mesh = infile.read_mesh(name="mesh")
mesh.topology.create_connectivity(mesh.topology.dim, mesh.topology.dim - 1)
# Create a function space (this needs to match the space used in the saved file)
V = dolfinx.fem.functionspace(mesh, ("Lagrange", 1))
# Create a function in the space
u = dolfinx.fem.Function(V)
# --- Load function values from HDF5 file ---
with h5py.File(sol_fname_h5, "r") as h5f:
# print("Available datasets in HDF5 file:", list(h5f.keys())) # Use to see datasets in the h5 file
# print("Datasets inside 'Function':", list(group.keys())) # Check available datasets
data = h5f['Function']['Velocity']
u.x.array[:] = data
return[u, V, mesh]
u, V, mesh = read_sol_file(comm, solname_xdmf, solname_h5)
print(u.x.array)
I inspected the .xdmf file with vim, and it contains information about the velocity which, however I do not know how to load it in.
I am using FEniCSx version 0.9, installed on WSL using conda.