How to write temporal UnstructuredGrid data to a VTKHDF file with vtkHDFWriter in Python?

Hi all,

I’m trying to write a time series of vtkUnstructuredGrid data to a single VTKHDF (.vtkhdf) file using Python. Specifically, the grid topology is constant, but a point data array named “foo” changes over 100 timesteps.

I’ve found several resources that explain how to construct the VTKHDF file format from scratch using h5py, such as:

However, I’m wondering if there’s a more direct way to do this using vtkHDFWriter. It seems like vtkHDFWriter should be able to handle this, which would be much simpler than manually creating the HDF5 structure.

My question is: How can I use vtkHDFWriter to save an UnstructuredGrid with 100 timesteps of a “foo” point data array into a single .vtkhdf file?

Here’s a conceptual code snippet of what I’m trying to do:

import numpy as np
from vtkmodules.util import numpy_support
from vtkmodules.vtkCommonDataModel import vtkUnstructuredGrid
from vtkmodules.vtkIOHDF import vtkHDFWriter

# 1. Create a sample UnstructuredGrid
grid = vtkUnstructuredGrid()
# make some structure

# 2. Prepare for time series data
num_timesteps = 100
# Dummy data: 3 points, so we need 3 values at each timestep
foo_data_over_time = [
    np.random.rand(grid.number_of_cells) for _ in range(num_timesteps)
]

# 3. This is where I'm unsure how to proceed
writer = vtkHDFWriter()
writer.SetFileName("temporal_ug.vtkhdf")
writer.SetInputData(grid)

# How do I add the time-varying data?
# Do I add all arrays to the grid and give them special names?
# Or do I call writer.Write() in a loop?
# The writer has Write() and AddInputDataObject(), but it's not clear how to use them for time series.

# I would expect something like this, but this is just a guess:
# writer.Start() # is there a method like this?
# for i in range(num_timesteps):
#     foo_array = numpy_support.numpy_to_vtk(foo_data_over_time[i])
#     foo_array.SetName("foo")
#     grid.GetPointData().SetArrays(None) # Clear previous array
#     grid.GetPointData().AddArray(foo_array)
#     writer.SetTimeValue(float(i)) # Is there a method like this?
#     writer.Write() # Append this timestep
# writer.Stop() # is there a method like this?

How can I achieve this correctly so that ParaView or other readers recognize it as a time series?

Any help or examples would be greatly appreciated!

Thanks!

@Louis_Gombert @lgivord

Hello, the vtkHDFWriter is capable of writing multiple time steps when the option WriteAllTimeSteps is toggled on. However, for that you need to use the VTK pipeline logic to create a temporal VTK source, by creating a class derived from VTKPythonAlgorithmBaseand generating time steps with your data, like vtkGenerateTimeSteps does for example.

Unfortunately, vtkHDFWriter does not currently have the ability to append time steps one by one without using the pipeline logic. That is something we want to tackle in the future.

For your simple use case, it may be easier to write data to the VTKHDF file using h5py. Note that you can only write your points once using the right temporal offsets. You can take a look at the VTKHDF tutorial to learn how to do that.

1 Like