1 | ## Automatically adapted for numpy.oldnumeric Oct 28, 2008 by alter_code1.py |
---|
2 | |
---|
3 | #!/usr/bin/env python |
---|
4 | |
---|
5 | import unittest |
---|
6 | from math import sqrt |
---|
7 | |
---|
8 | from domain import * |
---|
9 | from region import * |
---|
10 | #from anuga.config import epsilon |
---|
11 | from numpy.oldnumeric import allclose, average #, array, ones, Float |
---|
12 | """ |
---|
13 | This is what the mesh in these tests look like; |
---|
14 | |
---|
15 | 3---7 |
---|
16 | |5 /| |
---|
17 | | /6| |
---|
18 | 2---6 |
---|
19 | |3 /| |
---|
20 | | /2| |
---|
21 | 1---5 |
---|
22 | |1 /| |
---|
23 | | /0| |
---|
24 | 0---4 |
---|
25 | """ |
---|
26 | |
---|
27 | def add_x_y(x, y): |
---|
28 | return x+y |
---|
29 | |
---|
30 | def give_me_23(x, y): |
---|
31 | return 23.0 |
---|
32 | |
---|
33 | class Test_Region(unittest.TestCase): |
---|
34 | def setUp(self): |
---|
35 | pass |
---|
36 | |
---|
37 | |
---|
38 | def tearDown(self): |
---|
39 | pass |
---|
40 | |
---|
41 | |
---|
42 | def test_region_tags(self): |
---|
43 | """ |
---|
44 | get values based on triangle lists. |
---|
45 | """ |
---|
46 | from mesh_factory import rectangular |
---|
47 | from shallow_water import Domain |
---|
48 | from numpy.oldnumeric import zeros, Float |
---|
49 | |
---|
50 | #Create basic mesh |
---|
51 | points, vertices, boundary = rectangular(1, 3) |
---|
52 | |
---|
53 | #Create shallow water domain |
---|
54 | domain = Domain(points, vertices, boundary) |
---|
55 | domain.build_tagged_elements_dictionary({'bottom':[0,1], |
---|
56 | 'top':[4,5], |
---|
57 | 'all':[0,1,2,3,4,5]}) |
---|
58 | |
---|
59 | |
---|
60 | #Set friction |
---|
61 | manning = 0.07 |
---|
62 | domain.set_quantity('friction', manning) |
---|
63 | |
---|
64 | a = Set_region('bottom', 'friction', 0.09) |
---|
65 | b = Set_region('top', 'friction', 1.0) |
---|
66 | domain.set_region([a, b]) |
---|
67 | #print domain.quantities['friction'].get_values() |
---|
68 | assert allclose(domain.quantities['friction'].get_values(),\ |
---|
69 | [[ 0.09, 0.09, 0.09], |
---|
70 | [ 0.09, 0.09, 0.09], |
---|
71 | [ 0.07, 0.07, 0.07], |
---|
72 | [ 0.07, 0.07, 0.07], |
---|
73 | [ 1.0, 1.0, 1.0], |
---|
74 | [ 1.0, 1.0, 1.0]]) |
---|
75 | |
---|
76 | #c = Add_Value_To_region('all', 'friction', 10.0) |
---|
77 | domain.set_region(Add_value_to_region('all', 'friction', 10.0)) |
---|
78 | #print domain.quantities['friction'].get_values() |
---|
79 | assert allclose(domain.quantities['friction'].get_values(), |
---|
80 | [[ 10.09, 10.09, 10.09], |
---|
81 | [ 10.09, 10.09, 10.09], |
---|
82 | [ 10.07, 10.07, 10.07], |
---|
83 | [ 10.07, 10.07, 10.07], |
---|
84 | [ 11.0, 11.0, 11.0], |
---|
85 | [ 11.0, 11.0, 11.0]]) |
---|
86 | |
---|
87 | # trying a function |
---|
88 | domain.set_region(Set_region('top', 'friction', add_x_y)) |
---|
89 | #print domain.quantities['friction'].get_values() |
---|
90 | assert allclose(domain.quantities['friction'].get_values(), |
---|
91 | [[ 10.09, 10.09, 10.09], |
---|
92 | [ 10.09, 10.09, 10.09], |
---|
93 | [ 10.07, 10.07, 10.07], |
---|
94 | [ 10.07, 10.07, 10.07], |
---|
95 | [ 5./3, 2.0, 2./3], |
---|
96 | [ 1.0, 2./3, 2.0]]) |
---|
97 | |
---|
98 | domain.set_quantity('elevation', 10.0) |
---|
99 | domain.set_quantity('stage', 10.0) |
---|
100 | domain.set_region(Add_value_to_region('top', 'stage', 1.0,initial_quantity='elevation')) |
---|
101 | #print domain.quantities['stage'].get_values() |
---|
102 | assert allclose(domain.quantities['stage'].get_values(), |
---|
103 | [[ 10., 10., 10.], |
---|
104 | [ 10., 10., 10.], |
---|
105 | [ 10., 10., 10.], |
---|
106 | [ 10., 10., 10.], |
---|
107 | [ 11.0, 11.0, 11.0], |
---|
108 | [ 11.0, 11.0, 11.0]]) |
---|
109 | |
---|
110 | |
---|
111 | domain.set_quantity('elevation', 10.0) |
---|
112 | domain.set_quantity('stage', give_me_23) |
---|
113 | #this works as well, (is cleaner, but doesn't work for regions) |
---|
114 | #domain.set_quantity('stage', |
---|
115 | # domain.quantities['stage'].vertex_values+ \ |
---|
116 | # domain.quantities['elevation'].vertex_values) |
---|
117 | domain.set_region(Add_quantities('top', 'elevation','stage')) |
---|
118 | #print domain.quantities['stage'].get_values() |
---|
119 | assert allclose(domain.quantities['elevation'].get_values(), |
---|
120 | [[ 10., 10., 10.], |
---|
121 | [ 10., 10., 10.], |
---|
122 | [ 10., 10., 10.], |
---|
123 | [ 10., 10., 10.], |
---|
124 | [ 33., 33.0, 33.], |
---|
125 | [ 33.0, 33., 33.]]) |
---|
126 | |
---|
127 | def test_unique_vertices(self): |
---|
128 | """ |
---|
129 | get values based on triangle lists. |
---|
130 | """ |
---|
131 | from mesh_factory import rectangular |
---|
132 | from shallow_water import Domain |
---|
133 | from numpy.oldnumeric import zeros, Float |
---|
134 | |
---|
135 | #Create basic mesh |
---|
136 | points, vertices, boundary = rectangular(1, 3) |
---|
137 | |
---|
138 | #Create shallow water domain |
---|
139 | domain = Domain(points, vertices, boundary) |
---|
140 | domain.build_tagged_elements_dictionary({'bottom':[0,1], |
---|
141 | 'top':[4,5], |
---|
142 | 'all':[0,1,2,3,4,5]}) |
---|
143 | |
---|
144 | #Set friction |
---|
145 | manning = 0.07 |
---|
146 | domain.set_quantity('friction', manning) |
---|
147 | |
---|
148 | a = Set_region('bottom', 'friction', 0.09, location = 'unique vertices') |
---|
149 | domain.set_region(a) |
---|
150 | #print domain.quantities['friction'].get_values() |
---|
151 | assert allclose(domain.quantities['friction'].get_values(),\ |
---|
152 | [[ 0.09, 0.09, 0.09], |
---|
153 | [ 0.09, 0.09, 0.09], |
---|
154 | [ 0.09, 0.07, 0.09], |
---|
155 | [ 0.07, 0.09, 0.07], |
---|
156 | [ 0.07, 0.07, 0.07], |
---|
157 | [ 0.07, 0.07, 0.07]]) |
---|
158 | |
---|
159 | |
---|
160 | def test_unique_verticesII(self): |
---|
161 | """ |
---|
162 | get values based on triangle lists. |
---|
163 | """ |
---|
164 | from mesh_factory import rectangular |
---|
165 | from shallow_water import Domain |
---|
166 | from numpy.oldnumeric import zeros, Float |
---|
167 | |
---|
168 | #Create basic mesh |
---|
169 | points, vertices, boundary = rectangular(1, 3) |
---|
170 | |
---|
171 | #Create shallow water domain |
---|
172 | domain = Domain(points, vertices, boundary) |
---|
173 | domain.build_tagged_elements_dictionary({'bottom':[0,1], |
---|
174 | 'top':[4,5], |
---|
175 | 'all':[0,1,2,3,4,5]}) |
---|
176 | |
---|
177 | #Set friction |
---|
178 | manning = 0.07 |
---|
179 | domain.set_quantity('friction', manning) |
---|
180 | |
---|
181 | domain.set_region(Add_value_to_region('bottom', 'friction', 1.0,initial_quantity='friction', location = 'unique vertices')) |
---|
182 | |
---|
183 | #print domain.quantities['friction'].get_values() |
---|
184 | assert allclose(domain.quantities['friction'].get_values(),\ |
---|
185 | [[ 1.07, 1.07, 1.07], |
---|
186 | [ 1.07, 1.07, 1.07], |
---|
187 | [ 1.07, 0.07, 1.07], |
---|
188 | [ 0.07, 1.07, 0.07], |
---|
189 | [ 0.07, 0.07, 0.07], |
---|
190 | [ 0.07, 0.07, 0.07]]) |
---|
191 | |
---|
192 | def test_unique_vertices_average_loc_vert(self): |
---|
193 | """ |
---|
194 | get values based on triangle lists. |
---|
195 | """ |
---|
196 | from mesh_factory import rectangular |
---|
197 | from shallow_water import Domain |
---|
198 | from numpy.oldnumeric import zeros, Float |
---|
199 | |
---|
200 | #Create basic mesh |
---|
201 | points, vertices, boundary = rectangular(1, 3) |
---|
202 | #Create shallow water domain |
---|
203 | domain = Domain(points, vertices, boundary) |
---|
204 | domain.build_tagged_elements_dictionary({'bottom':[0,1], |
---|
205 | 'top':[4,5], |
---|
206 | 'not_bottom':[2,3,4,5]}) |
---|
207 | |
---|
208 | #Set friction |
---|
209 | domain.set_quantity('friction', add_x_y) |
---|
210 | av_bottom = 2.0/3.0 |
---|
211 | add = 60.0 |
---|
212 | calc_frict = av_bottom + add |
---|
213 | domain.set_region(Add_value_to_region('bottom', 'friction', add, |
---|
214 | initial_quantity='friction', |
---|
215 | #location='unique vertices', |
---|
216 | location='vertices', |
---|
217 | average=True |
---|
218 | )) |
---|
219 | |
---|
220 | #print domain.quantities['friction'].get_values() |
---|
221 | frict_points = domain.quantities['friction'].get_values() |
---|
222 | assert allclose(frict_points[0],\ |
---|
223 | [ calc_frict, calc_frict, calc_frict]) |
---|
224 | assert allclose(frict_points[1],\ |
---|
225 | [ calc_frict, calc_frict, calc_frict]) |
---|
226 | |
---|
227 | def test_unique_vertices_average_loc_unique_vert(self): |
---|
228 | """ |
---|
229 | get values based on triangle lists. |
---|
230 | """ |
---|
231 | from mesh_factory import rectangular |
---|
232 | from shallow_water import Domain |
---|
233 | from numpy.oldnumeric import zeros, Float |
---|
234 | |
---|
235 | #Create basic mesh |
---|
236 | points, vertices, boundary = rectangular(1, 3) |
---|
237 | #Create shallow water domain |
---|
238 | domain = Domain(points, vertices, boundary) |
---|
239 | domain.build_tagged_elements_dictionary({'bottom':[0,1], |
---|
240 | 'top':[4,5], |
---|
241 | 'not_bottom':[2,3,4,5]}) |
---|
242 | |
---|
243 | #Set friction |
---|
244 | domain.set_quantity('friction', add_x_y) |
---|
245 | av_bottom = 2.0/3.0 |
---|
246 | add = 60.0 |
---|
247 | calc_frict = av_bottom + add |
---|
248 | domain.set_region(Add_value_to_region('bottom', 'friction', add, |
---|
249 | initial_quantity='friction', |
---|
250 | location='unique vertices', |
---|
251 | average=True |
---|
252 | )) |
---|
253 | |
---|
254 | #print domain.quantities['friction'].get_values() |
---|
255 | frict_points = domain.quantities['friction'].get_values() |
---|
256 | assert allclose(frict_points[0],\ |
---|
257 | [ calc_frict, calc_frict, calc_frict]) |
---|
258 | assert allclose(frict_points[1],\ |
---|
259 | [ calc_frict, calc_frict, calc_frict]) |
---|
260 | assert allclose(frict_points[2],\ |
---|
261 | [ calc_frict, 1.0 + 2.0/3.0, calc_frict]) |
---|
262 | assert allclose(frict_points[3],\ |
---|
263 | [ 2.0/3.0,calc_frict, 1.0 + 2.0/3.0]) |
---|
264 | |
---|
265 | |
---|
266 | #------------------------------------------------------------- |
---|
267 | if __name__ == "__main__": |
---|
268 | ## suite = unittest.makeSuite(Test_Region,'test') |
---|
269 | suite = unittest.makeSuite(Test_Region,'test_unique_vertices_average_loc_unique_vert') |
---|
270 | runner = unittest.TextTestRunner() |
---|
271 | runner.run(suite) |
---|