[432] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
| 3 | import unittest |
---|
| 4 | |
---|
[475] | 5 | |
---|
[438] | 6 | from Numeric import dot, allclose, array, transpose, arange, ones, Float |
---|
[432] | 7 | from cg_solve import * |
---|
[586] | 8 | from sparse import Sparse, Sparse_CSR |
---|
[432] | 9 | |
---|
[438] | 10 | |
---|
[432] | 11 | class TestCase(unittest.TestCase): |
---|
| 12 | |
---|
[438] | 13 | def test_sparse_solve(self): |
---|
| 14 | """Small Sparse Matrix""" |
---|
| 15 | |
---|
[432] | 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]] |
---|
| 20 | |
---|
[475] | 21 | A = Sparse(A) |
---|
[435] | 22 | |
---|
[432] | 23 | xe = [0.0, 1.0, 2.0, 3.0] |
---|
| 24 | b = A*xe |
---|
| 25 | x = [0.0, 0.0, 0.0, 0.0] |
---|
| 26 | |
---|
[438] | 27 | x = conjugate_gradient(A,b,x,iprint=0) |
---|
[432] | 28 | |
---|
| 29 | assert allclose(x,xe) |
---|
| 30 | |
---|
| 31 | |
---|
[435] | 32 | def test_solve_large(self): |
---|
[438] | 33 | """Standard 1d laplacian """ |
---|
| 34 | |
---|
[435] | 35 | n = 50 |
---|
[475] | 36 | A = Sparse(n,n) |
---|
| 37 | |
---|
[435] | 38 | for i in arange(0,n): |
---|
| 39 | A[i,i] = 1.0 |
---|
| 40 | if i > 0 : |
---|
| 41 | A[i,i-1] = -0.5 |
---|
| 42 | if i < n-1 : |
---|
| 43 | A[i,i+1] = -0.5 |
---|
| 44 | |
---|
| 45 | xe = ones( (n,), Float) |
---|
| 46 | |
---|
[432] | 47 | b = A*xe |
---|
[438] | 48 | x = conjugate_gradient(A,b,b,tol=1.0e-5,iprint=0) |
---|
[432] | 49 | |
---|
| 50 | assert allclose(x,xe) |
---|
| 51 | |
---|
[435] | 52 | def test_solve_large_2d(self): |
---|
[438] | 53 | """Standard 2d laplacian""" |
---|
| 54 | |
---|
| 55 | n = 20 |
---|
| 56 | m = 10 |
---|
[475] | 57 | |
---|
| 58 | A = Sparse(m*n, m*n) |
---|
| 59 | |
---|
[435] | 60 | for i in arange(0,n): |
---|
| 61 | for j in arange(0,m): |
---|
| 62 | I = j+m*i |
---|
| 63 | A[I,I] = 4.0 |
---|
| 64 | if i > 0 : |
---|
| 65 | A[I,I-m] = -1.0 |
---|
| 66 | if i < n-1 : |
---|
| 67 | A[I,I+m] = -1.0 |
---|
| 68 | if j > 0 : |
---|
| 69 | A[I,I-1] = -1.0 |
---|
| 70 | if j < m-1 : |
---|
| 71 | A[I,I+1] = -1.0 |
---|
| 72 | |
---|
| 73 | xe = ones( (n*m,), Float) |
---|
| 74 | |
---|
| 75 | b = A*xe |
---|
[438] | 76 | x = conjugate_gradient(A,b,b,iprint=0) |
---|
[435] | 77 | |
---|
[438] | 78 | assert allclose(x,xe) |
---|
[435] | 79 | |
---|
[586] | 80 | def test_solve_large_2d_csr_matrix(self): |
---|
[596] | 81 | """Standard 2d laplacian with csr format |
---|
| 82 | """ |
---|
[586] | 83 | |
---|
[594] | 84 | n = 100 |
---|
| 85 | m = 100 |
---|
[435] | 86 | |
---|
[586] | 87 | A = Sparse(m*n, m*n) |
---|
| 88 | |
---|
| 89 | for i in arange(0,n): |
---|
| 90 | for j in arange(0,m): |
---|
| 91 | I = j+m*i |
---|
| 92 | A[I,I] = 4.0 |
---|
| 93 | if i > 0 : |
---|
| 94 | A[I,I-m] = -1.0 |
---|
| 95 | if i < n-1 : |
---|
| 96 | A[I,I+m] = -1.0 |
---|
| 97 | if j > 0 : |
---|
| 98 | A[I,I-1] = -1.0 |
---|
| 99 | if j < m-1 : |
---|
| 100 | A[I,I+1] = -1.0 |
---|
| 101 | |
---|
| 102 | xe = ones( (n*m,), Float) |
---|
| 103 | |
---|
| 104 | # Convert to csr format |
---|
[599] | 105 | #print 'start covert' |
---|
[586] | 106 | A = Sparse_CSR(A) |
---|
[599] | 107 | #print 'finish covert' |
---|
[586] | 108 | b = A*xe |
---|
[594] | 109 | x = conjugate_gradient(A,b,b,iprint=20) |
---|
[586] | 110 | |
---|
| 111 | assert allclose(x,xe) |
---|
| 112 | |
---|
| 113 | |
---|
[475] | 114 | def test_solve_large_2d_with_default_guess(self): |
---|
| 115 | """Standard 2d laplacian using default first guess""" |
---|
| 116 | |
---|
| 117 | n = 20 |
---|
| 118 | m = 10 |
---|
| 119 | |
---|
| 120 | A = Sparse(m*n, m*n) |
---|
| 121 | |
---|
| 122 | for i in arange(0,n): |
---|
| 123 | for j in arange(0,m): |
---|
| 124 | I = j+m*i |
---|
| 125 | A[I,I] = 4.0 |
---|
| 126 | if i > 0 : |
---|
| 127 | A[I,I-m] = -1.0 |
---|
| 128 | if i < n-1 : |
---|
| 129 | A[I,I+m] = -1.0 |
---|
| 130 | if j > 0 : |
---|
| 131 | A[I,I-1] = -1.0 |
---|
| 132 | if j < m-1 : |
---|
| 133 | A[I,I+1] = -1.0 |
---|
| 134 | |
---|
| 135 | xe = ones( (n*m,), Float) |
---|
| 136 | |
---|
| 137 | b = A*xe |
---|
| 138 | x = conjugate_gradient(A,b) |
---|
| 139 | |
---|
| 140 | assert allclose(x,xe) |
---|
| 141 | |
---|
| 142 | |
---|
[438] | 143 | def test_vector_shape_error(self): |
---|
| 144 | """Raise VectorShapeError""" |
---|
| 145 | |
---|
| 146 | A = [[2.0, -1.0, 0.0, 0.0 ], |
---|
| 147 | [-1.0, 2.0, -1.0, 0.0], |
---|
| 148 | [0.0, -1.0, 2.0, -1.0], |
---|
| 149 | [0.0,0.0, -1.0, 2.0]] |
---|
| 150 | |
---|
[475] | 151 | A = Sparse(A) |
---|
[435] | 152 | |
---|
[438] | 153 | xe = [[0.0,2.0], [1.0,3.0], [2.0,4.0], [3.0,2.0]] |
---|
| 154 | |
---|
| 155 | try: |
---|
[475] | 156 | x = conjugate_gradient(A,xe,xe,iprint=0) |
---|
[438] | 157 | except: |
---|
| 158 | pass |
---|
| 159 | else: |
---|
| 160 | msg = 'Should have raised exception' |
---|
| 161 | raise msg |
---|
| 162 | |
---|
[435] | 163 | |
---|
[432] | 164 | #------------------------------------------------------------- |
---|
| 165 | if __name__ == "__main__": |
---|
[438] | 166 | suite = unittest.makeSuite(TestCase,'test') |
---|
[454] | 167 | runner = unittest.TextTestRunner() #(verbosity=2) |
---|
[438] | 168 | runner.run(suite) |
---|
[432] | 169 | |
---|
| 170 | |
---|
| 171 | |
---|
| 172 | |
---|
| 173 | |
---|
| 174 | |
---|