CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Visualization & Post-Processing Software > ParaView

[General] Get data from Calculator filter in python script

Register Blogs Community New Posts Updated Threads Search

Like Tree16Likes
  • 1 Post By taalf
  • 8 Post By wyldckat
  • 2 Post By taalf
  • 1 Post By wyldckat
  • 1 Post By taalf
  • 2 Post By wyldckat
  • 1 Post By taalf

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   October 4, 2013, 09:20
Unhappy Get data from Calculator filter in python script
  #1
Member
 
William Tougeron
Join Date: Jan 2011
Location: Czech Republic
Posts: 70
Rep Power: 15
taalf is on a distinguished road
Hi all,

New on python scripting with Paraview, I am trying for a while to do a (I hope) simple thing but without any success...

I have generated in my script a Calculator filter (let's call it Calculator1).

When I display it in a Paraview spreadsheet view, it looks like below:

___|__Point_ID__|___Points___|___Result___|
_0_|__0_________|__67.15...__|__4.35e+06__|


That is: a single row with a column called Result (among other columns).

I would like to get the value of this Result (4.35e+06 here) into a python variable.

Can anyone help me with this?...

Thank you very much!

Best regards,

William

Last edited by taalf; October 6, 2013 at 03:22.
taalf is offline   Reply With Quote

Old   October 6, 2013, 13:56
Default
  #2
Retired Super Moderator
 
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 10,982
Blog Entries: 45
Rep Power: 128
wyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to all
Greetings William,

Have a look into this section: http://www.paraview.org/Wiki/ParaVie...tiple_Datasets

In other words, you can get in into another Python calculator the "Result" field, for example:
Code:
inputs[0].PointData['Result']
But if you could provide a more detailed example, it would be easier to help you.

Best regards,
Bruno
__________________
wyldckat is offline   Reply With Quote

Old   October 7, 2013, 04:50
Default
  #3
Member
 
William Tougeron
Join Date: Jan 2011
Location: Czech Republic
Posts: 70
Rep Power: 15
taalf is on a distinguished road
Hi Bruno,

Thank you very much for your help !

I cannot share my case but I enclose the python script I use... (and copy paste it in this post).

I have a wing surface with pressure data. I want to split it into several 2D profiles, and to calculate the lift coefficient on each of these profiles.

The last object "Calculator2" is the one from which I would like to extract the "Result" value...

I tried Calculator2.PointData['Result'] but it returns "Array: Result"...

Here is my script:



try:
paraview.simple

except:
from paraview.simple import *

# Get active boundary (actualy a 3D wing from a mesh with pressure data)
Boundary = GetActiveSource()

# Extract surface from this wing boundary (necessary to compute normal vectors)
ExtractSurface1 = ExtractSurface( Boundary )

# Compute normal vectors of the wing surface
GenerateSurfaceNormals1 = GenerateSurfaceNormals( ExtractSurface1 )

# For several positions
on the z axis...
for z in range(500,5500,1000):
# Make a cut of the wing to have a local 2D profile
Slice1 = Slice( GenerateSurfaceNormals1 )
Slice1.SliceType = "Plane"
Slice1.SliceOffsetValues = [0.0]
Slice1.SliceType.Origin = [0.0, 0.0, z]
Slice1.SliceType.Normal = [0.0, 0.0, 1.0]

# Calculate the local "pressure" vectors on the 2D profile
Calculator1 = Calculator( Slice1 )
Calculator1.Function = 'Normals*pressure'

# Make the integral of pressure vectors to have the total force exerted on the 2D profile (a single vector)
IntegrateVariables1 = IntegrateVariables( Calculator1 )

# Project the force vector to get the lift coefficient (here, with angle of attack = 0 deg)
Calculator2 = Calculator( IntegrateVariables1 )
Calculator2.Function = 'Result.(0*iHat+1*jHat)'

# Print the local lift coefficient ! :-/
print 'C_L at z =',z/1000.0,'m'
print Calculator2.PointData['Result']# Doesn't work... :-|

Attached Files
File Type: zip compute_local_lift_coefficient.py.zip (846 Bytes, 73 views)
Pagoda likes this.
taalf is offline   Reply With Quote

Old   October 7, 2013, 08:56
Default
  #4
Member
 
William Tougeron
Join Date: Jan 2011
Location: Czech Republic
Posts: 70
Rep Power: 15
taalf is on a distinguished road
Hi,

I made a test case on which to run my script:

https://www.dropbox.com/s/tacsiz95qe...g_pressure.zip

It consist of a Ensight Gold case.

I just use an Extract Block filter to extract the "wing" boundary, then select the ExtractBlock1 object and open a Python shell and run the script.

Before: before.jpg

After: after.jpg

Best regards,

William
taalf is offline   Reply With Quote

Old   October 7, 2013, 10:11
Default
  #5
Member
 
William Tougeron
Join Date: Jan 2011
Location: Czech Republic
Posts: 70
Rep Power: 15
taalf is on a distinguished road
I finally use a workaround consisting in exporting data into CSV files...

http://www.paraview.org/Wiki/ParaVie...rting_CSV_Data

Adapting to my script, it looks like:

writer = CreateWriter(str(z)+".csv", Calculator2)
writer.FieldAssociation = "Points"
writer.UpdatePipeline()
del writer


Like this I have separated files for each 2D profile. Not beautiful at all, but it works...

But I hope there is a way to avoid such CSV file export and to get values directly in the python script...

Best regards,

William
taalf is offline   Reply With Quote

Old   October 13, 2013, 07:33
Default
  #6
Retired Super Moderator
 
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 10,982
Blog Entries: 45
Rep Power: 128
wyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to all
Hi William,

Many thanks for sharing the information you've managed to figure out!

The idea I had given on the other post relied on using the filter "Python Calculator", which does provide access to values on arrays, but it works differently from the main Python scripting system that ParaView uses.

It took me a few minutes to figure this one out. The instructions are partially given here: http://paraview.org/Wiki/ParaView/Py...Source_Proxies
The rest I used the knowledge I have of VTK.

The example is as follows, with comments along the way:
Code:
Calculator2=GetActiveSource()

#get access to the VTK data
realCalculator2=servermanager.Fetch(Calculator2)

#The resulting calculation is in Point data format
rcPointData=realCalculator2.GetPointData()

#Get the array we want, which in my case is named "Result2", which is the result of "Result*iHat" from the previous integrated results
Result2Array=rcPointData.GetArray('Result2')

#Now you can access the values in two ways:
#1- Directly access all values on the array
manualVector = [Result2Array.GetValue(0), Result2Array.GetValue(1), Result2Array.GetValue(2)]

#2- Directly access the vector itself, without having to do it manually
automaticVector = Result2Array.GetTuple(0)
Best regards,
Bruno
taalf, j-avdeev, Pagoda and 5 others like this.
__________________
wyldckat is offline   Reply With Quote

Old   October 14, 2013, 09:17
Default
  #7
Member
 
William Tougeron
Join Date: Jan 2011
Location: Czech Republic
Posts: 70
Rep Power: 15
taalf is on a distinguished road
Thank you Bruno ! ! ! !

I dreamed it, you did it !

It works perfectly

Enclosed is my script fixed with your solution

Thank you again !

Best regards,

William
Attached Files
File Type: zip compute_local_lift_coefficient_fixed.py.zip (904 Bytes, 174 views)
wyldckat and r2d21 like this.
taalf is offline   Reply With Quote

Old   October 15, 2013, 13:06
Default
  #8
Member
 
William Tougeron
Join Date: Jan 2011
Location: Czech Republic
Posts: 70
Rep Power: 15
taalf is on a distinguished road
Hi all...

I am still struggling making my script.

I figured out that I have to use the Python Calculator to do what I need.

But after creating the Python Calculator object, I cannot use the solution above on it.

I put
Code:
data=servermanager.Fetch(areaCalculator)
But then if I do
Code:
pointData=data.GetPointData()
I have an error... Thought I can display the Python Calculator in a Paraview Spreadsheet and see the Point Data...

If someone can help..............

Enclosed is my script (please, use the previous case and modify the path inside the python script).

Code:
try: paraview.simple
except: from paraview.simple import *

# Import data

wing_pressure_case = EnSightReader( CaseFileName='/home/tougeron/test/wing_pressure.case' )
wing_pressure_case.CellArrays = []
wing_pressure_case.PointArrays = ['pressure']

# Extract wing

wing=ExtractBlock(wing_pressure_case)
wing.BlockIndices = [2]

# Compute normals

wingSurface=ExtractSurface(wing)
wingSurfaceWithNormals=GenerateSurfaceNormals(wingSurface)

# Clip the wing

wingClip1=Clip(wingSurfaceWithNormals,ClipType="Plane")
wingClip1.Scalars = ['POINTS', 'pressure']
wingClip1.ClipType.Origin = [0.0, 0.0, 1000.0]
wingClip1.ClipType.Normal = [0.0, 0.0, 1.0]

wingClip2=Clip(wingClip1,ClipType="Plane")
wingClip2.Scalars = ['POINTS', 'pressure']
wingClip2.ClipType.Origin = [0.0, 0.0, 1010.0]
wingClip2.ClipType.Normal = [0.0, 0.0, -1.0]

# Get the local area

areaCalculator=PythonCalculator(wingClip2)
areaCalculator.ArrayAssociation='Point Data'
areaCalculator.ArrayName='area'
areaCalculator.Expression='area(inputs[0])'

# Get data

data=servermanager.Fetch(areaCalculator)

pointData=data.GetPointData() # <------- Doesn't work ! :-( ???
Attached Files
File Type: zip compute_local_lift_coefficient_v2.py.zip (704 Bytes, 22 views)
taalf is offline   Reply With Quote

Old   October 15, 2013, 17:18
Default
  #9
Retired Super Moderator
 
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 10,982
Blog Entries: 45
Rep Power: 128
wyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to all
Hi William,

I won't be able to look into the code before the weekend, but there is a quick tip I can give right now... actually, I already mentioned in the previous post, so I'll rephrase it:
  1. The Python shell in ParaView (accessible from the menu "Tools"), gives full access from an outside view of the Python environment. In other words, it has it's own workspace.
  2. The "Python Calculator", as well as the "Programmable Filter", use their own Python workspaces and do not provide the full feature-set that is available on the Python Shell.
Nonetheless, if my memory doesn't fail me, the "print" function works on both workspaces and should be able to print out to the Python Shell, therefore this is a way that you can use to diagnose how things are working inside the other workspace.


Another tip is from another thread and I quote:
Quote:
Originally Posted by mpeti View Post
I believe it is worth mentioning that it is possible to put the debugger breakpoints (pdb.set_trace()) in the progFilter.Script string and this way to figure out what exactly the input of the filter is like.
I've never used this myself, but it seems promising.

Best regards,
Bruno
CFDelix likes this.
__________________
wyldckat is offline   Reply With Quote

Old   October 16, 2013, 09:10
Default
  #10
Member
 
William Tougeron
Join Date: Jan 2011
Location: Czech Republic
Posts: 70
Rep Power: 15
taalf is on a distinguished road
Hi @wyldckat,

Thank you for your time!

I read you, trust you, but I still don't see how to do...

But the thing which is very strange to me is that my script works perfectly with a Python Calculator made on a source Sphere, but not with the same Python Calculator made on my wing surface...

Here is my script:

Code:
try: paraview.simple
except: from paraview.simple import *

test_on='wing'
#test_on='sphere'

if test_on=='wing':
    
    # Import data
    
    wing_pressure_case = EnSightReader( CaseFileName='/home/tougeron/test/wing_pressure.case' )
    wing_pressure_case.CellArrays = []
    wing_pressure_case.PointArrays = ['pressure']
    
    # Extract wing
    
    wing=ExtractBlock(wing_pressure_case)
    wing.BlockIndices = [2]
    
    # Compute normals
    
    wingSurface=ExtractSurface(wing)
    wingSurfaceWithNormals=GenerateSurfaceNormals(wingSurface)
    
    # Clip the wing
    
    wingClip1=Clip(wingSurfaceWithNormals,ClipType="Plane")
    wingClip1.Scalars = ['POINTS', 'pressure']
    wingClip1.ClipType.Origin = [0.0, 0.0, 1000.0]
    wingClip1.ClipType.Normal = [0.0, 0.0, 1.0]
    
    wingClip2=Clip(wingClip1,ClipType="Plane")
    wingClip2.Scalars = ['POINTS', 'pressure']
    wingClip2.ClipType.Origin = [0.0, 0.0, 1010.0]
    wingClip2.ClipType.Normal = [0.0, 0.0, -1.0]
    
    # Get the local area
    
    areaCalculator=PythonCalculator(wingClip2)
    

if test_on=='sphere':
    
    # Create a sphere
    
    Sphere1 = Sphere()
    Sphere1.Radius = 5.0
    Sphere1.ThetaResolution = 36
    Sphere1.PhiResolution = 36
    
    # Get the local area
    
    areaCalculator=PythonCalculator(Sphere1)
    

areaCalculator.ArrayAssociation='Point Data'
areaCalculator.ArrayName='area'
areaCalculator.Expression='area(inputs[0])'

# Get data

data=servermanager.Fetch(areaCalculator)

nbPoints=data.GetNumberOfPoints()
print 'Found',nbPoints,'points'

pointData=data.GetPointData() # <------- Doesn't work with wing! :-( ???

areaArray=pointData.GetArray('area')

area=[]
for i in range(nbPoints): area.append(areaArray.GetTuple(i)[0])

print len(area),"values got successfully:"
print area[0]
print area[1]
print area[2]
print "..."
When the
Code:
#test_on='sphere'
line is uncommented, so the script can get the local area from the areaCalculator object which is a Python Calculator of which the source is a Sphere.

Output:

Quote:
Found 1226 points
1226 values got successfully:
0.0174589491329
0.0174589529311
0.0174589475408
...
When the same line is commented, the areaCalculator is made exactly by the same way but have the wing clip as a source. And in this case I get the following error:

Quote:
Found 354 points
Traceback (most recent call last):
File "<string>", line 69, in <module>
AttributeError: GetPointData
. . . . . .

Attached Files
File Type: zip compute_local_lift_coefficient_v2-with_sphere_test.py.zip (969 Bytes, 28 views)
Pagoda likes this.
taalf is offline   Reply With Quote

Old   October 16, 2013, 15:01
Default
  #11
Retired Super Moderator
 
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 10,982
Blog Entries: 45
Rep Power: 128
wyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to all
Hi William,

A quick question: does it work if you work with it manually on ParaView?

Because I'm guessing that you don't know about the method "UpdatePipeline()": http://paraview.org/Wiki/ParaView/Py...ing_a_Pipeline
Quote:
At this point, if you are interested in getting some information about the output of the shrink filter, you can force it to update (which will also cause the execution of the cone source). For details about VTK's demand-driven pipeline model used by ParaView, see one of the VTK books.
Code:
>>> shrinkFilter.UpdatePipeline()
>>> shrinkFilter.GetDataInformation().GetNumberOfCells()
33L
>>> shrinkFilter.GetDataInformation().GetNumberOfPoints()
128L
As you can see, the data should only be available after you've forced an update.

Best regards,
Bruno
__________________
wyldckat is offline   Reply With Quote

Old   October 17, 2013, 03:59
Default
  #12
Member
 
William Tougeron
Join Date: Jan 2011
Location: Czech Republic
Posts: 70
Rep Power: 15
taalf is on a distinguished road
Yahaaaa !

Thank you very much @Wyldckat !

with a
Code:
areaCalculator.UpdatePipeline()
line before the "Fetch" command it works perfectly!

I hope you don't think I don't read the documentation

Just that it is lots of info at a time and I don't have much time to develop my script...

Merci infiniment !

Best regards,

William

Last edited by taalf; October 17, 2013 at 05:06.
taalf is offline   Reply With Quote

Old   October 17, 2013, 07:18
Default
  #13
Member
 
William Tougeron
Join Date: Jan 2011
Location: Czech Republic
Posts: 70
Rep Power: 15
taalf is on a distinguished road


Spoke to fast...

It doesn't change anything actually..........................

taalf is offline   Reply With Quote

Old   October 17, 2013, 15:32
Default
  #14
Retired Super Moderator
 
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 10,982
Blog Entries: 45
Rep Power: 128
wyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to all
Hi William,

Try on the other variables before that part.
The biggest suspect is "wing_pressure_case" since it's the reader and seems to be a bit more tricky to use, given its configuration settings.

And if "UpdatePipeline()" doesn't work, try "Update()".

Best regards,
Bruno
__________________
wyldckat is offline   Reply With Quote

Old   October 18, 2013, 07:27
Default
  #15
Member
 
William Tougeron
Join Date: Jan 2011
Location: Czech Republic
Posts: 70
Rep Power: 15
taalf is on a distinguished road
Hi @Wyldckat,

I gave up with this method.

I put an UpdatePipe() on all objects. I tried Update() without success.

I came back to the CSV file solution.

I export the data into a temporary CSV file, then import it with the csv python library and delete it. It takes one cent of second and it is very robust.

Maybe I will later understand what I did wrong.

Thank you very much for your precious help anyway !

Best regards,

William
taalf is offline   Reply With Quote

Old   October 19, 2013, 15:55
Default
  #16
Retired Super Moderator
 
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 10,982
Blog Entries: 45
Rep Power: 128
wyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to all
Hi William,

OK, I've figured out the problem. If you had checked what "data" was, it would tell you this:
Code:
>>> data
(vtkMultiBlockDataSet)0x5b143c0
"MultiBlocks" have to be dealt differently, since each one of the blocks contains a part of the geometry+data. In this case, it's likely just one block, but still it is packed inside a "MultiBlock".

To unwrap the data, you can use "MergeBlocks":
Code:
mergedAreaCalculator=MergeBlocks(areaCalculator)

# Get data

data=servermanager.Fetch(mergedAreaCalculator)
The rest of the code should now work as intended!

If I'm not mistaken, this filter can be applied directly to the reader, so that you won't have to be bothered by it later on in the code .

Best regards,
Bruno
elvis and lukasf like this.
__________________
wyldckat is offline   Reply With Quote

Old   October 21, 2013, 04:38
Default
  #17
Member
 
William Tougeron
Join Date: Jan 2011
Location: Czech Republic
Posts: 70
Rep Power: 15
taalf is on a distinguished road
Hello, @Wyldckat

I had seen that the wing data was under multi-block format and noticed this was the only one difference between it and the source sphere case.

But I really didn't know what to do with this! I tried lots of things, and finally gave up!

Now my script is ready to use with the CSV solution. Don't know if I will use the MergeBlock solution on it.

But I am happy to see the source of the mistake ! (I tested on my test script, it works)

Thank you very much!

Best regards,

William
wyldckat likes this.
taalf is offline   Reply With Quote

Old   February 15, 2018, 05:53
Default Using mergeBlock in Paraview
  #18
New Member
 
Ouardia
Join Date: Feb 2018
Posts: 2
Rep Power: 0
Ouardia is on a distinguished road
Hi all,

I have a question about using mergeBlock in Paraview ! I tried to save the data on a .vtk file, it works for the first iteration but I need to save the last iteration's data in my .vtk file.

Any one have an idea please ?

Thanks
Ouardia is offline   Reply With Quote

Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[General] “Upload” vtk data from client to server in paraview script Jack001 ParaView 0 March 8, 2018 08:27
python script to create geometry for salome, and then mesh 6863523 Mesh Generation & Pre-Processing 4 March 18, 2017 10:00
Run OpenFoam in 2 nodes of a cluster WhiteW OpenFOAM Running, Solving & CFD 16 December 20, 2016 01:51
How to use python script in paraFoam? Thomas pan OpenFOAM Post-Processing 0 October 13, 2015 22:29
[OpenFOAM] How to use python script in paraFoam? Thomas pan ParaView 0 October 12, 2015 05:34


All times are GMT -4. The time now is 02:56.