Exemplo n.º 1
0
 def rand(shape, p=None, dtype=None):
     if dtype == complex128 or dtype == complex64:
         ten_real = ctf.zeros(shape, dtype=float_, sp=True)
         ten_real.fill_sp_random()
         ten = ctf.zeros(shape, dtype=dtype, sp=True)
         ten = ten_real + 0.j
     else:
         ten = ctf.zeros(shape, dtype=dtype, sp=True)
         ten_real.fill_sp_random()
     return ten
Exemplo n.º 2
0
    def test_conj(self):
        a0 = ctf.zeros((2,3))
        self.assertTrue(ctf.conj(a0).dtype == numpy.double)
        self.assertTrue(a0.conj().dtype == numpy.double)

        a0 = ctf.zeros((2,3), dtype=numpy.complex)
        self.assertTrue(ctf.conj(a0).dtype == numpy.complex128)
        self.assertTrue(a0.conj().dtype == numpy.complex128)
        a0[:] = 1j
        a0 = a0.conj()
        self.assertTrue(ctf.all(a0 == -1j))
Exemplo n.º 3
0
    def test_conj(self):
        a0 = ctf.zeros((2,3))
        self.assertTrue(ctf.conj(a0).dtype == numpy.double)
        self.assertTrue(a0.conj().dtype == numpy.double)

        a0 = ctf.zeros((2,3), dtype=numpy.complex)
        self.assertTrue(ctf.conj(a0).dtype == numpy.complex128)
        self.assertTrue(a0.conj().dtype == numpy.complex128)
        a0[:] = 1j
        a0 = a0.conj()
        self.assertTrue(ctf.all(a0 == -1j))
Exemplo n.º 4
0
def rand(shape, p=None, dtype=None):
    if dtype == complex128 or dtype == complex64:
        ten_real1 = ctf.zeros(shape, dtype=float_)
        ten_real1.fill_random()
        #ten_real2 = ctf.zeros(shape,dtype=float_)
        #ten_real2.fill_random()
        ten = ctf.zeros(shape, dtype=complex_)
        ten += ten_real1
        #ten += 1.j*ten_real2
    else:
        ten = ctf.zeros(shape, dtype=dtype, sp=False)
        ten.fill_random()
    return ten
Exemplo n.º 5
0
def updateH(A, W, regParam, omega, m, n, k):
    '''Gets the new H matrix by using the formula, subbing H for W'''
    H = ctf.zeros((n, k))
    identity = ctf.eye(k)
    for i in range(n):
        newW = getNewWCTF(omega[:, i], W, m, n, k)
        newMat = ctf.zeros((k, k))
        newMat.i("xy") << newW.i("bx") * newW.i("by")
        newMat.i("xy") << regParam * identity.i("xy")
        inverseFirstPart = CTFinverse(newMat)
        temp = ctf.zeros((k))
        temp.i("j") << inverseFirstPart.i("ja") * W.i("ba") * A[:, i].i("b")
        H[i] = temp
    return H
Exemplo n.º 6
0
def updateW(A, H, regParam, omega, m, n, k):
    '''Gets the new W matrix by using the formula'''
    Wctf = ctf.zeros((m, k))
    identity = ctf.eye(k)
    for i in range(m):
        newH = getNewHCTF(omega[i], H, m, n, k)
        newMat = ctf.zeros((k, k))
        newMat.i(
            "xy") << newH.i("bx") * newH.i("by") + regParam * identity.i("xy")
        inverseFirstPart = CTFinverse(newMat)
        temp = ctf.zeros((k))
        temp.i("j") << inverseFirstPart.i("ja") * H.i("ba") * A[i].i("b")
        Wctf[i] = temp
    return Wctf
