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