[vtkusers] Unstructured Grid: Volume averaging

Justin Weber onlyjus at gmail.com
Tue Mar 27 10:19:50 EDT 2012


Greetings,

So, I have unstructured grid simulation data (produced by MFix) saved in a
.vtk file. I have had success using the vtk unstructured grid reader to
read the vtk files and see all the arrays which contain the simulation
variables.

I would like to reduce the data by volume averaging which means I need the
cell volumes, cell bounds, etc.

Any help in extracting this information would be much appreciated. I am
currently using the latest python wrapped vtk library.

I have been piecing together a python class to provide some tools for
processing vtk files in python. Currently it can read unstructured grids,
return the available variables, cell centers, etc.

Thanks!
Justin

import vtk
import numpy as np
import glob
import vtk.util.numpy_support as vtkNP

class vtkFileRead:
    def __init__(self):
        self.filename='F:/vtk/RISER_5838.vtk'
        self.directory='F:/vtk/'
    def setFile(self,Fname):
        self.filename=Fname
    def readFile(self):
        self.reader=vtk.vtkUnstructuredGridReader()
        try:
            self.reader.SetFileName(self.filename)
        except:
            if len(self.filename)<=0:
                print 'Filename not set'
                return
            else:
                print 'Could not find file: ',self.filename
                return
        self.reader.ReadAllFieldsOn()
        self.reader.ReadAllScalarsOn()
        self.reader.ReadAllTensorsOn()
        self.reader.ReadAllVectorsOn()
        self.reader.ReadAllNormalsOn()
        self.reader.ReadAllColorScalarsOn()
        self.reader.ReadAllTCoordsOn()
        self.reader.Update()
        return self.reader
    def getReaderOutput(self):
        self.readerOutput=self.reader.GetOutput()
        return self.readerOutput
    def getCellData(self):
        self.output=self.reader.GetOutput()
        self.cellData=self.output.GetCellData()
        return self.cellData
    def getVariableNames(self):
        VarNames=[]
        for i in range(0,self.cellData.GetNumberOfArrays()-1):
            VarNames.append(self.cellData.GetArrayName(i))
        return VarNames
    def GetValue(self,cell,var):
        for i in range(0,self.cellData.GetNumberOfArrays()-1):
            if var==self.cellData.GetArrayName(i):
                array=self.cellData.GetArray(i)
                return array.GetTuple(cell)
        return 'Could not Find'
    def GetArray(self,var):
        for i in range(0,self.cellData.GetNumberOfArrays()-1):
            if var==self.cellData.GetArrayName(i):
                return self.cellData.GetArray(i)
    def vtkArrayToNumpy(self,vtkArray):
        return vtkNP.vtk_to_numpy(vtkArray)
    def CalcCenters(self):
        centers=vtk.vtkCellCenters()
        centers.AddInput(self.output)
        centers.Update()
        self.centersOutput=centers.GetOutput()
        return self.centersOutput
    def GetCenter(self,cell):
        return self.centersOutput.GetPoint(cell)
    def setDirectory(self,directory):
        self.directory=directory
    def findFiles(self):
        self.files=glob.glob(self.directory+'*.vtk')
        print 'Found',len(self.files),'Files'
        return self.files
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20120327/422037e8/attachment.htm>


More information about the vtkusers mailing list