Save data to VTK using Python and tvtk with more t

2019-05-08 14:37发布

问题:

I'm trying to save three sets of vector quantities corresponding to the same structured grid (velocity, turbulence intensity and standard deviation of velocity fluctuations). Ideally, I'd like them to be a part of the same vtk file but so far I have only been able to get one of them into the file like so:

sg = tvtk.StructuredGrid(dimensions=x.shape, points=pts)
sg.point_data.vectors = U
sg.point_data.vectors.name = 'U'
write_data(sg, 'vtktestWake.vtk')

I've spent past few hours searching for an example of how to add more then one vector or scalar field but failed and so thought I'd ask here. Any guidance will be most appreciated.

Thanks,

Artur

回答1:

After some digging around I found the following solution based on this and this example. You have to add the additional data field using the add_array method see:

from tvtk.api import tvtk, write_data
import numpy as np

data = np.random.random((3,3,3))
data2 = np.random.random((3,3,3))

i = tvtk.ImageData(spacing=(1, 1, 1), origin=(0, 0, 0))
i.point_data.scalars = data.ravel()
i.point_data.scalars.name = 'scalars'
i.dimensions = data.shape
# add second point data field
i.point_data.add_array(data2.ravel())
i.point_data.get_array(1).name = 'field2'
i.point_data.update()

write_data(i, 'vtktest.vtk')