BHAC Python tools
Public Member Functions | Public Attributes | Static Public Attributes | List of all members
read.load Class Reference

Loader class for vtu and pvtu files. More...

Inheritance diagram for read.load:
read.loadvti

Public Member Functions

def __init__ (self, offset, get=1, file='data', type='vtu', mirrorPlane=None, rotateX=0, rotateY=0, rotateZ=0, scaleX=1, scaleY=1, scaleZ=1, silent=0)
 
def getTime (self)
 
def getVar (self, varname)
 
def getData (self)
 
def getPointData (self)
 
def getVars (self)
 
def getVarnames (self)
 
def getBounds (self)
 
def getVert (self, icell)
 
def getPointList (self)
 
def getCenterPoints (self)
 
def getSurface (self)
 
def getPieces (self, nBWidth, nBHeight, nIgnore)
 
def showValues (self, icell)
 
def getIcellByPoint (self, x, y)
 
def getPoints (self)
 
def mirror (self)
 
def reflectVar (self, var)
 
def getNdim (self)
 
def getAll (self)
 

Public Attributes

 offset
 
 filenameout
 
 type
 
 isLoaded
 
 mirrorPlane
 
 silent
 
 rotateX
 
 rotateY
 
 rotateZ
 
 scaleX
 
 scaleY
 
 scaleZ
 
 filename
 
 datareader
 
 time
 
 data
 
 ncells
 
 pointdata
 
 xlist
 
 ylist
 
 centerpoints
 
 surface
 
 xBlockList
 
 yBlockList
 
 points
 
 ndim
 

Static Public Attributes

 tend = default_timer()
 self.nblocks = self.data.GetMaximumNumberOfPieces() self.data.SetUpdateNumberOfPieces(self.nblocks) self.xBlockList=[] self.yBlockList=[] More...
 

Detailed Description

Loader class for vtu and pvtu files.

Definition at line 50 of file read.py.

Constructor & Destructor Documentation

◆ __init__()

def read.load.__init__ (   self,
  offset,
  get = 1,
  file = 'data',
  type = 'vtu',
  mirrorPlane = None,
  rotateX = 0,
  rotateY = 0,
  rotateZ = 0,
  scaleX = 1,
  scaleY = 1,
  scaleZ = 1,
  silent = 0 
)

Definition at line 54 of file read.py.

54  silent=0):
55  self.offset=offset
56  self.filenameout = file
57  self.type = type
58  self.isLoaded = False
59  self.mirrorPlane=mirrorPlane
60  self.silent = silent
61 
62  self.rotateX = rotateX
63  self.rotateY = rotateY
64  self.rotateZ = rotateZ
65 
66  self.scaleX = scaleX
67  self.scaleY = scaleY
68  self.scaleZ = scaleZ
69 
70  if type == 'vtu':
71  self.filename=''.join([self.filenameout,repr(offset).zfill(4),'.vtu'])
72  self.datareader = v.vtkXMLUnstructuredGridReader()
73  elif type == 'pvtu':
74  self.filename=''.join([self.filenameout,repr(offset).zfill(4),'.pvtu'])
75  self.datareader = v.vtkXMLPUnstructuredGridReader()
76  elif type == 'vti':
77  self.filename=''.join([self.filenameout,repr(offset).zfill(4),'.vti'])
78  self.datareader = v.vtkXMLImageDataReader()
79  else:
80  print('Unknown filetype')
81 
82  if (self.silent == 0): print('========================================')
83  if (self.silent == 0): print('loading file %s' % (self.filename))
84 
85  if get != None:
86  self.getAll()
87 
88 

Member Function Documentation

◆ getAll()

def read.load.getAll (   self)

Definition at line 401 of file read.py.

