Dear community,
I am trying to write and read the mesh function and solution in HDF5 interface. The writing step works fine either in serial or parallel but when I try to read the mesh function and solution from written “mesh_mark_and_solution.h5” file, the cell ids get changed and also the solution does not get assigned same as it was written.
However the issue gets resolved if I do both reading and writing with serial or use same number of processors in
mpirun -np 9 python3 write_h5_file_solution.py
mpirun -np 9 python3 read_h5_file_solution.py
The code for writing data is following:
from dolfin import *
mesh_cube = UnitCubeMesh(5,5,5)
mesh_and_solution = HDF5File(mesh_cube.mpi_comm(),'mesh_mark_and_solution.h5', 'w')
mesh_function = MeshFunction("size_t", mesh_cube, mesh_cube.topology().dim(), 0)
domain = AutoSubDomain( lambda x, bndry: between( x[0], (0 , 0.5) ))
domain.mark(mesh_function,1)
mesh = MeshView.create(mesh_function, 1)
result_file_mesh = XDMFFile('mesh_to_write.xdmf')
result_file_mesh.write(mesh)
Sol_space = FunctionSpace(mesh, "Lagrange", 1)
sol = Function(Sol_space, name="Sol")
expression = Expression(" 100*x[2]*x[2]*x[2]",degree=2)
sol.assign(interpolate(expression, Sol_space))
mesh_and_solution.write(mesh_function,"/mesh_function/mesh_function_0")
mesh_and_solution.write(sol, "/solution/sol_0")
result_file = XDMFFile('solution_to_write.xdmf')
result_file.write(sol, t=float(0));
mesh_and_solution.close()
While the separate code file for reading the code is following:
from dolfin import *
mesh_cube = UnitCubeMesh(5,5,5)
mesh_and_solution = HDF5File(mesh_cube.mpi_comm(),'mesh_mark_and_solution.h5', 'r')
mesh_function = MeshFunction("size_t", mesh_cube, mesh_cube.topology().dim(), 0)
mesh_and_solution.read(mesh_function,"/mesh_function/mesh_function_0")
mesh = MeshView.create(mesh_function, 1)
result_file_mesh = XDMFFile('mesh_after_reading.xdmf')
result_file_mesh.write(mesh);
Sol_space = FunctionSpace(mesh, "Lagrange", 1)
sol = Function(Sol_space, name="Sol")
mesh_and_solution.read(sol,"/solution/sol_0")
result_file = XDMFFile('solution_after_reading.xdmf')
result_file.write(sol, t=float(0));
mesh_and_solution.close()
What I am interested in is that no matter what is the choice of either serial or certain processors in parallel I opt for, I want to have same results every time I write and especially read the data. Thanks in advance!