Changeset 2659


Ignore:
Timestamp:
Apr 5, 2006, 11:22:10 AM (18 years ago)
Author:
duncan
Message:

Added an extra test, changed interpolate function to handle points outside the mesh

Location:
inundation/fit_interpolate
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • inundation/fit_interpolate/interpolate.py

    r2655 r2659  
    265265        """
    266266        z = self._A * f
    267         for i in self.outside_poly_indices: # Taking into account points outside the
    268                                             # mesh.
    269             z[i,:] = INF
     267        # Taking into account points outside the mesh.
     268        #print "self.outside_poly_indices", self.outside_poly_indices
     269        #print "self.inside_poly_indices", self.inside_poly_indices
     270        #print "z", z
     271        #print "z.size", z.size
     272        for i in self.outside_poly_indices:
     273            z[i] = INF
    270274        return z
    271275
     
    511515                    #Use precomputed point
    512516                    Q0 = Q[self.index, point_id]
    513                     if ratio > 0: Q1 = Q[self.index+1, point_id]
    514 
     517                    if ratio > 0:
     518                        Q1 = Q[self.index+1, point_id]
     519           
    515520            #Linear temporal interpolation   
    516521            if ratio > 0:
    517                 q[i] = Q0 + ratio*(Q1 - Q0)
     522                if Q0 == INF and Q1 == INF:
     523                    q[i]  = Q0
     524                else:
     525                    q[i] = Q0 + ratio*(Q1 - Q0)
    518526            else:
    519527                q[i] = Q0
  • inundation/fit_interpolate/test_interpolate.py

    r2655 r2659  
    727727        # interpolations in both time and space
    728728   
    729 
    730729        #Three timesteps
    731         time = [1.0, 5.0, 6.0]
    732        
     730        time = [1.0, 5.0, 6.0]   
    733731
    734732        #Setup mesh used to represent fitted function
     
    753751                                [ 2.8, 1.2]]
    754752
    755 
    756753        #One quantity
    757754        Q = zeros( (3,6), Float )
     
    760757        for i, t in enumerate(time):
    761758            Q[i, :] = t*linear_function(points)
    762 
    763759
    764760        #Check interpolation of one quantity using interpolaton points)
     
    769765                                   verbose = False)
    770766
    771 
    772767        answer = linear_function(interpolation_points)
    773768       
     
    776771            for id in range(len(interpolation_points)):
    777772                assert allclose(I(t, id), t*answer[id])
    778 
    779773            t += 0.1   
    780774
     
    787781
    788782
    789     def test_points_outside_the_polygon(self):
    790         a = [-1.0, 0.0]
    791         b = [3.0, 4.0]
    792         c = [4.0, 1.0]
    793         d = [-3.0, 2.0] #3
    794         e = [-1.0, -2.0]
    795         f = [1.0, -2.0] #5
    796 
    797         vertices = [a, b, c, d,e,f]
    798         triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc
    799 
    800         point_coords = [[-2.0, 2.0],
    801                         [-1.0, 1.0],
    802                         [0.0, 2.0],
    803                         [1.0, 1.0],
    804                         [2.0, 1.0],
    805                         [0.0, 0.0],
    806                         [1.0, 0.0],
    807                         [0.0, -1.0],
    808                         [-0.2, -0.5],
    809                         [-0.9, -1.5],
    810                         [0.5, -1.9],
    811                         [999999, 9999999]] # point Outside poly
    812         geo_data = Geospatial_data(data_points = point_coords)
    813 
    814         interp = Interpolate(vertices, triangles)
    815         f = array([linear_function(vertices),2*linear_function(vertices) ])
    816         f = transpose(f)
    817         #print "f",f
    818         z = interp.interpolate(f, geo_data)
    819         #z = interp.interpolate(f, point_coords)
    820         answer = [linear_function(point_coords),
    821                   2*linear_function(point_coords) ]
    822         answer = transpose(answer)
    823         answer[11,:] = [INF, INF]
    824         #print "z",z
    825         #print "answer _ fixed",answer
    826         assert allclose(z[0:10], answer[0:10])
    827         self.failUnless( z[11,1] == answer[11,1], 'Fail!')
    828         self.failUnless( z[11,0] == answer[11,0], 'Fail!')
    829 
    830        
     783    def test_interpolation_function_outside_point(self):
     784        # Test spatio-temporal interpolation
     785        # Test that spatio temporal function performs the correct
     786        # interpolations in both time and space
     787   
     788        #Three timesteps
     789        time = [1.0, 5.0, 6.0]   
     790
     791        #Setup mesh used to represent fitted function
     792        a = [0.0, 0.0]
     793        b = [0.0, 2.0]
     794        c = [2.0, 0.0]
     795        d = [0.0, 4.0]
     796        e = [2.0, 2.0]
     797        f = [4.0, 0.0]
     798
     799        points = [a, b, c, d, e, f]
     800        #bac, bce, ecf, dbe
     801        triangles = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]
     802
     803
     804        #New datapoints where interpolated values are sought
     805        interpolation_points = [[ 0.0, 0.0],
     806                                [ 0.5, 0.5],
     807                                [ 0.7, 0.7],
     808                                [ 1.0, 0.5],
     809                                [ 2.0, 0.4],
     810                                [ 545354534, 4354354353]] # outside the mesh
     811
     812        #One quantity
     813        Q = zeros( (3,6), Float )
     814
     815        #Linear in time and space
     816        for i, t in enumerate(time):
     817            Q[i, :] = t*linear_function(points)
     818
     819        #Check interpolation of one quantity using interpolaton points)
     820        I = Interpolation_function(time, Q,
     821                                   vertex_coordinates = points,
     822                                   triangles = triangles,
     823                                   interpolation_points = interpolation_points,
     824                                   verbose = False)
     825
     826        answer = linear_function(interpolation_points)
     827         
     828        t = time[0]
     829        for j in range(50): #t in [1, 6]
     830            for id in range(len(interpolation_points)-1):
     831                assert allclose(I(t, id), t*answer[id])
     832            t += 0.1
     833           
     834        # Now test the point outside the mesh
     835        t = time[0]
     836        for j in range(50): #t in [1, 6]
     837            self.failUnless(I(t, 5) == INF, 'Fail!')
     838            t += 0.1 
     839           
     840        try:   
     841            I(1)
     842        except:
     843            pass
     844        else:
     845            raise 'Should raise exception'
     846
    831847    def test_points_outside_the_polygon(self):
    832848        a = [-1.0, 0.0]
     
    877893if __name__ == "__main__":
    878894
    879     #suite = unittest.makeSuite(Test_Interpolate,'test')
    880895    suite = unittest.makeSuite(Test_Interpolate,'test')
    881896    #suite = unittest.makeSuite(Test_Interpolate,'test_points_outside_the_polygon')
Note: See TracChangeset for help on using the changeset viewer.