我在 Paraview 中使用可编程过滤器导出名为
metrique
的字段以进行重新网格划分。 metrique
是一个 VTKCompositeDataArray,由我的计算结果(3*3 矩阵)的每个点的 9 个值组成。如果低于阈值,我想替换此 VTKCompositeDataArray 对角项上的值,但出现错误“'VTKCompositeDataArray' 对象不支持项目分配”。
这是我的 python 脚本(Paraview 中的可编程过滤器):
import numpy as np
import vtk
#Parameters
nb_points=395699
replacement_value=0.5
lower_threshold=0.5
#Reading quantities
input0=inputs[0]
Velocity=input0.PointData["Velocity"]
gradU=gradient(Velocity)
U=Velocity[:,0]
# Repeat the array along a new axis to create the desired shape
B = np.array([[[0.1,0,0],[0,0.1,0],[0,0,0.1]]])
B_tensorial = np.tile(B, (nb_points, 1, 1))
# Get indices of diagonal elements
diagonal_indices = np.diag_indices(B_tensorial.shape[1])
metrique=gradU*B_tensorial*U
# Apply the condition and replace values for diagonal elements
metrique[..., diagonal_indices[0], diagonal_indices[1]] = np.where(abs(metrique[..., diagonal_indices[0], diagonal_indices[1]]) > lower_threshold, replacement_value, metrique[..., diagonal_indices[0], diagonal_indices[1]])
output.PointData.append(abs(metrique), "metrique")
如果我将其应用于 Numpy 数组,但不适用于 VTK 数组,则此代码可以很好地工作
metrique
。如何按照所需条件替换 VTK 数组metrique
中的对角线项?
为了回应一个答案,我尝试创建一个函数将 VTKCompositeDataArray 转换为 Numpy 数组以对其进行处理,但出现错误 File "", line 18, in vtk_composite_data_array_to_numpy AttributeError:“VTKCompositeDataArray”对象没有属性“GetNumberOfArrays”
def vtk_composite_data_array_to_numpy(composite_data_array): # 获取复合数据数组中数组的个数 num_arrays = composite_data_array.GetNumberOfArrays()
# Initialize an empty list to store the arrays
arrays = []
# Iterate over each array in the composite data array
for i in range(num_arrays):
# Get the i-th array
array = composite_data_array.GetArray(i)
# Convert the vtkDataArray to a NumPy array
num_values = array.GetNumberOfTuples() * array.GetNumberOfComponents()
numpy_array = np.zeros(num_values, dtype=array.GetDataType())
array.ExportToVoidPointer(numpy_array)
# Reshape the 1D NumPy array into the appropriate shape
numpy_array = numpy_array.reshape((array.GetNumberOfTuples(), array.GetNumberOfComponents()))
# Append the NumPy array to the list
arrays.append(numpy_array)
# Concatenate the arrays into a single NumPy array
numpy_array = np.concatenate(arrays, axis=0)
return numpy_array
非常感谢您的帮助!