pion-reader 2.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,265 @@
1
+ # Author: Sam Green, Created: 18-10-17
2
+ # Script opens up the header of silo files and saves some variables.
3
+
4
+ # New comments:
5
+ # - 2016-12.14 JM: ReadData has a CLOSE() function that calls the OpenSilo
6
+ # CLOSE() function. Xmin/Xmax/time etc. all now new directory to /header.
7
+ # - 15-02-2019 SG: Added new functions level_max and level_min and nlevels to help with nested grid python.
8
+ # - 22-04-2020 SG: Added all functions that use the init function in the OpenData class.
9
+ # - 22-04-2020 SG: Removed hard-coded cm in xmax and xmin.
10
+ # - 2026-12-03 JM: Snapshots with all levels in one set of files (PION
11
+ # silo_format 2, since 3.0.0-dev.2): one file name (the _0000 file) gives
12
+ # all levels, in /level_LL. Old snapshots (one file per level) are
13
+ # read as before, from a list of files.
14
+
15
+ # -------------- Set of libraries needed:
16
+ import sys
17
+
18
+ # Point to silo installation folder.
19
+ # I recommend installed silo here at ~/.local/silo/
20
+ from pathlib import Path
21
+
22
+ home = str(Path.home())
23
+ sys.path.insert(0, home + "/.local/silo/lib")
24
+ import Silo
25
+
26
+ import numpy as np
27
+ from astropy import units as u
28
+ from os import path as ospath
29
+
30
+ # -------------- Class to open Silo files and save variables.
31
+
32
+
33
+ class OpenData:
34
+ # This will open the .SILO file and enters the 'header' directory.
35
+ # "files" is the snapshot's file name (all levels in one set of files,
36
+ # PION silo_format 2), or a list of file names, one per level (old
37
+ # layout; a one-element list works for both).
38
+ def __init__(self, files):
39
+ if isinstance(files, (str, bytes)):
40
+ files = [files]
41
+ self.data = list(files)
42
+ self.db = Silo.Open(self.data[0])
43
+ self.OpenFile = self.data[0]
44
+ self.db.SetDir('/header')
45
+ # layout of the snapshot: 2 = all levels in one set of files
46
+ self.format = 1
47
+ if "silo_format" in self.db.GetToc().var_names:
48
+ self.format = int(self.db.GetVar("silo_format"))
49
+ self.level = 0
50
+ self.db.SetDir('/header')
51
+
52
+ # Directory with the data of the current level ("/" for old files).
53
+ def level_dir(self):
54
+ if self.format >= 2:
55
+ return "/level_%02d" % self.level
56
+ return "/"
57
+
58
+ # Call this function with a for loop to cycle through each level:
59
+ # selects the level's directory (new layout) or opens the level's file
60
+ # (old layout).
61
+ def open(self, level):
62
+ self.level = level
63
+ if self.format >= 2:
64
+ if self.OpenFile != self.data[0]:
65
+ self.db.Close()
66
+ self.db = Silo.Open(self.data[0])
67
+ self.OpenFile = self.data[0]
68
+ self.db.SetDir('/header')
69
+ return
70
+ self.db.Close()
71
+ self.db = Silo.Open(self.data[level])
72
+ self.db.SetDir('/header')
73
+ self.OpenFile = self.data[level]
74
+
75
+ # To close all the variables after use.
76
+ def close(self):
77
+ self.db.Close()
78
+ self.OpenFile = ""
79
+
80
+ # Returns the header information:
81
+ def header_info(self):
82
+ header = self.db.GetToc()
83
+ return header
84
+
85
+ # Xmax variable contains the max value for each axis of the grid, for each level
86
+ def xmax_all(self):
87
+ l = self.nlevels()
88
+ xmax = [[None]] * l
89
+ for i in range(l):
90
+ self.open(i)
91
+ xmax[i] = self.level_max()
92
+ return xmax
93
+
94
+ # Xmin variable contains the min value for every axis of the grid.
95
+ def xmin_all(self):
96
+ l = self.nlevels()
97
+ xmin = [[None]] * l
98
+ for i in range(l):
99
+ self.open(i)
100
+ xmin[i] = self.level_min()
101
+ return xmin
102
+
103
+ # Xmax variable contains the max value for every axis of the grid.
104
+ def xmax(self):
105
+ self.db.SetDir('/header')
106
+ xmax = self.db.GetVar("Xmax")
107
+ return xmax
108
+
109
+ # Xmin variable contains the min value for every axis of the grid.
110
+ def xmin(self):
111
+ self.db.SetDir('/header')
112
+ xmin = self.db.GetVar("Xmin")
113
+ return xmin
114
+
115
+ # level_xmax variable contains the max value for every axes of each level.
116
+ def level_max(self):
117
+ self.db.SetDir(self.level_dir() if self.format >= 2 else '/header')
118
+ level_max = self.db.GetVar("level_xmax")
119
+ return level_max
120
+
121
+ # level_xmin variable contains the min value for every axis of each level.
122
+ def level_min(self):
123
+ self.db.SetDir(self.level_dir() if self.format >= 2 else '/header')
124
+ level_min = self.db.GetVar("level_xmin")
125
+ return level_min
126
+
127
+ # nlevels variable contains the number of levels in the simulation.
128
+ def nlevels(self):
129
+ self.db.SetDir('/header')
130
+ level = self.db.GetVar("grid_nlevels")
131
+ return level
132
+
133
+ # Number of levels to read: all levels of a snapshot in one set of
134
+ # files, or the number of level files given (old layout).
135
+ def nlevels_to_read(self):
136
+ if self.format >= 2:
137
+ return int(self.nlevels())
138
+ return min(int(self.nlevels()), len(self.data))
139
+
140
+ # ndim variable: how many spatial dimensions on grid
141
+ def ndim(self):
142
+ self.db.SetDir('/header')
143
+ dim = self.db.GetVar("gridndim")
144
+ return dim
145
+
146
+ # cycle is the simulation timestep on the finest level.
147
+ def cycle(self):
148
+ self.db.SetDir('/')
149
+ cycle = self.db.GetVar("cycle")
150
+ return cycle
151
+
152
+ # sim_time variable contains the simulation time.
153
+ def sim_time(self):
154
+ self.db.SetDir('/header')
155
+ simtime = self.db.GetVar("t_sim") * u.second
156
+ return simtime
157
+
158
+ # ngrid variable contains the size of the grid.
159
+ def ngrid(self):
160
+ self.db.SetDir('/header')
161
+ ngrid = self.db.GetVar("NGrid")
162
+ ngrid = [int(_) for _ in ngrid]
163
+ return ngrid
164
+
165
+ # nproc variable contains the number of domains in the silo file.
166
+ def nproc(self):
167
+ self.db.SetDir('/header')
168
+ nproc = self.db.GetVar("MPI_nproc")
169
+ return nproc
170
+
171
+ # dom_size contains the size of the domain in the grid.
172
+ def dom_size(self):
173
+ ndom = np.array([1, 1, 1])
174
+ cells = np.copy(self.ngrid())
175
+ i = 1
176
+ while i < self.nproc():
177
+ axis = np.argmax(cells)
178
+ i *= 2
179
+ ndom[axis] *= 2
180
+ cells[axis] /= 2
181
+ domsize = self.ngrid() / ndom
182
+ domsize = [int(_) for _ in domsize]
183
+ ndom1 = ndom
184
+ ndom1 = [int(_) for _ in ndom1]
185
+ return {'DomSize': domsize, 'Ndom': ndom1}
186
+
187
+ # Retrieves the requested data from the silo file
188
+ def variable(self, par):
189
+ # Saves the selected data as a variable.
190
+ param = self.db.GetVar(par + "_data")
191
+ # Saves the selected data's dimensions.
192
+ param_dims = self.db.GetVar(par + "_dims")
193
+ # Reshapes the dimensions into the correct orientation.
194
+ if (self.ndim() == 1):
195
+ param_dims = [param_dims]
196
+ else:
197
+ param_dims = param_dims[::-1]
198
+ # Puts the array into the correct format, i.e. (a,b).
199
+ param = np.array(param).reshape(param_dims)
200
+ # return the data, and the number of dimensions
201
+ return param, len(param_dims)
202
+
203
+ # Opens up the sub-domains and saves the data specified in "variable".
204
+ # "data" is a string corresponding to a scalar variable, e.g. "Density"
205
+ def parameter(self, data):
206
+ array_param = [] # data for each sub-domain
207
+ exten_param = [] # min-extents of each sub-domain
208
+ # the level's multimesh: in /level_LL (new layout) or / (old)
209
+ self.db.SetDir(self.level_dir())
210
+ extents = self.db.GetVar("MultiMesh_extents")
211
+ i = 0 # index in extents
212
+ pp = self.db.GetVar(data + "_varnames")
213
+ newfile = ""
214
+ nowfile = self.OpenFile
215
+ newpar = ""
216
+ origfile = ""
217
+ #for n in paths:
218
+ for n in pp:
219
+ if n == "":
220
+ continue # 1st and last elements are empty
221
+ # if "n" contains a ":" then the domain is in a different file.
222
+ # We need to close the current file and open another one.
223
+ loc = n.find(":")
224
+ if loc != -1:
225
+ if (newfile == ""):
226
+ origfile = self.OpenFile
227
+ nowfile = self.OpenFile
228
+ else:
229
+ nowfile = newfile
230
+ # split newfile into filename + location
231
+ newfile = n[:loc]
232
+ newpar = n[loc + 1:]
233
+ # append full path to new filename
234
+ newfile = ospath.dirname(self.OpenFile) + "/" + newfile
235
+ # only close old / open new file if they are not the same file
236
+ if (newfile != nowfile):
237
+ self.db.Close()
238
+ self.db = Silo.Open(newfile)
239
+ self.db.SetDir("/")
240
+ nowfile = newfile
241
+ elif nowfile != self.OpenFile:
242
+ # domain in the first file, but another file is open
243
+ self.db.Close()
244
+ self.db = Silo.Open(self.OpenFile)
245
+ newpar = n
246
+ nowfile = self.OpenFile
247
+ newfile = ""
248
+ else:
249
+ # nothing do do because domain is in my file.
250
+ newpar = n
251
+ nowfile = self.OpenFile
252
+ #print(nowfile,newpar)
253
+ d, v = ospath.split(newpar)
254
+ rank = d[6:10] # save rank so i can get extents.
255
+ self.db.SetDir(d)
256
+ variable_data, idim = self.variable(v)
257
+ array_param.append(variable_data)
258
+ min_extents = extents[i:i + idim]
259
+ i = i + 2 * idim
260
+ exten_param.append(min_extents)
261
+ # re-open original file that was being used for the 1st sub-domain.
262
+ self.db.Close()
263
+ self.db = Silo.Open(self.OpenFile)
264
+ self.db.SetDir("/")
265
+ return np.array(array_param), np.array(exten_param)
@@ -0,0 +1,4 @@
1
+ from .SiloHeader_data import OpenData
2
+ from .util import *
3
+ from .ReadData import ReadData
4
+ from .microphysics import microphysics