Changeset 438 for inundation/ga/storm_surge/pyvolution
- Timestamp:
- Oct 21, 2004, 11:54:49 PM (20 years ago)
- Location:
- inundation/ga/storm_surge/pyvolution
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/ga/storm_surge/pyvolution/cg_solve.py
r435 r438 1 1 import exceptions 2 class VectorShapeError(exceptions.Exception): 3 """ 4 Setup Exception for routine conjugate_gradient 5 """ 6 def __init__(self,args=None): 7 self.args = args 8 9 10 2 class VectorShapeError(exceptions.Exception): pass 11 3 12 4 13 5 def conjugate_gradient(A,b,x0,imax=2000,tol=1.0e-8,iprint=0): 14 # 15 # Try to solve linear equation Ax = b using 16 # conjugate gradient method 17 # 18 # Input 19 # A: matrix or function which applies a matrix, assumed symmetric 20 # b: right hand side 21 # x0: inital guess 22 # imax: max number of iterations 23 # tol: tolerance used for residual 24 # 25 # Output 26 # x: approximate solution 27 # i: number of iterations 28 # 6 """ 7 Try to solve linear equation Ax = b using 8 conjugate gradient method 9 10 Input 11 A: matrix or function which applies a matrix, assumed symmetric 12 b: right hand side 13 x0: inital guess 14 imax: max number of iterations 15 tol: tolerance used for residual 16 17 Output 18 x: approximate solution 19 """ 29 20 30 21 #if nargin<3 … … 37 28 b = asarray(b, typecode=Float) 38 29 x0 = asarray(x0, typecode=Float) 30 #A = asarray(A, typecode=Float) 39 31 40 print "A shape",A.shape[0]41 print "b shape",len(b.shape)42 print "x0 shape",x0.shape[0]32 #print "A shape",A.shape 33 #print "b shape",len(b.shape) 34 #print "x0 shape",x0.shape[0] 43 35 44 36 if len(b.shape) != 1 : -
inundation/ga/storm_surge/pyvolution/test_cg_solve.py
r435 r438 2 2 3 3 import unittest 4 #from math import sqrt5 4 6 from Numeric import allclose, array, transpose, arange, ones, Float 5 from Matrix import * 6 from Numeric import dot, allclose, array, transpose, arange, ones, Float 7 from cg_solve import * 8 from scipy import sparse, mat 7 9 8 from cg_solve import *9 10 10 from scipy import sparse11 12 11 class TestCase(unittest.TestCase): 13 12 14 def setUp(self):15 pass 13 ## def test_solve(self): 14 ## """Small Matrix""" 16 15 17 def tearDown(self): 18 pass 16 ## A = [[2.0, -1.0, 0.0, 0.0 ], 17 ## [-1.0, 2.0, -1.0, 0.0], 18 ## [0.0, -1.0, 2.0, -1.0], 19 ## [0.0, 0.0, -1.0, 2.0]] 19 20 21 ## A = mat(A) 20 22 21 def test_solve(self): 23 ## xe = asarray([0.0, 1.0, 2.0, 3.0]) 24 ## b = A*xe 25 ## x = asarray([0.0, 0.0, 0.0, 0.0]) 26 27 ## print A 28 ## print xe 29 ## print b 30 ## print b.shape 31 32 ## x = conjugate_gradient(A,b,x,iprint=1) 33 34 ## assert allclose(x,xe) 35 36 def test_sparse_solve(self): 37 """Small Sparse Matrix""" 38 22 39 A = [[2.0, -1.0, 0.0, 0.0 ], 23 40 [-1.0, 2.0, -1.0, 0.0], … … 27 44 A = sparse.dok_matrix(A) 28 45 29 print "A"30 print A31 32 46 xe = [0.0, 1.0, 2.0, 3.0] 33 47 b = A*xe 34 48 x = [0.0, 0.0, 0.0, 0.0] 35 49 36 x = conjugate_gradient(A,b,x,iprint=1) 37 38 print "x",x 50 x = conjugate_gradient(A,b,x,iprint=0) 39 51 40 52 assert allclose(x,xe) … … 42 54 43 55 def test_solve_large(self): 56 """Standard 1d laplacian """ 57 44 58 A = sparse.dok_matrix() 45 59 … … 52 66 A[i,i+1] = -0.5 53 67 54 # print "\n"55 # print "A\n"56 # print A57 58 59 68 xe = ones( (n,), Float) 60 69 61 # print xe62 70 b = A*xe 63 64 x = conjugate_gradient(A,b,b,tol=1.0e-5,iprint=10) 65 66 # print "x",x 71 x = conjugate_gradient(A,b,b,tol=1.0e-5,iprint=0) 67 72 68 73 assert allclose(x,xe) 69 74 70 75 def test_solve_large_2d(self): 76 """Standard 2d laplacian""" 77 71 78 A = sparse.dok_matrix() 72 79 73 n = 3074 m = 3080 n = 20 81 m = 10 75 82 for i in arange(0,n): 76 83 for j in arange(0,m): … … 86 93 A[I,I+1] = -1.0 87 94 88 # print "\n"89 # print "A\n"90 # print A.todense()91 92 93 95 xe = ones( (n*m,), Float) 94 96 95 # print xe96 97 b = A*xe 97 98 x = conjugate_gradient(A,b,b,iprint=10) 99 100 # print "x",x 98 x = conjugate_gradient(A,b,b,iprint=0) 101 99 102 100 assert allclose(x,xe) 101 102 103 def test_vector_shape_error(self): 104 """Raise VectorShapeError""" 105 106 A = [[2.0, -1.0, 0.0, 0.0 ], 107 [-1.0, 2.0, -1.0, 0.0], 108 [0.0, -1.0, 2.0, -1.0], 109 [0.0,0.0, -1.0, 2.0]] 110 111 A = sparse.dok_matrix(A) 112 113 xe = [[0.0,2.0], [1.0,3.0], [2.0,4.0], [3.0,2.0]] 114 b = A*xe 115 x = [0.0, 0.0, 0.0, 0.0] 116 117 try: 118 x = conjugate_gradient(A,b,b,iprint=0) 119 except: 120 pass 121 else: 122 msg = 'Should have raised exception' 123 raise msg 103 124 104 125 105 126 #------------------------------------------------------------- 106 127 if __name__ == "__main__": 107 suite = unittest.makeSuite(TestCase,'test')108 runner = unittest.TextTestRunner()109 runner.run(suite)128 suite = unittest.makeSuite(TestCase,'test') 129 runner = unittest.TextTestRunner(verbosity=2) 130 runner.run(suite) 110 131 111 132
Note: See TracChangeset
for help on using the changeset viewer.