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.abstract_2d_finite_volumes.domain import Domain |
---|
8 | from anuga.config import epsilon |
---|
9 | |
---|
10 | import numpy as num |
---|
11 | |
---|
12 | |
---|
13 | class Test_Generic_Boundary_Conditions(unittest.TestCase): |
---|
14 | def setUp(self): |
---|
15 | pass |
---|
16 | #print " Setting up" |
---|
17 | |
---|
18 | def tearDown(self): |
---|
19 | pass |
---|
20 | #print " Tearing down" |
---|
21 | |
---|
22 | |
---|
23 | def test_generic(self): |
---|
24 | b = Boundary() |
---|
25 | |
---|
26 | try: |
---|
27 | b.evaluate() |
---|
28 | except: |
---|
29 | pass |
---|
30 | else: |
---|
31 | raise 'Should have raised exception' |
---|
32 | |
---|
33 | |
---|
34 | def test_dirichlet_empty(self): |
---|
35 | |
---|
36 | try: |
---|
37 | Bd = Dirichlet_boundary() |
---|
38 | except: |
---|
39 | pass |
---|
40 | else: |
---|
41 | raise 'Should have raised exception' |
---|
42 | |
---|
43 | def test_dirichlet(self): |
---|
44 | x = [3.14,0,0.1] |
---|
45 | Bd = Dirichlet_boundary(x) |
---|
46 | |
---|
47 | q = Bd.evaluate() |
---|
48 | assert num.allclose(q, x) |
---|
49 | |
---|
50 | |
---|
51 | def test_time(self): |
---|
52 | from quantity import Quantity |
---|
53 | |
---|
54 | a = [0.0, 0.0] |
---|
55 | b = [0.0, 2.0] |
---|
56 | c = [2.0,0.0] |
---|
57 | d = [0.0, 4.0] |
---|
58 | e = [2.0, 2.0] |
---|
59 | f = [4.0,0.0] |
---|
60 | |
---|
61 | points = [a, b, c, d, e, f] |
---|
62 | |
---|
63 | #bac, bce, ecf, dbe |
---|
64 | elements = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ] |
---|
65 | |
---|
66 | domain = Domain(points, elements) |
---|
67 | domain.check_integrity() |
---|
68 | |
---|
69 | domain.conserved_quantities = ['stage', 'ymomentum'] |
---|
70 | domain.evolved_quantities = ['stage', 'ymomentum'] |
---|
71 | domain.quantities['stage'] =\ |
---|
72 | Quantity(domain, [[1,2,3], [5,5,5], |
---|
73 | [0,0,9], [-6, 3, 3]]) |
---|
74 | |
---|
75 | domain.quantities['ymomentum'] =\ |
---|
76 | Quantity(domain, [[2,3,4], [5,5,5], |
---|
77 | [0,0,9], [-6, 3, 3]]) |
---|
78 | |
---|
79 | |
---|
80 | domain.check_integrity() |
---|
81 | |
---|
82 | #Test time bdry, you need to provide a domain and function |
---|
83 | try: |
---|
84 | T = Time_boundary(domain) |
---|
85 | except: |
---|
86 | pass |
---|
87 | else: |
---|
88 | raise 'Should have raised exception' |
---|
89 | |
---|
90 | #Test time bdry, you need to provide a function |
---|
91 | try: |
---|
92 | T = Time_boundary() |
---|
93 | except: |
---|
94 | pass |
---|
95 | else: |
---|
96 | raise 'Should have raised exception' |
---|
97 | |
---|
98 | |
---|
99 | def function(t): |
---|
100 | return [1.0, 0.0] |
---|
101 | |
---|
102 | T = Time_boundary(domain, function) |
---|
103 | |
---|
104 | from anuga.config import default_boundary_tag |
---|
105 | domain.set_boundary( {default_boundary_tag: T} ) |
---|
106 | |
---|
107 | |
---|
108 | #FIXME: should not necessarily be true always. |
---|
109 | #E.g. with None as a boundary object. |
---|
110 | assert len(domain.boundary) == len(domain.boundary_objects) |
---|
111 | |
---|
112 | q = T.evaluate(0, 2) #Vol=0, edge=2 |
---|
113 | |
---|
114 | assert num.allclose(q, [1.0, 0.0]) |
---|
115 | |
---|
116 | |
---|
117 | def test_time_space_boundary(self): |
---|
118 | from quantity import Quantity |
---|
119 | |
---|
120 | a = [0.0, 0.0] |
---|
121 | b = [0.0, 2.0] |
---|
122 | c = [2.0,0.0] |
---|
123 | d = [0.0, 4.0] |
---|
124 | e = [2.0, 2.0] |
---|
125 | f = [4.0,0.0] |
---|
126 | |
---|
127 | points = [a, b, c, d, e, f] |
---|
128 | |
---|
129 | #bac, bce, ecf, dbe |
---|
130 | elements = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ] |
---|
131 | |
---|
132 | domain = Domain(points, elements) |
---|
133 | domain.check_integrity() |
---|
134 | |
---|
135 | domain.conserved_quantities = ['stage', 'ymomentum'] |
---|
136 | domain.evolved_quantities = ['stage', 'ymomentum'] |
---|
137 | domain.quantities['stage'] =\ |
---|
138 | Quantity(domain, [[1,2,3], [5,5,5], |
---|
139 | [0,0,9], [-6, 3, 3]]) |
---|
140 | |
---|
141 | domain.quantities['ymomentum'] =\ |
---|
142 | Quantity(domain, [[2,3,4], [5,5,5], |
---|
143 | [0,0,9], [-6, 3, 3]]) |
---|
144 | |
---|
145 | |
---|
146 | domain.check_integrity() |
---|
147 | |
---|
148 | #Test time space bdry, you need to provide a domain and function |
---|
149 | try: |
---|
150 | T = Time_space_boundary(domain) |
---|
151 | except: |
---|
152 | pass |
---|
153 | else: |
---|
154 | raise 'Should have raised exception' |
---|
155 | |
---|
156 | #Test time bdry, you need to provide a function |
---|
157 | try: |
---|
158 | T = Time_space_boundary() |
---|
159 | except: |
---|
160 | pass |
---|
161 | else: |
---|
162 | raise 'Should have raised exception' |
---|
163 | |
---|
164 | |
---|
165 | def function(t,x,y): |
---|
166 | return [x,y] |
---|
167 | |
---|
168 | T = Time_space_boundary(domain, function) |
---|
169 | |
---|
170 | from anuga.config import default_boundary_tag |
---|
171 | domain.set_boundary( {default_boundary_tag: T} ) |
---|
172 | |
---|
173 | |
---|
174 | #FIXME: should not necessarily be true always. |
---|
175 | #E.g. with None as a boundary object. |
---|
176 | assert len(domain.boundary) == len(domain.boundary_objects) |
---|
177 | |
---|
178 | q = T.evaluate(0, 2) #Vol=0, edge=2 |
---|
179 | assert num.allclose(q, domain.get_edge_midpoint_coordinate(0,2)) |
---|
180 | |
---|
181 | |
---|
182 | q = T.evaluate(1, 1) #Vol=1, edge=1 |
---|
183 | assert num.allclose(q, domain.get_edge_midpoint_coordinate(1,1)) |
---|
184 | |
---|
185 | |
---|
186 | |
---|
187 | |
---|
188 | |
---|
189 | def test_transmissive(self): |
---|
190 | from quantity import Quantity |
---|
191 | |
---|
192 | a = [0.0, 0.0] |
---|
193 | b = [0.0, 2.0] |
---|
194 | c = [2.0,0.0] |
---|
195 | d = [0.0, 4.0] |
---|
196 | e = [2.0, 2.0] |
---|
197 | f = [4.0,0.0] |
---|
198 | |
---|
199 | points = [a, b, c, d, e, f] |
---|
200 | |
---|
201 | #bac, bce, ecf, dbe |
---|
202 | elements = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ] |
---|
203 | |
---|
204 | domain = Domain(points, elements) |
---|
205 | domain.check_integrity() |
---|
206 | |
---|
207 | domain.conserved_quantities = ['stage', 'ymomentum'] |
---|
208 | domain.evolved_quantities = ['stage', 'ymomentum'] |
---|
209 | domain.quantities['stage'] =\ |
---|
210 | Quantity(domain, [[1,2,3], [5,5,5], |
---|
211 | [0,0,9], [-6, 3, 3]]) |
---|
212 | |
---|
213 | domain.quantities['ymomentum'] =\ |
---|
214 | Quantity(domain, [[2,3,4], [5,5,5], |
---|
215 | [0,0,9], [-6, 3, 3]]) |
---|
216 | |
---|
217 | |
---|
218 | domain.check_integrity() |
---|
219 | |
---|
220 | #Test transmissve bdry |
---|
221 | try: |
---|
222 | T = Transmissive_boundary() |
---|
223 | except: |
---|
224 | pass |
---|
225 | else: |
---|
226 | raise 'Should have raised exception' |
---|
227 | |
---|
228 | T = Transmissive_boundary(domain) |
---|
229 | |
---|
230 | from anuga.config import default_boundary_tag |
---|
231 | domain.set_boundary( {default_boundary_tag: T} ) |
---|
232 | |
---|
233 | |
---|
234 | #FIXME: should not necessarily be true always. |
---|
235 | #E.g. with None as a boundary object. |
---|
236 | assert len(domain.boundary) == len(domain.boundary_objects) |
---|
237 | |
---|
238 | q = T.evaluate(0, 2) #Vol=0, edge=2 |
---|
239 | |
---|
240 | assert num.allclose(q, [1.5, 2.5]) |
---|
241 | |
---|
242 | |
---|
243 | # Now set the centroid_transmissive_bc flag to true |
---|
244 | domain.set_centroid_transmissive_bc(True) |
---|
245 | |
---|
246 | q = T.evaluate(0, 2) #Vol=0, edge=2 |
---|
247 | |
---|
248 | assert num.allclose(q, [2.0 ,3.0]) # centroid value |
---|
249 | |
---|
250 | |
---|
251 | |
---|
252 | |
---|
253 | |
---|
254 | def NOtest_fileboundary_time_only(self): |
---|
255 | """Test that boundary values can be read from file and interpolated |
---|
256 | This is using the .tms file format |
---|
257 | |
---|
258 | See also test_util for comprenhensive testing of the underlying |
---|
259 | file_function and also tests in test_datamanager which tests |
---|
260 | file_function using the sts format |
---|
261 | """ |
---|
262 | #FIXME (Ole): This test was disabled 18 August 2008 as no |
---|
263 | # need for this was found. Rather I implemented an Exception |
---|
264 | # to catch possible errors in the model setup |
---|
265 | |
---|
266 | from quantity import Quantity |
---|
267 | import time, os |
---|
268 | from math import sin, pi |
---|
269 | from anuga.config import time_format |
---|
270 | |
---|
271 | a = [0.0, 0.0] |
---|
272 | b = [0.0, 2.0] |
---|
273 | c = [2.0, 0.0] |
---|
274 | d = [0.0, 4.0] |
---|
275 | e = [2.0, 2.0] |
---|
276 | f = [4.0, 0.0] |
---|
277 | |
---|
278 | points = [a, b, c, d, e, f] |
---|
279 | |
---|
280 | #bac, bce, ecf, dbe |
---|
281 | elements = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ] |
---|
282 | |
---|
283 | domain = Domain(points, elements) |
---|
284 | domain.conserved_quantities = ['stage', 'ymomentum'] |
---|
285 | domain.quantities['stage'] =\ |
---|
286 | Quantity(domain, [[1,2,3], [5,5,5], |
---|
287 | [0,0,9], [-6, 3, 3]]) |
---|
288 | |
---|
289 | domain.quantities['ymomentum'] =\ |
---|
290 | Quantity(domain, [[2,3,4], [5,5,5], |
---|
291 | [0,0,9], [-6, 3, 3]]) |
---|
292 | |
---|
293 | domain.check_integrity() |
---|
294 | |
---|
295 | |
---|
296 | #Write file |
---|
297 | filename = 'boundarytest' + str(time.time()) |
---|
298 | fid = open(filename + '.txt', 'w') |
---|
299 | start = time.mktime(time.strptime('2000', '%Y')) |
---|
300 | dt = 5*60 #Five minute intervals |
---|
301 | for i in range(10): |
---|
302 | t = start + i*dt |
---|
303 | t_string = time.strftime(time_format, time.gmtime(t)) |
---|
304 | |
---|
305 | fid.write('%s,%f %f\n' %(t_string, 1.0*i, sin(i*2*pi/10))) |
---|
306 | fid.close() |
---|
307 | |
---|
308 | |
---|
309 | #Convert ASCII file to NetCDF (Which is what we really like!) |
---|
310 | |
---|
311 | from anuga.shallow_water.data_manager import timefile2netcdf |
---|
312 | |
---|
313 | timefile2netcdf(filename, quantity_names = ['stage', 'ymomentum']) |
---|
314 | |
---|
315 | |
---|
316 | |
---|
317 | F = File_boundary(filename + '.tms', domain) |
---|
318 | |
---|
319 | |
---|
320 | os.remove(filename + '.txt') |
---|
321 | os.remove(filename + '.tms') |
---|
322 | |
---|
323 | |
---|
324 | |
---|
325 | |
---|
326 | #Check that midpoint coordinates at boundary are correctly computed |
---|
327 | assert num.allclose( F.midpoint_coordinates, |
---|
328 | [[1.0, 0.0], [0.0, 1.0], [3.0, 0.0], |
---|
329 | [3.0, 1.0], [1.0, 3.0], [0.0, 3.0]]) |
---|
330 | |
---|
331 | #assert allclose(F.midpoint_coordinates[(3,2)], [0.0, 3.0]) |
---|
332 | #assert allclose(F.midpoint_coordinates[(3,1)], [1.0, 3.0]) |
---|
333 | #assert allclose(F.midpoint_coordinates[(0,2)], [0.0, 1.0]) |
---|
334 | #assert allclose(F.midpoint_coordinates[(0,0)], [1.0, 0.0]) |
---|
335 | #assert allclose(F.midpoint_coordinates[(2,0)], [3.0, 0.0]) |
---|
336 | #assert allclose(F.midpoint_coordinates[(2,1)], [3.0, 1.0]) |
---|
337 | |
---|
338 | |
---|
339 | #Check time interpolation |
---|
340 | from anuga.config import default_boundary_tag |
---|
341 | domain.set_boundary( {default_boundary_tag: F} ) |
---|
342 | |
---|
343 | domain.time = 5*30/2 #A quarter way through first step |
---|
344 | q = F.evaluate() |
---|
345 | assert num.allclose(q, [1.0/4, sin(2*pi/10)/4]) |
---|
346 | |
---|
347 | |
---|
348 | domain.time = 2.5*5*60 #Half way between steps 2 and 3 |
---|
349 | q = F.evaluate() |
---|
350 | assert num.allclose(q, [2.5, (sin(2*2*pi/10) + sin(3*2*pi/10))/2]) |
---|
351 | |
---|
352 | |
---|
353 | |
---|
354 | def test_fileboundary_exception(self): |
---|
355 | """Test that boundary object complains if number of |
---|
356 | conserved quantities are wrong |
---|
357 | """ |
---|
358 | |
---|
359 | from quantity import Quantity |
---|
360 | import time, os |
---|
361 | from math import sin, pi |
---|
362 | from anuga.config import time_format |
---|
363 | |
---|
364 | a = [0.0, 0.0] |
---|
365 | b = [0.0, 2.0] |
---|
366 | c = [2.0,0.0] |
---|
367 | d = [0.0, 4.0] |
---|
368 | e = [2.0, 2.0] |
---|
369 | f = [4.0,0.0] |
---|
370 | |
---|
371 | points = [a, b, c, d, e, f] |
---|
372 | |
---|
373 | #bac, bce, ecf, dbe |
---|
374 | elements = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ] |
---|
375 | |
---|
376 | domain = Domain(points, elements) |
---|
377 | domain.conserved_quantities = ['stage', 'xmomentum', 'ymomentum'] |
---|
378 | domain.evolved_quantities = ['stage', 'xmomentum', 'ymomentum'] |
---|
379 | domain.quantities['stage'] =\ |
---|
380 | Quantity(domain, [[1,2,3], [5,5,5], |
---|
381 | [0,0,9], [-6, 3, 3]]) |
---|
382 | |
---|
383 | domain.quantities['xmomentum'] =\ |
---|
384 | Quantity(domain, [[2,3,4], [5,5,5], |
---|
385 | [0,0,9], [-6, 3, 3]]) |
---|
386 | domain.quantities['ymomentum'] =\ |
---|
387 | Quantity(domain, [[2,3,4], [5,5,5], |
---|
388 | [0,0,9], [-6, 3, 3]]) |
---|
389 | |
---|
390 | domain.check_integrity() |
---|
391 | |
---|
392 | #Write file (with only two values) |
---|
393 | filename = 'boundarytest' + str(time.time()) |
---|
394 | fid = open(filename + '.txt', 'w') |
---|
395 | start = time.mktime(time.strptime('2000', '%Y')) |
---|
396 | dt = 5*60 #Five minute intervals |
---|
397 | for i in range(10): |
---|
398 | t = start + i*dt |
---|
399 | t_string = time.strftime(time_format, time.gmtime(t)) |
---|
400 | |
---|
401 | fid.write('%s,%f %f\n' %(t_string, 1.0*i, sin(i*2*pi/10))) |
---|
402 | fid.close() |
---|
403 | |
---|
404 | |
---|
405 | #Convert ASCII file to NetCDF (Which is what we really like!) |
---|
406 | from anuga.shallow_water.data_manager import timefile2netcdf |
---|
407 | |
---|
408 | timefile2netcdf(filename, quantity_names = ['stage', 'xmomentum']) |
---|
409 | |
---|
410 | |
---|
411 | try: |
---|
412 | F = File_boundary(filename + '.tms', |
---|
413 | domain) |
---|
414 | except: |
---|
415 | pass |
---|
416 | else: |
---|
417 | raise 'Should have raised an exception' |
---|
418 | |
---|
419 | os.remove(filename + '.txt') |
---|
420 | os.remove(filename + '.tms') |
---|
421 | |
---|
422 | |
---|
423 | #------------------------------------------------------------- |
---|
424 | |
---|
425 | if __name__ == "__main__": |
---|
426 | suite = unittest.makeSuite(Test_Generic_Boundary_Conditions, 'test') |
---|
427 | runner = unittest.TextTestRunner() |
---|
428 | runner.run(suite) |
---|