Exemplo n.º 7
0
    def test_mixtype_comp(self):
        a = numpy.ones(2, dtype=int) < 1.0
        b = ctf.ones(2, dtype=int) < 1.0
        self.assertTrue(numpy.all(a == b.to_nparray()))
        a = numpy.zeros(2, dtype=numpy.float32) < numpy.ones(
            2, dtype=numpy.float64)
        b = ctf.zeros(2, dtype=numpy.float32) < ctf.ones(2,
                                                         dtype=numpy.float64)
        self.assertTrue(numpy.all(a == b.to_nparray()))
        a = numpy.ones(2, dtype=int) == numpy.ones(2, dtype=float)
        b = ctf.ones(2, dtype=int) == ctf.ones(2, dtype=float)
        self.assertTrue(numpy.all(a == b.to_nparray()))

        a = numpy.ones(2, dtype=int) <= 1.0
        b = ctf.ones(2, dtype=int) <= 1.0
        self.assertTrue(numpy.all(a == b.to_nparray()))
        a = numpy.zeros(2, dtype=numpy.float32) <= numpy.ones(
            2, dtype=numpy.float64)
        b = ctf.zeros(2, dtype=numpy.float32) <= ctf.ones(2,
                                                          dtype=numpy.float64)
        self.assertTrue(numpy.all(a == b.to_nparray()))
        a = numpy.ones(2, dtype=int) == numpy.ones(2, dtype=float)
        b = ctf.ones(2, dtype=int) == ctf.ones(2, dtype=float)
        self.assertTrue(numpy.all(a == b.to_nparray()))

        a = numpy.ones(2, dtype=int) > 1.0
        b = ctf.ones(2, dtype=int) > 1.0
        self.assertTrue(numpy.all(a == b.to_nparray()))
        a = numpy.zeros(2, dtype=numpy.float32) > numpy.ones(
            2, dtype=numpy.float64)
        b = ctf.zeros(2, dtype=numpy.float32) > ctf.ones(2,
                                                         dtype=numpy.float64)
        self.assertTrue(numpy.all(a == b.to_nparray()))
        a = numpy.ones(2, dtype=int) == numpy.ones(2, dtype=float)
        b = ctf.ones(2, dtype=int) == ctf.ones(2, dtype=float)
        self.assertTrue(numpy.all(a == b.to_nparray()))

        a = numpy.ones(2, dtype=int) >= 1.0
        b = ctf.ones(2, dtype=int) >= 1.0
        self.assertTrue(numpy.all(a == b.to_nparray()))
        a = numpy.zeros(2, dtype=numpy.float32) >= numpy.ones(
            2, dtype=numpy.float64)
        b = ctf.zeros(2, dtype=numpy.float32) >= ctf.ones(2,
                                                          dtype=numpy.float64)
        self.assertTrue(numpy.all(a == b.to_nparray()))
        a = numpy.ones(2, dtype=int) == numpy.ones(2, dtype=float)
        b = ctf.ones(2, dtype=int) == ctf.ones(2, dtype=float)
        self.assertTrue(numpy.all(a == b.to_nparray()))
def fast_gemm(A, B, n, b):
    A = A.reshape((n, b, n, b)).transpose([0, 2, 1, 3])
    B = B.reshape((n, b, n, b)).transpose([0, 2, 1, 3])
    sA = -ctf.einsum("ijxy->ixy", A)
    sB = -ctf.einsum("ijxy->ixy", B)
    for i in range(n):
        sA[i, :, :] += 2. * A[i, i, :, :]
        sB[i, :, :] += 2. * B[i, i, :, :]
    nn = n * (n - 1) * (n - 2) // 6
    idPC = ctf.zeros((nn, n, n))
    idPA = ctf.zeros((nn, n, n))
    idPB = ctf.zeros((nn, n, n))
    l = 0
    for i in range(n):
        for j in range(i):
            for k in range(j):
                idPC[l, i, j] = 1
                idPA[l, i, k] = 1
                idPB[l, j, k] = 1
                l += 1
    oA = ctf.einsum("aij,ijxy->axy", idPC, A) + ctf.einsum(
        "aik,ikxy->axy", idPA, A) + ctf.einsum("ajk,jkxy->axy", idPB, A)
    oB = ctf.einsum("aij,ijxy->axy", idPC, B) + ctf.einsum(
        "aik,ikxy->axy", idPA, B) + ctf.einsum("ajk,jkxy->axy", idPB, B)
    P = ctf.einsum("axy,ayz->axz", oA, oB)
    Z = ctf.einsum("aij,axy->ijxy", idPC, P) + ctf.einsum(
        "aik,axy->ikxy", idPA, P) + ctf.einsum("ajk,axy->jkxy", idPB, P)
    Z += Z.transpose([1, 0, 2, 3])
    U = ctf.einsum("ijxy,iyz->ijxz", A, sB)
    U += U.transpose([1, 0, 2, 3])
    V = ctf.einsum("ixy,ijyz->ijxz", sA, B)
    V += V.transpose([1, 0, 2, 3])
    W = ctf.einsum("ijxy,ijyz->ijxz", A, B)
    sW = ctf.einsum("ijxy->ixy", W)
    for i in range(n):
        sW[i, :, :] -= W[i, i, :, :]
        U[i, i, :, :] = 0
        V[i, i, :, :] = 0

    #print(U.transpose([0,2,1,3]).reshape((n*b,n*b)))
    #print(V.transpose([0,2,1,3]).reshape((n*b,n*b)))
    #print("W",W.transpose([0,2,1,3]).reshape((n*b,n*b)))
    #print((- sW.reshape((1,n,b,b)) - sW.reshape((n,1,b,b))).transpose([0,2,1,3]).reshape((n*b,n*b)))
    C = Z - (n - 8) * W + U + V - sW.reshape((1, n, b, b)) - sW.reshape(
        (n, 1, b, b))
    for i in range(n):
        C[i, i, :, :] = 2 * sW[i, :, :] + 2 * W[i, i, :, :]
    return C.transpose([0, 2, 1, 3]).reshape((n * b, n * b))
Exemplo n.º 9
0
def getNewWCTF(omegaj, W, m, n, k):
    '''gets the new W for each iteration'''
    newW = ctf.zeros((m, k))
    for i in range(m):
        if omegaj[i] != 0:
            newW[i, :] = W[i, :]
    return newW