401  def getAll(self):
402  t0 = default_timer()
403 
404  self.getData()
405  tdata = default_timer()
406  if (self.silent == 0): print('Reading data time= %f sec' % (tdata-t0))
407 
408  if self.mirrorPlane != None:
409  if (self.silent == 0): print('========== Mirror about plane ',self.mirrorPlane,' ... ============')
410  self.mirror()
411 
412 
413  if (self.silent == 0): print('========== Initializing ... ============')
414 
415 
416  self.getVars()
417  tvars = default_timer()
418  if (self.silent == 0): print('Getting vars time= %f sec' % (tvars-tdata))
419 
420  self.getPoints()
421  tpoints = default_timer()
422  if (self.silent == 0): print('Getting points time= %f sec' % (tpoints-tvars))
423 
424  self.getTime()
425 
426  tend = default_timer()
427  if (self.silent == 0): print('========== Finished loading %d cells in %f sec, have a nice day! ===========' % (self.ncells, (tend-t0) ))
428 
429 #=============================================================================
default_timer
Definition: read.py:14

◆ getBounds()

def read.load.getBounds (   self)

Definition at line 159 of file read.py.

159  def getBounds(self):
160  return self.data.GetBounds()
161 
162 

◆ getCenterPoints()

def read.load.getCenterPoints (   self)

Definition at line 197 of file read.py.

197  def getCenterPoints(self):
198  tstart = default_timer()
199  firstget = False
200  try:
201  self.data
202  except AttributeError:
203  self.getData()
204  firstget = True
205  try:
206  self.centerpoints
207  except AttributeError:
208  if self.getNdim() == 2 or self.getNdim() == 3:
209  self.centerpoints=np.empty((self.ncells,2))
210  for icell in range(self.ncells):
211  vert=self.getVert(icell)
212  self.centerpoints[icell,0]=vert[:,0].mean()
213  self.centerpoints[icell,1]=vert[:,1].mean()
214  if self.getNdim() == 1 :
215  self.centerpoints=np.empty((self.ncells))
216  for icell in range(self.ncells):
217  vert=self.getVert(icell)
218  self.centerpoints[icell]=vert.mean()
219  tend = default_timer()
220  if firstget:
221  if (self.silent == 0): print('Getting cell center coordiantes time=%f sec' % (tend-tstart))
222  return self.centerpoints
223 
224 
default_timer
Definition: read.py:14

◆ getData()

def read.load.getData (   self)

Definition at line 104 of file read.py.

104  def getData(self):
105  self.datareader.SetFileName(self.filename)
106  self.datareader.Update()
107  self.data = self.datareader.GetOutput()
108  self.ncells = self.data.GetNumberOfCells()
109  self.isLoaded = True
110 
111  if (self.rotateX != 0 or self.rotateY != 0 or self.rotateZ != 0
112  or self.scaleX != 1 or self.scaleY != 1 or self.scaleZ != 1):
113  transform = v.vtkTransform()
114  transform.RotateX(self.rotateX)
115  transform.RotateY(self.rotateY)
116  transform.RotateZ(self.rotateZ)
117  transform.Scale(self.scaleX, self.scaleY, self.scaleZ)
118  transfilter = v.vtkTransformFilter()
119  transfilter.SetTransform(transform)
120  transfilter.SetInputData(self.data)
121  self.data = transfilter.GetOutput()
122  transfilter.Update()
123 
124 

◆ getIcellByPoint()

def read.load.getIcellByPoint (   self,
  x,
  y 
)

Definition at line 339 of file read.py.

339  def getIcellByPoint(self,x,y):
340  radii2 = (self.getCenterPoints()[:,0]-x)**2 + (self.getCenterPoints()[:,1]-y)**2
341  icell=radii2.argmin()
342  return icell
343 
344 

◆ getNdim()

def read.load.getNdim (   self)

Definition at line 389 of file read.py.

389  def getNdim(self):
390  smalldouble = 1e-10
391  self.ndim = 3
392  if abs(self.data.GetBounds()[1] - self.data.GetBounds()[0]) <= smalldouble:
393  self.ndim=self.ndim - 1
394  if abs(self.data.GetBounds()[3] - self.data.GetBounds()[2]) <= smalldouble:
395  self.ndim=self.ndim - 1
396  if abs(self.data.GetBounds()[5] - self.data.GetBounds()[4]) <= smalldouble:
397  self.ndim=self.ndim - 1
398  return self.ndim
399 
400 

