Script for retrieving vtk data and manipulating in Python

Hi everybody,
I have temporal and spacial data saved from a simulation in vtk files. The dataset consists of almost a million points in the domain, and temporal results of about 1000 timesteps.
I can do the analysis that I will explain below in the Paraview GUI very easily within a couple of minutes, but due to the large number of timesteps I need to automate the process.

To briefly explain the problem, I have cfd results of a flame, which I need to split in 2 pieces based on some criteria and compute the integrals of the heat release for the 2 separate domains separated by the straight line split. I used 2 methods so far but I ran accross some problems.

Method A: Converted the vtk datasets into csv files within Paraview, one csv file for each timestep. Through a python script outside paraview I could do all the computations as explained above, with the only problem that I had to manually set the size of each cell when computing the integrals. My data are Point Data by the way. This is a simple task, if the cells are made by a structured fixed size mesh, but unfortunately this is not the case. So this could be resolved if there was a straightforward method to export the cell size when converting the vtk to csv.

Method B: Tried to complete the whole task within a Paraview python script, which is my preferred method. But since I’m not very comfortable with the different Paraview pathways to go about this, I am a little confused whether I should use a python calculator, a programmable filter, paraview.simple, other libraries of paraview, or any other method. My first problem was how to transform the vtk data into a format that can be easily understood and manipulated with a python script. With some difficulty I could extract some datasets of interest, but not the full matrix of data including the coordinates, so I could do the subsequent calculations. The code is attached below, which stops at the point where I can get the datasets. If I manage to get this part, the remaining calculations are easy for me within python.

from paraview.simple import *
from paraview.vtk.numpy_interface import dataset_adapter as dsa
from paraview.numpy_support import vtk_to_numpy
import numpy as np

#### disable automatic camera reset on 'Show'

fileIn = './vtk/file.pvd'

variableName = ['HRR  ', 'TEMPE']

# create a new 'PVD Reader'
objectCase = PVDReader(registrationName='file.pvd', FileName=fileIn)
objectCase.PointArrays = variableName

# get animation scene
animationScene1 = GetAnimationScene()

timesteps = objectCase.TimestepValues

d = []

for x in range(0, len(timesteps)):
    hrr_range = objectCase.PointData['HRR  '].GetRange(0)
    vtk_data = servermanager.Fetch(objectCase)
    numofvalues = vtk_data.GetPointData().GetArray('HRR  ').GetNumberOfValues()
    vtk_data = dsa.WrapDataObject(vtk_data)
    data = vtk_data.PointData[1]
    data_np = np.vstack(data)
    d_np = np.vstack(d)
np.savetxt('out.txt', d_np, delimiter='\t', header='Header', comments='#')

Up to the part shown above, I’m trying to retrieve the full dataset. The computations will also be performed within the for loop, so for each pass in the loop I should retain a single row of results, which would represent the results of a single timestep. Am I going in the right direction, or should I use another method?

Any help would be appreciated.

Hi @phivos

You want to perform the computation server side instead.
Look into ProgrammableFilters.


Hi Mathieu,
I will try that, thanks.