|
[Sponsors] |
[General] Get data from Calculator filter in python script |
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
October 4, 2013, 09:20 |
Get data from Calculator filter in python script
|
#1 |
Member
William Tougeron
Join Date: Jan 2011
Location: Czech Republic
Posts: 70
Rep Power: 15 |
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. |
|
October 6, 2013, 13:56 |
|
#2 |
Retired Super Moderator
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 10,982
Blog Entries: 45
Rep Power: 128 |
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'] Best regards, Bruno
__________________
|
|
October 7, 2013, 04:50 |
|
#3 |
Member
William Tougeron
Join Date: Jan 2011
Location: Czech Republic
Posts: 70
Rep Power: 15 |
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 |
|
October 7, 2013, 08:56 |
|
#4 |
Member
William Tougeron
Join Date: Jan 2011
Location: Czech Republic
Posts: 70
Rep Power: 15 |
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 |
|
October 7, 2013, 10:11 |
|
#5 |
Member
William Tougeron
Join Date: Jan 2011
Location: Czech Republic
Posts: 70
Rep Power: 15 |
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 |
|
October 13, 2013, 07:33 |
|
#6 |
Retired Super Moderator
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 10,982
Blog Entries: 45
Rep Power: 128 |
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) Bruno
__________________
|
|
October 14, 2013, 09:17 |
|
#7 |
Member
William Tougeron
Join Date: Jan 2011
Location: Czech Republic
Posts: 70
Rep Power: 15 |
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 |
|
October 15, 2013, 13:06 |
|
#8 |
Member
William Tougeron
Join Date: Jan 2011
Location: Czech Republic
Posts: 70
Rep Power: 15 |
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) Code:
pointData=data.GetPointData() 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 ! :-( ??? |
|
October 15, 2013, 17:18 |
|
#9 | |
Retired Super Moderator
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 10,982
Blog Entries: 45
Rep Power: 128 |
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:
Another tip is from another thread and I quote: Quote:
Best regards, Bruno
__________________
|
||
October 16, 2013, 09:10 |
|
#10 | ||
Member
William Tougeron
Join Date: Jan 2011
Location: Czech Republic
Posts: 70
Rep Power: 15 |
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 "..." Code:
#test_on='sphere' Output: Quote:
Quote:
|
|||
October 16, 2013, 15:01 |
|
#11 | |
Retired Super Moderator
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 10,982
Blog Entries: 45
Rep Power: 128 |
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:
Best regards, Bruno
__________________
|
||
October 17, 2013, 03:59 |
|
#12 |
Member
William Tougeron
Join Date: Jan 2011
Location: Czech Republic
Posts: 70
Rep Power: 15 |
Yahaaaa !
Thank you very much @Wyldckat ! with a Code:
areaCalculator.UpdatePipeline() 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. |
|
October 17, 2013, 07:18 |
|
#13 |
Member
William Tougeron
Join Date: Jan 2011
Location: Czech Republic
Posts: 70
Rep Power: 15 |
Spoke to fast... It doesn't change anything actually.......................... |
|
October 17, 2013, 15:32 |
|
#14 |
Retired Super Moderator
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 10,982
Blog Entries: 45
Rep Power: 128 |
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
__________________
|
|
October 18, 2013, 07:27 |
|
#15 |
Member
William Tougeron
Join Date: Jan 2011
Location: Czech Republic
Posts: 70
Rep Power: 15 |
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 |
|
October 19, 2013, 15:55 |
|
#16 |
Retired Super Moderator
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 10,982
Blog Entries: 45
Rep Power: 128 |
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 To unwrap the data, you can use "MergeBlocks": Code:
mergedAreaCalculator=MergeBlocks(areaCalculator) # Get data data=servermanager.Fetch(mergedAreaCalculator) 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
__________________
|
|
October 21, 2013, 04:38 |
|
#17 |
Member
William Tougeron
Join Date: Jan 2011
Location: Czech Republic
Posts: 70
Rep Power: 15 |
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 |
|
February 15, 2018, 05:53 |
Using mergeBlock in Paraview
|
#18 |
New Member
Ouardia
Join Date: Feb 2018
Posts: 2
Rep Power: 0 |
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 |
|
|
|
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 |