Efficient way to update values of vtk array in python

I am asking this in the context of updating data arrays in a catalyst enabled simulation, though this doesnt have relate to catalyst directly.

I create and initialize a multiblockdataset with all my data arrays from numpy arrays at startup.

dummyArray = numpy_to_vtk(someNumpyArray)
dummyArray.SetName("dummyArray")
grid.GetCellData().AddArray(dummyArray)

That works great, however I’m not sure how to update that array with new data during the next iteration in python.

When doing this in c++ I just use a for loop and array.SetValue(i, val) and update one item at a time, however, that is very expensive in python.

I tried using SetArray

dummyArray = grid.GetCellData().GetArray("dummyArray")
dummyArray.SetArray(numpy_to_vtk(someNumpyArray), numberOfCells, 1)

However this is resulting in corrupt data on output.

Is there a better way to update the vtk arrays in python without an explicit for loop?

Hi Kyle,

Could you please try this:

grid.GetCellData().RemoveArray(“dummyArray”)
someVTKArray = numpy_to_vtk(someNumpyArray)
someVTKArray.SetName(“dummyArray”)
grid.GetCellData().AddArray(someVTKArray)

Best Regards,
Pavel

It seems to work, and is significantly faster than the manual loop. Thanks!