source: branches/numpy/anuga/abstract_2d_finite_volumes/test_region.py @ 6883

Last change on this file since 6883 was 6689, checked in by rwilson, 16 years ago

Back-merge from Numeric trunk to numpy branch.

File size: 9.8 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
9
10import numpy as num
11
12
13"""
14This is what the mesh in these tests look like;
15
163---7
17|5 /|
18| /6|
192---6
20|3 /|
21| /2|
221---5
23|1 /|
24| /0|
250---4
26"""
27
28def add_x_y(x, y):
29    return x+y
30
31def give_me_23(x, y):
32    return 23.0
33
34class Test_Region(unittest.TestCase):
35    def setUp(self):
36        pass
37
38
39    def tearDown(self):
40        pass
41
42
43    def test_region_tags(self):
44        """get values based on triangle lists."""
45
46        from mesh_factory import rectangular
47        from shallow_water import Domain
48
49        #Create basic mesh
50        points, vertices, boundary = rectangular(1, 3)
51
52        #Create shallow water domain
53        domain = Domain(points, vertices, boundary)
54        domain.build_tagged_elements_dictionary({'bottom': [0,1],
55                                                 'top': [4,5],
56                                                 'all': [0,1,2,3,4,5]})
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
66        expected = [[ 0.09,  0.09,  0.09],
67                    [ 0.09,  0.09,  0.09],
68                    [ 0.07,  0.07,  0.07],
69                    [ 0.07,  0.07,  0.07],
70                    [ 1.0,   1.0,   1.0],
71                    [ 1.0,   1.0,   1.0]]
72        msg = ("\ndomain.quantities['friction']=%s\nexpected value=%s"
73               % (str(domain.quantities['friction'].get_values()),
74                  str(expected)))
75        assert num.allclose(domain.quantities['friction'].get_values(),
76                            expected), msg
77
78        #c = Add_Value_To_region('all', 'friction', 10.0)
79        domain.set_region(Add_value_to_region('all', 'friction', 10.0))
80        #print domain.quantities['friction'].get_values()
81        assert num.allclose(domain.quantities['friction'].get_values(),
82                            [[ 10.09, 10.09, 10.09],
83                             [ 10.09, 10.09, 10.09],
84                             [ 10.07, 10.07, 10.07],
85                             [ 10.07, 10.07, 10.07],
86                             [ 11.0,  11.0,  11.0],
87                             [ 11.0,  11.0,  11.0]])
88
89        # trying a function
90        domain.set_region(Set_region('top', 'friction', add_x_y))
91        #print domain.quantities['friction'].get_values()
92        assert num.allclose(domain.quantities['friction'].get_values(),
93                            [[ 10.09, 10.09, 10.09],
94                             [ 10.09, 10.09, 10.09],
95                             [ 10.07, 10.07, 10.07],
96                             [ 10.07, 10.07, 10.07],
97                             [ 5./3,  2.0,  2./3],
98                             [ 1.0,  2./3,  2.0]])
99
100        domain.set_quantity('elevation', 10.0)
101        domain.set_quantity('stage', 10.0)
102        domain.set_region(Add_value_to_region('top', 'stage', 1.0,initial_quantity='elevation'))
103        #print domain.quantities['stage'].get_values()
104        assert num.allclose(domain.quantities['stage'].get_values(),
105                            [[ 10., 10., 10.],
106                             [ 10., 10., 10.],
107                             [ 10., 10., 10.],
108                             [ 10., 10., 10.],
109                             [ 11.0,  11.0,  11.0],
110                             [ 11.0,  11.0,  11.0]])
111
112       
113        domain.set_quantity('elevation', 10.0)
114        domain.set_quantity('stage', give_me_23)
115        #this works as well, (is cleaner, but doesn't work for regions)
116        #domain.set_quantity('stage',
117        #                    domain.quantities['stage'].vertex_values+ \
118        #                    domain.quantities['elevation'].vertex_values)
119        domain.set_region(Add_quantities('top', 'elevation','stage'))
120        #print domain.quantities['stage'].get_values()
121        assert num.allclose(domain.quantities['elevation'].get_values(),
122                            [[ 10., 10., 10.],
123                             [ 10., 10., 10.],
124                             [ 10., 10., 10.],
125                             [ 10., 10., 10.],
126                             [ 33.,  33.0,  33.],
127                             [ 33.0,  33.,  33.]])
128       
129    def test_unique_vertices(self):
130        """get values based on triangle lists."""
131
132        from mesh_factory import rectangular
133        from shallow_water import Domain
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        assert num.allclose(domain.quantities['friction'].get_values(),
151                            [[ 0.09,  0.09,  0.09],
152                             [ 0.09,  0.09,  0.09],
153                             [ 0.09,  0.07,  0.09],
154                             [ 0.07,  0.09,  0.07],
155                             [ 0.07,  0.07,  0.07],
156                             [ 0.07,  0.07,  0.07]])
157
158
159    def test_unique_verticesII(self):
160        """
161        get values based on triangle lists.
162        """
163        from mesh_factory import rectangular
164        from shallow_water import Domain
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 num.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        """Get values based on triangle lists."""
192
193        from mesh_factory import rectangular
194        from shallow_water import Domain
195
196        #Create basic mesh
197        points, vertices, boundary = rectangular(1, 3)
198
199        #Create shallow water domain
200        domain = Domain(points, vertices, boundary)
201        domain.build_tagged_elements_dictionary({'bottom': [0, 1],
202                                                 'top': [4, 5],
203                                                 'not_bottom': [2, 3, 4, 5]})
204
205        #Set friction
206        domain.set_quantity('friction', add_x_y)
207        av_bottom = 2.0 / 3.0
208        add = 60.0
209        calc_frict = av_bottom + add
210        domain.set_region(Add_value_to_region('bottom', 'friction', add,
211                                              initial_quantity='friction',
212                                              location='vertices',
213                                              average=True))
214
215        frict_points = domain.quantities['friction'].get_values()
216
217        expected = [calc_frict, calc_frict, calc_frict]
218        msg = ('frict_points[0]=%s\nexpected=%s'
219               % (str(frict_points[0]), str(expected)))
220        assert num.allclose(frict_points[0], expected), msg
221
222        msg = ('frict_points[1]=%s\nexpected=%s'
223               % (str(frict_points[1]), str(expected)))
224        assert num.allclose(frict_points[1], expected), msg
225 
226    def test_unique_vertices_average_loc_unique_vert(self):
227        """
228        get values based on triangle lists.
229        """
230        from mesh_factory import rectangular
231        from shallow_water import Domain
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 num.allclose(frict_points[0],\
255                            [ calc_frict, calc_frict, calc_frict])
256        assert num.allclose(frict_points[1],\
257                            [ calc_frict, calc_frict, calc_frict])
258        assert num.allclose(frict_points[2],\
259                            [ calc_frict, 1.0 + 2.0/3.0, calc_frict])
260        assert num.allclose(frict_points[3],\
261                            [ 2.0/3.0,calc_frict, 1.0 + 2.0/3.0])
262                                               
263                         
264#-------------------------------------------------------------
265
266if __name__ == "__main__":
267    suite = unittest.makeSuite(Test_Region, 'test')   
268    runner = unittest.TextTestRunner()
269    runner.run(suite)
Note: See TracBrowser for help on using the repository browser.