Probing/sampling/interpolating VTK data using python TVTK or MayaVi

2024/10/2 8:42:54

I would like to visualise a VTK data file (OpenFOAM output) using python. The plot I would like to make is a 1-d line plot of a quantity between two endpoints. To do so, the unstructured data should be interpolated on the points which lie between the two endpoints.

I've used the package Mayavi to visualise the VTK data. At the mayavi webpage there is a description of probing a single value from a scalarfield. This function does not work on a VTK file.

Also I've found a delaunay3d method (mlab.pipeline.delaunay3d) at the mayavi webpage. I did not get this one to work either.

Could anyone advise me how to interpolate my data?

Answer

Eventually, I found the answer to my own question. Just in case anyone visits this topic ans has the same problems, I will post my solution. I've used the vtkProbeFilter to interpolate my VTK-data. After the interpolation I've transformed the VTK-line to a numpy array for plotting convenience.

#!/usr/bin/env python
import numpy as np
from vtk.util import numpy_support as VN
from matplotlib import pyplot as plt
import vtkdef readVTK(filename):#read the vtk file with an unstructured gridreader = vtk.vtkUnstructuredGridReader()reader.SetFileName(filename)reader.ReadAllVectorsOn()reader.ReadAllScalarsOn()reader.Update()return readerdef createLine(p1,p2,numPoints):# Create the line along which you want to sampleline = vtk.vtkLineSource()line.SetResolution(numPoints)line.SetPoint1(p1)line.SetPoint2(p2)line.Update()return linedef probeOverLine(line,reader):#Interpolate the data from the VTK-file on the created line.data = reader.GetOutput()# vtkProbeFilter, the probe line is the input, and the underlying dataset is the source.probe = vtk.vtkProbeFilter()probe.SetInputConnection(line.GetOutputPort())probe.SetSource(data)probe.Update()#get the data from the VTK-object (probe) to an numpy arrayq=VN.vtk_to_numpy(probe.GetOutput().GetPointData().GetArray('U'))numPoints = probe.GetOutput().GetNumberOfPoints() # get the number of points on the line#intialise the points on the line    x = np.zeros(numPoints)y = np.zeros(numPoints)z = np.zeros(numPoints)points = np.zeros((numPoints , 3))#get the coordinates of the points on the linefor i in range(numPoints):x[i],y[i],z[i] = probe.GetOutput().GetPoint(i)points[i,0]=x[i]points[i,1]=y[i]points[i,2]=z[i]return points,qdef setZeroToNaN(array):# In case zero-values in the data, these are set to NaN.array[array==0]=np.nanreturn array#Define the filename of VTK file
filename='a-VTK-file.vtk'#Set the points between which the line is constructed.
p1=[0.0,-0.1,0.0]
p2=[0.0,-0.1,1.0]#Define the numer of interpolation points
numPoints=100reader = readVTK(filename) # read the VTKfile
line=createLine(p1,p2,numPoints) # Create the line
points,U =  probeOverLine(line,reader) # interpolate the data over the lineU = setZeroToNaN(U) # Set the zero's to NaN's
plt.plot(points[:,2],U[:,0]) #plot the data
plt.show()
https://en.xdnf.cn/q/70876.html

Related Q&A

Make Sphinx generate RST class documentation from pydoc

Im currently migrating all existing (incomplete) documentation to Sphinx.The problem is that the documentation uses Python docstrings (the module is written in C, but it probably does not matter) and t…

inspect.getfile () vs inspect.getsourcefile()

I was just going through the inspect module docs.What exactly is the difference between:inspect.getfile()andinspect.getsourcefile()I get exactly the same file path (of the module) for both.

Get a row of data in pandas as a dict

To get a row of data in pandas by index I can do:df.loc[100].tolist()Is there a way to get that row of data as a dict, other than doing:dict(zip(df.columns.tolist(),df.loc[100], tolist() ))

Find duplicate records in large text file

Im on a linux machine (Redhat) and I have an 11GB text file. Each line in the text file contains data for a single record and the first n characters of the line contains a unique identifier for the rec…

Select the first item from a drop down by index is not working. Unbound method select_by_index

I am trying to click the first item from a drop down. I want to use its index value because the value could be different each time. I only need to select the 1st item in the drop down for this particul…

finding the app window currently in focus on Mac OSX

I am writing a desktop usage statistics app. It runs a background daemon which wakes up at regular intervals, finds the name of the application window currently in focus and logs that data in database.…

os.system vs subprocess in python on linux

I have two python scripts. The first script calls a table of second scripts in which I need to execute a third party python script. It looks something like this: # the call from the first script. cmd …

How can I call a python script from a python script

I have a python script b.py which prints out time ever 5 sec.while (1):print "Start : %s" % time.ctime()time.sleep( 5 )print "End : %s" % time.ctime()time.sleep( 5 )And in my a.py, …

Read EXE, MSI, and ZIP file metadata in Python in Linux

I am writing a Python script to index a large set of Windows installers into a DB. I would like top know how to read the metadata information (Company, Product Name, Version, etc) from EXE, MSI and ZIP…

IllegalArgumentException thrown when count and collect function in spark

I tried to load a small dataset on local Spark when this exception is thrown when I used count() in PySpark (take() seems working). I tried to search about this issue but got no luck in figuring out wh…