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

[General] Extracting ParaView Data into Python Arrays

Register Blogs Community New Posts Updated Threads Search

Like Tree14Likes

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   January 5, 2023, 21:00
Default
  #21
Member
 
Santhosh
Join Date: Nov 2021
Posts: 44
Rep Power: 5
Santhosh91 is on a distinguished road
Hello guys,

I found this thread really interesting. I was wondering, if there is a way to go trough cell datas information across multiple slices alongside a pipe since the usuals ''GetPoints'', ''GetNumbersOfPoints'' and ''SetPoint'' does not work with the Slice class.

Any help would be greatly appreciated
Santhosh91 is offline   Reply With Quote

Old   January 6, 2023, 04:05
Default
  #22
Senior Member
 
Lukas Fischer
Join Date: May 2018
Location: Germany, Munich
Posts: 117
Rep Power: 8
lukasf is on a distinguished road
Hi,

if you run a python script in the graphical mode of paraview (GUI) you first apply all needed filters, e.g. your slice, to your data.

Use "tools/start trace" and "tools/stop trace" to get the python code from ParaView.

Then you could write a for loop to get data along your pipe e.g. if x-direction the main flow direction:

Code:
 #import all needed packages


 #x locations of interest
x_list = [0, 1, 2, 3] #made up values

for x in x_list:

  #if your slice is called slice1 in paraview
  slice = FindSource("slice1")

  slice.SliceType.Origin = [x, 0, 0]

  slice.SliceType.Normal = [1, 0, 0]
  #then apply the commands I have shown in my post:


  m = MergeBlocks(slice)
  d = servermanager.Fetch(m)

  #continue the commands I have shown 
  #save or stack the data to a new array for all x values
Quote:
Originally Posted by Santhosh91 View Post
Hello guys,

I found this thread really interesting. I was wondering, if there is a way to go trough cell datas information across multiple slices alongside a pipe since the usuals ''GetPoints'', ''GetNumbersOfPoints'' and ''SetPoint'' does not work with the Slice class.

Any help would be greatly appreciated
lukasf is offline   Reply With Quote

Old   January 6, 2023, 14:53
Default
  #23
Member
 
Santhosh
Join Date: Nov 2021
Posts: 44
Rep Power: 5
Santhosh91 is on a distinguished road
Quote:
Originally Posted by lukasf View Post
Hi,

if you run a python script in the graphical mode of paraview (GUI) you first apply all needed filters, e.g. your slice, to your data.

Use "tools/start trace" and "tools/stop trace" to get the python code from ParaView.

Then you could write a for loop to get data along your pipe e.g. if x-direction the main flow direction:

Code:
 #import all needed packages


 #x locations of interest
x_list = [0, 1, 2, 3] #made up values

for x in x_list:

  #if your slice is called slice1 in paraview
  slice = FindSource("slice1")

  slice.SliceType.Origin = [x, 0, 0]

  slice.SliceType.Normal = [1, 0, 0]
  #then apply the commands I have shown in my post:


  m = MergeBlocks(slice)
  d = servermanager.Fetch(m)

  #continue the commands I have shown 
  #save or stack the data to a new array for all x values

Hello Lukas,
Thanks a lot for the help ! I am starting to get some advances.


I managed to get the array of the average velocity field going trough the different slices. Here is my code :
Code:
    #Import the useful library
  from paraview.simple import *
  import numpy as np
  from paraview import numpy_support as ns
  import vtk
  
  # Get active boundary (actualy a 3D wing from a mesh with pressure data)
  Junction = GetActiveSource()
  
  # For several positions on the z axis
  for z in np.arange(2e-6,28e-6,2e-6):
      Slice1 = Slice(Input=Junction)
      Slice1.SliceType = "Plane"
      Slice1.SliceOffsetValues = [0.0]
      Slice1.SliceType.Origin = [0.0, 0.0, z]
      Slice1.SliceType.Normal = [0.0, 0.0, 1.0]
      Z.append(Slice1)
  
  # Took two slice as an example but for multiple ones, just repeat the procedure
  
  Slice1 = FindSource("Slice1")
  m1 = MergeBlocks(Slice1)
  d1 = servermanager.Fetch(m1)
  d1.GetCellData()
  u1= ns.vtk_to_numpy(d1.GetCellData().GetArray("U"))
  #numCells = u1.GetNumberOfCells()
  
  Slice2 = FindSource("Slice2")
  m2 = MergeBlocks(Slice2)
  d2 = servermanager.Fetch(m2)
  d2.GetCellData()
  u2=ns.vtk_to_numpy(d2.GetCellData().GetArray("U"))
  
  uvg = (u1+u2)/2
