Example #1
0
    def ao2mo(self, mo_coeff=None):
        nocc = self.nocc
        nvir = self.nmo - nocc
        mem_incore, mem_outcore, mem_basic = ccsd._mem_usage(nocc, nvir)
        mem_now = lib.current_memory()[0]
        if (self._scf._eri is not None and
            (mem_incore+mem_now < self.max_memory) or self.mol.incore_anyway):
            return _make_eris_incore(self, mo_coeff)

        elif hasattr(self._scf, 'with_df'):
            raise NotImplementedError

        else:
            return _make_eris_outcore(self, mo_coeff)
Example #2
0
    def __init__(self, myci, mo_coeff=None, method='incore'):
        cput0 = (time.clock(), time.time())
        moidx = numpy.ones(myci.mo_occ.size, dtype=numpy.bool)
        if isinstance(myci.frozen, (int, numpy.integer)):
            moidx[:myci.frozen] = False
        elif len(myci.frozen) > 0:
            moidx[numpy.asarray(myci.frozen)] = False
        if mo_coeff is None:
            self.mo_coeff = mo_coeff = myci.mo_coeff[:, moidx]
        else:
            self.mo_coeff = mo_coeff = mo_coeff[:, moidx]
        dm = myci._scf.make_rdm1(myci.mo_coeff, myci.mo_occ)
        fockao = myci._scf.get_hcore() + myci._scf.get_veff(myci.mol, dm)
        self.fock = reduce(numpy.dot, (mo_coeff.T, fockao, mo_coeff))

        nocc = myci.nocc
        nmo = myci.nmo
        nvir = nmo - nocc
        mem_incore, mem_outcore, mem_basic = ccsd._mem_usage(nocc, nvir)
        mem_now = lib.current_memory()[0]

        log = logger.Logger(myci.stdout, myci.verbose)
        if (method == 'incore' and myci._scf._eri is not None and
            (mem_incore + mem_now < myci.max_memory)
                or myci.mol.incore_anyway):
            eri1 = ao2mo.incore.full(myci._scf._eri, mo_coeff)
            #:eri1 = ao2mo.restore(1, eri1, nmo)
            #:self.oooo = eri1[:nocc,:nocc,:nocc,:nocc].copy()
            #:self.ooov = eri1[:nocc,:nocc,:nocc,nocc:].copy()
            #:self.vooo = eri1[nocc:,:nocc,:nocc,:nocc].copy()
            #:self.voov = eri1[nocc:,:nocc,:nocc,nocc:].copy()
            #:self.vvoo = eri1[nocc:,nocc:,:nocc,:nocc].copy()
            #:vovv = eri1[nocc:,:nocc,nocc:,nocc:].copy()
            #:self.vovv = lib.pack_tril(vovv.reshape(-1,nvir,nvir))
            #:self.vvvv = ao2mo.restore(4, eri1[nocc:,nocc:,nocc:,nocc:], nvir)
            nvir_pair = nvir * (nvir + 1) // 2
            self.oooo = numpy.empty((nocc, nocc, nocc, nocc))
            self.ooov = numpy.empty((nocc, nocc, nocc, nvir))
            self.vooo = numpy.empty((nvir, nocc, nocc, nocc))
            self.voov = numpy.empty((nvir, nocc, nocc, nvir))
            self.vovv = numpy.empty((nvir, nocc, nvir_pair))
            self.vvvv = numpy.empty((nvir_pair, nvir_pair))
            ij = 0
            outbuf = numpy.empty((nmo, nmo, nmo))
            oovv = numpy.empty((nocc, nocc, nvir, nvir))
            for i in range(nocc):
                buf = lib.unpack_tril(eri1[ij:ij + i + 1], out=outbuf[:i + 1])
                for j in range(i + 1):
                    self.oooo[i, j] = self.oooo[j, i] = buf[j, :nocc, :nocc]
                    self.ooov[i, j] = self.ooov[j, i] = buf[j, :nocc, nocc:]
                    oovv[i, j] = oovv[j, i] = buf[j, nocc:, nocc:]
                ij += i + 1
            self.vvoo = lib.transpose(oovv.reshape(nocc**2, -1)).reshape(
                nvir, nvir, nocc, nocc)
            oovv = None
            ij1 = 0
            for i in range(nocc, nmo):
                buf = lib.unpack_tril(eri1[ij:ij + i + 1], out=outbuf[:i + 1])
                self.vooo[i - nocc] = buf[:nocc, :nocc, :nocc]
                self.voov[i - nocc] = buf[:nocc, :nocc, nocc:]
                lib.pack_tril(_cp(buf[:nocc, nocc:, nocc:]),
                              out=self.vovv[i - nocc])
                dij = i - nocc + 1
                lib.pack_tril(_cp(buf[nocc:i + 1, nocc:, nocc:]),
                              out=self.vvvv[ij1:ij1 + dij])
                ij += i + 1
                ij1 += dij
        else:
            cput1 = time.clock(), time.time()
            self.feri1 = lib.H5TmpFile()
            orbo = mo_coeff[:, :nocc]
            orbv = mo_coeff[:, nocc:]
            nvpair = nvir * (nvir + 1) // 2
            self.oooo = self.feri1.create_dataset('oooo',
                                                  (nocc, nocc, nocc, nocc),
                                                  'f8')
            self.ooov = self.feri1.create_dataset('ooov',
                                                  (nocc, nocc, nocc, nvir),
                                                  'f8')
            self.vvoo = self.feri1.create_dataset('vvoo',
                                                  (nvir, nvir, nocc, nocc),
                                                  'f8')
            self.vooo = self.feri1.create_dataset('vooo',
                                                  (nvir, nocc, nocc, nocc),
                                                  'f8')
            self.voov = self.feri1.create_dataset('voov',
                                                  (nvir, nocc, nocc, nvir),
                                                  'f8')
            self.vovv = self.feri1.create_dataset('vovv', (nvir, nocc, nvpair),
                                                  'f8')
            fsort = _ccsd.libcc.CCsd_sort_inplace
            nocc_pair = nocc * (nocc + 1) // 2
            nvir_pair = nvir * (nvir + 1) // 2

            def sort_inplace(p0, p1, eri):
                fsort(eri.ctypes.data_as(ctypes.c_void_p), ctypes.c_int(nocc),
                      ctypes.c_int(nvir), ctypes.c_int((p1 - p0) * nocc))
                vv = eri[:, :nvir_pair]
                oo = eri[:, nvir_pair:nvir_pair + nocc_pair]
                ov = eri[:, nvir_pair + nocc_pair:].reshape(-1, nocc, nvir)
                return oo, ov, vv

            buf = numpy.empty((nmo, nmo, nmo))
            oovv = numpy.empty((nocc, nocc, nvir, nvir))

            def save_occ_frac(p0, p1, eri):
                oo, ov, vv = sort_inplace(p0, p1, eri)
                self.oooo[p0:p1] = lib.unpack_tril(oo, out=buf).reshape(
                    p1 - p0, nocc, nocc, nocc)
                self.ooov[p0:p1] = ov.reshape(p1 - p0, nocc, nocc, nvir)
                oovv[p0:p1] = lib.unpack_tril(vv, out=buf).reshape(
                    p1 - p0, nocc, nvir, nvir)

            def save_vir_frac(p0, p1, eri):
                oo, ov, vv = sort_inplace(p0, p1, eri)
                self.vooo[p0:p1] = lib.unpack_tril(oo, out=buf).reshape(
                    p1 - p0, nocc, nocc, nocc)
                self.voov[p0:p1] = ov.reshape(p1 - p0, nocc, nocc, nvir)
                self.vovv[p0:p1] = vv.reshape(p1 - p0, nocc, -1)

            if not myci.direct:
                max_memory = max(2000,
                                 myci.max_memory - lib.current_memory()[0])
                self.feri2 = lib.H5TmpFile()
                ao2mo.full(myci.mol,
                           orbv,
                           self.feri2,
                           max_memory=max_memory,
                           verbose=log)
                self.vvvv = self.feri2['eri_mo']
                cput1 = log.timer_debug1('transforming vvvv', *cput1)

            tmpfile3 = tempfile.NamedTemporaryFile(dir=lib.param.TMPDIR)
            with h5py.File(tmpfile3.name, 'w') as feri:
                max_memory = max(2000,
                                 myci.max_memory - lib.current_memory()[0])
                mo = numpy.hstack((orbv, orbo))
                ao2mo.general(myci.mol, (mo, orbo, mo, mo),
                              feri,
                              max_memory=max_memory,
                              verbose=log)
                cput1 = log.timer_debug1('transforming oppp', *cput1)
                blksize = max(
                    1, int(min(8e9, max_memory * .5e6) / 8 / nmo**2 / nocc))
                handler = None
                for p0, p1 in lib.prange(0, nvir, blksize):
                    eri = _cp(feri['eri_mo'][p0 * nocc:p1 * nocc])
                    handler = async_do(handler, save_vir_frac, p0, p1, eri)
                for p0, p1 in lib.prange(0, nocc, blksize):
                    eri = _cp(feri['eri_mo'][(p0 + nvir) * nocc:(p1 + nvir) *
                                             nocc])
                    handler = async_do(handler, save_occ_frac, p0, p1, eri)
                if handler is not None:
                    handler.join()
            self.vvoo[:] = lib.transpose(oovv.reshape(nocc**2, -1)).reshape(
                nvir, nvir, nocc, nocc)
        log.timer('CISD integral transformation', *cput0)
Example #3
0
File: cisd.py Project: eronca/pyscf
    def __init__(self, myci, mo_coeff=None, method='incore'):
        cput0 = (time.clock(), time.time())
        moidx = numpy.ones(myci.mo_occ.size, dtype=numpy.bool)
        if isinstance(myci.frozen, (int, numpy.integer)):
            moidx[:myci.frozen] = False
        elif len(myci.frozen) > 0:
            moidx[numpy.asarray(myci.frozen)] = False
        if mo_coeff is None:
            self.mo_coeff = mo_coeff = myci.mo_coeff[:,moidx]
        else:
            self.mo_coeff = mo_coeff = mo_coeff[:,moidx]
        dm = myci._scf.make_rdm1(myci.mo_coeff, myci.mo_occ)
        fockao = myci._scf.get_hcore() + myci._scf.get_veff(myci.mol, dm)
        self.fock = reduce(numpy.dot, (mo_coeff.T, fockao, mo_coeff))

        nocc = myci.nocc
        nmo = myci.nmo
        nvir = nmo - nocc
        mem_incore, mem_outcore, mem_basic = ccsd._mem_usage(nocc, nvir)
        mem_now = lib.current_memory()[0]

        log = logger.Logger(myci.stdout, myci.verbose)
        if (method == 'incore' and myci._scf._eri is not None and
            (mem_incore+mem_now < myci.max_memory) or myci.mol.incore_anyway):
            eri1 = ao2mo.incore.full(myci._scf._eri, mo_coeff)
            #:eri1 = ao2mo.restore(1, eri1, nmo)
            #:self.oooo = eri1[:nocc,:nocc,:nocc,:nocc].copy()
            #:self.ooov = eri1[:nocc,:nocc,:nocc,nocc:].copy()
            #:self.vooo = eri1[nocc:,:nocc,:nocc,:nocc].copy()
            #:self.voov = eri1[nocc:,:nocc,:nocc,nocc:].copy()
            #:self.vvoo = eri1[nocc:,nocc:,:nocc,:nocc].copy()
            #:vovv = eri1[nocc:,:nocc,nocc:,nocc:].copy()
            #:self.vovv = lib.pack_tril(vovv.reshape(-1,nvir,nvir))
            #:self.vvvv = ao2mo.restore(4, eri1[nocc:,nocc:,nocc:,nocc:], nvir)
            nvir_pair = nvir * (nvir+1) // 2
            self.oooo = numpy.empty((nocc,nocc,nocc,nocc))
            self.ooov = numpy.empty((nocc,nocc,nocc,nvir))
            self.vooo = numpy.empty((nvir,nocc,nocc,nocc))
            self.voov = numpy.empty((nvir,nocc,nocc,nvir))
            self.vovv = numpy.empty((nvir,nocc,nvir_pair))
            self.vvvv = numpy.empty((nvir_pair,nvir_pair))
            ij = 0
            outbuf = numpy.empty((nmo,nmo,nmo))
            oovv = numpy.empty((nocc,nocc,nvir,nvir))
            for i in range(nocc):
                buf = lib.unpack_tril(eri1[ij:ij+i+1], out=outbuf[:i+1])
                for j in range(i+1):
                    self.oooo[i,j] = self.oooo[j,i] = buf[j,:nocc,:nocc]
                    self.ooov[i,j] = self.ooov[j,i] = buf[j,:nocc,nocc:]
                    oovv[i,j] = oovv[j,i] = buf[j,nocc:,nocc:]
                ij += i + 1
            self.vvoo = lib.transpose(oovv.reshape(nocc**2,-1)).reshape(nvir,nvir,nocc,nocc)
            oovv = None
            ij1 = 0
            for i in range(nocc,nmo):
                buf = lib.unpack_tril(eri1[ij:ij+i+1], out=outbuf[:i+1])
                self.vooo[i-nocc] = buf[:nocc,:nocc,:nocc]
                self.voov[i-nocc] = buf[:nocc,:nocc,nocc:]
                lib.pack_tril(_cp(buf[:nocc,nocc:,nocc:]), out=self.vovv[i-nocc])
                dij = i - nocc + 1
                lib.pack_tril(_cp(buf[nocc:i+1,nocc:,nocc:]),
                              out=self.vvvv[ij1:ij1+dij])
                ij += i + 1
                ij1 += dij
        else:
            cput1 = time.clock(), time.time()
            _tmpfile1 = tempfile.NamedTemporaryFile(dir=lib.param.TMPDIR)
            _tmpfile2 = tempfile.NamedTemporaryFile(dir=lib.param.TMPDIR)
            self.feri1 = feri1 = h5py.File(_tmpfile1.name)
            def __del__feri1(self):
                feri1.close()
            self.feri1.__del__ = __del__feri1
            orbo = mo_coeff[:,:nocc]
            orbv = mo_coeff[:,nocc:]
            nvpair = nvir * (nvir+1) // 2
            self.oooo = self.feri1.create_dataset('oooo', (nocc,nocc,nocc,nocc), 'f8')
            self.ooov = self.feri1.create_dataset('ooov', (nocc,nocc,nocc,nvir), 'f8')
            self.vvoo = self.feri1.create_dataset('vvoo', (nvir,nvir,nocc,nocc), 'f8')
            self.vooo = self.feri1.create_dataset('vooo', (nvir,nocc,nocc,nocc), 'f8')
            self.voov = self.feri1.create_dataset('voov', (nvir,nocc,nocc,nvir), 'f8')
            self.vovv = self.feri1.create_dataset('vovv', (nvir,nocc,nvpair), 'f8')
            fsort = _ccsd.libcc.CCsd_sort_inplace
            nocc_pair = nocc*(nocc+1)//2
            nvir_pair = nvir*(nvir+1)//2
            def sort_inplace(p0, p1, eri):
                fsort(eri.ctypes.data_as(ctypes.c_void_p),
                      ctypes.c_int(nocc), ctypes.c_int(nvir),
                      ctypes.c_int((p1-p0)*nocc))
                vv = eri[:,:nvir_pair]
                oo = eri[:,nvir_pair:nvir_pair+nocc_pair]
                ov = eri[:,nvir_pair+nocc_pair:].reshape(-1,nocc,nvir)
                return oo, ov, vv
            buf = numpy.empty((nmo,nmo,nmo))
            oovv = numpy.empty((nocc,nocc,nvir,nvir))
            def save_occ_frac(p0, p1, eri):
                oo, ov, vv = sort_inplace(p0, p1, eri)
                self.oooo[p0:p1] = lib.unpack_tril(oo, out=buf).reshape(p1-p0,nocc,nocc,nocc)
                self.ooov[p0:p1] = ov.reshape(p1-p0,nocc,nocc,nvir)
                oovv[p0:p1] = lib.unpack_tril(vv, out=buf).reshape(p1-p0,nocc,nvir,nvir)
            def save_vir_frac(p0, p1, eri):
                oo, ov, vv = sort_inplace(p0, p1, eri)
                self.vooo[p0:p1] = lib.unpack_tril(oo, out=buf).reshape(p1-p0,nocc,nocc,nocc)
                self.voov[p0:p1] = ov.reshape(p1-p0,nocc,nocc,nvir)
                self.vovv[p0:p1] = vv.reshape(p1-p0,nocc,-1)

            if not myci.direct:
                max_memory = max(2000,myci.max_memory-lib.current_memory()[0])
                self.feri2 = feri2 = h5py.File(_tmpfile2.name)
                def __del__feri2(self):
                    feri2.close()
                self.feri2.__del__ = __del__feri2
                ao2mo.full(myci.mol, orbv, self.feri2, max_memory=max_memory, verbose=log)
                self.vvvv = self.feri2['eri_mo']
                cput1 = log.timer_debug1('transforming vvvv', *cput1)

            tmpfile3 = tempfile.NamedTemporaryFile(dir=lib.param.TMPDIR)
            with h5py.File(tmpfile3.name, 'w') as feri:
                max_memory = max(2000, myci.max_memory-lib.current_memory()[0])
                mo = numpy.hstack((orbv, orbo))
                ao2mo.general(myci.mol, (mo,orbo,mo,mo),
                              feri, max_memory=max_memory, verbose=log)
                cput1 = log.timer_debug1('transforming oppp', *cput1)
                blksize = max(1, int(min(8e9,max_memory*.5e6)/8/nmo**2/nocc))
                handler = None
                for p0, p1 in lib.prange(0, nvir, blksize):
                    eri = _cp(feri['eri_mo'][p0*nocc:p1*nocc])
                    handler = async_do(handler, save_vir_frac, p0, p1, eri)
                for p0, p1 in lib.prange(0, nocc, blksize):
                    eri = _cp(feri['eri_mo'][(p0+nvir)*nocc:(p1+nvir)*nocc])
                    handler = async_do(handler, save_occ_frac, p0, p1, eri)
                if handler is not None:
                    handler.join()
            self.vvoo[:] = lib.transpose(oovv.reshape(nocc**2,-1)).reshape(nvir,nvir,nocc,nocc)
        log.timer('CISD integral transformation', *cput0)