source: anuga_core/source/anuga/abstract_2d_finite_volumes/test_region.py @ 5571

Last change on this file since 5571 was 5211, checked in by duncan, 16 years ago

typo

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