print "Time for constructing the matrix using poisson2d : %8.2f sec" % (time.clock() - t1,) A = L.to_csr() S = L.to_sss() print L.nnz print S.nnz print A.nnz b = np.ones(n * n, "d") # ----------------------------------------------------------------------------- t1 = time.clock() x = np.empty(n * n, "d") info, iter, relres = pcg(S, b, x, tol, 2000) print "info=%d, iter=%d, relres=%e" % (info, iter, relres) print "Solve time using SSS matrix: %8.2f s" % (time.clock() - t1) print "norm(x) = %g" % np.linalg.norm(x) r = np.empty(n * n, "d") S.matvec(x, r) r = b - r print "norm(b - A*x) = %g" % np.linalg.norm(r) print x[0:10] # -----------------------------------------------------------------------------
L = poisson2d_vec_sym_blk(N) S = L.to_sss() b = np.ones(N*N, 'd') print 'Solving 2D-Laplace equation using PCG and SSOR preconditioner with variable omega' print print 'omega nit time resid' print '--------------------------------' for omega in [0.1*(i+1) for i in range(20)]: K_ssor = precon.ssor(S, omega, SSOR_STEPS) t1 = time.clock() x = np.empty(N*N, 'd') info, iter, relres = pcg(S, b, x, TOL, MAXIT, K_ssor) elapsed_time = time.clock() - t1 r = np.zeros(N*N, 'd') S.matvec(x, r) r = b - r res_nrm2 = np.linalg.norm(r) if info == 0: iter_str = str(iter) else: iter_str = '---' print '%5.1f %5s %6.2f %10.3e' % (omega, iter_str, elapsed_time, res_nrm2)
A.matvec(x, y) print y print 'norm(y) = ', math.sqrt(np.add.reduce(y)) ##A.matvec_transp(x, z) ##print z ##print 'norm(z) = ', math.sqrt(np.add.reduce(z)) L = spmatrix.ll_mat(10, 10) for i in range(10): L[i, i] = float(i + 1) A = L.to_csr() print A x = np.zeros(10, 'd') b = np.ones(10, 'd') info, iter, relres = pcg(A, b, x, 1e-8, 100) print info, iter, relres print x if (info != 0): print >> sys.stderr, 'cg not converged' L2 = L.copy() x = np.zeros(10, 'd') info, iter, relres = pcg(A, b, x, 1e-8, 100) print info, iter, relres # ----------------------------------------------------------- print 'remove test' n = 100 L = spmatrix.ll_mat(n, n)
def pcg(*args, **kwargs): return krylov.pcg(*args, **kwargs)
def pcg(*args, **kwargs): return krylov.pcg(*args, **kwargs)
print 'Time for constructing the matrix using poisson2d : %8.2f sec' % ( time.clock() - t1, ) A = L.to_csr() S = L.to_sss() print L.nnz print S.nnz print A.nnz b = np.ones(n * n, 'd') # ----------------------------------------------------------------------------- t1 = time.clock() x = np.empty(n * n, 'd') info, iter, relres = pcg(S, b, x, tol, 2000) print 'info=%d, iter=%d, relres=%e' % (info, iter, relres) print 'Solve time using SSS matrix: %8.2f s' % (time.clock() - t1) print 'norm(x) = %g' % np.linalg.norm(x) r = np.empty(n * n, 'd') S.matvec(x, r) r = b - r print 'norm(b - A*x) = %g' % np.linalg.norm(r) print x[0:10] # -----------------------------------------------------------------------------
A.matvec(x, y) print y print 'norm(y) = ', math.sqrt(np.add.reduce(y)) ##A.matvec_transp(x, z) ##print z ##print 'norm(z) = ', math.sqrt(np.add.reduce(z)) L = spmatrix.ll_mat(10,10) for i in range(10): L[i,i] = float(i+1) A = L.to_csr() print A x = np.zeros(10, 'd') b = np.ones(10, 'd') info, iter, relres = pcg(A, b, x, 1e-8, 100) print info, iter, relres print x if (info != 0): print >> sys.stderr, 'cg not converged' L2 = L.copy() x = np.zeros(10, 'd') info, iter, relres = pcg(A, b, x, 1e-8, 100) print info, iter, relres # ----------------------------------------------------------- print 'remove test' n = 100 L = spmatrix.ll_mat(n, n)
def test_pcg(ProblemList, tol=1.0e-6): if len(ProblemList) == 0: usage() sys.exit(1) header1 = '%10s %6s %6s ' % ('Name', 'n', 'nnz') header2 = '%6s %8s %8s %4s %6s %6s\n' % ('iter','relres','error','info','form M','solve') dheader1 = '%10s %6d %6d ' dheader2 = '%6d %8.1e %8.1e %4d %6.2f %6.2f\n' lhead1 = len(header1) lhead2 = len(header2) lhead = lhead1 + lhead2 sys.stderr.write('-' * lhead + '\n') sys.stderr.write(header1) sys.stderr.write(header2) sys.stderr.write('-' * lhead + '\n') # Record timings for each preconditioner timings = { 'None' : [], 'Diagonal' : [], 'SSOR' : [] } for problem in ProblemList: A = spmatrix.ll_mat_from_mtx(problem) (m, n) = A.shape if m != n: break prob = os.path.basename(problem) if prob[-4:] == '.mtx': prob = prob[:-4] # Right-hand side is Ae e = np.ones(n, 'd') b = np.empty(n, 'd') A.matvec(e, b) sys.stdout.write(dheader1 % (prob, n, A.nnz)) # No preconditioner x = np.zeros(n, 'd') t = cputime() info, iter, relres = pcg(A, b, x, tol, 2*n) t_noprec = cputime() - t err = np.linalg.norm(x-e, ord=np.Inf) sys.stdout.write(dheader2 % (iter, relres, err, info, 0.0, t_noprec)) timings['None'].append(t_noprec) # Diagonal preconditioner x = np.zeros(n, 'd') t = cputime() M = precon.jacobi(A, 1.0, 1) t_getM_diag = cputime() - t t = cputime() info, iter, relres = pcg(A, b, x, tol, 2*n, M) t_diag = cputime() - t err = np.linalg.norm(x-e, ord=np.Inf) sys.stdout.write(lhead1 * ' ') sys.stdout.write(dheader2 % (iter,relres,err,info,t_getM_diag,t_diag)) timings['Diagonal'].append(t_diag) # SSOR preconditioner # It appears that steps=1 and omega=1.0 are nearly optimal in all cases x = np.zeros(n, 'd') t = cputime() M = precon.ssor(A.to_sss(), 1.0, 1) t_getM_ssor = cputime() - t t = cputime() info, iter, relres = pcg(A, b, x, tol, 2*n, M) t_ssor = cputime() - t err = np.linalg.norm(x-e, ord=np.Inf) sys.stdout.write(lhead1 * ' ') sys.stdout.write(dheader2 % (iter,relres,err,info,t_getM_ssor,t_ssor)) timings['SSOR'].append(t_ssor) sys.stderr.write('-' * lhead + '\n') return timings
S = L.to_sss() b = np.ones(N * N, 'd') print 'Solving 2D-Laplace equation using PCG and SSOR preconditioner with variable omega' print print 'omega nit time resid' print '--------------------------------' for omega in [0.1 * (i + 1) for i in range(20)]: K_ssor = precon.ssor(S, omega, SSOR_STEPS) t1 = time.clock() x = np.empty(N * N, 'd') info, iter, relres = pcg(S, b, x, TOL, MAXIT, K_ssor) elapsed_time = time.clock() - t1 r = np.zeros(N * N, 'd') S.matvec(x, r) r = b - r res_nrm2 = np.linalg.norm(r) if info == 0: iter_str = str(iter) else: iter_str = '---' print '%5.1f %5s %6.2f %10.3e' % (omega, iter_str, elapsed_time, res_nrm2)