◆ getPieces()

def read.load.getPieces (   self,
  nBWidth,
  nBHeight,
  nIgnore 
)

Definition at line 246 of file read.py.

246  def getPieces(self,nBWidth,nBHeight,nIgnore):
247  tstart = default_timer()
248  try:
249  self.data
250  except AttributeError:
251  self.getData()
252  try:
253  [self.xBlockList,self.yBlockList]
254  except AttributeError:
255 
256  self.xBlockList=[]
257  self.yBlockList=[]
258 
259  pts=self.getPoints()
260  nPoints = np.shape(pts)[0]
261 
262  # Calculate number of blocks
263 
264  nBSize=(nBWidth+1)*(nBHeight+1)
265 
266  nBlocks=int(nPoints/nBSize)
267 
268  # For all blocks, draw the perimeter
269  for iBlock in range(nBlocks):
270  start=iBlock*nBSize
271  end =(iBlock+1)*nBSize-1
272  # Side 1
273  for i in range(start,start+nBWidth+1):
274  self.xBlockList.append(pts[i][0])
275  self.yBlockList.append(pts[i][1])
276  # Side 2
277  for i in range(start+nBWidth,end+1,nBWidth+1):
278  self.xBlockList.append(pts[i][0])
279  self.yBlockList.append(pts[i][1])
280  # Side 3
281  for i in range(end,end-nBWidth-1,-1):
282  self.xBlockList.append(pts[i][0])
283  self.yBlockList.append(pts[i][1])
284  # Side 4
285  for i in range(end-nBWidth,start-1,-nBWidth-1):
286  self.xBlockList.append(pts[i][0])
287  self.yBlockList.append(pts[i][1])
288 
289  self.xBlockList.append(None)
290  self.yBlockList.append(None)
291 
default_timer
Definition: read.py:14

◆ getPointData()

def read.load.getPointData (   self)

Definition at line 125 of file read.py.

125  def getPointData(self):
126  self.getData()
127  if self.data.GetPointData().GetNumberOfArrays() == 0:
128  c2p = v.vtkCellDataToPointData()
129  # vtk 4,5:
130 # c2p.SetInput(self.data)
131  # vtk 6:
132  c2p.SetInputData(self.data)
133  self.pointdata=c2p.GetOutput()
134  # vtk 4,5:
135 # self.pointdata.Update()
136  # vtk 6:
137  c2p.Update()
138  else:
139  self.pointdata=self.data
140 
141 

◆ getPointList()

def read.load.getPointList (   self)

Definition at line 174 of file read.py.

174  def getPointList(self):
175  tstart = default_timer()
176  try:
177  self.data
178  except AttributeError:
179  self.getData()
180  try:
181  [self.xlist,self.ylist]
182  except AttributeError:
183  if self.data.GetCell(0).GetCellType() != 8 :
184  if (self.silent == 0): print("Can handle pixel types only")
185  pass
186  self.xlist = []
187  self.ylist = []
188  for icell in range(self.ncells):
189  pts=ah.vtk2array(self.data.GetCell(icell).GetPoints().GetData())
190  self.xlist.extend((pts[0][0],pts[1][0],pts[3][0],pts[2][0],None))
191  self.ylist.extend((pts[0][1],pts[1][1],pts[3][1],pts[2][1],None))
192  tend = default_timer()
193  if (self.silent == 0): print('Getting formatted pointlist time=%f sec' % (tend-tstart))
194  return [self.xlist,self.ylist]
195 
196 
default_timer
Definition: read.py:14

◆ getPoints()

def read.load.getPoints (   self)

Definition at line 345 of file read.py.

345  def getPoints(self):
346  try:
347  self.data
348  except AttributeError:
349  self.getData()
350  try:
351  self.points
352  except AttributeError:
353  vtk_points=self.data.GetPoints().GetData()
354  self.points=ah.vtk2array(vtk_points)
355  return self.points
356 
357 

