# Calculate volume of tetrahedral mesh regions

**URL:** https://discourse.paraview.org/t/calculate-volume-of-tetrahedral-mesh-regions/2609
**Category:** ParaView Support
**Created:** [September 24, 2019, 1:34am UTC](https://discourse.paraview.org/t/calculate-volume-of-tetrahedral-mesh-regions/2609 "2019-09-24T01:34:58Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Hamburgerfinger](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/h/eb9ed0/32.png) [@Hamburgerfinger](https://discourse.paraview.org/u/Hamburgerfinger)
#### Post date: [September 24, 2019, 1:34am UTC](https://discourse.paraview.org/t/calculate-volume-of-tetrahedral-mesh-regions/2609/1 "2019-09-24T01:34:58Z")

</div>

I have a tetrahedral mesh with different regions defined by common scalars. I’m trying to figure out how to use the ‘Cell Size’ filter to calculate the volume of each region, but right now I can only calculate the volume of each tetrahedral element.

I can calculate the volume of a region using the Threshold filter, and then use the Cell Size filter with ‘calculate sum’ selected to get the volume of that region, but there are a lot of regions so I can’t go through each one-by-one.

Would appreciate any help!  
Thanks!

---

<div class="post-metadata">

### Author: ![cory.quammen](https://discourse.paraview.org/user_avatar/discourse.paraview.org/cory.quammen/32/11193_2.png) [@cory.quammen](https://discourse.paraview.org/u/cory.quammen)
#### Post date: [September 25, 2019, 9:27pm UTC](https://discourse.paraview.org/t/calculate-volume-of-tetrahedral-mesh-regions/2609/2 "2019-09-25T21:27:19Z")

</div>

You can do this with the **Programmable Filter** and a fairly short script. Assume the name of your scalars that label the regions is called ‘RegionId’, then your script would look like:

```python
# Find unique region IDs
regionArray = inputs[0].CellData['RegionId']
regions = unique(regionArray)

v = volume(inputs[0])
for regionValue in regions:
  regionVolume = sum(v[regionArray == regionValue])
  print(regionValue, regionVolume)

```

This will print the volume of each region value and region.

---

<div class="post-metadata">

### Author: ![sharaborin](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/s/9de053/32.png) [@sharaborin](https://discourse.paraview.org/u/sharaborin)
#### Post date: [October 5, 2020, 1:33pm UTC](https://discourse.paraview.org/t/calculate-volume-of-tetrahedral-mesh-regions/2609/3 "2020-10-05T13:33:56Z")

</div>

Dear Cory,  
How to implement this snippet into the pvpython script?  
Since `unique` and `volume` functions are undefined:  
`NameError: name 'unique' is not defined`.

This piece of code works perfectly in python Programmable filter, but not in pvpython:(  
Best regards,  
Evgenii

---

<div class="post-metadata">

### Author: ![cory.quammen](https://discourse.paraview.org/user_avatar/discourse.paraview.org/cory.quammen/32/11193_2.png) [@cory.quammen](https://discourse.paraview.org/u/cory.quammen)
#### Post date: [October 5, 2020, 2:23pm UTC](https://discourse.paraview.org/t/calculate-volume-of-tetrahedral-mesh-regions/2609/4 "2020-10-05T14:23:17Z")

</div>

```python
from paraview.vtk.util import numpy_support # provides unique()
from vtkmodules.numpy_interface.algorithms import * # provides volume()

```

---

<div class="post-metadata">

### Author: ![sharaborin](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/s/9de053/32.png) [@sharaborin](https://discourse.paraview.org/u/sharaborin)
#### Post date: [October 5, 2020, 7:27pm UTC](https://discourse.paraview.org/t/calculate-volume-of-tetrahedral-mesh-regions/2609/5 "2020-10-05T19:27:40Z")

</div>

Thank you, Cory!

### Find unique region IDs

```
regionArray = connectivity2.CellData['RegionId']
regions = np.unique(regionArray) # I don't understand how to use numpy_support:(
v = volume(regionArray)
a=[]
for regionValue in regions:
    regionVolume = sum(v[regionArray == regionValue])
    a.append((regionValue,regionVolume))
    print(regionValue, regionVolume)

```

I have a strange error:  
from paraview.vtk.util import numpy\_support # provides unique()  
from vtkmodules.numpy\_interface.algorithms import \* # provides volume()  
Traceback (most recent call last):  
File “lambda2\_final.py”, line 655, in   
v = volume(regionArray)  
File “/Applications/ParaView-5.8.0.app/Contents/Python/vtkmodules/numpy\_interface/algorithms.py”, line 140, in new\_dsfunc2  
return dsfunc(ds)  
File “/Applications/ParaView-5.8.0.app/Contents/Python/vtkmodules/numpy\_interface/internal\_algorithms.py”, line 501, in volume  
ds.CopyStructure(dataset.VTKObject)  
AttributeError: ‘paraview.modules.vtkRemotingCore.vtkPVArrayInforma’ object has no attribute ‘CopyStructure’

Thererefore, I changed and renamed module algs

from paraview.vtk.util import numpy\_support # provides unique()  
import vtk.numpy\_interface.algorithms as algs

I have the following error  
Traceback (most recent call last):  
File “lambda2\_final.py”, line 655, in   
v = algs.volume(regionArray)  
File “/Applications/ParaView-5.8.0.app/Contents/Python/vtkmodules/numpy\_interface/algorithms.py”, line 140, in new\_dsfunc2  
return dsfunc(ds)  
File “/Applications/ParaView-5.8.0.app/Contents/Python/vtkmodules/numpy\_interface/internal\_algorithms.py”, line 501, in volume  
ds.CopyStructure(dataset.VTKObject)  
AttributeError: ‘paraview.modules.vtkRemotingCore.vtkPVArrayInforma’ object has no attribute ‘CopyStructure’
