Saving vector function with length greater than the geometrical dimension

I’m trying to write vector-valued functions to view into Paraview, where the vector has a length that is not the geometrical dimension. It appears that using VTK or VTX, writing a function with shape (n,) results in X, Y and Z components regardless of the value of n: if it is more than 3 the components are either not written or not read by Paraview, I am not sure. XDMF on the other hand always displays the right number of components. It is also true for tensor-valued functions where VTK and VTX always show 9 components.
An illustrative MWE with a function space of shape (4,):

from dolfinx import mesh, fem, io
from mpi4py import MPI

domain = mesh.create_unit_square(MPI.COMM_WORLD, 10, 10, mesh.CellType.quadrilateral)
V = fem.functionspace(domain, ("Lagrange", 1, (4,)))
v = fem.Function(V, name="4comp-vector")

# Gives components X,Y,Z in Paraview
with io.VTKFile(domain.comm, "4comp_vector.pvd", "w") as vtk:
    vtk.write_function(v, 0.0)

# Gives components X,Y,Z in Paraview
with io.VTXWriter(domain.comm, "4comp_vector.bp", [v]) as vtx:
    vtx.write(0.0)

# Gives components 0,1,2,3 in Paraview
with io.XDMFFile(domain.comm, "4comp_vector.xdmf", "w") as xdmf:
    xdmf.write_mesh(domain)
    xdmf.write_function(v, 0.0)
1 Like

I tried out this code on a recent commit of the main branch (May 17th) and it crashes if the dimension of the function is 4, runs for dim 3 or lower. I get:

double free or corruption (!prev)
Aborted

Where does the error occur?
I’m running 0.8.0 and it works fine.

I meant to say it’s probably a bug in dolfinx. I ran using the latest dolfinx:nightly docker image and it does crash.

I am not sure VTKFile can support tensors of size 4.
I tried changing the code to support it, see: Comparing main...dokken/arbitrary_tensor · FEniCS/dolfinx · GitHub
which is similar to:
Fix padding of xdmf vector of len < 3 by jorgensd · Pull Request #3095 · FEniCS/dolfinx · GitHub
I would have to test it for VTXWriter to see if it is supported.

Correction, my branch fixes VTKFile and extends it to arbitrary tensors.
I’ve generalized all formats to take this into account in

1 Like