Exemplo n.º 10
0
def getNewHCTF(omegai, H, m, n, k):
    '''gets the new W for each iteration'''
    newH = ctf.zeros((n, k))
    for j in range(n):
        if omegai[j] != 0:
            newH[j, :] = H[j, :]
    return newH
Exemplo n.º 11
0
def CTFinverse(A):
    """Gets the pseudoinverse of matrix A"""
    U, sigma, VT = ctf.svd(A)
    sigmaInv = 1 / sigma
    Ainv = ctf.zeros((A.to_nparray().shape[1], U.to_nparray().shape[0]))
    Ainv.i("ad") << VT.i("ba") * sigmaInv.i("b") * U.i("db")
    return Ainv
Exemplo n.º 12
0
def load_ten(
    dim,
    fname,
    dtype=None,
):
    ten = ctf.zeros(dim, dtype=dtype)
    ten.read_from_file(fname)
    return ten
Exemplo n.º 13
0
    def test_astype(self):
        a0 = ctf.zeros((2,3))
        self.assertTrue(a0.astype(numpy.complex128).dtype == numpy.complex128)
        self.assertTrue(a0.astype('D').dtype == numpy.complex128)
        self.assertTrue(a0.real().dtype == numpy.double)
        self.assertTrue(a0.imag().dtype == numpy.double)
        self.assertTrue(a0.real().shape == (2,3))
        self.assertTrue(a0.imag().shape == (2,3))
        self.assertTrue(a0.conj().dtype == numpy.double)

        a0 = ctf.zeros((2,3), dtype='D')
        self.assertTrue(a0.astype(numpy.double).dtype == numpy.double)
        self.assertTrue(a0.astype('d').dtype == numpy.double)
        self.assertTrue(a0.real().dtype == numpy.double)
        self.assertTrue(a0.imag().dtype == numpy.double)
        self.assertTrue(a0.real().shape == (2,3))
        self.assertTrue(a0.imag().shape == (2,3))
        self.assertTrue(a0.conj().dtype == numpy.complex128)
Exemplo n.º 14
0
 def test_attributes(self):
     a0 = ctf.zeros((2,3,4,5))
     self.assertTrue(a0.shape == (2,3,4,5))
     self.assertTrue(a0.T().shape == (5,4,3,2))
     self.assertTrue(a0.size == 120)
     self.assertTrue(a0.dtype == numpy.double)
     self.assertTrue(a0.real().shape == (2,3,4,5))
     self.assertTrue(a0.imag().shape == (2,3,4,5))
     self.assertTrue(a0.ndim == 4)
Exemplo n.º 15
0
    def test_astype(self):
        a0 = ctf.zeros((2, 3))
        self.assertTrue(a0.astype(numpy.complex128).dtype == numpy.complex128)
        self.assertTrue(a0.astype('D').dtype == numpy.complex128)
        self.assertTrue(a0.real().dtype == numpy.double)
        self.assertTrue(a0.imag().dtype == numpy.double)
        self.assertTrue(a0.real().shape == (2, 3))
        self.assertTrue(a0.imag().shape == (2, 3))
        self.assertTrue(a0.conj().dtype == numpy.double)

        a0 = ctf.zeros((2, 3), dtype='D')
        self.assertTrue(a0.astype(numpy.double).dtype == numpy.double)
        self.assertTrue(a0.astype('d').dtype == numpy.double)
        self.assertTrue(a0.real().dtype == numpy.double)
        self.assertTrue(a0.imag().dtype == numpy.double)
        self.assertTrue(a0.real().shape == (2, 3))
        self.assertTrue(a0.imag().shape == (2, 3))
        self.assertTrue(a0.conj().dtype == numpy.complex128)
Exemplo n.º 16
0
 def test_attributes(self):
     a0 = ctf.zeros((2, 3, 4, 5))
     self.assertTrue(a0.shape == (2, 3, 4, 5))
     self.assertTrue(a0.T().shape == (5, 4, 3, 2))
     self.assertTrue(a0.size == 120)
     self.assertTrue(a0.dtype == numpy.double)
     self.assertTrue(a0.real().shape == (2, 3, 4, 5))
     self.assertTrue(a0.imag().shape == (2, 3, 4, 5))
     self.assertTrue(a0.ndim == 4)
Exemplo n.º 17
0
    def test_int_conv(self):
        a = ctf.ones(2, dtype=float)
        b = numpy.ones(2, dtype=float)
        self.assertTrue(numpy.allclose(a.to_nparray(),b))

        a = ctf.zeros(2, dtype=complex)
        a[0] = 1
        self.assertTrue(numpy.allclose([a.norm2()],[1.]))
        a *= 2
        self.assertTrue(numpy.allclose([a.norm2()],[2.]))
Exemplo n.º 18
0
 def test_MTTKRP_vec(self):
     for N in range(2, 5):
         lens = numpy.random.randint(3, 4, N)
         A = ctf.tensor(lens)
         A.fill_sp_random(-1., 1., .5)
         mats = []
         for i in range(N):
             mats.append(ctf.random.random([lens[i]]))
         for i in range(N):
             ctr = A.i("ijklm"[0:N])
             for j in range(N):
                 if i != j:
                     ctr *= mats[j].i("ijklm"[j])
             ans = ctf.zeros(mats[i].shape)
             ans.i("ijklm"[i]) << ctr
             ctf.MTTKRP(A, mats, i)
             self.assertTrue(allclose(ans, mats[i]))
