我想使用 vtk 脚本将 Paraview 中的数据沿着一条线保存在 .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)
你可以帮我吗?
非常感谢!
我不确定ParaView是如何参与其中的。
如果你在ParaView可编程过滤器中,我建议你最多使用ParaView过滤器并尽量减少使用python,这样更不容易出错。
所以:
Plot Over Line
Pass Arrays
仅保留感兴趣的数据Calculator
提取成分)CSV
用于写入文件的提取器