◆ getSurface()

def read.load.getSurface (   self)

Definition at line 225 of file read.py.

225  def getSurface(self):
226  def calcSurface(icell):
227  vert=self.getVert(icell)
228  return np.sqrt(((vert[0][0]-vert[1][0])**2+(vert[0][1]-vert[1][1])**2)*
229  ((vert[1][0]-vert[2][0])**2+(vert[1][1]-vert[2][1])**2))
230 
231  tstart = default_timer()
232  try:
233  self.data
234  except AttributeError:
235  self.getData()
236  try:
237  self.surface
238  except AttributeError:
239  # Assuming cells are rectangles here.
240  surface=list(map(calcSurface,list(range(self.ncells))))
241  self.surface=np.array(surface)
242  tend = default_timer()
243  if (self.silent == 0): print('Getting cell surface (assuming rectangles) time=%f sec' % (tend-tstart))
244  return self.surface
245 
default_timer
Definition: read.py:14

◆ getTime()

def read.load.getTime (   self)

Definition at line 89 of file read.py.

89  def getTime(self):
90  try:
91  self.time=ah.vtk2array(self.data.GetFieldData().GetArray(0))[0]
92  except AttributeError:
93  self.time = np.nan
94  return self.time
95 
96 

◆ getVar()

def read.load.getVar (   self,
  varname 
)

Definition at line 97 of file read.py.

97  def getVar(self,varname):
98  try:
99  exec("tmp=self.%s" % (varname))
100  return tmp
101  except AttributeError:
102  print("Unknown variable", varname)
103 

◆ getVarnames()

def read.load.getVarnames (   self)

Definition at line 151 of file read.py.

151  def getVarnames(self):
152  nvars= self.data.GetCellData().GetNumberOfArrays()
153  varnames=[]
154  for i in range(nvars):
155  varnames.append(self.data.GetCellData().GetArrayName(i))
156  return varnames
157 
158 

◆ getVars()

def read.load.getVars (   self)

Definition at line 142 of file read.py.

142  def getVars(self):
143  nvars= self.data.GetCellData().GetNumberOfArrays()
144  for i in range(nvars):
145  varname = self.data.GetCellData().GetArrayName(i)
146  if (self.silent == 0): print("Assigning variable:", varname)
147  vtk_values = self.data.GetCellData().GetArray(varname)
148  exec("self.%s = ah.vtk2array(vtk_values)[0:self.ncells]" % (varname))
149 
150 

◆ getVert()

def read.load.getVert (   self,
  icell 
)

Definition at line 163 of file read.py.

163  def getVert(self,icell):
164  if self.data.GetCell(icell).GetCellType() == 8 :
165  pts=ah.vtk2array(self.data.GetCell(icell).GetPoints().GetData())
166  return np.array((pts[0][0:2],pts[1][0:2],pts[3][0:2],pts[2][0:2]))
167  if self.data.GetCell(icell).GetCellType() == 3 :
168  pts=ah.vtk2array(self.data.GetCell(icell).GetPoints().GetData())
169  return np.array((pts[0][0],pts[1][0]))
170  else:
171  if (self.silent == 0): print("Can handle only type 3 or type 8")
172 
173 

◆ mirror()

def read.load.mirror (   self)

Definition at line 367 of file read.py.

