Save data to VTK using Python and tvtk with more than one vector field
Question: 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


Answer: After some digging around I found the following solution based on
[this](http://docs.enthought.com/mayavi/mayavi/data.html#imagedata) and
[this](http://docs.enthought.com/mayavi/mayavi/auto/example_atomic_orbital.html#example-
atomic-orbital) 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')
    

