I’ve saved an XDMF file of a mesh with a function assigned to each point at certain times using time-stamps. Now, I’d like to read that file and retrieve the values of that function at each saved time. Is this possible? I’m using dolfinx.
The code for writing the file goes as follows:
#Function space
V = fem.FunctionSpace(mesh, ("Lagrange", 1))
#Time array where solution SOL is defined
times = np.shape(SOL)[1]
#Function to assign values
f = fem.Function(V)
#Write
with XDMFFile(MPI.COMM_WORLD, "surf_fib.xdmf", "w") as xdmf:
xdmf.write_mesh(mesh)
for i in range(len(times)):
f.x.array[:]=SOL[:,i] #Solution at time times[i]
xdmf.write_function(f, times[i]) #Save solution
Yeah I just ran across a similar issue ; dolfinx is sometimes inconsistent with mesh partitioning. What I observed on my end :
Read a .xxx file (using meshio)
Convert it to .xdmf (meshio)
Read it back (dolfinx)
Save its partition (which part of the mesh is owned by what processor) (dolfinx)
Do stuff, save data, end first program (PetscBinaryIO)
Begin second program, read .xdmf mesh (dolfinx)
Save 2nd partition (dolfinx)
Attempt to read data back (PetscBinaryIO)
Running this process twice in a parallel fashion on a .msh file works, but not a .xmf file. dolfinx is inconsistent in its first and second partition - it splits the mesh the same but changes which proc accesses what, which changes vector size and breaks PetscBinaryIO in turn.
Long story short, careful of the original mesh format fed to meshio, it matters.
An even simpler case of inconsistency can be seen here:
import ufl
from dolfinx.io import XDMFFile
from dolfinx.fem import FunctionSpace, Function
from mpi4py.MPI import COMM_WORLD as comm
for i in range(2):
with XDMFFile(comm, "mesh.xdmf", "r") as file:
mesh = file.read_mesh(name="Grid")
FE_constant=ufl.FiniteElement("DG",mesh.ufl_cell(),0)
W = FunctionSpace(mesh,FE_constant)
partition=Function(W)
partition.x.array[:]=comm.rank
with XDMFFile(comm, f"partition{i}.xdmf", "w") as xdmf:
xdmf.write_mesh(mesh)
xdmf.write_function(partition)