def general(mol, mo_coeffs, erifile, auxbasis='weigend+etb', dataname='eri_mo', tmpdir=None, int3c='int3c2e', aosym='s2ij', int2c='int2c2e', comp=1, max_memory=MAX_MEMORY, verbose=0, compact=True): ''' Transform ij of (ij|L) to MOs. ''' assert (aosym in ('s1', 's2ij')) time0 = (time.clock(), time.time()) log = logger.new_logger(mol, verbose) if tmpdir is None: tmpdir = lib.param.TMPDIR swapfile = tempfile.NamedTemporaryFile(dir=tmpdir) cholesky_eri_b(mol, swapfile.name, auxbasis, dataname, int3c, aosym, int2c, comp, max_memory, verbose=log) fswap = h5py.File(swapfile.name, 'r') time1 = log.timer('AO->MO eri transformation 1 pass', *time0) nao = mo_coeffs[0].shape[0] if aosym == 's1': nao_pair = nao * nao aosym_as_nr_e2 = 's1' else: nao_pair = nao * (nao + 1) // 2 aosym_as_nr_e2 = 's2kl' ijmosym, nij_pair, moij, ijshape = \ ao2mo.incore._conc_mos(mo_coeffs[0], mo_coeffs[1], compact and aosym != 's1') naoaux = fswap['%s/0' % dataname].shape[-2] feri = _create_h5file(erifile, dataname) if comp == 1: h5d_eri = feri.create_dataset(dataname, (naoaux, nij_pair), 'f8') else: h5d_eri = feri.create_dataset(dataname, (comp, naoaux, nij_pair), 'f8') def save(row0, row1, buf): if comp == 1: h5d_eri[row0:row1] = buf else: h5d_eri[:, row0:row1] = buf iolen = min(max(int(max_memory * .45e6 / 8 / (nao_pair + nij_pair)), 28), naoaux) totstep = (naoaux + iolen - 1) // iolen ti0 = time1 with lib.call_in_background(save) as bsave: for istep, (row0, row1) in enumerate(lib.prange(0, naoaux, iolen)): nrow = row1 - row0 log.debug('step 2 [%d/%d], [%d:%d], row = %d', istep + 1, totstep, row0, row1, nrow) buf = _load_from_h5g(fswap[dataname], row0, row1) if comp == 1: buf = _ao2mo.nr_e2(buf, moij, ijshape, aosym_as_nr_e2, ijmosym) bsave(row0, row1, buf) else: buf = _ao2mo.nr_e2(buf.reshape(comp * nrow, nao_pair), moij, ijshape, aosym_as_nr_e2, ijmosym) bsave(row0, row1, buf.reshape(comp, nrow, nij_pair)) buf = None ti0 = log.timer( 'step 2 [%d/%d], [%d:%d], row = %d' % (istep + 1, totstep, row0, row1, nrow), *ti0) fswap.close() feri.close() log.timer('AO->MO CD eri transformation 2 pass', *time1) log.timer('AO->MO CD eri transformation', *time0) return erifile
def cholesky_eri(mol, erifile, auxbasis='weigend+etb', dataname='j3c', tmpdir=None, int3c='int3c2e', aosym='s2ij', int2c='int2c2e', comp=1, max_memory=MAX_MEMORY, auxmol=None, verbose=logger.NOTE): '''3-index density-fitting tensor. ''' assert (aosym in ('s1', 's2ij')) assert (comp == 1) log = logger.new_logger(mol, verbose) time0 = (time.clock(), time.time()) if auxmol is None: auxmol = make_auxmol(mol, auxbasis) if tmpdir is None: tmpdir = lib.param.TMPDIR swapfile = tempfile.NamedTemporaryFile(dir=tmpdir) cholesky_eri_b(mol, swapfile.name, auxbasis, dataname, int3c, aosym, int2c, comp, max_memory, auxmol, verbose=log) fswap = h5py.File(swapfile.name, 'r') time1 = log.timer('generate (ij|L) 1 pass', *time0) # Cannot let naoaux = auxmol.nao_nr() if auxbasis has linear dependence nao = mol.nao_nr() if aosym == 's1': nao_pair = nao * nao else: nao_pair = nao * (nao + 1) // 2 feri = _create_h5file(erifile, dataname) if comp == 1: naoaux = fswap['%s/0' % dataname].shape[0] h5d_eri = feri.create_dataset(dataname, (naoaux, nao_pair), 'f8') else: naoaux = fswap['%s/0' % dataname].shape[1] h5d_eri = feri.create_dataset(dataname, (comp, naoaux, nao_pair), 'f8') def save(row0, row1, buf): if comp == 1: h5d_eri[row0:row1] = buf else: h5d_eri[:, row0:row1] = buf iolen = min(max(int(max_memory * .45e6 / 8 / nao_pair), 28), naoaux) totstep = (naoaux + iolen - 1) // iolen bufs1 = numpy.empty((iolen, nao_pair)) bufs2 = numpy.empty_like(bufs1) ti0 = time1 with lib.call_in_background(save) as bsave: for istep, (row0, row1) in enumerate(lib.prange(0, naoaux, iolen)): nrow = row1 - row0 buf = _load_from_h5g(fswap[dataname], row0, row1, bufs1) bufs1, bufs2 = bufs2, bufs1 bsave(row0, row1, buf) ti0 = log.timer( 'step 2 [%d/%d], [%d:%d], row = %d' % (istep + 1, totstep, row0, row1, nrow), *ti0) fswap.close() feri.close() log.timer('cholesky_eri', *time0) return erifile
def load(row_slice): row0, row1 = row_slice return _load_from_h5g(fswap[dataname], row0, row1)
def load(aux_slice): b0, b1 = aux_slice return _load_from_h5g(feri, b0, b1)
def load(start, stop, buf): if start < stop: _load_from_h5g(feri_swap, start, stop, buf)
def load(b0, b1, prefetch): prefetch[0] = _load_from_h5g(feri, b0, b1)
def _make_eris_df(cc, mo_coeff=None): eris = _RCCSD_ERIs(cc, mo_coeff) nocc = eris.nocc nmo = eris.fock.shape[0] nvir = nmo - nocc nocc_pair = nocc * (nocc + 1) // 2 nvir_pair = nvir * (nvir + 1) // 2 with_df = cc._scf.with_df naux = eris.naux = with_df.get_naoaux() eris.feri = lib.H5TmpFile() eris.oovv = eris.feri.create_dataset('oovv', (nocc, nocc, nvir, nvir), 'f8') eris.ovvv = eris.feri.create_dataset('ovvv', (nocc, nvir, nvir_pair), 'f8') eris.vvL = eris.feri.create_dataset('vvL', (nvir_pair, naux), 'f8') Loo = numpy.empty((naux, nocc, nocc)) Lov = numpy.empty((naux, nocc, nvir)) fswap = lib.H5TmpFile() mo = numpy.asarray(eris.mo_coeff, order='F') ijslice = (0, nmo, 0, nmo) Lpqbuf = numpy.empty((with_df.blockdim, nmo, nmo)) vvbuf = numpy.empty((with_df.blockdim, nvir, nvir)) p1 = 0 for k, eri1 in enumerate(with_df.loop()): Lpq = _ao2mo.nr_e2(eri1, mo, ijslice, aosym='s2', mosym='s1', out=Lpqbuf) p0, p1 = p1, p1 + Lpq.shape[0] Lpq = Lpq.reshape(p1 - p0, nmo, nmo) Loo[p0:p1] = Lpq[:, :nocc, :nocc] Lov[p0:p1] = Lpq[:, :nocc, nocc:] vvbuf[:p1 - p0] = Lpq[:, nocc:, nocc:] Lvv = lib.pack_tril(vvbuf[:p1 - p0], out=Lpqbuf) fswap[str(k)] = lib.transpose(Lvv, out=vvbuf) Lpq = Lvv = Lpqbuf = vvbuf = None Loo = Loo.reshape(naux, nocc**2) Lov = Lov.reshape(naux, nocc * nvir) eris.feri['oooo'] = lib.ddot(Loo.T, Loo).reshape(nocc, nocc, nocc, nocc) eris.feri['ooov'] = lib.ddot(Loo.T, Lov).reshape(nocc, nocc, nocc, nvir) eris.feri['ovoo'] = lib.ddot(Lov.T, Loo).reshape(nocc, nvir, nocc, nocc) eris.feri['ovov'] = lib.ddot(Lov.T, Lov).reshape(nocc, nvir, nocc, nvir) eris.oooo = eris.feri['oooo'] eris.ooov = eris.feri['ooov'] eris.ovoo = eris.feri['ovoo'] eris.ovov = eris.feri['ovov'] mem_now = lib.current_memory()[0] max_memory = cc.max_memory - mem_now blksize = max( 4, int( (max_memory * .9e6 / 8 - nocc**2 * nvir_pair) / (nocc**2 + naux))) oovv = numpy.empty((nocc, nocc, nvir_pair)) vvL = numpy.empty((blksize, naux)) for p0, p1 in lib.prange(0, nvir_pair, blksize): outcore._load_from_h5g(fswap, p0, p1, vvL) eris.vvL[p0:p1] = vvL[:p1 - p0] oovv[:, :, p0:p1] = lib.ddot(Loo.T, vvL[:p1 - p0].T).reshape(nocc, nocc, p1 - p0) oovv = lib.unpack_tril(oovv.reshape(nocc**2, nvir_pair)) eris.oovv[:] = oovv.reshape(nocc, nocc, nvir, nvir) oovv = vvL = Loo = None Lov = Lov.reshape(naux, nocc, nvir) vblk = max(nocc, int((max_memory * .8e6 / 8) / (nocc * nvir_pair))) vvblk = max(4, int((max_memory * .15e6 / 8) / (vblk * nocc + naux))) ovvv = numpy.empty((nocc, vblk, nvir_pair)) for p0, p1 in lib.prange(0, nvir, vblk): tmpLov = numpy.asarray(Lov[:, :, p0:p1].reshape(naux, -1), order='C') for q0, q1 in lib.prange(0, nvir_pair, vvblk): vvL = numpy.asarray(eris.vvL[q0:q1]) ovvv[:, :p1 - p0, q0:q1] = lib.ddot(tmpLov.T, vvL.T).reshape(nocc, p1 - p0, q1 - q0) vvL = None eris.ovvv[:, p0:p1] = ovvv[:, :p1 - p0] return eris