|
[Sponsors] |
[General] Extracting ParaView Data into Python Arrays |
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
January 5, 2023, 21:00 |
|
#21 |
Member
Santhosh
Join Date: Nov 2021
Posts: 44
Rep Power: 5 |
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 |
|
January 6, 2023, 04:05 |
|
#22 | |
Senior Member
Lukas Fischer
Join Date: May 2018
Location: Germany, Munich
Posts: 117
Rep Power: 8 |
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:
|
||
January 6, 2023, 14:53 |
|
#23 | |
Member
Santhosh
Join Date: Nov 2021
Posts: 44
Rep Power: 5 |
Quote:
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 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. |
||
January 9, 2023, 04:56 |
|
#24 | |
Senior Member
Lukas Fischer
Join Date: May 2018
Location: Germany, Munich
Posts: 117
Rep Power: 8 |
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) 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:
|
||
January 9, 2023, 18:34 |
|
#25 |
Member
Santhosh
Join Date: Nov 2021
Posts: 44
Rep Power: 5 |
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 ? |
|
January 10, 2023, 04:49 |
|
#26 | |
Senior Member
Lukas Fischer
Join Date: May 2018
Location: Germany, Munich
Posts: 117
Rep Power: 8 |
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:
|
||
January 10, 2023, 22:18 |
|
#27 | |
Member
Santhosh
Join Date: Nov 2021
Posts: 44
Rep Power: 5 |
Quote:
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")) 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’ 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 |
||
January 11, 2023, 03:30 |
|
#28 | |
Senior Member
Lukas Fischer
Join Date: May 2018
Location: Germany, Munich
Posts: 117
Rep Power: 8 |
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... 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:
|
||
January 12, 2023, 21:54 |
|
#29 |
Member
Santhosh
Join Date: Nov 2021
Posts: 44
Rep Power: 5 |
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 |
|
January 18, 2023, 08:37 |
More information in a different thread
|
#30 |
Senior Member
Lukas Fischer
Join Date: May 2018
Location: Germany, Munich
Posts: 117
Rep Power: 8 |
||
November 6, 2023, 22:00 |
|
#31 |
New Member
Join Date: Aug 2017
Posts: 16
Rep Power: 9 |
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. |
|
Tags |
openfoam, paraview 3.10, python, vtk |
|
|
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 |