source: anuga_core/source/anuga/fit_interpolate/test_interpolate.py @ 3941

Last change on this file since 3941 was 3941, checked in by duncan, 17 years ago

change Interpolation_function (in interpolate.py) so it accepts geospatial data objects. See ticket#108

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