1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | #TEST |
---|
4 | |
---|
5 | #import time, os |
---|
6 | |
---|
7 | |
---|
8 | import sys |
---|
9 | import os |
---|
10 | import unittest |
---|
11 | from math import sqrt |
---|
12 | import tempfile |
---|
13 | import csv |
---|
14 | |
---|
15 | from Scientific.IO.NetCDF import NetCDFFile |
---|
16 | |
---|
17 | import numpy as num |
---|
18 | |
---|
19 | |
---|
20 | |
---|
21 | # ANUGA code imports |
---|
22 | from interpolate import * |
---|
23 | from anuga.coordinate_transforms.geo_reference import Geo_reference |
---|
24 | from anuga.shallow_water import Domain, Transmissive_boundary |
---|
25 | from anuga.utilities.numerical_tools import mean, NAN |
---|
26 | from anuga.shallow_water.sww_file import SWW_file |
---|
27 | from anuga.geospatial_data.geospatial_data import Geospatial_data |
---|
28 | from anuga.pmesh.mesh import Mesh |
---|
29 | |
---|
30 | def distance(x, y): |
---|
31 | return sqrt(num.sum((num.array(x)-num.array(y))**2)) |
---|
32 | |
---|
33 | def linear_function(point): |
---|
34 | point = num.array(point) |
---|
35 | return point[:,0]+point[:,1] |
---|
36 | |
---|
37 | |
---|
38 | class Test_Interpolate(unittest.TestCase): |
---|
39 | |
---|
40 | def setUp(self): |
---|
41 | |
---|
42 | import time |
---|
43 | from mesh_factory import rectangular |
---|
44 | |
---|
45 | |
---|
46 | #Create basic mesh |
---|
47 | points, vertices, boundary = rectangular(2, 2) |
---|
48 | |
---|
49 | #Create shallow water domain |
---|
50 | domain = Domain(points, vertices, boundary) |
---|
51 | domain.default_order=2 |
---|
52 | |
---|
53 | |
---|
54 | #Set some field values |
---|
55 | domain.set_quantity('elevation', lambda x,y: -x) |
---|
56 | domain.set_quantity('friction', 0.03) |
---|
57 | |
---|
58 | |
---|
59 | ###################### |
---|
60 | # Boundary conditions |
---|
61 | B = Transmissive_boundary(domain) |
---|
62 | domain.set_boundary( {'left': B, 'right': B, 'top': B, 'bottom': B}) |
---|
63 | |
---|
64 | |
---|
65 | ###################### |
---|
66 | #Initial condition - with jumps |
---|
67 | |
---|
68 | bed = domain.quantities['elevation'].vertex_values |
---|
69 | stage = num.zeros(bed.shape, num.float) |
---|
70 | |
---|
71 | h = 0.3 |
---|
72 | for i in range(stage.shape[0]): |
---|
73 | if i % 2 == 0: |
---|
74 | stage[i,:] = bed[i,:] + h |
---|
75 | else: |
---|
76 | stage[i,:] = bed[i,:] |
---|
77 | |
---|
78 | domain.set_quantity('stage', stage) |
---|
79 | |
---|
80 | domain.distribute_to_vertices_and_edges() |
---|
81 | |
---|
82 | |
---|
83 | self.domain = domain |
---|
84 | |
---|
85 | C = domain.get_vertex_coordinates() |
---|
86 | self.X = C[:,0:6:2].copy() |
---|
87 | self.Y = C[:,1:6:2].copy() |
---|
88 | |
---|
89 | self.F = bed |
---|
90 | |
---|
91 | |
---|
92 | |
---|
93 | def tearDown(self): |
---|
94 | pass |
---|
95 | |
---|
96 | def test_datapoint_at_centroid(self): |
---|
97 | a = [0.0, 0.0] |
---|
98 | b = [0.0, 2.0] |
---|
99 | c = [2.0,0.0] |
---|
100 | points = [a, b, c] |
---|
101 | vertices = [ [1,0,2] ] #bac |
---|
102 | |
---|
103 | data = [ [2.0/3, 2.0/3] ] #Use centroid as one data point |
---|
104 | |
---|
105 | interp = Interpolate(points, vertices) |
---|
106 | A, _, _, _ = interp._build_interpolation_matrix_A(data) |
---|
107 | assert num.allclose(A.todense(), [[1./3, 1./3, 1./3]]) |
---|
108 | |
---|
109 | def test_datapoint_in_hole(self): |
---|
110 | # create 3 right-angled triangles arranged in a bigger triangle |
---|
111 | a = [0.0, 0.0] #0 |
---|
112 | b = [0.0, 2.0] #1 |
---|
113 | c = [2.0,0.0] #2 |
---|
114 | d = [0.0,4.0] #3 |
---|
115 | e = [2.0,2.0] #4 |
---|
116 | f = [4.0,0.0] #5 |
---|
117 | points = [a, b, c, d, e, f] |
---|
118 | vertices = [ [1,0,2], [3,1,4], [4,2,5] ] #bac dbe ecf |
---|
119 | |
---|
120 | point_in_hole = [1.5, 1.5] # a point in the hole |
---|
121 | data = [ [20, 20], [0.3, 0.3], point_in_hole, [2.5, 0.3], [30, 30] ] #some points inside and outside the hole |
---|
122 | |
---|
123 | # any function for the vertices, we don't care about the result |
---|
124 | f = num.array([linear_function(points), 2*linear_function(points)]) |
---|
125 | f = num.transpose(f) |
---|
126 | |
---|
127 | interp = Interpolate(points, vertices) |
---|
128 | interp.interpolate(f, data) |
---|
129 | |
---|
130 | assert not set(interp.inside_poly_indices).intersection(set(interp.outside_poly_indices)), \ |
---|
131 | 'Some points are in both lists!' |
---|
132 | assert len(interp.inside_poly_indices) == 2 |
---|
133 | assert len(interp.outside_poly_indices) == 3 |
---|
134 | |
---|
135 | interp.outside_poly_indices.sort() |
---|
136 | assert interp.outside_poly_indices[1] == 2, \ |
---|
137 | 'third outside point should be inside the hole!' |
---|
138 | |
---|
139 | def test_simple_interpolation_example(self): |
---|
140 | |
---|
141 | from mesh_factory import rectangular |
---|
142 | from shallow_water import Domain |
---|
143 | from abstract_2d_finite_volumes.quantity import Quantity |
---|
144 | |
---|
145 | # Create basic mesh |
---|
146 | points, vertices, boundary = rectangular(1, 3) |
---|
147 | |
---|
148 | # Create shallow water domain |
---|
149 | domain = Domain(points, vertices, boundary) |
---|
150 | |
---|
151 | #---------------- |
---|
152 | #Constant values |
---|
153 | #---------------- |
---|
154 | quantity = Quantity(domain,[[0,0,0],[1,1,1],[2,2,2],[3,3,3], |
---|
155 | [4,4,4],[5,5,5]]) |
---|
156 | |
---|
157 | |
---|
158 | x, y, vertex_values, triangles = quantity.get_vertex_values(xy=True, smooth=False) |
---|
159 | vertex_coordinates = num.concatenate( (x[:, num.newaxis], y[:, num.newaxis]), axis=1 ) |
---|
160 | # FIXME: This concat should roll into get_vertex_values |
---|
161 | |
---|
162 | |
---|
163 | # Get interpolated values at centroids |
---|
164 | interpolation_points = domain.get_centroid_coordinates() |
---|
165 | answer = quantity.get_values(location='centroids') |
---|
166 | |
---|
167 | I = Interpolate(vertex_coordinates, triangles) |
---|
168 | result = I.interpolate(vertex_values, interpolation_points) |
---|
169 | assert num.allclose(result, answer) |
---|
170 | |
---|
171 | |
---|
172 | #---------------- |
---|
173 | # Variable values |
---|
174 | #---------------- |
---|
175 | quantity = Quantity(domain,[[0,1,2],[3,1,7],[2,1,2],[3,3,7], |
---|
176 | [1,4,-9],[2,5,0]]) |
---|
177 | |
---|
178 | x, y, vertex_values, triangles = quantity.get_vertex_values(xy=True, smooth=False) |
---|
179 | vertex_coordinates = num.concatenate( (x[:, num.newaxis], y[:, num.newaxis]), axis=1 ) |
---|
180 | # FIXME: This concat should roll into get_vertex_values |
---|
181 | |
---|
182 | |
---|
183 | # Get interpolated values at centroids |
---|
184 | interpolation_points = domain.get_centroid_coordinates() |
---|
185 | answer = quantity.get_values(location='centroids') |
---|
186 | |
---|
187 | I = Interpolate(vertex_coordinates, triangles) |
---|
188 | result = I.interpolate(vertex_values, interpolation_points) |
---|
189 | assert num.allclose(result, answer) |
---|
190 | |
---|
191 | |
---|
192 | def test_simple_interpolation_example_using_direct_interface(self): |
---|
193 | |
---|
194 | from mesh_factory import rectangular |
---|
195 | from shallow_water import Domain |
---|
196 | from abstract_2d_finite_volumes.quantity import Quantity |
---|
197 | |
---|
198 | # Create basic mesh |
---|
199 | points, vertices, boundary = rectangular(1, 3) |
---|
200 | |
---|
201 | # Create shallow water domain |
---|
202 | domain = Domain(points, vertices, boundary) |
---|
203 | |
---|
204 | #---------------- |
---|
205 | # Constant values |
---|
206 | #---------------- |
---|
207 | quantity = Quantity(domain,[[0,0,0],[1,1,1],[2,2,2],[3,3,3], |
---|
208 | [4,4,4],[5,5,5]]) |
---|
209 | |
---|
210 | |
---|
211 | x, y, vertex_values, triangles = quantity.get_vertex_values(xy=True, smooth=False) |
---|
212 | vertex_coordinates = num.concatenate( (x[:, num.newaxis], y[:, num.newaxis]), axis=1 ) |
---|
213 | # FIXME: This concat should roll into get_vertex_values |
---|
214 | |
---|
215 | |
---|
216 | # Get interpolated values at centroids |
---|
217 | interpolation_points = domain.get_centroid_coordinates() |
---|
218 | answer = quantity.get_values(location='centroids') |
---|
219 | |
---|
220 | result = interpolate(vertex_coordinates, triangles, vertex_values, interpolation_points) |
---|
221 | assert num.allclose(result, answer) |
---|
222 | |
---|
223 | |
---|
224 | #---------------- |
---|
225 | # Variable values |
---|
226 | #---------------- |
---|
227 | quantity = Quantity(domain,[[0,1,2],[3,1,7],[2,1,2],[3,3,7], |
---|
228 | [1,4,-9],[2,5,0]]) |
---|
229 | |
---|
230 | x, y, vertex_values, triangles = quantity.get_vertex_values(xy=True, smooth=False) |
---|
231 | vertex_coordinates = num.concatenate( (x[:, num.newaxis], y[:, num.newaxis]), axis=1 ) |
---|
232 | # FIXME: This concat should roll into get_vertex_values |
---|
233 | |
---|
234 | |
---|
235 | # Get interpolated values at centroids |
---|
236 | interpolation_points = domain.get_centroid_coordinates() |
---|
237 | answer = quantity.get_values(location='centroids') |
---|
238 | |
---|
239 | result = interpolate(vertex_coordinates, triangles, |
---|
240 | vertex_values, interpolation_points) |
---|
241 | assert num.allclose(result, answer) |
---|
242 | |
---|
243 | |
---|
244 | def test_simple_interpolation_example_using_direct_interface_and_caching(self): |
---|
245 | |
---|
246 | from mesh_factory import rectangular |
---|
247 | from shallow_water import Domain |
---|
248 | from abstract_2d_finite_volumes.quantity import Quantity |
---|
249 | |
---|
250 | # Create basic mesh |
---|
251 | points, vertices, boundary = rectangular(1, 3) |
---|
252 | |
---|
253 | # Create shallow water domain |
---|
254 | domain = Domain(points, vertices, boundary) |
---|
255 | |
---|
256 | #---------------- |
---|
257 | # First call |
---|
258 | #---------------- |
---|
259 | quantity = Quantity(domain,[[0,1,2],[3,1,7],[2,1,2],[3,3,7], |
---|
260 | [1,4,-9],[2,5,0]]) |
---|
261 | |
---|
262 | x, y, vertex_values, triangles = quantity.get_vertex_values(xy=True, smooth=False) |
---|
263 | vertex_coordinates = num.concatenate( (x[:, num.newaxis], y[:, num.newaxis]), axis=1 ) |
---|
264 | # FIXME: This concat should roll into get_vertex_values |
---|
265 | |
---|
266 | |
---|
267 | # Get interpolated values at centroids |
---|
268 | interpolation_points = domain.get_centroid_coordinates() |
---|
269 | answer = quantity.get_values(location='centroids') |
---|
270 | |
---|
271 | result = interpolate(vertex_coordinates, triangles, |
---|
272 | vertex_values, interpolation_points, |
---|
273 | use_cache=True, |
---|
274 | verbose=False) |
---|
275 | assert num.allclose(result, answer) |
---|
276 | |
---|
277 | # Second call using the cache |
---|
278 | result = interpolate(vertex_coordinates, triangles, |
---|
279 | vertex_values, interpolation_points, |
---|
280 | use_cache=True, |
---|
281 | verbose=False) |
---|
282 | assert num.allclose(result, answer) |
---|
283 | |
---|
284 | |
---|
285 | def test_quad_tree(self): |
---|
286 | p0 = [-10.0, -10.0] |
---|
287 | p1 = [20.0, -10.0] |
---|
288 | p2 = [-10.0, 20.0] |
---|
289 | p3 = [10.0, 50.0] |
---|
290 | p4 = [30.0, 30.0] |
---|
291 | p5 = [50.0, 10.0] |
---|
292 | p6 = [40.0, 60.0] |
---|
293 | p7 = [60.0, 40.0] |
---|
294 | p8 = [-66.0, 20.0] |
---|
295 | p9 = [10.0, -66.0] |
---|
296 | |
---|
297 | points = [p0, p1, p2, p3, p4, p5, p6, p7, p8, p9] |
---|
298 | triangles = [ [0, 1, 2], |
---|
299 | [3, 2, 4], |
---|
300 | [4, 2, 1], |
---|
301 | [4, 1, 5], |
---|
302 | [3, 4, 6], |
---|
303 | [6, 4, 7], |
---|
304 | [7, 4, 5], |
---|
305 | [8, 0, 2], |
---|
306 | [0, 9, 1]] |
---|
307 | |
---|
308 | data = [ [4,4] ] |
---|
309 | interp = Interpolate(points, triangles) |
---|
310 | #print "PDSG - interp.get_A()", interp.get_A() |
---|
311 | answer = [ [ 0.06666667, 0.46666667, 0.46666667, 0., |
---|
312 | 0., 0. , 0., 0., 0., 0.]] |
---|
313 | |
---|
314 | A,_,_,_ = interp._build_interpolation_matrix_A(data) |
---|
315 | assert num.allclose(A.todense(), answer) |
---|
316 | |
---|
317 | #interp.set_point_coordinates([[-30, -30]]) #point outside of mesh |
---|
318 | #print "PDSG - interp.get_A()", interp.get_A() |
---|
319 | data = [[-30, -30]] |
---|
320 | answer = [ [ 0.0, 0.0, 0.0, 0., |
---|
321 | 0., 0. , 0., 0., 0., 0.]] |
---|
322 | |
---|
323 | A,_,_,_ = interp._build_interpolation_matrix_A(data) |
---|
324 | assert num.allclose(A.todense(), answer) |
---|
325 | |
---|
326 | |
---|
327 | #point outside of quad tree root cell |
---|
328 | #interp.set_point_coordinates([[-70, -70]]) |
---|
329 | #print "PDSG - interp.get_A()", interp.get_A() |
---|
330 | data = [[-70, -70]] |
---|
331 | answer = [ [ 0.0, 0.0, 0.0, 0., |
---|
332 | 0., 0. , 0., 0., 0., 0.]] |
---|
333 | |
---|
334 | A,_,_,_ = interp._build_interpolation_matrix_A(data) |
---|
335 | assert num.allclose(A.todense(), answer) |
---|
336 | |
---|
337 | |
---|
338 | def test_datapoints_at_vertices(self): |
---|
339 | #Test that data points coinciding with vertices yield a diagonal matrix |
---|
340 | |
---|
341 | |
---|
342 | a = [0.0, 0.0] |
---|
343 | b = [0.0, 2.0] |
---|
344 | c = [2.0,0.0] |
---|
345 | points = [a, b, c] |
---|
346 | vertices = [ [1,0,2] ] #bac |
---|
347 | |
---|
348 | data = points #Use data at vertices |
---|
349 | |
---|
350 | interp = Interpolate(points, vertices) |
---|
351 | answer = [[1., 0., 0.], |
---|
352 | [0., 1., 0.], |
---|
353 | [0., 0., 1.]] |
---|
354 | |
---|
355 | A,_,_,_ = interp._build_interpolation_matrix_A(data) |
---|
356 | assert num.allclose(A.todense(), answer) |
---|
357 | |
---|
358 | |
---|
359 | def test_datapoints_on_edge_midpoints(self): |
---|
360 | #Try datapoints midway on edges - |
---|
361 | #each point should affect two matrix entries equally |
---|
362 | |
---|
363 | |
---|
364 | a = [0.0, 0.0] |
---|
365 | b = [0.0, 2.0] |
---|
366 | c = [2.0,0.0] |
---|
367 | points = [a, b, c] |
---|
368 | vertices = [ [1,0,2] ] #bac |
---|
369 | |
---|
370 | data = [ [0., 1.], [1., 0.], [1., 1.] ] |
---|
371 | answer = [[0.5, 0.5, 0.0], #Affects vertex 1 and 0 |
---|
372 | [0.5, 0.0, 0.5], #Affects vertex 0 and 2 |
---|
373 | [0.0, 0.5, 0.5]] |
---|
374 | interp = Interpolate(points, vertices) |
---|
375 | |
---|
376 | A,_,_,_ = interp._build_interpolation_matrix_A(data) |
---|
377 | assert num.allclose(A.todense(), answer) |
---|
378 | |
---|
379 | def test_datapoints_on_edges(self): |
---|
380 | #Try datapoints on edges - |
---|
381 | #each point should affect two matrix entries in proportion |
---|
382 | |
---|
383 | |
---|
384 | a = [0.0, 0.0] |
---|
385 | b = [0.0, 2.0] |
---|
386 | c = [2.0,0.0] |
---|
387 | points = [a, b, c] |
---|
388 | vertices = [ [1,0,2] ] #bac |
---|
389 | |
---|
390 | data = [ [0., 1.5], [1.5, 0.], [1.5, 0.5] ] |
---|
391 | answer = [[0.25, 0.75, 0.0], #Affects vertex 1 and 0 |
---|
392 | [0.25, 0.0, 0.75], #Affects vertex 0 and 2 |
---|
393 | [0.0, 0.25, 0.75]] |
---|
394 | |
---|
395 | interp = Interpolate(points, vertices) |
---|
396 | |
---|
397 | A,_,_,_ = interp._build_interpolation_matrix_A(data) |
---|
398 | assert num.allclose(A.todense(), answer) |
---|
399 | |
---|
400 | |
---|
401 | def test_arbitrary_datapoints(self): |
---|
402 | #Try arbitrary datapoints |
---|
403 | |
---|
404 | |
---|
405 | a = [0.0, 0.0] |
---|
406 | b = [0.0, 2.0] |
---|
407 | c = [2.0,0.0] |
---|
408 | points = [a, b, c] |
---|
409 | vertices = [ [1,0,2] ] #bac |
---|
410 | |
---|
411 | data = [ [0.2, 1.5], [0.123, 1.768], [1.43, 0.44] ] |
---|
412 | |
---|
413 | interp = Interpolate(points, vertices) |
---|
414 | #print "interp.get_A()", interp.get_A() |
---|
415 | |
---|
416 | A,_,_,_ = interp._build_interpolation_matrix_A(data) |
---|
417 | results = A.todense() |
---|
418 | assert num.allclose(num.sum(results, axis=1), 1.0) |
---|
419 | |
---|
420 | |
---|
421 | def test_arbitrary_datapoints_return_centroids(self): |
---|
422 | #Try arbitrary datapoints, making sure they return |
---|
423 | #an interpolation matrix for the intersected triangle's |
---|
424 | #centroid. |
---|
425 | |
---|
426 | a = [1.0, 0.0] |
---|
427 | b = [0.0, 3.0] |
---|
428 | c = [4.0,0.0] |
---|
429 | points = [a, b, c] |
---|
430 | vertices = [ [1,0,2] ] |
---|
431 | |
---|
432 | data = [ [1.2, 1.5], [1.123, 1.768], [2.43, 0.44] ] |
---|
433 | |
---|
434 | interp = Interpolate(points, vertices) |
---|
435 | |
---|
436 | third = [1.0/3.0, 1.0/3.0, 1.0/3.0] |
---|
437 | answer = [third, third, third] |
---|
438 | |
---|
439 | A,_,_,_ = interp._build_interpolation_matrix_A(data, output_centroids=True) |
---|
440 | results = A.todense() |
---|
441 | assert num.allclose(results, answer) |
---|
442 | |
---|
443 | |
---|
444 | def test_arbitrary_datapoints_some_outside(self): |
---|
445 | #Try arbitrary datapoints one outside the triangle. |
---|
446 | #That one should be ignored |
---|
447 | |
---|
448 | |
---|
449 | a = [0.0, 0.0] |
---|
450 | b = [0.0, 2.0] |
---|
451 | c = [2.0,0.0] |
---|
452 | points = [a, b, c] |
---|
453 | vertices = [ [1,0,2] ] #bac |
---|
454 | |
---|
455 | data = [ [0.2, 1.5], [0.123, 1.768], [1.43, 0.44], [5.0, 7.0]] |
---|
456 | |
---|
457 | interp = Interpolate(points, vertices) |
---|
458 | |
---|
459 | A,_,_,_ = interp._build_interpolation_matrix_A(data) |
---|
460 | results = A.todense() |
---|
461 | assert num.allclose(num.sum(results, axis=1), [1,1,1,0]) |
---|
462 | |
---|
463 | |
---|
464 | |
---|
465 | # this causes a memory error in scipy.sparse |
---|
466 | def test_more_triangles(self): |
---|
467 | |
---|
468 | a = [-1.0, 0.0] |
---|
469 | b = [3.0, 4.0] |
---|
470 | c = [4.0,1.0] |
---|
471 | d = [-3.0, 2.0] #3 |
---|
472 | e = [-1.0,-2.0] |
---|
473 | f = [1.0, -2.0] #5 |
---|
474 | |
---|
475 | points = [a, b, c, d,e,f] |
---|
476 | triangles = [[0,1,3],[1,0,2],[0,4,5], [0,5,2]] #abd bac aef afc |
---|
477 | |
---|
478 | #Data points |
---|
479 | data = [ [-3., 2.0], [-2, 1], [0.0, 1], [0, 3], [2, 3], [-1.0/3,-4./3] ] |
---|
480 | interp = Interpolate(points, triangles) |
---|
481 | |
---|
482 | answer = [[0.0, 0.0, 0.0, 1.0, 0.0, 0.0], #Affects point d |
---|
483 | [0.5, 0.0, 0.0, 0.5, 0.0, 0.0], #Affects points a and d |
---|
484 | [0.75, 0.25, 0.0, 0.0, 0.0, 0.0], #Affects points a and b |
---|
485 | [0.0, 0.5, 0.0, 0.5, 0.0, 0.0], #Affects points a and d |
---|
486 | [0.25, 0.75, 0.0, 0.0, 0.0, 0.0], #Affects points a and b |
---|
487 | [1./3, 0.0, 0.0, 0.0, 1./3, 1./3]] #Affects points a, e and f |
---|
488 | |
---|
489 | |
---|
490 | A,_,_,_ = interp._build_interpolation_matrix_A(data) |
---|
491 | A = A.todense() |
---|
492 | for i in range(A.shape[0]): |
---|
493 | for j in range(A.shape[1]): |
---|
494 | if not num.allclose(A[i,j], answer[i][j]): |
---|
495 | print i,j,':',A[i,j], answer[i][j] |
---|
496 | |
---|
497 | |
---|
498 | #results = interp._build_interpolation_matrix_A(data).todense() |
---|
499 | |
---|
500 | assert num.allclose(A, answer) |
---|
501 | |
---|
502 | def test_geo_ref(self): |
---|
503 | v0 = [0.0, 0.0] |
---|
504 | v1 = [0.0, 5.0] |
---|
505 | v2 = [5.0, 0.0] |
---|
506 | |
---|
507 | vertices_absolute = [v0, v1, v2] |
---|
508 | triangles = [ [1,0,2] ] #bac |
---|
509 | |
---|
510 | geo = Geo_reference(57,100, 500) |
---|
511 | |
---|
512 | vertices = geo.change_points_geo_ref(vertices_absolute) |
---|
513 | #print "vertices",vertices |
---|
514 | |
---|
515 | d0 = [1.0, 1.0] |
---|
516 | d1 = [1.0, 2.0] |
---|
517 | d2 = [3.0, 1.0] |
---|
518 | point_coords = [ d0, d1, d2] |
---|
519 | |
---|
520 | interp = Interpolate(vertices, triangles, mesh_origin=geo) |
---|
521 | f = linear_function(vertices_absolute) |
---|
522 | z = interp.interpolate(f, point_coords) |
---|
523 | answer = linear_function(point_coords) |
---|
524 | |
---|
525 | #print "z",z |
---|
526 | #print "answer",answer |
---|
527 | assert num.allclose(z, answer) |
---|
528 | |
---|
529 | |
---|
530 | z = interp.interpolate(f, point_coords, start_blocking_len = 2) |
---|
531 | answer = linear_function(point_coords) |
---|
532 | |
---|
533 | #print "z",z |
---|
534 | #print "answer",answer |
---|
535 | assert num.allclose(z, answer) |
---|
536 | |
---|
537 | |
---|
538 | def test_sigma_epsilon(self): |
---|
539 | """ |
---|
540 | def test_sigma_epsilon(self): |
---|
541 | Testing ticket 168. I could not reduce the bug to this small |
---|
542 | test though. |
---|
543 | |
---|
544 | """ |
---|
545 | v0 = [22031.25, 59687.5] |
---|
546 | v1 = [22500., 60000.] |
---|
547 | v2 = [22350.31640625, 59716.71484375] |
---|
548 | |
---|
549 | vertices = [v0, v1, v2] |
---|
550 | triangles = [ [1,0,2] ] #bac |
---|
551 | |
---|
552 | |
---|
553 | point_coords = [[22050., 59700.]] |
---|
554 | |
---|
555 | interp = Interpolate(vertices, triangles) |
---|
556 | f = linear_function(vertices) |
---|
557 | z = interp.interpolate(f, point_coords) |
---|
558 | answer = linear_function(point_coords) |
---|
559 | |
---|
560 | #print "z",z |
---|
561 | #print "answer",answer |
---|
562 | assert num.allclose(z, answer) |
---|
563 | |
---|
564 | |
---|
565 | z = interp.interpolate(f, point_coords, start_blocking_len = 2) |
---|
566 | answer = linear_function(point_coords) |
---|
567 | |
---|
568 | #print "z",z |
---|
569 | #print "answer",answer |
---|
570 | assert num.allclose(z, answer) |
---|
571 | |
---|
572 | |
---|
573 | def test_Geospatial_verts(self): |
---|
574 | v0 = [0.0, 0.0] |
---|
575 | v1 = [0.0, 5.0] |
---|
576 | v2 = [5.0, 0.0] |
---|
577 | |
---|
578 | vertices_absolute = [v0, v1, v2] |
---|
579 | triangles = [ [1,0,2] ] #bac |
---|
580 | |
---|
581 | geo = Geo_reference(57,100, 500) |
---|
582 | vertices = geo.change_points_geo_ref(vertices_absolute) |
---|
583 | geopoints = Geospatial_data(vertices,geo_reference = geo) |
---|
584 | #print "vertices",vertices |
---|
585 | |
---|
586 | d0 = [1.0, 1.0] |
---|
587 | d1 = [1.0, 2.0] |
---|
588 | d2 = [3.0, 1.0] |
---|
589 | point_coords = [ d0, d1, d2] |
---|
590 | |
---|
591 | interp = Interpolate(geopoints, triangles) |
---|
592 | f = linear_function(vertices_absolute) |
---|
593 | z = interp.interpolate(f, point_coords) |
---|
594 | answer = linear_function(point_coords) |
---|
595 | |
---|
596 | #print "z",z |
---|
597 | #print "answer",answer |
---|
598 | assert num.allclose(z, answer) |
---|
599 | |
---|
600 | z = interp.interpolate(f, point_coords, start_blocking_len = 2) |
---|
601 | answer = linear_function(point_coords) |
---|
602 | |
---|
603 | #print "z",z |
---|
604 | #print "answer",answer |
---|
605 | assert num.allclose(z, answer) |
---|
606 | |
---|
607 | def test_interpolate_attributes_to_points(self): |
---|
608 | v0 = [0.0, 0.0] |
---|
609 | v1 = [0.0, 5.0] |
---|
610 | v2 = [5.0, 0.0] |
---|
611 | |
---|
612 | vertices = [v0, v1, v2] |
---|
613 | triangles = [ [1,0,2] ] #bac |
---|
614 | |
---|
615 | d0 = [1.0, 1.0] |
---|
616 | d1 = [1.0, 2.0] |
---|
617 | d2 = [3.0, 1.0] |
---|
618 | point_coords = [ d0, d1, d2] |
---|
619 | |
---|
620 | interp = Interpolate(vertices, triangles) |
---|
621 | f = linear_function(vertices) |
---|
622 | z = interp.interpolate(f, point_coords) |
---|
623 | answer = linear_function(point_coords) |
---|
624 | |
---|
625 | #print "z",z |
---|
626 | #print "answer",answer |
---|
627 | assert num.allclose(z, answer) |
---|
628 | |
---|
629 | |
---|
630 | z = interp.interpolate(f, point_coords, start_blocking_len = 2) |
---|
631 | answer = linear_function(point_coords) |
---|
632 | |
---|
633 | #print "z",z |
---|
634 | #print "answer",answer |
---|
635 | assert num.allclose(z, answer) |
---|
636 | |
---|
637 | def test_interpolate_attributes_to_pointsII(self): |
---|
638 | a = [-1.0, 0.0] |
---|
639 | b = [3.0, 4.0] |
---|
640 | c = [4.0, 1.0] |
---|
641 | d = [-3.0, 2.0] #3 |
---|
642 | e = [-1.0, -2.0] |
---|
643 | f = [1.0, -2.0] #5 |
---|
644 | |
---|
645 | vertices = [a, b, c, d,e,f] |
---|
646 | triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc |
---|
647 | |
---|
648 | |
---|
649 | point_coords = [[-2.0, 2.0], |
---|
650 | [-1.0, 1.0], |
---|
651 | [0.0, 2.0], |
---|
652 | [1.0, 1.0], |
---|
653 | [2.0, 1.0], |
---|
654 | [0.0, 0.0], |
---|
655 | [1.0, 0.0], |
---|
656 | [0.0, -1.0], |
---|
657 | [-0.2, -0.5], |
---|
658 | [-0.9, -1.5], |
---|
659 | [0.5, -1.9], |
---|
660 | [3.0, 1.0]] |
---|
661 | |
---|
662 | interp = Interpolate(vertices, triangles) |
---|
663 | f = linear_function(vertices) |
---|
664 | z = interp.interpolate(f, point_coords) |
---|
665 | answer = linear_function(point_coords) |
---|
666 | #print "z",z |
---|
667 | #print "answer",answer |
---|
668 | assert num.allclose(z, answer) |
---|
669 | |
---|
670 | z = interp.interpolate(f, point_coords, start_blocking_len = 2) |
---|
671 | answer = linear_function(point_coords) |
---|
672 | |
---|
673 | #print "z",z |
---|
674 | #print "answer",answer |
---|
675 | assert num.allclose(z, answer) |
---|
676 | |
---|
677 | def test_interpolate_attributes_to_pointsIII(self): |
---|
678 | #Test linear interpolation of known values at vertices to |
---|
679 | #new points inside a triangle |
---|
680 | |
---|
681 | a = [0.0, 0.0] |
---|
682 | b = [0.0, 5.0] |
---|
683 | c = [5.0, 0.0] |
---|
684 | d = [5.0, 5.0] |
---|
685 | |
---|
686 | vertices = [a, b, c, d] |
---|
687 | triangles = [ [1,0,2], [2,3,1] ] #bac, cdb |
---|
688 | |
---|
689 | #Points within triangle 1 |
---|
690 | d0 = [1.0, 1.0] |
---|
691 | d1 = [1.0, 2.0] |
---|
692 | d2 = [3.0, 1.0] |
---|
693 | |
---|
694 | #Point within triangle 2 |
---|
695 | d3 = [4.0, 3.0] |
---|
696 | |
---|
697 | #Points on common edge |
---|
698 | d4 = [2.5, 2.5] |
---|
699 | d5 = [4.0, 1.0] |
---|
700 | |
---|
701 | #Point on common vertex |
---|
702 | d6 = [0., 5.] |
---|
703 | |
---|
704 | point_coords = [d0, d1, d2, d3, d4, d5, d6] |
---|
705 | |
---|
706 | interp = Interpolate(vertices, triangles) |
---|
707 | |
---|
708 | #Known values at vertices |
---|
709 | #Functions are x+y, x+2y, 2x+y, x-y-5 |
---|
710 | f = [ [0., 0., 0., -5.], # (0,0) |
---|
711 | [5., 10., 5., -10.], # (0,5) |
---|
712 | [5., 5., 10.0, 0.], # (5,0) |
---|
713 | [10., 15., 15., -5.]] # (5,5) |
---|
714 | |
---|
715 | z = interp.interpolate(f, point_coords) |
---|
716 | answer = [ [2., 3., 3., -5.], # (1,1) |
---|
717 | [3., 5., 4., -6.], # (1,2) |
---|
718 | [4., 5., 7., -3.], # (3,1) |
---|
719 | [7., 10., 11., -4.], # (4,3) |
---|
720 | [5., 7.5, 7.5, -5.], # (2.5, 2.5) |
---|
721 | [5., 6., 9., -2.], # (4,1) |
---|
722 | [5., 10., 5., -10.]] # (0,5) |
---|
723 | |
---|
724 | #print "***********" |
---|
725 | #print "z",z |
---|
726 | #print "answer",answer |
---|
727 | #print "***********" |
---|
728 | |
---|
729 | assert num.allclose(z, answer) |
---|
730 | |
---|
731 | |
---|
732 | z = interp.interpolate(f, point_coords, start_blocking_len = 2) |
---|
733 | |
---|
734 | #print "z",z |
---|
735 | #print "answer",answer |
---|
736 | assert num.allclose(z, answer) |
---|
737 | |
---|
738 | def test_interpolate_point_outside_of_mesh(self): |
---|
739 | #Test linear interpolation of known values at vertices to |
---|
740 | #new points inside a triangle |
---|
741 | |
---|
742 | a = [0.0, 0.0] |
---|
743 | b = [0.0, 5.0] |
---|
744 | c = [5.0, 0.0] |
---|
745 | d = [5.0, 5.0] |
---|
746 | |
---|
747 | vertices = [a, b, c, d] |
---|
748 | triangles = [ [1,0,2], [2,3,1] ] #bac, cdb |
---|
749 | |
---|
750 | #Far away point |
---|
751 | d7 = [-1., -1.] |
---|
752 | |
---|
753 | point_coords = [ d7] |
---|
754 | interp = Interpolate(vertices, triangles) |
---|
755 | |
---|
756 | #Known values at vertices |
---|
757 | #Functions are x+y, x+2y, 2x+y, x-y-5 |
---|
758 | f = [ [0., 0., 0., -5.], # (0,0) |
---|
759 | [5., 10., 5., -10.], # (0,5) |
---|
760 | [5., 5., 10.0, 0.], # (5,0) |
---|
761 | [10., 15., 15., -5.]] # (5,5) |
---|
762 | |
---|
763 | z = interp.interpolate(f, point_coords) #, verbose=True) |
---|
764 | answer = num.array([ [NAN, NAN, NAN, NAN]]) # (-1,-1) |
---|
765 | |
---|
766 | #print "***********" |
---|
767 | #print "z",z |
---|
768 | #print "answer",answer |
---|
769 | #print "***********" |
---|
770 | |
---|
771 | #Should an error message be returned if points are outside |
---|
772 | # of the mesh? |
---|
773 | # A warning message is printed, if verbose is on. |
---|
774 | |
---|
775 | for i in range(4): |
---|
776 | self.failUnless( z[0,i] == answer[0,i], 'Fail!') |
---|
777 | |
---|
778 | z = interp.interpolate(f, point_coords, start_blocking_len = 2) |
---|
779 | |
---|
780 | #print "z",z |
---|
781 | #print "answer",answer |
---|
782 | |
---|
783 | for i in range(4): |
---|
784 | self.failUnless( z[0,i] == answer[0,i], 'Fail!') |
---|
785 | |
---|
786 | |
---|
787 | def test_interpolate_attributes_to_pointsIV(self): |
---|
788 | a = [-1.0, 0.0] |
---|
789 | b = [3.0, 4.0] |
---|
790 | c = [4.0, 1.0] |
---|
791 | d = [-3.0, 2.0] #3 |
---|
792 | e = [-1.0, -2.0] |
---|
793 | f = [1.0, -2.0] #5 |
---|
794 | |
---|
795 | vertices = [a, b, c, d,e,f] |
---|
796 | triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc |
---|
797 | |
---|
798 | |
---|
799 | point_coords = [[-2.0, 2.0], |
---|
800 | [-1.0, 1.0], |
---|
801 | [0.0, 2.0], |
---|
802 | [1.0, 1.0], |
---|
803 | [2.0, 1.0], |
---|
804 | [0.0, 0.0], |
---|
805 | [1.0, 0.0], |
---|
806 | [0.0, -1.0], |
---|
807 | [-0.2, -0.5], |
---|
808 | [-0.9, -1.5], |
---|
809 | [0.5, -1.9], |
---|
810 | [3.0, 1.0]] |
---|
811 | |
---|
812 | interp = Interpolate(vertices, triangles) |
---|
813 | f = num.array([linear_function(vertices),2*linear_function(vertices)]) |
---|
814 | f = num.transpose(f) |
---|
815 | #print "f",f |
---|
816 | z = interp.interpolate(f, point_coords) |
---|
817 | answer = [linear_function(point_coords), |
---|
818 | 2*linear_function(point_coords) ] |
---|
819 | answer = num.transpose(answer) |
---|
820 | #print "z",z |
---|
821 | #print "answer",answer |
---|
822 | assert num.allclose(z, answer) |
---|
823 | |
---|
824 | z = interp.interpolate(f, point_coords, start_blocking_len = 2) |
---|
825 | |
---|
826 | #print "z",z |
---|
827 | #print "answer",answer |
---|
828 | assert num.allclose(z, answer) |
---|
829 | |
---|
830 | def test_interpolate_blocking(self): |
---|
831 | a = [-1.0, 0.0] |
---|
832 | b = [3.0, 4.0] |
---|
833 | c = [4.0, 1.0] |
---|
834 | d = [-3.0, 2.0] #3 |
---|
835 | e = [-1.0, -2.0] |
---|
836 | f = [1.0, -2.0] #5 |
---|
837 | |
---|
838 | vertices = [a, b, c, d,e,f] |
---|
839 | triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc |
---|
840 | |
---|
841 | |
---|
842 | point_coords = [[-2.0, 2.0], |
---|
843 | [-1.0, 1.0], |
---|
844 | [0.0, 2.0], |
---|
845 | [1.0, 1.0], |
---|
846 | [2.0, 1.0], |
---|
847 | [0.0, 0.0], |
---|
848 | [1.0, 0.0], |
---|
849 | [0.0, -1.0], |
---|
850 | [-0.2, -0.5], |
---|
851 | [-0.9, -1.5], |
---|
852 | [0.5, -1.9], |
---|
853 | [3.0, 1.0]] |
---|
854 | |
---|
855 | interp = Interpolate(vertices, triangles) |
---|
856 | f = num.array([linear_function(vertices),2*linear_function(vertices)]) |
---|
857 | f = num.transpose(f) |
---|
858 | #print "f",f |
---|
859 | for blocking_max in range(len(point_coords)+2): |
---|
860 | #if True: |
---|
861 | # blocking_max = 5 |
---|
862 | z = interp.interpolate(f, point_coords, |
---|
863 | start_blocking_len=blocking_max) |
---|
864 | answer = [linear_function(point_coords), |
---|
865 | 2*linear_function(point_coords) ] |
---|
866 | answer = num.transpose(answer) |
---|
867 | #print "z",z |
---|
868 | #print "answer",answer |
---|
869 | assert num.allclose(z, answer) |
---|
870 | |
---|
871 | f = num.array([linear_function(vertices),2*linear_function(vertices), |
---|
872 | 2*linear_function(vertices) - 100]) |
---|
873 | f = num.transpose(f) |
---|
874 | #print "f",f |
---|
875 | for blocking_max in range(len(point_coords)+2): |
---|
876 | #if True: |
---|
877 | # blocking_max = 5 |
---|
878 | z = interp.interpolate(f, point_coords, |
---|
879 | start_blocking_len=blocking_max) |
---|
880 | answer = num.array([linear_function(point_coords), |
---|
881 | 2*linear_function(point_coords) , |
---|
882 | 2*linear_function(point_coords)-100]) |
---|
883 | z = num.transpose(z) |
---|
884 | #print "z",z |
---|
885 | #print "answer",answer |
---|
886 | assert num.allclose(z, answer) |
---|
887 | |
---|
888 | def test_interpolate_geo_spatial(self): |
---|
889 | a = [-1.0, 0.0] |
---|
890 | b = [3.0, 4.0] |
---|
891 | c = [4.0, 1.0] |
---|
892 | d = [-3.0, 2.0] #3 |
---|
893 | e = [-1.0, -2.0] |
---|
894 | f = [1.0, -2.0] #5 |
---|
895 | |
---|
896 | vertices = [a, b, c, d,e,f] |
---|
897 | triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc |
---|
898 | |
---|
899 | |
---|
900 | point_coords_absolute = [[-2.0, 2.0], |
---|
901 | [-1.0, 1.0], |
---|
902 | [0.0, 2.0], |
---|
903 | [1.0, 1.0], |
---|
904 | [2.0, 1.0], |
---|
905 | [0.0, 0.0], |
---|
906 | [1.0, 0.0], |
---|
907 | [0.0, -1.0], |
---|
908 | [-0.2, -0.5], |
---|
909 | [-0.9, -1.5], |
---|
910 | [0.5, -1.9], |
---|
911 | [3.0, 1.0]] |
---|
912 | |
---|
913 | geo = Geo_reference(57,100, 500) |
---|
914 | point_coords = geo.change_points_geo_ref(point_coords_absolute) |
---|
915 | point_coords = Geospatial_data(point_coords,geo_reference = geo) |
---|
916 | |
---|
917 | interp = Interpolate(vertices, triangles) |
---|
918 | f = num.array([linear_function(vertices),2*linear_function(vertices)]) |
---|
919 | f = num.transpose(f) |
---|
920 | #print "f",f |
---|
921 | for blocking_max in range(14): |
---|
922 | #if True: |
---|
923 | # blocking_max = 5 |
---|
924 | z = interp.interpolate(f, point_coords, |
---|
925 | start_blocking_len=blocking_max) |
---|
926 | answer = [linear_function(point_coords.get_data_points( \ |
---|
927 | absolute = True)), |
---|
928 | 2*linear_function(point_coords.get_data_points( \ |
---|
929 | absolute = True)) ] |
---|
930 | answer = num.transpose(answer) |
---|
931 | #print "z",z |
---|
932 | #print "answer",answer |
---|
933 | assert num.allclose(z, answer) |
---|
934 | |
---|
935 | f = num.array([linear_function(vertices),2*linear_function(vertices), |
---|
936 | 2*linear_function(vertices) - 100]) |
---|
937 | f = num.transpose(f) |
---|
938 | #print "f",f |
---|
939 | for blocking_max in range(14): |
---|
940 | #if True: |
---|
941 | # blocking_max = 5 |
---|
942 | z = interp.interpolate(f, point_coords, |
---|
943 | start_blocking_len=blocking_max) |
---|
944 | answer = num.array([linear_function(point_coords.get_data_points( \ |
---|
945 | absolute = True)), |
---|
946 | 2*linear_function(point_coords.get_data_points( \ |
---|
947 | absolute = True)) , |
---|
948 | 2*linear_function(point_coords.get_data_points( \ |
---|
949 | absolute = True))-100]) |
---|
950 | z = num.transpose(z) |
---|
951 | #print "z",z |
---|
952 | #print "answer",answer |
---|
953 | assert num.allclose(z, answer) |
---|
954 | |
---|
955 | z = interp.interpolate(f, point_coords, start_blocking_len = 2) |
---|
956 | |
---|
957 | #print "z",z |
---|
958 | #print "answer",answer |
---|
959 | assert num.allclose(z, answer) |
---|
960 | |
---|
961 | def test_interpolate_geo_spatial(self): |
---|
962 | a = [-1.0, 0.0] |
---|
963 | b = [3.0, 4.0] |
---|
964 | c = [4.0, 1.0] |
---|
965 | d = [-3.0, 2.0] #3 |
---|
966 | e = [-1.0, -2.0] |
---|
967 | f = [1.0, -2.0] #5 |
---|
968 | |
---|
969 | vertices = [a, b, c, d,e,f] |
---|
970 | triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc |
---|
971 | |
---|
972 | point_coords_absolute = [[-2.0, 2.0], |
---|
973 | [-1.0, 1.0], |
---|
974 | [ 0.0, 2.0], |
---|
975 | [ 1.0, 1.0], |
---|
976 | [ 2.0, 1.0], |
---|
977 | [ 0.0, 0.0], |
---|
978 | [ 1.0, 0.0], |
---|
979 | [ 0.0, -1.0], |
---|
980 | [-0.2, -0.5], |
---|
981 | [-0.9, -1.5], |
---|
982 | [ 0.5, -1.9], |
---|
983 | [ 3.0, 1.0]] |
---|
984 | |
---|
985 | geo = Geo_reference(57, 100, 500) |
---|
986 | point_coords = geo.change_points_geo_ref(point_coords_absolute) |
---|
987 | point_coords = Geospatial_data(point_coords, geo_reference=geo) |
---|
988 | |
---|
989 | interp = Interpolate(vertices, triangles) |
---|
990 | f = num.array([linear_function(vertices), 2*linear_function(vertices)]) |
---|
991 | f = num.transpose(f) |
---|
992 | z = interp.interpolate_block(f, point_coords) |
---|
993 | answer = [linear_function(point_coords.get_data_points(absolute=True)), |
---|
994 | 2*linear_function(point_coords.get_data_points(absolute=True)) |
---|
995 | ] |
---|
996 | answer = num.transpose(answer) |
---|
997 | msg = ('Expected z\n%s\nto be close to answer\n%s' |
---|
998 | % (str(z), str(answer))) |
---|
999 | assert num.allclose(z, answer), msg |
---|
1000 | |
---|
1001 | z = interp.interpolate(f, point_coords, start_blocking_len = 2) |
---|
1002 | |
---|
1003 | msg = ('Expected z\n%s\nto be close to answer\n%s' |
---|
1004 | % (str(z), str(answer))) |
---|
1005 | assert num.allclose(z, answer) |
---|
1006 | |
---|
1007 | |
---|
1008 | def test_interpolate_reuse_if_None(self): |
---|
1009 | a = [-1.0, 0.0] |
---|
1010 | b = [3.0, 4.0] |
---|
1011 | c = [4.0, 1.0] |
---|
1012 | d = [-3.0, 2.0] #3 |
---|
1013 | e = [-1.0, -2.0] |
---|
1014 | f = [1.0, -2.0] #5 |
---|
1015 | |
---|
1016 | vertices = [a, b, c, d,e,f] |
---|
1017 | triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc |
---|
1018 | |
---|
1019 | |
---|
1020 | point_coords = [[-2.0, 2.0], |
---|
1021 | [-1.0, 1.0], |
---|
1022 | [ 0.0, 2.0], |
---|
1023 | [ 1.0, 1.0], |
---|
1024 | [ 2.0, 1.0], |
---|
1025 | [ 0.0, 0.0], |
---|
1026 | [ 1.0, 0.0], |
---|
1027 | [ 0.0, -1.0], |
---|
1028 | [-0.2, -0.5], |
---|
1029 | [-0.9, -1.5], |
---|
1030 | [ 0.5, -1.9], |
---|
1031 | [ 3.0, 1.0]] |
---|
1032 | |
---|
1033 | interp = Interpolate(vertices, triangles) |
---|
1034 | f = num.array([linear_function(vertices),2*linear_function(vertices)]) |
---|
1035 | f = num.transpose(f) |
---|
1036 | z = interp.interpolate(f, point_coords, |
---|
1037 | start_blocking_len=20) |
---|
1038 | answer = [linear_function(point_coords), |
---|
1039 | 2*linear_function(point_coords) ] |
---|
1040 | answer = num.transpose(answer) |
---|
1041 | #print "z",z |
---|
1042 | #print "answer",answer |
---|
1043 | assert num.allclose(z, answer) |
---|
1044 | assert num.allclose(interp._A_can_be_reused, True) |
---|
1045 | |
---|
1046 | z = interp.interpolate(f) |
---|
1047 | assert num.allclose(z, answer) |
---|
1048 | |
---|
1049 | # This causes blocking to occur. |
---|
1050 | z = interp.interpolate(f, start_blocking_len=10) |
---|
1051 | assert num.allclose(z, answer) |
---|
1052 | assert num.allclose(interp._A_can_be_reused, False) |
---|
1053 | |
---|
1054 | #A is recalculated |
---|
1055 | z = interp.interpolate(f) |
---|
1056 | assert num.allclose(z, answer) |
---|
1057 | assert num.allclose(interp._A_can_be_reused, True) |
---|
1058 | |
---|
1059 | interp = Interpolate(vertices, triangles) |
---|
1060 | #Must raise an exception, no points specified |
---|
1061 | try: |
---|
1062 | z = interp.interpolate(f) |
---|
1063 | except: |
---|
1064 | pass |
---|
1065 | |
---|
1066 | def xxtest_interpolate_reuse_if_same(self): |
---|
1067 | |
---|
1068 | # This on tests that repeated identical interpolation |
---|
1069 | # points makes use of precomputed matrix (Ole) |
---|
1070 | # This is not really a test and is disabled for now |
---|
1071 | |
---|
1072 | a = [-1.0, 0.0] |
---|
1073 | b = [3.0, 4.0] |
---|
1074 | c = [4.0, 1.0] |
---|
1075 | d = [-3.0, 2.0] #3 |
---|
1076 | e = [-1.0, -2.0] |
---|
1077 | f = [1.0, -2.0] #5 |
---|
1078 | |
---|
1079 | vertices = [a, b, c, d,e,f] |
---|
1080 | triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc |
---|
1081 | |
---|
1082 | |
---|
1083 | point_coords = [[-2.0, 2.0], |
---|
1084 | [-1.0, 1.0], |
---|
1085 | [ 0.0, 2.0], |
---|
1086 | [ 1.0, 1.0], |
---|
1087 | [ 2.0, 1.0], |
---|
1088 | [ 0.0, 0.0], |
---|
1089 | [ 1.0, 0.0], |
---|
1090 | [ 0.0, -1.0], |
---|
1091 | [-0.2, -0.5], |
---|
1092 | [-0.9, -1.5], |
---|
1093 | [ 0.5, -1.9], |
---|
1094 | [ 3.0, 1.0]] |
---|
1095 | |
---|
1096 | interp = Interpolate(vertices, triangles) |
---|
1097 | f = num.array([linear_function(vertices), 2*linear_function(vertices)]) |
---|
1098 | f = num.transpose(f) |
---|
1099 | z = interp.interpolate(f, point_coords) |
---|
1100 | answer = [linear_function(point_coords), |
---|
1101 | 2*linear_function(point_coords) ] |
---|
1102 | answer = num.transpose(answer) |
---|
1103 | |
---|
1104 | assert num.allclose(z, answer) |
---|
1105 | assert num.allclose(interp._A_can_be_reused, True) |
---|
1106 | |
---|
1107 | |
---|
1108 | z = interp.interpolate(f) # None |
---|
1109 | assert num.allclose(z, answer) |
---|
1110 | z = interp.interpolate(f, point_coords) # Repeated (not really a test) |
---|
1111 | assert num.allclose(z, answer) |
---|
1112 | |
---|
1113 | |
---|
1114 | |
---|
1115 | def test_interpolation_interface_time_only(self): |
---|
1116 | |
---|
1117 | # Test spatio-temporal interpolation |
---|
1118 | # Test that spatio temporal function performs the correct |
---|
1119 | # interpolations in both time and space |
---|
1120 | |
---|
1121 | |
---|
1122 | |
---|
1123 | #Three timesteps |
---|
1124 | time = [1.0, 5.0, 6.0] |
---|
1125 | |
---|
1126 | |
---|
1127 | #One quantity |
---|
1128 | Q = num.zeros( (3,6), num.float ) |
---|
1129 | |
---|
1130 | #Linear in time and space |
---|
1131 | a = [0.0, 0.0] |
---|
1132 | b = [0.0, 2.0] |
---|
1133 | c = [2.0, 0.0] |
---|
1134 | d = [0.0, 4.0] |
---|
1135 | e = [2.0, 2.0] |
---|
1136 | f = [4.0, 0.0] |
---|
1137 | |
---|
1138 | points = [a, b, c, d, e, f] |
---|
1139 | |
---|
1140 | for i, t in enumerate(time): |
---|
1141 | Q[i, :] = t*linear_function(points) |
---|
1142 | |
---|
1143 | |
---|
1144 | #Check basic interpolation of one quantity using averaging |
---|
1145 | #(no interpolation points or spatial info) |
---|
1146 | I = Interpolation_function(time, [mean(Q[0,:]), |
---|
1147 | mean(Q[1,:]), |
---|
1148 | mean(Q[2,:])]) |
---|
1149 | |
---|
1150 | |
---|
1151 | |
---|
1152 | #Check temporal interpolation |
---|
1153 | for i in [0,1,2]: |
---|
1154 | assert num.allclose(I(time[i]), mean(Q[i,:])) |
---|
1155 | |
---|
1156 | #Midway |
---|
1157 | assert num.allclose(I( (time[0] + time[1])/2 ), |
---|
1158 | (I(time[0]) + I(time[1]))/2 ) |
---|
1159 | |
---|
1160 | assert num.allclose(I( (time[1] + time[2])/2 ), |
---|
1161 | (I(time[1]) + I(time[2]))/2 ) |
---|
1162 | |
---|
1163 | assert num.allclose(I( (time[0] + time[2])/2 ), |
---|
1164 | (I(time[0]) + I(time[2]))/2 ) |
---|
1165 | |
---|
1166 | #1/3 |
---|
1167 | assert num.allclose(I( (time[0] + time[2])/3 ), |
---|
1168 | (I(time[0]) + I(time[2]))/3 ) |
---|
1169 | |
---|
1170 | |
---|
1171 | #Out of bounds checks |
---|
1172 | try: |
---|
1173 | I(time[0]-1) |
---|
1174 | except: |
---|
1175 | pass |
---|
1176 | else: |
---|
1177 | raise 'Should raise exception' |
---|
1178 | |
---|
1179 | try: |
---|
1180 | I(time[-1]+1) |
---|
1181 | except: |
---|
1182 | pass |
---|
1183 | else: |
---|
1184 | raise 'Should raise exception' |
---|
1185 | |
---|
1186 | |
---|
1187 | |
---|
1188 | |
---|
1189 | def test_interpolation_interface_spatial_only(self): |
---|
1190 | # Test spatio-temporal interpolation with constant time |
---|
1191 | |
---|
1192 | #Three timesteps |
---|
1193 | time = [1.0, 5.0, 6.0] |
---|
1194 | |
---|
1195 | #Setup mesh used to represent fitted function |
---|
1196 | a = [0.0, 0.0] |
---|
1197 | b = [0.0, 2.0] |
---|
1198 | c = [2.0, 0.0] |
---|
1199 | d = [0.0, 4.0] |
---|
1200 | e = [2.0, 2.0] |
---|
1201 | f = [4.0, 0.0] |
---|
1202 | |
---|
1203 | points = [a, b, c, d, e, f] |
---|
1204 | #bac, bce, ecf, dbe |
---|
1205 | triangles = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]] |
---|
1206 | |
---|
1207 | |
---|
1208 | #New datapoints where interpolated values are sought |
---|
1209 | interpolation_points = [[ 0.0, 0.0], |
---|
1210 | [ 0.5, 0.5], |
---|
1211 | [ 0.7, 0.7], |
---|
1212 | [ 1.0, 0.5], |
---|
1213 | [ 2.0, 0.4], |
---|
1214 | [ 2.8, 1.2]] |
---|
1215 | |
---|
1216 | |
---|
1217 | #One quantity linear in space |
---|
1218 | Q = linear_function(points) |
---|
1219 | |
---|
1220 | |
---|
1221 | #Check interpolation of one quantity using interpolaton points |
---|
1222 | I = Interpolation_function(time, Q, |
---|
1223 | vertex_coordinates = points, |
---|
1224 | triangles = triangles, |
---|
1225 | interpolation_points = interpolation_points, |
---|
1226 | verbose = False) |
---|
1227 | |
---|
1228 | |
---|
1229 | answer = linear_function(interpolation_points) |
---|
1230 | |
---|
1231 | t = time[0] |
---|
1232 | for j in range(50): #t in [1, 6] |
---|
1233 | for id in range(len(interpolation_points)): |
---|
1234 | assert num.allclose(I(t, id), answer[id]) |
---|
1235 | t += 0.1 |
---|
1236 | |
---|
1237 | try: |
---|
1238 | I(1) |
---|
1239 | except: |
---|
1240 | pass |
---|
1241 | else: |
---|
1242 | raise 'Should raise exception' |
---|
1243 | |
---|
1244 | |
---|
1245 | def test_interpolation_interface(self): |
---|
1246 | # Test spatio-temporal interpolation |
---|
1247 | # Test that spatio temporal function performs the correct |
---|
1248 | # interpolations in both time and space |
---|
1249 | |
---|
1250 | #Three timesteps |
---|
1251 | time = [1.0, 5.0, 6.0] |
---|
1252 | |
---|
1253 | #Setup mesh used to represent fitted function |
---|
1254 | a = [0.0, 0.0] |
---|
1255 | b = [0.0, 2.0] |
---|
1256 | c = [2.0, 0.0] |
---|
1257 | d = [0.0, 4.0] |
---|
1258 | e = [2.0, 2.0] |
---|
1259 | f = [4.0, 0.0] |
---|
1260 | |
---|
1261 | points = [a, b, c, d, e, f] |
---|
1262 | #bac, bce, ecf, dbe |
---|
1263 | triangles = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]] |
---|
1264 | |
---|
1265 | |
---|
1266 | #New datapoints where interpolated values are sought |
---|
1267 | interpolation_points = [[ 0.0, 0.0], |
---|
1268 | [ 0.5, 0.5], |
---|
1269 | [ 0.7, 0.7], |
---|
1270 | [ 1.0, 0.5], |
---|
1271 | [ 2.0, 0.4], |
---|
1272 | [ 2.8, 1.2]] |
---|
1273 | |
---|
1274 | #One quantity |
---|
1275 | Q = num.zeros( (3,6), num.float ) |
---|
1276 | |
---|
1277 | #Linear in time and space |
---|
1278 | for i, t in enumerate(time): |
---|
1279 | Q[i, :] = t*linear_function(points) |
---|
1280 | |
---|
1281 | #Check interpolation of one quantity using interpolaton points) |
---|
1282 | I = Interpolation_function(time, Q, |
---|
1283 | vertex_coordinates = points, |
---|
1284 | triangles = triangles, |
---|
1285 | interpolation_points = interpolation_points, |
---|
1286 | verbose = False) |
---|
1287 | |
---|
1288 | answer = linear_function(interpolation_points) |
---|
1289 | |
---|
1290 | t = time[0] |
---|
1291 | for j in range(50): #t in [1, 6] |
---|
1292 | for id in range(len(interpolation_points)): |
---|
1293 | assert num.allclose(I(t, id), t*answer[id]) |
---|
1294 | t += 0.1 |
---|
1295 | |
---|
1296 | try: |
---|
1297 | I(1) |
---|
1298 | except: |
---|
1299 | pass |
---|
1300 | else: |
---|
1301 | raise 'Should raise exception' |
---|
1302 | |
---|
1303 | |
---|
1304 | |
---|
1305 | def test_interpolation_interface_with_time_thinning(self): |
---|
1306 | # Test spatio-temporal interpolation |
---|
1307 | # Test that spatio temporal function performs the correct |
---|
1308 | # interpolations in both time and space |
---|
1309 | |
---|
1310 | # Eight timesteps |
---|
1311 | time = [1.0, 2.0, 4.0, 5.0, 7.0, 8.0, 9.0, 10.0] |
---|
1312 | |
---|
1313 | # Setup mesh used to represent fitted function |
---|
1314 | a = [0.0, 0.0] |
---|
1315 | b = [0.0, 2.0] |
---|
1316 | c = [2.0, 0.0] |
---|
1317 | d = [0.0, 4.0] |
---|
1318 | e = [2.0, 2.0] |
---|
1319 | f = [4.0, 0.0] |
---|
1320 | |
---|
1321 | points = [a, b, c, d, e, f] |
---|
1322 | # bac, bce, ecf, dbe |
---|
1323 | triangles = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]] |
---|
1324 | |
---|
1325 | |
---|
1326 | # New datapoints where interpolated values are sought |
---|
1327 | interpolation_points = [[ 0.0, 0.0], |
---|
1328 | [ 0.5, 0.5], |
---|
1329 | [ 0.7, 0.7], |
---|
1330 | [ 1.0, 0.5], |
---|
1331 | [ 2.0, 0.4], |
---|
1332 | [ 2.8, 1.2]] |
---|
1333 | |
---|
1334 | # One quantity |
---|
1335 | Q = num.zeros((8,6), num.float) |
---|
1336 | |
---|
1337 | # Linear in time and space |
---|
1338 | for i, t in enumerate(time): |
---|
1339 | Q[i, :] = t*linear_function(points) |
---|
1340 | |
---|
1341 | # Check interpolation of one quantity using interpolaton points) using default |
---|
1342 | # time_thinning of 1 |
---|
1343 | I = Interpolation_function(time, Q, |
---|
1344 | vertex_coordinates=points, |
---|
1345 | triangles=triangles, |
---|
1346 | interpolation_points=interpolation_points, |
---|
1347 | verbose=False) |
---|
1348 | |
---|
1349 | answer = linear_function(interpolation_points) |
---|
1350 | |
---|
1351 | |
---|
1352 | t = time[0] |
---|
1353 | for j in range(90): #t in [1, 10] |
---|
1354 | for id in range(len(interpolation_points)): |
---|
1355 | assert num.allclose(I(t, id), t*answer[id]) |
---|
1356 | t += 0.1 |
---|
1357 | |
---|
1358 | |
---|
1359 | # Now check time_thinning |
---|
1360 | I = Interpolation_function(time, Q, |
---|
1361 | vertex_coordinates=points, |
---|
1362 | triangles=triangles, |
---|
1363 | interpolation_points=interpolation_points, |
---|
1364 | time_thinning=2, |
---|
1365 | verbose=False) |
---|
1366 | |
---|
1367 | |
---|
1368 | assert len(I.time) == 4 |
---|
1369 | assert( num.allclose(I.time, [1.0, 4.0, 7.0, 9.0] )) |
---|
1370 | |
---|
1371 | answer = linear_function(interpolation_points) |
---|
1372 | |
---|
1373 | t = time[0] |
---|
1374 | for j in range(80): #t in [1, 9] |
---|
1375 | for id in range(len(interpolation_points)): |
---|
1376 | assert num.allclose(I(t, id), t*answer[id]) |
---|
1377 | t += 0.1 |
---|
1378 | |
---|
1379 | |
---|
1380 | |
---|
1381 | def test_interpolation_precompute_points(self): |
---|
1382 | # looking at a discrete mesh |
---|
1383 | # |
---|
1384 | |
---|
1385 | #Three timesteps |
---|
1386 | time = [0.0, 60.0] |
---|
1387 | |
---|
1388 | #Setup mesh used to represent fitted function |
---|
1389 | points = [[ 15., -20.], |
---|
1390 | [ 15., 10.], |
---|
1391 | [ 0., -20.], |
---|
1392 | [ 0., 10.], |
---|
1393 | [ 0., -20.], |
---|
1394 | [ 15., 10.]] |
---|
1395 | |
---|
1396 | triangles = [[0, 1, 2], |
---|
1397 | [3, 4, 5]] |
---|
1398 | |
---|
1399 | #New datapoints where interpolated values are sought |
---|
1400 | interpolation_points = [[ 1., 0.], [0.,1.]] |
---|
1401 | |
---|
1402 | #One quantity |
---|
1403 | Q = num.zeros( (2,6), num.float ) |
---|
1404 | |
---|
1405 | #Linear in time and space |
---|
1406 | for i, t in enumerate(time): |
---|
1407 | Q[i, :] = t*linear_function(points) |
---|
1408 | #print "Q", Q |
---|
1409 | |
---|
1410 | |
---|
1411 | |
---|
1412 | interp = Interpolate(points, triangles) |
---|
1413 | f = num.array([linear_function(points),2*linear_function(points)]) |
---|
1414 | f = num.transpose(f) |
---|
1415 | #print "f",f |
---|
1416 | z = interp.interpolate(f, interpolation_points) |
---|
1417 | answer = [linear_function(interpolation_points), |
---|
1418 | 2*linear_function(interpolation_points) ] |
---|
1419 | answer = num.transpose(answer) |
---|
1420 | #print "z",z |
---|
1421 | #print "answer",answer |
---|
1422 | assert num.allclose(z, answer) |
---|
1423 | |
---|
1424 | |
---|
1425 | #Check interpolation of one quantity using interpolaton points) |
---|
1426 | I = Interpolation_function(time, Q, |
---|
1427 | vertex_coordinates = points, |
---|
1428 | triangles = triangles, |
---|
1429 | interpolation_points = interpolation_points, |
---|
1430 | verbose = False) |
---|
1431 | |
---|
1432 | #print "I.precomputed_values", I.precomputed_values |
---|
1433 | |
---|
1434 | msg = 'Interpolation failed' |
---|
1435 | assert num.allclose(I.precomputed_values['Attribute'][1], [60, 60]), msg |
---|
1436 | #self.failUnless( I.precomputed_values['Attribute'][1] == 60.0, |
---|
1437 | # ' failed') |
---|
1438 | |
---|
1439 | def test_interpolation_function_outside_point(self): |
---|
1440 | # Test spatio-temporal interpolation |
---|
1441 | # Test that spatio temporal function performs the correct |
---|
1442 | # interpolations in both time and space |
---|
1443 | |
---|
1444 | # Three timesteps |
---|
1445 | time = [1.0, 5.0, 6.0] |
---|
1446 | |
---|
1447 | # Setup mesh used to represent fitted function |
---|
1448 | a = [0.0, 0.0] |
---|
1449 | b = [0.0, 2.0] |
---|
1450 | c = [2.0, 0.0] |
---|
1451 | d = [0.0, 4.0] |
---|
1452 | e = [2.0, 2.0] |
---|
1453 | f = [4.0, 0.0] |
---|
1454 | |
---|
1455 | points = [a, b, c, d, e, f] |
---|
1456 | #bac, bce, ecf, dbe |
---|
1457 | triangles = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]] |
---|
1458 | |
---|
1459 | |
---|
1460 | # New datapoints where interpolated values are sought |
---|
1461 | interpolation_points = [[ 0.0, 0.0], |
---|
1462 | [ 0.5, 0.5], |
---|
1463 | [ 0.7, 0.7], |
---|
1464 | [ 1.0, 0.5], |
---|
1465 | [ 2.0, 0.4], |
---|
1466 | [ 545354534, 4354354353]] # outside the mesh |
---|
1467 | |
---|
1468 | # One quantity |
---|
1469 | Q = num.zeros( (3,6), num.float ) |
---|
1470 | |
---|
1471 | # Linear in time and space |
---|
1472 | for i, t in enumerate(time): |
---|
1473 | Q[i, :] = t*linear_function(points) |
---|
1474 | |
---|
1475 | # Check interpolation of one quantity using interpolaton points) |
---|
1476 | |
---|
1477 | I = Interpolation_function(time, Q, |
---|
1478 | vertex_coordinates = points, |
---|
1479 | triangles = triangles, |
---|
1480 | interpolation_points = interpolation_points, |
---|
1481 | verbose = False) |
---|
1482 | |
---|
1483 | |
---|
1484 | assert num.alltrue(I.precomputed_values['Attribute'][:,4] != NAN) |
---|
1485 | assert num.sometrue(I.precomputed_values['Attribute'][:,5] == NAN) |
---|
1486 | |
---|
1487 | #X = I.precomputed_values['Attribute'][1,:] |
---|
1488 | #print X |
---|
1489 | #print take(X, X == NAN) |
---|
1490 | #print where(X == NAN, range(len(X)), 0) |
---|
1491 | |
---|
1492 | answer = linear_function(interpolation_points) |
---|
1493 | |
---|
1494 | t = time[0] |
---|
1495 | for j in range(50): #t in [1, 6] |
---|
1496 | for id in range(len(interpolation_points)-1): |
---|
1497 | assert num.allclose(I(t, id), t*answer[id]) |
---|
1498 | t += 0.1 |
---|
1499 | |
---|
1500 | # Now test the point outside the mesh |
---|
1501 | t = time[0] |
---|
1502 | for j in range(50): #t in [1, 6] |
---|
1503 | self.failUnless(I(t, 5) == NAN, 'Fail!') |
---|
1504 | t += 0.1 |
---|
1505 | |
---|
1506 | try: |
---|
1507 | I(1) |
---|
1508 | except: |
---|
1509 | pass |
---|
1510 | else: |
---|
1511 | raise 'Should raise exception' |
---|
1512 | |
---|
1513 | |
---|
1514 | def test_interpolation_function_time(self): |
---|
1515 | #Test a long time series with an error in it (this did cause an |
---|
1516 | #error once) |
---|
1517 | |
---|
1518 | |
---|
1519 | time = num.array(\ |
---|
1520 | [0.00000000e+00, 5.00000000e-02, 1.00000000e-01, 1.50000000e-01, |
---|
1521 | 2.00000000e-01, 2.50000000e-01, 3.00000000e-01, 3.50000000e-01, |
---|
1522 | 4.00000000e-01, 4.50000000e-01, 5.00000000e-01, 5.50000000e-01, |
---|
1523 | 6.00000000e-01, 6.50000000e-01, 7.00000000e-01, 7.50000000e-01, |
---|
1524 | 8.00000000e-01, 8.50000000e-01, 9.00000000e-01, 9.50000000e-01, |
---|
1525 | 1.00000000e-00, 1.05000000e+00, 1.10000000e+00, 1.15000000e+00, |
---|
1526 | 1.20000000e+00, 1.25000000e+00, 1.30000000e+00, 1.35000000e+00, |
---|
1527 | 1.40000000e+00, 1.45000000e+00, 1.50000000e+00, 1.55000000e+00, |
---|
1528 | 1.60000000e+00, 1.65000000e+00, 1.70000000e+00, 1.75000000e+00, |
---|
1529 | 1.80000000e+00, 1.85000000e+00, 1.90000000e+00, 1.95000000e+00, |
---|
1530 | 2.00000000e+00, 2.05000000e+00, 2.10000000e+00, 2.15000000e+00, |
---|
1531 | 2.20000000e+00, 2.25000000e+00, 2.30000000e+00, 2.35000000e+00, |
---|
1532 | 2.40000000e+00, 2.45000000e+00, 2.50000000e+00, 2.55000000e+00, |
---|
1533 | 2.60000000e+00, 2.65000000e+00, 2.70000000e+00, 2.75000000e+00, |
---|
1534 | 2.80000000e+00, 2.85000000e+00, 2.90000000e+00, 2.95000000e+00, |
---|
1535 | 3.00000000e+00, 3.05000000e+00, 9.96920997e+36, 3.15000000e+00, |
---|
1536 | 3.20000000e+00, 3.25000000e+00, 3.30000000e+00, 3.35000000e+00, |
---|
1537 | 3.40000000e+00, 3.45000000e+00, 3.50000000e+00, 3.55000000e+00, |
---|
1538 | 3.60000000e+00, 3.65000000e+00, 3.70000000e+00, 3.75000000e+00, |
---|
1539 | 3.80000000e+00, 3.85000000e+00, 3.90000000e+00, 3.95000000e+00, |
---|
1540 | 4.00000000e+00, 4.05000000e+00, 4.10000000e+00, 4.15000000e+00, |
---|
1541 | 4.20000000e+00, 4.25000000e+00, 4.30000000e+00, 4.35000000e+00, |
---|
1542 | 4.40000000e+00, 4.45000000e+00, 4.50000000e+00, 4.55000000e+00, |
---|
1543 | 4.60000000e+00, 4.65000000e+00, 4.70000000e+00, 4.75000000e+00, |
---|
1544 | 4.80000000e+00, 4.85000000e+00, 4.90000000e+00, 4.95000000e+00, |
---|
1545 | 5.00000000e+00, 5.05000000e+00, 5.10000000e+00, 5.15000000e+00, |
---|
1546 | 5.20000000e+00, 5.25000000e+00, 5.30000000e+00, 5.35000000e+00, |
---|
1547 | 5.40000000e+00, 5.45000000e+00, 5.50000000e+00, 5.55000000e+00, |
---|
1548 | 5.60000000e+00, 5.65000000e+00, 5.70000000e+00, 5.75000000e+00, |
---|
1549 | 5.80000000e+00, 5.85000000e+00, 5.90000000e+00, 5.95000000e+00, |
---|
1550 | 6.00000000e+00, 6.05000000e+00, 6.10000000e+00, 6.15000000e+00, |
---|
1551 | 6.20000000e+00, 6.25000000e+00, 6.30000000e+00, 6.35000000e+00, |
---|
1552 | 6.40000000e+00, 6.45000000e+00, 6.50000000e+00, 6.55000000e+00, |
---|
1553 | 6.60000000e+00, 6.65000000e+00, 6.70000000e+00, 6.75000000e+00, |
---|
1554 | 6.80000000e+00, 6.85000000e+00, 6.90000000e+00, 6.95000000e+00, |
---|
1555 | 7.00000000e+00, 7.05000000e+00, 7.10000000e+00, 7.15000000e+00, |
---|
1556 | 7.20000000e+00, 7.25000000e+00, 7.30000000e+00, 7.35000000e+00, |
---|
1557 | 7.40000000e+00, 7.45000000e+00, 7.50000000e+00, 7.55000000e+00, |
---|
1558 | 7.60000000e+00, 7.65000000e+00, 7.70000000e+00, 7.75000000e+00, |
---|
1559 | 7.80000000e+00, 7.85000000e+00, 7.90000000e+00, 7.95000000e+00, |
---|
1560 | 8.00000000e+00, 8.05000000e+00, 8.10000000e+00, 8.15000000e+00, |
---|
1561 | 8.20000000e+00, 8.25000000e+00, 8.30000000e+00, 8.35000000e+00, |
---|
1562 | 8.40000000e+00, 8.45000000e+00, 8.50000000e+00, 8.55000000e+00, |
---|
1563 | 8.60000000e+00, 8.65000000e+00, 8.70000000e+00, 8.75000000e+00, |
---|
1564 | 8.80000000e+00, 8.85000000e+00, 8.90000000e+00, 8.95000000e+00, |
---|
1565 | 9.00000000e+00, 9.05000000e+00, 9.10000000e+00, 9.15000000e+00, |
---|
1566 | 9.20000000e+00, 9.25000000e+00, 9.30000000e+00, 9.35000000e+00, |
---|
1567 | 9.40000000e+00, 9.45000000e+00, 9.50000000e+00, 9.55000000e+00, |
---|
1568 | 9.60000000e+00, 9.65000000e+00, 9.70000000e+00, 9.75000000e+00, |
---|
1569 | 9.80000000e+00, 9.85000000e+00, 9.90000000e+00, 9.95000000e+00, |
---|
1570 | 1.00000000e+01, 1.00500000e+01, 1.01000000e+01, 1.01500000e+01, |
---|
1571 | 1.02000000e+01, 1.02500000e+01, 1.03000000e+01, 1.03500000e+01, |
---|
1572 | 1.04000000e+01, 1.04500000e+01, 1.05000000e+01, 1.05500000e+01, |
---|
1573 | 1.06000000e+01, 1.06500000e+01, 1.07000000e+01, 1.07500000e+01, |
---|
1574 | 1.08000000e+01, 1.08500000e+01, 1.09000000e+01, 1.09500000e+01, |
---|
1575 | 1.10000000e+01, 1.10500000e+01, 1.11000000e+01, 1.11500000e+01, |
---|
1576 | 1.12000000e+01, 1.12500000e+01, 1.13000000e+01, 1.13500000e+01, |
---|
1577 | 1.14000000e+01, 1.14500000e+01, 1.15000000e+01, 1.15500000e+01, |
---|
1578 | 1.16000000e+01, 1.16500000e+01, 1.17000000e+01, 1.17500000e+01, |
---|
1579 | 1.18000000e+01, 1.18500000e+01, 1.19000000e+01, 1.19500000e+01, |
---|
1580 | 1.20000000e+01, 1.20500000e+01, 1.21000000e+01, 1.21500000e+01, |
---|
1581 | 1.22000000e+01, 1.22500000e+01, 1.23000000e+01, 1.23500000e+01, |
---|
1582 | 1.24000000e+01, 1.24500000e+01, 1.25000000e+01, 1.25500000e+01, |
---|
1583 | 1.26000000e+01, 1.26500000e+01, 1.27000000e+01, 1.27500000e+01, |
---|
1584 | 1.28000000e+01, 1.28500000e+01, 1.29000000e+01, 1.29500000e+01, |
---|
1585 | 1.30000000e+01, 1.30500000e+01, 1.31000000e+01, 1.31500000e+01, |
---|
1586 | 1.32000000e+01, 1.32500000e+01, 1.33000000e+01, 1.33500000e+01, |
---|
1587 | 1.34000000e+01, 1.34500000e+01, 1.35000000e+01, 1.35500000e+01, |
---|
1588 | 1.36000000e+01, 1.36500000e+01, 1.37000000e+01, 1.37500000e+01, |
---|
1589 | 1.38000000e+01, 1.38500000e+01, 1.39000000e+01, 1.39500000e+01, |
---|
1590 | 1.40000000e+01, 1.40500000e+01, 1.41000000e+01, 1.41500000e+01, |
---|
1591 | 1.42000000e+01, 1.42500000e+01, 1.43000000e+01, 1.43500000e+01, |
---|
1592 | 1.44000000e+01, 1.44500000e+01, 1.45000000e+01, 1.45500000e+01, |
---|
1593 | 1.46000000e+01, 1.46500000e+01, 1.47000000e+01, 1.47500000e+01, |
---|
1594 | 1.48000000e+01, 1.48500000e+01, 1.49000000e+01, 1.49500000e+01, |
---|
1595 | 1.50000000e+01, 1.50500000e+01, 1.51000000e+01, 1.51500000e+01, |
---|
1596 | 1.52000000e+01, 1.52500000e+01, 1.53000000e+01, 1.53500000e+01, |
---|
1597 | 1.54000000e+01, 1.54500000e+01, 1.55000000e+01, 1.55500000e+01, |
---|
1598 | 1.56000000e+01, 1.56500000e+01, 1.57000000e+01, 1.57500000e+01, |
---|
1599 | 1.58000000e+01, 1.58500000e+01, 1.59000000e+01, 1.59500000e+01, |
---|
1600 | 1.60000000e+01, 1.60500000e+01, 1.61000000e+01, 1.61500000e+01, |
---|
1601 | 1.62000000e+01, 1.62500000e+01, 1.63000000e+01, 1.63500000e+01, |
---|
1602 | 1.64000000e+01, 1.64500000e+01, 1.65000000e+01, 1.65500000e+01, |
---|
1603 | 1.66000000e+01, 1.66500000e+01, 1.67000000e+01, 1.67500000e+01, |
---|
1604 | 1.68000000e+01, 1.68500000e+01, 1.69000000e+01, 1.69500000e+01, |
---|
1605 | 1.70000000e+01, 1.70500000e+01, 1.71000000e+01, 1.71500000e+01, |
---|
1606 | 1.72000000e+01, 1.72500000e+01, 1.73000000e+01, 1.73500000e+01, |
---|
1607 | 1.74000000e+01, 1.74500000e+01, 1.75000000e+01, 1.75500000e+01, |
---|
1608 | 1.76000000e+01, 1.76500000e+01, 1.77000000e+01, 1.77500000e+01, |
---|
1609 | 1.78000000e+01, 1.78500000e+01, 1.79000000e+01, 1.79500000e+01, |
---|
1610 | 1.80000000e+01, 1.80500000e+01, 1.81000000e+01, 1.81500000e+01, |
---|
1611 | 1.82000000e+01, 1.82500000e+01, 1.83000000e+01, 1.83500000e+01, |
---|
1612 | 1.84000000e+01, 1.84500000e+01, 1.85000000e+01, 1.85500000e+01, |
---|
1613 | 1.86000000e+01, 1.86500000e+01, 1.87000000e+01, 1.87500000e+01, |
---|
1614 | 1.88000000e+01, 1.88500000e+01, 1.89000000e+01, 1.89500000e+01, |
---|
1615 | 1.90000000e+01, 1.90500000e+01, 1.91000000e+01, 1.91500000e+01, |
---|
1616 | 1.92000000e+01, 1.92500000e+01, 1.93000000e+01, 1.93500000e+01, |
---|
1617 | 1.94000000e+01, 1.94500000e+01, 1.95000000e+01, 1.95500000e+01, |
---|
1618 | 1.96000000e+01, 1.96500000e+01, 1.97000000e+01, 1.97500000e+01, |
---|
1619 | 1.98000000e+01, 1.98500000e+01, 1.99000000e+01, 1.99500000e+01, |
---|
1620 | 2.00000000e+01, 2.00500000e+01, 2.01000000e+01, 2.01500000e+01, |
---|
1621 | 2.02000000e+01, 2.02500000e+01, 2.03000000e+01, 2.03500000e+01, |
---|
1622 | 2.04000000e+01, 2.04500000e+01, 2.05000000e+01, 2.05500000e+01, |
---|
1623 | 2.06000000e+01, 2.06500000e+01, 2.07000000e+01, 2.07500000e+01, |
---|
1624 | 2.08000000e+01, 2.08500000e+01, 2.09000000e+01, 2.09500000e+01, |
---|
1625 | 2.10000000e+01, 2.10500000e+01, 2.11000000e+01, 2.11500000e+01, |
---|
1626 | 2.12000000e+01, 2.12500000e+01, 2.13000000e+01, 2.13500000e+01, |
---|
1627 | 2.14000000e+01, 2.14500000e+01, 2.15000000e+01, 2.15500000e+01, |
---|
1628 | 2.16000000e+01, 2.16500000e+01, 2.17000000e+01, 2.17500000e+01, |
---|
1629 | 2.18000000e+01, 2.18500000e+01, 2.19000000e+01, 2.19500000e+01, |
---|
1630 | 2.20000000e+01, 2.20500000e+01, 2.21000000e+01, 2.21500000e+01, |
---|
1631 | 2.22000000e+01, 2.22500000e+01, 2.23000000e+01, 2.23500000e+01, |
---|
1632 | 2.24000000e+01, 2.24500000e+01, 2.25000000e+01]) |
---|
1633 | |
---|
1634 | #print 'Diff', time[1:] - time[:-1] |
---|
1635 | |
---|
1636 | #Setup mesh used to represent fitted function |
---|
1637 | a = [0.0, 0.0] |
---|
1638 | b = [0.0, 2.0] |
---|
1639 | c = [2.0, 0.0] |
---|
1640 | d = [0.0, 4.0] |
---|
1641 | e = [2.0, 2.0] |
---|
1642 | f = [4.0, 0.0] |
---|
1643 | |
---|
1644 | points = [a, b, c, d, e, f] |
---|
1645 | #bac, bce, ecf, dbe |
---|
1646 | triangles = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]] |
---|
1647 | |
---|
1648 | |
---|
1649 | #New datapoints where interpolated values are sought |
---|
1650 | interpolation_points = [[ 0.0, 0.0], |
---|
1651 | [ 0.5, 0.5], |
---|
1652 | [ 0.7, 0.7], |
---|
1653 | [ 1.0, 0.5], |
---|
1654 | [ 2.0, 0.4], |
---|
1655 | [ 545354534, 4354354353]] # outside the mesh |
---|
1656 | |
---|
1657 | #One quantity |
---|
1658 | Q = num.zeros( (len(time),6), num.float ) |
---|
1659 | |
---|
1660 | #Linear in time and space |
---|
1661 | for i, t in enumerate(time): |
---|
1662 | Q[i, :] = t*linear_function(points) |
---|
1663 | |
---|
1664 | #Check interpolation of one quantity using interpolaton points) |
---|
1665 | try: |
---|
1666 | I = Interpolation_function(time, Q, |
---|
1667 | vertex_coordinates = points, |
---|
1668 | triangles = triangles, |
---|
1669 | interpolation_points = interpolation_points, |
---|
1670 | verbose = False) |
---|
1671 | except: |
---|
1672 | pass |
---|
1673 | else: |
---|
1674 | raise 'Should raise exception due to time being non-monotoneous' |
---|
1675 | |
---|
1676 | |
---|
1677 | def test_points_outside_the_polygon(self): |
---|
1678 | a = [-1.0, 0.0] |
---|
1679 | b = [3.0, 4.0] |
---|
1680 | c = [4.0, 1.0] |
---|
1681 | d = [-3.0, 2.0] #3 |
---|
1682 | e = [-1.0, -2.0] |
---|
1683 | f = [1.0, -2.0] #5 |
---|
1684 | |
---|
1685 | vertices = [a, b, c, d,e,f] |
---|
1686 | triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc |
---|
1687 | |
---|
1688 | point_coords = [[-2.0, 2.0], |
---|
1689 | [-1.0, 1.0], |
---|
1690 | [9999.0, 9999.0], # point Outside poly |
---|
1691 | [-9999.0, 1.0], # point Outside poly |
---|
1692 | [2.0, 1.0], |
---|
1693 | [0.0, 0.0], |
---|
1694 | [1.0, 0.0], |
---|
1695 | [0.0, -1.0], |
---|
1696 | [-0.2, -0.5], |
---|
1697 | [-0.9, -1.5], |
---|
1698 | [0.5, -1.9], |
---|
1699 | [999999, 9999999]] # point Outside poly |
---|
1700 | geo_data = Geospatial_data(data_points = point_coords) |
---|
1701 | |
---|
1702 | interp = Interpolate(vertices, triangles) |
---|
1703 | f = num.array([linear_function(vertices),2*linear_function(vertices)]) |
---|
1704 | f = num.transpose(f) |
---|
1705 | #print "f",f |
---|
1706 | z = interp.interpolate(f, geo_data) |
---|
1707 | #z = interp.interpolate(f, point_coords) |
---|
1708 | answer = [linear_function(point_coords), |
---|
1709 | 2*linear_function(point_coords) ] |
---|
1710 | answer = num.transpose(answer) |
---|
1711 | answer[2,:] = [NAN, NAN] |
---|
1712 | answer[3,:] = [NAN, NAN] |
---|
1713 | answer[11,:] = [NAN, NAN] |
---|
1714 | #print "z",z |
---|
1715 | #print "answer _ fixed",answer |
---|
1716 | assert num.allclose(z[0:1], answer[0:1]) |
---|
1717 | assert num.allclose(z[4:10], answer[4:10]) |
---|
1718 | for i in [2,3,11]: |
---|
1719 | self.failUnless( z[i,1] == answer[11,1], 'Fail!') |
---|
1720 | self.failUnless( z[i,0] == answer[11,0], 'Fail!') |
---|
1721 | |
---|
1722 | def test_interpolate_sww2csv(self): |
---|
1723 | |
---|
1724 | def elevation_function(x, y): |
---|
1725 | return -x |
---|
1726 | |
---|
1727 | # Create mesh |
---|
1728 | mesh_file = tempfile.mktemp(".tsh") |
---|
1729 | points = [[0.0,0.0],[6.0,0.0],[6.0,6.0],[0.0,6.0]] |
---|
1730 | m = Mesh() |
---|
1731 | m.add_vertices(points) |
---|
1732 | m.auto_segment() |
---|
1733 | m.generate_mesh(verbose=False) |
---|
1734 | m.export_mesh_file(mesh_file) |
---|
1735 | |
---|
1736 | # Create shallow water domain |
---|
1737 | domain = Domain(mesh_file) |
---|
1738 | os.remove(mesh_file) |
---|
1739 | |
---|
1740 | domain.default_order = 2 |
---|
1741 | |
---|
1742 | # This test was made before tight_slope_limiters were introduced |
---|
1743 | # Since were are testing interpolation values this is OK |
---|
1744 | domain.tight_slope_limiters = 0 |
---|
1745 | |
---|
1746 | # Set some field values |
---|
1747 | domain.set_quantity('elevation', elevation_function) |
---|
1748 | domain.set_quantity('friction', 0.03) |
---|
1749 | domain.set_quantity('xmomentum', 3.0) |
---|
1750 | domain.set_quantity('ymomentum', 4.0) |
---|
1751 | |
---|
1752 | ###################### |
---|
1753 | # Boundary conditions |
---|
1754 | B = Transmissive_boundary(domain) |
---|
1755 | domain.set_boundary( {'exterior': B}) |
---|
1756 | |
---|
1757 | # This call mangles the stage values. |
---|
1758 | domain.distribute_to_vertices_and_edges() |
---|
1759 | domain.set_quantity('stage', 1.0) |
---|
1760 | |
---|
1761 | |
---|
1762 | domain.set_name('datatest' + str(time.time())) |
---|
1763 | domain.smooth = True |
---|
1764 | domain.reduction = mean |
---|
1765 | |
---|
1766 | sww = SWW_file(domain) |
---|
1767 | sww.store_connectivity() |
---|
1768 | sww.store_timestep() |
---|
1769 | domain.set_quantity('stage', 10.0) # This is automatically limited |
---|
1770 | # So it will not be less than the elevation |
---|
1771 | domain.time = 2. |
---|
1772 | sww.store_timestep() |
---|
1773 | |
---|
1774 | # Test the function |
---|
1775 | points = [[5.0,1.],[0.5,2.]] |
---|
1776 | depth_file = tempfile.mktemp(".csv") |
---|
1777 | velocity_x_file = tempfile.mktemp(".csv") |
---|
1778 | velocity_y_file = tempfile.mktemp(".csv") |
---|
1779 | interpolate_sww2csv(sww.filename, points, depth_file, |
---|
1780 | velocity_x_file, |
---|
1781 | velocity_y_file, |
---|
1782 | verbose=False) |
---|
1783 | |
---|
1784 | depth_answers_array = [[0.0, 6.0, 1.5], [2.0, 15., 10.5]] |
---|
1785 | velocity_x_answers_array = [[0.0, 3./6.0, 3./1.5], |
---|
1786 | [2.0, 3./15., 3/10.5]] |
---|
1787 | velocity_y_answers_array = [[0.0, 4./6.0, 4./1.5], |
---|
1788 | [2.0, 4./15., 4./10.5]] |
---|
1789 | depth_file_handle = file(depth_file) |
---|
1790 | depth_reader = csv.reader(depth_file_handle) |
---|
1791 | depth_reader.next() |
---|
1792 | velocity_x_file_handle = file(velocity_x_file) |
---|
1793 | velocity_x_reader = csv.reader(velocity_x_file_handle) |
---|
1794 | velocity_x_reader.next() |
---|
1795 | for depths, velocitys, depth_answers, velocity_answers in map(None, |
---|
1796 | depth_reader, |
---|
1797 | velocity_x_reader, |
---|
1798 | depth_answers_array, |
---|
1799 | velocity_x_answers_array): |
---|
1800 | for i in range(len(depths)): |
---|
1801 | #print "depths",depths[i] |
---|
1802 | #print "depth_answers",depth_answers[i] |
---|
1803 | #print "velocitys",velocitys[i] |
---|
1804 | #print "velocity_answers_array", velocity_answers[i] |
---|
1805 | msg = 'Interpolation failed' |
---|
1806 | assert num.allclose(float(depths[i]), depth_answers[i]), msg |
---|
1807 | assert num.allclose(float(velocitys[i]), velocity_answers[i]), msg |
---|
1808 | |
---|
1809 | velocity_y_file_handle = file(velocity_y_file) |
---|
1810 | velocity_y_reader = csv.reader(velocity_y_file_handle) |
---|
1811 | velocity_y_reader.next() |
---|
1812 | for velocitys, velocity_answers in map(None, |
---|
1813 | velocity_y_reader, |
---|
1814 | velocity_y_answers_array): |
---|
1815 | for i in range(len(depths)): |
---|
1816 | #print "depths",depths[i] |
---|
1817 | #print "depth_answers",depth_answers[i] |
---|
1818 | #print "velocitys",velocitys[i] |
---|
1819 | #print "velocity_answers_array", velocity_answers[i] |
---|
1820 | msg = 'Interpolation failed' |
---|
1821 | assert num.allclose(float(depths[i]), depth_answers[i]), msg |
---|
1822 | assert num.allclose(float(velocitys[i]), velocity_answers[i]), msg |
---|
1823 | |
---|
1824 | # clean up |
---|
1825 | depth_file_handle.close() |
---|
1826 | velocity_y_file_handle.close() |
---|
1827 | velocity_x_file_handle.close() |
---|
1828 | #print "sww.filename",sww.filename |
---|
1829 | os.remove(sww.filename) |
---|
1830 | os.remove(depth_file) |
---|
1831 | os.remove(velocity_x_file) |
---|
1832 | os.remove(velocity_y_file) |
---|
1833 | |
---|
1834 | |
---|
1835 | def test_interpolate_one_point_many_triangles(self): |
---|
1836 | z0 = [2.0, 5.0] |
---|
1837 | |
---|
1838 | v0 = [0.0, 0.0] |
---|
1839 | v1 = [1.0, 0.0] |
---|
1840 | v2 = [2.0, 0.0] |
---|
1841 | v3 = [3.0, 0.0] |
---|
1842 | v4 = [4.0, 0.0] |
---|
1843 | v5 = [5.0, 0.0] |
---|
1844 | v6 = [6.0, 0.0] |
---|
1845 | v7 = [0.0, 10.0] |
---|
1846 | v8 = [1.0, 10.0] |
---|
1847 | v9 = [2.0, 10.0] |
---|
1848 | v10= [3.0, 10.0] |
---|
1849 | v11= [4.0, 10.0] |
---|
1850 | v12= [5.0, 10.0] |
---|
1851 | v13= [6.0, 10.0] |
---|
1852 | |
---|
1853 | vertices = [z0,v0, v1, v2, v3,v4 ,v5, v6, v7, v8, v9, v10, v11, |
---|
1854 | v12, v13] |
---|
1855 | triangles = [ |
---|
1856 | [0,1,2], |
---|
1857 | [0,2,3], |
---|
1858 | [0,3,4], |
---|
1859 | [0,4,5], |
---|
1860 | [0,5,6], |
---|
1861 | [0,6,7], |
---|
1862 | [0,9,8], |
---|
1863 | [0,10,9], |
---|
1864 | [0,11,10], |
---|
1865 | [0,12,11], |
---|
1866 | [0,13,12], |
---|
1867 | [0,14,13] |
---|
1868 | ] |
---|
1869 | |
---|
1870 | d0 = [1.0, 1.0] |
---|
1871 | d1 = [1.0, 2.0] |
---|
1872 | d2 = [3.0, 1.0] |
---|
1873 | point_coords = [ d0, d1, d2] |
---|
1874 | try: |
---|
1875 | interp = Interpolate(vertices, triangles) |
---|
1876 | except RuntimeError: |
---|
1877 | self.failUnless(0 ==1, 'quad fails with 14 verts at the same \ |
---|
1878 | position. Should be able to handle any number.') |
---|
1879 | f = linear_function(vertices) |
---|
1880 | z = interp.interpolate(f, point_coords) |
---|
1881 | answer = linear_function(point_coords) |
---|
1882 | |
---|
1883 | #print "z",z |
---|
1884 | #print "answer",answer |
---|
1885 | assert num.allclose(z, answer) |
---|
1886 | |
---|
1887 | ################################################################################ |
---|
1888 | |
---|
1889 | if __name__ == "__main__": |
---|
1890 | suite = unittest.makeSuite(Test_Interpolate,'test') |
---|
1891 | runner = unittest.TextTestRunner() #verbosity=1) |
---|
1892 | runner.run(suite) |
---|
1893 | |
---|