1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | import unittest |
---|
4 | from math import sqrt, pi |
---|
5 | |
---|
6 | |
---|
7 | from quantity import * |
---|
8 | from config import epsilon |
---|
9 | from Numeric import allclose, array |
---|
10 | |
---|
11 | |
---|
12 | class TestCase(unittest.TestCase): |
---|
13 | def setUp(self): |
---|
14 | from domain import Domain |
---|
15 | |
---|
16 | a = [0.0, 0.0] |
---|
17 | b = [0.0, 2.0] |
---|
18 | c = [2.0,0.0] |
---|
19 | d = [0.0, 4.0] |
---|
20 | e = [2.0, 2.0] |
---|
21 | f = [4.0,0.0] |
---|
22 | |
---|
23 | points = [a, b, c, d, e, f] |
---|
24 | |
---|
25 | #bac, bce, ecf, dbe |
---|
26 | elements = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ] |
---|
27 | |
---|
28 | self.mesh1 = Domain(points[:3], [elements[0]]) |
---|
29 | self.mesh1.check_integrity() |
---|
30 | |
---|
31 | self.mesh4 = Domain(points, elements) |
---|
32 | self.mesh4.check_integrity() |
---|
33 | |
---|
34 | def tearDown(self): |
---|
35 | pass |
---|
36 | #print " Tearing down" |
---|
37 | |
---|
38 | |
---|
39 | def test_creation(self): |
---|
40 | |
---|
41 | quantity = Quantity(self.mesh1, [[1,2,3]]) |
---|
42 | assert allclose(quantity.vertex_values, [[1.,2.,3.]]) |
---|
43 | |
---|
44 | try: |
---|
45 | quantity = Quantity() |
---|
46 | except: |
---|
47 | pass |
---|
48 | else: |
---|
49 | raise 'Should have raised empty quantity exception' |
---|
50 | |
---|
51 | |
---|
52 | try: |
---|
53 | quantity = Quantity([1,2,3]) |
---|
54 | except AssertionError: |
---|
55 | pass |
---|
56 | except: |
---|
57 | raise 'Should have raised "mising mesh object" error' |
---|
58 | |
---|
59 | |
---|
60 | def test_creation_zeros(self): |
---|
61 | |
---|
62 | quantity = Quantity(self.mesh1) |
---|
63 | assert allclose(quantity.vertex_values, [[0.,0.,0.]]) |
---|
64 | |
---|
65 | |
---|
66 | quantity = Quantity(self.mesh4) |
---|
67 | assert allclose(quantity.vertex_values, [[0.,0.,0.], [0.,0.,0.], |
---|
68 | [0.,0.,0.], [0.,0.,0.]]) |
---|
69 | |
---|
70 | |
---|
71 | def test_interpolation(self): |
---|
72 | quantity = Quantity(self.mesh1, [[1,2,3]]) |
---|
73 | assert allclose(quantity.centroid_values, [2.0]) #Centroid |
---|
74 | |
---|
75 | assert allclose(quantity.edge_values, [[2.5, 2.0, 1.5]]) |
---|
76 | |
---|
77 | |
---|
78 | def test_interpolation2(self): |
---|
79 | quantity = Conserved_quantity(self.mesh4, |
---|
80 | [[1,2,3], [5,5,5], [0,0,9], [-6, 3, 3]]) |
---|
81 | assert allclose(quantity.centroid_values, [2., 5., 3., 0.]) #Centroid |
---|
82 | |
---|
83 | |
---|
84 | quantity.extrapolate_second_order() |
---|
85 | |
---|
86 | assert allclose(quantity.vertex_values, [[2., 2., 2.], |
---|
87 | [3.+2./3, 6.+2./3, 4.+2./3], |
---|
88 | [7.5, 0.5, 1.], |
---|
89 | [-5, -2.5, 7.5]]) |
---|
90 | |
---|
91 | #print quantity.edge_values |
---|
92 | assert allclose(quantity.edge_values, [[2.5, 2.0, 1.5], |
---|
93 | [5., 5., 5.], |
---|
94 | [4.5, 4.5, 0.], |
---|
95 | [3.0, -1.5, -1.5]]) |
---|
96 | |
---|
97 | def test_boundary_allocation(self): |
---|
98 | quantity = Conserved_quantity(self.mesh4, |
---|
99 | [[1,2,3], [5,5,5], [0,0,9], [-6, 3, 3]]) |
---|
100 | |
---|
101 | assert quantity.boundary_values.shape[0] == len(self.mesh4.boundary) |
---|
102 | |
---|
103 | |
---|
104 | def test_set_values(self): |
---|
105 | quantity = Quantity(self.mesh4) |
---|
106 | |
---|
107 | |
---|
108 | quantity.set_values([[1,2,3], [5,5,5], [0,0,9], [-6, 3, 3]], |
---|
109 | location = 'vertices') |
---|
110 | assert allclose(quantity.vertex_values, |
---|
111 | [[1,2,3], [5,5,5], [0,0,9], [-6, 3, 3]]) |
---|
112 | assert allclose(quantity.centroid_values, [2., 5., 3., 0.]) #Centroid |
---|
113 | assert allclose(quantity.edge_values, [[2.5, 2.0, 1.5], |
---|
114 | [5., 5., 5.], |
---|
115 | [4.5, 4.5, 0.], |
---|
116 | [3.0, -1.5, -1.5]]) |
---|
117 | |
---|
118 | |
---|
119 | #Test default |
---|
120 | quantity.set_values([[1,2,3], [5,5,5], [0,0,9], [-6, 3, 3]]) |
---|
121 | assert allclose(quantity.vertex_values, |
---|
122 | [[1,2,3], [5,5,5], [0,0,9], [-6, 3, 3]]) |
---|
123 | assert allclose(quantity.centroid_values, [2., 5., 3., 0.]) #Centroid |
---|
124 | assert allclose(quantity.edge_values, [[2.5, 2.0, 1.5], |
---|
125 | [5., 5., 5.], |
---|
126 | [4.5, 4.5, 0.], |
---|
127 | [3.0, -1.5, -1.5]]) |
---|
128 | |
---|
129 | #Test centroids |
---|
130 | quantity.set_values([1,2,3,4], location = 'centroids') |
---|
131 | assert allclose(quantity.centroid_values, [1., 2., 3., 4.]) #Centroid |
---|
132 | |
---|
133 | #Test edges |
---|
134 | quantity.set_values([[1,2,3], [5,5,5], [0,0,9], [-6, 3, 3]], |
---|
135 | location = 'edges') |
---|
136 | assert allclose(quantity.edge_values, |
---|
137 | [[1,2,3], [5,5,5], [0,0,9], [-6, 3, 3]]) |
---|
138 | |
---|
139 | #Test exceptions |
---|
140 | try: |
---|
141 | quantity.set_values([[1,2,3], [5,5,5], [0,0,9], [-6, 3, 3]], |
---|
142 | location = 'bas kamel tuba') |
---|
143 | except: |
---|
144 | pass |
---|
145 | |
---|
146 | |
---|
147 | try: |
---|
148 | quantity.set_values([[1,2,3], [0,0,9]]) |
---|
149 | except AssertionError: |
---|
150 | pass |
---|
151 | except: |
---|
152 | raise 'should have raised Assertionerror' |
---|
153 | |
---|
154 | |
---|
155 | |
---|
156 | def test_set_values_const(self): |
---|
157 | quantity = Quantity(self.mesh4) |
---|
158 | |
---|
159 | quantity.set_values(1.0, location = 'vertices') |
---|
160 | assert allclose(quantity.vertex_values, |
---|
161 | [[1,1,1], [1,1,1], [1,1,1], [1, 1, 1]]) |
---|
162 | assert allclose(quantity.centroid_values, [1, 1, 1, 1]) #Centroid |
---|
163 | assert allclose(quantity.edge_values, [[1, 1, 1], |
---|
164 | [1, 1, 1], |
---|
165 | [1, 1, 1], |
---|
166 | [1, 1, 1]]) |
---|
167 | |
---|
168 | |
---|
169 | quantity.set_values(2.0, location = 'centroids') |
---|
170 | assert allclose(quantity.centroid_values, [2, 2, 2, 2]) |
---|
171 | |
---|
172 | quantity.set_values(3.0, location = 'edges') |
---|
173 | assert allclose(quantity.edge_values, [[3, 3, 3], |
---|
174 | [3, 3, 3], |
---|
175 | [3, 3, 3], |
---|
176 | [3, 3, 3]]) |
---|
177 | |
---|
178 | |
---|
179 | def test_set_values_func(self): |
---|
180 | quantity = Quantity(self.mesh4) |
---|
181 | |
---|
182 | def f(x, y): |
---|
183 | return x+y |
---|
184 | |
---|
185 | quantity.set_values(f, location = 'vertices') |
---|
186 | assert allclose(quantity.vertex_values, |
---|
187 | [[2,0,2], [2,2,4], [4,2,4], [4,2,4]]) |
---|
188 | assert allclose(quantity.centroid_values, |
---|
189 | [4.0/3, 8.0/3, 10.0/3, 10.0/3]) |
---|
190 | assert allclose(quantity.edge_values, |
---|
191 | [[1,2,1], [3,3,2], [3,4,3], [3,4,3]]) |
---|
192 | |
---|
193 | |
---|
194 | quantity.set_values(f, location = 'centroids') |
---|
195 | assert allclose(quantity.centroid_values, |
---|
196 | [4.0/3, 8.0/3, 10.0/3, 10.0/3]) |
---|
197 | |
---|
198 | |
---|
199 | |
---|
200 | |
---|
201 | def test_set_vertex_values(self): |
---|
202 | quantity = Quantity(self.mesh4) |
---|
203 | |
---|
204 | |
---|
205 | quantity.set_vertex_values([0,1,2,3,4,5]) |
---|
206 | |
---|
207 | |
---|
208 | assert allclose(quantity.vertex_values, |
---|
209 | [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]) |
---|
210 | |
---|
211 | assert allclose(quantity.centroid_values, [1., 7./3, 11./3, 8./3]) #Centroid |
---|
212 | |
---|
213 | assert allclose(quantity.edge_values, [[1., 1.5, 0.5], |
---|
214 | [3., 2.5, 1.5], |
---|
215 | [3.5, 4.5, 3.], |
---|
216 | [2.5, 3.5, 2]]) |
---|
217 | |
---|
218 | |
---|
219 | |
---|
220 | |
---|
221 | def test_set_vertex_values_using_general_interface(self): |
---|
222 | quantity = Quantity(self.mesh4) |
---|
223 | |
---|
224 | |
---|
225 | quantity.set_values([0,1,2,3,4,5]) |
---|
226 | |
---|
227 | |
---|
228 | assert allclose(quantity.vertex_values, |
---|
229 | [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]) |
---|
230 | |
---|
231 | assert allclose(quantity.centroid_values, [1., 7./3, 11./3, 8./3]) #Centroid |
---|
232 | |
---|
233 | assert allclose(quantity.edge_values, [[1., 1.5, 0.5], |
---|
234 | [3., 2.5, 1.5], |
---|
235 | [3.5, 4.5, 3.], |
---|
236 | [2.5, 3.5, 2]]) |
---|
237 | |
---|
238 | |
---|
239 | |
---|
240 | |
---|
241 | def test_gradient(self): |
---|
242 | quantity = Conserved_quantity(self.mesh4) |
---|
243 | |
---|
244 | #Set up for a gradient of (3,0) at mid triangle |
---|
245 | quantity.set_values([2.0, 4.0, 8.0, 2.0], |
---|
246 | location = 'centroids') |
---|
247 | |
---|
248 | |
---|
249 | |
---|
250 | #Gradients |
---|
251 | a, b = quantity.compute_gradients() |
---|
252 | |
---|
253 | #gradient bewteen t0 and t1 is undefined as det==0 |
---|
254 | assert a[0] == 0.0 |
---|
255 | assert b[0] == 0.0 |
---|
256 | #The others are OK |
---|
257 | for i in range(1,4): |
---|
258 | assert a[i] == 3.0 |
---|
259 | assert b[i] == 0.0 |
---|
260 | |
---|
261 | |
---|
262 | quantity.extrapolate_second_order() |
---|
263 | |
---|
264 | #print quantity.vertex_values |
---|
265 | assert allclose(quantity.vertex_values, [[2., 2., 2.], |
---|
266 | [0., 6., 6.], |
---|
267 | [6., 6., 12.], |
---|
268 | [0., 0., 6.]]) |
---|
269 | |
---|
270 | |
---|
271 | def test_second_order_extrapolation2(self): |
---|
272 | quantity = Conserved_quantity(self.mesh4) |
---|
273 | |
---|
274 | #Set up for a gradient of (3,1), f(x) = 3x+y |
---|
275 | quantity.set_values([2.0+2.0/3, 4.0+4.0/3, 8.0+2.0/3, 2.0+8.0/3], |
---|
276 | location = 'centroids') |
---|
277 | |
---|
278 | #Gradients |
---|
279 | a, b = quantity.compute_gradients() |
---|
280 | |
---|
281 | #gradient bewteen t0 and t1 is undefined as det==0 |
---|
282 | assert a[0] == 0.0 |
---|
283 | assert b[0] == 0.0 |
---|
284 | #The others are OK |
---|
285 | for i in range(1,4): |
---|
286 | assert allclose(a[i], 3.0) |
---|
287 | assert allclose(b[i], 1.0) |
---|
288 | |
---|
289 | quantity.extrapolate_second_order() |
---|
290 | |
---|
291 | #print quantity.vertex_values |
---|
292 | assert allclose(quantity.vertex_values[1,0], 2.0) |
---|
293 | assert allclose(quantity.vertex_values[1,1], 6.0) |
---|
294 | assert allclose(quantity.vertex_values[1,2], 8.0) |
---|
295 | |
---|
296 | |
---|
297 | |
---|
298 | def test_first_order_extrapolator(self): |
---|
299 | quantity = Conserved_quantity(self.mesh4) |
---|
300 | |
---|
301 | #Test centroids |
---|
302 | quantity.set_values([1.,2.,3.,4.], location = 'centroids') |
---|
303 | assert allclose(quantity.centroid_values, [1, 2, 3, 4]) #Centroid |
---|
304 | |
---|
305 | #Extrapolate |
---|
306 | quantity.extrapolate_first_order() |
---|
307 | |
---|
308 | #Check vertices but not edge values |
---|
309 | assert allclose(quantity.vertex_values, |
---|
310 | [[1,1,1], [2,2,2], [3,3,3], [4, 4, 4]]) |
---|
311 | |
---|
312 | |
---|
313 | def test_second_order_extrapolator(self): |
---|
314 | quantity = Conserved_quantity(self.mesh4) |
---|
315 | |
---|
316 | #Set up for a gradient of (3,0) at mid triangle |
---|
317 | quantity.set_values([2.0, 4.0, 8.0, 2.0], |
---|
318 | location = 'centroids') |
---|
319 | |
---|
320 | |
---|
321 | |
---|
322 | quantity.extrapolate_second_order() |
---|
323 | quantity.limit() |
---|
324 | |
---|
325 | |
---|
326 | #Assert that central triangle is limited by neighbours |
---|
327 | assert quantity.vertex_values[1,0] >= quantity.vertex_values[0,0] |
---|
328 | assert quantity.vertex_values[1,0] >= quantity.vertex_values[3,1] |
---|
329 | |
---|
330 | assert quantity.vertex_values[1,1] <= quantity.vertex_values[2,1] |
---|
331 | assert quantity.vertex_values[1,1] >= quantity.vertex_values[0,2] |
---|
332 | |
---|
333 | assert quantity.vertex_values[1,2] <= quantity.vertex_values[2,0] |
---|
334 | assert quantity.vertex_values[1,2] >= quantity.vertex_values[3,1] |
---|
335 | |
---|
336 | |
---|
337 | #Assert that quantities are conserved |
---|
338 | from Numeric import sum |
---|
339 | for k in range(quantity.centroid_values.shape[0]): |
---|
340 | assert allclose (quantity.centroid_values[k], |
---|
341 | sum(quantity.vertex_values[k,:])/3) |
---|
342 | |
---|
343 | |
---|
344 | |
---|
345 | |
---|
346 | |
---|
347 | def test_limiter(self): |
---|
348 | quantity = Conserved_quantity(self.mesh4) |
---|
349 | |
---|
350 | #Create a deliberate overshoot (e.g. from gradient computation) |
---|
351 | quantity.set_values([[3,0,3], [2,2,6], [5,3,8], [8,3,5]]) |
---|
352 | |
---|
353 | |
---|
354 | #Limit |
---|
355 | quantity.limit() |
---|
356 | |
---|
357 | #Assert that central triangle is limited by neighbours |
---|
358 | assert quantity.vertex_values[1,0] >= quantity.vertex_values[0,0] |
---|
359 | assert quantity.vertex_values[1,0] <= quantity.vertex_values[3,1] |
---|
360 | |
---|
361 | assert quantity.vertex_values[1,1] <= quantity.vertex_values[2,1] |
---|
362 | assert quantity.vertex_values[1,1] >= quantity.vertex_values[0,2] |
---|
363 | |
---|
364 | assert quantity.vertex_values[1,2] <= quantity.vertex_values[2,0] |
---|
365 | assert quantity.vertex_values[1,2] <= quantity.vertex_values[3,1] |
---|
366 | |
---|
367 | |
---|
368 | |
---|
369 | #Assert that quantities are conserved |
---|
370 | from Numeric import sum |
---|
371 | for k in range(quantity.centroid_values.shape[0]): |
---|
372 | assert allclose (quantity.centroid_values[k], |
---|
373 | sum(quantity.vertex_values[k,:])/3) |
---|
374 | |
---|
375 | |
---|
376 | |
---|
377 | def test_distribute_first_order(self): |
---|
378 | quantity = Conserved_quantity(self.mesh4) |
---|
379 | |
---|
380 | #Test centroids |
---|
381 | quantity.set_values([1.,2.,3.,4.], location = 'centroids') |
---|
382 | assert allclose(quantity.centroid_values, [1, 2, 3, 4]) #Centroid |
---|
383 | |
---|
384 | |
---|
385 | #Extrapolate |
---|
386 | quantity.extrapolate_first_order() |
---|
387 | |
---|
388 | #Interpolate |
---|
389 | quantity.interpolate_from_vertices_to_edges() |
---|
390 | |
---|
391 | assert allclose(quantity.vertex_values, |
---|
392 | [[1,1,1], [2,2,2], [3,3,3], [4, 4, 4]]) |
---|
393 | assert allclose(quantity.edge_values, [[1,1,1], [2,2,2], |
---|
394 | [3,3,3], [4, 4, 4]]) |
---|
395 | |
---|
396 | |
---|
397 | |
---|
398 | def test_update_explicit(self): |
---|
399 | quantity = Conserved_quantity(self.mesh4) |
---|
400 | |
---|
401 | #Test centroids |
---|
402 | quantity.set_values([1.,2.,3.,4.], location = 'centroids') |
---|
403 | assert allclose(quantity.centroid_values, [1, 2, 3, 4]) #Centroid |
---|
404 | |
---|
405 | #Set explicit_update |
---|
406 | quantity.explicit_update = array( [1.,1.,1.,1.] ) |
---|
407 | |
---|
408 | #Update with given timestep |
---|
409 | quantity.update(0.1) |
---|
410 | |
---|
411 | x = array([1, 2, 3, 4]) + array( [.1,.1,.1,.1] ) |
---|
412 | assert allclose( quantity.centroid_values, x) |
---|
413 | |
---|
414 | def test_update_semi_implicit(self): |
---|
415 | quantity = Conserved_quantity(self.mesh4) |
---|
416 | |
---|
417 | #Test centroids |
---|
418 | quantity.set_values([1.,2.,3.,4.], location = 'centroids') |
---|
419 | assert allclose(quantity.centroid_values, [1, 2, 3, 4]) #Centroid |
---|
420 | |
---|
421 | #Set semi implicit update |
---|
422 | quantity.semi_implicit_update = array( [1.,1.,1.,1.] ) |
---|
423 | |
---|
424 | #Update with given timestep |
---|
425 | quantity.update(0.1) |
---|
426 | |
---|
427 | x = array([1, 2, 3, 4])/array( [.9,.9,.9,.9] ) |
---|
428 | assert allclose( quantity.centroid_values, x) |
---|
429 | |
---|
430 | def test_both_updates(self): |
---|
431 | quantity = Conserved_quantity(self.mesh4) |
---|
432 | |
---|
433 | #Test centroids |
---|
434 | quantity.set_values([1.,2.,3.,4.], location = 'centroids') |
---|
435 | assert allclose(quantity.centroid_values, [1, 2, 3, 4]) #Centroid |
---|
436 | |
---|
437 | #Set explicit_update |
---|
438 | quantity.explicit_update = array( [4.,3.,2.,1.] ) |
---|
439 | |
---|
440 | #Set semi implicit update |
---|
441 | quantity.semi_implicit_update = array( [1.,1.,1.,1.] ) |
---|
442 | |
---|
443 | #Update with given timestep |
---|
444 | quantity.update(0.1) |
---|
445 | |
---|
446 | x = array([1, 2, 3, 4]) + array( [.4,.3,.2,.1] ) |
---|
447 | x /= array( [.9,.9,.9,.9] ) |
---|
448 | assert allclose( quantity.centroid_values, x) |
---|
449 | |
---|
450 | |
---|
451 | |
---|
452 | |
---|
453 | #Test smoothing |
---|
454 | def test_smoothing(self): |
---|
455 | |
---|
456 | from mesh_factory import rectangular |
---|
457 | from shallow_water import Domain, Transmissive_boundary |
---|
458 | from Numeric import zeros, Float |
---|
459 | from util import mean |
---|
460 | |
---|
461 | #Create basic mesh |
---|
462 | points, vertices, boundary = rectangular(2, 2) |
---|
463 | |
---|
464 | #Create shallow water domain |
---|
465 | domain = Domain(points, vertices, boundary) |
---|
466 | domain.default_order=2 |
---|
467 | domain.reduction = mean |
---|
468 | |
---|
469 | |
---|
470 | #Set some field values |
---|
471 | domain.set_quantity('elevation', lambda x,y: x) |
---|
472 | domain.set_quantity('friction', 0.03) |
---|
473 | |
---|
474 | |
---|
475 | ###################### |
---|
476 | # Boundary conditions |
---|
477 | B = Transmissive_boundary(domain) |
---|
478 | domain.set_boundary( {'left': B, 'right': B, 'top': B, 'bottom': B}) |
---|
479 | |
---|
480 | |
---|
481 | ###################### |
---|
482 | #Initial condition - with jumps |
---|
483 | |
---|
484 | bed = domain.quantities['elevation'].vertex_values |
---|
485 | level = zeros(bed.shape, Float) |
---|
486 | |
---|
487 | h = 0.03 |
---|
488 | for i in range(level.shape[0]): |
---|
489 | if i % 2 == 0: |
---|
490 | level[i,:] = bed[i,:] + h |
---|
491 | else: |
---|
492 | level[i,:] = bed[i,:] |
---|
493 | |
---|
494 | domain.set_quantity('level', level) |
---|
495 | |
---|
496 | level = domain.quantities['level'] |
---|
497 | |
---|
498 | #Get smoothed level |
---|
499 | A, V = level.get_vertex_values(xy=False, smooth=True) |
---|
500 | Q = level.vertex_values |
---|
501 | |
---|
502 | |
---|
503 | assert A.shape[0] == 9 |
---|
504 | assert V.shape[0] == 8 |
---|
505 | assert V.shape[1] == 3 |
---|
506 | |
---|
507 | #First four points |
---|
508 | assert allclose(A[0], (Q[0,2] + Q[1,1])/2) |
---|
509 | assert allclose(A[1], (Q[1,0] + Q[3,1] + Q[2,2])/3) |
---|
510 | assert allclose(A[2], Q[3,0]) |
---|
511 | assert allclose(A[3], (Q[0,0] + Q[5,1] + Q[4,2])/3) |
---|
512 | |
---|
513 | #Center point |
---|
514 | assert allclose(A[4], (Q[0,1] + Q[1,2] + Q[2,0] +\ |
---|
515 | Q[5,0] + Q[6,2] + Q[7,1])/6) |
---|
516 | |
---|
517 | |
---|
518 | #Check V |
---|
519 | assert allclose(V[0,:], [3,4,0]) |
---|
520 | assert allclose(V[1,:], [1,0,4]) |
---|
521 | assert allclose(V[2,:], [4,5,1]) |
---|
522 | assert allclose(V[3,:], [2,1,5]) |
---|
523 | assert allclose(V[4,:], [6,7,3]) |
---|
524 | assert allclose(V[5,:], [4,3,7]) |
---|
525 | assert allclose(V[6,:], [7,8,4]) |
---|
526 | assert allclose(V[7,:], [5,4,8]) |
---|
527 | |
---|
528 | #Get smoothed level with XY |
---|
529 | X, Y, A1, V1 = level.get_vertex_values(xy=True, smooth=True) |
---|
530 | |
---|
531 | assert allclose(A, A1) |
---|
532 | assert allclose(V, V1) |
---|
533 | |
---|
534 | #Check XY |
---|
535 | assert allclose(X[4], 0.5) |
---|
536 | assert allclose(Y[4], 0.5) |
---|
537 | |
---|
538 | assert allclose(X[7], 1.0) |
---|
539 | assert allclose(Y[7], 0.5) |
---|
540 | |
---|
541 | |
---|
542 | |
---|
543 | |
---|
544 | def test_vertex_values_no_smoothing(self): |
---|
545 | |
---|
546 | from mesh_factory import rectangular |
---|
547 | from shallow_water import Domain, Transmissive_boundary |
---|
548 | from Numeric import zeros, Float |
---|
549 | from util import mean |
---|
550 | |
---|
551 | |
---|
552 | #Create basic mesh |
---|
553 | points, vertices, boundary = rectangular(2, 2) |
---|
554 | |
---|
555 | #Create shallow water domain |
---|
556 | domain = Domain(points, vertices, boundary) |
---|
557 | domain.default_order=2 |
---|
558 | domain.reduction = mean |
---|
559 | |
---|
560 | |
---|
561 | #Set some field values |
---|
562 | domain.set_quantity('elevation', lambda x,y: x) |
---|
563 | domain.set_quantity('friction', 0.03) |
---|
564 | |
---|
565 | |
---|
566 | ###################### |
---|
567 | #Initial condition - with jumps |
---|
568 | |
---|
569 | bed = domain.quantities['elevation'].vertex_values |
---|
570 | level = zeros(bed.shape, Float) |
---|
571 | |
---|
572 | h = 0.03 |
---|
573 | for i in range(level.shape[0]): |
---|
574 | if i % 2 == 0: |
---|
575 | level[i,:] = bed[i,:] + h |
---|
576 | else: |
---|
577 | level[i,:] = bed[i,:] |
---|
578 | |
---|
579 | domain.set_quantity('level', level) |
---|
580 | |
---|
581 | #Get level |
---|
582 | level = domain.quantities['level'] |
---|
583 | A, V = level.get_vertex_values(xy=False, smooth=False) |
---|
584 | Q = level.vertex_values.flat |
---|
585 | |
---|
586 | for k in range(8): |
---|
587 | assert allclose(A[k], Q[k]) |
---|
588 | |
---|
589 | |
---|
590 | for k in range(8): |
---|
591 | assert V[k, 0] == 3*k |
---|
592 | assert V[k, 1] == 3*k+1 |
---|
593 | assert V[k, 2] == 3*k+2 |
---|
594 | |
---|
595 | |
---|
596 | |
---|
597 | X, Y, A1, V1 = level.get_vertex_values(xy=True, smooth=False) |
---|
598 | |
---|
599 | |
---|
600 | assert allclose(A, A1) |
---|
601 | assert allclose(V, V1) |
---|
602 | |
---|
603 | #Check XY |
---|
604 | assert allclose(X[1], 0.5) |
---|
605 | assert allclose(Y[1], 0.5) |
---|
606 | assert allclose(X[4], 0.0) |
---|
607 | assert allclose(Y[4], 0.0) |
---|
608 | assert allclose(X[12], 1.0) |
---|
609 | assert allclose(Y[12], 0.0) |
---|
610 | |
---|
611 | |
---|
612 | |
---|
613 | |
---|
614 | |
---|
615 | #------------------------------------------------------------- |
---|
616 | if __name__ == "__main__": |
---|
617 | suite = unittest.makeSuite(TestCase,'test') |
---|
618 | runner = unittest.TextTestRunner() |
---|
619 | runner.run(suite) |
---|
620 | |
---|
621 | |
---|