Skip to content

Pad VTKHDF vector/tensors - #116

Merged
jorgensd merged 2 commits into
mainfrom
dokken/vtkhdf-pad-3d
Nov 18, 2026
Merged

jorgensd merged 2 commits into
mainfrom
dokken/vtkhdf-pad-3d

Conversation

@jorgensd

Copy link
Copy Markdown
Member

Summary

Stacked on the VTKHDF time-steps PR (dokken/vtkhdf-read-timestamps).

VTK reads arrays with 3 components as vectors and arrays with 9 components as tensors. The VTKHDF backend stored vectors with as many components as the function has (2 in 2D), and could not write tensors at all. ParaView therefore could not use a 2D velocity for glyphs or Warp By Vector.

  • Padding. Point and cell data are padded with zeros when written. Vectors with fewer than 3 components get 3, and tensors of at most 3 × 3 are embedded in a 3 × 3 tensor, row by row. Other shapes are stored as they are.
  • Reading back. The value shape is stored as the attribute ValueShape of the array's metadata group, /io4dolfinx/<mesh>/<Point|Cell>Data/<name>, which VTK ignores. io4dolfinx strips the padding on read and restores the shape. A vector of length 1 now reads back as a vector, not a scalar.
  • Other files. Arrays without the attribute, written by VTK, DOLFINx or older io4dolfinx, are read with all stored components: one is a scalar, more is a vector. VTK's 1D datasets for single-component arrays now read; before, they failed.
  • Tensors. write_point_data now writes tensor-valued functions. Before, it failed with "elements have different value dimensions".
  • Mismatches. Appending an array whose value shape differs from the stored one raises a ValueError before anything is written.

Other changes

  • io4dolfinx.utils gets vtk_components, pad_for_vtk and strip_vtk_padding.
  • ArrayData gets an optional value_shape, which write_point_data and write_cell_data fill from the function.
  • create_geometry_function_space(mesh, N, value_shape=None) builds tensor-valued spaces.
  • Backends can provide an optional read_value_shape, which read_point_data and read_cell_data use when present (vtkhdf and vtkhdf-serial). It is not part of the IOBackend protocol, so the other backends need no change.
  • A "Vectors and tensors" section in docs/backends/vtkhdf.rst.

Testing

34 new tests in tests/test_vtkhdf.py:

  • point and cell data of shapes from 1 to 3 × 3, on both VTKHDF backends: stored widths, zeros in the padding, and exact read-back;
  • pyvista reading the padded arrays as vectors and tensors;
  • the value-shape mismatch;
  • 2-component vectors written by DOLFINx's VTKHDF writer, and 1D scalar arrays written by VTK.

33 of them fail without this PR. The one that passes checks that unpadded DOLFINx vectors still read as before. The full suite passes in serial and on 4 MPI ranks.

🤖 Generated with Claude Code

@jorgensd
jorgensd changed the base branch from main to dokken/vtkhdf-read-timestamps November 18, 2026 15:33
@jorgensd
jorgensd added this pull request to stack #117 November 18, 2026 15:33
@jorgensd
jorgensd force-pushed the dokken/vtkhdf-pad-3d branch from 7911e0b to dcfe169 Compare November 18, 2026 15:39
Base automatically changed from dokken/vtkhdf-read-timestamps to main November 18, 2026 18:34
@jorgensd
jorgensd force-pushed the dokken/vtkhdf-pad-3d branch from d357739 to 4a510cb Compare November 18, 2026 18:34
@jorgensd
jorgensd merged commit 45d22cb into main Nov 18, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant