source: trunk/anuga_core/source/anuga/abstract_2d_finite_volumes/test_region.py @ 7780

Last change on this file since 7780 was 7780, checked in by hudson, 14 years ago

Almost all failing tests fixed.

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