-
Notifications
You must be signed in to change notification settings - Fork 19
/
miranda_reader.py
648 lines (475 loc) · 19.8 KB
/
miranda_reader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
import numpy
import os
from base_reader import BaseReader
class MirandaReader(BaseReader):
"""
Class to read in parallel binary data generated by the Miranda code.
"""
def __init__(self, plotmir_path, periodic_dimensions=(False,False,False), verbose=False):
"""
Constructor of the Miranda reader class
"""
# Main meta data file
self.filename_prefix = plotmir_path
self.plotMir = plotmir_path
# Proc boundaries
self.procExtents = {}
if len(periodic_dimensions) != 3:
raise ValueError("Input option periodic must be an iterable of size 3")
self._periodic = tuple(periodic_dimensions)
# Read in metadata and make a dictionary
self.plotDict = {}
pid = open(self.plotMir)
lines = pid.readlines()
for line in lines[1:]:
if (":" in line):
ikey = line.split(':')[0]
data = line.split(':')[1].split('#')[0].strip()
self.plotDict[ikey] = [data]
else:
data = line.split('#')[0].strip()
self.plotDict[ikey].append(data)
# Unpack the dict into usable data
self.verbose = verbose
self.zonal = False
self.legacy = False
if self.plotDict.has_key('zonal'):
if ( 'yes' in self.plotDict['zonal'] ):
self.zonal = True
else:
if self.verbose:
print "Warning: Zonal flag not given... using legacy options."
self.legacy = True
self.dx = float(filter(None,self.plotDict['spacing'][0].split(' '))[0].replace('D','e'))
self.dy = float(filter(None,self.plotDict['spacing'][0].split(' '))[1].replace('D','e'))
self.dz = float(filter(None,self.plotDict['spacing'][0].split(' '))[2].replace('D','e'))
# These include the ghost points
self.ax = int(filter(None,self.plotDict['blocksize'][0].split(' '))[0])
self.ay = int(filter(None,self.plotDict['blocksize'][0].split(' '))[1])
self.az = int(filter(None,self.plotDict['blocksize'][0].split(' '))[2])
self.nx = int(filter(None,self.plotDict['domainsize'][0].split(' '))[0])
self.ny = int(filter(None,self.plotDict['domainsize'][0].split(' '))[1])
self.nz = int(filter(None,self.plotDict['domainsize'][0].split(' '))[2])
self.px = self.nx / self.ax
self.py = self.ny / self.ay
self.pz = self.nz / self.az
offsetx = 0
offsety = 0
offsetz = 0
if self.zonal:
if self.nx > 1:
offsetx = 1
if self.ny > 1:
offsety = 1
if self.nz > 1:
offsetz = 1
self.xblk = 0
self.yblk = 0
self.zblk = 0
if not self.legacy:
if not self.zonal:
if self.px > 1:
self.xblk = 1
if self.py > 1:
self.yblk = 1
if self.pz > 1:
self.zblk = 1
# Fix the local indices
self.ax -= numpy.minimum(self.px-1,1)
self.ay -= numpy.minimum(self.py-1,1)
self.az -= numpy.minimum(self.pz-1,1)
# Fix the global indices
if not self.zonal:
self.nx -= numpy.minimum(self.px-1,1)*self.px
self.ny -= numpy.minimum(self.py-1,1)*self.py
self.nz -= numpy.minimum(self.pz-1,1)*self.pz
else:
self.nx -= offsetx
self.ny -= offsety
self.nz -= offsetz
self._domain_size = numpy.array([self.nx,self.ny,self.nz])
# Recomputed to fix actual count
self.px = self.nx / self.ax
self.py = self.ny / self.ay
self.pz = self.nz / self.az
self.nprocs = self.px * self.py * self.pz
self.procs = {}
ipp = 0
for ipz in range(self.pz):
for ipy in range(self.py):
for ipx in range(self.px):
gx1 = self.ax*ipx
gy1 = self.ay*ipy
gz1 = self.az*ipz
gxn = self.ax*(ipx+1)
gyn = self.ay*(ipy+1)
gzn = self.az*(ipz+1)
self.procs[ipp] = {}
self.procs[ipp]['g1'] = [gx1,gy1,gz1]
self.procs[ipp]['gn'] = [gxn,gyn,gzn]
ipp += 1
self.varNames = []
for var in self.plotDict['variables'][1:]:
tmp = filter(None,var.split(' '))
name = tmp[0]
num = int(tmp[1])
if num > 1:
for inum in range(num):
self.varNames.append( name+'-'+str(inum) )
else:
self.varNames.append( name )
# Also include the materials if it exists
if self.plotDict.has_key('materials'):
for var in self.plotDict['materials'][1:]:
tmp = filter(None,var.split(' '))
name = tmp[0]
num = int(tmp[1])
if num > 1:
for inum in range(num):
self.varNames.append( name+'-'+str(inum) )
else:
self.varNames.append( name )
self.nvars = len(self.varNames)
# Make timearray
self._times = {}
self._steps = []
if self.plotDict.has_key('timesteps'):
for tt in self.plotDict['timesteps'][1:]:
tmp = filter(None,tt.split(' '))
tindex = int(tmp[0])
time = float(tmp[1])
self._times[tindex] = time
self._steps.append( tindex )
self.maxTimeIndex = numpy.max( self._times.keys() )
# Make a dictionary for lookups
self.varDict = {}
for ii in range(len(self.varNames)):
name = self.varNames[ii]
self.varDict[name] = ii
self.dataFiles = os.path.dirname(self.plotMir) + '/' + self.plotDict['datafiles'][0]
self.gridFiles = os.path.dirname(self.plotMir) + '/' + self.plotDict['gridfiles'][0]
# Step is set to 0 by default.
self._step = 0
def setStep(self, step):
"""
Update the metadata from the summary file in the data directory at a new time step.
"""
assert (step in self._steps), "Step to read in is not available in the dataset."
self._step = step
def getStep(self):
return self._step
step = property(getStep, setStep)
def setSubDomain(self, lo_and_hi):
"""
Set the sub-domain for reading coordinates and data.
"""
# Check if lo and hi are within the domain bounds first!!!
try:
lo, hi = lo_and_hi
except ValueError:
raise ValueError("Pass an iterable with two items!")
for i in range(3):
if lo[i] < 0 or lo[i] > self._domain_size[i]:
raise ValueError('Invalid indices in chunk. Cannot be < 0 or > domain size!')
if hi[i] < 0 or hi[i] > self._domain_size[i]:
raise ValueError('Invalid indices in chunk. Cannot be < 0 or > domain size!')
if hi[i] < lo[i]:
raise ValueError('Invalid indices in chunk. Upper bound cannot be smaller than lower bound!')
# Now set the chunk to be used later.
self.chunk = ( (lo[0], hi[0] + 1), (lo[1], hi[1] + 1), (lo[2], hi[2] + 1) )
def getSubDomain(self):
"""
Return two tuples containing the sub-domain used in this reader
as a lower bound (lo) and upper bound (hi).
"""
lo = (self.chunk[0][0], self.chunk[1][0], self.chunk[2][0])
hi = (self.chunk[0][1] - 1, self.chunk[1][1] - 1, self.chunk[2][1] - 1)
return lo, hi
sub_domain = property(getSubDomain, setSubDomain)
@property
def domain_size(self):
"""
Return a tuple containing the full domain size of this dataset.
"""
return tuple(self._domain_size)
@property
def dimension(self):
"""
Return the dimension of the domain.
"""
return 3
@property
def periodic_dimensions(self):
"""
Return a tuple indicating if data is periodic in each dimension.
"""
return self._periodic
@property
def time(self):
"""
Return the simulation time at current time step.
"""
return self._times[self._step]
@property
def data_order(self):
"""
Return the data order.
"""
return 'F'
@property
def steps(self):
"""
Return all of the steps.
"""
return self._steps
def readDataProc(self,time,proc,var_list ):
# Take diff argument types
for ivar in var_list:
if ivar not in self.varDict.keys():
print "%s not a valid variable" % ivar
sdata = self.dataFiles % (time,proc)
# Open fortran file
fd = open(sdata,'rb')
data = numpy.fromfile(file=fd,dtype=numpy.single)
fd.close()
# Return either list of arrays or single array
vals = []
for ivar in var_list:
val = self.readDataProc_var( self.varDict[ivar] , proc, data)
vals.append( val )
return vals
def readDataProc_var(self,ivar, proc, data):
# Ghost point in read
ibx = self.xblk
iby = self.yblk
ibz = self.zblk
# shape = (self.az+ibz,self.ay+iby,self.ax+ibx)
shape = (self.ax+ibx,self.ay+iby,self.az+ibz)
stride = numpy.product(shape) + 2
istart = ivar*stride + 1
iend = (ivar+1)*stride - 1
# Vdata = data[istart:iend].reshape(shape)
Vdata = data[istart:iend].reshape(shape, order='F') # Reshape using Fortran order directly
# and avoid axis swap later
# Some logic
sx = 0; ex = self.ax
sy = 0; ey = self.ay
sz = 0; ez = self.az
if (not self.legacy) and (not self.zonal):
if ( (self.procs[proc]['gn'][0] == self.nx) and self.px > 1 ):
sx += 1
ex += 1
if ( (self.procs[proc]['gn'][1] == self.ny) and self.py > 1 ):
sy += 1
ey += 1
if ( (self.procs[proc]['gn'][2] == self.nz) and self.pz > 1 ):
sz += 1
ez += 1
# return numpy.swapaxes(Vdata,0,2)[sx:ex,sy:ey,sz:ez]
return Vdata
def readGridProc(self,proc):
sdata = self.gridFiles % (proc)
# Ghost point in read
ibx = self.xblk
iby = self.yblk
ibz = self.zblk
# Zonals have redundant data
if self.zonal:
if self.nx > 1:
ibx += 1
if self.ny > 1:
iby += 1
if self.nz > 1:
ibz += 1
# Open fortran file
fd = open(sdata,'rb')
data = numpy.fromfile(file=fd,dtype=numpy.single)
# shape = (self.az+ibz,self.ay+iby,self.ax+ibx)
shape = (self.ax+ibx,self.ay+iby,self.az+ibz)
stride = numpy.product(shape) + 2
ivar = 0 # xgrid
istart = ivar*stride + 1
iend = (ivar+1)*stride - 1
# Xdata = data[istart:iend].reshape(shape)
Xdata = data[istart:iend].reshape(shape, order='F') # Reshape using Fortran order directly
# Xdata = numpy.swapaxes(Xdata,0,2)
ivar = 1 # ygrid
istart = ivar*stride + 1
iend = (ivar+1)*stride - 1
# Ydata = data[istart:iend].reshape(shape)
Ydata = data[istart:iend].reshape(shape, order='F') # Reshape using Fortran order directly
# Ydata = numpy.swapaxes(Ydata,0,2)
ivar = 2 # zgrid
istart = ivar*stride + 1
iend = (ivar+1)*stride - 1
# Zdata = data[istart:iend].reshape(shape)
Zdata = data[istart:iend].reshape(shape, order='F') # Reshape using Fortran order directly
# Zdata = numpy.swapaxes(Zdata,0,2)
# Zonal average in each direction
if self.zonal:
isx = int(self.nx > 1)
isy = int(self.ny > 1)
isz = int(self.nz > 1)
if isx:
Xdata = (Xdata[:-1,isy:,isz:] + Xdata[1:,isy:,isz:] ) / 2.0
else:
Xdata = Xdata[isx:,isy:,isz:]
if isy:
Ydata = (Ydata[isx:,:-1,isz:] + Ydata[isx:,1:,isz:] ) / 2.0
else:
Ydata = Ydata[isx:,isy:,isz:]
if isz:
Zdata = (Zdata[isx:,isy:,:-1] + Zdata[isx,isy:,1:] ) / 2.0
else:
Zdata = Zdata[isx:,isy:,isz:]
# Some logic
sx = 0; ex = self.ax
sy = 0; ey = self.ay
sz = 0; ez = self.az
if not self.legacy and (not self.zonal) :
if ( (self.procs[proc]['gn'][0] == self.nx) and self.px > 1 ):
sx += 1
ex += 1
if ( (self.procs[proc]['gn'][1] == self.ny) and self.py > 1 ):
sy += 1
ey += 1
if ( (self.procs[proc]['gn'][2] == self.nz) and self.pz > 1 ):
sz += 1
ez += 1
return [Xdata[sx:ex,sy:ey,sz:ez] , Ydata[sx:ex,sy:ey,sz:ez] , Zdata[sx:ex,sy:ey,sz:ez] ]
def readGridChunk(self,irange):
"""
Same as readData but only reads in global range of data given by
irange.
"""
Rx = [0]*2
Ry = [0]*2
Rz = [0]*2
Rx[0] = irange[0]
Rx[1] = irange[1]
Ry[0] = irange[2]
Ry[1] = irange[3]
Rz[0] = irange[4]
Rz[1] = irange[5]
xx = numpy.zeros( (Rx[1]-Rx[0],Ry[1]-Ry[0],Rz[1]-Rz[0]), order='F' )
yy = numpy.zeros( (Rx[1]-Rx[0],Ry[1]-Ry[0],Rz[1]-Rz[0]), order='F' )
zz = numpy.zeros( (Rx[1]-Rx[0],Ry[1]-Ry[0],Rz[1]-Rz[0]), order='F' )
for iproc in range(self.nprocs):
g1 = self.procs[iproc]['g1']
gn = self.procs[iproc]['gn']
# Shift left point if node data
iff = 0;jff = 0;kff = 0;
c1 = (Rx[1] in range(g1[0],gn[0]) )
c2 = (Rx[0] in range(g1[0],gn[0]) )
c3 = ( (g1[0] and gn[0]) in range(Rx[0],Rx[1]+1) )
CX = c1 or c2 or c3
c1 = (Ry[1] in range(g1[1],gn[1]) )
c2 = (Ry[0] in range(g1[1],gn[1]) )
c3 = ( (g1[1] and gn[1]) in range(Ry[0],Ry[1]+1) )
CY = c1 or c2 or c3
c1 = (Rz[1] in range(g1[2],gn[2]) )
c2 = (Rz[0] in range(g1[2],gn[2]) )
c3 = ( (g1[2] and gn[2]) in range(Rz[0],Rz[1]+1) )
CZ = c1 or c2 or c3
if ( CX and CY and CZ ):
Li1 = numpy.max( (0 , Rx[0] - g1[0] ) ) + iff
Lif = numpy.min( (Rx[1] , gn[0] ) ) - g1[0] + iff
Ki1 = numpy.max( (Rx[0] , g1[0]) ) - Rx[0]
Kif = Ki1 + (Lif-Li1)
Lj1 = numpy.max( (0 , Ry[0] - g1[1] ) ) + jff
Ljf = numpy.min( (Ry[1] , gn[1] ) ) - g1[1] + jff
Kj1 = numpy.max( (Ry[0] , g1[1]) ) - Ry[0]
Kjf = Kj1 + (Ljf-Lj1)
Lk1 = numpy.max( (0 , Rz[0] - g1[2] ) ) + kff
Lkf = numpy.min( (Rz[1] , gn[2] ) ) - g1[2] + kff
Kk1 = numpy.max( (Rz[0] , g1[2]) ) - Rz[0]
Kkf = Kk1 + (Lkf-Lk1)
[x,y,z] = self.readGridProc(iproc)
xx[Ki1:Kif,Kj1:Kjf,Kk1:Kkf] = x[Li1:Lif,Lj1:Ljf,Lk1:Lkf]
yy[Ki1:Kif,Kj1:Kjf,Kk1:Kkf] = y[Li1:Lif,Lj1:Ljf,Lk1:Lkf]
zz[Ki1:Kif,Kj1:Kjf,Kk1:Kkf] = z[Li1:Lif,Lj1:Ljf,Lk1:Lkf]
return [xx,yy,zz]
def readCoordinates(self):
"""
Method to read in the X, Y and Z coordinates of a chunk of index values.
"""
irange = [self.chunk[0][0],self.chunk[0][1],
self.chunk[1][0],self.chunk[1][1],
self.chunk[2][0],self.chunk[2][1]]
[x,y,z] = self.readGridChunk(irange)
return x , y , z
def readChunk(self,time,variable,irange):
"""
Same as readData but only reads in global range of data given by
irange.
"""
# Make them all lists
if type(variable) == type('foo'):
variable = [ variable ]
Rx = [0]*2
Ry = [0]*2
Rz = [0]*2
Rx[0] = irange[0]
Rx[1] = irange[1]
Ry[0] = irange[2]
Ry[1] = irange[3]
Rz[0] = irange[4]
Rz[1] = irange[5]
vdata = []
for ii in range(len(variable)):
vdata.append( numpy.zeros( (Rx[1]-Rx[0],Ry[1]-Ry[0],Rz[1]-Rz[0]), order='F' ) )
for iproc in range(self.nprocs):
g1 = self.procs[iproc]['g1']
gn = self.procs[iproc]['gn']
# Shift left point if node data
iff = 0;jff = 0;kff = 0;
c1 = (Rx[1] in range(g1[0],gn[0]) )
c2 = (Rx[0] in range(g1[0],gn[0]) )
c3 = ( (g1[0] and gn[0]) in range(Rx[0],Rx[1]+1) )
CX = c1 or c2 or c3
c1 = (Ry[1] in range(g1[1],gn[1]) )
c2 = (Ry[0] in range(g1[1],gn[1]) )
c3 = ( (g1[1] and gn[1]) in range(Ry[0],Ry[1]+1) )
CY = c1 or c2 or c3
c1 = (Rz[1] in range(g1[2],gn[2]) )
c2 = (Rz[0] in range(g1[2],gn[2]) )
c3 = ( (g1[2] and gn[2]) in range(Rz[0],Rz[1]+1) )
CZ = c1 or c2 or c3
if ( CX and CY and CZ ):
Li1 = numpy.max( (0 , Rx[0] - g1[0] ) ) + iff
Lif = numpy.min( (Rx[1] , gn[0] ) ) - g1[0] + iff
Ki1 = numpy.max( (Rx[0] , g1[0]) ) - Rx[0]
Kif = Ki1 + (Lif-Li1)
Lj1 = numpy.max( (0 , Ry[0] - g1[1] ) ) + jff
Ljf = numpy.min( (Ry[1] , gn[1] ) ) - g1[1] + jff
Kj1 = numpy.max( (Ry[0] , g1[1]) ) - Ry[0]
Kjf = Kj1 + (Ljf-Lj1)
Lk1 = numpy.max( (0 , Rz[0] - g1[2] ) ) + kff
Lkf = numpy.min( (Rz[1] , gn[2] ) ) - g1[2] + kff
Kk1 = numpy.max( (Rz[0] , g1[2]) ) - Rz[0]
Kkf = Kk1 + (Lkf-Lk1)
pdata = self.readDataProc(time,iproc,variable)
for ii in range(len(variable)):
vdata[ii][Ki1:Kif,Kj1:Kjf,Kk1:Kkf] = pdata[ii][Li1:Lif,Lj1:Ljf,Lk1:Lkf]
# # Return non-list
# if len(variable) == 1:
# return vdata[0]
# else:
# return vdata
# Return tuple
return tuple(vdata)
def readData(self, var_names, data=None):
"""
Method to read in the a chunk of the data for variables at current vizdump step.
"""
irange = [self.chunk[0][0],self.chunk[0][1],
self.chunk[1][0],self.chunk[1][1],
self.chunk[2][0],self.chunk[2][1]]
variable = var_names
time = self._step
data = self.readChunk(time,variable,irange)
return data
BaseReader.register(MirandaReader)
if __name__ == '__main__':
print 'Subclass:', issubclass(MirandaReader, BaseReader)
print 'Instance:', isinstance(MirandaReader("../tests/test_data_miranda/RM_CTR_3D_64/plot.mir"), BaseReader)