# Prgrammable filter - Create a tensor of velocity gradients

**URL:** https://discourse.paraview.org/t/prgrammable-filter-create-a-tensor-of-velocity-gradients/14201
**Category:** ParaView Support
**Tags:** python, vtk
**Created:** [March 19, 2024, 11:33am UTC](https://discourse.paraview.org/t/prgrammable-filter-create-a-tensor-of-velocity-gradients/14201 "2024-03-19T11:33:07Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Boone](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/b/c67d28/32.png) [@Boone](https://discourse.paraview.org/u/Boone)
#### Post date: [March 19, 2024, 11:33am UTC](https://discourse.paraview.org/t/prgrammable-filter-create-a-tensor-of-velocity-gradients/14201/1 "2024-03-19T11:33:07Z")

</div>

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:

```auto
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!

---

<div class="post-metadata">

### Author: ![Kenichiro-Yoshimi](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/k/ecc23a/32.png) [@Kenichiro-Yoshimi](https://discourse.paraview.org/u/Kenichiro-Yoshimi)
#### Post date: [March 21, 2024, 10:35am UTC](https://discourse.paraview.org/t/prgrammable-filter-create-a-tensor-of-velocity-gradients/14201/2 "2024-03-21T10:35:21Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Boone](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/b/c67d28/32.png) [@Boone](https://discourse.paraview.org/u/Boone)
#### Post date: [March 21, 2024, 1:13pm UTC](https://discourse.paraview.org/t/prgrammable-filter-create-a-tensor-of-velocity-gradients/14201/3 "2024-03-21T13:13:16Z")

</div>

> [@Kenichiro-Yoshimi](#):
>
> 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.
