forked from python-hydro/pyro2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_patch.py
More file actions
164 lines (122 loc) · 5.47 KB
/
Copy pathtest_patch.py
File metadata and controls
164 lines (122 loc) · 5.47 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
# unit tests for the patch
import mesh.patch as patch
import mesh.array_indexer as ai
import numpy as np
from numpy.testing import assert_array_equal
from nose.tools import assert_equal
import util.testing_help as th
# utilities
@th.with_named_setup(th.setup_func, th.teardown_func)
def test_buf_split():
assert_array_equal(ai._buf_split(2), [2, 2, 2, 2])
assert_array_equal(ai._buf_split((2, 3)), [2, 3, 2, 3])
# Grid2d tests
@th.with_named_setup(th.setup_func, th.teardown_func)
def test_dx_dy():
g = patch.Grid2d(4, 6, ng=2, ymax=1.5)
assert_equal(g.dx, 0.25)
assert_equal(g.dy, 0.25)
@th.with_named_setup(th.setup_func, th.teardown_func)
def test_grid_coords():
g = patch.Grid2d(4, 6, ng=2, ymax=1.5)
assert_array_equal(g.x[g.ilo:g.ihi+1], np.array([0.125, 0.375, 0.625, 0.875]))
assert_array_equal(g.y[g.jlo:g.jhi+1], np.array([0.125, 0.375, 0.625, 0.875, 1.125, 1.375]))
@th.with_named_setup(th.setup_func, th.teardown_func)
def test_grid_2d_coords():
g = patch.Grid2d(4, 6, ng=2, ymax=1.5)
assert_array_equal(g.x, g.x2d[:,g.jc])
assert_array_equal(g.y, g.y2d[g.ic,:])
@th.with_named_setup(th.setup_func, th.teardown_func)
def test_course_like():
g = patch.Grid2d(4, 6, ng=2, ymax=1.5)
c = g.coarse_like(2)
assert_equal(c.nx, 2)
assert_equal(c.ny, 3)
# CellCenterData2d tests
@th.with_named_setup(th.setup_func, th.teardown_func)
def test_bcs():
myg = patch.Grid2d(4,4, ng = 2, xmax=1.0, ymax=1.0)
myd = patch.CellCenterData2d(myg, dtype=np.int)
bco = patch.BCObject(xlb="outflow", xrb="outflow",
ylb="outflow", yrb="outflow")
myd.register_var("outflow", bco)
bcp = patch.BCObject(xlb="periodic", xrb="periodic",
ylb="periodic", yrb="periodic")
myd.register_var("periodic", bcp)
bcre = patch.BCObject(xlb="reflect-even", xrb="reflect-even",
ylb="reflect-even", yrb="reflect-even")
myd.register_var("reflect-even", bcre)
bcro = patch.BCObject(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]))
# ArrayInderex tests
@th.with_named_setup(th.setup_func, th.teardown_func)
def test_indexer():
g = patch.Grid2d(2,3, ng=2)
a = g.scratch_array()
a[:,:] = np.arange(g.qx*g.qy).reshape(g.qx, g.qy)
assert_array_equal(a.v(), np.array([[16., 17., 18.], [23., 24., 25.]]))
assert_array_equal(a.ip(1), np.array([[23., 24., 25.], [30., 31., 32.]]))
assert_array_equal(a.ip(-1), np.array([[9., 10., 11.], [16., 17., 18.]]))
assert_array_equal(a.jp(1), np.array([[17., 18., 19.], [24., 25., 26.]]))
assert_array_equal(a.jp(-1), np.array([[15., 16., 17.], [ 22., 23., 24.]]))
assert_array_equal(a.ip_jp(1, 1), np.array([[24., 25., 26.], [ 31., 32., 33.]]))