Beispiel #1
0
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
Beispiel #2
0
    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
Beispiel #3
0
 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')
Beispiel #4
0
    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')
Beispiel #5
0
    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
Beispiel #6
0
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
Beispiel #7
0
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
Beispiel #8
0
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)


##path = '../spmatrix/'
##A_ll = spmatrix.ll_mat_from_mtx(path + 'cop18_el5_A.mtx')
##M_ll = spmatrix.ll_mat_from_mtx(path + 'cop18_el5_M.mtx')
import sys
from pysparse import jdsym, spmatrix, itsolvers, precon
matfiles = sys.argv[1:5]
target_value = eval(sys.argv[3])
eigenval_num = eval(sys.argv[4])
jdtol = eval(sys.argv[5])
max_iter = eval(sys.argv[6])
mat_left = spmatrix.ll_mat_from_mtx(matfiles[0])
mat_right = spmatrix.ll_mat_from_mtx(matfiles[1])
shape = mat_left.shape
T = mat_left.copy()
T.shift(-target_value, mat_right)
K = precon.ssor(T.to_sss(), 1.0, 1) # K is preconditioner.
A = mat_left.to_sss()
M = mat_right.to_sss()
k_conv, lmbd, Q, it, itall = jdsym.jdsym(A, M, K, eigenval_num, target_value, jdtol, max_iter, itsolvers.minres)
NEIG = len(lmbd)
#for lam in lmbd:
#    print "value:", lam
eivecfile = open("eivecs.dat", "w")
N = len(Q[:,0])
print >> eivecfile, N
print >> eivecfile, NEIG
for ieig in range(len(lmbd)):
    eivec = Q[:,ieig]
    print >> eivecfile, lmbd[ieig] # printing eigenvalue
    for val in eivec:              # printing eigenvector
        print >> eivecfile, val
eivecfile.close()

    
Beispiel #10
0
    # 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())
    
    b = Numeric.ones(n, 'd')
    x = Numeric.zeros(n, 'd')
    K.precon(b, x)
Beispiel #11
0
import sys
from pysparse import jdsym, spmatrix, itsolvers, precon
matfiles = sys.argv[1:5]
target_value = eval(sys.argv[3])
eigenval_num = eval(sys.argv[4])
jdtol = eval(sys.argv[5])
max_iter = eval(sys.argv[6])
mat_left = spmatrix.ll_mat_from_mtx(matfiles[0])
mat_right = spmatrix.ll_mat_from_mtx(matfiles[1])
shape = mat_left.shape
T = mat_left.copy()
T.shift(-target_value, mat_right)
K = precon.ssor(T.to_sss(), 1.0, 1)  # K is preconditioner.
A = mat_left.to_sss()
M = mat_right.to_sss()
k_conv, lmbd, Q, it, itall = jdsym.jdsym(A, M, K, eigenval_num, target_value,
                                         jdtol, max_iter, itsolvers.minres)
NEIG = len(lmbd)
#for lam in lmbd:
#    print "value:", lam
eivecfile = open("eivecs.dat", "w")
N = len(Q[:, 0])
print >> eivecfile, N
print >> eivecfile, NEIG
for ieig in range(len(lmbd)):
    eivec = Q[:, ieig]
    print >> eivecfile, lmbd[ieig]  # printing eigenvalue
    for val in eivec:  # printing eigenvector
        print >> eivecfile, val
eivecfile.close()
Beispiel #12
0
print 'norm(x) = %g' % math.sqrt(numpy.dot(x, x))

r = numpy.zeros(n*n, 'd')
A.matvec(x, r)
r = b - r
print 'norm(b - A*x) = %g' % math.sqrt(numpy.dot(r, r))

# ---------------------------------------------------------------------------------------

K_ssor = precon.ssor(S, 1.9)
t1 = time.clock()

x = numpy.zeros(n*n, 'd')
info, iter, relres = itsolvers.pcg(S, b, x, tol, 2000, K_ssor)
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(numpy.dot(x, x))

r = numpy.zeros(n*n, 'd')
S.matvec(x, r)
r = b - r
print 'norm(b - A*x) = %g' % math.sqrt(numpy.dot(r, r))

# ---------------------------------------------------------------------------------------

from pysparse import jdsym
jdsym.jdsym(S, None, None, 5, 0.0, 1e-8, 100, itsolvers.qmrs, clvl=1)
from pysparse import precon
from pysparse import jdsym
import time

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 = 200

t1 = time.clock()
L = poisson2d_sym_blk(n)
print 'Time for constructing the matrix: %8.2f sec' % (time.clock() - t1, )

print L.nnz

# ---------------------------------------------------------------------------------------
t1 = time.clock()
jdsym.jdsym(L.to_sss(), None, None, 5, 0.0, 1e-8, 100, itsolvers.qmrs, clvl=1)
print 'Time spend in jdsym: %8.2f sec' % (time.clock() - t1, )
Beispiel #14
0
    M[i,i] = float(n/2) + i
Ms = M.to_sss()
normM = M[n-1,n-1]

K = diagPrecShifted(A, M, 0.006)

#-------------------------------------------------------------------------------
# Test 1: M = K = None

print 'Test 1'

lmbd_exact = zeros(ncv, 'd')
for k in xrange(ncv):
    lmbd_exact[k] =  A[k,k]

kconv, lmbd, Q, it, it_inner = jdsym.jdsym(As, None, None, ncv, 0.0, tol, 150, itsolvers.qmrs,
                                           jmin=5, jmax=10, eps_tr=1e-4, clvl=1)
    
assert ncv == kconv
assert allclose(computeResiduals(As, None, lmbd, Q), zeros(kconv), 0.0, tol)
assert allclose(lmbd, lmbd_exact, tol*tol, 0.0)

print 'OK'

#-------------------------------------------------------------------------------
# Test 2: K = None

print 'Test 2',

lmbd_exact = zeros(ncv, 'd')
for k in xrange(ncv):
    lmbd_exact[k] =  A[k,k]/M[k,k]