Search code examples
pythonvtkopenfoam

Plot contour of vtk. file (from OpenFOAM) with Python-vtk module


I have a vtk.file (pressure field) generated from cuttingPlane of OpenFOAM, in the format of Legacy, POLYGONS and Field Data:

    # vtk DataFile Version 2.0
        sampleSurface
        ASCII
        DATASET POLYDATA
        POINTS 55181 double
        0.312848 -0.389703 0
        -0.319252 -0.384475 0
        0.246285 -0.434844 0
        ...(points  coordinates) ...

        POLYGONS 109010 436040
        3 54060 54066 54200
        3 54206 54200 54066
        3 54204 54065 54200
        3 54060 54200 54065
        ...

        POINT_DATA 55181
        FIELD attributes 1
        p 1 55181 float
        -0.622929 -0.202063 -0.860382 ....

What I want to do is to visualize this file with python-vtk module, and output .png image. I can plot contour of this file in Paraview (see enter image description here) but when using python scripting the domain is white and no pressure distribution, which is weird (see enter image description here). I also attach my python code.

#!/usr/bin/env python

import vtk
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from scipy.interpolate import griddata
from vtk.util.numpy_support import vtk_to_numpy

# 0. set the vtk.file path
filePath = '.../p_zNormal.vtk'

# reader 
reader = vtk.vtkPolyDataReader()            # vtkDataSetReader()
reader.SetFileName(filePath)
reader.ReadAllScalarsOn()
reader.ReadAllVectorsOn()
reader.Update() 

vtkdata = reader.GetOutput()
scalar_range = vtkdata.GetScalarRange()     # (0.0, 1.0) ?

np_coordinates = vtk_to_numpy(vtkdata.GetPoints().GetData()) 

pointData = vtkdata.GetPointData()
fieldData_vtk_array = pointData.GetArray(0)    
fieldData_np_array = np.array(vtk_to_numpy(fieldData_vtk_array))

DataMin = np.min(fieldData_np_array)
DataMax = np.max(fieldData_np_array)

# look-up table
pColorTable = vtk.vtkLookupTable()
pColorTable.SetHueRange(0, 0.67)             # red -> blue
pColorTable.SetNumberOfTableValues(16)       # number of colors
pColorTable.SetTableRange(scalar_range)
pColorTable.SetValueRange(0.0, 1.0)
pColorTable.Build()

# mapper
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(reader.GetOutputPort())
mapper.SetScalarRange(DataMin,DataMax)
mapper.SetLookupTable(pColorTable)
mapper.ScalarVisibilityOn()
mapper.SetScalarModeToUsePointFieldData();
mapper.Update()

# actor
actor = vtk.vtkActor()
actor.SetMapper(mapper)

scalarBar = vtk.vtkScalarBarActor()
scalarBar.SetLookupTable(pColorTable)
scalarBar.SetTitle("p (Pa)")
scalarBar.SetNumberOfLabels(10)
#scalarBar.GetTitleTextProperty().SetColor(0, 0, 0);
#scalarBar.GetTitleTextProperty().SetFontFamilyToArial();
#scalarBar.GetTitleTextProperty().SetFontSize(20);
#scalarBar.GetLabelTextProperty().SetColor(0, 0, 0);
#scalarBar.SetLabelFormat("%5.3f");
#scalarBar.GetLabelTextProperty().SetFontFamilyToArial();
#scalarBar.GetLabelTextProperty().SetFontSize(20);
scalarBar.SetUnconstrainedFontSize(1)

# render 
ren = vtk.vtkRenderer()
ren.AddActor(actor) 
ren.AddActor(scalarBar)               
ren.SetBackground(0.2, 0.3, 0.4)

# render window
renwin = vtk.vtkRenderWindow()
renwin.AddRenderer(ren)             
renwin.SetWindowName("Test")
renwin.SetSize(800,600)
renwin.Render()

# interactor
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renwin)

iren.Initialize()
iren.Start()
              

It seems that I CANNOT upload the source file.... I would be very very appreciated if anyone could help! Thanks!


Solution

  • Try adding reader.ReadAllFieldsOn().