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