[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 * |
---|
[475] | 8 | from sparse import Sparse |
---|
[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 | |
---|
| 80 | |
---|
[475] | 81 | def test_solve_large_2d_with_default_guess(self): |
---|
| 82 | """Standard 2d laplacian using default first guess""" |
---|
| 83 | |
---|
| 84 | n = 20 |
---|
| 85 | m = 10 |
---|
| 86 | |
---|
| 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 | b = A*xe |
---|
| 105 | x = conjugate_gradient(A,b) |
---|
| 106 | |
---|
| 107 | assert allclose(x,xe) |
---|
| 108 | |
---|
| 109 | |
---|
[438] | 110 | def test_vector_shape_error(self): |
---|
| 111 | """Raise VectorShapeError""" |
---|
| 112 | |
---|
| 113 | A = [[2.0, -1.0, 0.0, 0.0 ], |
---|
| 114 | [-1.0, 2.0, -1.0, 0.0], |
---|
| 115 | [0.0, -1.0, 2.0, -1.0], |
---|
| 116 | [0.0,0.0, -1.0, 2.0]] |
---|
| 117 | |
---|
[475] | 118 | A = Sparse(A) |
---|
[435] | 119 | |
---|
[438] | 120 | xe = [[0.0,2.0], [1.0,3.0], [2.0,4.0], [3.0,2.0]] |
---|
| 121 | |
---|
| 122 | try: |
---|
[475] | 123 | x = conjugate_gradient(A,xe,xe,iprint=0) |
---|
[438] | 124 | except: |
---|
| 125 | pass |
---|
| 126 | else: |
---|
| 127 | msg = 'Should have raised exception' |
---|
| 128 | raise msg |
---|
| 129 | |
---|
[435] | 130 | |
---|
[432] | 131 | #------------------------------------------------------------- |
---|
| 132 | if __name__ == "__main__": |
---|
[438] | 133 | suite = unittest.makeSuite(TestCase,'test') |
---|
[454] | 134 | runner = unittest.TextTestRunner() #(verbosity=2) |
---|
[438] | 135 | runner.run(suite) |
---|
[432] | 136 | |
---|
| 137 | |
---|
| 138 | |
---|
| 139 | |
---|
| 140 | |
---|
| 141 | |
---|