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