forked from python-hydro/pyro2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_indexer.py
More file actions
148 lines (117 loc) · 4.48 KB
/
Copy patharray_indexer.py
File metadata and controls
148 lines (117 loc) · 4.48 KB
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
from __future__ import print_function
import numpy as np
from util import msg
def _buf_split(b):
try: bxlo, bxhi, bylo, byhi = b
except:
try: blo, bhi = b
except:
blo = b
bhi = b
bxlo = bylo = blo
bxhi = byhi = bhi
return bxlo, bxhi, bylo, byhi
class ArrayIndexer(np.ndarray):
""" 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, grid=None):
obj = np.asarray(d).view(self)
obj.g = grid
obj.c = len(d.shape)
return obj
def __array_finalize__(self, obj):
if obj is None: return
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 self.ip_jp(0, 0, buf=buf, n=n, s=s)
def ip(self, shift, buf=0, n=0, s=1):
return self.ip_jp(shift, 0, buf=buf, n=n, s=s)
def jp(self, shift, buf=0, n=0, s=1):
return self.ip_jp(0, shift, buf=buf, n=n, s=s)
def ip_jp(self, ishift, jshift, buf=0, n=0, s=1):
bxlo, bxhi, bylo, byhi = _buf_split(buf)
if self.c == 2:
return np.asarray(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 np.asarray(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 norm(self, n=0):
"""
find the norm of the quantity (index n) defined on the same grid,
in the domain's valid region
"""
if self.c == 2:
return self.g.norm(self)
else:
return self.g.norm(self[:,:,n])
def copy(self):
return ArrayIndexer(np.asarray(self).copy(), grid=self.g)
def is_symmetric(self, nodal=False, tol=1.e-14):
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:
print(self.g.ilo,self.g.ilo+self.g.nx/2+1)
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 - np.flipud(R)).max()
print(e, tol, e < tol)
return e < tol
def is_asymmetric(self, nodal=False, tol=1.e-14):
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:
print(self.g.ilo,self.g.ilo+self.g.nx/2+1)
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 + np.flipud(R)).max()
print(e, tol, e < tol)
return e < tol
def pretty_print(self, fmt=None):
"""
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)
for j in reversed(range(self.g.qy)):
for i in range(self.g.qx):
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 gc:
print("\033[31m" + fmt % (self[i,j]) + "\033[0m", end="")
else:
print(fmt % (self[i,j]), end="")
print(" ")
leg = """
^ y
|
+---> x
"""
print(leg)