I would like to write Paraview data in binary to save space. I saw that VTKFile takes a file_mode argument. I tried setting it to wb without success. Looking at the source code I think this argument is unused but I could be wrong. Here is an MVP:
from dolfinx import mesh, io, fem
from mpi4py import MPI
nels_per_side = 2
mesh = mesh.create_unit_square(MPI.COMM_WORLD, nels_per_side, nels_per_side, mesh.CellType.quadrilateral)
V = fem.functionspace(mesh, ("Lagrange", 1))
u1, u2 = fem.Function(V, name="u1"), fem.Function(V, name="u2")
u1.interpolate( lambda x : x[0]**2 )
u2.interpolate( lambda x : x[1]**2 )
# VTK out
with io.VTKFile(mesh.comm, "out/dolfinx_out.pvd", 'FILE_MODE_CAN_BE_ANYTHING') as iofile:
iofile.write_function([u1, u2])
About this, XDMF is a bit buggy when you write multiple functions. I reported this in this issue. Not only it duplicates the data so that it occupies more memory, the coloring of a dataset can be “hidden” by the other one somehow. See the MWE below:
from dolfinx import mesh, io, fem
from mpi4py import MPI
nels_per_side = 2
mesh = mesh.create_unit_square(MPI.COMM_WORLD, nels_per_side, nels_per_side, mesh.CellType.quadrilateral)
V = fem.functionspace(mesh, ("Lagrange", 1))
D = fem.functionspace(mesh, ("DG",0))
u1, u2 = fem.Function(V, name="u1"), fem.Function(D, name="u2")
u1.interpolate( lambda x : x[0]**2 )
u2.interpolate( lambda x : x[1]**2 )
writer = io.XDMFFile(mesh.comm,"test.xdmf", 'w')
writer.write_mesh(mesh)
writer.write_function(u1)
writer.write_function(u2)
writer.close()
If I only write u2, the correct number of elements is written and I can see the coloring of u2:
If I write u1 and u2, a multiblock dataset is written, the elements are duplicated (it takes twice the memory) and I can neither see the coloring of u1 nor u2:
I am using Paraview 5.12. I think there used to be a fix for this in dolfin (see this post). The issue is similar in VTK except for some reason I can still see the coloring of both fields.
Thank you for the update on VTXWriter, it works well. I might give a try to implementing the changes although I am not sure my coding would be up to the standards of your project. From the code in ADIOSWriters.h , it looks like the main tasks are to modify _is_piecewise_constant into an array and modify impl_vtx::extract_function_names to return two vectors of strings for the nodal and DG0 functions repectively.
I am not familiar with XDMF but in VTK, what’s happening is that the VTUs are referenced one more time in the PVTUs for every call to write_function, where they should only be referenced once.
Thank you, I managed to get it to work by using a different Paraview reader. Was using Xdmf3_Reader_S and with Xdmf3_Reader_T it works for me even without the extract_block.
I was talking about RAM. In the information window some posts ago, you can see that the dataset occupies 5KBs if I only write a single function and 10KBs if I write 2 functions. I think this can only be the case if the geometry data is read multiple times by Paraview. I think this is confirmed by Paraview displaying 4 and 8 elements in the respective information windows.
This is a Paraview issue, not a storage/DOLFINx issue. We provide pointers to the same data in the h5 file for both of them.
As I mentioned earlier, XDMF is no longer maintained by its developers (Kitware) and is not a format that is receiving updates in Paraview. Thus there is very little we will do wrt to this. Therefore I would strongly suggest to use VTXWriter, that uses ADIOS2 (Visualizing Data — ADIOS2 2.10.0-rc1 documentation)