I was wondering now how can I render this field (uvg) as a color map for instance ?
Can I ''rebuild'' a slice away from my gemeotry where I can stock the average field and then access it on the Render view ?


Hope you can help me debug this. Thanks a lot for your time and help.


Sincerely,
Santhosh

Last edited by Santhosh91; January 6, 2023 at 16:59.
Santhosh91 is offline   Reply With Quote

Old   January 9, 2023, 04:56
Default
  #24
Senior Member
 
Lukas Fischer
Join Date: May 2018
Location: Germany, Munich
Posts: 117
Rep Power: 8
lukasf is on a distinguished road
Hi,

If you want to take screenshots of the GUI's Render Views then you do not have to use the approach to extract the data to python arrays. You can just set up your render views and loop through each render view and take screen shots:

Code:
renderviews = GetRenderViews()

for j, renderview in enumerate(renderviews):            

            Render()        

            SaveScreenshot(image_save_folder, magnification=1, quality=100, view=renderview)
If you like to render your uvg field, you should use the "calculator" filter in paraview.

I think that the paraview images are not good enough for scientific papers. That is why I like to access the data and turn these into numpy arrays to plot e.g. 2D Contours with matplotlib.


https://matplotlib.org/3.1.1/gallery...lardatagrid-py


Quote:
Originally Posted by Santhosh91 View Post
Hello Lukas,
Thanks a lot for the help ! I am starting to get some advances.


I managed to get the array of the average velocity field going trough the different slices. Here is my code :
Code:
    #Import the useful library
  from paraview.simple import *
  import numpy as np
  from paraview import numpy_support as ns
  import vtk
  
  # Get active boundary (actualy a 3D wing from a mesh with pressure data)
  Junction = GetActiveSource()
  
  # For several positions on the z axis
  for z in np.arange(2e-6,28e-6,2e-6):
      Slice1 = Slice(Input=Junction)
      Slice1.SliceType = "Plane"
      Slice1.SliceOffsetValues = [0.0]
      Slice1.SliceType.Origin = [0.0, 0.0, z]
      Slice1.SliceType.Normal = [0.0, 0.0, 1.0]
      Z.append(Slice1)
  
  # Took two slice as an example but for multiple ones, just repeat the procedure
  
  Slice1 = FindSource("Slice1")
  m1 = MergeBlocks(Slice1)
  d1 = servermanager.Fetch(m1)
  d1.GetCellData()
  u1= ns.vtk_to_numpy(d1.GetCellData().GetArray("U"))
  #numCells = u1.GetNumberOfCells()
  
  Slice2 = FindSource("Slice2")
  m2 = MergeBlocks(Slice2)
  d2 = servermanager.Fetch(m2)
  d2.GetCellData()
  u2=ns.vtk_to_numpy(d2.GetCellData().GetArray("U"))
  
  uvg = (u1+u2)/2
I was wondering now how can I render this field (uvg) as a color map for instance ?
Can I ''rebuild'' a slice away from my gemeotry where I can stock the average field and then access it on the Render view ?


Hope you can help me debug this. Thanks a lot for your time and help.


Sincerely,
Santhosh
lukasf is offline   Reply With Quote

Old   January 9, 2023, 18:34
Default
  #25
Member
 
Santhosh
Join Date: Nov 2021
Posts: 44
Rep Power: 5
Santhosh91 is on a distinguished road
Hello Lukas,


Thanks for the response.


I am not sure to understand the first method. It seems I need to go trough the python arrays, because it is the only way for me to sum cell velocity values across the slices. Otherway, using the calculator and screenshots will just give me multiple averaged slice instead on one that correspond to the average of all ?



For the second method, I can't import matplotlib.pyplot on python shell, is there something I need to do ?
Santhosh91 is offline   Reply With Quote

Old   January 10, 2023, 04:49
Default
  #26
Senior Member
 
Lukas Fischer
Join Date: May 2018
Location: Germany, Munich
Posts: 117
Rep Power: 8
lukasf is on a distinguished road
Hi,


