CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   OpenFOAM Paraview & paraFoam (http://www.cfd-online.com/Forums/openfoam-paraview/)
-   -   Paraview arrays to numpy arrays (http://www.cfd-online.com/Forums/openfoam-paraview/133115-paraview-arrays-numpy-arrays.html)

francois April 10, 2014 09:01

Paraview arrays to numpy arrays
 
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

wyldckat April 13, 2014 16:48

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

francois April 14, 2014 08:33

Thanks for the advice Bruno ! :)
I will look at the threads you have mentioned

Regards,
François


All times are GMT -4. The time now is 00:01.