1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | import unittest |
---|
4 | |
---|
5 | |
---|
6 | from Numeric import dot, allclose, array, transpose, arange, ones, Float |
---|
7 | from cg_solve import * |
---|
8 | from sparse import Sparse, Sparse_CSR |
---|
9 | |
---|
10 | |
---|
11 | class TestCase(unittest.TestCase): |
---|
12 | |
---|
13 | def test_sparse_solve(self): |
---|
14 | """Small Sparse Matrix""" |
---|
15 | |
---|
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 | |
---|
21 | A = Sparse(A) |
---|
22 | |
---|
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 | |
---|
27 | x = conjugate_gradient(A,b,x,iprint=0) |
---|
28 | |
---|
29 | assert allclose(x,xe) |
---|
30 | |
---|
31 | |
---|
32 | def test_solve_large(self): |
---|
33 | """Standard 1d laplacian """ |
---|
34 | |
---|
35 | n = 50 |
---|
36 | A = Sparse(n,n) |
---|
37 | |
---|
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 | |
---|
47 | b = A*xe |
---|
48 | x = conjugate_gradient(A,b,b,tol=1.0e-5,iprint=0) |
---|
49 | |
---|
50 | assert allclose(x,xe) |
---|
51 | |
---|
52 | def test_solve_large_2d(self): |
---|
53 | """Standard 2d laplacian""" |
---|
54 | |
---|
55 | n = 20 |
---|
56 | m = 10 |
---|
57 | |
---|
58 | A = Sparse(m*n, m*n) |
---|
59 | |
---|
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 |
---|
76 | x = conjugate_gradient(A,b,b,iprint=0) |
---|
77 | |
---|
78 | assert allclose(x,xe) |
---|
79 | |
---|
80 | def test_solve_large_2d_csr_matrix(self): |
---|
81 | """Standard 2d laplacian with csr format |
---|
82 | """ |
---|
83 | |
---|
84 | n = 100 |
---|
85 | m = 100 |
---|
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 | # Convert to csr format |
---|
105 | #print 'start covert' |
---|
106 | A = Sparse_CSR(A) |
---|
107 | #print 'finish covert' |
---|
108 | b = A*xe |
---|
109 | x = conjugate_gradient(A,b,b,iprint=20) |
---|
110 | |
---|
111 | assert allclose(x,xe) |
---|
112 | |
---|
113 | |
---|
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 | |
---|
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 | |
---|
151 | A = Sparse(A) |
---|
152 | |
---|
153 | xe = [[0.0,2.0], [1.0,3.0], [2.0,4.0], [3.0,2.0]] |
---|
154 | |
---|
155 | try: |
---|
156 | x = conjugate_gradient(A,xe,xe,iprint=0) |
---|
157 | except: |
---|
158 | pass |
---|
159 | else: |
---|
160 | msg = 'Should have raised exception' |
---|
161 | raise msg |
---|
162 | |
---|
163 | |
---|
164 | #------------------------------------------------------------- |
---|
165 | if __name__ == "__main__": |
---|
166 | suite = unittest.makeSuite(TestCase,'test') |
---|
167 | runner = unittest.TextTestRunner() #(verbosity=2) |
---|
168 | runner.run(suite) |
---|
169 | |
---|
170 | |
---|
171 | |
---|
172 | |
---|
173 | |
---|
174 | |
---|