使用 python 中的 'vtkXMLUnstructuredGridReader' 从 VTU 文件中读取矢量信息作为多维数组

Reading vector information as a multidimensional array from VTU files using 'vtkXMLUnstructuredGridReader' in python

我正在尝试使用 vtkXMLUnstructuredGridReader 从 python 中的 VTU 文件读取矢量场信息。要读取的向量场是一个N*3维的数组,其中N是单元格的个数,3是向量的分量个数。 VTU 文件看起来像这样(没有 XML 数据),

<?xml version="1.0"?>
<VTKFile type="UnstructuredGrid" version="0.1" byte_order="LittleEndian" header_type="UInt32" compressor="vtkZLibDataCompressor">
  <UnstructuredGrid>
    <FieldData>
      <DataArray type="Float64" Name="timeInPs" NumberOfTuples="1" format="appended" RangeMin="600"                  RangeMax="600"                  offset="0"                   />
    </FieldData>
    <Piece NumberOfPoints="145705"               NumberOfCells="838547"              >
      <PointData Scalars="Material" Vectors="Magnetization">
        <DataArray type="Float64" Name="Magnetization" NumberOfComponents="3" format="appended" RangeMin="1"                    RangeMax="1"                    offset="48"                  />
        <DataArray type="Int32" Name="Material" format="appended" RangeMin="0"                    RangeMax="0"                    offset="4455172"             />
      </PointData>
      <CellData>
      </CellData>
      <Points>
        <DataArray type="Float32" Name="Points" NumberOfComponents="3" format="appended" RangeMin="1.0415804282e-12"     RangeMax="10.00000052"          offset="4456528"             >
          <InformationKey name="L2_NORM_RANGE" location="vtkDataArray" length="2">
            <Value index="0">
              1.0415804282e-12
            </Value>
            <Value index="1">
              10.00000052
            </Value>
          </InformationKey>
        </DataArray>
      </Points>
      <Cells>
        <DataArray type="Int64" Name="connectivity" format="appended" RangeMin=""                     RangeMax=""                     offset="6589768"             />
        <DataArray type="Int64" Name="offsets" format="appended" RangeMin=""                     RangeMax=""                     offset="20080856"            />
        <DataArray type="UInt8" Name="types" format="appended" RangeMin=""                     RangeMax=""                     offset="21531024"            />
      </Cells>
    </Piece>
  </UnstructuredGrid>
  <AppendedData encoding="base64">


为此我做了一些在线搜索,尽管我找不到合适的文档,但我在 Stack here () 上找到了一个线程我尝试使用这里提供的代码

import vtk
import numpy
filname = trial.vtu
reader = vtk.vtkXMLUnstructuredGridReader()
reader.SetFileName(filename)
reader.Update() 
output = reader.GetOutput()
potential = output.GetPointData().GetArray("Magnetization")
print potential

但作为输出,我得到的不是 N*3 数组,而是以下内容。

  vtkDoubleArray (0x28567a0)
  Debug: Off
  Modified Time: 389
  Reference Count: 2
  Registered Events: (none)
  Name: Magnetization
  Data type: double
  Size: 24519
  MaxId: 24518
  NumberOfComponents: 3
  Information: 0x1fbab50
    Debug: Off
    Modified Time: 388
    Reference Count: 1
    Registered Events: (none)
  Name: Magnetization
  Number Of Components: 3
  Number Of Tuples: 8173
  Size: 24519
  MaxId: 24518
  LookupTable: (none)

这包含有关字段的所有信息,但作为 N*3 数组的向量分量除外。

我有两个问题,

1) 代码中缺少什么?

2) 是否有关于此的适当文档?

您导入了 numpy 包而忘记了 vtk numpy 支持包。我发布了您的示例代码并添加了缺失的行。

import vtk
import numpy as np
from vtk.util.numpy_support import vtk_to_numpy #thats what you need 

filname = trial.vtu
reader = vtk.vtkXMLUnstructuredGridReader()
reader.SetFileName(filename)
reader.Update() 
output = reader.GetOutput()

# apply the vtk_to_numpy function to your output. Your output should be a N*3 numpy 
# array.
potential = vtk_to_numpy(output.GetPointData().GetArray("Magnetization"))
print potential