Changeset 485 for inundation/ga
- Timestamp:
- Nov 5, 2004, 10:51:54 AM (20 years ago)
- Location:
- inundation/ga/storm_surge/pyvolution
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/ga/storm_surge/pyvolution/least_squares.py
r484 r485 19 19 20 20 21 #FIXME (Ole): Currently datapoints outside the triangular mesh are ignored. 22 # Is there a clean way of inlcuding them? 23 24 21 25 import exceptions 22 26 class ShapeError(exceptions.Exception): pass … … 30 34 try: 31 35 from util import gradient 32 33 36 except ImportError, e: 34 37 #FIXME reduce the dependency of modules in pyvolution … … 50 53 DEFAULT_ALPHA = 0.001 51 54 52 53 55 def fit_to_mesh_file(mesh_file, point_file, mesh_output_file, alpha=DEFAULT_ALPHA): 54 56 """ … … 67 69 old_title_list = mesh_dict['generatedpointattributetitlelist'] 68 70 69 71 70 72 # load in the .xya file 71 72 73 point_dict = load_xya_file(point_file) 73 74 point_coordinates = point_dict['pointlist'] 74 75 point_attributes = point_dict['pointattributelist'] 75 76 title_string = point_dict['title'] 76 title_list = title_string.split(',') # iffy! Hard coding title delimiter77 title_list = title_string.split(',') #FIXME iffy! Hard coding title delimiter 77 78 for i in range(len(title_list)): 78 79 title_list[i] = title_list[i].strip() … … 218 219 self.AtA = Sparse(m,m) 219 220 220 #Build quad tree of vertices 221 #print 'Building quad tree' 221 #Build quad tree of vertices (FIXME: Is this the right spot for that?) 222 222 root = build_quadtree(self.mesh) 223 #print 'Quad tree built'224 #root.show()225 223 226 224 #Compute matrix elements … … 235 233 candidate_vertices = root.search(x[0], x[1]) 236 234 237 #Find triangle containing x 235 #Find triangle containing x: 238 236 element_found = False 237 238 #For all vertices in same cell as point x 239 239 for v in candidate_vertices: 240 for triangle_id, vertex_id in self.mesh.vertexlist[v]: 241 242 k = triangle_id 240 241 #for each triangle id (k) which has v as a vertex 242 for k, _ in self.mesh.vertexlist[v]: 243 243 244 244 #Get the three vertex_points of candidate triangle 245 245 xi0 = self.mesh.get_vertex_coordinate(k, 0) 246 246 xi1 = self.mesh.get_vertex_coordinate(k, 1) 247 xi2 = self.mesh.get_vertex_coordinate(k, 2) 247 xi2 = self.mesh.get_vertex_coordinate(k, 2) 248 248 249 249 #Get the three normals … … 269 269 #Don't look for any other triangle 270 270 break 271 271 272 273 #Update interpolation matrix A if necessary 272 274 if element_found is True: 273 275 #Assign values to matrix A … … 291 293 else: 292 294 pass 293 #Ok if there is no triangle for datapoint (as in brute force version) 295 #Ok if there is no triangle for datapoint 296 #(as in brute force version) 294 297 #raise 'Could not find triangle for point', x 295 298 … … 301 304 m is the number of basis functions phi_k (one per vertex) 302 305 303 304 This is the brute force which is too slow for large problems, but good for testing306 This is the brute force which is too slow for large problems, 307 but could be used for testing 305 308 """ 306 309 … … 413 416 #"element stiffness matrices: 414 417 415 416 418 m = self.mesh.coordinates.shape[0] #Nbr of basis functions (1/vertex) 417 419 418 #self.D = zeros((m,m), Float)419 420 self.D = Sparse(m,m) 420 421 … … 463 464 self.D[v0,v2] += e20 464 465 465 #self.D = (self.D).tocsc()466 466 467 467 def fit(self, z): … … 488 488 Atz = self.A.trans_mult(z) 489 489 490 491 #print 'fit: Atz',Atz492 490 493 491 #Check sanity … … 500 498 501 499 502 #Solve and return503 #return solve_linear_equations(self.get_B(), Atz)504 505 # caused errors506 #return sparse.solve(self.B,Atz)507 508 500 return conjugate_gradient(self.B, Atz, Atz,imax=2*len(Atz) ) 509 501 #FIXME: Should we store the result here for later use? (ON) 510 502 503 511 504 def fit_points(self, z): 512 """ 513 Like fit, but more robust when each point has two or more attributes 514 """ 505 """Like fit, but more robust when each point has two or more attributes 506 FIXME(Ole): The name fit_points doesn't carry any meaning for me. 507 How about something like fit_multiple or fit_columns? 508 """ 509 515 510 try: 516 511 return self.fit(z) … … 525 520 n = z.shape[1] #Number of data points 526 521 527 f = zeros((m,n), Float) 528 #f = sparse.dok_matrix() # even though it wont be sparse? 522 f = zeros((m,n), Float) #Resulting columns 529 523 530 524 for i in range(z.shape[1]): 531 525 f[:,i] = self.fit(z[:,i]) 526 532 527 return f 533 528 … … 548 543 """ 549 544 550 try: 551 return self.A * f 552 except ValueError: 553 # We are here, so it probalby means that f is 2 dimensional 554 # and so will do each column separately due to problems in 555 # sparse matrix, 2d array multiplication 556 (N , M) = self.A.shape 557 f = array(f).astype(Float) 558 (m , n) = f.shape 559 #print "m",m 560 #print "M",M 561 if m != M : 562 #print "!!self.A",self.A 563 #print "!!self.A.todense() ",self.A.todense() 564 #print "self.get_A()",self.get_A() 565 #return dot(self.get_A(),f) 566 raise VectorShapeError, 'Mismatch between A and f dimensions' 567 568 y = zeros( (N,n), Float) 569 570 for i in range(n): 571 y[:,i] = self.A * f[:,i] 572 573 #print "!!self.A.todense() ",self.A.todense() 574 return y 545 return self.A * f 546 547 575 548 576 549 #FIXME: We will need a method 'evaluate(self):' that will interpolate … … 597 570 """ 598 571 import os, sys 599 usage = "usage: %s mesh_input.tsh point.xya mesh_output.tsh alpha" % os.path.basename(sys.argv[0]) 572 usage = "usage: %s mesh_input.tsh point.xya mesh_output.tsh alpha"\ 573 %os.path.basename(sys.argv[0]) 600 574 601 575 if len(sys.argv) < 4: -
inundation/ga/storm_surge/pyvolution/sparse.py
r474 r485 116 116 print 'FIXME: Only Numeric types implemented so far' 117 117 118 119 118 #Assume numeric types from now on 120 R = zeros((self.M,), Float) #Result 121 119 122 120 if len(B.shape) == 0: 123 121 #Scalar - use __rmul__ method 124 122 R = B*self 123 125 124 elif len(B.shape) == 1: 126 125 #Vector 127 126 assert B.shape[0] == self.N, 'Mismatching dimensions' 128 127 128 R = zeros(self.M, Float) #Result 129 129 130 #Multiply nonzero elements 130 131 for key in self.A.keys(): … … 133 134 R[i] += self.A[key]*B[j] 134 135 elif len(B.shape) == 2: 135 raise ValueError, 'Numeric matrix not yet implemented' 136 137 138 R = zeros((self.M, B.shape[1]), Float) #Result matrix 139 140 #Multiply nonzero elements 141 for col in range(R.shape[1]): 142 #For each column 143 144 for key in self.A.keys(): 145 i, j = key 146 147 R[i, col] += self.A[key]*B[j, col] 148 149 136 150 else: 137 151 raise ValueError, 'Dimension too high: d=%d' %len(B.shape) -
inundation/ga/storm_surge/pyvolution/test_least_squares.py
r484 r485 168 168 #print "answer\n",answer 169 169 170 assert allclose(f, answer, atol=1e-7)170 assert allclose(f, answer, atol=1e-7) 171 171 172 172 … … 200 200 201 201 vertices = [a, b, c, d,e,f] 202 triangles = [[0,1,3], [1,0,2],[0,4,5], [0,5,2]] #abd bac aef afc202 triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc 203 203 204 204 point_coords = [[-2.0, 2.0], … … 238 238 d1 = [1.0, 1.0] 239 239 d2 = [1.0, 3.0] 240 d3 = [3.0, 1.0]241 z1 = [2, 4]242 z2 = [4, 8]243 z3 = [4, 8]244 data_coords = [ 240 d3 = [3.0, 1.0] 241 z1 = [2, 4] 242 z2 = [4, 8] 243 z3 = [4, 8] 244 data_coords = [d1, d2, d3] 245 245 246 246 interp = Interpolation(points, triangles, data_coords, alpha=0.0) … … 260 260 d0 = [1.0, 1.0] 261 261 d1 = [1.0, 2.0] 262 d2 = [3.0, 1.0]262 d2 = [3.0, 1.0] 263 263 point_coords = [ d0, d1, d2] 264 264 … … 275 275 a = [-1.0, 0.0] 276 276 b = [3.0, 4.0] 277 c = [4.0, 1.0]277 c = [4.0, 1.0] 278 278 d = [-3.0, 2.0] #3 279 e = [-1.0, -2.0]279 e = [-1.0, -2.0] 280 280 f = [1.0, -2.0] #5 281 281 282 282 vertices = [a, b, c, d,e,f] 283 triangles = [[0,1,3], [1,0,2],[0,4,5], [0,5,2]] #abd bac aef afc283 triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc 284 284 285 285 286 286 point_coords = [[-2.0, 2.0], 287 287 [-1.0, 1.0], 288 [0.0, 2.0],288 [0.0, 2.0], 289 289 [1.0, 1.0], 290 290 [2.0, 1.0], 291 [0.0, 0.0],291 [0.0, 0.0], 292 292 [1.0, 0.0], 293 293 [0.0, -1.0], 294 [-0.2, -0.5],294 [-0.2, -0.5], 295 295 [-0.9, -1.5], 296 296 [0.5, -1.9], 297 [3.0, 1.0]]297 [3.0, 1.0]] 298 298 299 299 interp = Interpolation(vertices, triangles, point_coords) … … 335 335 a = [-1.0, 0.0] 336 336 b = [3.0, 4.0] 337 c = [4.0, 1.0]337 c = [4.0, 1.0] 338 338 d = [-3.0, 2.0] #3 339 e = [-1.0, -2.0]339 e = [-1.0, -2.0] 340 340 f = [1.0, -2.0] #5 341 341 342 342 vertices = [a, b, c, d,e,f] 343 triangles = [[0,1,3], [1,0,2],[0,4,5], [0,5,2]] #abd bac aef afc343 triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc 344 344 345 345 346 346 point_coords = [[-2.0, 2.0], 347 347 [-1.0, 1.0], 348 [0.0, 2.0],348 [0.0, 2.0], 349 349 [1.0, 1.0], 350 350 [2.0, 1.0], 351 [0.0, 0.0],351 [0.0, 0.0], 352 352 [1.0, 0.0], 353 353 [0.0, -1.0], 354 [-0.2, -0.5],354 [-0.2, -0.5], 355 355 [-0.9, -1.5], 356 356 [0.5, -1.9], 357 [3.0, 1.0]]357 [3.0, 1.0]] 358 358 359 359 interp = Interpolation(vertices, triangles, point_coords) … … 384 384 d1 = [1.0, 1.0] 385 385 d2 = [1.0, 3.0] 386 d3 = [3.0, 1.0]387 z1 = [2, 4]388 z2 = [4, 8]389 z3 = [4, 8]390 data_coords = [ 386 d3 = [3.0, 1.0] 387 z1 = [2, 4] 388 z2 = [4, 8] 389 z3 = [4, 8] 390 data_coords = [d1, d2, d3] 391 391 z = [z1, z2, z3] 392 392 -
inundation/ga/storm_surge/pyvolution/test_sparse.py
r479 r485 3 3 import unittest 4 4 from math import sqrt 5 6 5 7 6 from sparse import * … … 15 14 def tearDown(self): 16 15 pass 17 18 16 19 17 def test_init1(Self): … … 72 70 assert allclose(A.todense(), [[0,0,0], [0,0,0], [0,0,0]]) 73 71 74 def test_sparse_multiplication (self):72 def test_sparse_multiplication_vector(self): 75 73 A = Sparse(3,3) 76 74 … … 83 81 v = [2,3,4] 84 82 85 86 83 u = A*v 87 84 assert allclose(u, [6,14,4]) 88 89 90 85 91 86 #Right hand side column … … 97 92 u = A*v[:,1] 98 93 assert allclose(u, [12,16,4]) 94 95 96 def test_sparse_multiplication_matrix(self): 97 A = Sparse(3,3) 98 99 A[0,0] = 3 100 A[1,1] = 2 101 A[1,2] = 2 102 A[2,2] = 1 103 104 #Right hand side matrix 105 v = array([[2,4],[3,4],[4,4]]) 106 107 u = A*v 108 assert allclose(u, [[6,12], [14,16], [4,4]]) 109 99 110 100 111
Note: See TracChangeset
for help on using the changeset viewer.