-
Notifications
You must be signed in to change notification settings - Fork 123
/
array_indexer.py
330 lines (266 loc) · 11.3 KB
/
array_indexer.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
"""An array class that has methods supporting the type of stencil
operations we see in finite-difference methods, like i+1, i-1, etc.
"""
from __future__ import print_function
import numpy as np
from unyt import unyt_array
def _buf_split(b):
""" take an integer or iterable and break it into a -x, +x, -y, +y
value representing a ghost cell buffer
"""
try:
bxlo, bxhi, bylo, byhi = b
except (ValueError, TypeError):
try:
blo, bhi = b
except (ValueError, TypeError):
blo = b
bhi = b
bxlo = bylo = blo
bxhi = byhi = bhi
return bxlo, bxhi, bylo, byhi
class ArrayIndexer(unyt_array):
""" a class that wraps the data region of a single array (d)
and allows us to easily do array operations like d[i+1,j]
using the ip() method. """
def __new__(self, d, input_units=None, registry=None, dtype=None, bypass_validation=False,
grid=None):
obj = super(ArrayIndexer, self).__new__(self, d, input_units, registry, dtype,
bypass_validation)
obj.g = grid
obj.c = len(d.shape)
return obj
def __array_finalize__(self, obj):
if obj is None:
return
super(ArrayIndexer, self).__array_finalize__(obj)
self.g = getattr(obj, "g", None)
self.c = getattr(obj, "c", None)
def __array_wrap__(self, out_arr, context=None):
return np.ndarray.__array_wrap__(self, out_arr, context)
def v(self, buf=0, n=0, s=1):
"""return a view of the valid data region for component n, with stride
s, and a buffer of ghost cells given by buf
"""
return self.ip_jp(0, 0, buf=buf, n=n, s=s)
def ip(self, shift, buf=0, n=0, s=1):
"""return a view of the data shifted by shift in the x direction. By
default the view is the same size as the valid region, but the
buf can specify how many ghost cells on each side to include.
The component is n and s is the stride
"""
return self.ip_jp(shift, 0, buf=buf, n=n, s=s)
def jp(self, shift, buf=0, n=0, s=1):
"""return a view of the data shifted by shift in the y direction. By
default the view is the same size as the valid region, but the
buf can specify how many ghost cells on each side to include.
The component is n and s is the stride
"""
return self.ip_jp(0, shift, buf=buf, n=n, s=s)
def ip_jp(self, ishift, jshift, buf=0, n=0, s=1):
"""return a view of the data shifted by ishift in the x direction and
jshift in the y direction. By default the view is the same
size as the valid region, but the buf can specify how many
ghost cells on each side to include. The component is n and s
is the stride
"""
bxlo, bxhi, bylo, byhi = _buf_split(buf)
c = len(self.shape)
if c == 2:
return self[self.g.ilo-bxlo+ishift:self.g.ihi+1+bxhi+ishift:s,
self.g.jlo-bylo+jshift:self.g.jhi+1+byhi+jshift:s]
else:
return self[self.g.ilo-bxlo+ishift:self.g.ihi+1+bxhi+ishift:s,
self.g.jlo-bylo+jshift:self.g.jhi+1+byhi+jshift:s, n]
def lap(self, n=0, buf=0):
"""return the 5-point Laplacian"""
l = (self.ip(-1, n=n, buf=buf) - 2*self.v(n=n, buf=buf) + self.ip(1, n=n, buf=buf))/self.g.dx**2 + \
(self.jp(-1, n=n, buf=buf) - 2*self.v(n=n, buf=buf) + self.jp(1, n=n, buf=buf))/self.g.dy**2
return l
def norm(self, n=0):
"""
find the norm of the quantity (index n) defined on the same grid,
in the domain's valid region
"""
c = len(self.shape)
if c == 2:
return self.g.norm(self)
else:
return self.g.norm(self[:, :, n])
def copy(self):
"""make a copy of the array, defined on the same grid"""
return ArrayIndexer(np.asarray(self).copy(), grid=self.g)
def is_symmetric(self, nodal=False, tol=1.e-14, asymmetric=False):
"""return True is the data is left-right symmetric (to the tolerance
tol) For node-centered data, set nodal=True
"""
# prefactor to convert from symmetric to asymmetric test
s = 1
if asymmetric:
s = -1
if not nodal:
L = self[self.g.ilo:self.g.ilo+self.g.nx//2,
self.g.jlo:self.g.jhi+1]
R = self[self.g.ilo+self.g.nx//2:self.g.ihi+1,
self.g.jlo:self.g.jhi+1]
else:
L = self[self.g.ilo:self.g.ilo+self.g.nx//2+1,
self.g.jlo:self.g.jhi+1]
print(self.g.ilo+self.g.nx//2, self.g.ihi+2)
R = self[self.g.ilo+self.g.nx//2:self.g.ihi+2,
self.g.jlo:self.g.jhi+1]
e = abs(L - s*np.flipud(R)).max()
return e < tol
def is_asymmetric(self, nodal=False, tol=1.e-14):
"""return True is the data is left-right asymmetric (to the tolerance
tol)---e.g, the sign flips. For node-centered data, set nodal=True
"""
return self.is_symmetric(nodal=nodal, tol=tol, asymmetric=True)
def fill_ghost(self, n=0, bc=None):
"""Fill the boundary conditions. This operates on a single component,
n. We do periodic, reflect-even, reflect-odd, and outflow
We need a BC object to tell us what BC type on each boundary.
"""
# there is only a single grid, so every boundary is on
# a physical boundary (except if we are periodic)
# Note: we piggy-back on outflow and reflect-odd for
# Neumann and Dirichlet homogeneous BCs respectively, but
# this only works for a single ghost cell
# -x boundary
if bc.xlb in ["outflow", "neumann"]:
if bc.xl_value is None:
for i in range(self.g.ilo):
self[i, :, n] = self[self.g.ilo, :, n]
else:
self[self.g.ilo-1, :, n] = \
self[self.g.ilo, :, n] - self.g.dx*bc.xl_value[:]
elif bc.xlb == "reflect-even":
for i in range(self.g.ilo):
self[i, :, n] = self[2*self.g.ng-i-1, :, n]
elif bc.xlb in ["reflect-odd", "dirichlet"]:
if bc.xl_value is None:
for i in range(self.g.ilo):
self[i, :, n] = -self[2*self.g.ng-i-1, :, n]
else:
self[self.g.ilo-1, :, n] = \
2*bc.xl_value[:] - self[self.g.ilo, :, n]
elif bc.xlb == "periodic":
for i in range(self.g.ilo):
self[i, :, n] = self[self.g.ihi-self.g.ng+i+1, :, n]
# +x boundary
if bc.xrb in ["outflow", "neumann"]:
if bc.xr_value is None:
for i in range(self.g.ihi+1, self.g.nx+2*self.g.ng):
self[i, :, n] = self[self.g.ihi, :, n]
else:
self[self.g.ihi+1, :, n] = \
self[self.g.ihi, :, n] + self.g.dx*bc.xr_value[:]
elif bc.xrb == "reflect-even":
for i in range(self.g.ng):
i_bnd = self.g.ihi+1+i
i_src = self.g.ihi-i
self[i_bnd, :, n] = self[i_src, :, n]
elif bc.xrb in ["reflect-odd", "dirichlet"]:
if bc.xr_value is None:
for i in range(self.g.ng):
i_bnd = self.g.ihi+1+i
i_src = self.g.ihi-i
self[i_bnd, :, n] = -self[i_src, :, n]
else:
self[self.g.ihi+1, :, n] = \
2*bc.xr_value[:] - self[self.g.ihi, :, n]
elif bc.xrb == "periodic":
for i in range(self.g.ihi+1, 2*self.g.ng + self.g.nx):
self[i, :, n] = self[i-self.g.ihi-1+self.g.ng, :, n]
# -y boundary
if bc.ylb in ["outflow", "neumann"]:
if bc.yl_value is None:
for j in range(self.g.jlo):
self[:, j, n] = self[:, self.g.jlo, n]
else:
self[:, self.g.jlo-1, n] = \
self[:, self.g.jlo, n] - self.g.dy*bc.yl_value[:]
elif bc.ylb == "reflect-even":
for j in range(self.g.jlo):
self[:, j, n] = self[:, 2*self.g.ng-j-1, n]
elif bc.ylb in ["reflect-odd", "dirichlet"]:
if bc.yl_value is None:
for j in range(self.g.jlo):
self[:, j, n] = -self[:, 2*self.g.ng-j-1, n]
else:
self[:, self.g.jlo-1, n] = \
2*bc.yl_value[:] - self[:, self.g.jlo, n]
elif bc.ylb == "periodic":
for j in range(self.g.jlo):
self[:, j, n] = self[:, self.g.jhi-self.g.ng+j+1, n]
# +y boundary
if bc.yrb in ["outflow", "neumann"]:
if bc.yr_value is None:
for j in range(self.g.jhi+1, self.g.ny+2*self.g.ng):
self[:, j, n] = self[:, self.g.jhi, n]
else:
self[:, self.g.jhi+1, n] = \
self[:, self.g.jhi, n] + self.g.dy*bc.yr_value[:]
elif bc.yrb == "reflect-even":
for j in range(self.g.ng):
j_bnd = self.g.jhi+1+j
j_src = self.g.jhi-j
self[:, j_bnd, n] = self[:, j_src, n]
elif bc.yrb in ["reflect-odd", "dirichlet"]:
if bc.yr_value is None:
for j in range(self.g.ng):
j_bnd = self.g.jhi+1+j
j_src = self.g.jhi-j
self[:, j_bnd, n] = -self[:, j_src, n]
else:
self[:, self.g.jhi+1, n] = \
2*bc.yr_value[:] - self[:, self.g.jhi, n]
elif bc.yrb == "periodic":
for j in range(self.g.jhi+1, 2*self.g.ng + self.g.ny):
self[:, j, n] = self[:, j-self.g.jhi-1+self.g.ng, n]
def pretty_print(self, n=0, fmt=None, show_ghost=True):
"""
Print out a small dataset to the screen with the ghost cells
a different color, to make things stand out
"""
if fmt is None:
if self.dtype == np.int:
fmt = "%4d"
elif self.dtype == np.float64:
fmt = "%10.5g"
else:
raise ValueError("ERROR: dtype not supported")
# print j descending, so it looks like a grid (y increasing
# with height)
if show_ghost:
ilo = 0
ihi = self.g.qx-1
jlo = 0
jhi = self.g.qy-1
else:
ilo = self.g.ilo
ihi = self.g.ihi
jlo = self.g.jlo
jhi = self.g.jhi
for j in reversed(range(jlo, jhi+1)):
for i in range(ilo, ihi+1):
if (j < self.g.jlo or j > self.g.jhi or
i < self.g.ilo or i > self.g.ihi):
gc = 1
else:
gc = 0
if self.c == 2:
val = self[i, j]
else:
val = self[i, j, n]
if gc:
print("\033[31m" + fmt % (val) + "\033[0m", end="")
else:
print(fmt % (val), end="")
print(" ")
leg = """
^ y
|
+---> x
"""
print(leg)