1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | |
---|
4 | import unittest |
---|
5 | from Numeric import zeros, array, allclose, Float |
---|
6 | from math import sqrt, pi |
---|
7 | import tempfile, os |
---|
8 | from os import access, F_OK,sep, removedirs,remove,mkdir,getcwd |
---|
9 | |
---|
10 | from anuga.abstract_2d_finite_volumes.util import * |
---|
11 | from anuga.config import epsilon |
---|
12 | from anuga.shallow_water.data_manager import timefile2netcdf,del_dir |
---|
13 | |
---|
14 | from anuga.utilities.numerical_tools import NAN |
---|
15 | |
---|
16 | from sys import platform |
---|
17 | |
---|
18 | def test_function(x, y): |
---|
19 | return x+y |
---|
20 | |
---|
21 | class Test_Util(unittest.TestCase): |
---|
22 | def setUp(self): |
---|
23 | pass |
---|
24 | |
---|
25 | def tearDown(self): |
---|
26 | pass |
---|
27 | |
---|
28 | |
---|
29 | |
---|
30 | |
---|
31 | #Geometric |
---|
32 | #def test_distance(self): |
---|
33 | # from anuga.abstract_2d_finite_volumes.util import distance# |
---|
34 | # |
---|
35 | # self.failUnless( distance([4,2],[7,6]) == 5.0, |
---|
36 | # 'Distance is wrong!') |
---|
37 | # self.failUnless( allclose(distance([7,6],[9,8]), 2.82842712475), |
---|
38 | # 'distance is wrong!') |
---|
39 | # self.failUnless( allclose(distance([9,8],[4,2]), 7.81024967591), |
---|
40 | # 'distance is wrong!') |
---|
41 | # |
---|
42 | # self.failUnless( distance([9,8],[4,2]) == distance([4,2],[9,8]), |
---|
43 | # 'distance is wrong!') |
---|
44 | |
---|
45 | |
---|
46 | def test_file_function_time1(self): |
---|
47 | """Test that File function interpolates correctly |
---|
48 | between given times. No x,y dependency here. |
---|
49 | """ |
---|
50 | |
---|
51 | #Write file |
---|
52 | import os, time |
---|
53 | from anuga.config import time_format |
---|
54 | from math import sin, pi |
---|
55 | |
---|
56 | #Typical ASCII file |
---|
57 | finaltime = 1200 |
---|
58 | filename = 'test_file_function' |
---|
59 | fid = open(filename + '.txt', 'w') |
---|
60 | start = time.mktime(time.strptime('2000', '%Y')) |
---|
61 | dt = 60 #One minute intervals |
---|
62 | t = 0.0 |
---|
63 | while t <= finaltime: |
---|
64 | t_string = time.strftime(time_format, time.gmtime(t+start)) |
---|
65 | fid.write('%s, %f %f %f\n' %(t_string, 2*t, t**2, sin(t*pi/600))) |
---|
66 | t += dt |
---|
67 | |
---|
68 | fid.close() |
---|
69 | |
---|
70 | #Convert ASCII file to NetCDF (Which is what we really like!) |
---|
71 | timefile2netcdf(filename) |
---|
72 | |
---|
73 | |
---|
74 | #Create file function from time series |
---|
75 | F = file_function(filename + '.tms', |
---|
76 | quantities = ['Attribute0', |
---|
77 | 'Attribute1', |
---|
78 | 'Attribute2']) |
---|
79 | |
---|
80 | #Now try interpolation |
---|
81 | for i in range(20): |
---|
82 | t = i*10 |
---|
83 | q = F(t) |
---|
84 | |
---|
85 | #Exact linear intpolation |
---|
86 | assert allclose(q[0], 2*t) |
---|
87 | if i%6 == 0: |
---|
88 | assert allclose(q[1], t**2) |
---|
89 | assert allclose(q[2], sin(t*pi/600)) |
---|
90 | |
---|
91 | #Check non-exact |
---|
92 | |
---|
93 | t = 90 #Halfway between 60 and 120 |
---|
94 | q = F(t) |
---|
95 | assert allclose( (120**2 + 60**2)/2, q[1] ) |
---|
96 | assert allclose( (sin(120*pi/600) + sin(60*pi/600))/2, q[2] ) |
---|
97 | |
---|
98 | |
---|
99 | t = 100 #Two thirds of the way between between 60 and 120 |
---|
100 | q = F(t) |
---|
101 | assert allclose( 2*120**2/3 + 60**2/3, q[1] ) |
---|
102 | assert allclose( 2*sin(120*pi/600)/3 + sin(60*pi/600)/3, q[2] ) |
---|
103 | |
---|
104 | os.remove(filename + '.txt') |
---|
105 | os.remove(filename + '.tms') |
---|
106 | |
---|
107 | |
---|
108 | |
---|
109 | def test_spatio_temporal_file_function_basic(self): |
---|
110 | """Test that spatio temporal file function performs the correct |
---|
111 | interpolations in both time and space |
---|
112 | NetCDF version (x,y,t dependency) |
---|
113 | """ |
---|
114 | import time |
---|
115 | |
---|
116 | #Create sww file of simple propagation from left to right |
---|
117 | #through rectangular domain |
---|
118 | from shallow_water import Domain, Dirichlet_boundary |
---|
119 | from mesh_factory import rectangular |
---|
120 | from Numeric import take, concatenate, reshape |
---|
121 | |
---|
122 | #Create basic mesh and shallow water domain |
---|
123 | points, vertices, boundary = rectangular(3, 3) |
---|
124 | domain1 = Domain(points, vertices, boundary) |
---|
125 | |
---|
126 | from anuga.utilities.numerical_tools import mean |
---|
127 | domain1.reduction = mean |
---|
128 | domain1.smooth = True #NOTE: Mimic sww output where each vertex has |
---|
129 | # only one value. |
---|
130 | |
---|
131 | domain1.default_order = 2 |
---|
132 | domain1.store = True |
---|
133 | domain1.set_datadir('.') |
---|
134 | domain1.set_name('spatio_temporal_boundary_source_%d' %(id(self))) |
---|
135 | domain1.quantities_to_be_stored = ['stage', 'xmomentum', 'ymomentum'] |
---|
136 | |
---|
137 | #Bed-slope, friction and IC at vertices (and interpolated elsewhere) |
---|
138 | domain1.set_quantity('elevation', 0) |
---|
139 | domain1.set_quantity('friction', 0) |
---|
140 | domain1.set_quantity('stage', 0) |
---|
141 | |
---|
142 | # Boundary conditions |
---|
143 | B0 = Dirichlet_boundary([0,0,0]) |
---|
144 | B6 = Dirichlet_boundary([0.6,0,0]) |
---|
145 | domain1.set_boundary({'left': B6, 'top': B6, 'right': B0, 'bottom': B0}) |
---|
146 | domain1.check_integrity() |
---|
147 | |
---|
148 | finaltime = 8 |
---|
149 | #Evolution |
---|
150 | t0 = -1 |
---|
151 | for t in domain1.evolve(yieldstep = 0.1, finaltime = finaltime): |
---|
152 | #print 'Timesteps: %.16f, %.16f' %(t0, t) |
---|
153 | #if t == t0: |
---|
154 | # msg = 'Duplicate timestep found: %f, %f' %(t0, t) |
---|
155 | # raise msg |
---|
156 | t0 = t |
---|
157 | |
---|
158 | #domain1.write_time() |
---|
159 | |
---|
160 | |
---|
161 | #Now read data from sww and check |
---|
162 | from Scientific.IO.NetCDF import NetCDFFile |
---|
163 | filename = domain1.get_name() + '.' + domain1.format |
---|
164 | fid = NetCDFFile(filename) |
---|
165 | |
---|
166 | x = fid.variables['x'][:] |
---|
167 | y = fid.variables['y'][:] |
---|
168 | stage = fid.variables['stage'][:] |
---|
169 | xmomentum = fid.variables['xmomentum'][:] |
---|
170 | ymomentum = fid.variables['ymomentum'][:] |
---|
171 | time = fid.variables['time'][:] |
---|
172 | |
---|
173 | #Take stage vertex values at last timestep on diagonal |
---|
174 | #Diagonal is identified by vertices: 0, 5, 10, 15 |
---|
175 | |
---|
176 | last_time_index = len(time)-1 #Last last_time_index |
---|
177 | d_stage = reshape(take(stage[last_time_index, :], [0,5,10,15]), (4,1)) |
---|
178 | d_uh = reshape(take(xmomentum[last_time_index, :], [0,5,10,15]), (4,1)) |
---|
179 | d_vh = reshape(take(ymomentum[last_time_index, :], [0,5,10,15]), (4,1)) |
---|
180 | D = concatenate( (d_stage, d_uh, d_vh), axis=1) |
---|
181 | |
---|
182 | #Reference interpolated values at midpoints on diagonal at |
---|
183 | #this timestep are |
---|
184 | r0 = (D[0] + D[1])/2 |
---|
185 | r1 = (D[1] + D[2])/2 |
---|
186 | r2 = (D[2] + D[3])/2 |
---|
187 | |
---|
188 | #And the midpoints are found now |
---|
189 | Dx = take(reshape(x, (16,1)), [0,5,10,15]) |
---|
190 | Dy = take(reshape(y, (16,1)), [0,5,10,15]) |
---|
191 | |
---|
192 | diag = concatenate( (Dx, Dy), axis=1) |
---|
193 | d_midpoints = (diag[1:] + diag[:-1])/2 |
---|
194 | |
---|
195 | #Let us see if the file function can find the correct |
---|
196 | #values at the midpoints at the last timestep: |
---|
197 | f = file_function(filename, domain1, |
---|
198 | interpolation_points = d_midpoints) |
---|
199 | |
---|
200 | T = f.get_time() |
---|
201 | msg = 'duplicate timesteps: %.16f and %.16f' %(T[-1], T[-2]) |
---|
202 | assert not T[-1] == T[-2], msg |
---|
203 | t = time[last_time_index] |
---|
204 | q = f(t, point_id=0); assert allclose(r0, q) |
---|
205 | q = f(t, point_id=1); assert allclose(r1, q) |
---|
206 | q = f(t, point_id=2); assert allclose(r2, q) |
---|
207 | |
---|
208 | |
---|
209 | ################## |
---|
210 | #Now do the same for the first timestep |
---|
211 | |
---|
212 | timestep = 0 #First timestep |
---|
213 | d_stage = reshape(take(stage[timestep, :], [0,5,10,15]), (4,1)) |
---|
214 | d_uh = reshape(take(xmomentum[timestep, :], [0,5,10,15]), (4,1)) |
---|
215 | d_vh = reshape(take(ymomentum[timestep, :], [0,5,10,15]), (4,1)) |
---|
216 | D = concatenate( (d_stage, d_uh, d_vh), axis=1) |
---|
217 | |
---|
218 | #Reference interpolated values at midpoints on diagonal at |
---|
219 | #this timestep are |
---|
220 | r0 = (D[0] + D[1])/2 |
---|
221 | r1 = (D[1] + D[2])/2 |
---|
222 | r2 = (D[2] + D[3])/2 |
---|
223 | |
---|
224 | #Let us see if the file function can find the correct |
---|
225 | #values |
---|
226 | q = f(0, point_id=0); assert allclose(r0, q) |
---|
227 | q = f(0, point_id=1); assert allclose(r1, q) |
---|
228 | q = f(0, point_id=2); assert allclose(r2, q) |
---|
229 | |
---|
230 | |
---|
231 | ################## |
---|
232 | #Now do it again for a timestep in the middle |
---|
233 | |
---|
234 | timestep = 33 |
---|
235 | d_stage = reshape(take(stage[timestep, :], [0,5,10,15]), (4,1)) |
---|
236 | d_uh = reshape(take(xmomentum[timestep, :], [0,5,10,15]), (4,1)) |
---|
237 | d_vh = reshape(take(ymomentum[timestep, :], [0,5,10,15]), (4,1)) |
---|
238 | D = concatenate( (d_stage, d_uh, d_vh), axis=1) |
---|
239 | |
---|
240 | #Reference interpolated values at midpoints on diagonal at |
---|
241 | #this timestep are |
---|
242 | r0 = (D[0] + D[1])/2 |
---|
243 | r1 = (D[1] + D[2])/2 |
---|
244 | r2 = (D[2] + D[3])/2 |
---|
245 | |
---|
246 | q = f(timestep/10., point_id=0); assert allclose(r0, q) |
---|
247 | q = f(timestep/10., point_id=1); assert allclose(r1, q) |
---|
248 | q = f(timestep/10., point_id=2); assert allclose(r2, q) |
---|
249 | |
---|
250 | |
---|
251 | ################## |
---|
252 | #Now check temporal interpolation |
---|
253 | #Halfway between timestep 15 and 16 |
---|
254 | |
---|
255 | timestep = 15 |
---|
256 | d_stage = reshape(take(stage[timestep, :], [0,5,10,15]), (4,1)) |
---|
257 | d_uh = reshape(take(xmomentum[timestep, :], [0,5,10,15]), (4,1)) |
---|
258 | d_vh = reshape(take(ymomentum[timestep, :], [0,5,10,15]), (4,1)) |
---|
259 | D = concatenate( (d_stage, d_uh, d_vh), axis=1) |
---|
260 | |
---|
261 | #Reference interpolated values at midpoints on diagonal at |
---|
262 | #this timestep are |
---|
263 | r0_0 = (D[0] + D[1])/2 |
---|
264 | r1_0 = (D[1] + D[2])/2 |
---|
265 | r2_0 = (D[2] + D[3])/2 |
---|
266 | |
---|
267 | # |
---|
268 | timestep = 16 |
---|
269 | d_stage = reshape(take(stage[timestep, :], [0,5,10,15]), (4,1)) |
---|
270 | d_uh = reshape(take(xmomentum[timestep, :], [0,5,10,15]), (4,1)) |
---|
271 | d_vh = reshape(take(ymomentum[timestep, :], [0,5,10,15]), (4,1)) |
---|
272 | D = concatenate( (d_stage, d_uh, d_vh), axis=1) |
---|
273 | |
---|
274 | #Reference interpolated values at midpoints on diagonal at |
---|
275 | #this timestep are |
---|
276 | r0_1 = (D[0] + D[1])/2 |
---|
277 | r1_1 = (D[1] + D[2])/2 |
---|
278 | r2_1 = (D[2] + D[3])/2 |
---|
279 | |
---|
280 | # The reference values are |
---|
281 | r0 = (r0_0 + r0_1)/2 |
---|
282 | r1 = (r1_0 + r1_1)/2 |
---|
283 | r2 = (r2_0 + r2_1)/2 |
---|
284 | |
---|
285 | q = f((timestep - 0.5)/10., point_id=0); assert allclose(r0, q) |
---|
286 | q = f((timestep - 0.5)/10., point_id=1); assert allclose(r1, q) |
---|
287 | q = f((timestep - 0.5)/10., point_id=2); assert allclose(r2, q) |
---|
288 | |
---|
289 | ################## |
---|
290 | #Finally check interpolation 2 thirds of the way |
---|
291 | #between timestep 15 and 16 |
---|
292 | |
---|
293 | # The reference values are |
---|
294 | r0 = (r0_0 + 2*r0_1)/3 |
---|
295 | r1 = (r1_0 + 2*r1_1)/3 |
---|
296 | r2 = (r2_0 + 2*r2_1)/3 |
---|
297 | |
---|
298 | #And the file function gives |
---|
299 | q = f((timestep - 1.0/3)/10., point_id=0); assert allclose(r0, q) |
---|
300 | q = f((timestep - 1.0/3)/10., point_id=1); assert allclose(r1, q) |
---|
301 | q = f((timestep - 1.0/3)/10., point_id=2); assert allclose(r2, q) |
---|
302 | |
---|
303 | fid.close() |
---|
304 | import os |
---|
305 | os.remove(filename) |
---|
306 | |
---|
307 | |
---|
308 | |
---|
309 | def test_spatio_temporal_file_function_different_origin(self): |
---|
310 | """Test that spatio temporal file function performs the correct |
---|
311 | interpolations in both time and space where space is offset by |
---|
312 | xllcorner and yllcorner |
---|
313 | NetCDF version (x,y,t dependency) |
---|
314 | """ |
---|
315 | import time |
---|
316 | |
---|
317 | #Create sww file of simple propagation from left to right |
---|
318 | #through rectangular domain |
---|
319 | from shallow_water import Domain, Dirichlet_boundary |
---|
320 | from mesh_factory import rectangular |
---|
321 | from Numeric import take, concatenate, reshape |
---|
322 | |
---|
323 | |
---|
324 | from anuga.coordinate_transforms.geo_reference import Geo_reference |
---|
325 | xllcorner = 2048 |
---|
326 | yllcorner = 11000 |
---|
327 | zone = 2 |
---|
328 | |
---|
329 | #Create basic mesh and shallow water domain |
---|
330 | points, vertices, boundary = rectangular(3, 3) |
---|
331 | domain1 = Domain(points, vertices, boundary, |
---|
332 | geo_reference = Geo_reference(xllcorner = xllcorner, |
---|
333 | yllcorner = yllcorner)) |
---|
334 | |
---|
335 | |
---|
336 | from anuga.utilities.numerical_tools import mean |
---|
337 | domain1.reduction = mean |
---|
338 | domain1.smooth = True #NOTE: Mimic sww output where each vertex has |
---|
339 | # only one value. |
---|
340 | |
---|
341 | domain1.default_order = 2 |
---|
342 | domain1.store = True |
---|
343 | domain1.set_datadir('.') |
---|
344 | domain1.set_name('spatio_temporal_boundary_source_%d' %(id(self))) |
---|
345 | domain1.quantities_to_be_stored = ['stage', 'xmomentum', 'ymomentum'] |
---|
346 | |
---|
347 | #Bed-slope, friction and IC at vertices (and interpolated elsewhere) |
---|
348 | domain1.set_quantity('elevation', 0) |
---|
349 | domain1.set_quantity('friction', 0) |
---|
350 | domain1.set_quantity('stage', 0) |
---|
351 | |
---|
352 | # Boundary conditions |
---|
353 | B0 = Dirichlet_boundary([0,0,0]) |
---|
354 | B6 = Dirichlet_boundary([0.6,0,0]) |
---|
355 | domain1.set_boundary({'left': B6, 'top': B6, 'right': B0, 'bottom': B0}) |
---|
356 | domain1.check_integrity() |
---|
357 | |
---|
358 | finaltime = 8 |
---|
359 | #Evolution |
---|
360 | for t in domain1.evolve(yieldstep = 0.1, finaltime = finaltime): |
---|
361 | pass |
---|
362 | #domain1.write_time() |
---|
363 | |
---|
364 | |
---|
365 | #Now read data from sww and check |
---|
366 | from Scientific.IO.NetCDF import NetCDFFile |
---|
367 | filename = domain1.get_name() + '.' + domain1.format |
---|
368 | fid = NetCDFFile(filename) |
---|
369 | |
---|
370 | x = fid.variables['x'][:] |
---|
371 | y = fid.variables['y'][:] |
---|
372 | stage = fid.variables['stage'][:] |
---|
373 | xmomentum = fid.variables['xmomentum'][:] |
---|
374 | ymomentum = fid.variables['ymomentum'][:] |
---|
375 | time = fid.variables['time'][:] |
---|
376 | |
---|
377 | #Take stage vertex values at last timestep on diagonal |
---|
378 | #Diagonal is identified by vertices: 0, 5, 10, 15 |
---|
379 | |
---|
380 | last_time_index = len(time)-1 #Last last_time_index |
---|
381 | d_stage = reshape(take(stage[last_time_index, :], [0,5,10,15]), (4,1)) |
---|
382 | d_uh = reshape(take(xmomentum[last_time_index, :], [0,5,10,15]), (4,1)) |
---|
383 | d_vh = reshape(take(ymomentum[last_time_index, :], [0,5,10,15]), (4,1)) |
---|
384 | D = concatenate( (d_stage, d_uh, d_vh), axis=1) |
---|
385 | |
---|
386 | #Reference interpolated values at midpoints on diagonal at |
---|
387 | #this timestep are |
---|
388 | r0 = (D[0] + D[1])/2 |
---|
389 | r1 = (D[1] + D[2])/2 |
---|
390 | r2 = (D[2] + D[3])/2 |
---|
391 | |
---|
392 | #And the midpoints are found now |
---|
393 | Dx = take(reshape(x, (16,1)), [0,5,10,15]) |
---|
394 | Dy = take(reshape(y, (16,1)), [0,5,10,15]) |
---|
395 | |
---|
396 | diag = concatenate( (Dx, Dy), axis=1) |
---|
397 | d_midpoints = (diag[1:] + diag[:-1])/2 |
---|
398 | |
---|
399 | |
---|
400 | #Adjust for georef - make interpolation points absolute |
---|
401 | d_midpoints[:,0] += xllcorner |
---|
402 | d_midpoints[:,1] += yllcorner |
---|
403 | |
---|
404 | #Let us see if the file function can find the correct |
---|
405 | #values at the midpoints at the last timestep: |
---|
406 | f = file_function(filename, domain1, |
---|
407 | interpolation_points = d_midpoints) |
---|
408 | |
---|
409 | t = time[last_time_index] |
---|
410 | q = f(t, point_id=0); assert allclose(r0, q) |
---|
411 | q = f(t, point_id=1); assert allclose(r1, q) |
---|
412 | q = f(t, point_id=2); assert allclose(r2, q) |
---|
413 | |
---|
414 | |
---|
415 | ################## |
---|
416 | #Now do the same for the first timestep |
---|
417 | |
---|
418 | timestep = 0 #First timestep |
---|
419 | d_stage = reshape(take(stage[timestep, :], [0,5,10,15]), (4,1)) |
---|
420 | d_uh = reshape(take(xmomentum[timestep, :], [0,5,10,15]), (4,1)) |
---|
421 | d_vh = reshape(take(ymomentum[timestep, :], [0,5,10,15]), (4,1)) |
---|
422 | D = concatenate( (d_stage, d_uh, d_vh), axis=1) |
---|
423 | |
---|
424 | #Reference interpolated values at midpoints on diagonal at |
---|
425 | #this timestep are |
---|
426 | r0 = (D[0] + D[1])/2 |
---|
427 | r1 = (D[1] + D[2])/2 |
---|
428 | r2 = (D[2] + D[3])/2 |
---|
429 | |
---|
430 | #Let us see if the file function can find the correct |
---|
431 | #values |
---|
432 | q = f(0, point_id=0); assert allclose(r0, q) |
---|
433 | q = f(0, point_id=1); assert allclose(r1, q) |
---|
434 | q = f(0, point_id=2); assert allclose(r2, q) |
---|
435 | |
---|
436 | |
---|
437 | ################## |
---|
438 | #Now do it again for a timestep in the middle |
---|
439 | |
---|
440 | timestep = 33 |
---|
441 | d_stage = reshape(take(stage[timestep, :], [0,5,10,15]), (4,1)) |
---|
442 | d_uh = reshape(take(xmomentum[timestep, :], [0,5,10,15]), (4,1)) |
---|
443 | d_vh = reshape(take(ymomentum[timestep, :], [0,5,10,15]), (4,1)) |
---|
444 | D = concatenate( (d_stage, d_uh, d_vh), axis=1) |
---|
445 | |
---|
446 | #Reference interpolated values at midpoints on diagonal at |
---|
447 | #this timestep are |
---|
448 | r0 = (D[0] + D[1])/2 |
---|
449 | r1 = (D[1] + D[2])/2 |
---|
450 | r2 = (D[2] + D[3])/2 |
---|
451 | |
---|
452 | q = f(timestep/10., point_id=0); assert allclose(r0, q) |
---|
453 | q = f(timestep/10., point_id=1); assert allclose(r1, q) |
---|
454 | q = f(timestep/10., point_id=2); assert allclose(r2, q) |
---|
455 | |
---|
456 | |
---|
457 | ################## |
---|
458 | #Now check temporal interpolation |
---|
459 | #Halfway between timestep 15 and 16 |
---|
460 | |
---|
461 | timestep = 15 |
---|
462 | d_stage = reshape(take(stage[timestep, :], [0,5,10,15]), (4,1)) |
---|
463 | d_uh = reshape(take(xmomentum[timestep, :], [0,5,10,15]), (4,1)) |
---|
464 | d_vh = reshape(take(ymomentum[timestep, :], [0,5,10,15]), (4,1)) |
---|
465 | D = concatenate( (d_stage, d_uh, d_vh), axis=1) |
---|
466 | |
---|
467 | #Reference interpolated values at midpoints on diagonal at |
---|
468 | #this timestep are |
---|
469 | r0_0 = (D[0] + D[1])/2 |
---|
470 | r1_0 = (D[1] + D[2])/2 |
---|
471 | r2_0 = (D[2] + D[3])/2 |
---|
472 | |
---|
473 | # |
---|
474 | timestep = 16 |
---|
475 | d_stage = reshape(take(stage[timestep, :], [0,5,10,15]), (4,1)) |
---|
476 | d_uh = reshape(take(xmomentum[timestep, :], [0,5,10,15]), (4,1)) |
---|
477 | d_vh = reshape(take(ymomentum[timestep, :], [0,5,10,15]), (4,1)) |
---|
478 | D = concatenate( (d_stage, d_uh, d_vh), axis=1) |
---|
479 | |
---|
480 | #Reference interpolated values at midpoints on diagonal at |
---|
481 | #this timestep are |
---|
482 | r0_1 = (D[0] + D[1])/2 |
---|
483 | r1_1 = (D[1] + D[2])/2 |
---|
484 | r2_1 = (D[2] + D[3])/2 |
---|
485 | |
---|
486 | # The reference values are |
---|
487 | r0 = (r0_0 + r0_1)/2 |
---|
488 | r1 = (r1_0 + r1_1)/2 |
---|
489 | r2 = (r2_0 + r2_1)/2 |
---|
490 | |
---|
491 | q = f((timestep - 0.5)/10., point_id=0); assert allclose(r0, q) |
---|
492 | q = f((timestep - 0.5)/10., point_id=1); assert allclose(r1, q) |
---|
493 | q = f((timestep - 0.5)/10., point_id=2); assert allclose(r2, q) |
---|
494 | |
---|
495 | ################## |
---|
496 | #Finally check interpolation 2 thirds of the way |
---|
497 | #between timestep 15 and 16 |
---|
498 | |
---|
499 | # The reference values are |
---|
500 | r0 = (r0_0 + 2*r0_1)/3 |
---|
501 | r1 = (r1_0 + 2*r1_1)/3 |
---|
502 | r2 = (r2_0 + 2*r2_1)/3 |
---|
503 | |
---|
504 | #And the file function gives |
---|
505 | q = f((timestep - 1.0/3)/10., point_id=0); assert allclose(r0, q) |
---|
506 | q = f((timestep - 1.0/3)/10., point_id=1); assert allclose(r1, q) |
---|
507 | q = f((timestep - 1.0/3)/10., point_id=2); assert allclose(r2, q) |
---|
508 | |
---|
509 | fid.close() |
---|
510 | import os |
---|
511 | os.remove(filename) |
---|
512 | |
---|
513 | |
---|
514 | |
---|
515 | |
---|
516 | def qtest_spatio_temporal_file_function_time(self): |
---|
517 | """Test that File function interpolates correctly |
---|
518 | between given times. |
---|
519 | NetCDF version (x,y,t dependency) |
---|
520 | """ |
---|
521 | |
---|
522 | #Create NetCDF (sww) file to be read |
---|
523 | # x: 0, 5, 10, 15 |
---|
524 | # y: -20, -10, 0, 10 |
---|
525 | # t: 0, 60, 120, ...., 1200 |
---|
526 | # |
---|
527 | # test quantities (arbitrary but non-trivial expressions): |
---|
528 | # |
---|
529 | # stage = 3*x - y**2 + 2*t |
---|
530 | # xmomentum = exp( -((x-7)**2 + (y+5)**2)/20 ) * t**2 |
---|
531 | # ymomentum = x**2 + y**2 * sin(t*pi/600) |
---|
532 | |
---|
533 | #NOTE: Nice test that may render some of the others redundant. |
---|
534 | |
---|
535 | import os, time |
---|
536 | from anuga.config import time_format |
---|
537 | from Numeric import sin, pi, exp |
---|
538 | from mesh_factory import rectangular |
---|
539 | from shallow_water import Domain |
---|
540 | import anuga.shallow_water.data_manager |
---|
541 | |
---|
542 | finaltime = 1200 |
---|
543 | filename = 'test_file_function' |
---|
544 | |
---|
545 | #Create a domain to hold test grid |
---|
546 | #(0:15, -20:10) |
---|
547 | points, vertices, boundary =\ |
---|
548 | rectangular(4, 4, 15, 30, origin = (0, -20)) |
---|
549 | print "points", points |
---|
550 | |
---|
551 | #print 'Number of elements', len(vertices) |
---|
552 | domain = Domain(points, vertices, boundary) |
---|
553 | domain.smooth = False |
---|
554 | domain.default_order = 2 |
---|
555 | domain.set_datadir('.') |
---|
556 | domain.set_name(filename) |
---|
557 | domain.store = True |
---|
558 | domain.format = 'sww' #Native netcdf visualisation format |
---|
559 | |
---|
560 | #print points |
---|
561 | start = time.mktime(time.strptime('2000', '%Y')) |
---|
562 | domain.starttime = start |
---|
563 | |
---|
564 | |
---|
565 | #Store structure |
---|
566 | domain.initialise_storage() |
---|
567 | |
---|
568 | #Compute artificial time steps and store |
---|
569 | dt = 60 #One minute intervals |
---|
570 | t = 0.0 |
---|
571 | while t <= finaltime: |
---|
572 | #Compute quantities |
---|
573 | f1 = lambda x,y: 3*x - y**2 + 2*t + 4 |
---|
574 | domain.set_quantity('stage', f1) |
---|
575 | |
---|
576 | f2 = lambda x,y: x+y+t**2 |
---|
577 | domain.set_quantity('xmomentum', f2) |
---|
578 | |
---|
579 | f3 = lambda x,y: x**2 + y**2 * sin(t*pi/600) |
---|
580 | domain.set_quantity('ymomentum', f3) |
---|
581 | |
---|
582 | #Store and advance time |
---|
583 | domain.time = t |
---|
584 | domain.store_timestep(domain.conserved_quantities) |
---|
585 | t += dt |
---|
586 | |
---|
587 | |
---|
588 | interpolation_points = [[0,-20], [1,0], [0,1], [1.1, 3.14], [10,-12.5]] |
---|
589 | |
---|
590 | #Deliberately set domain.starttime to too early |
---|
591 | domain.starttime = start - 1 |
---|
592 | |
---|
593 | #Create file function |
---|
594 | F = file_function(filename + '.sww', domain, |
---|
595 | quantities = domain.conserved_quantities, |
---|
596 | interpolation_points = interpolation_points) |
---|
597 | |
---|
598 | #Check that FF updates fixes domain starttime |
---|
599 | assert allclose(domain.starttime, start) |
---|
600 | |
---|
601 | #Check that domain.starttime isn't updated if later |
---|
602 | domain.starttime = start + 1 |
---|
603 | F = file_function(filename + '.sww', domain, |
---|
604 | quantities = domain.conserved_quantities, |
---|
605 | interpolation_points = interpolation_points) |
---|
606 | assert allclose(domain.starttime, start+1) |
---|
607 | domain.starttime = start |
---|
608 | |
---|
609 | |
---|
610 | #Check linear interpolation in time |
---|
611 | F = file_function(filename + '.sww', domain, |
---|
612 | quantities = domain.conserved_quantities, |
---|
613 | interpolation_points = interpolation_points) |
---|
614 | for id in range(len(interpolation_points)): |
---|
615 | x = interpolation_points[id][0] |
---|
616 | y = interpolation_points[id][1] |
---|
617 | |
---|
618 | for i in range(20): |
---|
619 | t = i*10 |
---|
620 | k = i%6 |
---|
621 | |
---|
622 | if k == 0: |
---|
623 | q0 = F(t, point_id=id) |
---|
624 | q1 = F(t+60, point_id=id) |
---|
625 | |
---|
626 | if q0 == NAN: |
---|
627 | actual = q0 |
---|
628 | else: |
---|
629 | actual = (k*q1 + (6-k)*q0)/6 |
---|
630 | q = F(t, point_id=id) |
---|
631 | #print i, k, t, q |
---|
632 | #print ' ', q0 |
---|
633 | #print ' ', q1 |
---|
634 | print "q",q |
---|
635 | print "actual", actual |
---|
636 | #print |
---|
637 | if q0 == NAN: |
---|
638 | self.failUnless( q == actual, 'Fail!') |
---|
639 | else: |
---|
640 | assert allclose(q, actual) |
---|
641 | |
---|
642 | |
---|
643 | #Another check of linear interpolation in time |
---|
644 | for id in range(len(interpolation_points)): |
---|
645 | q60 = F(60, point_id=id) |
---|
646 | q120 = F(120, point_id=id) |
---|
647 | |
---|
648 | t = 90 #Halfway between 60 and 120 |
---|
649 | q = F(t, point_id=id) |
---|
650 | assert allclose( (q120+q60)/2, q ) |
---|
651 | |
---|
652 | t = 100 #Two thirds of the way between between 60 and 120 |
---|
653 | q = F(t, point_id=id) |
---|
654 | assert allclose(q60/3 + 2*q120/3, q) |
---|
655 | |
---|
656 | |
---|
657 | |
---|
658 | #Check that domain.starttime isn't updated if later than file starttime but earlier |
---|
659 | #than file end time |
---|
660 | delta = 23 |
---|
661 | domain.starttime = start + delta |
---|
662 | F = file_function(filename + '.sww', domain, |
---|
663 | quantities = domain.conserved_quantities, |
---|
664 | interpolation_points = interpolation_points) |
---|
665 | assert allclose(domain.starttime, start+delta) |
---|
666 | |
---|
667 | |
---|
668 | |
---|
669 | |
---|
670 | #Now try interpolation with delta offset |
---|
671 | for id in range(len(interpolation_points)): |
---|
672 | x = interpolation_points[id][0] |
---|
673 | y = interpolation_points[id][1] |
---|
674 | |
---|
675 | for i in range(20): |
---|
676 | t = i*10 |
---|
677 | k = i%6 |
---|
678 | |
---|
679 | if k == 0: |
---|
680 | q0 = F(t-delta, point_id=id) |
---|
681 | q1 = F(t+60-delta, point_id=id) |
---|
682 | |
---|
683 | q = F(t-delta, point_id=id) |
---|
684 | assert allclose(q, (k*q1 + (6-k)*q0)/6) |
---|
685 | |
---|
686 | |
---|
687 | os.remove(filename + '.sww') |
---|
688 | |
---|
689 | |
---|
690 | |
---|
691 | def NOtest_spatio_temporal_file_function_time(self): |
---|
692 | # FIXME: This passes but needs some TLC |
---|
693 | # Test that File function interpolates correctly |
---|
694 | # When some points are outside the mesh |
---|
695 | |
---|
696 | import os, time |
---|
697 | from anuga.config import time_format |
---|
698 | from Numeric import sin, pi, exp |
---|
699 | from mesh_factory import rectangular |
---|
700 | from shallow_water import Domain |
---|
701 | import anuga.shallow_water.data_manager |
---|
702 | from anuga.pmesh.mesh_interface import create_mesh_from_regions |
---|
703 | finaltime = 1200 |
---|
704 | |
---|
705 | filename = tempfile.mktemp() |
---|
706 | #print "filename",filename |
---|
707 | filename = 'test_file_function' |
---|
708 | |
---|
709 | meshfilename = tempfile.mktemp(".tsh") |
---|
710 | |
---|
711 | boundary_tags = {'walls':[0,1],'bom':[2]} |
---|
712 | |
---|
713 | polygon_absolute = [[0,-20],[10,-20],[10,15],[-20,15]] |
---|
714 | |
---|
715 | create_mesh_from_regions(polygon_absolute, |
---|
716 | boundary_tags, |
---|
717 | 10000000, |
---|
718 | filename=meshfilename) |
---|
719 | domain = Domain(mesh_filename=meshfilename) |
---|
720 | domain.smooth = False |
---|
721 | domain.default_order = 2 |
---|
722 | domain.set_datadir('.') |
---|
723 | domain.set_name(filename) |
---|
724 | domain.store = True |
---|
725 | domain.format = 'sww' #Native netcdf visualisation format |
---|
726 | |
---|
727 | #print points |
---|
728 | start = time.mktime(time.strptime('2000', '%Y')) |
---|
729 | domain.starttime = start |
---|
730 | |
---|
731 | |
---|
732 | #Store structure |
---|
733 | domain.initialise_storage() |
---|
734 | |
---|
735 | #Compute artificial time steps and store |
---|
736 | dt = 60 #One minute intervals |
---|
737 | t = 0.0 |
---|
738 | while t <= finaltime: |
---|
739 | #Compute quantities |
---|
740 | f1 = lambda x,y: 3*x - y**2 + 2*t + 4 |
---|
741 | domain.set_quantity('stage', f1) |
---|
742 | |
---|
743 | f2 = lambda x,y: x+y+t**2 |
---|
744 | domain.set_quantity('xmomentum', f2) |
---|
745 | |
---|
746 | f3 = lambda x,y: x**2 + y**2 * sin(t*pi/600) |
---|
747 | domain.set_quantity('ymomentum', f3) |
---|
748 | |
---|
749 | #Store and advance time |
---|
750 | domain.time = t |
---|
751 | domain.store_timestep(domain.conserved_quantities) |
---|
752 | t += dt |
---|
753 | |
---|
754 | interpolation_points = [[1,0]] |
---|
755 | interpolation_points = [[100,1000]] |
---|
756 | |
---|
757 | interpolation_points = [[0,-20], [1,0], [0,1], [1.1, 3.14], [10,-12.5], |
---|
758 | [78787,78787],[7878,3432]] |
---|
759 | |
---|
760 | #Deliberately set domain.starttime to too early |
---|
761 | domain.starttime = start - 1 |
---|
762 | |
---|
763 | #Create file function |
---|
764 | F = file_function(filename + '.sww', domain, |
---|
765 | quantities = domain.conserved_quantities, |
---|
766 | interpolation_points = interpolation_points) |
---|
767 | |
---|
768 | #Check that FF updates fixes domain starttime |
---|
769 | assert allclose(domain.starttime, start) |
---|
770 | |
---|
771 | #Check that domain.starttime isn't updated if later |
---|
772 | domain.starttime = start + 1 |
---|
773 | F = file_function(filename + '.sww', domain, |
---|
774 | quantities = domain.conserved_quantities, |
---|
775 | interpolation_points = interpolation_points) |
---|
776 | assert allclose(domain.starttime, start+1) |
---|
777 | domain.starttime = start |
---|
778 | |
---|
779 | |
---|
780 | #Check linear interpolation in time |
---|
781 | # checking points inside and outside the mesh |
---|
782 | F = file_function(filename + '.sww', domain, |
---|
783 | quantities = domain.conserved_quantities, |
---|
784 | interpolation_points = interpolation_points) |
---|
785 | |
---|
786 | for id in range(len(interpolation_points)): |
---|
787 | x = interpolation_points[id][0] |
---|
788 | y = interpolation_points[id][1] |
---|
789 | |
---|
790 | for i in range(20): |
---|
791 | t = i*10 |
---|
792 | k = i%6 |
---|
793 | |
---|
794 | if k == 0: |
---|
795 | q0 = F(t, point_id=id) |
---|
796 | q1 = F(t+60, point_id=id) |
---|
797 | |
---|
798 | if q0 == NAN: |
---|
799 | actual = q0 |
---|
800 | else: |
---|
801 | actual = (k*q1 + (6-k)*q0)/6 |
---|
802 | q = F(t, point_id=id) |
---|
803 | #print i, k, t, q |
---|
804 | #print ' ', q0 |
---|
805 | #print ' ', q1 |
---|
806 | #print "q",q |
---|
807 | #print "actual", actual |
---|
808 | #print |
---|
809 | if q0 == NAN: |
---|
810 | self.failUnless( q == actual, 'Fail!') |
---|
811 | else: |
---|
812 | assert allclose(q, actual) |
---|
813 | |
---|
814 | # now lets check points inside the mesh |
---|
815 | interpolation_points = [[0,-20], [1,0], [0,1], [1.1, 3.14]] #, [10,-12.5]] - this point doesn't work WHY? |
---|
816 | interpolation_points = [[10,-12.5]] |
---|
817 | |
---|
818 | print "len(interpolation_points)",len(interpolation_points) |
---|
819 | F = file_function(filename + '.sww', domain, |
---|
820 | quantities = domain.conserved_quantities, |
---|
821 | interpolation_points = interpolation_points) |
---|
822 | |
---|
823 | domain.starttime = start |
---|
824 | |
---|
825 | |
---|
826 | #Check linear interpolation in time |
---|
827 | F = file_function(filename + '.sww', domain, |
---|
828 | quantities = domain.conserved_quantities, |
---|
829 | interpolation_points = interpolation_points) |
---|
830 | for id in range(len(interpolation_points)): |
---|
831 | x = interpolation_points[id][0] |
---|
832 | y = interpolation_points[id][1] |
---|
833 | |
---|
834 | for i in range(20): |
---|
835 | t = i*10 |
---|
836 | k = i%6 |
---|
837 | |
---|
838 | if k == 0: |
---|
839 | q0 = F(t, point_id=id) |
---|
840 | q1 = F(t+60, point_id=id) |
---|
841 | |
---|
842 | if q0 == NAN: |
---|
843 | actual = q0 |
---|
844 | else: |
---|
845 | actual = (k*q1 + (6-k)*q0)/6 |
---|
846 | q = F(t, point_id=id) |
---|
847 | print "############" |
---|
848 | print "id, x, y ", id, x, y #k, t, q |
---|
849 | print "t", t |
---|
850 | #print ' ', q0 |
---|
851 | #print ' ', q1 |
---|
852 | print "q",q |
---|
853 | print "actual", actual |
---|
854 | #print |
---|
855 | if q0 == NAN: |
---|
856 | self.failUnless( q == actual, 'Fail!') |
---|
857 | else: |
---|
858 | assert allclose(q, actual) |
---|
859 | |
---|
860 | |
---|
861 | #Another check of linear interpolation in time |
---|
862 | for id in range(len(interpolation_points)): |
---|
863 | q60 = F(60, point_id=id) |
---|
864 | q120 = F(120, point_id=id) |
---|
865 | |
---|
866 | t = 90 #Halfway between 60 and 120 |
---|
867 | q = F(t, point_id=id) |
---|
868 | assert allclose( (q120+q60)/2, q ) |
---|
869 | |
---|
870 | t = 100 #Two thirds of the way between between 60 and 120 |
---|
871 | q = F(t, point_id=id) |
---|
872 | assert allclose(q60/3 + 2*q120/3, q) |
---|
873 | |
---|
874 | |
---|
875 | |
---|
876 | #Check that domain.starttime isn't updated if later than file starttime but earlier |
---|
877 | #than file end time |
---|
878 | delta = 23 |
---|
879 | domain.starttime = start + delta |
---|
880 | F = file_function(filename + '.sww', domain, |
---|
881 | quantities = domain.conserved_quantities, |
---|
882 | interpolation_points = interpolation_points) |
---|
883 | assert allclose(domain.starttime, start+delta) |
---|
884 | |
---|
885 | |
---|
886 | |
---|
887 | |
---|
888 | #Now try interpolation with delta offset |
---|
889 | for id in range(len(interpolation_points)): |
---|
890 | x = interpolation_points[id][0] |
---|
891 | y = interpolation_points[id][1] |
---|
892 | |
---|
893 | for i in range(20): |
---|
894 | t = i*10 |
---|
895 | k = i%6 |
---|
896 | |
---|
897 | if k == 0: |
---|
898 | q0 = F(t-delta, point_id=id) |
---|
899 | q1 = F(t+60-delta, point_id=id) |
---|
900 | |
---|
901 | q = F(t-delta, point_id=id) |
---|
902 | assert allclose(q, (k*q1 + (6-k)*q0)/6) |
---|
903 | |
---|
904 | |
---|
905 | os.remove(filename + '.sww') |
---|
906 | |
---|
907 | def test_file_function_time_with_domain(self): |
---|
908 | """Test that File function interpolates correctly |
---|
909 | between given times. No x,y dependency here. |
---|
910 | Use domain with starttime |
---|
911 | """ |
---|
912 | |
---|
913 | #Write file |
---|
914 | import os, time, calendar |
---|
915 | from anuga.config import time_format |
---|
916 | from math import sin, pi |
---|
917 | from domain import Domain |
---|
918 | |
---|
919 | finaltime = 1200 |
---|
920 | filename = 'test_file_function' |
---|
921 | fid = open(filename + '.txt', 'w') |
---|
922 | start = time.mktime(time.strptime('2000', '%Y')) |
---|
923 | dt = 60 #One minute intervals |
---|
924 | t = 0.0 |
---|
925 | while t <= finaltime: |
---|
926 | t_string = time.strftime(time_format, time.gmtime(t+start)) |
---|
927 | fid.write('%s, %f %f %f\n' %(t_string, 2*t, t**2, sin(t*pi/600))) |
---|
928 | t += dt |
---|
929 | |
---|
930 | fid.close() |
---|
931 | |
---|
932 | |
---|
933 | #Convert ASCII file to NetCDF (Which is what we really like!) |
---|
934 | timefile2netcdf(filename) |
---|
935 | |
---|
936 | |
---|
937 | |
---|
938 | a = [0.0, 0.0] |
---|
939 | b = [4.0, 0.0] |
---|
940 | c = [0.0, 3.0] |
---|
941 | |
---|
942 | points = [a, b, c] |
---|
943 | vertices = [[0,1,2]] |
---|
944 | domain = Domain(points, vertices) |
---|
945 | |
---|
946 | #Check that domain.starttime is updated if non-existing |
---|
947 | F = file_function(filename + '.tms', domain) |
---|
948 | |
---|
949 | assert allclose(domain.starttime, start) |
---|
950 | |
---|
951 | #Check that domain.starttime is updated if too early |
---|
952 | domain.starttime = start - 1 |
---|
953 | F = file_function(filename + '.tms', domain) |
---|
954 | assert allclose(domain.starttime, start) |
---|
955 | |
---|
956 | #Check that domain.starttime isn't updated if later |
---|
957 | domain.starttime = start + 1 |
---|
958 | F = file_function(filename + '.tms', domain) |
---|
959 | assert allclose(domain.starttime, start+1) |
---|
960 | |
---|
961 | domain.starttime = start |
---|
962 | F = file_function(filename + '.tms', domain, |
---|
963 | quantities = ['Attribute0', 'Attribute1', 'Attribute2'], |
---|
964 | use_cache=True) |
---|
965 | |
---|
966 | |
---|
967 | #print F.precomputed_values |
---|
968 | #print 'F(60)', F(60) |
---|
969 | |
---|
970 | #Now try interpolation |
---|
971 | for i in range(20): |
---|
972 | t = i*10 |
---|
973 | q = F(t) |
---|
974 | |
---|
975 | #Exact linear intpolation |
---|
976 | assert allclose(q[0], 2*t) |
---|
977 | if i%6 == 0: |
---|
978 | assert allclose(q[1], t**2) |
---|
979 | assert allclose(q[2], sin(t*pi/600)) |
---|
980 | |
---|
981 | #Check non-exact |
---|
982 | |
---|
983 | t = 90 #Halfway between 60 and 120 |
---|
984 | q = F(t) |
---|
985 | assert allclose( (120**2 + 60**2)/2, q[1] ) |
---|
986 | assert allclose( (sin(120*pi/600) + sin(60*pi/600))/2, q[2] ) |
---|
987 | |
---|
988 | |
---|
989 | t = 100 #Two thirds of the way between between 60 and 120 |
---|
990 | q = F(t) |
---|
991 | assert allclose( 2*120**2/3 + 60**2/3, q[1] ) |
---|
992 | assert allclose( 2*sin(120*pi/600)/3 + sin(60*pi/600)/3, q[2] ) |
---|
993 | |
---|
994 | os.remove(filename + '.tms') |
---|
995 | os.remove(filename + '.txt') |
---|
996 | |
---|
997 | def test_file_function_time_with_domain_different_start(self): |
---|
998 | """Test that File function interpolates correctly |
---|
999 | between given times. No x,y dependency here. |
---|
1000 | Use domain with a starttime later than that of file |
---|
1001 | |
---|
1002 | ASCII version |
---|
1003 | """ |
---|
1004 | |
---|
1005 | #Write file |
---|
1006 | import os, time, calendar |
---|
1007 | from anuga.config import time_format |
---|
1008 | from math import sin, pi |
---|
1009 | from domain import Domain |
---|
1010 | |
---|
1011 | finaltime = 1200 |
---|
1012 | filename = 'test_file_function' |
---|
1013 | fid = open(filename + '.txt', 'w') |
---|
1014 | start = time.mktime(time.strptime('2000', '%Y')) |
---|
1015 | dt = 60 #One minute intervals |
---|
1016 | t = 0.0 |
---|
1017 | while t <= finaltime: |
---|
1018 | t_string = time.strftime(time_format, time.gmtime(t+start)) |
---|
1019 | fid.write('%s, %f %f %f\n' %(t_string, 2*t, t**2, sin(t*pi/600))) |
---|
1020 | t += dt |
---|
1021 | |
---|
1022 | fid.close() |
---|
1023 | |
---|
1024 | #Convert ASCII file to NetCDF (Which is what we really like!) |
---|
1025 | timefile2netcdf(filename) |
---|
1026 | |
---|
1027 | a = [0.0, 0.0] |
---|
1028 | b = [4.0, 0.0] |
---|
1029 | c = [0.0, 3.0] |
---|
1030 | |
---|
1031 | points = [a, b, c] |
---|
1032 | vertices = [[0,1,2]] |
---|
1033 | domain = Domain(points, vertices) |
---|
1034 | |
---|
1035 | #Check that domain.starttime isn't updated if later than file starttime but earlier |
---|
1036 | #than file end time |
---|
1037 | delta = 23 |
---|
1038 | domain.starttime = start + delta |
---|
1039 | F = file_function(filename + '.tms', domain, |
---|
1040 | quantities = ['Attribute0', 'Attribute1', 'Attribute2']) |
---|
1041 | assert allclose(domain.starttime, start+delta) |
---|
1042 | |
---|
1043 | |
---|
1044 | |
---|
1045 | |
---|
1046 | #Now try interpolation with delta offset |
---|
1047 | for i in range(20): |
---|
1048 | t = i*10 |
---|
1049 | q = F(t-delta) |
---|
1050 | |
---|
1051 | #Exact linear intpolation |
---|
1052 | assert allclose(q[0], 2*t) |
---|
1053 | if i%6 == 0: |
---|
1054 | assert allclose(q[1], t**2) |
---|
1055 | assert allclose(q[2], sin(t*pi/600)) |
---|
1056 | |
---|
1057 | #Check non-exact |
---|
1058 | |
---|
1059 | t = 90 #Halfway between 60 and 120 |
---|
1060 | q = F(t-delta) |
---|
1061 | assert allclose( (120**2 + 60**2)/2, q[1] ) |
---|
1062 | assert allclose( (sin(120*pi/600) + sin(60*pi/600))/2, q[2] ) |
---|
1063 | |
---|
1064 | |
---|
1065 | t = 100 #Two thirds of the way between between 60 and 120 |
---|
1066 | q = F(t-delta) |
---|
1067 | assert allclose( 2*120**2/3 + 60**2/3, q[1] ) |
---|
1068 | assert allclose( 2*sin(120*pi/600)/3 + sin(60*pi/600)/3, q[2] ) |
---|
1069 | |
---|
1070 | |
---|
1071 | os.remove(filename + '.tms') |
---|
1072 | os.remove(filename + '.txt') |
---|
1073 | |
---|
1074 | |
---|
1075 | |
---|
1076 | def test_apply_expression_to_dictionary(self): |
---|
1077 | |
---|
1078 | #FIXME: Division is not expected to work for integers. |
---|
1079 | #This must be caught. |
---|
1080 | foo = array([[1,2,3], |
---|
1081 | [4,5,6]], Float) |
---|
1082 | |
---|
1083 | bar = array([[-1,0,5], |
---|
1084 | [6,1,1]], Float) |
---|
1085 | |
---|
1086 | D = {'X': foo, 'Y': bar} |
---|
1087 | |
---|
1088 | Z = apply_expression_to_dictionary('X+Y', D) |
---|
1089 | assert allclose(Z, foo+bar) |
---|
1090 | |
---|
1091 | Z = apply_expression_to_dictionary('X*Y', D) |
---|
1092 | assert allclose(Z, foo*bar) |
---|
1093 | |
---|
1094 | Z = apply_expression_to_dictionary('4*X+Y', D) |
---|
1095 | assert allclose(Z, 4*foo+bar) |
---|
1096 | |
---|
1097 | # test zero division is OK |
---|
1098 | Z = apply_expression_to_dictionary('X/Y', D) |
---|
1099 | assert allclose(1/Z, 1/(foo/bar)) # can't compare inf to inf |
---|
1100 | |
---|
1101 | # make an error for zero on zero |
---|
1102 | # this is really an error in Numeric, SciPy core can handle it |
---|
1103 | # Z = apply_expression_to_dictionary('0/Y', D) |
---|
1104 | |
---|
1105 | #Check exceptions |
---|
1106 | try: |
---|
1107 | #Wrong name |
---|
1108 | Z = apply_expression_to_dictionary('4*X+A', D) |
---|
1109 | except NameError: |
---|
1110 | pass |
---|
1111 | else: |
---|
1112 | msg = 'Should have raised a NameError Exception' |
---|
1113 | raise msg |
---|
1114 | |
---|
1115 | |
---|
1116 | try: |
---|
1117 | #Wrong order |
---|
1118 | Z = apply_expression_to_dictionary(D, '4*X+A') |
---|
1119 | except AssertionError: |
---|
1120 | pass |
---|
1121 | else: |
---|
1122 | msg = 'Should have raised a AssertionError Exception' |
---|
1123 | raise msg |
---|
1124 | |
---|
1125 | |
---|
1126 | def test_multiple_replace(self): |
---|
1127 | """Hard test that checks a true word-by-word simultaneous replace |
---|
1128 | """ |
---|
1129 | |
---|
1130 | D = {'x': 'xi', 'y': 'eta', 'xi':'lam'} |
---|
1131 | exp = '3*x+y + xi' |
---|
1132 | |
---|
1133 | new = multiple_replace(exp, D) |
---|
1134 | |
---|
1135 | assert new == '3*xi+eta + lam' |
---|
1136 | |
---|
1137 | |
---|
1138 | |
---|
1139 | def test_point_on_line_obsolete(self): |
---|
1140 | """Test that obsolete call issues appropriate warning""" |
---|
1141 | |
---|
1142 | #Turn warning into an exception |
---|
1143 | import warnings |
---|
1144 | warnings.filterwarnings('error') |
---|
1145 | |
---|
1146 | try: |
---|
1147 | assert point_on_line( 0, 0.5, 0,1, 0,0 ) |
---|
1148 | except DeprecationWarning: |
---|
1149 | pass |
---|
1150 | else: |
---|
1151 | msg = 'point_on_line should have issued a DeprecationWarning' |
---|
1152 | raise Exception(msg) |
---|
1153 | |
---|
1154 | warnings.resetwarnings() |
---|
1155 | |
---|
1156 | def test_get_revision_number(self): |
---|
1157 | """test_get_revision_number(self): |
---|
1158 | |
---|
1159 | Test that revision number can be retrieved. |
---|
1160 | """ |
---|
1161 | |
---|
1162 | n = get_revision_number() |
---|
1163 | assert n>=0 |
---|
1164 | |
---|
1165 | |
---|
1166 | |
---|
1167 | def test_add_directories(self): |
---|
1168 | |
---|
1169 | import tempfile |
---|
1170 | root_dir = tempfile.mkdtemp('_test_util', 'test_util_') |
---|
1171 | directories = ['ja','ne','ke'] |
---|
1172 | kens_dir = add_directories(root_dir, directories) |
---|
1173 | assert kens_dir == root_dir + sep + 'ja' + sep + 'ne' + \ |
---|
1174 | sep + 'ke' |
---|
1175 | assert access(root_dir,F_OK) |
---|
1176 | |
---|
1177 | add_directories(root_dir, directories) |
---|
1178 | assert access(root_dir,F_OK) |
---|
1179 | |
---|
1180 | #clean up! |
---|
1181 | os.rmdir(kens_dir) |
---|
1182 | os.rmdir(root_dir + sep + 'ja' + sep + 'ne') |
---|
1183 | os.rmdir(root_dir + sep + 'ja') |
---|
1184 | os.rmdir(root_dir) |
---|
1185 | |
---|
1186 | def test_add_directories_bad(self): |
---|
1187 | |
---|
1188 | import tempfile |
---|
1189 | root_dir = tempfile.mkdtemp('_test_util', 'test_util_') |
---|
1190 | directories = ['/\/!@#@#$%^%&*((*:*:','ne','ke'] |
---|
1191 | |
---|
1192 | try: |
---|
1193 | kens_dir = add_directories(root_dir, directories) |
---|
1194 | except OSError: |
---|
1195 | pass |
---|
1196 | else: |
---|
1197 | msg = 'bad dir name should give OSError' |
---|
1198 | raise Exception(msg) |
---|
1199 | |
---|
1200 | #clean up! |
---|
1201 | os.rmdir(root_dir) |
---|
1202 | |
---|
1203 | def test_check_list(self): |
---|
1204 | |
---|
1205 | check_list(['stage','xmomentum']) |
---|
1206 | |
---|
1207 | |
---|
1208 | def test_add_directories(self): |
---|
1209 | |
---|
1210 | import tempfile |
---|
1211 | root_dir = tempfile.mkdtemp('_test_util', 'test_util_') |
---|
1212 | directories = ['ja','ne','ke'] |
---|
1213 | kens_dir = add_directories(root_dir, directories) |
---|
1214 | assert kens_dir == root_dir + sep + 'ja' + sep + 'ne' + \ |
---|
1215 | sep + 'ke' |
---|
1216 | assert access(root_dir,F_OK) |
---|
1217 | |
---|
1218 | add_directories(root_dir, directories) |
---|
1219 | assert access(root_dir,F_OK) |
---|
1220 | |
---|
1221 | #clean up! |
---|
1222 | os.rmdir(kens_dir) |
---|
1223 | os.rmdir(root_dir + sep + 'ja' + sep + 'ne') |
---|
1224 | os.rmdir(root_dir + sep + 'ja') |
---|
1225 | os.rmdir(root_dir) |
---|
1226 | |
---|
1227 | def test_add_directories_bad(self): |
---|
1228 | |
---|
1229 | import tempfile |
---|
1230 | root_dir = tempfile.mkdtemp('_test_util', 'test_util_') |
---|
1231 | directories = ['/\/!@#@#$%^%&*((*:*:','ne','ke'] |
---|
1232 | |
---|
1233 | try: |
---|
1234 | kens_dir = add_directories(root_dir, directories) |
---|
1235 | except OSError: |
---|
1236 | pass |
---|
1237 | else: |
---|
1238 | msg = 'bad dir name should give OSError' |
---|
1239 | raise Exception(msg) |
---|
1240 | |
---|
1241 | #clean up! |
---|
1242 | os.rmdir(root_dir) |
---|
1243 | |
---|
1244 | def test_check_list(self): |
---|
1245 | |
---|
1246 | check_list(['stage','xmomentum']) |
---|
1247 | |
---|
1248 | def test_remove_lone_verts_d(self): |
---|
1249 | verts = [[0,0],[1,0],[0,1]] |
---|
1250 | tris = [[0,1,2]] |
---|
1251 | new_verts, new_tris = remove_lone_verts(verts, tris) |
---|
1252 | assert new_verts == verts |
---|
1253 | assert new_tris == tris |
---|
1254 | |
---|
1255 | |
---|
1256 | def test_remove_lone_verts_e(self): |
---|
1257 | verts = [[0,0],[1,0],[0,1],[99,99]] |
---|
1258 | tris = [[0,1,2]] |
---|
1259 | new_verts, new_tris = remove_lone_verts(verts, tris) |
---|
1260 | assert new_verts == verts[0:3] |
---|
1261 | assert new_tris == tris |
---|
1262 | |
---|
1263 | def test_remove_lone_verts_a(self): |
---|
1264 | verts = [[99,99],[0,0],[1,0],[99,99],[0,1],[99,99]] |
---|
1265 | tris = [[1,2,4]] |
---|
1266 | new_verts, new_tris = remove_lone_verts(verts, tris) |
---|
1267 | #print "new_verts", new_verts |
---|
1268 | assert new_verts == [[0,0],[1,0],[0,1]] |
---|
1269 | assert new_tris == [[0,1,2]] |
---|
1270 | |
---|
1271 | def test_remove_lone_verts_c(self): |
---|
1272 | verts = [[0,0],[1,0],[99,99],[0,1]] |
---|
1273 | tris = [[0,1,3]] |
---|
1274 | new_verts, new_tris = remove_lone_verts(verts, tris) |
---|
1275 | #print "new_verts", new_verts |
---|
1276 | assert new_verts == [[0,0],[1,0],[0,1]] |
---|
1277 | assert new_tris == [[0,1,2]] |
---|
1278 | |
---|
1279 | def test_remove_lone_verts_b(self): |
---|
1280 | verts = [[0,0],[1,0],[0,1],[99,99],[99,99],[99,99]] |
---|
1281 | tris = [[0,1,2]] |
---|
1282 | new_verts, new_tris = remove_lone_verts(verts, tris) |
---|
1283 | assert new_verts == verts[0:3] |
---|
1284 | assert new_tris == tris |
---|
1285 | |
---|
1286 | |
---|
1287 | def test_remove_lone_verts_e(self): |
---|
1288 | verts = [[0,0],[1,0],[0,1],[99,99]] |
---|
1289 | tris = [[0,1,2]] |
---|
1290 | new_verts, new_tris = remove_lone_verts(verts, tris) |
---|
1291 | assert new_verts == verts[0:3] |
---|
1292 | assert new_tris == tris |
---|
1293 | |
---|
1294 | def test_get_min_max_values(self): |
---|
1295 | |
---|
1296 | list=[8,9,6,1,4] |
---|
1297 | min1, max1 = get_min_max_values(list) |
---|
1298 | |
---|
1299 | assert min1==1 |
---|
1300 | assert max1==9 |
---|
1301 | |
---|
1302 | def test_get_min_max_values1(self): |
---|
1303 | |
---|
1304 | list=[-8,-9,-6,-1,-4] |
---|
1305 | min1, max1 = get_min_max_values(list,10,-10) |
---|
1306 | |
---|
1307 | # print 'min1,max1',min1,max1 |
---|
1308 | assert min1==-9 |
---|
1309 | assert max1==-1 |
---|
1310 | |
---|
1311 | def test_get_min_max_values2(self): |
---|
1312 | ''' |
---|
1313 | The min and max supplied are greater than the ones in the |
---|
1314 | list and therefore are the ones returned |
---|
1315 | ''' |
---|
1316 | list=[-8,-9,-6,-1,-4] |
---|
1317 | min1, max1 = get_min_max_values(list,-10,10) |
---|
1318 | |
---|
1319 | # print 'min1,max1',min1,max1 |
---|
1320 | assert min1==-10 |
---|
1321 | assert max1==10 |
---|
1322 | |
---|
1323 | def test_make_plots_from_csv_files(self): |
---|
1324 | |
---|
1325 | try: |
---|
1326 | import pylab |
---|
1327 | except ImportError: |
---|
1328 | #ANUGA don't need pylab to work so the system doesn't |
---|
1329 | #rely on pylab being installed |
---|
1330 | return |
---|
1331 | |
---|
1332 | current_dir=getcwd()+sep+'abstract_2d_finite_volumes' |
---|
1333 | temp_dir = tempfile.mkdtemp('','figures') |
---|
1334 | # print 'temp_dir',temp_dir |
---|
1335 | fileName = temp_dir+sep+'time_series_3.csv' |
---|
1336 | file = open(fileName,"w") |
---|
1337 | file.write("Time,Stage,Speed,Momentum,Elevation\n\ |
---|
1338 | 1.0, 0, 0, 0, 10 \n\ |
---|
1339 | 2.0, 5, 2, 4, 10 \n\ |
---|
1340 | 3.0, 3, 3, 5, 10 \n") |
---|
1341 | file.close() |
---|
1342 | |
---|
1343 | fileName1 = temp_dir+sep+'time_series_4.csv' |
---|
1344 | file1 = open(fileName1,"w") |
---|
1345 | file1.write("Time,Stage,Speed,Momentum,Elevation\n\ |
---|
1346 | 1.0, 0, 0, 0, 5 \n\ |
---|
1347 | 2.0, -5, -2, -4, 5 \n\ |
---|
1348 | 3.0, -4, -3, -5, 5 \n") |
---|
1349 | file1.close() |
---|
1350 | |
---|
1351 | fileName2 = temp_dir+sep+'time_series_5.csv' |
---|
1352 | file2 = open(fileName2,"w") |
---|
1353 | file2.write("Time,Stage,Speed,Momentum,Elevation\n\ |
---|
1354 | 1.0, 0, 0, 0, 7 \n\ |
---|
1355 | 2.0, 4, -0.45, 57, 7 \n\ |
---|
1356 | 3.0, 6, -0.5, 56, 7 \n") |
---|
1357 | file2.close() |
---|
1358 | |
---|
1359 | dir, name=os.path.split(fileName) |
---|
1360 | make_plots_from_csv_file(directories_dic={dir:['gauge', 0, 0]}, |
---|
1361 | output_dir=temp_dir, |
---|
1362 | base_name='time_series_', |
---|
1363 | plot_numbers=['3-5'], |
---|
1364 | quantities=['Speed','Stage','Momentum'], |
---|
1365 | assess_all_csv_files=True, |
---|
1366 | extra_plot_name='test') |
---|
1367 | |
---|
1368 | # print 'stage+fileName[:-4]+test.png',dir+sep+'stage_'+name[:-4]+'_test.png' |
---|
1369 | assert(access(dir+sep+'stage_'+name[:-4]+'_test.png',F_OK)==True) |
---|
1370 | assert(access(dir+sep+'speed_'+name[:-4]+'_test.png',F_OK)==True) |
---|
1371 | assert(access(dir+sep+'momentum_'+name[:-4]+'_test.png',F_OK)==True) |
---|
1372 | |
---|
1373 | dir1, name1=os.path.split(fileName1) |
---|
1374 | assert(access(dir+sep+'stage_'+name1[:-4]+'_test.png',F_OK)==True) |
---|
1375 | assert(access(dir+sep+'speed_'+name1[:-4]+'_test.png',F_OK)==True) |
---|
1376 | assert(access(dir+sep+'momentum_'+name1[:-4]+'_test.png',F_OK)==True) |
---|
1377 | |
---|
1378 | |
---|
1379 | dir2, name2=os.path.split(fileName2) |
---|
1380 | assert(access(dir+sep+'stage_'+name2[:-4]+'_test.png',F_OK)==True) |
---|
1381 | assert(access(dir+sep+'speed_'+name2[:-4]+'_test.png',F_OK)==True) |
---|
1382 | assert(access(dir+sep+'momentum_'+name2[:-4]+'_test.png',F_OK)==True) |
---|
1383 | |
---|
1384 | del_dir(temp_dir) |
---|
1385 | |
---|
1386 | |
---|
1387 | |
---|
1388 | #------------------------------------------------------------- |
---|
1389 | if __name__ == "__main__": |
---|
1390 | suite = unittest.makeSuite(Test_Util,'test') |
---|
1391 | # suite = unittest.makeSuite(Test_Util,'test_get_min_max_values') |
---|
1392 | runner = unittest.TextTestRunner(verbosity=0) |
---|
1393 | runner.run(suite) |
---|
1394 | |
---|
1395 | |
---|
1396 | |
---|
1397 | |
---|