5
votes

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
Would it be possible to save each field to a separate file and then combine them together, maybe even in ParaView or some-such, should a need be?Aleksander Lidtke
I don't know if there is a way to do it in paraview, I'll have a look. Still, I would think there should be a more direct way...Artur
I'm pretty sure that this is not possible. You might be able to store the right information as contrastingly point and cell data, but this is clunky, can't be simultaneously visualized (afaik) and doesn't help in the general case of storing arbitrary numbers of vectors. There are other file formats that might be more useful, that save the current state of a vtk scene or camera. Can you give more details as to what you are trying to do?aestrivex
Thanks for your reply. In the CFD program I use the data is visualised through paraview format and one may import data for multiple fields for one case file. Hence I thought that a vtk object may hold more than one vector and/or scalar field. The idea therefore was to have all the three fields I am interested in to be stored in the same vtk file. If you say it's not possible I'll just make 3 separate vtk files, one for each quantity. Should work equally well I suppose.Artur
Actually, The vtk file format allows to save more than one point or cell data per point and cell - I use this quite often! It's just a question if the tvtk writer allows to do so.Jakob

1 Answers

5
votes

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')