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

Last change on this file since 4779 was 4779, checked in by duncan, 16 years ago

reduce memory use in quantity.set_value. fit_to_mesh can now use an existing mesh instance, which it does in quantity.set_value.

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