source: inundation/ga/storm_surge/pyvolution/least_squares.py @ 303

Last change on this file since 303 was 303, checked in by ole, 21 years ago

comments

File size: 3.1 KB
Line 
1"""Least squares smooting and interpolation.
2   Very first cut - just to check a few things.
3   Architecture is not set in concrete at all.
4
5   O
6"""
7
8
9from mesh import Mesh
10
11
12class Interpolation(Mesh):
13
14    def __init__(self, vertex_coordinates, triangles, data):
15        """ Build interpolation matrix mapping from
16        function values at vertices to function values at data points
17
18        Inputs:
19       
20          vertex_coordinates: List of coordinate pairs [xi, eta] of points
21          constituting mesh (or a an m x 2 Numeric array)
22       
23          triangles: List of 3-tuples (or a Numeric array) of
24          integers representing indices of all vertices in the mesh.
25
26          data: List of coordinate pairs [x, y] of data points
27          (or an nx2 Numeric array)
28         
29        """
30
31        from Numeric import zeros, array, Float, Int, dot
32        from config import epsilon
33
34        #Convert input to Numeric arrays
35        data = array(data).astype(Float)
36        vertex_coordinates = array(vertex_coordinates).astype(Float)
37        triangles = array(triangles).astype(Int)               
38       
39        #Build underlying mesh
40        Mesh.__init__(self, vertex_coordinates, triangles)
41       
42
43        #Build n x m interpolation matrix       
44        m = vertex_coordinates.shape[0] #Number of basis functions (1/vertex)
45        n = data.shape[0]               #Number of data points         
46
47        self.matrix = zeros((n,m), Float)
48
49        #Compute matrix elements
50        for i in range(n):
51            #For each data point
52
53            x = data[i]
54            for k in range(len(self)):
55                #For each triangle (brute force)
56                #FIXME: Real algorithm should only visit relevant triangles
57
58                #Get the three vertex_points
59                xi0 = self.get_vertex_coordinate(k, 0)
60                xi1 = self.get_vertex_coordinate(k, 1)
61                xi2 = self.get_vertex_coordinate(k, 2)                 
62
63                #Get the three normals
64                n0 = self.get_normal(k, 0)
65                n1 = self.get_normal(k, 1)
66                n2 = self.get_normal(k, 2)               
67
68                #Compute interpolation
69                sigma2 = dot((x-xi0), n2)/dot((xi2-xi0), n2)
70                sigma0 = dot((x-xi1), n0)/dot((xi0-xi1), n0)
71                sigma1 = dot((x-xi2), n1)/dot((xi1-xi2), n1)
72
73                #FIXME: Maybe move out to test or something
74                assert abs(sigma0 + sigma1 + sigma2 - 1.0) < epsilon
75
76               
77                #Check that this trinagle contains data point
78                if sigma0 >= 0 and sigma1 >= 0 and sigma2 >= 0:
79
80                    #Assign values to matrix
81                    v_id = self.vertices[k,0]
82                    self.matrix[i, v_id] = sigma0
83
84                    v_id = self.vertices[k,1]
85                    self.matrix[i, v_id] = sigma1
86
87                    v_id = self.vertices[k,2]
88                    self.matrix[i, v_id] = sigma2
89
90               
91
92               
93
94               
95               
96               
Note: See TracBrowser for help on using the repository browser.