In my opinion if you want to sum up the axial profiles you will need to extract the data into python arrays, stack these and determine the average. Then you need to use e.g. the matplotlib library to plot it.



I do not think that you can recreate a renderview in paraview to visualize the average field. You may create a VTK file after averaging and reread this file into paraview to visualize it. I would prefer to use the matplotlib library for plotting directly.



If your python and paraview installation does not have certain packages you need to install these.



Quote:
Originally Posted by Santhosh91 View Post
Hello Lukas,


Thanks for the response.


I am not sure to understand the first method. It seems I need to go trough the python arrays, because it is the only way for me to sum cell velocity values across the slices. Otherway, using the calculator and screenshots will just give me multiple averaged slice instead on one that correspond to the average of all ?



For the second method, I can't import matplotlib.pyplot on python shell, is there something I need to do ?
lukasf is offline   Reply With Quote

Old   January 10, 2023, 22:18
Default
  #27
Member
 
Santhosh
Join Date: Nov 2021
Posts: 44
Rep Power: 5
Santhosh91 is on a distinguished road
Quote:
Originally Posted by lukasf View Post
Hi,


In my opinion if you want to sum up the axial profiles you will need to extract the data into python arrays, stack these and determine the average. Then you need to use e.g. the matplotlib library to plot it.



I do not think that you can recreate a renderview in paraview to visualize the average field. You may create a VTK file after averaging and reread this file into paraview to visualize it. I would prefer to use the matplotlib library for plotting directly.

Thanks a lot for clarifying I understand it now.



I still have two issues left.


When I try to plot my field uavg trough matplotlib I get the figure ''uavg" (attached file) which does not look like the figure ''meshJunction'' (attached file) that I would like. It is surely due to the fact that I don't have to cell coordinates (x,y,z) associated to the velocity datas that I extracted with the previous code

Code:
Slice1 = FindSource("Slice1")
  m1 = MergeBlocks(Slice1)
  d1 = servermanager.Fetch(m1)
  d1.GetCellData()
  u1= ns.vtk_to_numpy(d1.GetCellData().GetArray("U"))
Is there a mean to get the coordinate of the cells associate to "u1" array ? I tried to look into but didn't find anything.


Second (less important problem). I cannot plot matplotlib on my GUI in Paraview. Just following the exemple of the paraview guide : https://docs.paraview.org/en/latest/...ayingData.html
The bluntfin.vts dataset. I get the following error on Paraview while I am just copy pasting the exemple response :
Code:
Traceback (most recent call last):
File “C:/Program  Files/blueCFD-Core-2020/AddOns/ParaView/bin\Lib/site-packages\paraview\python_view.py”,  line 167, in call_render
image = render_function(view, width, height)
File “Script”, line 62, in render
File “C:/Program  Files/blueCFD-Core-2020/AddOns/ParaView/bin\Lib/site-packages\vtkmodules\util\numpy_support.py”,  line 215, in vtk_to_numpy
typ = vtk_array.GetDataType()
AttributeError: ‘NoneType’ object has no attribute ‘GetDataType’
I understand perfectly, if it's not possible to help me on this second problem since it looks more related to Paraview than OpenFoam but I am trying in case you already faced this issue.


Again Thanks a lot for the time and involvement in my problem Lukas. It helped me advanced a lot.

Hopefully, you can help me once more.


Sincerely,
Santhosh
Attached Images
File Type: png uavg.png (28.3 KB, 10 views)
File Type: jpg Meshjunction.jpg (123.7 KB, 14 views)
Santhosh91 is offline   Reply With Quote

Old   January 11, 2023, 03:30
Default
  #28
Senior Member
 
Lukas Fischer
Join Date: May 2018
Location: Germany, Munich
Posts: 117
Rep Power: 8
lukasf is on a distinguished road
Hi,


in the first image you just plotted data with the "plot" function. However, you need to do a contour plot and not simple plot. Please, look at the example of the matplotlib link I gave you.


Note, to acces the coordinates of the cells I do the following (I mentioned this in this thread before):


Code:
#cellCenters1    
cellCenters1 = CellCenters(Input=Filter_Object_of_interest_like_a  _slice)
cellCenters1.VertexCells = 0
cellCenters1.UpdatePipeline()

#create a calculator and calculate Points
#otherwise GetPointData().GetArray('Points') does not find any Points

Calculator1 = Calculator(Input=cellCenters1)
Calculator1.ResultArrayName = 'Points'
 Calculator1.Function = 'coords' 

 data_cell = servermanager.Fetch(Calculator1)
