Unable to Write XDMF File

I’m trying to write to an XDMF file to record time stepping as in Diffusion of a Gaussian function — FEniCSx tutorial.

I use the code:

def rand_scal_eta(x):
    return StSt[0]+perm*(np.random.rand(x.shape[1])-np.random.rand(x.shape[1]))
def Initial_eta(out):
    #perm and StSt must be declared as global
    out.interpolate(rand_scal_eta)
    return out
x0=0
y0=0
xm=10
ym=10
nx=10
ny=10
r_mesh=mesh.create_rectangle(MPI.COMM_WORLD,(np.array([x0,y0]),np.array([xm,ym])),(nx,ny), mesh.CellType.triangle)
V = fem.FunctionSpace(r_mesh, ('Lagrange',1))
u= fem.Function(V)
global perm, StSt
StSt=[0,1]
perm=0.1
u=Initial_eta(u)
xdmf=io.XDMFFile(r_mesh.comm,"var.xdmf","w")
xdmf.write_mesh(r_mesh)
xdmf.write_function(u,0)

I get the Output:

HDF5-DIAG: Error detected in HDF5 (1.12.1) MPI-process 0:
#000: H5F.c line 532 in H5Fcreate(): unable to create file
major: File accessibility
minor: Unable to open file
#001: H5VLcallback.c line 3282 in H5VL_file_create(): file create failed
major: Virtual Object Layer
minor: Unable to create file
#002: H5VLcallback.c line 3248 in H5VL__file_create(): file create failed
major: Virtual Object Layer
minor: Unable to create file
#003: H5VLnative_file.c line 63 in H5VL__native_file_create(): unable to create file
major: File accessibility
minor: Unable to open file
#004: H5Fint.c line 1858 in H5F_open(): unable to truncate a file which is already open
major: File accessibility
minor: Unable to open file


RuntimeError Traceback (most recent call last)
Input In [7], in <cell line: 1>()
----> 1 xdmf=io.XDMFFile(r_mesh.comm,“var.xdmf”,“w”)
2 xdmf.write_mesh(r_mesh)
3 xdmf.write_function(u,0)
RuntimeError: Failed to create HDF5 file.

I cannot reproduce the error on my PC. However, a similar error has been reported in the post below. It seems to be related to MPI. How are you running your code?

I launched dolfinx kernel in jupyter lab using:

sudo docker run --init -p 8888:8888 -v "$(pwd)":/root/shared dolfinx/lab:stable

Then ran the code within the notebook

I think your error arises when you run the cell multiple times. Try to close xdmf at the end of your script:

def rand_scal_eta(x):
    return StSt[0]+perm*(np.random.rand(x.shape[1])-np.random.rand(x.shape[1]))
def Initial_eta(out):
    #perm and StSt must be declared as global
    out.interpolate(rand_scal_eta)
    return out
x0=0
y0=0
xm=10
ym=10
nx=10
ny=10
r_mesh=mesh.create_rectangle(MPI.COMM_WORLD,(np.array([x0,y0]),np.array([xm,ym])),(nx,ny), mesh.CellType.triangle)
V = fem.FunctionSpace(r_mesh, ('Lagrange',1))
u= fem.Function(V)
global perm, StSt
StSt=[0,1]
perm=0.1
u=Initial_eta(u)
xdmf=io.XDMFFile(r_mesh.comm,"var.xdmf","w")
xdmf.write_mesh(r_mesh)
xdmf.write_function(u,0)
xdmf.close()

Thank you. I restarted the kernel and the error went but I was annoyed I hadn’t worked out why so this is really helpful.