1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | |
---|
4 | import unittest |
---|
5 | from Numeric import zeros, array, allclose |
---|
6 | from math import sqrt, pi |
---|
7 | |
---|
8 | from util import * |
---|
9 | from config import epsilon |
---|
10 | |
---|
11 | |
---|
12 | def test_function(x, y): |
---|
13 | return x+y |
---|
14 | |
---|
15 | class TestCase(unittest.TestCase): |
---|
16 | def setUp(self): |
---|
17 | pass |
---|
18 | |
---|
19 | def tearDown(self): |
---|
20 | pass |
---|
21 | |
---|
22 | |
---|
23 | def test_gradient(self): |
---|
24 | x0 = 0.0; y0 = 0.0; z0 = 0.0 |
---|
25 | x1 = 1.0; y1 = 0.0; z1 = -1.0 |
---|
26 | x2 = 0.0; y2 = 1.0; z2 = 0.0 |
---|
27 | |
---|
28 | zx, zy = gradient(x0, y0, x1, y1, x2, y2, z0, z1, z2) |
---|
29 | |
---|
30 | assert zx == -1.0 |
---|
31 | assert zy == 0.0 |
---|
32 | |
---|
33 | |
---|
34 | |
---|
35 | |
---|
36 | def test_gradient2(self): |
---|
37 | |
---|
38 | from util import gradient |
---|
39 | x0 = 2.0/3; y0 = 2.0/3 |
---|
40 | x1= 8.0/3; y1 = 2.0/3 |
---|
41 | x2 = 2.0/3; y2 = 8.0/3 |
---|
42 | |
---|
43 | q0 = 2.0+2.0/3 |
---|
44 | q1 = 8.0+2.0/3 |
---|
45 | q2 = 2.0+8.0/3 |
---|
46 | |
---|
47 | #Gradient of fitted pwl surface |
---|
48 | a, b = gradient(x0, y0, x1, y1, x2, y2, q0, q1, q2) |
---|
49 | |
---|
50 | assert abs(a - 3.0) < epsilon |
---|
51 | assert abs(b - 1.0) < epsilon |
---|
52 | |
---|
53 | |
---|
54 | |
---|
55 | def test_that_C_extension_compiles(self): |
---|
56 | FN = 'util_ext.c' |
---|
57 | try: |
---|
58 | import util_ext |
---|
59 | except: |
---|
60 | from compile import compile |
---|
61 | |
---|
62 | try: |
---|
63 | compile(FN) |
---|
64 | except: |
---|
65 | raise 'Could not compile %s' %FN |
---|
66 | else: |
---|
67 | import util_ext |
---|
68 | |
---|
69 | |
---|
70 | def test_gradient_C_extension(self): |
---|
71 | from util_ext import gradient as gradient_c |
---|
72 | |
---|
73 | x0 = 2.0/3; y0 = 2.0/3 |
---|
74 | x1= 8.0/3; y1 = 2.0/3 |
---|
75 | x2 = 2.0/3; y2 = 8.0/3 |
---|
76 | |
---|
77 | q0 = 2.0+2.0/3 |
---|
78 | q1 = 8.0+2.0/3 |
---|
79 | q2 = 2.0+8.0/3 |
---|
80 | |
---|
81 | #Gradient of fitted pwl surface |
---|
82 | a, b = gradient_c(x0, y0, x1, y1, x2, y2, q0, q1, q2) |
---|
83 | |
---|
84 | assert abs(a - 3.0) < epsilon |
---|
85 | assert abs(b - 1.0) < epsilon |
---|
86 | |
---|
87 | |
---|
88 | def test_gradient_C_extension3(self): |
---|
89 | from util_ext import gradient as gradient_c |
---|
90 | |
---|
91 | from RandomArray import uniform, seed |
---|
92 | seed(17, 53) |
---|
93 | |
---|
94 | x0, x1, x2, y0, y1, y2 = uniform(0.0,3.0,6) |
---|
95 | |
---|
96 | q0 = uniform(0.0, 10.0, 4) |
---|
97 | q1 = uniform(1.0, 3.0, 4) |
---|
98 | q2 = uniform(7.0, 20.0, 4) |
---|
99 | |
---|
100 | |
---|
101 | for i in range(4): |
---|
102 | #Gradient of fitted pwl surface |
---|
103 | from util import gradient_python |
---|
104 | a_ref, b_ref = gradient(x0, y0, x1, y1, x2, y2, |
---|
105 | q0[i], q1[i], q2[i]) |
---|
106 | |
---|
107 | #print a_ref, b_ref |
---|
108 | a, b = gradient_c(x0, y0, x1, y1, x2, y2, |
---|
109 | q0[i], q1[i], q2[i]) |
---|
110 | |
---|
111 | #print a, a_ref, b, b_ref |
---|
112 | assert abs(a - a_ref) < epsilon |
---|
113 | assert abs(b - b_ref) < epsilon |
---|
114 | |
---|
115 | |
---|
116 | |
---|
117 | #Geometric |
---|
118 | #def test_distance(self): |
---|
119 | # from util import distance# |
---|
120 | # |
---|
121 | # self.failUnless( distance([4,2],[7,6]) == 5.0, |
---|
122 | # 'Distance is wrong!') |
---|
123 | # self.failUnless( allclose(distance([7,6],[9,8]), 2.82842712475), |
---|
124 | # 'distance is wrong!') |
---|
125 | # self.failUnless( allclose(distance([9,8],[4,2]), 7.81024967591), |
---|
126 | # 'distance is wrong!') |
---|
127 | # |
---|
128 | # self.failUnless( distance([9,8],[4,2]) == distance([4,2],[9,8]), |
---|
129 | # 'distance is wrong!') |
---|
130 | |
---|
131 | def test_angle(self): |
---|
132 | from util import angle |
---|
133 | |
---|
134 | assert allclose(angle([1.0, 1.0])/pi*180, 45.0) |
---|
135 | |
---|
136 | |
---|
137 | def test_anglediff(self): |
---|
138 | from util import anglediff |
---|
139 | |
---|
140 | assert allclose(anglediff([0.0, 1.], [1.0, 1.0])/pi*180, 45.0) |
---|
141 | |
---|
142 | |
---|
143 | |
---|
144 | |
---|
145 | def test_file_function_time(self): |
---|
146 | """Test that File function interpolates correctly |
---|
147 | between given times. No x,y dependency here. |
---|
148 | """ |
---|
149 | |
---|
150 | #Write file |
---|
151 | import os, time |
---|
152 | from config import time_format |
---|
153 | from math import sin, pi |
---|
154 | |
---|
155 | finaltime = 1200 |
---|
156 | filename = 'test_file_function.txt' |
---|
157 | fid = open(filename, 'w') |
---|
158 | start = time.mktime(time.strptime('2000', '%Y')) |
---|
159 | dt = 60 #One minute intervals |
---|
160 | t = 0.0 |
---|
161 | while t <= finaltime: |
---|
162 | t_string = time.strftime(time_format, time.gmtime(t+start)) |
---|
163 | fid.write('%s, %f %f %f\n' %(t_string, 2*t, t**2, sin(t*pi/600))) |
---|
164 | t += dt |
---|
165 | |
---|
166 | fid.close() |
---|
167 | |
---|
168 | F = file_function(filename) |
---|
169 | |
---|
170 | #Now try interpolation |
---|
171 | for i in range(20): |
---|
172 | t = i*10 |
---|
173 | q = F(t) |
---|
174 | |
---|
175 | #Exact linear intpolation |
---|
176 | assert allclose(q[0], 2*t) |
---|
177 | if i%6 == 0: |
---|
178 | assert allclose(q[1], t**2) |
---|
179 | assert allclose(q[2], sin(t*pi/600)) |
---|
180 | |
---|
181 | #Check non-exact |
---|
182 | |
---|
183 | t = 90 #Halfway between 60 and 120 |
---|
184 | q = F(t) |
---|
185 | assert allclose( (120**2 + 60**2)/2, q[1] ) |
---|
186 | assert allclose( (sin(120*pi/600) + sin(60*pi/600))/2, q[2] ) |
---|
187 | |
---|
188 | |
---|
189 | t = 100 #Two thirds of the way between between 60 and 120 |
---|
190 | q = F(t) |
---|
191 | assert allclose( 2*120**2/3 + 60**2/3, q[1] ) |
---|
192 | assert allclose( 2*sin(120*pi/600)/3 + sin(60*pi/600)/3, q[2] ) |
---|
193 | |
---|
194 | os.remove(filename) |
---|
195 | |
---|
196 | |
---|
197 | def test_file_function_time_with_domain(self): |
---|
198 | """Test that File function interpolates correctly |
---|
199 | between given times. No x,y dependency here. |
---|
200 | Use domain with starttime |
---|
201 | """ |
---|
202 | |
---|
203 | #Write file |
---|
204 | import os, time, calendar |
---|
205 | from config import time_format |
---|
206 | from math import sin, pi |
---|
207 | from domain import Domain |
---|
208 | |
---|
209 | finaltime = 1200 |
---|
210 | filename = 'test_file_function.txt' |
---|
211 | fid = open(filename, 'w') |
---|
212 | start = time.mktime(time.strptime('2000', '%Y')) |
---|
213 | dt = 60 #One minute intervals |
---|
214 | t = 0.0 |
---|
215 | while t <= finaltime: |
---|
216 | t_string = time.strftime(time_format, time.gmtime(t+start)) |
---|
217 | fid.write('%s, %f %f %f\n' %(t_string, 2*t, t**2, sin(t*pi/600))) |
---|
218 | t += dt |
---|
219 | |
---|
220 | fid.close() |
---|
221 | |
---|
222 | a = [0.0, 0.0] |
---|
223 | b = [4.0, 0.0] |
---|
224 | c = [0.0, 3.0] |
---|
225 | |
---|
226 | points = [a, b, c] |
---|
227 | vertices = [[0,1,2]] |
---|
228 | domain = Domain(points, vertices) |
---|
229 | |
---|
230 | #Check that domain.starttime is updated if non-existing |
---|
231 | F = file_function(filename, domain) |
---|
232 | assert allclose(domain.starttime, start) |
---|
233 | |
---|
234 | #Check that domain.starttime is updated if too early |
---|
235 | domain.starttime = start - 1 |
---|
236 | F = file_function(filename, domain) |
---|
237 | assert allclose(domain.starttime, start) |
---|
238 | |
---|
239 | #Check that domain.starttime isn't updated if later |
---|
240 | #domain.starttime = start + 1 |
---|
241 | #F = file_function(filename, domain) |
---|
242 | #assert allclose(domain.starttime, start+1) |
---|
243 | |
---|
244 | |
---|
245 | |
---|
246 | #Now try interpolation |
---|
247 | for i in range(20): |
---|
248 | t = i*10 |
---|
249 | q = F(t) |
---|
250 | |
---|
251 | #Exact linear intpolation |
---|
252 | assert allclose(q[0], 2*t) |
---|
253 | if i%6 == 0: |
---|
254 | assert allclose(q[1], t**2) |
---|
255 | assert allclose(q[2], sin(t*pi/600)) |
---|
256 | |
---|
257 | #Check non-exact |
---|
258 | |
---|
259 | t = 90 #Halfway between 60 and 120 |
---|
260 | q = F(t) |
---|
261 | assert allclose( (120**2 + 60**2)/2, q[1] ) |
---|
262 | assert allclose( (sin(120*pi/600) + sin(60*pi/600))/2, q[2] ) |
---|
263 | |
---|
264 | |
---|
265 | t = 100 #Two thirds of the way between between 60 and 120 |
---|
266 | q = F(t) |
---|
267 | assert allclose( 2*120**2/3 + 60**2/3, q[1] ) |
---|
268 | assert allclose( 2*sin(120*pi/600)/3 + sin(60*pi/600)/3, q[2] ) |
---|
269 | |
---|
270 | os.remove(filename) |
---|
271 | |
---|
272 | |
---|
273 | def test_file_function_time_with_domain_different_start(self): |
---|
274 | """Test that File function interpolates correctly |
---|
275 | between given times. No x,y dependency here. |
---|
276 | Use domain with a starttime later than that of file |
---|
277 | """ |
---|
278 | |
---|
279 | #Write file |
---|
280 | import os, time, calendar |
---|
281 | from config import time_format |
---|
282 | from math import sin, pi |
---|
283 | from domain import Domain |
---|
284 | |
---|
285 | finaltime = 1200 |
---|
286 | filename = 'test_file_function.txt' |
---|
287 | fid = open(filename, 'w') |
---|
288 | start = time.mktime(time.strptime('2000', '%Y')) |
---|
289 | dt = 60 #One minute intervals |
---|
290 | t = 0.0 |
---|
291 | while t <= finaltime: |
---|
292 | t_string = time.strftime(time_format, time.gmtime(t+start)) |
---|
293 | fid.write('%s, %f %f %f\n' %(t_string, 2*t, t**2, sin(t*pi/600))) |
---|
294 | t += dt |
---|
295 | |
---|
296 | fid.close() |
---|
297 | |
---|
298 | a = [0.0, 0.0] |
---|
299 | b = [4.0, 0.0] |
---|
300 | c = [0.0, 3.0] |
---|
301 | |
---|
302 | points = [a, b, c] |
---|
303 | vertices = [[0,1,2]] |
---|
304 | domain = Domain(points, vertices) |
---|
305 | |
---|
306 | #Check that domain.starttime isn't updated if later |
---|
307 | delta = 23 |
---|
308 | domain.starttime = start + delta |
---|
309 | F = file_function(filename, domain) |
---|
310 | assert allclose(domain.starttime, start+delta) |
---|
311 | |
---|
312 | |
---|
313 | |
---|
314 | |
---|
315 | #Now try interpolation with delta offset |
---|
316 | for i in range(20): |
---|
317 | t = i*10 |
---|
318 | q = F(t-delta) |
---|
319 | |
---|
320 | #Exact linear intpolation |
---|
321 | assert allclose(q[0], 2*t) |
---|
322 | if i%6 == 0: |
---|
323 | assert allclose(q[1], t**2) |
---|
324 | assert allclose(q[2], sin(t*pi/600)) |
---|
325 | |
---|
326 | #Check non-exact |
---|
327 | |
---|
328 | t = 90 #Halfway between 60 and 120 |
---|
329 | q = F(t-delta) |
---|
330 | assert allclose( (120**2 + 60**2)/2, q[1] ) |
---|
331 | assert allclose( (sin(120*pi/600) + sin(60*pi/600))/2, q[2] ) |
---|
332 | |
---|
333 | |
---|
334 | t = 100 #Two thirds of the way between between 60 and 120 |
---|
335 | q = F(t-delta) |
---|
336 | assert allclose( 2*120**2/3 + 60**2/3, q[1] ) |
---|
337 | assert allclose( 2*sin(120*pi/600)/3 + sin(60*pi/600)/3, q[2] ) |
---|
338 | |
---|
339 | os.remove(filename) |
---|
340 | |
---|
341 | |
---|
342 | def test_spatio_temporal_file_function(self): |
---|
343 | """Test that spatio temporal file function performs the correct |
---|
344 | interpolations in both time and space |
---|
345 | """ |
---|
346 | import time |
---|
347 | |
---|
348 | #Create sww file of simple propagation from left to right |
---|
349 | #through rectangular domain |
---|
350 | from shallow_water import Domain, Dirichlet_boundary |
---|
351 | from mesh_factory import rectangular |
---|
352 | from Numeric import take, concatenate, reshape |
---|
353 | |
---|
354 | #Create basic mesh and shallow water domain |
---|
355 | points, vertices, boundary = rectangular(3, 3) |
---|
356 | domain1 = Domain(points, vertices, boundary) |
---|
357 | |
---|
358 | from util import mean |
---|
359 | domain1.reduction = mean |
---|
360 | domain1.smooth = True #NOTE: Mimic sww output where each vertex has |
---|
361 | # only one value. |
---|
362 | |
---|
363 | domain1.default_order = 2 |
---|
364 | domain1.store = True |
---|
365 | domain1.set_datadir('.') |
---|
366 | domain1.set_name('spatio_temporal_boundary_source' + str(time.time())) |
---|
367 | domain1.quantities_to_be_stored = ['stage', 'xmomentum', 'ymomentum'] |
---|
368 | |
---|
369 | #Bed-slope, friction and IC at vertices (and interpolated elsewhere) |
---|
370 | domain1.set_quantity('elevation', 0) |
---|
371 | domain1.set_quantity('friction', 0) |
---|
372 | domain1.set_quantity('stage', 0) |
---|
373 | |
---|
374 | # Boundary conditions |
---|
375 | B0 = Dirichlet_boundary([0,0,0]) |
---|
376 | B6 = Dirichlet_boundary([0.6,0,0]) |
---|
377 | domain1.set_boundary({'left': B6, 'top': B6, 'right': B0, 'bottom': B0}) |
---|
378 | domain1.check_integrity() |
---|
379 | |
---|
380 | finaltime = 8 |
---|
381 | #Evolution |
---|
382 | for t in domain1.evolve(yieldstep = 0.1, finaltime = finaltime): |
---|
383 | pass |
---|
384 | #domain1.write_time() |
---|
385 | |
---|
386 | |
---|
387 | #Now read data from sww and check |
---|
388 | from Scientific.IO.NetCDF import NetCDFFile |
---|
389 | filename = domain1.get_name() + '.' + domain1.format |
---|
390 | fid = NetCDFFile(filename) |
---|
391 | |
---|
392 | x = fid.variables['x'][:] |
---|
393 | y = fid.variables['y'][:] |
---|
394 | stage = fid.variables['stage'][:] |
---|
395 | xmomentum = fid.variables['xmomentum'][:] |
---|
396 | ymomentum = fid.variables['ymomentum'][:] |
---|
397 | time = fid.variables['time'][:] |
---|
398 | |
---|
399 | #Take stage vertex values at last timestep on diagonal |
---|
400 | #Diagonal is identified by vertices: 0, 5, 10, 15 |
---|
401 | |
---|
402 | timestep = len(time)-1 #Last timestep |
---|
403 | d_stage = reshape(take(stage[timestep, :], [0,5,10,15]), (4,1)) |
---|
404 | d_uh = reshape(take(xmomentum[timestep, :], [0,5,10,15]), (4,1)) |
---|
405 | d_vh = reshape(take(ymomentum[timestep, :], [0,5,10,15]), (4,1)) |
---|
406 | D = concatenate( (d_stage, d_uh, d_vh), axis=1) |
---|
407 | |
---|
408 | #Reference interpolated values at midpoints on diagonal at |
---|
409 | #this timestep are |
---|
410 | r0 = (D[0] + D[1])/2 |
---|
411 | r1 = (D[1] + D[2])/2 |
---|
412 | r2 = (D[2] + D[3])/2 |
---|
413 | |
---|
414 | #And the midpoints are found now |
---|
415 | Dx = take(reshape(x, (16,1)), [0,5,10,15]) |
---|
416 | Dy = take(reshape(y, (16,1)), [0,5,10,15]) |
---|
417 | |
---|
418 | diag = concatenate( (Dx, Dy), axis=1) |
---|
419 | d_midpoints = (diag[1:] + diag[:-1])/2 |
---|
420 | |
---|
421 | #Let us see if the file function can find the correct |
---|
422 | #values at the midpoints at the last timestep: |
---|
423 | f = file_function(filename, domain1, |
---|
424 | interpolation_points = d_midpoints) |
---|
425 | |
---|
426 | q = f(timestep/10., point_id=0); assert allclose(r0, q) |
---|
427 | q = f(timestep/10., point_id=1); assert allclose(r1, q) |
---|
428 | q = f(timestep/10., point_id=2); assert allclose(r2, q) |
---|
429 | |
---|
430 | |
---|
431 | ################## |
---|
432 | #Now do the same for the first timestep |
---|
433 | |
---|
434 | timestep = 0 #First timestep |
---|
435 | d_stage = reshape(take(stage[timestep, :], [0,5,10,15]), (4,1)) |
---|
436 | d_uh = reshape(take(xmomentum[timestep, :], [0,5,10,15]), (4,1)) |
---|
437 | d_vh = reshape(take(ymomentum[timestep, :], [0,5,10,15]), (4,1)) |
---|
438 | D = concatenate( (d_stage, d_uh, d_vh), axis=1) |
---|
439 | |
---|
440 | #Reference interpolated values at midpoints on diagonal at |
---|
441 | #this timestep are |
---|
442 | r0 = (D[0] + D[1])/2 |
---|
443 | r1 = (D[1] + D[2])/2 |
---|
444 | r2 = (D[2] + D[3])/2 |
---|
445 | |
---|
446 | #Let us see if the file function can find the correct |
---|
447 | #values |
---|
448 | q = f(0, point_id=0); assert allclose(r0, q) |
---|
449 | q = f(0, point_id=1); assert allclose(r1, q) |
---|
450 | q = f(0, point_id=2); assert allclose(r2, q) |
---|
451 | |
---|
452 | |
---|
453 | ################## |
---|
454 | #Now do it again for a timestep in the middle |
---|
455 | |
---|
456 | timestep = 33 |
---|
457 | d_stage = reshape(take(stage[timestep, :], [0,5,10,15]), (4,1)) |
---|
458 | d_uh = reshape(take(xmomentum[timestep, :], [0,5,10,15]), (4,1)) |
---|
459 | d_vh = reshape(take(ymomentum[timestep, :], [0,5,10,15]), (4,1)) |
---|
460 | D = concatenate( (d_stage, d_uh, d_vh), axis=1) |
---|
461 | |
---|
462 | #Reference interpolated values at midpoints on diagonal at |
---|
463 | #this timestep are |
---|
464 | r0 = (D[0] + D[1])/2 |
---|
465 | r1 = (D[1] + D[2])/2 |
---|
466 | r2 = (D[2] + D[3])/2 |
---|
467 | |
---|
468 | q = f(timestep/10., point_id=0); assert allclose(r0, q) |
---|
469 | q = f(timestep/10., point_id=1); assert allclose(r1, q) |
---|
470 | q = f(timestep/10., point_id=2); assert allclose(r2, q) |
---|
471 | |
---|
472 | |
---|
473 | ################## |
---|
474 | #Now check temporal interpolation |
---|
475 | #Halfway between timestep 15 and 16 |
---|
476 | |
---|
477 | timestep = 15 |
---|
478 | d_stage = reshape(take(stage[timestep, :], [0,5,10,15]), (4,1)) |
---|
479 | d_uh = reshape(take(xmomentum[timestep, :], [0,5,10,15]), (4,1)) |
---|
480 | d_vh = reshape(take(ymomentum[timestep, :], [0,5,10,15]), (4,1)) |
---|
481 | D = concatenate( (d_stage, d_uh, d_vh), axis=1) |
---|
482 | |
---|
483 | #Reference interpolated values at midpoints on diagonal at |
---|
484 | #this timestep are |
---|
485 | r0_0 = (D[0] + D[1])/2 |
---|
486 | r1_0 = (D[1] + D[2])/2 |
---|
487 | r2_0 = (D[2] + D[3])/2 |
---|
488 | |
---|
489 | # |
---|
490 | timestep = 16 |
---|
491 | d_stage = reshape(take(stage[timestep, :], [0,5,10,15]), (4,1)) |
---|
492 | d_uh = reshape(take(xmomentum[timestep, :], [0,5,10,15]), (4,1)) |
---|
493 | d_vh = reshape(take(ymomentum[timestep, :], [0,5,10,15]), (4,1)) |
---|
494 | D = concatenate( (d_stage, d_uh, d_vh), axis=1) |
---|
495 | |
---|
496 | #Reference interpolated values at midpoints on diagonal at |
---|
497 | #this timestep are |
---|
498 | r0_1 = (D[0] + D[1])/2 |
---|
499 | r1_1 = (D[1] + D[2])/2 |
---|
500 | r2_1 = (D[2] + D[3])/2 |
---|
501 | |
---|
502 | # The reference values are |
---|
503 | r0 = (r0_0 + r0_1)/2 |
---|
504 | r1 = (r1_0 + r1_1)/2 |
---|
505 | r2 = (r2_0 + r2_1)/2 |
---|
506 | |
---|
507 | q = f((timestep - 0.5)/10., point_id=0); assert allclose(r0, q) |
---|
508 | q = f((timestep - 0.5)/10., point_id=1); assert allclose(r1, q) |
---|
509 | q = f((timestep - 0.5)/10., point_id=2); assert allclose(r2, q) |
---|
510 | |
---|
511 | ################## |
---|
512 | #Finally check interpolation 2 thirds of the way |
---|
513 | #between timestep 15 and 16 |
---|
514 | |
---|
515 | # The reference values are |
---|
516 | r0 = (r0_0 + 2*r0_1)/3 |
---|
517 | r1 = (r1_0 + 2*r1_1)/3 |
---|
518 | r2 = (r2_0 + 2*r2_1)/3 |
---|
519 | |
---|
520 | #And the file function gives |
---|
521 | q = f((timestep - 1.0/3)/10., point_id=0); assert allclose(r0, q) |
---|
522 | q = f((timestep - 1.0/3)/10., point_id=1); assert allclose(r1, q) |
---|
523 | q = f((timestep - 1.0/3)/10., point_id=2); assert allclose(r2, q) |
---|
524 | |
---|
525 | fid.close() |
---|
526 | |
---|
527 | def test_xya_ascii(self): |
---|
528 | import time, os |
---|
529 | FN = 'xyatest' + str(time.time()) + '.xya' |
---|
530 | fid = open(FN, 'w') |
---|
531 | fid.write(' %s %s %s\n' %('a1', 'a2', 'a3') ) |
---|
532 | fid.write('%f %f %f %f %f\n' %(0,1,10,20,30) ) |
---|
533 | fid.write('%f %f %f %f %f\n' %(1,0,30,20,10) ) |
---|
534 | fid.write('%f %f %f %f %f\n' %(1,1,40.2,40.3,40.4) ) |
---|
535 | fid.close() |
---|
536 | |
---|
537 | points, attributes = read_xya(FN, format = 'asc') |
---|
538 | |
---|
539 | assert allclose(points, [ [0,1], [1,0], [1,1] ]) |
---|
540 | assert allclose(attributes['a1'], [10,30,40.2]) |
---|
541 | assert allclose(attributes['a2'], [20,20,40.3]) |
---|
542 | assert allclose(attributes['a3'], [30,10,40.4]) |
---|
543 | |
---|
544 | os.remove(FN) |
---|
545 | |
---|
546 | def test_xya_ascii_w_names(self): |
---|
547 | import time, os |
---|
548 | FN = 'xyatest' + str(time.time()) + '.xya' |
---|
549 | fid = open(FN, 'w') |
---|
550 | fid.write(' %s %s %s\n' %('a1', 'a2', 'a3') ) |
---|
551 | fid.write('%f %f %f %f %f\n' %(0,1,10,20,30) ) |
---|
552 | fid.write('%f %f %f %f %f\n' %(1,0,30,20,10) ) |
---|
553 | fid.write('%f %f %f %f %f\n' %(1,1,40.2,40.3,40.4) ) |
---|
554 | fid.close() |
---|
555 | |
---|
556 | points, attributes = read_xya(FN, format = 'asc') |
---|
557 | |
---|
558 | assert allclose(points, [ [0,1], [1,0], [1,1] ]) |
---|
559 | |
---|
560 | assert allclose(attributes['a1'], [10,30,40.2]) |
---|
561 | assert allclose(attributes['a2'], [20,20,40.3]) |
---|
562 | assert allclose(attributes['a3'], [30,10,40.4]) |
---|
563 | |
---|
564 | |
---|
565 | os.remove(FN) |
---|
566 | |
---|
567 | |
---|
568 | |
---|
569 | |
---|
570 | #Polygon stuff |
---|
571 | def test_polygon_function_constants(self): |
---|
572 | p1 = [[0,0], [10,0], [10,10], [0,10]] |
---|
573 | p2 = [[0,0], [10,10], [15,5], [20, 10], [25,0], [30,10], [40,-10]] |
---|
574 | |
---|
575 | f = Polygon_function( [(p1, 1.0)] ) |
---|
576 | z = f([5, 5, 27, 35], [5, 9, 8, -5]) #Two first inside p1 |
---|
577 | assert allclose(z, [1,1,0,0]) |
---|
578 | |
---|
579 | |
---|
580 | f = Polygon_function( [(p2, 2.0)] ) |
---|
581 | z = f([5, 5, 27, 35], [5, 9, 8, -5]) #First and last inside p2 |
---|
582 | assert allclose(z, [2,0,0,2]) |
---|
583 | |
---|
584 | |
---|
585 | #Combined |
---|
586 | f = Polygon_function( [(p1, 1.0), (p2, 2.0)] ) |
---|
587 | z = f([5, 5, 27, 35], [5, 9, 8, -5]) |
---|
588 | assert allclose(z, [2,1,0,2]) |
---|
589 | |
---|
590 | |
---|
591 | def test_polygon_function_callable(self): |
---|
592 | """Check that values passed into Polygon_function can be callable |
---|
593 | themselves. |
---|
594 | """ |
---|
595 | p1 = [[0,0], [10,0], [10,10], [0,10]] |
---|
596 | p2 = [[0,0], [10,10], [15,5], [20, 10], [25,0], [30,10], [40,-10]] |
---|
597 | |
---|
598 | f = Polygon_function( [(p1, test_function)] ) |
---|
599 | z = f([5, 5, 27, 35], [5, 9, 8, -5]) #Two first inside p1 |
---|
600 | assert allclose(z, [10,14,0,0]) |
---|
601 | |
---|
602 | #Combined |
---|
603 | f = Polygon_function( [(p1, test_function), (p2, 2.0)] ) |
---|
604 | z = f([5, 5, 27, 35], [5, 9, 8, -5]) |
---|
605 | assert allclose(z, [2,14,0,2]) |
---|
606 | |
---|
607 | |
---|
608 | #Combined w default |
---|
609 | f = Polygon_function( [(p1, test_function), (p2, 2.0)], default = 3.14) |
---|
610 | z = f([5, 5, 27, 35], [5, 9, 8, -5]) |
---|
611 | assert allclose(z, [2,14,3.14,2]) |
---|
612 | |
---|
613 | |
---|
614 | #Combined w default func |
---|
615 | f = Polygon_function( [(p1, test_function), (p2, 2.0)], |
---|
616 | default = test_function) |
---|
617 | z = f([5, 5, 27, 35], [5, 9, 8, -5]) |
---|
618 | assert allclose(z, [2,14,35,2]) |
---|
619 | |
---|
620 | |
---|
621 | def test_point_on_line(self): |
---|
622 | |
---|
623 | #Endpoints first |
---|
624 | assert point_on_line( 0, 0, 0,0, 1,0 ) |
---|
625 | assert point_on_line( 1, 0, 0,0, 1,0 ) |
---|
626 | |
---|
627 | #Then points on line |
---|
628 | assert point_on_line( 0.5, 0, 0,0, 1,0 ) |
---|
629 | assert point_on_line( 0, 0.5, 0,1, 0,0 ) |
---|
630 | assert point_on_line( 1, 0.5, 1,1, 1,0 ) |
---|
631 | assert point_on_line( 0.5, 0.5, 0,0, 1,1 ) |
---|
632 | |
---|
633 | #Then points not on line |
---|
634 | assert not point_on_line( 0.5, 0, 0,1, 1,1 ) |
---|
635 | assert not point_on_line( 0, 0.5, 0,0, 1,1 ) |
---|
636 | |
---|
637 | #From real example that failed |
---|
638 | assert not point_on_line( 40,50, 40,20, 40,40 ) |
---|
639 | |
---|
640 | |
---|
641 | #From real example that failed |
---|
642 | assert not point_on_line( 40,19, 40,20, 40,40 ) |
---|
643 | |
---|
644 | |
---|
645 | |
---|
646 | |
---|
647 | def test_inside_polygon(self): |
---|
648 | |
---|
649 | |
---|
650 | #Simplest case: Polygon is the unit square |
---|
651 | polygon = [[0,0], [1,0], [1,1], [0,1]] |
---|
652 | |
---|
653 | assert inside_polygon( (0.5, 0.5), polygon ) |
---|
654 | assert not inside_polygon( (0.5, 1.5), polygon ) |
---|
655 | assert not inside_polygon( (0.5, -0.5), polygon ) |
---|
656 | assert not inside_polygon( (-0.5, 0.5), polygon ) |
---|
657 | assert not inside_polygon( (1.5, 0.5), polygon ) |
---|
658 | |
---|
659 | #Try point on borders |
---|
660 | assert inside_polygon( (1., 0.5), polygon, closed=True) |
---|
661 | assert inside_polygon( (0.5, 1), polygon, closed=True) |
---|
662 | assert inside_polygon( (0., 0.5), polygon, closed=True) |
---|
663 | assert inside_polygon( (0.5, 0.), polygon, closed=True) |
---|
664 | |
---|
665 | assert not inside_polygon( (0.5, 1), polygon, closed=False) |
---|
666 | assert not inside_polygon( (0., 0.5), polygon, closed=False) |
---|
667 | assert not inside_polygon( (0.5, 0.), polygon, closed=False) |
---|
668 | assert not inside_polygon( (1., 0.5), polygon, closed=False) |
---|
669 | |
---|
670 | |
---|
671 | |
---|
672 | #From real example (that failed) |
---|
673 | polygon = [[20,20], [40,20], [40,40], [20,40]] |
---|
674 | points = [ [40, 50] ] |
---|
675 | res = inside_polygon(points, polygon) |
---|
676 | assert len(res) == 0 |
---|
677 | |
---|
678 | polygon = [[20,20], [40,20], [40,40], [20,40]] |
---|
679 | points = [ [25, 25], [30, 20], [40, 50], [90, 20], [40, 90] ] |
---|
680 | res = inside_polygon(points, polygon) |
---|
681 | assert len(res) == 2 |
---|
682 | assert allclose(res, [0,1]) |
---|
683 | |
---|
684 | |
---|
685 | |
---|
686 | #More convoluted and non convex polygon |
---|
687 | polygon = [[0,0], [1,0], [0.5,-1], [2, -1], [2,1], [0,1]] |
---|
688 | assert inside_polygon( (0.5, 0.5), polygon ) |
---|
689 | assert inside_polygon( (1, -0.5), polygon ) |
---|
690 | assert inside_polygon( (1.5, 0), polygon ) |
---|
691 | |
---|
692 | assert not inside_polygon( (0.5, 1.5), polygon ) |
---|
693 | assert not inside_polygon( (0.5, -0.5), polygon ) |
---|
694 | |
---|
695 | #Now try the vector formulation returning indices |
---|
696 | points = [ [0.5, 0.5], [1, -0.5], [1.5, 0], [0.5, 1.5], [0.5, -0.5]] |
---|
697 | res = inside_polygon( points, polygon ) |
---|
698 | |
---|
699 | assert allclose( res, [0,1,2] ) |
---|
700 | |
---|
701 | #Very convoluted polygon |
---|
702 | polygon = [[0,0], [10,10], [15,5], [20, 10], [25,0], [30,10], [40,-10]] |
---|
703 | assert inside_polygon( (5, 5), polygon ) |
---|
704 | assert inside_polygon( (17, 7), polygon ) |
---|
705 | assert inside_polygon( (27, 2), polygon ) |
---|
706 | assert inside_polygon( (35, -5), polygon ) |
---|
707 | assert not inside_polygon( (15, 7), polygon ) |
---|
708 | assert not inside_polygon( (24, 3), polygon ) |
---|
709 | assert not inside_polygon( (25, -10), polygon ) |
---|
710 | |
---|
711 | |
---|
712 | |
---|
713 | #Another combination (that failed) |
---|
714 | polygon = [[0,0], [10,0], [10,10], [0,10]] |
---|
715 | assert inside_polygon( (5, 5), polygon ) |
---|
716 | assert inside_polygon( (7, 7), polygon ) |
---|
717 | assert not inside_polygon( (-17, 7), polygon ) |
---|
718 | assert not inside_polygon( (7, 17), polygon ) |
---|
719 | assert not inside_polygon( (17, 7), polygon ) |
---|
720 | assert not inside_polygon( (27, 8), polygon ) |
---|
721 | assert not inside_polygon( (35, -5), polygon ) |
---|
722 | |
---|
723 | |
---|
724 | |
---|
725 | |
---|
726 | #Multiple polygons |
---|
727 | |
---|
728 | polygon = [[0,0], [1,0], [1,1], [0,1], [0,0], |
---|
729 | [10,10], [11,10], [11,11], [10,11], [10,10]] |
---|
730 | assert inside_polygon( (0.5, 0.5), polygon ) |
---|
731 | assert inside_polygon( (10.5, 10.5), polygon ) |
---|
732 | |
---|
733 | #FIXME: Fails if point is 5.5, 5.5 |
---|
734 | assert not inside_polygon( (0, 5.5), polygon ) |
---|
735 | |
---|
736 | #Polygon with a hole |
---|
737 | polygon = [[-1,-1], [2,-1], [2,2], [-1,2], [-1,-1], |
---|
738 | [0,0], [1,0], [1,1], [0,1], [0,0]] |
---|
739 | |
---|
740 | assert inside_polygon( (0, -0.5), polygon ) |
---|
741 | assert not inside_polygon( (0.5, 0.5), polygon ) |
---|
742 | |
---|
743 | |
---|
744 | def test_populate_polygon(self): |
---|
745 | |
---|
746 | polygon = [[0,0], [1,0], [1,1], [0,1]] |
---|
747 | points = populate_polygon(polygon, 5) |
---|
748 | |
---|
749 | assert len(points) == 5 |
---|
750 | for point in points: |
---|
751 | assert inside_polygon(point, polygon) |
---|
752 | |
---|
753 | |
---|
754 | #Very convoluted polygon |
---|
755 | polygon = [[0,0], [10,10], [15,5], [20, 10], [25,0], [30,10], [40,-10]] |
---|
756 | |
---|
757 | points = populate_polygon(polygon, 5) |
---|
758 | |
---|
759 | assert len(points) == 5 |
---|
760 | for point in points: |
---|
761 | assert inside_polygon(point, polygon) |
---|
762 | |
---|
763 | |
---|
764 | |
---|
765 | #------------------------------------------------------------- |
---|
766 | if __name__ == "__main__": |
---|
767 | suite = unittest.makeSuite(TestCase,'test') |
---|
768 | runner = unittest.TextTestRunner() |
---|
769 | runner.run(suite) |
---|
770 | |
---|
771 | |
---|
772 | |
---|
773 | |
---|
774 | |
---|