points = ns.vtk_to_numpy(data_cell.GetPointData().GetArray(  'Points'))        
x =  points[:,0]
y =  points[:,1]
#etc...
Next, stack your profiles and determine the average field. Then save this array to a file.

Use a different python module (not the python within paraview) to reread this file and plot the data using e.g. matplotlib. You could even use MATLAB or any other tool afterwards.

Take your time and you will figure it out.



Quote:
Originally Posted by Santhosh91 View Post
Thanks a lot for clarifying I understand it now.



I still have two issues left.


When I try to plot my field uavg trough matplotlib I get the figure ''uavg" (attached file) which does not look like the figure ''meshJunction'' (attached file) that I would like. It is surely due to the fact that I don't have to cell coordinates (x,y,z) associated to the velocity datas that I extracted with the previous code

Code:
Slice1 = FindSource("Slice1")
  m1 = MergeBlocks(Slice1)
  d1 = servermanager.Fetch(m1)
  d1.GetCellData()
  u1= ns.vtk_to_numpy(d1.GetCellData().GetArray("U"))
Is there a mean to get the coordinate of the cells associate to "u1" array ? I tried to look into but didn't find anything.


Second (less important problem). I cannot plot matplotlib on my GUI in Paraview. Just following the exemple of the paraview guide : https://docs.paraview.org/en/latest/...ayingData.html
The bluntfin.vts dataset. I get the following error on Paraview while I am just copy pasting the exemple response :
Code:
Traceback (most recent call last):
File “C:/Program  Files/blueCFD-Core-2020/AddOns/ParaView/bin\Lib/site-packages\paraview\python_view.py”,  line 167, in call_render
image = render_function(view, width, height)
File “Script”, line 62, in render
File “C:/Program  Files/blueCFD-Core-2020/AddOns/ParaView/bin\Lib/site-packages\vtkmodules\util\numpy_support.py”,  line 215, in vtk_to_numpy
typ = vtk_array.GetDataType()
AttributeError: ‘NoneType’ object has no attribute ‘GetDataType’
I understand perfectly, if it's not possible to help me on this second problem since it looks more related to Paraview than OpenFoam but I am trying in case you already faced this issue.


Again Thanks a lot for the time and involvement in my problem Lukas. It helped me advanced a lot.

Hopefully, you can help me once more.


Sincerely,
Santhosh
lukasf is offline   Reply With Quote

Old   January 12, 2023, 21:54
Default
  #29
Member
 
Santhosh
Join Date: Nov 2021
Posts: 44
Rep Power: 5
Santhosh91 is on a distinguished road
Hello Lukas,


Thanks for everything. I managed to export the datas and use python modules to have a proper colormap.



Have a good day !



Sincerely,
Santhosh
lukasf likes this.
Santhosh91 is offline   Reply With Quote

Old   January 18, 2023, 08:37
Default More information in a different thread
  #30
Senior Member
 
Lukas Fischer
Join Date: May 2018
Location: Germany, Munich
Posts: 117
Rep Power: 8
lukasf is on a distinguished road
In this thread more information can be found:



Get data from Calculator filter in python script
lukasf is offline   Reply With Quote

Old   November 6, 2023, 22:00
Default
  #31
New Member
 
Join Date: Aug 2017
Posts: 16
Rep Power: 9
Wang Shang is on a distinguished road
Hi Lukas,

Recently I need to batch process the data, it is very troublesome to repeat the operation in the paraview, and the graph is not very beautiful. I noticed this thread, could you please show a piece of code to specify how to slice, export coordinates and field data into an array, and plot using matplotlib. Since I am a beginner, this will be of great help to me.

Thanks.
Wang Shang is offline   Reply With Quote

Reply

Tags
openfoam, paraview 3.10, python, vtk


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] Paraview crashes using "Find Data" functionality Turbine ParaView 3 May 6, 2020 23:40
[OpenFOAM] Paraview Find Data cojua8 ParaView 2 March 19, 2018 09:57
[General] “Upload” vtk data from client to server in paraview script Jack001 ParaView 0 March 8, 2018 08:27
[General] Determining number of cores and/or paraview executable via python aerogt3 ParaView 0 December 9, 2014 06:54
studying a valve case mina.basta OpenFOAM 33 August 30, 2013 05:46


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