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

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

ANUGA core modified to fix errors with new API modules not being found.

File size: 9.5 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        #Create basic mesh
132        points, vertices, boundary = rectangular(1, 3)
133
134        #Create shallow water domain
135        domain = Domain(points, vertices, boundary)
136        domain.build_tagged_elements_dictionary({'bottom':[0,1],
137                                                 'top':[4,5],
138                                                 'all':[0,1,2,3,4,5]})
139
140        #Set friction
141        manning = 0.07
142        domain.set_quantity('friction', manning)
143
144        a = Set_region('bottom', 'friction', 0.09, location = 'unique vertices')
145        domain.set_region(a)
146        assert num.allclose(domain.quantities['friction'].get_values(),
147                            [[ 0.09,  0.09,  0.09],
148                             [ 0.09,  0.09,  0.09],
149                             [ 0.09,  0.07,  0.09],
150                             [ 0.07,  0.09,  0.07],
151                             [ 0.07,  0.07,  0.07],
152                             [ 0.07,  0.07,  0.07]])
153
154
155    def test_unique_verticesII(self):
156        """
157        get values based on triangle lists.
158        """
159
160        #Create basic mesh
161        points, vertices, boundary = rectangular(1, 3)
162
163        #Create shallow water domain
164        domain = Domain(points, vertices, boundary)
165        domain.build_tagged_elements_dictionary({'bottom':[0,1],
166                                                 'top':[4,5],
167                                                 'all':[0,1,2,3,4,5]})
168
169        #Set friction
170        manning = 0.07
171        domain.set_quantity('friction', manning)
172
173        domain.set_region(Add_value_to_region('bottom', 'friction', 1.0,initial_quantity='friction', location = 'unique vertices'))
174
175        #print domain.quantities['friction'].get_values()
176        assert num.allclose(domain.quantities['friction'].get_values(),\
177                            [[ 1.07,  1.07,  1.07],
178                             [ 1.07,  1.07,  1.07],
179                             [ 1.07,  0.07,  1.07],
180                             [ 0.07,  1.07,  0.07],
181                             [ 0.07,  0.07,  0.07],
182                             [ 0.07,  0.07,  0.07]])
183                         
184    def test_unique_vertices_average_loc_vert(self):
185        """Get values based on triangle lists."""
186
187        #Create basic mesh
188        points, vertices, boundary = rectangular(1, 3)
189
190        #Create shallow water domain
191        domain = Domain(points, vertices, boundary)
192        domain.build_tagged_elements_dictionary({'bottom': [0, 1],
193                                                 'top': [4, 5],
194                                                 'not_bottom': [2, 3, 4, 5]})
195
196        #Set friction
197        domain.set_quantity('friction', add_x_y)
198        av_bottom = 2.0 / 3.0
199        add = 60.0
200        calc_frict = av_bottom + add
201        domain.set_region(Add_value_to_region('bottom', 'friction', add,
202                                              initial_quantity='friction',
203                                              location='vertices',
204                                              average=True))
205
206        frict_points = domain.quantities['friction'].get_values()
207
208        expected = [calc_frict, calc_frict, calc_frict]
209        msg = ('frict_points[0]=%s\nexpected=%s'
210               % (str(frict_points[0]), str(expected)))
211        assert num.allclose(frict_points[0], expected), msg
212
213        msg = ('frict_points[1]=%s\nexpected=%s'
214               % (str(frict_points[1]), str(expected)))
215        assert num.allclose(frict_points[1], expected), msg
216 
217    def test_unique_vertices_average_loc_unique_vert(self):
218        """
219        get values based on triangle lists.
220        """
221
222        #Create basic mesh
223        points, vertices, boundary = rectangular(1, 3)
224        #Create shallow water domain
225        domain = Domain(points, vertices, boundary)
226        domain.build_tagged_elements_dictionary({'bottom':[0,1],
227                                                 'top':[4,5],
228                                                 'not_bottom':[2,3,4,5]})
229
230        #Set friction
231        domain.set_quantity('friction', add_x_y)
232        av_bottom = 2.0/3.0
233        add = 60.0
234        calc_frict = av_bottom + add
235        domain.set_region(Add_value_to_region('bottom', 'friction', add,
236                          initial_quantity='friction',
237                           location='unique vertices',
238                           average=True
239                          ))
240
241        #print domain.quantities['friction'].get_values()
242        frict_points = domain.quantities['friction'].get_values()
243        assert num.allclose(frict_points[0],\
244                            [ calc_frict, calc_frict, calc_frict])
245        assert num.allclose(frict_points[1],\
246                            [ calc_frict, calc_frict, calc_frict])
247        assert num.allclose(frict_points[2],\
248                            [ calc_frict, 1.0 + 2.0/3.0, calc_frict])
249        assert num.allclose(frict_points[3],\
250                            [ 2.0/3.0,calc_frict, 1.0 + 2.0/3.0])
251                                               
252                         
253#-------------------------------------------------------------
254
255if __name__ == "__main__":
256    suite = unittest.makeSuite(Test_Region, 'test')   
257    runner = unittest.TextTestRunner()
258    runner.run(suite)
Note: See TracBrowser for help on using the repository browser.