Load DG0 Function from parallelly saved XDMF file

Hi all,

I encountered a problem when trying to load in DG functions from parallelly saved XDMF file. I installed FEniCs using conda, and the version is 2019.1.0.

I provided a snippet for an minimum working example. The following script is named example.py

from dolfin import *

mesh_2d = RectangleMesh.create(MPI.comm_world, [Point(0.0, 0.0), Point(1.0, 1.0)], [10, 10], CellType.Type.triangle, 'right/left')

xdmf_obj = XDMFFile(MPI.comm_world, 'mesh.xdmf')
xdmf_obj.write(mesh_2d)
xdmf_obj.close()

DG_space = FunctionSpace(mesh_2d, 'DG', 0)
# initial condition
function = interpolate(Expression('x[1]<=0.5 ? 1.0 : 0.0', degree=1), DG_space)

xdmf_obj = XDMFFile(MPI.comm_world, 'solution.xdmf')
xdmf_obj.write_checkpoint(function, 'func_to_save', 0, append=False)
xdmf_obj.close()

There is another serial script I used for visualization:

from dolfin import *
import matplotlib.pyplot as plt

mesh_xdmf = Mesh()
xdmf_obj = XDMFFile('mesh.xdmf')
xdmf_obj.read(mesh_xdmf)
xdmf_obj.close()

DG_space = FunctionSpace(mesh_xdmf, 'DG', 0)
func= Function(DG_space)

xdmf_obj = XDMFFile(MPI.comm_world, 'solution.xdmf')
xdmf_obj.read_checkpoint(func, 'func_to_save', 0)

plt.figure()
plot(func)
plt.show()

When I run the code in serial using mpirun -n 1 python example.py, the visualization code worked fine. It produced the expected output:

ex_right

However, when I run the code in parallel using mpirun -n 3 python example.py, the plot looks like this:

ex_wrong

How can one plot such function loaded from a XDMF file in a correct manner?

Thank you!

I would not plot with matplotlib in parallel. Usually, one should look at output from parallel computations in Paraview, for instance by adding:


xdmf_2 = XDMFFile(MPI.comm_world, "solution_out.xdmf")
xdmf_2.write_checkpoint(func, 'func_to_save', 0, append=False)
xdmf_2.close()

to your script.

2 Likes