File: test_superlu.py

package info (click to toggle)
python-scipy 0.5.2-0.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 33,888 kB
  • ctags: 44,231
  • sloc: ansic: 156,256; cpp: 90,347; python: 89,604; fortran: 73,083; sh: 1,318; objc: 424; makefile: 342
file content (192 lines) | stat: -rw-r--r-- 6,963 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import math, unittest
import scipy
import spmatrix, superlu
import poisson

def residual(A, x, b):
    n = A.shape[0]
    r = numpy.zeros(n, 'd')
    A.matvec(x, r)
    r -= b
    return math.sqrt(numpy.dot(r, r))

def error(x, y):
    n = len(x)
    t = x.copy(); t -= y
    return math.sqrt(numpy.dot(t, t))

def speye(n):
    A = spmatrix.ll_mat_sym(n, n)
    for i in xrange(n):
        A[i,i] = 1.0
    return A

def macheps():
    "compute machine epsilon"
    eps = 1.0
    while (1.0 + eps > 1.0):
        eps /= 2.0
    return 2.0 * eps

class EyeTestCase(unittest.TestCase):
    def setUp(self):
        self.n = 10000
        self.A = speye(self.n).to_csr()
        self.b = numpy.ones(self.n, 'd')
        self.x = numpy.zeros(self.n, 'd')

    def testTrivial(self):
        luA = superlu.factorize(self.A)
        luA.solve(self.b, self.x)
        self.failUnless(residual(self.A, self.x, self.b) == 0.0)

        luA = superlu.factorize(self.A, diag_pivot_thresh=0.0)
        luA.solve(self.b, self.x)
        self.failUnless(residual(self.A, self.x, self.b) == 0.0)

        luA = superlu.factorize(self.A, relax=20)
        luA.solve(self.b, self.x)
        self.failUnless(residual(self.A, self.x, self.b) == 0.0)

        luA = superlu.factorize(self.A, panel_size=1)
        luA.solve(self.b, self.x)
        self.failUnless(residual(self.A, self.x, self.b) == 0.0)

        for permc_spec in [0, 1, 2, 3]:
            luA = superlu.factorize(self.A, permc_spec=permc_spec)
            luA.solve(self.b, self.x)
            self.failUnless(residual(self.A, self.x, self.b) == 0.0)

class Poisson1dTestCase(unittest.TestCase):
    def setUp(self):
        self.n = 50000
        self.B = poisson.poisson1d(self.n).to_csr()

        self.b = numpy.zeros(self.n, 'd')
        self.x = numpy.zeros(self.n, 'd')
        self.x_exact = numpy.ones(self.n, 'd')
        self.x_exact /= math.sqrt(self.n)
        self.B.matvec(self.x_exact, self.b)

        lmbd_min = 4.0 * math.sin(math.pi/2.0/self.n) ** 2
        lmbd_max = 4.0 * math.sin((self.n - 1)*math.pi/2.0/self.n) ** 2
        cond = lmbd_max/lmbd_min
        self.tol = cond * macheps()

    def testPoisson1dDefault(self):
        luA = superlu.factorize(self.B)
        luA.solve(self.b, self.x)
        print error(self.x, self.x_exact), self.tol, luA.nnz
        self.failUnless(error(self.x, self.x_exact) < self.tol)

    def testPoisson1dNoPivot(self):
        luA = superlu.factorize(self.B, diag_pivot_thresh=0.0)
        luA.solve(self.b, self.x)
        print error(self.x, self.x_exact), self.tol, luA.nnz
        self.failUnless(error(self.x, self.x_exact) < self.tol)

    def testPoisson1dRelax(self):
        luA = superlu.factorize(self.B, relax=20)
        luA.solve(self.b, self.x)
        print error(self.x, self.x_exact), self.tol, luA.nnz
        self.failUnless(error(self.x, self.x_exact) < self.tol)

    def testPoisson1dSmallPanel(self):
        luA = superlu.factorize(self.B, panel_size=1)
        luA.solve(self.b, self.x)
        print error(self.x, self.x_exact), self.tol, luA.nnz
        self.failUnless(error(self.x, self.x_exact) < self.tol)

    def testPoisson1dOrigOrdering(self):
        luA = superlu.factorize(self.B, permc_spec=0)
        luA.solve(self.b, self.x)
        print error(self.x, self.x_exact), self.tol, luA.nnz
        self.failUnless(error(self.x, self.x_exact) < self.tol)

    def testPoisson1dMMD_AtimesA(self):
        luA = superlu.factorize(self.B, permc_spec=1)
        luA.solve(self.b, self.x)
        print error(self.x, self.x_exact), self.tol, luA.nnz
        self.failUnless(error(self.x, self.x_exact) < self.tol)

    def testPoisson1dMMD_AplusA(self):
        luA = superlu.factorize(self.B, permc_spec=2)
        luA.solve(self.b, self.x)
        print error(self.x, self.x_exact), self.tol, luA.nnz
        self.failUnless(error(self.x, self.x_exact) < self.tol)

    def testPoisson1dCOLAMD(self):
        luA = superlu.factorize(self.B, permc_spec=3)
        luA.solve(self.b, self.x)
        print error(self.x, self.x_exact), self.tol, luA.nnz
        self.failUnless(error(self.x, self.x_exact) < self.tol)

