-
Notifications
You must be signed in to change notification settings - Fork 1
/
pluto.py
371 lines (312 loc) · 10.7 KB
/
pluto.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
import numpy as np
try:
import h5py as h5
hasH5 = True
except ImportError:
hasH5 = False
class pload(object):
# based on A. Mignone routine
def __init__(
self,
parameters,
datatype=None,
level=0,
x1range=None,
x2range=None,
x3range=None,
):
"""Loads the data.
**Inputs**:
datatype -- Datatype (default = 'double')
**Outputs**:
pyPLUTO pload object whose keys are arrays of data values.
"""
self.NStep = parameters["on"]
self.Dt = 1.0e-4
self.n1 = 0
self.n2 = 0
self.n3 = 0
self.x1 = []
self.x2 = []
self.x3 = []
self.dx1 = []
self.dx2 = []
self.dx3 = []
self.x1range = x1range
self.x2range = x2range
self.x3range = x3range
self.NStepStr = str(self.NStep)
while len(self.NStepStr) < 4:
self.NStepStr = "0" + self.NStepStr
if datatype is None:
datatype = "double"
self.datatype = datatype
self.level = level
self.wdir = parameters["dir"]
Data_dictionary = self.ReadDataFile(self.NStepStr, parameters)
for keys in Data_dictionary:
object.__setattr__(self, keys, Data_dictionary.get(keys))
def ReadTimeInfo(self, timefile):
"""Read time info from the outfiles.
**Inputs**:
timefile -- name of the out file which has timing information.
"""
fh5 = h5.File(timefile, "r")
self.SimTime = fh5.attrs.get("time")
self.Dt = 1.0e-2
fh5.close()
def keys(self, f):
return [key for key in f.keys()]
def ReadVarFile(self, varfile, parameters):
"""Read variable names from the outfiles.
**Inputs**:
varfile -- name of the out file which has variable information.
"""
fh5 = h5.File(varfile, "r")
self.filetype = "single_file"
self.endianess = ">" # not used with AMR, kept for consistency
self.vars = []
num = str(parameters["on"])
for iv in range(len(fh5["Timestep_" + num + "/vars"].keys())):
self.vars.append(self.keys(fh5["Timestep_" + num + "/vars"])[iv])
fh5.close()
def ReadGridFile(self, gridfile):
"""Read grid values from the grid.out file.
*Inputs**:
gridfile -- name of the grid.out file which has information about the grid.
"""
xL = []
xR = []
nmax = []
gfp = open(gridfile, "r")
for i in gfp.readlines():
if len(i.split()) == 1:
try:
int(i.split()[0])
nmax.append(int(i.split()[0]))
except:
pass
if len(i.split()) == 3:
try:
int(i.split()[0])
xL.append(float(i.split()[1]))
xR.append(float(i.split()[2]))
except:
if i.split()[1] == "GEOMETRY:":
self.geometry = i.split()[2]
pass
self.n1, self.n2, self.n3 = nmax
n1 = self.n1
n1p2 = self.n1 + self.n2
n1p2p3 = self.n1 + self.n2 + self.n3
self.x1 = np.asarray([0.5 * (xL[i] + xR[i]) for i in range(n1)])
self.dx1 = np.asarray([(xR[i] - xL[i]) for i in range(n1)])
self.x2 = np.asarray([0.5 * (xL[i] + xR[i]) for i in range(n1, n1p2)])
self.dx2 = np.asarray([(xR[i] - xL[i]) for i in range(n1, n1p2)])
self.x3 = np.asarray([0.5 * (xL[i] + xR[i]) for i in range(n1p2, n1p2p3)])
self.dx3 = np.asarray([(xR[i] - xL[i]) for i in range(n1p2, n1p2p3)])
# Stores the total number of points in '_tot' variable in case only
# a portion of the domain is loaded. Redefine the x and dx arrays
# to match the requested ranges
self.n1_tot = self.n1
self.n2_tot = self.n2
self.n3_tot = self.n3
if self.x1range != None:
self.n1_tot = self.n1
self.irange = range(
abs(self.x1 - self.x1range[0]).argmin(),
abs(self.x1 - self.x1range[1]).argmin() + 1,
)
self.n1 = len(self.irange)
self.x1 = self.x1[self.irange]
self.dx1 = self.dx1[self.irange]
else:
self.irange = range(self.n1)
if self.x2range != None:
self.n2_tot = self.n2
self.jrange = range(
abs(self.x2 - self.x2range[0]).argmin(),
abs(self.x2 - self.x2range[1]).argmin() + 1,
)
self.n2 = len(self.jrange)
self.x2 = self.x2[self.jrange]
self.dx2 = self.dx2[self.jrange]
else:
self.jrange = range(self.n2)
if self.x3range != None:
self.n3_tot = self.n3
self.krange = range(
abs(self.x3 - self.x3range[0]).argmin(),
abs(self.x3 - self.x3range[1]).argmin() + 1,
)
self.n3 = len(self.krange)
self.x3 = self.x3[self.krange]
self.dx3 = self.dx3[self.krange]
else:
self.krange = range(self.n3)
self.Slice = (
(self.x1range != None) or (self.x2range != None) or (self.x3range != None)
)
# Create the xr arrays containing the edges positions
# Useful for pcolormesh which should use those
self.x1r = np.zeros(len(self.x1) + 1)
self.x1r[1:] = self.x1 + self.dx1 / 2.0
self.x1r[0] = self.x1r[1] - self.dx1[0]
self.x2r = np.zeros(len(self.x2) + 1)
self.x2r[1:] = self.x2 + self.dx2 / 2.0
self.x2r[0] = self.x2r[1] - self.dx2[0]
self.x3r = np.zeros(len(self.x3) + 1)
self.x3r[1:] = self.x3 + self.dx3 / 2.0
self.x3r[0] = self.x3r[1] - self.dx3[0]
prodn = self.n1 * self.n2 * self.n3
if prodn == self.n1:
self.nshp = self.n1
elif prodn == self.n1 * self.n2:
self.nshp = (self.n2, self.n1)
else:
self.nshp = (self.n3, self.n2, self.n1)
def getGrid(self, fp, dim):
x = fp["node_coords"][dim][:]
x = x.astype("float64")
return x
def getGridCell(self, fp, dim):
x = fp["cell_coords"][dim][:]
x = x.astype("float64")
return x
def getVar(self, fp, step, var):
returnData = fp["Timestep_" + str(step) + "/vars"][var][:]
return returnData
def DataScanHDF5(self, fp, myvars, parameters):
"""Scans HDF5 data files in PLUTO.
**Inputs**:
fp -- Data file pointer\n
myvars -- Names of the variables to read\n
**Output**:
Dictionary consisting of variable names as keys and its values.
"""
# Read the grid information
dim = np.size(fp["cell_coords"].keys())
x1r = self.getGrid(fp, "X")
x1 = self.getGridCell(fp, "X")
nx = x1.shape[2]
dx1 = x1r[1:] - x1r[:-1]
x2r = 0
x2 = 0
dx2 = 0
ny = 0
x3r = 0
x3 = 0
dx3 = 0
nz = 0
dt = 0
if dim > 1:
x2r = self.getGrid(fp, "Y")
x2 = self.getGridCell(fp, "Y")
ny = x2.shape[1]
dx2 = x2r[1:] - x2r[:-1]
if dim > 2:
x3r = self.getGrid(fp, "Z")
x3 = self.getGridCell(fp, "Z")
nz = x3.shape[0]
dx3 = x3r[1:] - x3r[:-1]
NewGridDict = dict(
[
("n1", nx),
("n2", ny),
("n3", nz),
("x1", x1),
("x2", x2),
("x3", x3),
("x1r", x1r),
("x2r", x2r),
("x3r", x3r),
("dx1", dx1),
("dx2", dx2),
("dx3", dx3),
("Dt", dt),
]
)
# Variables table
nvar = len(myvars)
vars = np.zeros((nx, ny, nz, nvar))
h5vardict = {}
for iv in range(nvar):
h5vardict[myvars[iv]] = self.getVar(fp, parameters["on"], myvars[iv])
# h5vardict[myvars[iv]] = vars[:,:,:,iv].squeeze()
OutDict = dict(NewGridDict)
OutDict.update(h5vardict)
return OutDict
def ReadSingleFile(
self, datafilename, myvars, parameters, n1, n2, n3, endian, dtype, ddict
):
"""Reads a single data file, data.****.dtype.
**Inputs**:
datafilename -- Data file name\n
myvars -- List of variable names to be read\n
n1 -- No. of points in X1 direction\n
n2 -- No. of points in X2 direction\n
n3 -- No. of points in X3 direction\n
endian -- Endianess of the data\n
dtype -- datatype\n
ddict -- Dictionary containing Grid and Time Information
which is updated
**Output**:
Updated Dictionary consisting of variable names as keys and its values.
"""
fp = h5.File(datafilename, "r")
print("Reading Data file : %s" % datafilename)
h5d = self.DataScanHDF5(fp, myvars, parameters)
ddict.update(h5d)
fp.close()
def ReadDataFile(self, num, parameters):
"""Reads the data file generated from PLUTO code.
**Inputs**:
num -- Data file number in form of an Integer.
**Outputs**:
Dictionary that contains all information about Grid, Time and
variables.
"""
gridfile = self.wdir + "grid.out"
dtype = "d"
dataext = ".dbl.h5"
nstr = num
varfile = self.wdir + "data." + nstr + dataext
self.ReadVarFile(varfile, parameters)
self.ReadGridFile(gridfile)
self.ReadTimeInfo(varfile)
nstr = num
if self.endianess == "big":
endian = ">"
else:
endian = "<"
D = [
("NStep", self.NStep),
("SimTime", self.SimTime),
("Dt", self.Dt),
("n1", self.n1),
("n2", self.n2),
("n3", self.n3),
("x1", self.x1),
("x2", self.x2),
("x3", self.x3),
("dx1", self.dx1),
("dx2", self.dx2),
("dx3", self.dx3),
("endianess", self.endianess),
("datatype", self.datatype),
("filetype", self.filetype),
]
ddict = dict(D)
datafilename = self.wdir + "data." + nstr + dataext
self.ReadSingleFile(
datafilename,
self.vars,
parameters,
self.n1,
self.n2,
self.n3,
endian,
dtype,
ddict,
)
return ddict