Prgrammable filter - Create a tensor of velocity gradients

Hi all,

I would like to create a tensor containing the velocity gradients for the 3 components of velocity for each cell of my mesh.

After several tries I obtain this solution but it still does not work:

import numpy as np
from paraview import numpy_support as ns

# Reading quantities
input0 = inputs[0]
U = input0.PointData["U_mean"]
V = input0.PointData["V_mean"]
W = input0.PointData["W_mean"]

gradu = gradient(U)
gradv = gradient(V)
gradw = gradient(W)

numPoints = input0.GetNumberOfPoints()

# Create a list to store flattened gradient tensors for each point
delta_tensors_flat = []

for i in range(numPoints):
    # Create the gradient tensor for the current point
    delta_tensor = np.array([[gradu[i, 0], gradu[i, 1], gradu[i, 2]],
                             [gradv[i, 0], gradv[i, 1], gradv[i, 2]],
                             [gradw[i, 0], gradw[i, 1], gradw[i, 2]]])
    # Flatten the gradient tensor and add it to the list
    delta_tensors_flat.append(delta_tensor.flatten())

# Convert the list to a numpy array
delta_tensors_flat_array = np.array(delta_tensors_flat)

# Print the shape of the array
print(delta_tensors_flat_array.shape)

# Add delta_tensors_flat_array to the point data
output.PointData.append(ns.numpy_to_vtk(delta_tensors_flat_array), "delta_tensor")

I get the error: Could not find a suitable VTK type for object.

The idea is to export the 9 component of the tensor as a .vtm file to latter remesh according to the velocity gradients (I need to have them as a tensor in the .vtm file and not component by component).

Thank you for your help!

If you want to calculate the velocity gradient tensor, it may be easier to use the Gradient filter instead of the Programmable filter.

The procedure is as follows
(1) Create velocity vector values from “U_mean”, “V_mean”, and “W_mean” Point Data using the MergeVectorComponent filter.
(2) Calculate the velocity gradient tensor for the created velocity vector using the Compute Gradient option of the Gradient filter.

Thank you very much for the answer. It works perfectly!

If someone can help regarding the way of doing this using a programmable filter I am still interested.