I want to display the VTK file of the 2D temperature simulation result with jupyter.
Read the VTK file with Python, extract the coordinate data and temperature data, and display them with Matplotlib.
OS: Windows 10 home edition
Tool: Anaconda(Python 3.7.4)
Using the finite element method free software Freefem ++, the thermal diffusion equation was solved and the resulting VTK file was created. The code of the heat diffusion equation used Heat.edp of the example. I will omit how to install and run Freefem ++. The used VTK file is stored in here.
Install using conda.
conda install vtk
Import the Numpy, Matplotlib, and VTK libraries required for operation.
import numpy as np
import maptlotlib.pyplot as plt
import vtk
from vtk.util import numpy_support
%matplotlib inline
Since the VTK file was an unstructured grid, read it with Unstructured.
#File reading
filename = "heat_result.vtk"
reader = vtk.vtkUnstructuredGridReader()
reader.SetFileName(filename)
reader.Update()
The coordinate data of this VTK file is Point Data, and the temperature data is Cell Data. I want to have a one-to-one relationship between the coordinate data and the temperature data, so I convert them all to Point Data.
#Convert from cell data to point data
cell2point = vtk.vtkCellDataToPointData()
cell2point.SetInputData(reader.GetOutput())
cell2point.Update()
Read the VTK file and use vtk_support to extract the coordinate data and temperature data from the VTK data.
#Numpy coordinate and temperature data
coord = numpy_support.vtk_to_numpy(cell2point.GetOutput().GetPoints().GetData())
x = coord[:,0]
y = coord[:,1]
z = coord[:,2]#do not use. Since it is the result of a two-dimensional thermal diffusion equation, it is all 0.
#GetAbstractArray(0)Label, GetAbstractArray(1)Contains temperature data.
temperature = numpy_support.vtk_to_numpy(cell2point.GetOutput().GetPointData().GetAbstractArray(1))
Now that we're ready, all we have to do is view it in Matplotlib. Since it is unstructured data, the color map is displayed using trikontourf.
##Color map output
plt.tricontourf(x,y,temperature,levels=15,cmap="jet")
plt.colorbar()
https://github.com/matsxxx/show_vtk_on_jupyter
import numpy as np
import matplotlib.pyplot as plt
import vtk
from vtk.util import numpy_support
%matplotlib inline
#File reading
filename = "heat_result.vtk"
reader = vtk.vtkUnstructuredGridReader()
reader.SetFileName(filename)
reader.Update()
#Convert from cell data to point data
cell2point = vtk.vtkCellDataToPointData()
cell2point.SetInputData(reader.GetOutput())
cell2point.Update()
#Numpy coordinate and temperature data
coord = numpy_support.vtk_to_numpy(cell2point.GetOutput().GetPoints().GetData())
x = coord[:,0]
y = coord[:,1]
z = coord[:,2]#do not use. Since it is the result of a two-dimensional thermal diffusion equation, it is all 0.
#GetAbstractArray(0)Label, GetAbstractArray(1)Contains temperature data.
temperature = numpy_support.vtk_to_numpy(cell2point.GetOutput().GetPointData().GetAbstractArray(1))
#Color map output
plt.tricontourf(x,y,temperature,levels=15,cmap="jet")
plt.colorbar()
For the VTK class, the [class reference] of c ++ (https://vtk.org/doc/nightly/html/index.html) is open to the public. The structure and name of Python VTK is the same as the c ++ reference, so you can refer to it. The following sites were mainly referred to.
https://vtk.org/doc/nightly/html/classvtkFieldData.html
https://vtk.org/doc/nightly/html/classvtkCellDataToPointData.html
https://stackoverflow.com/questions/23138112/vtk-to-matplotlib-using-numpy
https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.tricontourf.html
Recommended Posts