1 '''Modules used for reading AMRVAC data''' 5 from numpy
import loadtxt
as pylabload
6 import numpy_support
as ah
12 if sys.platform ==
"win32":
14 default_timer = time.clock
17 default_timer = time.time
21 def extract(data,varname,attribute_mode='cell'):
22 '''Extracts variable "varname" from vtk datastructure "data"''' 23 if attribute_mode ==
'cell':
24 vtk_values = data.GetCellData().GetArray(varname)
25 elif attribute_mode ==
'point':
26 if data.GetPointData().GetNumberOfArrays() > 0:
27 vtk_values = data.GetPointData().GetArray(varname)
30 c2p = v.vtkCellDataToPointData()
32 pointdata=c2p.GetOutput()
34 vtk_values = pointdata.GetPointData().GetScalars(varname)
35 elif attribute_mode ==
'topoint':
36 c2p = v.vtkCellDataToPointData()
38 pointdata=c2p.GetOutput()
40 vtk_values = pointdata.GetPointData().GetScalars(varname)
42 print(
"attribute_mode is either 'cell' or 'point'")
44 return ah.vtk2array(vtk_values)
49 Loader class for vtu and pvtu files. 51 def __init__(self,offset,get=1,file='data',type='vtu',mirrorPlane=None,
52 rotateX=0,rotateY=0,rotateZ=0,
53 scaleX=1,scaleY=1,scaleZ=1,
75 self.
datareader = v.vtkXMLPUnstructuredGridReader()
80 print(
'Unknown filetype')
82 if (self.
silent == 0): print(
'========================================')
91 self.
time=ah.vtk2array(self.
data.GetFieldData().GetArray(0))[0]
92 except AttributeError:
99 exec(
"tmp=self.%s" % (varname))
101 except AttributeError:
102 print(
"Unknown variable", varname)
113 transform = v.vtkTransform()
114 transform.RotateX(self.
rotateX)
115 transform.RotateY(self.
rotateY)
116 transform.RotateZ(self.
rotateZ)
118 transfilter = v.vtkTransformFilter()
119 transfilter.SetTransform(transform)
120 transfilter.SetInputData(self.
data)
121 self.
data = transfilter.GetOutput()
127 if self.
data.GetPointData().GetNumberOfArrays() == 0:
128 c2p = v.vtkCellDataToPointData()
132 c2p.SetInputData(self.
data)
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))
152 nvars= self.
data.GetCellData().GetNumberOfArrays()
154 for i
in range(nvars):
155 varnames.append(self.
data.GetCellData().GetArrayName(i))
160 return self.
data.GetBounds()
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]))
171 if (self.
silent == 0): print(
"Can handle only type 3 or type 8")
178 except AttributeError:
182 except AttributeError:
183 if self.
data.GetCell(0).GetCellType() != 8 :
184 if (self.
silent == 0): print(
"Can handle pixel types only")
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))
193 if (self.
silent == 0): print(
'Getting formatted pointlist time=%f sec' % (tend-tstart))
202 except AttributeError:
207 except AttributeError:
210 for icell
in range(self.
ncells):
216 for icell
in range(self.
ncells):
221 if (self.
silent == 0): print(
'Getting cell center coordiantes time=%f sec' % (tend-tstart))
226 def calcSurface(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))
234 except AttributeError:
238 except AttributeError:
240 surface=list(map(calcSurface,list(range(self.
ncells))))
243 if (self.
silent == 0): print(
'Getting cell surface (assuming rectangles) time=%f sec' % (tend-tstart))
250 except AttributeError:
254 except AttributeError:
260 nPoints = np.shape(pts)[0]
264 nBSize=(nBWidth+1)*(nBHeight+1)
266 nBlocks=int(nPoints/nBSize)
269 for iBlock
in range(nBlocks):
271 end =(iBlock+1)*nBSize-1
273 for i
in range(start,start+nBWidth+1):
277 for i
in range(start+nBWidth,end+1,nBWidth+1):
281 for i
in range(end,end-nBWidth-1,-1):
285 for i
in range(end-nBWidth,start-1,-nBWidth-1):
329 if (self.
silent == 0): print(
'Getting formatted blocklist time=%f sec' % (tend-tstart))
333 if (self.
silent == 0): print(
'=======================================================')
336 exec(
"if (self.silent == 0): print '%s =', self.%s[icell]" % (varname,varname))
341 icell=radii2.argmin()
348 except AttributeError:
352 except AttributeError:
353 vtk_points=self.
data.GetPoints().GetData()
360 Called when mirrorPlane != None 361 The reflection plane is labeled as follows: From the vtk documentation: 363 USE_X_MIN = 0, USE_Y_MIN = 1, USE_Z_MIN = 2, USE_X_MAX = 3, 364 USE_Y_MAX = 4, USE_Z_MAX = 5, USE_X = 6, USE_Y = 7, 369 vr=v.vtkReflectionFilter()
370 vr.SetInputData(self.
data)
372 self.
data=vr.GetOutput()
384 if (self.
silent == 0): print(
'reflection of this plane not yet implemented, sorry')
392 if abs(self.
data.GetBounds()[1] - self.
data.GetBounds()[0]) <= smalldouble:
394 if abs(self.
data.GetBounds()[3] - self.
data.GetBounds()[2]) <= smalldouble:
396 if abs(self.
data.GetBounds()[5] - self.
data.GetBounds()[4]) <= smalldouble:
406 if (self.
silent == 0): print(
'Reading data time= %f sec' % (tdata-t0))
409 if (self.
silent == 0): print(
'========== Mirror about plane ',self.
mirrorPlane,
' ... ============')
413 if (self.
silent == 0): print(
'========== Initializing ... ============')
418 if (self.
silent == 0): print(
'Getting vars time= %f sec' % (tvars-tdata))
422 if (self.
silent == 0): print(
'Getting points time= %f sec' % (tpoints-tvars))
427 if (self.
silent == 0): print(
'========== Finished loading %d cells in %f sec, have a nice day! ===========' % (self.
ncells, (tend-t0) ))
431 """Loader class for vti data""" 432 def __init__(self,offset,get=1,file='data',mirrorPlane=None,silent=0,
433 rotateX=0,rotateY=0,rotateZ=0,
434 scaleX=1,scaleY=1,scaleZ=1):
452 if (self.
silent == 0): print(
'========================================')
460 if self.
data.GetExtent()[1] - self.
data.GetExtent()[0] == 0.:
462 if self.
data.GetExtent()[3] - self.
data.GetExtent()[2] == 0.:
464 if self.
data.GetExtent()[5] - self.
data.GetExtent()[4] == 0.:
471 self.
x=self.
data.GetOrigin()[0] + np.arange(self.
data.GetExtent()[0],self.
data.GetExtent()[1])*self.
dx+self.
dx/2.
476 self.
y=self.
data.GetOrigin()[1] + np.arange(self.
data.GetExtent()[2],self.
data.GetExtent()[3])*self.
dy+self.
dy/2.
481 self.
z=self.
data.GetOrigin()[2] + np.arange(self.
data.GetExtent()[4],self.
data.GetExtent()[5])*self.
dz+self.
dz/2.
485 nvars= self.
data.GetCellData().GetNumberOfArrays()
486 for i
in range(nvars):
487 varname = self.
data.GetCellData().GetArrayName(i)
488 if (self.
silent == 0): print(
"Assigning variable:", varname)
490 vtk_values = self.
data.GetCellData().GetArray(varname)
492 exec(
"self.%s = ah.vtk2array(vtk_values)[0:self.ncells].reshape((self.nx),order='F')" % (varname))
494 exec(
"self.%s = ah.vtk2array(vtk_values)[0:self.ncells].reshape((self.nx,self.ny),order='F')" % (varname))
496 exec(
"self.%s = ah.vtk2array(vtk_values)[0:self.ncells].reshape((self.nx,self.ny,self.nz),order='F')" % (varname))
504 if (self.
silent == 0): print(
'Reading data time= %f sec' % (tdata-t0))
513 if (self.
silent == 0): print(
'========== Mirror about plane ',self.
mirrorPlane,
' ... ============')
517 if (self.
silent == 0): print(
'========== Initializing ... ============')
522 if (self.
silent == 0): print(
'Getting vars time= %f sec' % (tvars-tdata))
527 if (self.
silent == 0): print(
'========== Finished loading %d cells in %f sec, have a nice day! ===========' % (self.
ncells, (tend-t0) ))
531 '''Load 1D comma separated list from a cut''' 532 def __init__(self,offset,get=1,file='data',dir=1,coord=0.,silent=0):
541 if (self.
silent == 0): print(
'========================================')
549 number=
'%+.2e' % self.
coord 553 self.
filename = self.
filenameout+
'_d'+str(self.
dir)+
'_x-'+
'0.'+number[1]+number[3]+
'D+'+str(int(np.log10(abs(self.
coord)))+1).zfill(2)+
'_n'+str(self.
offset).zfill(4)+
'.csv' 555 self.
filename = self.
filenameout+
'_d'+str(self.
dir)+
'_x+'+
'0.'+number[1]+number[3]+
'D+'+str(int(np.log10(abs(self.
coord)))).zfill(2)+
'_n'+str(self.
offset).zfill(4)+
'.csv' 557 self.
filename = self.
filenameout+
'_d'+str(self.
dir)+
'_x-'+
'0.'+number[1]+number[3]+
'D+'+str(int(np.log10(abs(self.
coord)))).zfill(2)+
'_n'+str(self.
offset).zfill(4)+
'.csv' 559 self.
filename = self.
filenameout+
'_d'+str(self.
dir)+
'_x+'+
'0.'+number[1]+number[3]+
'D+'+str(0).zfill(2)+
'_n'+str(self.
offset).zfill(4)+
'.csv' 573 if (self.
silent == 0): print(
'========== Initializing ... ============')
578 if (self.
silent == 0): print(
'Reading data time= %f sec' % (tdata-t0))
584 exec(
"self.%s = pylabload(self.filename,usecols=[i],skiprows=1, delimiter=',')" % (var))
586 exec(
"self.ncells = len(self.%s)" % (self.
getHeader()[0]))
590 """Load binary particles data""" 592 def __init__(self,offset,file='data',components=3):
623 (self.nparticles,) = struct.unpack(
'i',self.
file.
read(4))
624 (self.itparticles,) = struct.unpack(
'i',self.
file.
read(4))
625 (self.npayload,) = struct.unpack(
'i',self.
file.
read(4))
631 payload = np.empty(self.npayload)
633 (index,) = struct.unpack(
'i',self.
file.
read(4))
635 (ifollow,) = struct.unpack(
'i',self.
file.
read(4))
641 (q,) = struct.unpack(
'd',self.
file.
read(8))
642 (m,) = struct.unpack(
'd',self.
file.
read(8))
643 (t,) = struct.unpack(
'd',self.
file.
read(8))
644 (dt,) = struct.unpack(
'd',self.
file.
read(8))
647 (x[icomp],) = struct.unpack(
'd',self.
file.
read(8))
650 (u[icomp],) = struct.unpack(
'd',self.
file.
read(8))
652 for ipayload
in range(self.npayload):
653 (payload[ipayload],) = struct.unpack(
'd',self.
file.
read(8))
655 self.
data.append({
'index':index,
'q':q,
656 'follow':follow,
'm':m,
't':t,
657 'dt':dt,
'x':x,
'u':u, 'payload':payload})
669 for particle
in self.
data:
670 if particle[
'index'] == index :
678 except AttributeError:
679 self.
index=np.array(self.nparticles)
681 for particle
in self.
data:
682 index.append(particle[
'index'])
683 index=np.array(index)
689 '''Load particle ensemble .csv files''' 691 def __init__(self,offset,file='data0000',npayload=1,components=3,delimiter=','):
719 index_ = iteration_+1
721 if x.shape[1] == index_+1 :
724 self.
data.append({
't':particle[t_],
725 'dt':particle[dt_],
'x':np.array(particle[x1_:x1_+self.
components]),
726 'u':np.array(particle[u1_:u1_+self.components]), 727 'payload':np.array(particle[payload_:payload_+self.
npayload]),
728 'ipe':particle[ipe_].astype(np.int),
'iteration':particle[iteration_].astype(np.int),
'index':particle[index_].astype(np.int)})
731 print(
'File inconsistent, assuming iteration counter overflow')
733 self.
data.append({
't':particle[t_],
734 'dt':particle[dt_],
'x':np.array(particle[x1_:x1_+self.
components]),
735 'u':np.array(particle[u1_:u1_+self.components]), 736 'payload':np.array(particle[payload_:payload_+self.
npayload]),
737 'garbage':particle[ipe_].astype(np.int),
'index':particle[index_-1].astype(np.int)})
744 for particle
in self.
data:
745 if particle[
'index'] == index :
750 """Load binary postrad data""" 752 def __init__(self,offset,file='data',headersize=256,nvars=8):
758 self.
indices={
'r':0,'theta':1,'phi':2,'rho':0,'vr':1,'vtheta':2,'vphi':3,'p':4,'br':5,'btheta':6,'bphi':7}
769 def __makefilenames(self):
773 def __openfiles(self):
777 def __closefiles(self):
787 def __read_header(self):
790 nr = struct.unpack(
'i',file_vars.read(4)) [0]
791 ntheta = struct.unpack(
'i',file_vars.read(4)) [0]
792 nphi = struct.unpack(
'i',file_vars.read(4)) [0]
793 t = struct.unpack(
'd',file_vars.read(8)) [0]
794 a = struct.unpack(
'd',file_vars.read(8)) [0]
795 r_in = struct.unpack(
'd',file_vars.read(8)) [0]
796 theta_in = struct.unpack(
'd',file_vars.read(8)) [0]
797 phi_in = struct.unpack(
'd',file_vars.read(8)) [0]
798 r_out = struct.unpack(
'd',file_vars.read(8)) [0]
799 theta_out = struct.unpack(
'd',file_vars.read(8)) [0]
800 phi_out = struct.unpack(
'd',file_vars.read(8)) [0]
801 gammahat = struct.unpack(
'd',file_vars.read(8)) [0]
804 imetric = struct.unpack(
'i',file_vars.read(4)) [0]
805 dummy = struct.unpack(
'i',file_vars.read(4)) [0]
806 dim = struct.unpack(
'i',file_vars.read(4)) [0]
809 single_precision =
True 811 single_precision =
False 813 self.
header = {
'nr':nr,
'ntheta':ntheta,
'nphi':nphi,
't':t,
'a':a,
'r_in':r_in,
'theta_in':theta_in,
'phi_in':phi_in,
'r_out':r_out,
'theta_out':theta_out,
'phi_out':phi_out,
'gammahat':gammahat,
'hslope':hslope,
'imetric':imetric,
'dim':dim,
'single_precision':single_precision}
815 def __set_dtype(self):
816 if (self.
header[
'single_precision']):
821 def __read_data(self):
826 len_bytes = header[
'nphi']*header[
'ntheta']*header[
'nr']*self.
nvars 827 data = np.array(struct.unpack(self.
__dtype_str*len_bytes,file_vars.read())).reshape(header[
'nphi'],header[
'ntheta'],header[
'nr'],self.
nvars)
831 def __read_grid(self):
835 len_bytes = header[
'nphi']*header[
'ntheta']*header[
'nr']*3
836 data = np.array(struct.unpack(self.
__dtype_str*len_bytes,file_grid.read())).reshape(header[
'nphi'],header[
'ntheta'],header[
'nr'],3)
def __init__(self, offset, file='data', components=3)
Loader class for vti data.
def reflectVar(self, var)
def extract(data, varname, attribute_mode='cell')
Extracts variable "varname" from vtk datastructure "data'.
def getCenterPoints(self)
def __init__(self, offset, get=1, file='data', mirrorPlane=None, silent=0, rotateX=0, rotateY=0, rotateZ=0, scaleX=1, scaleY=1, scaleZ=1)
Loader class for vtu and pvtu files.
def getVar(self, varname)
Load binary particles data.
def __init__(self, offset, get=1, file='data', dir=1, coord=0., silent=0)
def __init__(self, offset, file='data', headersize=256, nvars=8)
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 __makefilenames(self)
def getIcellByPoint(self, x, y)
Load 1D comma separated list from a cut.
def getPieces(self, nBWidth, nBHeight, nIgnore)
def showValues(self, icell)
Load particle ensemble .csv files.
Load binary postrad data.
def read_next_particle(self)
def particle(self, index)
def __init__(self, offset, file='data0000', npayload=1, components=3, delimiter=')
def particle(self, index)