1 | import quantity |
---|
2 | #!/usr/bin/env python |
---|
3 | |
---|
4 | import unittest |
---|
5 | from math import sqrt, pi |
---|
6 | |
---|
7 | |
---|
8 | from quantity import * |
---|
9 | from domain import * |
---|
10 | |
---|
11 | |
---|
12 | from Numeric import allclose, array, ones, Float, maximum, zeros |
---|
13 | |
---|
14 | |
---|
15 | class Test_Quantity(unittest.TestCase): |
---|
16 | def setUp(self): |
---|
17 | self.points3 = [0.0, 1.0, 2.0, 3.0] |
---|
18 | self.vertex_values3 = [[1.0,2.0],[4.0,5.0],[-1.0,2.0]] |
---|
19 | self.domain3 = Domain(self.points3) |
---|
20 | |
---|
21 | |
---|
22 | |
---|
23 | self.points4 = [0.0, 1.0, 2.5, 3.0, 5.0] |
---|
24 | self.vertex_values4 = [[1.0,2.0],[4.0,5.0],[-1.0,2.0],[3.0,6.0]] |
---|
25 | self.centroid_values4 = [1.5, 4.5, 0.5, 4.5] |
---|
26 | self.boundary4 = {(0, 0): 'left', (3, 1): 'right'} |
---|
27 | self.domain4 = Domain(self.points4,self.boundary4) |
---|
28 | |
---|
29 | self.points10 = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0] |
---|
30 | self.domain10 = Domain(self.points10) |
---|
31 | |
---|
32 | self.points6 = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0] |
---|
33 | self.domain6 = Domain(self.points6) |
---|
34 | |
---|
35 | |
---|
36 | def tearDown(self): |
---|
37 | pass |
---|
38 | #print " Tearing down" |
---|
39 | |
---|
40 | |
---|
41 | def test_creat_with_boundary(self): |
---|
42 | |
---|
43 | assert self.domain4.boundary == {(0, 0): 'left', (3, 1): 'right'} |
---|
44 | |
---|
45 | def test_creation(self): |
---|
46 | |
---|
47 | quantity = Quantity(self.domain3) |
---|
48 | assert allclose(quantity.vertex_values, [[0.0,0.0],[0.0,0.0],[0.0,0.0]]) |
---|
49 | |
---|
50 | |
---|
51 | try: |
---|
52 | quantity = Quantity() |
---|
53 | except: |
---|
54 | pass |
---|
55 | else: |
---|
56 | raise 'Should have raised empty quantity exception' |
---|
57 | |
---|
58 | |
---|
59 | try: |
---|
60 | quantity = Quantity([1,2,3]) |
---|
61 | except AssertionError: |
---|
62 | pass |
---|
63 | except: |
---|
64 | raise 'Should have raised "mising domain object" error' |
---|
65 | |
---|
66 | |
---|
67 | def test_creation_zeros(self): |
---|
68 | |
---|
69 | quantity = Quantity(self.domain3) |
---|
70 | assert allclose(quantity.centroid_values, [[0.,0.,0.]]) |
---|
71 | |
---|
72 | |
---|
73 | quantity = Quantity(self.domain4) |
---|
74 | assert allclose(quantity.vertex_values, [[0.,0.], [0.,0.], |
---|
75 | [0.,0.], [0.,0.]]) |
---|
76 | |
---|
77 | |
---|
78 | def test_interpolation(self): |
---|
79 | quantity = Quantity(self.domain4, self.vertex_values4) |
---|
80 | assert allclose(quantity.centroid_values, self.centroid_values4) #Centroid |
---|
81 | |
---|
82 | |
---|
83 | |
---|
84 | def test_interpolation2(self): |
---|
85 | quantity = Quantity(self.domain4, self.vertex_values4) |
---|
86 | assert allclose(quantity.centroid_values, self.centroid_values4) #Centroid |
---|
87 | |
---|
88 | quantity.extrapolate_second_order() |
---|
89 | |
---|
90 | #print quantity.vertex_values |
---|
91 | assert allclose(quantity.vertex_values,[[ 0.3, 2.7], |
---|
92 | [ 4.5, 4.5], |
---|
93 | [ 0.5, 0.5], |
---|
94 | [ 1.3, 7.7]]) |
---|
95 | |
---|
96 | |
---|
97 | |
---|
98 | |
---|
99 | def test_boundary_allocation(self): |
---|
100 | quantity = Quantity(self.domain4, |
---|
101 | [[1,2], [5,5], [0,9], [-6, 3]]) |
---|
102 | |
---|
103 | |
---|
104 | assert quantity.boundary_values.shape[0] == len(self.domain4.boundary) |
---|
105 | |
---|
106 | |
---|
107 | def test_set_values(self): |
---|
108 | quantity = Quantity(self.domain4) |
---|
109 | |
---|
110 | # Test vertices |
---|
111 | quantity.set_values([[1,2], [5,5], [0,9], [-6, 3]], location = 'vertices') |
---|
112 | assert allclose(quantity.vertex_values, |
---|
113 | [[1,2], [5,5], [0,9], [-6, 3]]) |
---|
114 | assert allclose(quantity.centroid_values, [1.5, 5., 4.5, -1.5]) #Centroid |
---|
115 | |
---|
116 | |
---|
117 | |
---|
118 | # Test unique_vertices |
---|
119 | quantity.set_values([1,2,4,-5,6], location='unique_vertices') |
---|
120 | assert allclose(quantity.vertex_values, |
---|
121 | [[1,2], [2,4], [4,-5], [-5,6]]) |
---|
122 | assert allclose(quantity.centroid_values, [1.5, 3., -.5, .5]) #Centroid |
---|
123 | |
---|
124 | |
---|
125 | # Test centroids |
---|
126 | quantity.set_values([1,2,3,4], location = 'centroids') |
---|
127 | assert allclose(quantity.centroid_values, [1., 2., 3., 4.]) #Centroid |
---|
128 | |
---|
129 | # Test exceptions |
---|
130 | try: |
---|
131 | quantity.set_values([[1,3], [5,5], [0,9], [-6, 3]], |
---|
132 | location = 'bas kamel tuba') |
---|
133 | except: |
---|
134 | pass |
---|
135 | |
---|
136 | |
---|
137 | try: |
---|
138 | quantity.set_values([[1,3], [0,9]]) |
---|
139 | except AssertionError: |
---|
140 | pass |
---|
141 | except: |
---|
142 | raise 'should have raised Assertionerror' |
---|
143 | |
---|
144 | |
---|
145 | |
---|
146 | def test_set_values_const(self): |
---|
147 | quantity = Quantity(self.domain4) |
---|
148 | |
---|
149 | quantity.set_values(1.0, location = 'vertices') |
---|
150 | |
---|
151 | assert allclose(quantity.vertex_values, [[1,1], [1,1], [1,1], [1, 1]]) |
---|
152 | assert allclose(quantity.centroid_values, [1, 1, 1, 1]) #Centroid |
---|
153 | |
---|
154 | |
---|
155 | quantity.set_values(2.0, location = 'centroids') |
---|
156 | |
---|
157 | assert allclose(quantity.centroid_values, [2, 2, 2, 2]) |
---|
158 | |
---|
159 | |
---|
160 | def test_set_values_func(self): |
---|
161 | quantity = Quantity(self.domain4) |
---|
162 | |
---|
163 | def f(x): |
---|
164 | return x*x |
---|
165 | |
---|
166 | |
---|
167 | |
---|
168 | quantity.set_values(f, location = 'vertices') |
---|
169 | |
---|
170 | |
---|
171 | assert allclose(quantity.vertex_values, |
---|
172 | [[0,1], [1,6.25], [6.25,9], [9,25]]) |
---|
173 | |
---|
174 | assert allclose(quantity.centroid_values, |
---|
175 | [0.5, 3.625, 7.625, 34*0.5]) |
---|
176 | |
---|
177 | |
---|
178 | quantity.set_values(f, location = 'centroids') |
---|
179 | |
---|
180 | |
---|
181 | assert allclose(quantity.centroid_values, |
---|
182 | [0.25, 3.0625, 7.5625, 16.0]) |
---|
183 | |
---|
184 | |
---|
185 | def test_integral(self): |
---|
186 | quantity = Quantity(self.domain4) |
---|
187 | |
---|
188 | #Try constants first |
---|
189 | const = 5 |
---|
190 | quantity.set_values(const, location = 'vertices') |
---|
191 | #print 'Q', quantity.get_integral() |
---|
192 | |
---|
193 | assert allclose(quantity.get_integral(), self.domain4.get_area() * const) |
---|
194 | |
---|
195 | #Try with a linear function |
---|
196 | def f(x): |
---|
197 | return x |
---|
198 | |
---|
199 | quantity.set_values(f, location = 'vertices') |
---|
200 | |
---|
201 | |
---|
202 | assert allclose (quantity.centroid_values, |
---|
203 | [ 0.5, 1.75, 2.75, 4. ]) |
---|
204 | |
---|
205 | assert allclose (quantity.vertex_values, [[ 0., 1. ], |
---|
206 | [ 1., 2.5], |
---|
207 | [ 2.5, 3. ], |
---|
208 | [ 3., 5. ]]) |
---|
209 | |
---|
210 | |
---|
211 | ref_integral = 0.5 + 1.5*1.75 + 0.5*2.75 + 2.0*4.0 |
---|
212 | |
---|
213 | assert allclose (quantity.get_integral(), ref_integral) |
---|
214 | |
---|
215 | |
---|
216 | |
---|
217 | |
---|
218 | |
---|
219 | def test_set_values_from_quantity(self): |
---|
220 | |
---|
221 | quantity1 = Quantity(self.domain4) |
---|
222 | quantity1.set_values([0,1,2,3,4], location='unique_vertices') |
---|
223 | |
---|
224 | assert allclose(quantity1.vertex_values, |
---|
225 | [[0,1], [1,2], [2,3], [3,4]]) |
---|
226 | |
---|
227 | |
---|
228 | quantity2 = Quantity(self.domain4) |
---|
229 | quantity2.set_values(quantity1) |
---|
230 | assert allclose(quantity2.vertex_values, |
---|
231 | [[0,1], [1,2], [2,3], [3,4]]) |
---|
232 | |
---|
233 | quantity2.set_values(2*quantity1) |
---|
234 | |
---|
235 | print quantity2.vertex_values |
---|
236 | assert allclose(quantity2.vertex_values, |
---|
237 | [[0,2], [2,4], [4,6], [6,8]]) |
---|
238 | |
---|
239 | quantity2.set_values(2*quantity1 + 3) |
---|
240 | assert allclose(quantity2.vertex_values, |
---|
241 | [[3,5], [5,7], [7,9], [9,11]]) |
---|
242 | |
---|
243 | |
---|
244 | |
---|
245 | def test_overloading(self): |
---|
246 | |
---|
247 | quantity1 = Quantity(self.domain4) |
---|
248 | quantity1.set_values( [[0,1],[1,2],[2,3],[3,4]], |
---|
249 | location = 'vertices') |
---|
250 | |
---|
251 | assert allclose(quantity1.vertex_values, |
---|
252 | [[0,1], [1,2], [2,3], [3,4]]) |
---|
253 | |
---|
254 | |
---|
255 | quantity2 = Quantity(self.domain4) |
---|
256 | quantity2.set_values([[1,2], [5,5], [0,9], [-6, 3]], |
---|
257 | location = 'vertices') |
---|
258 | |
---|
259 | |
---|
260 | |
---|
261 | quantity3 = Quantity(self.domain4) |
---|
262 | quantity3.set_values([[2,2], [7,8], [7,6], [3, -8]], |
---|
263 | location = 'vertices') |
---|
264 | |
---|
265 | |
---|
266 | # Negation |
---|
267 | Q = -quantity1 |
---|
268 | assert allclose(Q.vertex_values, -quantity1.vertex_values) |
---|
269 | assert allclose(Q.centroid_values, -quantity1.centroid_values) |
---|
270 | |
---|
271 | |
---|
272 | # Addition |
---|
273 | Q = quantity1 + 7 |
---|
274 | assert allclose(Q.vertex_values, quantity1.vertex_values + 7) |
---|
275 | assert allclose(Q.centroid_values, quantity1.centroid_values + 7) |
---|
276 | |
---|
277 | Q = 7 + quantity1 |
---|
278 | assert allclose(Q.vertex_values, quantity1.vertex_values + 7) |
---|
279 | assert allclose(Q.centroid_values, quantity1.centroid_values + 7) |
---|
280 | |
---|
281 | Q = quantity1 + quantity2 |
---|
282 | assert allclose(Q.vertex_values, |
---|
283 | quantity1.vertex_values + quantity2.vertex_values) |
---|
284 | assert allclose(Q.centroid_values, |
---|
285 | quantity1.centroid_values + quantity2.centroid_values) |
---|
286 | |
---|
287 | Q = quantity1 + quantity2 - 3 |
---|
288 | assert allclose(Q.vertex_values, |
---|
289 | quantity1.vertex_values + quantity2.vertex_values - 3) |
---|
290 | |
---|
291 | Q = quantity1 - quantity2 |
---|
292 | assert allclose(Q.vertex_values, |
---|
293 | quantity1.vertex_values - quantity2.vertex_values) |
---|
294 | |
---|
295 | #Scaling |
---|
296 | Q = quantity1*3 |
---|
297 | assert allclose(Q.vertex_values, quantity1.vertex_values*3) |
---|
298 | assert allclose(Q.centroid_values, quantity1.centroid_values*3) |
---|
299 | |
---|
300 | Q = 3*quantity1 |
---|
301 | assert allclose(Q.vertex_values, quantity1.vertex_values*3) |
---|
302 | |
---|
303 | #Multiplication |
---|
304 | Q = quantity1 * quantity2 |
---|
305 | assert allclose(Q.vertex_values, |
---|
306 | quantity1.vertex_values * quantity2.vertex_values) |
---|
307 | |
---|
308 | #Linear combinations |
---|
309 | Q = 4*quantity1 + 2 |
---|
310 | assert allclose(Q.vertex_values, |
---|
311 | 4*quantity1.vertex_values + 2) |
---|
312 | |
---|
313 | Q = quantity1*quantity2 + 2 |
---|
314 | assert allclose(Q.vertex_values, |
---|
315 | quantity1.vertex_values * quantity2.vertex_values + 2) |
---|
316 | |
---|
317 | Q = quantity1*quantity2 + quantity3 |
---|
318 | assert allclose(Q.vertex_values, |
---|
319 | quantity1.vertex_values * quantity2.vertex_values + |
---|
320 | quantity3.vertex_values) |
---|
321 | Q = quantity1*quantity2 + 3*quantity3 |
---|
322 | assert allclose(Q.vertex_values, |
---|
323 | quantity1.vertex_values * quantity2.vertex_values + |
---|
324 | 3*quantity3.vertex_values) |
---|
325 | Q = quantity1*quantity2 + 3*quantity3 + 5.0 |
---|
326 | assert allclose(Q.vertex_values, |
---|
327 | quantity1.vertex_values * quantity2.vertex_values + |
---|
328 | 3*quantity3.vertex_values + 5) |
---|
329 | |
---|
330 | Q = quantity1*quantity2 - quantity3 |
---|
331 | assert allclose(Q.vertex_values, |
---|
332 | quantity1.vertex_values * quantity2.vertex_values - |
---|
333 | quantity3.vertex_values) |
---|
334 | Q = 1.5*quantity1*quantity2 - 3*quantity3 + 5.0 |
---|
335 | assert allclose(Q.vertex_values, |
---|
336 | 1.5*quantity1.vertex_values * quantity2.vertex_values - |
---|
337 | 3*quantity3.vertex_values + 5) |
---|
338 | |
---|
339 | #Try combining quantities and arrays and scalars |
---|
340 | Q = 1.5*quantity1*quantity2.vertex_values -\ |
---|
341 | 3*quantity3.vertex_values + 5.0 |
---|
342 | assert allclose(Q.vertex_values, |
---|
343 | 1.5*quantity1.vertex_values * quantity2.vertex_values - |
---|
344 | 3*quantity3.vertex_values + 5) |
---|
345 | |
---|
346 | |
---|
347 | #Powers |
---|
348 | Q = quantity1**2 |
---|
349 | assert allclose(Q.vertex_values, quantity1.vertex_values**2) |
---|
350 | |
---|
351 | Q = quantity1**2 +quantity2**2 |
---|
352 | assert allclose(Q.vertex_values, |
---|
353 | quantity1.vertex_values**2 + \ |
---|
354 | quantity2.vertex_values**2) |
---|
355 | |
---|
356 | Q = (quantity1**2 +quantity2**2)**0.5 |
---|
357 | assert allclose(Q.vertex_values, |
---|
358 | (quantity1.vertex_values**2 + \ |
---|
359 | quantity2.vertex_values**2)**0.5) |
---|
360 | |
---|
361 | def test_compute_gradient(self): |
---|
362 | quantity = Quantity(self.domain6) |
---|
363 | |
---|
364 | #Set up for a gradient of (2,0) at mid triangle |
---|
365 | quantity.set_values([2.0, 4.0, 4.0, 5.0, 10.0, 12.0], |
---|
366 | location = 'centroids') |
---|
367 | |
---|
368 | |
---|
369 | #Gradients |
---|
370 | quantity.compute_gradients() |
---|
371 | |
---|
372 | a = quantity.gradients |
---|
373 | |
---|
374 | assert allclose(a, [ 3., 1., 0.5, 3., 3.5, 0.5]) |
---|
375 | |
---|
376 | quantity.extrapolate_second_order() |
---|
377 | |
---|
378 | |
---|
379 | assert allclose(quantity.vertex_values, [[ 1., 3. ], |
---|
380 | [ 4., 4. ], |
---|
381 | [ 4., 4. ], |
---|
382 | [ 4., 6.], |
---|
383 | [ 8.25, 11.75], |
---|
384 | [ 11., 13. ]]) |
---|
385 | |
---|
386 | |
---|
387 | |
---|
388 | def test_second_order_extrapolation2(self): |
---|
389 | quantity = Quantity(self.domain4) |
---|
390 | |
---|
391 | #Set up for a gradient of (3,1), f(x) = 3x+y |
---|
392 | quantity.set_values([2.0+2.0/3, 4.0+4.0/3, 8.0+2.0/3, 2.0+8.0/3], |
---|
393 | location = 'centroids') |
---|
394 | |
---|
395 | #Gradients |
---|
396 | quantity.compute_gradients() |
---|
397 | |
---|
398 | a = quantity.gradients |
---|
399 | |
---|
400 | |
---|
401 | assert allclose(a[1], 2.8) |
---|
402 | |
---|
403 | #Work out the others |
---|
404 | |
---|
405 | quantity.extrapolate_second_order() |
---|
406 | |
---|
407 | |
---|
408 | assert allclose(quantity.vertex_values[1,0], 3.33333333) |
---|
409 | assert allclose(quantity.vertex_values[1,1], 7.33333333) |
---|
410 | |
---|
411 | assert allclose(quantity.centroid_values[1], 0.5*(7.33333333+3.33333333) ) |
---|
412 | |
---|
413 | |
---|
414 | |
---|
415 | |
---|
416 | def test_backup_saxpy_centroid_values(self): |
---|
417 | quantity = Quantity(self.domain4) |
---|
418 | |
---|
419 | #Set up for a gradient of (3,1), f(x) = 3x+y |
---|
420 | c_values = array([2.0+2.0/3, 4.0+4.0/3, 8.0+2.0/3, 2.0+8.0/3]) |
---|
421 | d_values = array([1.0, 2.0, 3.0, 4.0]) |
---|
422 | quantity.set_values(c_values, location = 'centroids') |
---|
423 | |
---|
424 | #Backup |
---|
425 | quantity.backup_centroid_values() |
---|
426 | |
---|
427 | #print quantity.vertex_values |
---|
428 | assert allclose(quantity.centroid_values, quantity.centroid_backup_values) |
---|
429 | |
---|
430 | |
---|
431 | quantity.set_values(d_values, location = 'centroids') |
---|
432 | |
---|
433 | quantity.saxpy_centroid_values(2.0, 3.0) |
---|
434 | |
---|
435 | assert allclose(quantity.centroid_values, 2.0*d_values + 3.0*c_values) |
---|
436 | |
---|
437 | |
---|
438 | |
---|
439 | def test_first_order_extrapolator(self): |
---|
440 | quantity = Quantity(self.domain4) |
---|
441 | |
---|
442 | centroid_values = array([1.,2.,3.,4.]) |
---|
443 | quantity.set_values(centroid_values, location = 'centroids') |
---|
444 | assert allclose(quantity.centroid_values, centroid_values) #Centroid |
---|
445 | |
---|
446 | #Extrapolate |
---|
447 | quantity.extrapolate_first_order() |
---|
448 | |
---|
449 | #Check that gradient is zero |
---|
450 | a = quantity.gradients |
---|
451 | assert allclose(a, [0,0,0,0]) |
---|
452 | |
---|
453 | |
---|
454 | #Check vertices but not edge values |
---|
455 | assert allclose(quantity.vertex_values, |
---|
456 | [[1,1], [2,2], [3,3], [4,4]]) |
---|
457 | |
---|
458 | |
---|
459 | def test_second_order_extrapolator(self): |
---|
460 | quantity = Quantity(self.domain4) |
---|
461 | |
---|
462 | #Set up for a gradient of (3,0) at mid triangle |
---|
463 | quantity.set_values([2.0, 4.0, 8.0, 2.0], |
---|
464 | location = 'centroids') |
---|
465 | |
---|
466 | |
---|
467 | |
---|
468 | quantity.extrapolate_second_order() |
---|
469 | |
---|
470 | |
---|
471 | #Assert that quantities are conserved |
---|
472 | from numpy import sum |
---|
473 | for k in range(quantity.centroid_values.shape[0]): |
---|
474 | assert allclose (quantity.centroid_values[k], |
---|
475 | sum(quantity.vertex_values[k,:])/2.0) |
---|
476 | |
---|
477 | |
---|
478 | def test_limit(self): |
---|
479 | quantity = Quantity(self.domain4) |
---|
480 | |
---|
481 | #Create a deliberate overshoot (e.g. from gradient computation) |
---|
482 | quantity.set_values([[0,0], [2,20], [-20,3], [8,3]]) |
---|
483 | |
---|
484 | #Limit |
---|
485 | quantity.limit_minmod() |
---|
486 | |
---|
487 | |
---|
488 | #cells 1 and 2 are local max and min |
---|
489 | assert quantity.vertex_values[1][0] == quantity.centroid_values[1] |
---|
490 | assert quantity.vertex_values[1][1] == quantity.centroid_values[1] |
---|
491 | |
---|
492 | assert quantity.vertex_values[2][0] == quantity.centroid_values[2] |
---|
493 | assert quantity.vertex_values[2][1] == quantity.centroid_values[2] |
---|
494 | |
---|
495 | |
---|
496 | |
---|
497 | def test_distribute_first_order(self): |
---|
498 | quantity = Quantity(self.domain4) |
---|
499 | |
---|
500 | #Test centroids |
---|
501 | centroid_values = array([1.,2.,3.,4.]) |
---|
502 | quantity.set_values(centroid_values, location = 'centroids') |
---|
503 | assert allclose(quantity.centroid_values, centroid_values) #Centroid |
---|
504 | |
---|
505 | |
---|
506 | #Extrapolate from centroid to vertices and edges |
---|
507 | quantity.extrapolate_first_order() |
---|
508 | |
---|
509 | assert allclose(quantity.vertex_values,[[ 1., 1.], |
---|
510 | [ 2., 2.], |
---|
511 | [ 3., 3.], |
---|
512 | [ 4., 4.]]) |
---|
513 | |
---|
514 | |
---|
515 | |
---|
516 | def test_distribute_second_order(self): |
---|
517 | quantity = Quantity(self.domain4) |
---|
518 | |
---|
519 | #Test centroids |
---|
520 | centroid_values = array([2.,4.,8.,2.]) |
---|
521 | quantity.set_values(centroid_values, location = 'centroids') |
---|
522 | assert allclose(quantity.centroid_values, centroid_values) #Centroid |
---|
523 | |
---|
524 | |
---|
525 | #Extrapolate |
---|
526 | quantity.extrapolate_second_order() |
---|
527 | |
---|
528 | assert allclose(quantity.vertex_values, [[ 1.2, 2.8], |
---|
529 | [ 2., 6. ], |
---|
530 | [ 8., 8. ], |
---|
531 | [ 6.8, -2.8]]) |
---|
532 | |
---|
533 | |
---|
534 | def test_update_explicit(self): |
---|
535 | quantity = Quantity(self.domain4) |
---|
536 | |
---|
537 | #Test centroids |
---|
538 | quantity.set_values([1.,2.,3.,4.], location = 'centroids') |
---|
539 | assert allclose(quantity.centroid_values, [1, 2, 3, 4]) #Centroid |
---|
540 | |
---|
541 | #Set explicit_update |
---|
542 | quantity.explicit_update = array( [1.,1.,1.,1.] ) |
---|
543 | |
---|
544 | #Update with given timestep |
---|
545 | quantity.update(0.1) |
---|
546 | |
---|
547 | x = array([1, 2, 3, 4]) + array( [.1,.1,.1,.1] ) |
---|
548 | assert allclose( quantity.centroid_values, x) |
---|
549 | |
---|
550 | def test_update_semi_implicit(self): |
---|
551 | quantity = Quantity(self.domain4) |
---|
552 | |
---|
553 | #Test centroids |
---|
554 | quantity.set_values([1.,2.,3.,4.], location = 'centroids') |
---|
555 | assert allclose(quantity.centroid_values, [1, 2, 3, 4]) #Centroid |
---|
556 | |
---|
557 | #Set semi implicit update |
---|
558 | quantity.semi_implicit_update = array([1.,1.,1.,1.]) |
---|
559 | |
---|
560 | #Update with given timestep |
---|
561 | timestep = 0.1 |
---|
562 | quantity.update(timestep) |
---|
563 | |
---|
564 | sem = array([1.,1.,1.,1.])/array([1, 2, 3, 4]) |
---|
565 | denom = ones(4, Float)-timestep*sem |
---|
566 | |
---|
567 | x = array([1, 2, 3, 4])/denom |
---|
568 | assert allclose( quantity.centroid_values, x) |
---|
569 | |
---|
570 | |
---|
571 | def test_both_updates(self): |
---|
572 | quantity = Quantity(self.domain4) |
---|
573 | |
---|
574 | #Test centroids |
---|
575 | centroid_values = array( [1, 2, 3, 4] ) |
---|
576 | quantity.set_values(centroid_values, location = 'centroids') |
---|
577 | assert allclose(quantity.centroid_values, centroid_values) #Centroid |
---|
578 | |
---|
579 | #Set explicit_update |
---|
580 | explicit_update = array( [4.,3.,2.,1.] ) |
---|
581 | quantity.explicit_update[:] = explicit_update |
---|
582 | |
---|
583 | #Set semi implicit update |
---|
584 | semi_implicit_update = array( [1.,1.,1.,1.] ) |
---|
585 | quantity.semi_implicit_update[:] = semi_implicit_update |
---|
586 | |
---|
587 | #Update with given timestep |
---|
588 | timestep = 0.1 |
---|
589 | quantity.update(0.1) |
---|
590 | |
---|
591 | denom = 1.0-timestep*semi_implicit_update |
---|
592 | x = (centroid_values + timestep*explicit_update)/denom |
---|
593 | |
---|
594 | assert allclose( quantity.centroid_values, x) |
---|
595 | |
---|
596 | #Test smoothing |
---|
597 | def test_smoothing(self): |
---|
598 | |
---|
599 | |
---|
600 | from shallow_water import Domain, Transmissive_boundary |
---|
601 | from Numeric import zeros, Float |
---|
602 | from anuga.utilities.numerical_tools import mean |
---|
603 | |
---|
604 | |
---|
605 | #Create shallow water domain |
---|
606 | domain = Domain(points10) |
---|
607 | domain.default_order=2 |
---|
608 | domain.reduction = mean |
---|
609 | |
---|
610 | |
---|
611 | #Set some field values |
---|
612 | domain.set_quantity('elevation', lambda x: x) |
---|
613 | domain.set_quantity('friction', 0.03) |
---|
614 | |
---|
615 | |
---|
616 | ###################### |
---|
617 | # Boundary conditions |
---|
618 | B = Transmissive_boundary(domain) |
---|
619 | domain.set_boundary( {'left': B, 'right': B, 'top': B, 'bottom': B}) |
---|
620 | |
---|
621 | |
---|
622 | ###################### |
---|
623 | #Initial condition - with jumps |
---|
624 | |
---|
625 | bed = domain.quantities['elevation'].vertex_values |
---|
626 | stage = zeros(bed.shape, Float) |
---|
627 | |
---|
628 | h = 0.03 |
---|
629 | for i in range(stage.shape[0]): |
---|
630 | if i % 2 == 0: |
---|
631 | stage[i,:] = bed[i,:] + h |
---|
632 | else: |
---|
633 | stage[i,:] = bed[i,:] |
---|
634 | |
---|
635 | domain.set_quantity('stage', stage) |
---|
636 | |
---|
637 | stage = domain.quantities['stage'] |
---|
638 | |
---|
639 | #Get smoothed stage |
---|
640 | A, V = stage.get_vertex_values(xy=False, smooth=True) |
---|
641 | Q = stage.vertex_values |
---|
642 | |
---|
643 | |
---|
644 | assert A.shape[0] == 9 |
---|
645 | assert V.shape[0] == 8 |
---|
646 | assert V.shape[1] == 3 |
---|
647 | |
---|
648 | #First four points |
---|
649 | assert allclose(A[0], (Q[0,2] + Q[1,1])/2) |
---|
650 | assert allclose(A[1], (Q[1,0] + Q[3,1] + Q[2,2])/3) |
---|
651 | assert allclose(A[2], Q[3,0]) |
---|
652 | assert allclose(A[3], (Q[0,0] + Q[5,1] + Q[4,2])/3) |
---|
653 | |
---|
654 | #Center point |
---|
655 | assert allclose(A[4], (Q[0,1] + Q[1,2] + Q[2,0] +\ |
---|
656 | Q[5,0] + Q[6,2] + Q[7,1])/6) |
---|
657 | |
---|
658 | |
---|
659 | #Check V |
---|
660 | assert allclose(V[0,:], [3,4,0]) |
---|
661 | assert allclose(V[1,:], [1,0,4]) |
---|
662 | assert allclose(V[2,:], [4,5,1]) |
---|
663 | assert allclose(V[3,:], [2,1,5]) |
---|
664 | assert allclose(V[4,:], [6,7,3]) |
---|
665 | assert allclose(V[5,:], [4,3,7]) |
---|
666 | assert allclose(V[6,:], [7,8,4]) |
---|
667 | assert allclose(V[7,:], [5,4,8]) |
---|
668 | |
---|
669 | #Get smoothed stage with XY |
---|
670 | X, Y, A1, V1 = stage.get_vertex_values(xy=True, smooth=True) |
---|
671 | |
---|
672 | assert allclose(A, A1) |
---|
673 | assert allclose(V, V1) |
---|
674 | |
---|
675 | #Check XY |
---|
676 | assert allclose(X[4], 0.5) |
---|
677 | assert allclose(Y[4], 0.5) |
---|
678 | |
---|
679 | assert allclose(X[7], 1.0) |
---|
680 | assert allclose(Y[7], 0.5) |
---|
681 | |
---|
682 | |
---|
683 | |
---|
684 | |
---|
685 | def test_vertex_values_no_smoothing(self): |
---|
686 | |
---|
687 | from mesh_factory import rectangular |
---|
688 | from shallow_water import Domain, Transmissive_boundary |
---|
689 | from Numeric import zeros, Float |
---|
690 | from anuga.utilities.numerical_tools import mean |
---|
691 | |
---|
692 | |
---|
693 | #Create basic mesh |
---|
694 | points, vertices, boundary = rectangular(2, 2) |
---|
695 | |
---|
696 | #Create shallow water domain |
---|
697 | domain = Domain(points, vertices, boundary) |
---|
698 | domain.default_order=2 |
---|
699 | domain.reduction = mean |
---|
700 | |
---|
701 | |
---|
702 | #Set some field values |
---|
703 | domain.set_quantity('elevation', lambda x,y: x) |
---|
704 | domain.set_quantity('friction', 0.03) |
---|
705 | |
---|
706 | |
---|
707 | ###################### |
---|
708 | #Initial condition - with jumps |
---|
709 | |
---|
710 | bed = domain.quantities['elevation'].vertex_values |
---|
711 | stage = zeros(bed.shape, Float) |
---|
712 | |
---|
713 | h = 0.03 |
---|
714 | for i in range(stage.shape[0]): |
---|
715 | if i % 2 == 0: |
---|
716 | stage[i,:] = bed[i,:] + h |
---|
717 | else: |
---|
718 | stage[i,:] = bed[i,:] |
---|
719 | |
---|
720 | domain.set_quantity('stage', stage) |
---|
721 | |
---|
722 | #Get stage |
---|
723 | stage = domain.quantities['stage'] |
---|
724 | A, V = stage.get_vertex_values(xy=False, smooth=False) |
---|
725 | Q = stage.vertex_values.flat |
---|
726 | |
---|
727 | for k in range(8): |
---|
728 | assert allclose(A[k], Q[k]) |
---|
729 | |
---|
730 | |
---|
731 | for k in range(8): |
---|
732 | assert V[k, 0] == 3*k |
---|
733 | assert V[k, 1] == 3*k+1 |
---|
734 | assert V[k, 2] == 3*k+2 |
---|
735 | |
---|
736 | |
---|
737 | |
---|
738 | X, Y, A1, V1 = stage.get_vertex_values(xy=True, smooth=False) |
---|
739 | |
---|
740 | |
---|
741 | assert allclose(A, A1) |
---|
742 | assert allclose(V, V1) |
---|
743 | |
---|
744 | #Check XY |
---|
745 | assert allclose(X[1], 0.5) |
---|
746 | assert allclose(Y[1], 0.5) |
---|
747 | assert allclose(X[4], 0.0) |
---|
748 | assert allclose(Y[4], 0.0) |
---|
749 | assert allclose(X[12], 1.0) |
---|
750 | assert allclose(Y[12], 0.0) |
---|
751 | |
---|
752 | |
---|
753 | |
---|
754 | |
---|
755 | #------------------------------------------------------------- |
---|
756 | if __name__ == "__main__": |
---|
757 | suite = unittest.makeSuite(Test_Quantity, 'test_set') |
---|
758 | runner = unittest.TextTestRunner() |
---|
759 | runner.run(suite) |
---|