Calculate volume of tetrahedral mesh regions

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!

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:

``````# 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.

2 Likes

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

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

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’