def __call__(self, mtx_a, mtx_b=None, n_eigs=None, eigenvectors=None, status=None, conf=None): from pysparse import jdsym, itsolvers, precon output("loading...") A = self._convert_mat(mtx_a) output("...done") if mtx_b is not None: M = self._convert_mat(mtx_b) output("solving...") Atau=A.copy() Atau.shift(-conf.tau,M) K=precon.jacobi(Atau) A=A.to_sss(); if mtx_b is not None: M=M.to_sss(); method = getattr(itsolvers, conf.method) kconv, lmbd, Q, it, it_in = jdsym.jdsym(A, M, K, n_eigs, conf.tau, conf.eps_a, conf.i_max, method, clvl=conf.verbosity, strategy=conf.strategy) output("number of converged eigenvalues:", kconv) output("...done") if status is not None: status['q'] = Q status['it'] = it status['it_in'] = it_in return lmbd, Q
def solve(A, B, n_eigs=4, verbose=False): """ Solves the generalized eigenvalue problem. A, B ... scipy matrices returns (lmbd, Q), where lmbd are the eigenvalues and Q is a numpy array of solutions """ if verbose: print "converting to pysparse" n = A.shape[0] A = convert_mat(A) B = convert_mat(B) if verbose: print "solving (%d x %d)" % (n, n) Atau = A.copy() tau = -1 Atau.shift(-tau, B) K = precon.jacobi(Atau) A = A.to_sss() B = B.to_sss() kconv, lmbd, Q, it, it_in = jdsym.jdsym(A, B, K, n_eigs, tau, 1e-6, 150, itsolvers.qmrs) if verbose: print "number of converged eigenvalues:", kconv return lmbd, Q
def _solve_(self, L, x, b): A = L._getMatrix().to_csr() Assor=precon.jacobi(L._getMatrix()) info, iter, relres = itsolvers.gmres(A, b, x, self.tolerance, self.iterations, Assor) self._raiseWarning(info, iter, relres)
def modalSolver(self, nev = 3, ncv = -1, tol = -1, mxiter = -1): if SOLVER == 'default': if ncv < 0: ncv = 4*nev ncv = ncv + nev if tol < 0.: tol = 1e-2 if mxiter < 0: mxiter = 1000. return solver.arpack(self.GK, self.GM, nev, ncv, tol, mxiter) elif SOLVER == 'pysparse': # copy matrixes GK = self.GK A = spmatrix.ll_mat_sym(self.dofcount, len(GK)) # TODO change to scipy for row, col in GK: A[row,col] = GK[row,col] GM = self.GM M = spmatrix.ll_mat_sym(self.dofcount, len(GM)) # TODO change to scipy for row, col in GM: M[row,col] = GM[row,col] # Atau = A + tau*M tau = -1.0 Atau = A.copy() Atau.shift(tau, M) K = precon.jacobi(Atau) # convert to skyline A = A.to_sss() M = M.to_sss() # solve k_conv, lmbd, Q, it, it_innser = \ jdsym.jdsym(A, M, K, nev, tau, 1e-6, 1000, itsolvers.qmrs, jmin=5, jmax=10, clvl=0, strategy=1) # eigen values return lmbd else: raise FEError('unknown solver')
def modalSolver(self, nev=3, ncv=-1, tol=-1, mxiter=-1): if SOLVER == 'default': if ncv < 0: ncv = 4 * nev ncv = ncv + nev if tol < 0.: tol = 1e-2 if mxiter < 0: mxiter = 1000. return solver.arpack(self.GK, self.GM, nev, ncv, tol, mxiter) elif SOLVER == 'pysparse': # copy matrixes GK = self.GK A = spmatrix.ll_mat_sym(self.dofcount, len(GK)) for row, col in GK: A[row, col] = GK[row, col] GM = self.GM M = spmatrix.ll_mat_sym(self.dofcount, len(GM)) for row, col in GM: M[row, col] = GM[row, col] # Atau = A + tau*M tau = -1.0 Atau = A.copy() Atau.shift(tau, M) K = precon.jacobi(Atau) # convert to skyline A = A.to_sss() M = M.to_sss() # solve k_conv, lmbd, Q, it, it_innser = \ jdsym.jdsym(A, M, K, nev, tau, 1e-6, 1000, itsolvers.qmrs, jmin=5, jmax=10, clvl=0, strategy=1) # eigen values return lmbd else: raise FEError('unknown solver')
def __call__( self, mtx_a, mtx_b = None, n_eigs = None, eigenvectors = None, status = None, conf = None ): from pysparse import jdsym, itsolvers, precon conf = get_default( conf, self.conf ) mtx_a = get_default( mtx_a, self.mtx_a ) mtx_b = get_default( mtx_b, self.mtx_b ) n_eigs = get_default( n_eigs, self.n_eigs ) eigenvectors = get_default( eigenvectors, self.eigenvectors ) status = get_default( status, self.status ) output( "loading..." ) A = self._convert_mat( mtx_a ) output( "...done" ) if mtx_b is not None: M = self._convert_mat( mtx_b ) output( "solving..." ) tt = time.clock() Atau=A.copy() Atau.shift(-conf.tau,M) K=precon.jacobi(Atau) A=A.to_sss(); if mtx_b is not None: M=M.to_sss(); method = getattr( itsolvers, conf.method ) kconv, lmbd, Q, it, it_in = jdsym.jdsym( A, M, K, n_eigs, conf.tau, conf.eps_a, conf.i_max, method, clvl = conf.verbosity, strategy = conf.strategy ) output( "number of converged eigenvalues:", kconv ) ttt = time.clock() - tt output( '...done in %.2f s' % ttt ) if status is not None: status['time'] = ttt status['q'] = Q status['it'] = it status['it_in'] = it_in return lmbd, Q
def solve_eig_pysparse(A, B, n_eigs=4, verbose=False): """ Solves the generalized eigenvalue problem. A, B ..... scipy matrices n_eigs ... number of eigenvalues to solve for returns a list of (lmbd, vec), where lmbd is the eigenvalue and vec is the eigenvector """ from pysparse import jdsym, precon, itsolvers if verbose: print "converting to pysparse" n = A.shape[0] A = convert_mat(A) B = convert_mat(B) if verbose: print "solving (%d x %d)" % (n, n) Atau = A.copy() tau = -1 Atau.shift(-tau, B) K = precon.jacobi(Atau) A = A.to_sss() B = B.to_sss() kconv, lmbd, Q, it, it_in = jdsym.jdsym(A, B, K, n_eigs, tau, 1e-6, 150, itsolvers.qmrs) if verbose: print "number of converged eigenvalues:", kconv r = [] for i in range(len(lmbd)): vec = Q[:, i] r.append((lmbd[i], vec)) r.sort(key=lambda x: x[0]) print "eigenvalues:" eigs = [] for w, vec in r: if w > 0: break print w eigs.append(vec) return r
import os from pysparse import spmatrix, itsolvers, jdsym, precon path = os.path.join(os.environ['HOME'], 'matrices') A = spmatrix.ll_mat_from_mtx(os.path.join(path, 'edge6x3x5_A.mtx')) M = spmatrix.ll_mat_from_mtx(os.path.join(path, 'edge6x3x5_B.mtx')) sigma = 25.0 Asigma = A.copy() Asigma.shift(-sigma, M) K = precon.jacobi(Asigma.to_sss()) del Asigma ##n = A.shape[0] ##I = spmatrix.ll_mat(n, n) ##for i in xrange(n): ## I[i,i] = 1.0 ##K = precon.jacobi(I) k_conv, lmbd, Q, it, it_inner = \ jdsym.jdsym(A.to_sss(), M.to_sss(), K, 5, sigma, 1e-10, 150, itsolvers.qmrs, jmin=5, jmax=10, eps_tr=1e-4, toldecay=2.0, linitmax=200, clvl=1, strategy=1) print k_conv, lmbd, it, it_inner ##path = '/homes/geus/jdbsym/test/' ##A = spmatrix.ll_mat_from_mtx(path + 'sameh_10000.mtx').to_sss() ##K = precon.ssor(A) ##k_conv, lmbd, Q, it, it_inner = jdsym.jdsym(A, None, K, 4, 0.0, 1e-10, 50, itsolvers.qmrs, ## jmax=20, eps_tr=1e-4, toldecay=2.0, linitmax=60, clvl=1)
if test == 1: # Test: compare K=eye with K=None # # results are not the same, ritz-value in 2nd iterations differ path = '/local/home/geus/matrices' A = spmatrix.ll_mat_from_mtx(path + 'edge6x3x5_A.mtx') M = spmatrix.ll_mat_from_mtx(path + 'edge6x3x5_B.mtx') n = A.shape[0] sigma = 25.0 I = spmatrix.ll_mat(n, n) for i in xrange(n): I[i,i] = 1.0 Keye = precon.jacobi(I) k_conv, lmbd, Q, it, it_inner = jdsym.jdsym(A.to_sss(), M.to_sss(), None, 5, sigma, 1e-10, 15, itsolvers.qmrs, jmin=5, jmax=10, eps_tr=1e-4, toldecay=2.0, linitmax=200, clvl=1, strategy=1) k_conv, lmbd, Q, it, it_inner = jdsym.jdsym(A.to_sss(), M.to_sss(), Keye, 5, sigma, 1e-10, 15, itsolvers.qmrs, jmin=5, jmax=10, eps_tr=1e-4, toldecay=2.0, linitmax=200, clvl=1, strategy=1) elif test == 2: # Test 2: K = diag(A - sigma*M), test diagonal prec using Matlab Asigma = A.copy() Asigma.shift(-sigma, M) K = precon.jacobi(Asigma.to_sss())
def _applyToMatrix(self, A): """ Returns (preconditioning matrix, resulting matrix) """ return precon.jacobi(A), A.to_csr()
self.shape = A.shape n = self.shape[0] self.dinv = Numeric.zeros(n, 'd') for i in xrange(n): self.dinv[i] = 1.0 / A[i,i] def precon(self, x, y): Numeric.multiply(x, self.dinv, y) def resid(A, b, x): r = x.copy() A.matvec(x, r) r = b - r return math.sqrt(Numeric.dot(r, r)) K_diag = diag_prec(A) K_jac = precon.jacobi(A, 1.0, 1) K_ssor = precon.ssor(A, 1.0, 1) # K_ilu = precon.ilutp(L) n = L.shape[0]; b = Numeric.arange(n).astype(Numeric.Float) x = Numeric.zeros(n, 'd') info, iter, relres = itsolvers.pcg(A, b, x, 1e-6, 1000) print 'pcg, K_none: ', info, iter, relres, resid(A, b, x) x = Numeric.zeros(n, 'd') info, iter, relres = itsolvers.pcg(A, b, x, 1e-6, 1000, K_diag) print 'pcg, K_diag: ', info, iter, relres, resid(A, b, x) x = Numeric.zeros(n, 'd') info, iter, relres = itsolvers.pcg(A, b, x, 1e-6, 1000, K_jac) print 'pcg, K_jac: ', info, iter, relres, resid(A, b, x) x = Numeric.zeros(n, 'd')