CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > ParaView

Get data from Calculator filter in python script

Register Blogs Members List Search Today's Posts Mark Forums Read

Like Tree8Likes
  • 1 Post By taalf
  • 3 Post By wyldckat
  • 1 Post By taalf
  • 1 Post By taalf
  • 1 Post By wyldckat
  • 1 Post By taalf

Reply
 
LinkBack Thread Tools Display Modes
Old   October 4, 2013, 08:20
Unhappy Get data from Calculator filter in python script
  #1
Member
 
taalf's Avatar
 
William Tougeron
Join Date: Jan 2011
Location: Czech Republic
Posts: 43
Rep Power: 6
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 02:22.
taalf is offline   Reply With Quote

Old   October 6, 2013, 12:56
Default
  #2
Super Moderator
 
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 8,516
Blog Entries: 34
Rep Power: 86
wyldckat is just really nicewyldckat is just really nicewyldckat is just really nicewyldckat is just really nice
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, 03:50
Default
  #3
Member
 
taalf's Avatar
 
William Tougeron
Join Date: Jan 2011
Location: Czech Republic
Posts: 43
Rep Power: 6
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, 18 views)
Pagoda likes this.
taalf is offline   Reply With Quote

Old   October 7, 2013, 07:56
Default
  #4
Member
 
taalf's Avatar
 
William Tougeron
Join Date: Jan 2011
Location: Czech Republic
Posts: 43
Rep Power: 6
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, 09:11
Default
  #5
Member
 
taalf's Avatar
 
William Tougeron
Join Date: Jan 2011
Location: Czech Republic
Posts: 43
Rep Power: 6
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, 06:33
Default
  #6
Super Moderator
 
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 8,516
Blog Entries: 34
Rep Power: 86
wyldckat is just really nicewyldckat is just really nicewyldckat is just really nicewyldckat is just really nice
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 and Pagoda like this.
wyldckat is offline   Reply With Quote

Old   October 14, 2013, 08:17
Default
  #7
Member
 
taalf's Avatar
 
William Tougeron
Join Date: Jan 2011
Location: Czech Republic
Posts: 43
Rep Power: 6
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, 36 views)
wyldckat likes this.
taalf is offline   Reply With Quote

Old   October 15, 2013, 12:06
Default
  #8
Member
 
taalf's Avatar
 
William Tougeron
Join Date: Jan 2011
Location: Czech Republic
Posts: 43
Rep Power: 6
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, 7 views)
taalf is offline   Reply With Quote

Old   October 15, 2013, 16:18
Default
  #9
Super Moderator
 
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 8,516
Blog Entries: 34
Rep Power: 86
wyldckat is just really nicewyldckat is just really nicewyldckat is just really nicewyldckat is just really nice
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
wyldckat is offline   Reply With Quote

Old   October 16, 2013, 08:10
Default
  #10
Member
 
taalf's Avatar
 
William Tougeron
Join Date: Jan 2011
Location: Czech Republic
Posts: 43
Rep Power: 6
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, 8 views)
Pagoda likes this.
taalf is offline   Reply With Quote

Old   October 16, 2013, 14:01
Default
  #11
Super Moderator
 
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 8,516
Blog Entries: 34
Rep Power: 86
wyldckat is just really nicewyldckat is just really nicewyldckat is just really nicewyldckat is just really nice
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, 02:59
Default
  #12
Member
 
taalf's Avatar
 
William Tougeron
Join Date: Jan 2011
Location: Czech Republic
Posts: 43
Rep Power: 6
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 04:06.
taalf is offline   Reply With Quote

Old   October 17, 2013, 06:18
Default
  #13
Member
 
taalf's Avatar
 
William Tougeron
Join Date: Jan 2011
Location: Czech Republic
Posts: 43
Rep Power: 6
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, 14:32
Default
  #14
Super Moderator
 
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 8,516
Blog Entries: 34
Rep Power: 86
wyldckat is just really nicewyldckat is just really nicewyldckat is just really nicewyldckat is just really nice
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, 06:27
Default
  #15
Member
 
taalf's Avatar
 
William Tougeron
Join Date: Jan 2011
Location: Czech Republic
Posts: 43
Rep Power: 6
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, 14:55
Default
  #16
Super Moderator
 
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 8,516
Blog Entries: 34
Rep Power: 86
wyldckat is just really nicewyldckat is just really nicewyldckat is just really nicewyldckat is just really nice
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 likes this.
wyldckat is offline   Reply With Quote

Old   October 21, 2013, 03:38
Default
  #17
Member
 
taalf's Avatar
 
William Tougeron
Join Date: Jan 2011
Location: Czech Republic
Posts: 43
Rep Power: 6
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

Reply

Thread Tools
Display Modes

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 On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
【Help】"Error: Update_Time_Level: invalid data" Chen FLUENT 2 August 24, 2014 07:51
Python script to export animation images mahyarv14 OpenFOAM Paraview & paraFoam 10 August 16, 2014 11:11
Python Script for complete lift polar taxalian SU2 4 January 11, 2014 23:12
export data at nodes Meenu FLUENT 1 December 30, 2011 02:24
Problem in running ICEM grid in Openfoam Tarak OpenFOAM 6 September 9, 2011 17:51


All times are GMT -4. The time now is 10:45.