class Poisson2dTestCase(unittest.TestCase):
    def setUp(self):
        self.n = 200
        self.B = poisson.poisson2d(self.n).to_csr()

        self.b = numpy.zeros(self.n*self.n, 'd')
        self.x = numpy.zeros(self.n*self.n, 'd')
        self.x_exact = numpy.ones(self.n*self.n, 'd')
        self.x_exact /= math.sqrt(self.n*self.n)
        self.B.matvec(self.x_exact, self.b)

        h = 1.0 / self.n
        lmbd_min = 4.0/h/h * (math.sin(math.pi*h/2.0) ** 2 +
                              math.sin(math.pi*h/2.0) ** 2)
        lmbd_max = 4.0/h/h * (math.sin((self.n - 1)*math.pi*h/2.0) ** 2 +
                              math.sin((self.n - 1)*math.pi*h/2.0) ** 2)
        cond = lmbd_max/lmbd_min
        self.tol = cond * macheps()

    def testPoisson2dDefault(self):
        luA = superlu.factorize(self.B)
        luA.solve(self.b, self.x)
        print error(self.x, self.x_exact), self.tol, luA.nnz
        self.failUnless(error(self.x, self.x_exact) < self.tol)

    def testPoisson2dNoPivot(self):
        luA = superlu.factorize(self.B, diag_pivot_thresh=0.0)
        luA.solve(self.b, self.x)
        print error(self.x, self.x_exact), self.tol, luA.nnz
        self.failUnless(error(self.x, self.x_exact) < self.tol)

    def testPoisson2dRelax(self):
        luA = superlu.factorize(self.B, relax=20)
        luA.solve(self.b, self.x)
        print error(self.x, self.x_exact), self.tol, luA.nnz
        self.failUnless(error(self.x, self.x_exact) < self.tol)

    def testPoisson2dSmallPanel(self):
        luA = superlu.factorize(self.B, panel_size=1)
        luA.solve(self.b, self.x)
        print error(self.x, self.x_exact), self.tol, luA.nnz
        self.failUnless(error(self.x, self.x_exact) < self.tol)

    def testPoisson2dOrigOrdering(self):
        luA = superlu.factorize(self.B, permc_spec=0)
        luA.solve(self.b, self.x)
        print error(self.x, self.x_exact), self.tol, luA.nnz
        self.failUnless(error(self.x, self.x_exact) < self.tol)

    def testPoisson2dMMD_AtimesA(self):
        luA = superlu.factorize(self.B, permc_spec=1)
        luA.solve(self.b, self.x)
        print error(self.x, self.x_exact), self.tol, luA.nnz
        self.failUnless(error(self.x, self.x_exact) < self.tol)

    def testPoisson2dMMD_AplusA(self):
        luA = superlu.factorize(self.B, permc_spec=2)
        luA.solve(self.b, self.x)
        print error(self.x, self.x_exact), self.tol, luA.nnz
        self.failUnless(error(self.x, self.x_exact) < self.tol)

    def testPoisson2dCOLAMD(self):
        luA = superlu.factorize(self.B, permc_spec=3)
        luA.solve(self.b, self.x)
        print error(self.x, self.x_exact), self.tol, luA.nnz
        self.failUnless(error(self.x, self.x_exact) < self.tol)

if __name__ == '__main__':
    unittest.main()