1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | import unittest |
---|
4 | from math import sqrt, pi |
---|
5 | |
---|
6 | from generic_boundary_conditions import * |
---|
7 | from anuga.config import epsilon |
---|
8 | from Numeric import allclose, array |
---|
9 | |
---|
10 | |
---|
11 | class Test_Generic_Boundary_Conditions(unittest.TestCase): |
---|
12 | def setUp(self): |
---|
13 | pass |
---|
14 | #print " Setting up" |
---|
15 | |
---|
16 | def tearDown(self): |
---|
17 | pass |
---|
18 | #print " Tearing down" |
---|
19 | |
---|
20 | |
---|
21 | def test_generic(self): |
---|
22 | b = Boundary() |
---|
23 | |
---|
24 | try: |
---|
25 | b.evaluate() |
---|
26 | except: |
---|
27 | pass |
---|
28 | else: |
---|
29 | raise 'Should have raised exception' |
---|
30 | |
---|
31 | |
---|
32 | def test_dirichlet_empty(self): |
---|
33 | |
---|
34 | try: |
---|
35 | Bd = Dirichlet_boundary() |
---|
36 | except: |
---|
37 | pass |
---|
38 | else: |
---|
39 | raise 'Should have raised exception' |
---|
40 | |
---|
41 | def test_dirichlet(self): |
---|
42 | x = [3.14,0,0.1] |
---|
43 | Bd = Dirichlet_boundary(x) |
---|
44 | |
---|
45 | q = Bd.evaluate() |
---|
46 | assert allclose(q, x) |
---|
47 | |
---|
48 | |
---|
49 | def test_transmissive(self): |
---|
50 | from domain import Domain |
---|
51 | from quantity import Quantity |
---|
52 | |
---|
53 | a = [0.0, 0.0] |
---|
54 | b = [0.0, 2.0] |
---|
55 | c = [2.0,0.0] |
---|
56 | d = [0.0, 4.0] |
---|
57 | e = [2.0, 2.0] |
---|
58 | f = [4.0,0.0] |
---|
59 | |
---|
60 | points = [a, b, c, d, e, f] |
---|
61 | |
---|
62 | #bac, bce, ecf, dbe |
---|
63 | elements = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ] |
---|
64 | |
---|
65 | domain = Domain(points, elements) |
---|
66 | domain.check_integrity() |
---|
67 | |
---|
68 | domain.conserved_quantities = ['stage', 'ymomentum'] |
---|
69 | domain.quantities['stage'] =\ |
---|
70 | Quantity(domain, [[1,2,3], [5,5,5], |
---|
71 | [0,0,9], [-6, 3, 3]]) |
---|
72 | |
---|
73 | domain.quantities['ymomentum'] =\ |
---|
74 | Quantity(domain, [[2,3,4], [5,5,5], |
---|
75 | [0,0,9], [-6, 3, 3]]) |
---|
76 | |
---|
77 | |
---|
78 | domain.check_integrity() |
---|
79 | |
---|
80 | #Test transmissve bdry |
---|
81 | try: |
---|
82 | T = Transmissive_boundary() |
---|
83 | except: |
---|
84 | pass |
---|
85 | else: |
---|
86 | raise 'Should have raised exception' |
---|
87 | |
---|
88 | T = Transmissive_boundary(domain) |
---|
89 | |
---|
90 | from anuga.config import default_boundary_tag |
---|
91 | domain.set_boundary( {default_boundary_tag: T} ) |
---|
92 | |
---|
93 | |
---|
94 | #FIXME: should not necessarily be true always. |
---|
95 | #E.g. with None as a boundary object. |
---|
96 | assert len(domain.boundary) == len(domain.boundary_objects) |
---|
97 | |
---|
98 | q = T.evaluate(0, 2) #Vol=0, edge=2 |
---|
99 | |
---|
100 | assert allclose(q, [1.5, 2.5]) |
---|
101 | |
---|
102 | |
---|
103 | def test_fileboundary_time_only(self): |
---|
104 | """Test that boundary values can be read from file and interpolated |
---|
105 | This is using the .tms file format |
---|
106 | """ |
---|
107 | |
---|
108 | from domain import Domain |
---|
109 | from quantity import Quantity |
---|
110 | import time, os |
---|
111 | from math import sin, pi |
---|
112 | from anuga.config import time_format |
---|
113 | |
---|
114 | a = [0.0, 0.0] |
---|
115 | b = [0.0, 2.0] |
---|
116 | c = [2.0, 0.0] |
---|
117 | d = [0.0, 4.0] |
---|
118 | e = [2.0, 2.0] |
---|
119 | f = [4.0, 0.0] |
---|
120 | |
---|
121 | points = [a, b, c, d, e, f] |
---|
122 | |
---|
123 | #bac, bce, ecf, dbe |
---|
124 | elements = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ] |
---|
125 | |
---|
126 | domain = Domain(points, elements) |
---|
127 | domain.conserved_quantities = ['stage', 'ymomentum'] |
---|
128 | domain.quantities['stage'] =\ |
---|
129 | Quantity(domain, [[1,2,3], [5,5,5], |
---|
130 | [0,0,9], [-6, 3, 3]]) |
---|
131 | |
---|
132 | domain.quantities['ymomentum'] =\ |
---|
133 | Quantity(domain, [[2,3,4], [5,5,5], |
---|
134 | [0,0,9], [-6, 3, 3]]) |
---|
135 | |
---|
136 | domain.check_integrity() |
---|
137 | |
---|
138 | |
---|
139 | #Write file |
---|
140 | filename = 'boundarytest' + str(time.time()) |
---|
141 | fid = open(filename + '.txt', 'w') |
---|
142 | start = time.mktime(time.strptime('2000', '%Y')) |
---|
143 | dt = 5*60 #Five minute intervals |
---|
144 | for i in range(10): |
---|
145 | t = start + i*dt |
---|
146 | t_string = time.strftime(time_format, time.gmtime(t)) |
---|
147 | |
---|
148 | fid.write('%s,%f %f\n' %(t_string, 1.0*i, sin(i*2*pi/10))) |
---|
149 | fid.close() |
---|
150 | |
---|
151 | |
---|
152 | #Convert ASCII file to NetCDF (Which is what we really like!) |
---|
153 | |
---|
154 | from anuga.shallow_water.data_manager import timefile2netcdf |
---|
155 | |
---|
156 | timefile2netcdf(filename, quantity_names = ['stage', 'ymomentum']) |
---|
157 | |
---|
158 | |
---|
159 | |
---|
160 | F = File_boundary(filename + '.tms', domain) |
---|
161 | |
---|
162 | |
---|
163 | os.remove(filename + '.txt') |
---|
164 | os.remove(filename + '.tms') |
---|
165 | |
---|
166 | |
---|
167 | |
---|
168 | |
---|
169 | #Check that midpoint coordinates at boundary are correctly computed |
---|
170 | assert allclose( F.midpoint_coordinates, |
---|
171 | [[1.0, 0.0], [0.0, 1.0], [3.0, 0.0], |
---|
172 | [3.0, 1.0], [1.0, 3.0], [0.0, 3.0]]) |
---|
173 | |
---|
174 | #assert allclose(F.midpoint_coordinates[(3,2)], [0.0, 3.0]) |
---|
175 | #assert allclose(F.midpoint_coordinates[(3,1)], [1.0, 3.0]) |
---|
176 | #assert allclose(F.midpoint_coordinates[(0,2)], [0.0, 1.0]) |
---|
177 | #assert allclose(F.midpoint_coordinates[(0,0)], [1.0, 0.0]) |
---|
178 | #assert allclose(F.midpoint_coordinates[(2,0)], [3.0, 0.0]) |
---|
179 | #assert allclose(F.midpoint_coordinates[(2,1)], [3.0, 1.0]) |
---|
180 | |
---|
181 | |
---|
182 | #Check time interpolation |
---|
183 | from anuga.config import default_boundary_tag |
---|
184 | domain.set_boundary( {default_boundary_tag: F} ) |
---|
185 | |
---|
186 | domain.time = 5*30/2 #A quarter way through first step |
---|
187 | q = F.evaluate() |
---|
188 | assert allclose(q, [1.0/4, sin(2*pi/10)/4]) |
---|
189 | |
---|
190 | |
---|
191 | domain.time = 2.5*5*60 #Half way between steps 2 and 3 |
---|
192 | q = F.evaluate() |
---|
193 | assert allclose(q, [2.5, (sin(2*2*pi/10) + sin(3*2*pi/10))/2]) |
---|
194 | |
---|
195 | |
---|
196 | |
---|
197 | def test_fileboundary_exception(self): |
---|
198 | """Test that boundary object complains if number of |
---|
199 | conserved quantities are wrong |
---|
200 | """ |
---|
201 | |
---|
202 | from domain import Domain |
---|
203 | from quantity import Quantity |
---|
204 | import time, os |
---|
205 | from math import sin, pi |
---|
206 | from anuga.config import time_format |
---|
207 | |
---|
208 | a = [0.0, 0.0] |
---|
209 | b = [0.0, 2.0] |
---|
210 | c = [2.0,0.0] |
---|
211 | d = [0.0, 4.0] |
---|
212 | e = [2.0, 2.0] |
---|
213 | f = [4.0,0.0] |
---|
214 | |
---|
215 | points = [a, b, c, d, e, f] |
---|
216 | |
---|
217 | #bac, bce, ecf, dbe |
---|
218 | elements = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ] |
---|
219 | |
---|
220 | domain = Domain(points, elements) |
---|
221 | domain.conserved_quantities = ['stage', 'xmomentum', 'ymomentum'] |
---|
222 | domain.quantities['stage'] =\ |
---|
223 | Quantity(domain, [[1,2,3], [5,5,5], |
---|
224 | [0,0,9], [-6, 3, 3]]) |
---|
225 | |
---|
226 | domain.quantities['xmomentum'] =\ |
---|
227 | Quantity(domain, [[2,3,4], [5,5,5], |
---|
228 | [0,0,9], [-6, 3, 3]]) |
---|
229 | domain.quantities['ymomentum'] =\ |
---|
230 | Quantity(domain, [[2,3,4], [5,5,5], |
---|
231 | [0,0,9], [-6, 3, 3]]) |
---|
232 | |
---|
233 | domain.check_integrity() |
---|
234 | |
---|
235 | #Write file (with only two values) |
---|
236 | filename = 'boundarytest' + str(time.time()) |
---|
237 | fid = open(filename + '.txt', 'w') |
---|
238 | start = time.mktime(time.strptime('2000', '%Y')) |
---|
239 | dt = 5*60 #Five minute intervals |
---|
240 | for i in range(10): |
---|
241 | t = start + i*dt |
---|
242 | t_string = time.strftime(time_format, time.gmtime(t)) |
---|
243 | |
---|
244 | fid.write('%s,%f %f\n' %(t_string, 1.0*i, sin(i*2*pi/10))) |
---|
245 | fid.close() |
---|
246 | |
---|
247 | |
---|
248 | #Convert ASCII file to NetCDF (Which is what we really like!) |
---|
249 | from anuga.shallow_water.data_manager import timefile2netcdf |
---|
250 | |
---|
251 | timefile2netcdf(filename, quantity_names = ['stage', 'xmomentum']) |
---|
252 | |
---|
253 | |
---|
254 | try: |
---|
255 | F = File_boundary(filename + '.tms', |
---|
256 | domain) |
---|
257 | except: |
---|
258 | pass |
---|
259 | else: |
---|
260 | raise 'Should have raised an exception' |
---|
261 | |
---|
262 | os.remove(filename + '.txt') |
---|
263 | os.remove(filename + '.tms') |
---|
264 | |
---|
265 | |
---|
266 | |
---|
267 | |
---|
268 | |
---|
269 | #------------------------------------------------------------- |
---|
270 | if __name__ == "__main__": |
---|
271 | suite = unittest.makeSuite(Test_Generic_Boundary_Conditions,'test') |
---|
272 | runner = unittest.TextTestRunner() |
---|
273 | runner.run(suite) |
---|
274 | |
---|
275 | |
---|
276 | |
---|