Esempio n. 1
0
    def test_vstack(self):
        a1 = ctf.astensor(numpy.ones(4))
        a2 = ctf.astensor(numpy.ones(4))
        self.assertTrue(ctf.vstack((a1, a2)).shape == (2, 4))

        a1 = ctf.astensor(numpy.ones((2, 4)))
        a2 = ctf.astensor(numpy.ones((3, 4)))
        self.assertTrue(ctf.vstack((a1, a2)).shape == (5, 4))

        a1 = ctf.astensor(numpy.ones((2, 4)))
        a2 = ctf.astensor(numpy.ones((3, 4)) + 0j)
        self.assertTrue(ctf.vstack((a1, a2)).shape == (5, 4))
        self.assertTrue(ctf.vstack((a1, a2)).dtype == numpy.complex128)

        a1 = ctf.astensor(numpy.ones((4, 1)))
        self.assertTrue(ctf.vstack((a1, 1.5)).shape == (5, 1))

        a1 = ctf.astensor(numpy.ones((2, 4, 2)))
        a2 = ctf.astensor(numpy.ones((3, 4, 2)))
        self.assertTrue(ctf.vstack((a1, a2)).shape == (5, 4, 2))
Esempio n. 2
0
    def test_vstack(self):
        a1 = ctf.astensor(numpy.ones(4))
        a2 = ctf.astensor(numpy.ones(4))
        self.assertTrue(ctf.vstack((a1, a2)).shape == (2,4))

        a1 = ctf.astensor(numpy.ones((2,4)))
        a2 = ctf.astensor(numpy.ones((3,4)))
        self.assertTrue(ctf.vstack((a1, a2)).shape == (5,4))

        a1 = ctf.astensor(numpy.ones((2,4)))
        a2 = ctf.astensor(numpy.ones((3,4))+0j)
        self.assertTrue(ctf.vstack((a1, a2)).shape == (5,4))
        self.assertTrue(ctf.vstack((a1, a2)).dtype == numpy.complex128)

        a1 = ctf.astensor(numpy.ones((4,1)))
        self.assertTrue(ctf.vstack((a1, 1.5)).shape == (5,1))

        a1 = ctf.astensor(numpy.ones((2,4,2)))
        a2 = ctf.astensor(numpy.ones((3,4,2)))
        self.assertTrue(ctf.vstack((a1, a2)).shape == (5,4,2))
Esempio n. 3
0
def kernel(eom,
           nroots=1,
           koopmans=True,
           guess=None,
           left=False,
           eris=None,
           imds=None,
           partition=None,
           kptlist=None,
           dtype=None,
           **kwargs):

    cput0 = (time.clock(), time.time())
    eom.dump_flags()

    log = Logger(eom.stdout, eom.verbose)
    if imds is None:
        imds = eom.make_imds(eris)

    size = eom.vector_size()
    nroots = min(nroots, size)
    nkpts = eom.nkpts

    if kptlist is None:
        kptlist = range(nkpts)

    if dtype is None:
        dtype = imds.t1.dtype

    evals = np.zeros((len(kptlist), nroots), np.float)
    evecs = []
    convs = np.zeros((len(kptlist), nroots), dtype)
    from cc_sym.linalg_helper.davidson import eigs
    for k, kshift in enumerate(kptlist):
        matvec, diag = eom.gen_matvec(kshift, imds, left=left, **kwargs)
        eom.update_symlib(kshift)
        user_guess = False
        if guess:
            user_guess = True
            assert len(guess) == nroots
            for g in guess:
                assert g.size == size
        else:
            user_guess = False
            guess = eom.get_init_guess(kshift, nroots, koopmans, diag)

        conv_k, evals_k, evecs_k = eigs(matvec,
                                        size,
                                        nroots,
                                        x0=guess,
                                        Adiag=diag,
                                        verbose=eom.verbose)
        evals_k = evals_k.real
        evals[k] = evals_k.real
        evecs.append(evecs_k)
        convs[k] = conv_k

        for n, en, vn in zip(range(nroots), evals_k, evecs_k):
            r1, r2 = eom.vector_to_amplitudes(vn, kshift)
            qp_weight = r1.norm()**2
            log.info('EOM-CCSD root %d E = %.16g  qpwt = %0.6g', n, en,
                     qp_weight)
    log.timer('EOM-CCSD', *cput0)
    evecs = ctf.vstack(tuple(evecs))
    return convs, evals, evecs
