# VTK Gradient Filter

**URL:** https://discourse.vtk.org/t/vtk-gradient-filter/2961
**Category:** Support
**Created:** [April 5, 2020, 2:32pm UTC](https://discourse.vtk.org/t/vtk-gradient-filter/2961 "2020-04-05T14:32:38Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![RSwn](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/r/f05b48/32.png) [@RSwn](https://discourse.vtk.org/u/RSwn)
#### Post date: [April 5, 2020, 2:32pm UTC](https://discourse.vtk.org/t/vtk-gradient-filter/2961/1 "2020-04-05T14:32:38Z")

</div>

I am using the vtkGradientFilter in Python in order to compute velocity gradients from a vtkUnstructuredGrid file with a snippet shown below. However, when visualised in Paraview the results seem quite blocky and coarse. Is there any way to improve the quality of the output with the gradient filter or is it completely dependent on the mesh? Additionally, can anyone provide substantial documentation for vtkGradientFilter. I have found the basic description but was hoping for some more detail.

```
reader = vtk.vtkUnstructuredGridReader()
data = reader.GetOutput()

gradient = vtk.vtkGradientFilter()
gradient.SetInputData(data)
gradient.SetInputScalars(0, "Velocity") 
gradient.SetResultArrayName("Velocity Gradient")
gradient.Update()

```

 ![image](https://discourse.vtk.org/uploads/default/original/2X/5/5dfa3b29e21783daf4d475be077b411631d0223d.png)

---

<div class="post-metadata">

### Author: ![banesullivan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/banesullivan/32/7143_2.png) [@banesullivan](https://discourse.vtk.org/u/banesullivan)
#### Post date: [April 6, 2020, 1:41pm UTC](https://discourse.vtk.org/t/vtk-gradient-filter/2961/2 "2020-04-06T13:41:55Z")

</div>

This coarseness is a result of the mesh resolution itself, not the filter. You only have data values on the nodes of the mesh and the colors are be interpolated between those nodes. I’d recommend trying to increase your mesh’s resolution or playing around with different rendering techniques (i.e. make sure interpolate before mapping is enabled in the ParaView options).

---

<div class="post-metadata">

### Author: ![banesullivan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/banesullivan/32/7143_2.png) [@banesullivan](https://discourse.vtk.org/u/banesullivan)
#### Post date: [April 6, 2020, 1:44pm UTC](https://discourse.vtk.org/t/vtk-gradient-filter/2961/3 "2020-04-06T13:44:34Z")

</div>

If you are using Python, I highly recommend using PyVista: [https://docs.pyvista.org](https://docs.pyvista.org) which is a more Pythonic interface to VTK. And it has some more documentation on how to use the gradient filter:

- [https://docs.pyvista.org/core/filters.html#pyvista.DataSetFilters.compute\_gradient](https://docs.pyvista.org/core/filters.html#pyvista.DataSetFilters.compute_gradient)
- [https://docs.pyvista.org/examples/01-filter/gradients.html](https://docs.pyvista.org/examples/01-filter/gradients.html)

see also: [Gradient of Unstrucutred Grid in Python](https://discourse.vtk.org/t/gradient-of-unstrucutred-grid-in-python/2489)

---

<div class="post-metadata">

### Author: ![RSwn](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/r/f05b48/32.png) [@RSwn](https://discourse.vtk.org/u/RSwn)
#### Post date: [April 6, 2020, 2:43pm UTC](https://discourse.vtk.org/t/vtk-gradient-filter/2961/4 "2020-04-06T14:43:43Z")

</div>

If the input file has a VTK Unstructured Grid, how would it be converted to be read by PyVista? I receive the error that I am missing the ‘dataset’ positional argument when i try:

```
mesh = pv.DataSetFilter.compute_gradient(scalars="Velocity")

```

Below is my code for reading the VTK file mesh:

```
filename = 'soln_volume_CFD_00290.vtk'
reader = vtk.vtkUnstructuredGridReader()
reader.SetFileName(filename)
reader.ReadAllScalarsOn()
reader.ReadAllVectorsOn()
reader.ReadAllTensorsOn()
reader.Update()
data = reader.GetOutput() # the mesh

density_vtk_array = data.GetPointData().GetArray(0)
momentum_vtk_array = data.GetPointData().GetArray(1)
density_numpy_array = np.array(density_vtk_array)
momentum_numpy_array = np.array(momentum_vtk_array)

res = list(map(truediv, momentum_numpy_array, density_numpy_array))
velocity = np.array(res)
velocity_vtk_arr = numpy_to_vtk(velocity)
velocity_vtk_arr.SetName("Velocity")
data.GetPointData().AddArray(velocity_vtk_arr)
```

---

<div class="post-metadata">

### Author: ![banesullivan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/banesullivan/32/7143_2.png) [@banesullivan](https://discourse.vtk.org/u/banesullivan)
#### Post date: [April 6, 2020, 2:46pm UTC](https://discourse.vtk.org/t/vtk-gradient-filter/2961/5 "2020-04-06T14:46:26Z")

</div>

Try this:

```python
import pyvista as pv

filename = 'soln_volume_CFD_00290.vtk'
data = pv.read(filename)

mesh = data.compute_gradient(scalars="Velocity")

mesh["gradient"]

```

```python
mesh.plot(scalars="gradient")

```

---

<div class="post-metadata">

### Author: ![RSwn](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/r/f05b48/32.png) [@RSwn](https://discourse.vtk.org/u/RSwn)
#### Post date: [April 6, 2020, 3:20pm UTC](https://discourse.vtk.org/t/vtk-gradient-filter/2961/6 "2020-04-06T15:20:31Z")

</div>

Looks to be working thank you. Was wondering if there is any detailed documentation on the method used i.e. is it the same central differencing and interpolation method used by the VTK function?

---

<div class="post-metadata">

### Author: ![banesullivan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/banesullivan/32/7143_2.png) [@banesullivan](https://discourse.vtk.org/u/banesullivan)
#### Post date: [April 6, 2020, 3:22pm UTC](https://discourse.vtk.org/t/vtk-gradient-filter/2961/7 "2020-04-06T15:22:45Z")

</div>

PyVista is nothing more than a wrapper around VTK, so yes, PyVista is the same as the VTK filter. The docs for `vtk.vtkGradientFilter` have the most info, albeit not much: [https://vtk.org/doc/nightly/html/classvtkGradientFilter.html#details](https://vtk.org/doc/nightly/html/classvtkGradientFilter.html#details)
