-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathtest_patch.py
More file actions
322 lines (250 loc) · 10.4 KB
/
test_patch.py
File metadata and controls
322 lines (250 loc) · 10.4 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
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
# unit tests for the patch
import numpy as np
from numpy.testing import assert_array_equal
import pyro.mesh.boundary as bnd
import pyro.mesh.patch as patch
# Grid2d tests
class TestGrid2d(object):
@classmethod
def setup_class(cls):
""" this is run once for each class before any tests """
pass
@classmethod
def teardown_class(cls):
""" this is run once for each class after all tests """
pass
def setup_method(self):
""" this is run before each test """
self.g = patch.Grid2d(4, 6, ng=2, ymax=1.5)
def teardown_method(self):
""" this is run after each test """
self.g = None
def test_dx_dy(self):
assert self.g.dx == 0.25
assert self.g.dy == 0.25
def test_grid_coords(self):
assert_array_equal(self.g.x[self.g.ilo:self.g.ihi+1],
np.array([0.125, 0.375, 0.625, 0.875]))
assert_array_equal(self.g.y[self.g.jlo:self.g.jhi+1],
np.array([0.125, 0.375, 0.625, 0.875, 1.125, 1.375]))
def test_grid_2d_coords(self):
assert_array_equal(self.g.x, self.g.x2d[:, self.g.jc])
assert_array_equal(self.g.y, self.g.y2d[self.g.ic, :])
def test_scratch_array(self):
q = self.g.scratch_array()
assert q.shape == (self.g.qx, self.g.qy)
def test_coarse_like(self):
q = self.g.coarse_like(2)
assert q.qx == 2*self.g.ng + self.g.nx//2
assert q.qy == 2*self.g.ng + self.g.ny//2
def test_fine_like(self):
q = self.g.fine_like(2)
assert q.qx == 2*self.g.ng + 2*self.g.nx
assert q.qy == 2*self.g.ng + 2*self.g.ny
def test_norm(self):
q = self.g.scratch_array()
# there are 24 elements, the norm L2 norm is
# sqrt(dx*dy*24)
q.v()[:, :] = np.array([[1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1]])
assert q.norm() == np.sqrt(24*self.g.dx*self.g.dy)
def test_equality(self):
g2 = patch.Grid2d(2, 5, ng=1)
assert g2 != self.g
# CellCenterData2d tests
class TestCellCenterData2d(object):
@classmethod
def setup_class(cls):
""" this is run once for each class before any tests """
pass
@classmethod
def teardown_class(cls):
""" this is run once for each class after all tests """
pass
def setup_method(self):
""" this is run before each test """
nx = 8
ny = 8
self.g = patch.Grid2d(nx, ny, ng=2, xmax=1.0, ymax=1.0)
self.d = patch.CellCenterData2d(self.g, dtype=int)
bco = bnd.BC(xlb="outflow", xrb="outflow",
ylb="outflow", yrb="outflow")
self.d.register_var("a", bco)
self.d.register_var("b", bco)
self.d.create()
def teardown_method(self):
""" this is run after each test """
self.g = None
self.d = None
def test_zeros(self):
a = self.d.get_var("a")
a[:, :] = 1.0
self.d.zero("a")
assert np.all(a.v() == 0.0)
def test_aux(self):
self.d.set_aux("ftest", 1.0)
self.d.set_aux("stest", "this was a test")
assert self.d.get_aux("ftest") == 1.0
assert self.d.get_aux("stest") == "this was a test"
def test_gets(self):
aname = self.d.get_var("a")
aname[:, :] = np.random.rand(aname.shape[0], aname.shape[1])
aindex = self.d.get_var_by_index(0)
assert_array_equal(aname, aindex)
def test_min_and_max(self):
a = self.d.get_var("a")
a.v()[:, :] = np.arange(self.g.nx*self.g.ny).reshape(self.g.nx, self.g.ny) + 1
assert self.d.min("a") == 1.0
assert self.d.max("a") == 64.0
def test_restrict(self):
a = self.d.get_var("a")
a.v()[:, :] = np.arange(self.g.nx*self.g.ny).reshape(self.g.nx, self.g.ny) + 1
c = self.d.restrict("a")
# restriction should be conservative, so compare the volume-weighted sums
assert np.sum(a.v()) == 4.0*np.sum(c.v())
def test_prolong(self):
a = self.d.get_var("a")
a.v()[:, :] = np.arange(self.g.nx*self.g.ny).reshape(self.g.nx, self.g.ny) + 1
f = self.d.prolong("a")
# prologation should be conservative, so compare the volume-weighted sums
assert 4.0*np.sum(a.v()) == np.sum(f.v())
def test_zero(self):
a = self.d.get_var("a")
a.v()[:, :] = np.arange(self.g.nx*self.g.ny).reshape(self.g.nx, self.g.ny) + 1
self.d.zero("a")
assert self.d.min("a") == 0.0 and self.d.max("a") == 0.0
def test_bcs():
myg = patch.Grid2d(4, 4, ng=2, xmax=1.0, ymax=1.0)
myd = patch.CellCenterData2d(myg, dtype=int)
bco = bnd.BC(xlb="outflow", xrb="outflow",
ylb="outflow", yrb="outflow")
myd.register_var("outflow", bco)
bcp = bnd.BC(xlb="periodic", xrb="periodic",
ylb="periodic", yrb="periodic")
myd.register_var("periodic", bcp)
bcre = bnd.BC(xlb="reflect-even", xrb="reflect-even",
ylb="reflect-even", yrb="reflect-even")
myd.register_var("reflect-even", bcre)
bcro = bnd.BC(xlb="reflect-odd", xrb="reflect-odd",
ylb="reflect-odd", yrb="reflect-odd")
myd.register_var("reflect-odd", bcro)
myd.create()
a = myd.get_var("outflow")
a.v()[:, :] = np.fromfunction(lambda i, j: i+10*j+1, (4, 4), dtype=int)
b = myd.get_var("periodic")
c = myd.get_var("reflect-even")
d = myd.get_var("reflect-odd")
b[:, :] = a[:, :]
c[:, :] = a[:, :]
d[:, :] = a[:, :]
myd.fill_BC("outflow")
# left ghost
assert_array_equal(a[myg.ilo-1, myg.jlo:myg.jhi+1], np.array([1, 11, 21, 31]))
# right ghost
assert_array_equal(a[myg.ihi+1, myg.jlo:myg.jhi+1], np.array([4, 14, 24, 34]))
# bottom ghost
assert_array_equal(a[myg.ilo:myg.ihi+1, myg.jlo-1], np.array([1, 2, 3, 4]))
# top ghost
assert_array_equal(a[myg.ilo:myg.ihi+1, myg.jhi+1], np.array([31, 32, 33, 34]))
myd.fill_BC("periodic")
# x-boundaries
assert_array_equal(b[myg.ilo-1, myg.jlo:myg.jhi+1],
b[myg.ihi, myg.jlo:myg.jhi+1])
assert_array_equal(b[myg.ilo, myg.jlo:myg.jhi+1],
b[myg.ihi+1, myg.jlo:myg.jhi+1])
# y-boundaries
assert_array_equal(b[myg.ilo:myg.ihi+1, myg.jlo-1],
b[myg.ilo:myg.ihi+1, myg.jhi])
assert_array_equal(b[myg.ilo:myg.ihi+1, myg.jlo],
b[myg.ilo:myg.ihi+1, myg.jhi+1])
myd.fill_BC("reflect-even")
# left -- we'll check 2 ghost cells here -- now we use flipud here
# because our 'x' is the row index
# left
assert_array_equal(c[myg.ilo:myg.ilo+2, myg.jlo:myg.ihi+1],
np.flipud(c[myg.ilo-2:myg.ilo, myg.jlo:myg.jhi+1]))
# right
assert_array_equal(c[myg.ihi-1:myg.ihi+1, myg.jlo:myg.jhi+1],
np.flipud(c[myg.ihi+1:myg.ihi+3, myg.jlo:myg.jhi+1]))
# bottom
assert_array_equal(c[myg.ilo:myg.ihi+1, myg.jlo:myg.jlo+2],
np.fliplr(c[myg.ilo:myg.ihi+1, myg.jlo-2:myg.jlo]))
# top
assert_array_equal(c[myg.ilo:myg.ihi+1, myg.jhi-1:myg.jhi+1],
np.fliplr(c[myg.ilo:myg.ihi+1, myg.jhi+1:myg.jhi+3]))
myd.fill_BC("reflect-odd")
# left -- we'll check 2 ghost cells here -- now we use flipud here
# because our 'x' is the row index
# left
assert_array_equal(d[myg.ilo:myg.ilo+2, myg.jlo:myg.ihi+1],
-np.flipud(d[myg.ilo-2:myg.ilo, myg.jlo:myg.jhi+1]))
# right
assert_array_equal(d[myg.ihi-1:myg.ihi+1, myg.jlo:myg.jhi+1],
-np.flipud(d[myg.ihi+1:myg.ihi+3, myg.jlo:myg.jhi+1]))
# bottom
assert_array_equal(d[myg.ilo:myg.ihi+1, myg.jlo:myg.jlo+2],
-np.fliplr(d[myg.ilo:myg.ihi+1, myg.jlo-2:myg.jlo]))
# top
assert_array_equal(d[myg.ilo:myg.ihi+1, myg.jhi-1:myg.jhi+1],
-np.fliplr(d[myg.ilo:myg.ihi+1, myg.jhi+1:myg.jhi+3]))
# PolarGrid tests
class TestPolarGrid(object):
@classmethod
def setup_class(cls):
""" this is run once for each class before any tests """
pass
@classmethod
def teardown_class(cls):
""" this is run once for each class after all tests """
pass
def setup_method(self):
""" this is run before each test """
self.g = patch.PolarGrid(4, 6, ng=2, ymax=1.5)
def teardown_method(self):
""" this is run after each test """
self.g = None
def test_dx_dy(self):
assert self.g.dx == 0.25
assert self.g.dy == 0.25
def test_grid_coords(self):
assert_array_equal(self.g.x[self.g.ilo:self.g.ihi+1],
np.array([0.125, 0.375, 0.625, 0.875]))
assert_array_equal(self.g.y[self.g.jlo:self.g.jhi+1],
np.array([0.125, 0.375, 0.625, 0.875, 1.125, 1.375]))
def test_grid_2d_coords(self):
assert_array_equal(self.g.x, self.g.x2d[:, self.g.jc])
assert_array_equal(self.g.y, self.g.y2d[self.g.ic, :])
def test_scratch_array(self):
q = self.g.scratch_array()
assert q.shape == (self.g.qx, self.g.qy)
def test_coarse_like(self):
q = self.g.coarse_like(2)
assert q.qx == 2*self.g.ng + self.g.nx//2
assert q.qy == 2*self.g.ng + self.g.ny//2
def test_fine_like(self):
q = self.g.fine_like(2)
assert q.qx == 2*self.g.ng + 2*self.g.nx
assert q.qy == 2*self.g.ng + 2*self.g.ny
def test_norm(self):
q = self.g.scratch_array()
# there are 24 elements, the norm L2 norm is
# sqrt(dx*dy*24)
q.v()[:, :] = np.array([[1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1]])
assert q.norm() == np.sqrt(24*self.g.dx*self.g.dy)
def test_equality(self):
g2 = patch.PolarGrid(2, 5, ng=1)
assert g2 != self.g
def test_area_x(self):
A = self.g.area_x()
assert A[0,0] == (self.g.yr[0] - self.g.yl[0]) * (self.g.xr[0] - self.g.xl[0]) * 0.5
def test_area_y(self):
A = self.g.area_y()
assert A[0] == (self.g.xr - self.g.xl)
def test_cell_volumes(self):
V = self.g.cell_volumes()
assert V[0,0] == (self.g.yr[0] - self.g.yl[0]) * (self.g.xr[0] ** 2 - self.g.xl[0] ** 2) * 0.5