vtk 脚本 - 沿线提取数据

问题描述 投票:0回答:1

我想保存 CFD 计算的数据。我需要使用 vtk 脚本在 .csv 文件中沿线提取数据。这就是我想做的:

    1. 画一条线
    1. 保存感兴趣数量的值(第一 速度的分量)及其沿线 的坐标(应该 能够处理非结构化网格)
    1. 将 2 个数量保存在 .csv 文件中。

我试过这个:

import numpy as np
import vtk
from vtk.numpy_interface import dataset_adapter as dsa
from vtk.numpy_interface import algorithms as algs

""" I/O parameters """
path='path/RESU/20231219-2125/'
in_folder = path + 'postprocessing/'
out_folder = path + 'Profiles3/'
in_casefile = in_folder + 'RESULTS.case'
time_value = 999999.0 # Select the last time value

fields_vector = (
     ("Velocity", 0, None),
)
fields = fields_vector

reader = vtk.vtkEnSightGoldBinaryReader()
reader.SetCaseFileName(in_casefile)
reader.ReadAllVariablesOff()
reader.SetTimeValue(time_value)

x_coord=[0.03, 0.05]

# Can handle several x locations
for Lx in x_coord:
    lineSource = vtk.vtkLineSource()
    lineSource.SetPoint1(Lx, 0, 0)
    lineSource.SetPoint2(Lx, 0.13, 0)
    lineSource.SetResolution(100)
    for i, field in enumerate(fields):
        # Create a probe filter
        probeFilter = vtk.vtkProbeLineFilter()
        probeFilter.SetInputConnection(lineSource.GetOutputPort())
        probeFilter.SetSourceConnection(reader.GetOutputPort())               
        centers_data = dsa.WrapDataObject(probeFilter.GetOutputDataObject(0))
        field_centers = centers_data.PointData[field[0]]
        z_centers_uniq, z_idx, z_inv, z_cnt = np.unique(
                    centers_data.Points[:, 2],
                    return_index=True,
                    return_inverse=True,
                    return_counts=True)
    np.savetxt(out_folder + field[0] , np.vstack((z_centers_uniq,field_centers[:,0])).T, delimiter=',')

但是不起作用:

raise TypeError("Mismatch between array dtype ('%s') and "
TypeError: Mismatch between array dtype ('object') and format specifier ('%.18e,%.18e')

我尝试通过以下修改来处理此错误,但没有成功:

    index_1 = field[1]
    index_2 = field[2]                
    centers_data = dsa.WrapDataObject(probeFilter.GetOutputDataObject(0))
    field_centers = centers_data.PointData[field[0]].Arrays[0][:, index_1, index_2]
    z_centers_uniq, z_idx, z_inv, z_cnt = np.unique(
                    centers_data.Points[:, 2].Arrays[0].round(decimals=16),
                    return_index=True,
                    return_inverse=True,
                    return_counts=True)

你可以帮我吗?

非常感谢!

python extract vtk
1个回答
0
投票

我不确定ParaView是如何参与其中的。

如果你在ParaView可编程过滤器中,我建议你最多使用ParaView过滤器并尽量减少使用python,这样更不容易出错。

所以:

  • 创建一个
    Plot Over Line
  • Pass Arrays
    仅保留感兴趣的数据
  • (选择
    Calculator
    提取成分)
  • CSV
    用于写入文件的提取器
© www.soinside.com 2019 - 2024. All rights reserved.