/usr/share/pyshared/SparsExamples/poisson_gmres.py is in python-sparse-examples 1.1-1.3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | import Numeric
import math
from pysparse import spmatrix
from pysparse import itsolvers
from pysparse import precon
import time
def poisson2d(n):
L = spmatrix.ll_mat(n*n, n*n)
for i in range(n):
for j in range(n):
k = i + n*j
L[k,k] = 4
if i > 0:
L[k,k-1] = -1
if i < n-1:
L[k,k+1] = -1
if j > 0:
L[k,k-n] = -1
if j < n-1:
L[k,k+n] = -1
return L
def poisson2d_sym(n):
L = spmatrix.ll_mat_sym(n*n)
for i in range(n):
for j in range(n):
k = i + n*j
L[k,k] = 4
if i > 0:
L[k,k-1] = -1
if j > 0:
L[k,k-n] = -1
return L
def poisson2d_sym_blk(n):
L = spmatrix.ll_mat_sym(n*n)
I = spmatrix.ll_mat_sym(n)
P = spmatrix.ll_mat_sym(n)
for i in range(n):
I[i,i] = -1
for i in range(n):
P[i,i] = 4
if i > 0: P[i,i-1] = -1
for i in range(0, n*n, n):
L[i:i+n,i:i+n] = P
if i > 0: L[i:i+n,i-n:i] = I
return L
n = 50
t1 = time.clock()
L = poisson2d(n)
print 'Time for constructing the matrix: %8.2f sec' % (time.clock() - t1, )
#L.export_mtx('poi2d_100.mtx')
A = L.to_csr()
S = L.to_sss()
print L.nnz
print S.nnz
print A.nnz
b = Numeric.ones(n*n, 'd')
e = Numeric.ones(n*n, 'd')
c = Numeric.ones(n*n, 'd')
for loop in xrange(n*n):
b[loop]= loop
c[loop] = loop
y = Numeric.ones(n*n, 'd')
S.matvec(b,y)
b = y
#print b
# ---------------------------------------------------------------------------------------
t1 = time.clock()
x = Numeric.zeros(n*n, 'd')
info, iter, relres = itsolvers.gmres(S, b, x, 1e-12, 200, None, 100)
print 'info=%d, iter=%d, relres=%e' % (info, iter, relres)
print 'Time for solving the system using SSS matrix: %8.2f sec' % (time.clock() - t1, )
print 'norm(x) = %g' % math.sqrt(Numeric.dot(x, x))
r = Numeric.zeros(n*n, 'd')
S.matvec(x, r)
r = b - r
print 'norm(b - A*x) = %g' % math.sqrt(Numeric.dot(r, r))
# ---------------------------------------------------------------------------------------
t1 = time.clock()
x = Numeric.zeros(n*n, 'd')
info, iter, relres = itsolvers.gmres(A, b, x, 1e-12, 200)
print 'info=%d, iter=%d, relres=%e' % (info, iter, relres)
print 'Time for solving the system using CSR matrix: %8.2f sec' % (time.clock() - t1, )
print 'norm(x) = %g' % math.sqrt(Numeric.dot(x, x))
r = Numeric.zeros(n*n, 'd')
A.matvec(x, r)
r = b - r
print 'norm(b - A*x) = %g' % math.sqrt(Numeric.dot(r, r))
# ---------------------------------------------------------------------------------------
t1 = time.clock()
x = Numeric.zeros(n*n, 'd')
info, iter, relres = itsolvers.gmres(L, b, x, 1e-12, 200)
print 'info=%d, iter=%d, relres=%e' % (info, iter, relres)
print 'Time for solving the system using LL matrix: %8.2f sec' % (time.clock() - t1, )
print 'norm(x) = %g' % math.sqrt(Numeric.dot(x, x))
r = Numeric.zeros(n*n, 'd')
A.matvec(x, r)
r = b - r
print 'norm(b - A*x) = %g' % math.sqrt(Numeric.dot(r, r))
# ---------------------------------------------------------------------------------------
K_ssor = precon.ssor(S, 1.0)
t1 = time.clock()
x = Numeric.zeros(n*n, 'd')
info, iter, relres = itsolvers.gmres(S, b, x, 1e-12, 500, K_ssor, 20)
print 'info=%d, iter=%d, relres=%e' % (info, iter, relres)
print 'Time for solving the system using SSS matrix and SSOR preconditioner: %8.2f sec' % (time.clock() - t1, )
print 'norm(x) = %g' % math.sqrt(Numeric.dot(x, x))
r = Numeric.zeros(n*n, 'd')
S.matvec(x, r)
r = b - r
print 'norm(b - A*x) = %g' % math.sqrt(Numeric.dot(r, r))
# ---------------------------------------------------------------------------------------
#import jdsym
#jdsym.jdsym(S, None, None, 5, 0.0, 1e-8, 20, itsolvers.qmrs, clvl=1)
x = Numeric.zeros(n*n, 'd')
info, iter, relres = itsolvers.gmres(S, b, x, 1e-15, 500, K_ssor, 50)
print 'info=%d, iter=%d, relres=%e' % (info, iter, relres)
print 'Time for solving the system using SSS matrix and SSOR preconditioner: %8.2f sec' % (time.clock() - t1, )
print 'norm(x) = %g' % math.sqrt(Numeric.dot(x, x))
r = Numeric.zeros(n*n, 'd')
S.matvec(x, r)
r = b - r
print 'norm(b - A*x) = %g' % math.sqrt(Numeric.dot(r, r))
print 'bye'
|