1 | import sys |
---|
2 | from os import sep |
---|
3 | sys.path.append('..'+sep+'pyvolution') |
---|
4 | |
---|
5 | import unittest |
---|
6 | from math import sqrt, pi |
---|
7 | |
---|
8 | from config import g, epsilon |
---|
9 | from Numeric import allclose, array, zeros, ones, Float |
---|
10 | from advection import * |
---|
11 | |
---|
12 | |
---|
13 | class Test_Advection(unittest.TestCase): |
---|
14 | def setUp(self): |
---|
15 | pass |
---|
16 | |
---|
17 | def tearDown(self): |
---|
18 | pass |
---|
19 | |
---|
20 | def test_init(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]] |
---|
31 | |
---|
32 | domain = Domain(points, vertices) |
---|
33 | domain.check_integrity() |
---|
34 | |
---|
35 | assert domain.quantities.has_key('stage') |
---|
36 | |
---|
37 | assert domain.get_conserved_quantities(0, edge=1) == 0. |
---|
38 | |
---|
39 | |
---|
40 | def test_flux_1_triangle0(self): |
---|
41 | a = [0.0, 0.5] |
---|
42 | b = [0.0, 0.0] |
---|
43 | c = [0.5, 0.5] |
---|
44 | |
---|
45 | points = [a, b, c] |
---|
46 | vertices = [ [0,1,2] ] |
---|
47 | domain = Domain(points, vertices) |
---|
48 | domain.check_integrity() |
---|
49 | |
---|
50 | |
---|
51 | #Populate boundary array with dirichlet conditions. |
---|
52 | domain.neighbours = array([[-1,-2,-3]]) |
---|
53 | domain.quantities['stage'].boundary_values[:] = 1.0 |
---|
54 | |
---|
55 | domain.order = 1 |
---|
56 | |
---|
57 | domain.distribute_to_vertices_and_edges() #Use first order default |
---|
58 | |
---|
59 | domain.check_integrity() |
---|
60 | |
---|
61 | domain.compute_fluxes() |
---|
62 | U = -domain.quantities['stage'].explicit_update |
---|
63 | R = -0.5/domain.areas[0] |
---|
64 | |
---|
65 | assert U==R, '%s %s' %(U, R) |
---|
66 | |
---|
67 | |
---|
68 | def test_flux_1_triangle1(self): |
---|
69 | |
---|
70 | a = [0.0, 0.5] |
---|
71 | b = [0.0, 0.0] |
---|
72 | c = [0.5, 0.5] |
---|
73 | |
---|
74 | points = [a, b, c] |
---|
75 | vertices = [ [0,1,2] ] |
---|
76 | domain = Domain(points, vertices) |
---|
77 | domain.check_integrity() |
---|
78 | |
---|
79 | domain.set_quantity('stage', [1.0], 'centroids') |
---|
80 | |
---|
81 | domain.distribute_to_vertices_and_edges() |
---|
82 | domain.check_integrity() |
---|
83 | |
---|
84 | |
---|
85 | domain.compute_fluxes() |
---|
86 | U = -domain.quantities['stage'].explicit_update |
---|
87 | R = 0.5/domain.areas[0] |
---|
88 | |
---|
89 | assert U==R, '%s %s' %(U, R) |
---|
90 | |
---|
91 | |
---|
92 | |
---|
93 | def test_flux_1_triangle2(self): |
---|
94 | |
---|
95 | a = [0.0, 0.5] |
---|
96 | b = [0.0, 0.0] |
---|
97 | c = [0.5, 0.5] |
---|
98 | |
---|
99 | points = [a, b, c] |
---|
100 | vertices = [ [0,1,2] ] |
---|
101 | domain = Domain(points, vertices) |
---|
102 | domain.check_integrity() |
---|
103 | |
---|
104 | |
---|
105 | #Populate boundary array with dirichlet conditions. |
---|
106 | domain.neighbours = array([[-1,-2,-3]]) |
---|
107 | domain.quantities['stage'].boundary_values[0] = 1.0 |
---|
108 | |
---|
109 | domain.distribute_to_vertices_and_edges() #Use first order default |
---|
110 | |
---|
111 | domain.check_integrity() |
---|
112 | |
---|
113 | domain.compute_fluxes() |
---|
114 | U = domain.quantities['stage'].explicit_update |
---|
115 | assert allclose(U, 0) |
---|
116 | |
---|
117 | |
---|
118 | |
---|
119 | |
---|
120 | def test_flux_2_triangles(self): |
---|
121 | """Flow between two triangles |
---|
122 | Check that fluxes have opposite signs |
---|
123 | """ |
---|
124 | |
---|
125 | a = [0.0, 0.5] |
---|
126 | b = [0.0, 0.0] |
---|
127 | c = [0.5, 0.5] |
---|
128 | d = [0.5, 0.0] |
---|
129 | |
---|
130 | points = [a, b, c, d] |
---|
131 | vertices = [ [0,1,2], [3,2,1] ] |
---|
132 | domain = Domain(points, vertices) |
---|
133 | domain.check_integrity() |
---|
134 | |
---|
135 | |
---|
136 | #Populate boundary array with dirichlet conditions. |
---|
137 | domain.neighbours = array([[1,-1,-2], [0,-3,-4]]) |
---|
138 | domain.set_quantity('stage', [1.0, 0.0], 'centroids') |
---|
139 | domain.distribute_to_vertices_and_edges() |
---|
140 | |
---|
141 | domain.compute_fluxes() |
---|
142 | |
---|
143 | X = domain.quantities['stage'].explicit_update |
---|
144 | assert X[0] == -X[1] |
---|
145 | |
---|
146 | |
---|
147 | def test_advection_example(self): |
---|
148 | #Test that system can evolve |
---|
149 | |
---|
150 | from mesh_factory import rectangular |
---|
151 | |
---|
152 | points, vertices, boundary = rectangular(6, 6) |
---|
153 | |
---|
154 | #Create advection domain with direction (1,-1) |
---|
155 | domain = Domain(points, vertices, boundary, velocity=[1.0, -1.0]) |
---|
156 | |
---|
157 | # Initial condition is zero by default |
---|
158 | |
---|
159 | #Boundaries |
---|
160 | T = Transmissive_boundary(domain) |
---|
161 | D = Dirichlet_boundary(array([3.1415])) |
---|
162 | |
---|
163 | domain.set_boundary( {'left': D, 'right': T, 'bottom': T, 'top': T} ) |
---|
164 | domain.check_integrity() |
---|
165 | |
---|
166 | #Check that the boundary value gets propagated to all elements |
---|
167 | for t in domain.evolve(yieldstep = 0.05, finaltime = 10): |
---|
168 | if allclose(domain.quantities['stage'].centroid_values, 3.1415): |
---|
169 | break |
---|
170 | |
---|
171 | assert allclose(domain.quantities['stage'].centroid_values, 3.1415) |
---|
172 | |
---|
173 | |
---|
174 | #------------------------------------------------------------- |
---|
175 | if __name__ == "__main__": |
---|
176 | suite = unittest.makeSuite(Test_Advection,'test') |
---|
177 | runner = unittest.TextTestRunner() |
---|
178 | runner.run(suite) |
---|