CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   ParaView (https://www.cfd-online.com/Forums/paraview/)
-   -   [OpenFOAM] Get cell center coordinates in a python script (https://www.cfd-online.com/Forums/paraview/137559-get-cell-center-coordinates-python-script.html)

francois June 18, 2014 10:33

Get cell center coordinates in a python script
 
Hi all,

I've made a python script that create a slice and extract the points coordinates of the vertex as well as the velocity components.

But I'm unable to obtain the cell center coordinates, howto do that ?

You will see below my script which can extract the points coordinates.

Code:

import numpy as np

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

from vtk.util import numpy_support as npvtk

# Define the OpenFOAM data source
case_OpenFOAM = OpenDataFile('./profile4.OpenFOAM')
case_OpenFOAM.MeshParts = ['internalMesh']
case_OpenFOAM.VolumeFields = ['p', 'U']
print case_OpenFOAM.TimestepValues

mergedcase_OpenFOAM = MergeBlocks(case_OpenFOAM)
dataOpenFOAM = servermanager.Fetch(mergedcase_OpenFOAM)

# Select the active source
SetActiveSource(mergedcase_OpenFOAM)

# Define the plane
Slice1 = Slice(SliceType="Plane")
Slice1.SliceType = 'Plane'
Slice1.SliceType.Origin = [0.01,0,0]
Slice1.SliceType.Normal = [1,0,0]
Slice1.Triangulatetheslice = 0 # Don't triangulate the slice

dataPlane = servermanager.Fetch(Slice1)
nbCells = dataPlane.GetNumberOfCells()
nbPoints = dataPlane.GetNumberOfPoints()

print 'Number of cells in the slice', nbCells
print 'Number of points in the slice', nbPoints

# Put the velocity vector U into a numpy array
U = npvtk.vtk_to_numpy(dataPlane.GetCellData().GetArray('U'))

# Put the points coordinates in x, y and z arrays
x=[]
y=[]
z=[]

for i in range(nbPoints):
    coord = dataPlane.GetPoint(i)
    xx, yy, zz = coord[:3]
    x.append(xx)
    y.append(yy)
    z.append(zz)

x=np.array(x)
y=np.array(y)
z=np.array(z)

print 'x=',x
print 'y=',y
print 'z=',z

Any help will be warmly welcome. :D
Thanks !

François

t.oliveira January 12, 2017 06:47

Hi,

To obtain the cell center coordinates, I run the standard utility writeCellCentres. It creates fields ccx, ccy and ccz with the x, y and z components of the cell center coordinates, which can be loaded in Paraview.

There is also the cellCenters utility created by @opedrofunk, which is "similar to the writeCellCentres utility[...], except that it outputs a single file with the vector coordinates of the cell center positions".

Best wishes,
Thomas

francois June 3, 2017 08:20

Indeed the writeCellCentres do the trick.
But my goal is to obtain the cell centers in a Python script using the paraview.simple module.

Anyway thank you very much Thomas for your advice, it's very nice from you.
Have a good day.

François


All times are GMT -4. The time now is 05:54.