Esempio n. 4
0
def davidson(mult_by_A, N, neig, x0=None, Adiag=None, verbose=4):
    """Diagonalize a matrix via non-symmetric Davidson algorithm.

    mult_by_A() is a function which takes a vector of length N
        and returns a vector of length N.
    neig is the number of eigenvalues requested
    """

    if rank == 0:
        log = lib.logger.Logger(sys.stdout, verbose)
    else:
        log = lib.logger.Logger(sys.stdout, 0)

    cput1 = (time.clock(), time.time())

    Mmin = min(neig, N)
    Mmax = min(N, 2000)
    tol = 1e-6

    def mult(arg):
        return mult_by_A(arg)

    if Adiag is None:
        Adiag = np.zeros(N, np.complex)
        for i in range(N):
            test = np.zeros(N, np.complex)
            test[i] = 1.0
            Adiag[i] = mult(test)[i]
    else:
        Adiag = Adiag.to_nparray()

    idx = Adiag.argsort()
    lamda_k_old = 0
    lamda_k = 0
    target = 0
    conv = False
    if x0 is not None:
        assert x0.shape == (Mmin, N)
        b = x0.copy()
        Ab = tuple([mult(b[m, :]) for m in range(Mmin)])
        Ab = ctf.vstack(Ab).transpose()

    evals = np.zeros(neig, dtype=np.complex)
    evecs = []
    for istep, M in enumerate(range(Mmin, Mmax + 1)):
        if M == Mmin:
            b = ctf.zeros((N, M))
            if rank == 0:
                ind = [i * M + m for m, i in zip(range(M), idx)]
                fill = np.ones(len(ind))
                b.write(ind, fill)
            else:
                b.write([], [])
            Ab = tuple([mult(b[:, m]) for m in range(M)])
            Ab = ctf.vstack(Ab).transpose()

        else:

            Ab = ctf.hstack((Ab, mult(b[:, M - 1]).reshape(N, -1)))

        Atilde = ctf.dot(b.conj().transpose(), Ab)
        Atilde = Atilde.to_nparray()

        lamda, alpha = diagonalize_asymm(Atilde)
        lamda_k_old, lamda_k = lamda_k, lamda[target]
        alpha_k = ctf.astensor(alpha[:, target])
        if M == Mmax:
            break

        q = ctf.dot(Ab - lamda_k * b, alpha_k)
        qnorm = ctf.norm(q)
        log.info(
            'davidson istep = %d  root = %d  E = %.15g  dE = %.9g  residual = %.6g',
            istep, target, lamda_k.real, (lamda_k - lamda_k_old).real, qnorm)
        cput1 = log.timer('davidson iter', *cput1)

        if ctf.norm(q) < tol:
            evecs.append(ctf.dot(b, alpha_k))
            evals[target] = lamda_k
            if target == neig - 1:
                conv = True
                break
            else:
                target += 1
        eps = 1e-10
        xi = q / (lamda_k - Adiag + eps)
        bxi, R = ctf.qr(ctf.hstack((b, xi.reshape(N, -1))))
        nlast = bxi.shape[-1] - 1
        b = ctf.hstack(
            (b, bxi[:, nlast].reshape(N, -1))
        )  #can not replace nlast with -1, (inconsistent between numpy and ctf)
    evecs = ctf.vstack(tuple(evecs))

    return conv, evals, evecs
Esempio n. 5
0
 def vstack(self, tensors):
     return self.tensor(ctf.vstack(tuple(tsr.unwrap() for tsr in tensors)))