Exemplo n.º 19
0
 def get_init_guess(self, nroots=1, koopmans=True, diag=None):
     size = self.vector_size()
     dtype = getattr(diag, 'dtype', np.double)
     nroots = min(nroots, size)
     guess = []
     if koopmans:
         idx = range(nroots)
     else:
         if diag is None: diag = self.get_diag()
         idx = diag.to_nparray().argsort()[:nroots]
     guess = ctf.zeros([nroots, size], dtype)
     idx = np.arange(nroots) * size + np.asarray(idx)
     if rank == 0:
         guess.write(idx, np.ones(nroots))
     else:
         guess.write([], [])
     return guess
Exemplo n.º 20
0
 def get_init_guess(self, kshift, nroots=1, koopmans=False, diag=None):
     size = self.vector_size()
     dtype = getattr(diag, 'dtype', np.complex)
     nroots = min(nroots, size)
     guess = ctf.zeros([nroots, int(size)], dtype=dtype)
     if koopmans:
         ind = [
             kn * size + n
             for kn, n in enumerate(self.nonzero_vpadding[kshift][:nroots])
         ]
     else:
         idx = diag.to_nparray().argsort()[:nroots]
         ind = [kn * size + n for kn, n in enumerate(idx)]
     fill = np.ones(nroots)
     if rank == 0:
         guess.write(ind, fill)
     else:
         guess.write([], [])
     return guess
Exemplo n.º 21
0
def from_serial(mydf):
    """
    convert a serial pyscf.pbc.df.GDF object to cc_sym.mpigdf.GDF object
    only one process is used for reading from disk
    """
    newdf = GDF(mydf.cell, mydf.kpts)
    if mydf._cderi is None:
        newdf.build()
        return newdf
    import h5py
    kptij_lst = None
    auxcell = df.df.make_modrho_basis(mydf.cell, mydf.auxbasis, mydf.exp_to_discard)
    nao, naux = mydf.cell.nao_nr(), auxcell.nao_nr()
    if rank==0:
        feri = h5py.File(mydf._cderi,'r')
        kptij_lst = np.asarray(feri['j3c-kptij'])
    else:
        feri = None
    kptij_lst = comm.bcast(kptij_lst, root=0)
    newdf.auxcell = auxcell
    newdf.kptij_lst = kptij_lst
    j3c = ctf.zeros([len(kptij_lst), nao, nao, naux], dtype=np.complex128)
    for i in range(len(kptij_lst)):
        if rank==0:
            idx_j3c = np.arange(nao**2*naux)
            tmp = np.asarray([feri['j3c/%d/%d'%(i,istep)] for istep in range(len(feri['j3c/%d'%i]))])[0]
            kpti_kptj = kptij_lst[i]
            is_real = is_zero(kpti_kptj[0]-kpti_kptj[1])
            if is_real:
                tmp = pyscflib.unpack_tril(tmp)
            else:
                tmp = tmp.reshape(naux,nao,nao)
            tmp = tmp.transpose(1,2,0)
            j3c.write(i*idx_j3c.size+idx_j3c, tmp.ravel())
        else:
            j3c.write([],[])
    newdf.j3c = j3c
    return newdf
Exemplo n.º 22
0
    def test_transpose(self):
        a1 = ctf.zeros((2, 3))
        self.assertTrue(a1.transpose().shape == (3, 2))
        a1 = ctf.zeros((2, 3, 4, 5))
        self.assertTrue(a1.transpose().shape == (5, 4, 3, 2))

        a1 = ctf.zeros((2, 3))
        self.assertTrue(a1.T().shape == (3, 2))
        a1 = ctf.zeros((2, 3, 4, 5))
        self.assertTrue(a1.T().shape == (5, 4, 3, 2))

        a1 = ctf.zeros((2, 3, 4, 5))
        self.assertTrue(a1.transpose((0, 2, 1, -1)).shape == (2, 4, 3, 5))
        self.assertTrue(ctf.transpose(a1, (0, 2, 1, -1)).shape == (2, 4, 3, 5))
        self.assertTrue(a1.transpose(0, -1, 2, 1).shape == (2, 5, 4, 3))
        self.assertTrue(a1.transpose(0, -2, 1, -1).shape == (2, 4, 3, 5))
        self.assertTrue(a1.transpose(-3, -2, 0, -1).shape == (3, 4, 2, 5))
        self.assertTrue(a1.transpose(-3, 0, -1, 2).shape == (3, 2, 5, 4))
        self.assertTrue(a1.transpose(-3, -2, -1, -4).shape == (3, 4, 5, 2))

        # The case which does not change the data ordering in memory.
        # It does not need create new tensor.
        a2 = a1.transpose(0, 1, 2, 3)
        a2[:] = 1
        self.assertTrue(ctf.all(a2 == 1))
        a0 = ctf.zeros((1, 1, 3))
        a2 = a0.transpose(1, 0, 2)
        a0[:] = 1
        self.assertTrue(ctf.all(a0 == 1))

        a1 = ctf.zeros((2, 3, 4, 5))
        with self.assertRaises(ValueError):
            a1.transpose((1, 2))
        with self.assertRaises(ValueError):
            a1.transpose((0, 2, 1, 2))
        with self.assertRaises(ValueError):
            a1.transpose((0, 4, 1, 2))
Exemplo n.º 23
0
    def test_transpose(self):
        a1 = ctf.zeros((2,3))
        self.assertTrue(a1.transpose().shape == (3,2))
        a1 = ctf.zeros((2,3,4,5))
        self.assertTrue(a1.transpose().shape == (5,4,3,2))

        a1 = ctf.zeros((2,3))
        self.assertTrue(a1.T().shape == (3,2))
        a1 = ctf.zeros((2,3,4,5))
        self.assertTrue(a1.T().shape == (5,4,3,2))

        a1 = ctf.zeros((2,3,4,5))
        self.assertTrue(a1.transpose((0,2,1,-1)).shape == (2,4,3,5))
        self.assertTrue(ctf.transpose(a1, (0,2,1,-1)).shape == (2,4,3,5))
        self.assertTrue(a1.transpose(0,-1,2,1).shape == (2,5,4,3))
        self.assertTrue(a1.transpose(0,-2,1,-1).shape == (2,4,3,5))
        self.assertTrue(a1.transpose(-3,-2,0,-1).shape == (3,4,2,5))
        self.assertTrue(a1.transpose(-3,0,-1,2).shape == (3,2,5,4))
        self.assertTrue(a1.transpose(-3,-2,-1,-4).shape == (3,4,5,2))

        # The case which does not change the data ordering in memory.
        # It does not need create new tensor.
        a2 = a1.transpose(0,1,2,3)
        a2[:] = 1
        self.assertTrue(ctf.all(a2 == 1))
        a0 = ctf.zeros((1,1,3))
        a2 = a0.transpose(1,0,2)
        a0[:] = 1
        self.assertTrue(ctf.all(a0 == 1))

        a1 = ctf.zeros((2,3,4,5))
        with self.assertRaises(ValueError):
            a1.transpose((1,2))
        with self.assertRaises(ValueError):
            a1.transpose((0,2,1,2))
        with self.assertRaises(ValueError):
            a1.transpose((0,4,1,2))
Exemplo n.º 24
0
 def test_copy(self):
     a1 = ctf.zeros((2, 3, 4))
     a1 = ctf.copy(a1)
     a1 = a1.copy()
Exemplo n.º 25
0
 def test_zeros(self):
     a1 = ctf.zeros((2, 3, 4))
     a1 = ctf.zeros((2, 3, 4), dtype=numpy.complex128)
     a1 = ctf.zeros_like(a1)
     self.assertTrue(a1.dtype == numpy.complex128)
Exemplo n.º 26
0
 def zeros(shape, dtype=None):
     return ctf.zeros(shape, dtype=dtype, sp=True)
Exemplo n.º 27
0
 def test_copy(self):
     a1 = ctf.zeros((2,3,4))
     a1 = ctf.copy(a1)
     a1 = a1.copy()
Exemplo n.º 28
0
 def zeros_like(tensor):
     return ctf.zeros(CTFBackend.shape(tensor))
Exemplo n.º 29
0
 def test_zeros(self):
     a1 = ctf.zeros((2,3,4))
     a1 = ctf.zeros((2,3,4), dtype=numpy.complex128)
     a1 = ctf.zeros_like(a1)
     self.assertTrue(a1.dtype == numpy.complex128)
Exemplo n.º 30
0
    if cutoff != None:
        eris.ovvv = eris.ovvv.sparsify(cutoff)
        eris.oovv = eris.oovv.sparsify(cutoff)
        eris.oooo = eris.oooo.sparsify(cutoff)
        eris.ooov = eris.ooov.sparsify(cutoff)
        eris.vvvv = eris.vvvv.sparsify(cutoff)
        eris.ovov = eris.ovov.sparsify(cutoff)
        if (ctf.comm().rank() == 0):
            for e in [
                    eris.ovvv, eris.oovv, eris.oooo, eris.ooov, eris.vvvv,
                    eris.ovov
            ]:
                print "For integral tensor with shape", e.shape, "symmetry", e.sym, "number of nonzeros with cutoff", cutoff, "is ", (
                    int(10000000 * e.nnz_tot / e.size)) / 100000., "%"

    t1 = ctf.zeros([nocc, nvir])
    t2 = ctf.zeros([nocc, nocc, nvir, nvir])
    t2.fill_random(0., 1.)
    if (cutoff != None):
        t2 = t2.sparsify(1 - (1 - cutoff) * 10)
        if (ctf.comm().rank() == 0):
            print "For amplitude tensor with shape", t2.shape, "symmetry", t2.sym, "number of nonzeros with cutoff", 1 - (
                1 - cutoff) * 10, "is ", (int(
                    10000000 * t2.nnz_tot / t2.size)) / 100000., "%"

    start = time.time()
    [t1new, t2new] = update_amps(t1, t2, eris)
    end = time.time()
    t1norm = ctf.vecnorm(t1new)
    t2norm = ctf.vecnorm(t2new)
    if (ctf.comm().rank() == 0):
Exemplo n.º 31
0
 def test_norm(self):
     vec = ctf.zeros((20, 20), dtype=np.float64)
     vec.fill_random(-1, 1)
     self.assertTrue(
         np.allclose([ctf.norm(vec, 2)],
                     [np.linalg.norm(vec.to_nparray(), 2)]))
Exemplo n.º 32
0
 def zeros(self, shape, dtype=float):
     return self.tensor(ctf.zeros(shape, dtype=dtype))
def zeros(shape):
    return ctf.zeros(shape)
Exemplo n.º 34
0
def _make_j3c(mydf, cell, auxcell, kptij_lst):
    max_memory = max(2000, mydf.max_memory-pyscflib.current_memory()[0])
    fused_cell, fuse = df.df.fuse_auxcell(mydf, auxcell)
    log = Logger(mydf.stdout, mydf.verbose)
    nao, nfao = cell.nao_nr(), fused_cell.nao_nr()
    jobs = np.arange(fused_cell.nbas)
    tasks = list(static_partition(jobs))
    ntasks = max(comm.allgather(len(tasks)))
    j3c_junk = ctf.zeros([len(kptij_lst), nao**2, nfao], dtype=np.complex128)
    t1 =  t0 = (time.clock(), time.time())

    idx_full = np.arange(j3c_junk.size).reshape(j3c_junk.shape)
    if len(tasks) > 0:
        q0, q1 = tasks[0], tasks[-1] + 1
        shls_slice = (0, cell.nbas, 0, cell.nbas, q0, q1)
        bstart, bend = fused_cell.ao_loc_nr()[q0], fused_cell.ao_loc_nr()[q1]
        idx = idx_full[:,:,bstart:bend].ravel()
        tmp = df.incore.aux_e2(cell, fused_cell, intor='int3c2e', aosym='s2', kptij_lst=kptij_lst, shls_slice=shls_slice)
        nao_pair = nao**2
        if tmp.shape[-2] != nao_pair and tmp.ndim == 2:
            tmp = pyscflib.unpack_tril(tmp, axis=0).reshape(nao_pair,-1)
        j3c_junk.write(idx, tmp.ravel())
    else:
        j3c_junk.write([],[])

    t1 = log.timer('j3c_junk', *t1)

    naux = auxcell.nao_nr()
    mesh = mydf.mesh
    Gv, Gvbase, kws = cell.get_Gv_weights(mesh)
    b = cell.reciprocal_vectors()
    gxyz = pyscflib.cartesian_prod([np.arange(len(x)) for x in Gvbase])
    ngrids = gxyz.shape[0]

    kptis = kptij_lst[:,0]
    kptjs = kptij_lst[:,1]
    kpt_ji = kptjs - kptis
    mydf.kptij_lst = kptij_lst
    uniq_kpts, uniq_index, uniq_inverse = unique(kpt_ji)

    jobs = np.arange(len(uniq_kpts))
    tasks = list(static_partition(jobs))
    ntasks = max(comm.allgather(len(tasks)))

    blksize = max(2048, int(max_memory*.5e6/16/fused_cell.nao_nr()))
    log.debug2('max_memory %s (MB)  blocksize %s', max_memory, blksize)
    j2c  = ctf.zeros([len(uniq_kpts),naux,naux], dtype=np.complex128)

    a = cell.lattice_vectors() / (2*np.pi)
    def kconserve_indices(kpt):
        '''search which (kpts+kpt) satisfies momentum conservation'''
        kdif = np.einsum('wx,ix->wi', a, uniq_kpts + kpt)
        kdif_int = np.rint(kdif)
        mask = np.einsum('wi->i', abs(kdif - kdif_int)) < KPT_DIFF_TOL
        uniq_kptji_ids = np.where(mask)[0]
        return uniq_kptji_ids

    def cholesky_decomposed_metric(j2c_kptij):
        j2c_negative = None
        try:
            j2c_kptij = scipy.linalg.cholesky(j2c_kptij, lower=True)
            j2ctag = 'CD'
        except scipy.linalg.LinAlgError as e:
            w, v = scipy.linalg.eigh(j2c_kptij)
            log.debug('cond = %.4g, drop %d bfns',
                      w[-1]/w[0], np.count_nonzero(w<mydf.linear_dep_threshold))
            v1 = np.zeros(v.T.shape, dtype=v.dtype)
            v1[w>mydf.linear_dep_threshold,:] = v[:,w>mydf.linear_dep_threshold].conj().T
            v1[w>mydf.linear_dep_threshold,:] /= np.sqrt(w[w>mydf.linear_dep_threshold]).reshape(-1,1)
            j2c_kptij = v1
            if cell.dimension == 2 and cell.low_dim_ft_type != 'inf_vacuum':
                idx = np.where(w < -mydf.linear_dep_threshold)[0]
                if len(idx) > 0:
                    j2c_negative = np.zeros(v1.shape, dtype=v1.dtype)
                    j2c_negative[idx,:] = (v[:,idx]/np.sqrt(-w[idx])).conj().T

            w = v = None
            j2ctag = 'eig'
        return j2c_kptij, j2c_negative, j2ctag

    for itask in range(ntasks):
        if itask >= len(tasks):
            j2c.write([],[])
            continue
        k = tasks[itask]
        kpt = uniq_kpts[k]
        j2ctmp = np.asarray(fused_cell.pbc_intor('int2c2e', hermi=1, kpts=kpt))
        coulG = mydf.weighted_coulG(kpt, False, mesh)
        for p0, p1 in pyscflib.prange(0, ngrids, blksize):
            aoaux = ft_ao.ft_ao(fused_cell, Gv[p0:p1], None, b, gxyz[p0:p1], Gvbase, kpt).T
            if is_zero(kpt):
                j2ctmp[naux:] -= np.dot(aoaux[naux:].conj()*coulG[p0:p1].conj(), aoaux.T).real
                j2ctmp[:naux,naux:] = j2ctmp[naux:,:naux].T
            else:
                j2ctmp[naux:] -= np.dot(aoaux[naux:].conj()*coulG[p0:p1].conj(), aoaux.T)
                j2ctmp[:naux,naux:] = j2ctmp[naux:,:naux].T.conj()

        tmp = fuse(fuse(j2ctmp).T).T
        idx = k * naux**2 + np.arange(naux**2)
        j2c.write(idx, tmp.ravel())
        j2ctmp = tmp = None

    coulG = None
    t1 = log.timer('j2c', *t1)

    j3c = ctf.zeros([len(kpt_ji),nao,nao,naux], dtype=np.complex128)
    jobs = np.arange(len(kpt_ji))
    tasks = list(static_partition(jobs))
    ntasks = max(comm.allgather(len(tasks)))

    for itask in range(ntasks):
        if itask >= len(tasks):
            j2c_ji = j2c.read([])
            j3ctmp = j3c_junk.read([])
            j3c.write([],[])
            continue
        idx_ji = tasks[itask]
        kpti, kptj = kptij_lst[idx_ji]
        idxi, idxj = member(kpti, mydf.kpts), member(kptj, mydf.kpts)
        uniq_idx = uniq_inverse[idx_ji]
        kpt = uniq_kpts[uniq_idx]
        id_eq = kconserve_indices(-kpt)
        id_conj = kconserve_indices(kpt)
        id_conj = np.asarray([i for i in id_conj if i not in id_eq], dtype=int)
        id_full = np.hstack((id_eq, id_conj))
        map_id, conj = min(id_full), np.argmin(id_full) >=len(id_eq)
        j2cidx = map_id * naux**2 + np.arange(naux**2)
        j2c_ji = j2c.read(j2cidx).reshape(naux, naux) # read to be added
        j2c_ji, j2c_negative, j2ctag = cholesky_decomposed_metric(j2c_ji)
        if conj: j2c_ji = j2c_ji.conj()
        shls_slice= (auxcell.nbas, fused_cell.nbas)
        Gaux = ft_ao.ft_ao(fused_cell, Gv, shls_slice, b, gxyz, Gvbase, kpt)
        wcoulG = mydf.weighted_coulG(kpt, False, mesh)
        Gaux *= wcoulG.reshape(-1,1)
        j3c_id = idx_ji * nao**2*nfao + np.arange(nao**2*nfao)
        j3ctmp = j3c_junk.read(j3c_id).reshape(nao**2, fused_cell.nao_nr()).T
        if is_zero(kpt):  # kpti == kptj
            if cell.dimension == 3:
                vbar = fuse(mydf.auxbar(fused_cell))
                ovlp = cell.pbc_intor('int1e_ovlp', hermi=1, kpts=kptj)
                for i in np.where(vbar != 0)[0]:
                    j3ctmp[i] -= vbar[i] * ovlp.reshape(-1)

        aoao = ft_ao._ft_aopair_kpts(cell, Gv, None, 's1', b, gxyz, Gvbase, kpt, kptj)[0].reshape(len(Gv),-1)
        j3ctmp[naux:] -= np.dot(Gaux.T.conj(), aoao)
        j3ctmp = fuse(j3ctmp)
        if j2ctag == 'CD':
            v = scipy.linalg.solve_triangular(j2c_ji, j3ctmp, lower=True, overwrite_b=True)
        else:
            v = np.dot(j2c_ji, j3ctmp)
        v = v.T.reshape(nao,nao,naux)
        j3c_id = idx_ji * nao**2*naux + np.arange(nao**2*naux)
        j3c.write(j3c_id, v.ravel())

    mydf.j3c = j3c
    return None
Exemplo n.º 35
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
Exemplo n.º 36
0
def _make_df_eris(mycc, eris):
    from cc_sym import mpigdf
    mydf = mycc._scf.with_df
    mo_coeff = eris.mo_coeff
    if mydf.j3c is None: mydf.build()
    gvec = mydf.cell.reciprocal_vectors()
    nocc, nmo = mycc.nocc, mycc.nmo
    nvir = nmo - nocc
    kpts = mydf.kpts
    nkpts = len(kpts)
    nao, naux = mydf.j3c.shape[2:]
    ijL = ctf.zeros([nkpts,nkpts,nocc,nocc,naux], dtype=mydf.j3c.dtype)
    iaL = ctf.zeros([nkpts,nkpts,nocc,nvir,naux], dtype=mydf.j3c.dtype)
    aiL = ctf.zeros([nkpts,nkpts,nvir,nocc,naux], dtype=mydf.j3c.dtype)
    abL = ctf.zeros([nkpts,nkpts,nvir,nvir,naux], dtype=mydf.j3c.dtype)
    jobs = []
    for ki in range(nkpts):
        for kj in range(ki,nkpts):
            jobs.append([ki,kj])
    tasks = static_partition(jobs)
    ntasks = max(comm.allgather((len(tasks))))
    idx_j3c = np.arange(nao**2*naux)
    idx_ooL = np.arange(nocc**2*naux)
    idx_ovL = np.arange(nocc*nvir*naux)
    idx_vvL = np.arange(nvir**2*naux)
    log = Logger(mydf.stdout, mydf.verbose)
    cput1 = cput0 = (time.clock(), time.time())
    for itask in range(ntasks):
        if itask >= len(tasks):
            mydf.j3c.read([])
            ijL.write([], [])
            iaL.write([], [])
            aiL.write([], [])
            abL.write([], [])

            ijL.write([], [])
            iaL.write([], [])
            aiL.write([], [])
            abL.write([], [])
            continue
        ki, kj = tasks[itask]
        ijid, ijdagger = mpigdf.get_member(kpts[ki], kpts[kj], mydf.kptij_lst)
        uvL = mydf.j3c.read(ijid*idx_j3c.size+idx_j3c).reshape(nao,nao,naux)
        if ijdagger: uvL = uvL.transpose(1,0,2).conj()
        pvL = np.einsum("up,uvL->pvL", mo_coeff[ki].conj(), uvL, optimize=True)
        uvL = None
        pqL = np.einsum('vq,pvL->pqL', mo_coeff[kj], pvL, optimize=True)

        off = ki * nkpts + kj
        ijL.write(off*idx_ooL.size+idx_ooL, pqL[:nocc,:nocc].ravel())
        iaL.write(off*idx_ovL.size+idx_ovL, pqL[:nocc,nocc:].ravel())
        aiL.write(off*idx_ovL.size+idx_ovL, pqL[nocc:,:nocc].ravel())
        abL.write(off*idx_vvL.size+idx_vvL, pqL[nocc:,nocc:].ravel())

        off = kj * nkpts + ki
        pqL = pqL.transpose(1,0,2).conj()
        ijL.write(off*idx_ooL.size+idx_ooL, pqL[:nocc,:nocc].ravel())
        iaL.write(off*idx_ovL.size+idx_ovL, pqL[:nocc,nocc:].ravel())
        aiL.write(off*idx_ovL.size+idx_ovL, pqL[nocc:,:nocc].ravel())
        abL.write(off*idx_vvL.size+idx_vvL, pqL[nocc:,nocc:].ravel())

    cput1 = log.timer("j3c transformation", *cput1)
    sym1 = ["+-+", [kpts,]*3, None, gvec]
    sym2 = ["+--", [kpts,]*3, None, gvec]

    ooL = tensor(ijL, sym1, verbose=mycc.SYMVERBOSE)
    ovL = tensor(iaL, sym1, verbose=mycc.SYMVERBOSE)
    voL = tensor(aiL, sym1, verbose=mycc.SYMVERBOSE)
    vvL = tensor(abL, sym1, verbose=mycc.SYMVERBOSE)

    ooL2 = tensor(ijL, sym2, verbose=mycc.SYMVERBOSE)
    ovL2 = tensor(iaL, sym2, verbose=mycc.SYMVERBOSE)
    voL2 = tensor(aiL, sym2, verbose=mycc.SYMVERBOSE)
    vvL2 = tensor(abL, sym2, verbose=mycc.SYMVERBOSE)

    eris.oooo = lib.einsum('ijg,klg->ijkl', ooL, ooL2) / nkpts
    eris.ooov = lib.einsum('ijg,kag->ijka', ooL, ovL2) / nkpts
    eris.oovv = lib.einsum('ijg,abg->ijab', ooL, vvL2) / nkpts
    eris.ovvo = lib.einsum('iag,bjg->iabj', ovL, voL2) / nkpts
    eris.ovov = lib.einsum('iag,jbg->iajb', ovL, ovL2) / nkpts
    eris.ovvv = lib.einsum('iag,bcg->iabc', ovL, vvL2) / nkpts
    eris.vvvv = lib.einsum('abg,cdg->abcd', vvL, vvL2) / nkpts

    cput1 = log.timer("integral transformation", *cput1)