CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   ParaView (https://www.cfd-online.com/Forums/paraview/)
-   -   [General] Python script to sum up a variable for all cells (https://www.cfd-online.com/Forums/paraview/214348-python-script-sum-up-variable-all-cells.html)

hconel January 28, 2019 13:18

Python script to sum up a variable for all cells
 
Hello,
I am trying to write a Python script to use in Paraview, where I need:
( component of a vector field ) * ( cell volume )
summed up for all cells in the domain. I have no clue where to start and couldn't find a similar script. Can someone give me an idea for this?
Thanks in advance.

tkarabela February 7, 2019 17:59

Hi, you can use the Calculator to make a new variable from your vector field and then use the Integrate filter to compute the sum. Here is the example in Python (using Paraview 5.6, but it should work in earlier versions too):

Code:

# Load your case and extract the domain - this will be specific for your case.
# The example below is suitable for OpenFOAM case exported using foamToEnsight.
from paraview.simple import *
case = EnSightReader(CaseFileName="my-ensight-data.case")
Show(case)
domain = ExtractBlock(case)
domain.BlockIndices = [1]
Show(domain)

# If you have data in nodes, interpolate to cells first,
# otherwise skip this step.
domain = PointDatatoCellData(domain)

# Extract the vector component to scalar
# (here my variable is "U", replace it with your variable name).
calc = Calculator(domain)
calc.AttributeType = "Cell Data"
calc.Function = "U_X"

# Integrate over domain.
integrate = IntegrateVariables(calc)
result = integrate.GetCellDataInformation().GetArray("Result").GetRange()[0]
print "Summed value is", result



All times are GMT -4. The time now is 03:46.