简体   繁体   中英

Structure of a vtk file read with python

I have a vtk file and I need to extract from it a three-dimensional velocity vector. I have never used one so I do not really know what it contains and how to extract informations from it.

In stack overflow I found this question Save an array of data from a VTK in Python , which helped, and I was able to extract my data.

This is the script I used:

import vtk as v
import numpy as np

Filename = 'filename.vtk'
reader = v.vtkUnstructuredGridReader()
reader.SetFileName(Filename)
reader.ReadAllScalarsOn()
reader.ReadAllVectorsOn()

pa = v.vtkPassArrays()
pa.SetInputConnection(reader.GetOutputPort())
pa.AddArray(0, 'velocity_magnitude' )
print(pa)

writer = v.vtkDataSetWriter()
writer.SetFileName('test.vtk')
writer.SetInputConnection(pa.GetOutputPort())
writer.Update()
writer.Write()

And this is the file I ended up with:

# vtk DataFile Version 4.2
vtk output
ASCII
DATASET UNSTRUCTURED_GRID
FIELD FieldData 6
Nek_SpectralElementData 1 4 int
8 8 8 512 
avtOriginalBounds 1 6 double
0 6.283185482 -1 1 0 3.141592741 
VolumeDependent 1 1 bit
0 
MeshName 1 1 string
mesh

CYCLE 1 1 int
1000 
TIME 1 1 double
20 
POINTS 262144 float
0 -1 1.5708 0.0503675 -1 1.5708 0.160339 -1 1.5708 
0.310507 -1 1.5708 0.474891 -1 1.5708 0.625059 -1 1.5708 
0.735031 -1 1.5708 0.785398 -1 1.5708 0 -0.997435 1.5708 
0.0503675 -0.997435 1.5708 0.160339 -0.997435 1.5708 0.310507 -0.997435 1.5708 

.
.
.
CELLS 175616 1580544
8 0 1 9 8 64 65 73 72 
8 64 65 73 72 128 129 137 136 
8 128 129 137 136 192 193 201 200 
8 192 193 201 200 256 257 265 264 
8 256 257 265 264 320 321 329 328 

.
.
.

CELL_TYPES 175616
12
12
12
12

.
.
.
POINT_DATA 262144
VECTORS velocity float
0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0.0113612 1.21843e-05 0.000552347 
0.0115553 1.12597e-05 0.000419801 0.0121388 6.55409e-06 -0.000110149 0.0128729 -5.02466e-06 -0.000602613 

So, doing some research I understood that in the first part I have the coordinates of the points, but why are they 9 per row? Then I understood that the points are connected by hexahedronal cells, there are 175616 of them, each one with 8 points, and next we have the positions of the corners of them, but I do not know how they are written. In the last part I finally have my velocity vector, but again, why 9 components per row? And how are the points arranged? To which point does the velocity of the first line relate to? And for the second, or third?

If you know some guide where I can read how to understand this file, you will help a lot.

Thank you very much!

In the VTK file format the dataset attribute sections the line white space is ignored. So points and vectors are always taken as sets of 3 floats, regardless of where the line ends.

Your POINTS section has 262144 points, so it assumes there are 3*262144 floats in the data following, corresponding to each point's XYZ position.

Same things goes for the POINT_DATA. You have 262144 entries that map directly to the previous POINTS. And the VECTOR section is assumed to have 3*262144 values corresponding to the points. And, again, line feeds are ignored.

Here's the documentation for the VTK format: https://vtk.org/wp-content/uploads/2015/04/file-formats.pdf

You can find information on the CELL and CELL_TYPES here: Reading and plotting VTK file data structure with python and here: https://lorensen.github.io/VTKExamples/site/VTKFileFormats/#dataset-format as well.

Having nine entries in a row for your data entry for a 3D point may seem confusing. But if you count in 3-tuples, regardless of how many entries you have in a row, you should end up with 262144 points under the POINT section, for example. And each point is represented by a tuple of 3 entries (x,y,z)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM