CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > OpenFOAM Paraview & paraFoam

Paraview arrays to numpy arrays

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

Reply
 
LinkBack Thread Tools Display Modes
Old   April 10, 2014, 10:01
Default Paraview arrays to numpy arrays
  #1
Senior Member
 
Francois Beaubert
Join Date: Mar 2009
Location: Lille, France
Posts: 126
Rep Power: 7
francois is on a distinguished road
Hi all,

I'm trying to use IPython with numpy to do some of my post-processing.

I've encountered the following difficulty: I'm unable to import paraview arrays into numpy ones.

Here is a simple example that read an OpenFoam case, sample the data on lines using PlotOverline filter and export them in csv files:

Code:
try: paraview.simple
except: from paraview.simple import *
paraview.simple._DisableFirstRenderCameraReset()

import math

# Define geometric parameters
D = 0.036

# Define points coordinates in cylindrical coordinates
r1 = 0.0
r2 = 0.019

theta1 = 90
theta2 = 90

theta1Rad = theta1 * math.pi/180
theta2Rad = theta2 * math.pi/180

z1 = 1*D
z2 = 1*D

# Transform points coordinates from cylindrical to cartesian
x1 = r1*math.cos(theta1Rad)
x2 = r2*math.cos(theta2Rad)

y1 = r1*math.sin(theta1Rad)
y2 = r2*math.sin(theta2Rad)

# Define the OpenFOAM data source

case_OpenFOAM = OpenDataFile('./case1.OpenFOAM')
case_OpenFOAM.MeshParts = ['internalMesh', 'wall - group']
case_OpenFOAM.VolumeFields = ['p', 'U', 'Ucyl', 'ccCyl']

for i in range(0,10):

  # Define the source data
  SetActiveSource(case_OpenFOAM)
  
  # Define the line
  PlotOverLine1 = PlotOverLine( Source="High Resolution Line Source" )
  
  zPos = i*D

  PlotOverLine1.Source.Point1 = [x1, y1, z1 + zPos]
  PlotOverLine1.Source.Point2 = [x2, y2, z2 + zPos]

  PassArrays1 = PassArrays()
  PassArrays1.PointDataArrays = ['arc_length','ccCyl','U', 'Ucyl', 'p'] 

  source = PassArrays1
    
  # Define filename of the outputs
  fileName = 'line' + '_r=' + str(r1) + '-' + str(r2) + '_theta=' + str(theta1) + '-' + str(theta2) + '_z=' + str(z1+ zPos) + '-' + str(z2 + zPos) + '.csv'
    
  writer = CreateWriter(fileName, source)
  writer.FieldAssociation = "Points" # or "Cells"
  writer.UpdatePipeline()
  del writer
Everything work correctly but rather than importing the data from the files into numpy arrays with loadtxt, I would prefer to import them directly into numpy arrays right after my PlotOverline: convert paraview arrays directly into numpy arrays

I know that some support functions from numpy_support are readily available for this kind of tasks:

Code:
from vtk.util import numpy_support as npvtk 
vtkarray = npvtk.numpy_to_vtk(numpy_array) 
numpy_array = npvtk.vtk_to_numpy(vtkarray)
I've tried to use them without success:

Code:
pParaview = case_OpenFOAM.PointData['p']
pNumpy = npvtk.vtk_to_numpy(pParaview)
But get the following error messages:

Code:
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-85-597a4d038813> in <module>()
----> 1 pNumpy = npvtk.vtk_to_numpy(pParaview)

/opt/paraviewopenfoam410/lib/paraview-4.1/site-packages/vtk/util/numpy_support.py in vtk_to_numpy(vtk_array)
    206     # Get the data via the buffer interface
    207     dtype = get_numpy_array_type(typ)
--> 208     result = numpy.frombuffer(vtk_array, dtype=dtype)
    209     if shape[1] == 1:
    210         shape = (shape[0], )

/opt/paraviewopenfoam410/lib/paraview-4.1/site-packages/paraview/servermanager.pyc in __getattr__(self, name)
   1433         array = self.FieldData.GetFieldData().GetArrayInformation(self.Name)
   1434         if not array: return None
-> 1435         return getattr(array, name)
   1436 
   1437     def __repr__(self):

AttributeError: __buffer__
Any advice ?
How to manipulate paraview arrays using numpy ?

Thank you very much for your help
Regards

François
francois is offline   Reply With Quote

Old   April 13, 2014, 17:48
Default
  #2
Super Moderator
 
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 7,450
Blog Entries: 33
Rep Power: 73
wyldckat is a jewel in the roughwyldckat is a jewel in the roughwyldckat is a jewel in the roughwyldckat is a jewel in the rough
Greetings François,

I think you're overlooking an important detail with OpenFOAM related data: it's handled as "MultiBlock" data.
A few reference threads:
Best regards,
Bruno
wyldckat is offline   Reply With Quote

Old   April 14, 2014, 09:33
Default
  #3
Senior Member
 
Francois Beaubert
Join Date: Mar 2009
Location: Lille, France
Posts: 126
Rep Power: 7
francois is on a distinguished road
Thanks for the advice Bruno !
I will look at the threads you have mentioned

Regards,
François
francois 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
Paraview version update errors Dan Pearce OpenFOAM Installation 5 January 8, 2014 06:47
Installing OpenFOAM and ParaView in VirtualBox(Ubuntu on Win8) chrisb2244 OpenFOAM Installation 2 August 21, 2013 14:24
Extracting ParaView Data into Python Arrays Jeffzda ParaView 7 December 6, 2012 09:10
Newbie: Install ParaView 3.81 on OF-1.6-ext/OpenSuse 11.2? lentschi OpenFOAM Installation 1 March 9, 2011 03:32
paraFoam reader for OpenFOAM 1.6 smart OpenFOAM Installation 13 November 16, 2009 22:41


All times are GMT -4. The time now is 16:17.