source: inundation/ga/storm_surge/pyvolution/test_domain.py @ 240

Last change on this file since 240 was 215, checked in by ole, 21 years ago

More testing of gravity, forcing terms, distributors

File size: 9.1 KB
Line 
1#!/usr/bin/env python
2
3import unittest
4from math import sqrt
5
6from domain import *
7from config import epsilon
8from Numeric import allclose, array
9from util import distance
10
11class TestCase(unittest.TestCase):
12    def setUp(self):
13        pass
14
15       
16    def tearDown(self):
17        pass
18
19           
20    def test_simple(self):
21        a = [0.0, 0.0]
22        b = [0.0, 2.0]
23        c = [2.0,0.0]
24        d = [0.0, 4.0]
25        e = [2.0, 2.0]
26        f = [4.0,0.0]
27
28        points = [a, b, c, d, e, f]
29        #bac, bce, ecf, dbe, daf, dae
30        vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4], [3,0,5], [3,0,4]]
31
32        conserved_quantities = ['level', 'xmomentum', 'ymomentum']
33        other_quantities = ['elevation', 'friction']
34       
35        domain = Domain(points, vertices, None,
36                        conserved_quantities, other_quantities)       
37        domain.check_integrity()
38
39        for name in conserved_quantities + other_quantities:
40            assert domain.quantities.has_key(name)
41
42
43        assert domain.get_conserved_quantities(0, edge=1) == 0.   
44
45
46    def test_conserved_quantities(self):
47       
48        a = [0.0, 0.0]
49        b = [0.0, 2.0]
50        c = [2.0,0.0]
51        d = [0.0, 4.0]
52        e = [2.0, 2.0]
53        f = [4.0,0.0]
54
55        points = [a, b, c, d, e, f]
56        #bac, bce, ecf, dbe, daf, dae
57        vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4], [3,0,5], [3,0,4]]
58
59        domain = Domain(points, vertices, boundary=None,
60                        conserved_quantities =\
61                        ['level', 'xmomentum', 'ymomentum'])
62
63
64        domain.set_quantity('level', [[1,2,3], [5,5,5],
65                                      [0,0,9], [-6, 3, 3],
66                                      [0,0,0], [0,0,0]])       
67
68        domain.set_quantity('xmomentum', [[1,2,3], [5,5,5],
69                                          [0,0,9], [-6, 3, 3],
70                                          [0,0,0], [0,0,0]])       
71
72        domain.check_integrity()
73
74        #Centroids
75        q = domain.get_conserved_quantities(0)
76        assert allclose(q, [2., 2., 0.])
77
78        q = domain.get_conserved_quantities(1)
79        assert allclose(q, [5., 5., 0.])         
80
81        q = domain.get_conserved_quantities(2)
82        assert allclose(q, [3., 3., 0.])
83
84        q = domain.get_conserved_quantities(3)
85        assert allclose(q, [0., 0., 0.])                         
86       
87
88        #Edges
89        q = domain.get_conserved_quantities(0, edge=0)
90        assert allclose(q, [2.5, 2.5, 0.])
91        q = domain.get_conserved_quantities(0, edge=1)
92        assert allclose(q, [2., 2., 0.])
93        q = domain.get_conserved_quantities(0, edge=2)
94        assert allclose(q, [1.5, 1.5, 0.])
95
96        for i in range(3):
97            q = domain.get_conserved_quantities(1, edge=i)
98            assert allclose(q, [5, 5, 0.])
99
100
101        q = domain.get_conserved_quantities(2, edge=0)
102        assert allclose(q, [4.5, 4.5, 0.])
103        q = domain.get_conserved_quantities(2, edge=1)
104        assert allclose(q, [4.5, 4.5, 0.])
105        q = domain.get_conserved_quantities(2, edge=2)
106        assert allclose(q, [0., 0., 0.])
107
108
109        q = domain.get_conserved_quantities(3, edge=0)
110        assert allclose(q, [3., 3., 0.])
111        q = domain.get_conserved_quantities(3, edge=1)
112        assert allclose(q, [-1.5, -1.5, 0.])
113        q = domain.get_conserved_quantities(3, edge=2)
114        assert allclose(q, [-1.5, -1.5, 0.])                   
115
116
117
118    def test_boundary_conditions(self):
119       
120        a = [0.0, 0.0]
121        b = [0.0, 2.0]
122        c = [2.0,0.0]
123        d = [0.0, 4.0]
124        e = [2.0, 2.0]
125        f = [4.0,0.0]
126
127        points = [a, b, c, d, e, f]
128        #bac, bce, ecf, dbe
129        vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ]
130        boundary = { (0, 0): 'First',
131                     (0, 2): 'First',
132                     (2, 0): 'Second',
133                     (2, 1): 'Second',
134                     (3, 1): 'Second',
135                     (3, 2): 'Second'}                                         
136                     
137
138        domain = Domain(points, vertices, boundary,
139                        conserved_quantities =\
140                        ['level', 'xmomentum', 'ymomentum'])       
141        domain.check_integrity()
142
143
144
145        domain.set_quantity('level', [[1,2,3], [5,5,5],
146                                      [0,0,9], [-6, 3, 3]])
147
148
149        domain.set_boundary( {'First': Dirichlet_boundary([5,2,1]),
150                              'Second': Transmissive_boundary(domain)} )
151
152        domain.update_boundary()
153
154        assert domain.quantities['level'].boundary_values[0] == 5. #Dirichlet
155        assert domain.quantities['level'].boundary_values[1] == 5. #Dirichlet
156        assert domain.quantities['level'].boundary_values[2] ==\
157               domain.get_conserved_quantities(2, edge=0)[0] #Transmissive (4.5)
158        assert domain.quantities['level'].boundary_values[3] ==\
159               domain.get_conserved_quantities(2, edge=1)[0] #Transmissive (4.5)
160        assert domain.quantities['level'].boundary_values[4] ==\
161               domain.get_conserved_quantities(3, edge=1)[0] #Transmissive (-1.5)
162        assert domain.quantities['level'].boundary_values[5] ==\
163               domain.get_conserved_quantities(3, edge=2)[0] #Transmissive (-1.5)         
164
165
166
167    def test_distribute_first_order(self):
168        """Domain implements a default first order gradient limiter
169        """
170       
171        a = [0.0, 0.0]
172        b = [0.0, 2.0]
173        c = [2.0,0.0]
174        d = [0.0, 4.0]
175        e = [2.0, 2.0]
176        f = [4.0,0.0]
177
178        points = [a, b, c, d, e, f]
179        #bac, bce, ecf, dbe
180        vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ]
181        boundary = { (0, 0): 'Third',
182                     (0, 2): 'First',
183                     (2, 0): 'Second',
184                     (2, 1): 'Second',
185                     (3, 1): 'Second',
186                     (3, 2): 'Third'}                                         
187                     
188
189        domain = Domain(points, vertices, boundary,
190                        conserved_quantities =\
191                        ['level', 'xmomentum', 'ymomentum'])               
192        domain.check_integrity()
193
194
195        domain.set_quantity('level', [[1,2,3], [5,5,5],
196                                      [0,0,9], [-6, 3, 3]])
197
198        assert allclose( domain.quantities['level'].centroid_values,
199                         [2,5,3,0] )
200                         
201        domain.set_quantity('xmomentum', [[1,1,1], [2,2,2],
202                                          [3,3,3], [4, 4, 4]])
203
204        domain.set_quantity('ymomentum', [[10,10,10], [20,20,20],
205                                          [30,30,30], [40, 40, 40]])       
206
207
208        domain.distribute_to_vertices_and_edges()
209
210        #First order extrapolation
211        assert allclose( domain.quantities['level'].vertex_values,
212                         [[ 2.,  2.,  2.],
213                          [ 5.,  5.,  5.],
214                          [ 3.,  3.,  3.],
215                          [ 0.,  0.,  0.]])
216
217
218
219
220    def test_update_conserved_quantities(self):
221        a = [0.0, 0.0]
222        b = [0.0, 2.0]
223        c = [2.0,0.0]
224        d = [0.0, 4.0]
225        e = [2.0, 2.0]
226        f = [4.0,0.0]
227
228        points = [a, b, c, d, e, f]
229        #bac, bce, ecf, dbe
230        vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ]
231        boundary = { (0, 0): 'Third',
232                     (0, 2): 'First',
233                     (2, 0): 'Second',
234                     (2, 1): 'Second',
235                     (3, 1): 'Second',
236                     (3, 2): 'Third'}                                         
237                     
238
239        domain = Domain(points, vertices, boundary,
240                        conserved_quantities =\
241                        ['level', 'xmomentum', 'ymomentum'])               
242        domain.check_integrity()
243
244
245        domain.set_quantity('level', [1,2,3,4], 'centroids')
246        domain.set_quantity('xmomentum', [1,2,3,4], 'centroids')
247        domain.set_quantity('ymomentum', [1,2,3,4], 'centroids')
248                                         
249
250        #Assign some values to update vectors
251        #Set explicit_update
252
253        for name in domain.conserved_quantities:
254            domain.quantities[name].explicit_update = array([4.,3.,2.,1.])
255            domain.quantities[name].semi_implicit_update = array([1.,1.,1.,1.])
256           
257
258        #Update with given timestep (assuming no other forcing terms)
259        domain.timestep = 0.1
260        domain.update_conserved_quantities()
261
262        x = array([1, 2, 3, 4]) + array( [.4,.3,.2,.1] )
263        x /= array( [.9,.9,.9,.9] )
264
265        for name in domain.conserved_quantities:
266            assert allclose(domain.quantities[name].centroid_values, x)       
267
268       
269       
270#-------------------------------------------------------------
271if __name__ == "__main__":
272    suite = unittest.makeSuite(TestCase,'test')
273    runner = unittest.TextTestRunner()
274    runner.run(suite)
Note: See TracBrowser for help on using the repository browser.