Changeset 328 for inundation/ga/storm_surge/pyvolution/least_squares.py
- Timestamp:
- Sep 21, 2004, 1:26:23 PM (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/ga/storm_surge/pyvolution/least_squares.py
r321 r328 15 15 EPSILON = 1.0e-13 16 16 17 18 17 from general_mesh import General_Mesh 19 18 from Numeric import zeros, array, Float, Int, dot,transpose 20 19 from LinearAlgebra import solve_linear_equations 20 21 22 def smooth_attributes_to_mesh(vertex_coordinates, 23 triangles, 24 point_coordinates, 25 point_attributes): 26 """ 27 """ 28 interp = Interpolation(vertex_coordinates, 29 triangles, 30 point_coordinates) 31 vertex_attributes = interp.smooth_attributes_to_mesh(point_attributes) 32 return vertex_attributes 21 33 22 34 … … 52 64 self.mesh = General_Mesh(vertex_coordinates, triangles) 53 65 54 self.A _m= zeros((1,1), Float) #Not initialised66 self.A = zeros((1,1), Float) #Not initialised 55 67 56 68 if point_coordinates: 57 self.build_A _m(point_coordinates)69 self.build_A(point_coordinates) 58 70 59 def build_A_m(self, point_coordinates): 71 def build_A(self, point_coordinates): 72 """ 73 "A" is the matrix of hat functions. It has 1 row per data point and 1 column per vertex. 74 75 Post Condition 76 "A" has been initialised 77 """ 60 78 61 79 #Convert input to Numeric arrays … … 66 84 n = point_coordinates.shape[0] #Number of data points 67 85 68 self.A _m= zeros((n,m), Float)86 self.A = zeros((n,m), Float) 69 87 70 88 #Compute matrix elements … … 99 117 if sigma0 >= 0 and sigma1 >= 0 and sigma2 >= 0: 100 118 101 #Assign values to A _m119 #Assign values to A 102 120 j = self.mesh.triangles[k,0] #Global vertex id 103 self.A _m[i, j] = sigma0121 self.A[i, j] = sigma0 104 122 105 123 j = self.mesh.triangles[k,1] #Global vertex id 106 self.A _m[i, j] = sigma1124 self.A[i, j] = sigma1 107 125 108 126 j = self.mesh.triangles[k,2] #Global vertex id 109 self.A _m[i, j] = sigma2127 self.A[i, j] = sigma2 110 128 111 129 112 def smooth_att _to_mesh(self, z):130 def smooth_attributes_to_mesh(self, z): 113 131 """ Linearly interpolate many points with an attribute to the vertices. 114 132 Pre Condition: 115 A_mhas been initialised133 "A" has been initialised 116 134 117 135 Inputs: … … 121 139 #Convert input to Numeric arrays 122 140 z = array(z).astype(Float) 123 At _m = transpose(self.A_m)141 At = transpose(self.A) 124 142 #print "z", z 125 #print "At _m",At_m126 self.AtA _m = dot(At_m, self.A_m)127 Atz _m = dot(At_m, z)128 f = solve_linear_equations(self.AtA _m,Atz_m)143 #print "At",At 144 self.AtA = dot(At, self.A) 145 Atz = dot(At, z) 146 f = solve_linear_equations(self.AtA,Atz) 129 147 return f 130 148 131 def smooth_atts_to_mesh(self, z_m): 132 """ Linearly interpolate many points with attributes to the vertices. 149 150 def interpolate_attributes_to_points(self, f): 151 """ Linearly interpolate vertices with attributes to the points. 133 152 Pre Condition: 134 A_m has been initialised 135 136 Inputs: 137 z_m: A Matrix of data at the point_coordinates. 138 One attribute per colum. 139 140 """ 141 #Convert input to Numeric arrays 142 z_m = array(z_m).astype(Float) 143 144 145 #Build n x m interpolation matrix 146 m = self.mesh.coordinates.shape[0] #Number of vertices 147 n = z_m.shape[1] #Number of data points 148 149 f_m = zeros((n,m), Float) 150 151 for i in range(z_m.shape[1]): 152 f_m[:,i] = smooth_att_to_mesh(z_m[:,i]) 153 154 155 def smooth_att_to_points(self, f): 156 """ Linearly interpolate vertices with an attribute to the points. 157 Pre Condition: 158 A_m has been initialised 153 A has been initialised 159 154 160 155 Inputs: 161 z: Vector of data at the point_coordinates. One value per point. 162 163 """ 164 return dot(self.A_m,f) 165 166 def smooth_atts_to_points(self, f_m): 167 """ Linearly interpolate vertices with attributes to the points. 168 Pre Condition: 169 A_m has been initialised 170 171 Inputs: 172 f_m: Matrix of data at the vertices. 156 f: Matrix of data at the vertices. 173 157 One attribute per colum. 174 158 175 159 """ 176 #Convert input to Numeric arrays 177 z_m = array(z_m).astype(Float) 160 return dot(self.A,f) 178 161 162 #------------------------------------------------------------- 163 if __name__ == "__main__": 164 165 import os, sys 166 from load_mesh.loadASCII import mesh_file_to_mesh_dictionary 167 usage = "usage: %s pmesh_file_name" % os.path.basename(sys.argv[0]) 168 169 if len(sys.argv) < 2: 170 print usage 171 else: 172 mesh_file_name = sys.argv[1] 173 174 meshdic = mesh_file_to_mesh_dictionary(mesh_file_name) 175 vertex_coordinates = meshdic['generatedpointlist'] 176 triangles = meshdic['generatedtrianglelist'] 179 177 180 #Build n x m interpolation matrix 181 m = self.mesh.coordinates.shape[0] #Number of vertices 182 n = z_m.shape[1] #Number of data points 183 184 f_m = zeros((n,m), Float) 178 d1 = [1.0, 1.0] 179 d2 = [1.0, 3.0] 180 d3 = [3.0,1.0] 181 z1 = 2 182 z2 = 4 183 z3 = 4 184 data_coords = [ d1, d2, d3] 185 z = [z1, z2, z3] 185 186 186 for i in range(z_m.shape[1]): 187 f_m[:,i] = smooth_att_to_mesh(z_m[:,i]) 187 f = smooth_attributes_to_mesh(vertex_coordinates, 188 triangles, 189 data_coords, 190 z) 191 print f 188 192 193
Note: See TracChangeset
for help on using the changeset viewer.