367  """
368 
369  vr=v.vtkReflectionFilter()
370  vr.SetInputData(self.data)
371  vr.SetPlane(self.mirrorPlane)
372  self.data=vr.GetOutput()
373  vr.Update()
374  #self.data.Update()
375  self.ncells = self.data.GetNumberOfCells()
376 
377 

◆ reflectVar()

def read.load.reflectVar (   self,
  var 
)

Definition at line 378 of file read.py.

378  def reflectVar(self,var):
379  if self.mirrorPlane == 0:
380  CC=self.getCenterPoints()
381  im = CC[:,0] < 0
382  var[im] = -var[im]
383  else:
384  if (self.silent == 0): print('reflection of this plane not yet implemented, sorry')
385 
386  return var
387 
388 

◆ showValues()

def read.load.showValues (   self,
  icell 
)

Definition at line 332 of file read.py.

332  def showValues(self,icell):
333  if (self.silent == 0): print('=======================================================')
334  if (self.silent == 0): print('icell= %d; x=%e; y=%e' % (icell,self.getCenterPoints()[icell,0],self.getCenterPoints()[icell,1]))
335  for varname in self.getVarnames():
336  exec("if (self.silent == 0): print '%s =', self.%s[icell]" % (varname,varname))
337 
338 

Member Data Documentation

◆ centerpoints

read.load.centerpoints

Definition at line 209 of file read.py.

◆ data

read.load.data

Definition at line 107 of file read.py.

◆ datareader

read.load.datareader

Definition at line 72 of file read.py.

◆ filename

read.load.filename

Definition at line 71 of file read.py.

◆ filenameout

read.load.filenameout

Definition at line 56 of file read.py.

◆ isLoaded

read.load.isLoaded

Definition at line 58 of file read.py.

◆ mirrorPlane

read.load.mirrorPlane

Definition at line 59 of file read.py.

◆ ncells

read.load.ncells

Definition at line 108 of file read.py.

◆ ndim

read.load.ndim

Definition at line 391 of file read.py.

◆ offset

read.load.offset

Definition at line 55 of file read.py.

◆ pointdata

read.load.pointdata

Definition at line 133 of file read.py.

◆ points

read.load.points

Definition at line 354 of file read.py.

◆ rotateX

read.load.rotateX

Definition at line 62 of file read.py.

◆ rotateY

read.load.rotateY

Definition at line 63 of file read.py.

◆ rotateZ

read.load.rotateZ

Definition at line 64 of file read.py.

◆ scaleX

read.load.scaleX

Definition at line 66 of file read.py.

◆ scaleY

read.load.scaleY

Definition at line 67 of file read.py.

◆ scaleZ

read.load.scaleZ

Definition at line 68 of file read.py.

◆ silent

read.load.silent

Definition at line 60 of file read.py.

◆ surface

read.load.surface

Definition at line 241 of file read.py.

◆ tend

read.load.tend = default_timer()
static

self.nblocks = self.data.GetMaximumNumberOfPieces() self.data.SetUpdateNumberOfPieces(self.nblocks) self.xBlockList=[] self.yBlockList=[]

for i in range(nIgnore,self.nblocks): self.data.SetUpdatePiece(i) self.data.Update() pts = ah.vtk2array(self.data.GetPoints().GetData()) nPoints = np.shape(pts)[0]

for i in range(nPoints): if ((i < (nBWidth+1))or(i%(nBWidth+1)==nBWidth)): self.xBlockList.append(pts[i][0]) self.yBlockList.append(pts[i][1]) self.xBlockList.append(None) self.yBlockList.append(None) for i in range(nPoints): if (i%(nBWidth+1)==0): self.xBlockList.append(pts[i][0]) self.yBlockList.append(pts[i][1]) self.xBlockList.append(None) self.yBlockList.append(None) for i in range(nPoints): if (i>(nBWidth+1)*nBHeight): self.xBlockList.append(pts[i][0]) self.yBlockList.append(pts[i][1]) self.xBlockList.append(None) self.yBlockList.append(None)

self.data.SetUpdateNumberOfPieces(1) self.data.SetUpdatePiece(0) self.data.Update()

Definition at line 328 of file read.py.

◆ time

read.load.time

Definition at line 91 of file read.py.

◆ type

read.load.type

Definition at line 57 of file read.py.

◆ xBlockList

read.load.xBlockList

Definition at line 256 of file read.py.

◆ xlist

read.load.xlist

Definition at line 186 of file read.py.

◆ yBlockList

read.load.yBlockList

Definition at line 257 of file read.py.

◆ ylist

read.load.ylist

Definition at line 187 of file read.py.


The documentation for this class was generated from the following file: