def test_aux_e2_diff_bra_ket(self): mol1 = mol.copy() mol1.basis = 'sto3g' mol1.build(0, 0, verbose=0) atm1, bas1, env1 = gto.conc_env(atm, bas, env, mol1._atm, mol1._bas, mol1._env) ao_loc = gto.moleintor.make_loc(bas1, 'cint3c2e_sph') shls_slice = (0, mol.nbas, mol.nbas+auxmol.nbas, mol.nbas+auxmol.nbas+mol1.nbas, mol.nbas, mol.nbas+auxmol.nbas) j3c = _ri.nr_auxe2('cint3c2e_sph', atm1, bas1, env1, shls_slice, ao_loc, 's1', 1) nao = mol.nao_nr() naoj = mol1.nao_nr() naoaux = auxmol.nao_nr() eri0 = numpy.empty((nao,naoj,naoaux)) pi = 0 for i in range(mol.nbas): pj = 0 for j in range(mol.nbas+auxmol.nbas, len(bas1)): pk = 0 for k in range(mol.nbas, mol.nbas+auxmol.nbas): shls = (i, j, k) buf = gto.moleintor.getints_by_shell('cint3c2e_sph', shls, atm1, bas1, env1) di, dj, dk = buf.shape eri0[pi:pi+di,pj:pj+dj,pk:pk+dk] = buf pk += dk pj += dj pi += di self.assertTrue(numpy.allclose(eri0.reshape(-1,naoaux), j3c))
def aux_e2(mol, auxmol, intor='cint3c2e_sph', aosym='s1', comp=1, out=None): '''3-center AO integrals (ij|L), where L is the auxiliary basis. ''' atm, bas, env, ao_loc = _env_and_aoloc(intor, mol, auxmol) shls_slice = (0, mol.nbas, 0, mol.nbas, mol.nbas, mol.nbas+auxmol.nbas) return _ri.nr_auxe2(intor, atm, bas, env, shls_slice, ao_loc, aosym, comp, out=out)
def test_aux_e2_diff_bra_ket(self): mol1 = mol.copy() mol1.basis = 'sto3g' mol1.build(0, 0, verbose=0) atm1, bas1, env1 = gto.conc_env(atm, bas, env, mol1._atm, mol1._bas, mol1._env) ao_loc = gto.moleintor.make_loc(bas1, 'cint3c2e_sph') shls_slice = (0, mol.nbas, mol.nbas + auxmol.nbas, mol.nbas + auxmol.nbas + mol1.nbas, mol.nbas, mol.nbas + auxmol.nbas) j3c = _ri.nr_auxe2('cint3c2e_sph', atm1, bas1, env1, shls_slice, ao_loc, 's1', 1) nao = mol.nao_nr() naoj = mol1.nao_nr() naoaux = auxmol.nao_nr() eri0 = numpy.empty((nao, naoj, naoaux)) pi = 0 for i in range(mol.nbas): pj = 0 for j in range(mol.nbas + auxmol.nbas, len(bas1)): pk = 0 for k in range(mol.nbas, mol.nbas + auxmol.nbas): shls = (i, j, k) buf = gto.moleintor.getints_by_shell( 'cint3c2e_sph', shls, atm1, bas1, env1) di, dj, dk = buf.shape eri0[pi:pi + di, pj:pj + dj, pk:pk + dk] = buf pk += dk pj += dj pi += di self.assertTrue(numpy.allclose(eri0.reshape(-1, naoaux), j3c))
def get_Lpq(shls_slice, col0, col1, buf): # Be cautious here, _ri.nr_auxe2 assumes buf in F-order Lpq = _ri.nr_auxe2(intor, atm, bas, env, shls_slice, ao_loc, 's2ij', 1, cintopt, buf).T if mydf.charge_constraint: Lpq = numpy.ndarray(shape=(naux+1,col1-col0), buffer=buf) Lpq[naux,:] = ovlp[col0:col1] Lpq1 = solve(Lpq) assert(Lpq1.flags.f_contiguous) lib.transpose(Lpq1.T, out=Lpq) return Lpq[:naux] else: return solve(Lpq)
def aux_e2(mol, auxmol, intor='cint3c2e_sph', aosym='s1', comp=1, out=None): '''3-center AO integrals (ij|L), where L is the auxiliary basis. ''' atm, bas, env, ao_loc = _env_and_aoloc(intor, mol, auxmol) shls_slice = (0, mol.nbas, 0, mol.nbas, mol.nbas, mol.nbas + auxmol.nbas) return _ri.nr_auxe2(intor, atm, bas, env, shls_slice, ao_loc, aosym, comp, out=out)
def aux_e2(mol, auxmol, intor="cint3c2e_sph", aosym="s1", comp=1, hermi=0, mol1=None): """3-center AO integrals (ij|L), where L is the auxiliary basis. """ atm, bas, env = gto.mole.conc_env(mol._atm, mol._bas, mol._env, auxmol._atm, auxmol._bas, auxmol._env) if "cart" in intor: iloc = jloc = _ri.make_loc(0, mol.nbas, _ri._cgto_cart(bas)) else: iloc = jloc = _ri.make_loc(0, mol.nbas, _ri._cgto_spheric(bas)) if mol1 is None: basrange = (0, mol.nbas, 0, mol.nbas, mol.nbas, auxmol.nbas) else: # Append mol1 next to auxmol atm, bas, env = gto.mole.conc_env(atm, bas, env, mol1._atm, mol1._bas, mol1._env) basrange = (0, mol.nbas, mol.nbas + auxmol.nbas, mol1.nbas, mol.nbas, auxmol.nbas) jloc = None eri = _ri.nr_auxe2(intor, basrange, atm, bas, env, aosym, comp, iloc=iloc, jloc=jloc) return eri
def aux_e2(mol, auxmol, intor='cint3c2e_sph', aosym='s1', comp=1, hermi=0, mol1=None): '''3-center AO integrals (ij|L), where L is the auxiliary basis. ''' atm, bas, env = gto.mole.conc_env(mol._atm, mol._bas, mol._env, auxmol._atm, auxmol._bas, auxmol._env) if 'cart' in intor: iloc = jloc = _ri.make_loc(0, mol.nbas, bas, True) else: iloc = jloc = _ri.make_loc(0, mol.nbas, bas) if mol1 is None: basrange = (0, mol.nbas, 0, mol.nbas, mol.nbas, auxmol.nbas) else: # Append mol1 next to auxmol atm, bas, env = gto.mole.conc_env(atm, bas, env, mol1._atm, mol1._bas, mol1._env) basrange = (0, mol.nbas, mol.nbas+auxmol.nbas, mol1.nbas, mol.nbas, auxmol.nbas) jloc = _ri.make_loc(0, mol1.nbas, mol1._bas) eri = _ri.nr_auxe2(intor, basrange, atm, bas, env, aosym, comp, iloc=iloc, jloc=jloc) return eri
def cholesky_eri_b(mol, erifile, auxbasis='weigend+etb', dataname='eri_mo', int3c='cint3c2e_sph', aosym='s2ij', int2c='cint2c2e_sph', comp=1, ioblk_size=256, verbose=logger.NOTE): '''3-center 2-electron AO integrals ''' assert(aosym in ('s1', 's2ij')) assert(comp == 1) time0 = (time.clock(), time.time()) if isinstance(verbose, logger.Logger): log = verbose else: log = logger.Logger(mol.stdout, verbose) auxmol = incore.format_aux_basis(mol, auxbasis) j2c = incore.fill_2c2e(mol, auxmol, intor=int2c) log.debug('size of aux basis %d', j2c.shape[0]) time1 = log.timer('2c2e', *time0) low = scipy.linalg.cholesky(j2c, lower=True) j2c = None time1 = log.timer('Cholesky 2c2e', *time1) if h5py.is_hdf5(erifile): feri = h5py.File(erifile) if dataname in feri: del(feri[dataname]) else: feri = h5py.File(erifile, 'w') if comp > 1: for icomp in range(comp): feri.create_group(str(icomp)) # for h5py old version atm, bas, env = \ pyscf.gto.mole.conc_env(mol._atm, mol._bas, mol._env, auxmol._atm, auxmol._bas, auxmol._env) c_atm = numpy.asarray(atm, dtype=numpy.int32, order='C') c_bas = numpy.asarray(bas, dtype=numpy.int32, order='C') c_env = numpy.asarray(env, dtype=numpy.double, order='C') natm = ctypes.c_int(mol.natm+auxmol.natm) nbas = ctypes.c_int(mol.nbas) if 'ssc' in int3c: ao_loc = _ri.make_loc(0, mol.nbas, bas) kloc = _ri.make_loc(mol.nbas, auxmol.nbas, bas, True) elif 'cart' in int3c: ao_loc = _ri.make_loc(0, mol.nbas, bas, True) kloc = _ri.make_loc(mol.nbas, auxmol.nbas, bas, True) else: ao_loc = _ri.make_loc(0, mol.nbas, bas) kloc = _ri.make_loc(mol.nbas, auxmol.nbas, bas) nao = ao_loc[-1] - ao_loc[0] naoaux = kloc[-1] - kloc[0] if aosym == 's1': nao_pair = nao * nao buflen = min(max(int(ioblk_size*1e6/8/naoaux/comp), 1), nao_pair) shranges = _guess_shell_ranges(mol, buflen, 's1') else: nao_pair = nao * (nao+1) // 2 buflen = min(max(int(ioblk_size*1e6/8/naoaux/comp), 1), nao_pair) shranges = _guess_shell_ranges(mol, buflen, 's2ij') log.debug('erifile %.8g MB, IO buf size %.8g MB', naoaux*nao_pair*8/1e6, comp*buflen*naoaux*8/1e6) if log.verbose >= logger.DEBUG1: log.debug1('shranges = %s', shranges) cintopt = _vhf.make_cintopt(c_atm, c_bas, c_env, int3c) bufs1 = numpy.empty((comp*max([x[2] for x in shranges]),naoaux)) for istep, sh_range in enumerate(shranges): log.debug('int3c2e [%d/%d], AO [%d:%d], nrow = %d', \ istep+1, len(shranges), *sh_range) bstart, bend, nrow = sh_range basrange = (bstart, bend-bstart, 0, mol.nbas, mol.nbas, auxmol.nbas) buf = bufs1[:comp*nrow].reshape(comp,nrow,naoaux) if 's1' in aosym: ijkoff = ao_loc[bstart] * nao * naoaux else: ijkoff = ao_loc[bstart] * (ao_loc[bstart]+1) // 2 * naoaux _ri.nr_auxe2(int3c, basrange, atm, bas, env, aosym, comp, cintopt, buf, ijkoff, ao_loc[bend]-ao_loc[bstart], nao, naoaux, ao_loc[bstart:bend+1], ao_loc, kloc) for icomp in range(comp): if comp == 1: label = '%s/%d'%(dataname,istep) else: label = '%s/%d/%d'%(dataname,icomp,istep) cderi = scipy.linalg.solve_triangular(low, buf[icomp].T, lower=True, overwrite_b=True) feri[label] = cderi time1 = log.timer('gen CD eri [%d/%d]' % (istep+1,len(shranges)), *time1) buf = bufs1 = None feri.close() _ri.libri.CINTdel_optimizer(ctypes.byref(cintopt)) return erifile
def cholesky_eri_b(mol, erifile, auxbasis='weigend+etb', dataname='eri_mo', int3c='cint3c2e_sph', aosym='s2ij', int2c='cint2c2e_sph', comp=1, ioblk_size=256, verbose=logger.NOTE): '''3-center 2-electron AO integrals ''' assert(aosym in ('s1', 's2ij')) time0 = (time.clock(), time.time()) if isinstance(verbose, logger.Logger): log = verbose else: log = logger.Logger(mol.stdout, verbose) auxmol = incore.format_aux_basis(mol, auxbasis) j2c = incore.fill_2c2e(mol, auxmol, intor=int2c) log.debug('size of aux basis %d', j2c.shape[0]) time1 = log.timer('2c2e', *time0) low = scipy.linalg.cholesky(j2c, lower=True) j2c = None time1 = log.timer('Cholesky 2c2e', *time1) if h5py.is_hdf5(erifile): feri = h5py.File(erifile) if dataname in feri: del(feri[dataname]) else: feri = h5py.File(erifile, 'w') for icomp in range(comp): feri.create_group('%s/%d'%(dataname,icomp)) # for h5py old version def store(b, label): cderi = scipy.linalg.solve_triangular(low, b, lower=True, overwrite_b=True) if cderi.flags.f_contiguous: cderi = lib.transpose(cderi.T) feri[label] = cderi atm, bas, env, ao_loc = incore._env_and_aoloc(int3c, mol, auxmol) nao = ao_loc[mol.nbas] naoaux = ao_loc[-1] - nao if aosym == 's1': nao_pair = nao * nao buflen = min(max(int(ioblk_size*1e6/8/naoaux/comp), 1), nao_pair) shranges = _guess_shell_ranges(mol, buflen, 's1') else: nao_pair = nao * (nao+1) // 2 buflen = min(max(int(ioblk_size*1e6/8/naoaux/comp), 1), nao_pair) shranges = _guess_shell_ranges(mol, buflen, 's2ij') log.debug('erifile %.8g MB, IO buf size %.8g MB', naoaux*nao_pair*8/1e6, comp*buflen*naoaux*8/1e6) if log.verbose >= logger.DEBUG1: log.debug1('shranges = %s', shranges) cintopt = gto.moleintor.make_cintopt(atm, bas, env, int3c) bufs1 = numpy.empty((comp*max([x[2] for x in shranges]),naoaux)) for istep, sh_range in enumerate(shranges): log.debug('int3c2e [%d/%d], AO [%d:%d], nrow = %d', \ istep+1, len(shranges), *sh_range) bstart, bend, nrow = sh_range shls_slice = (bstart, bend, 0, mol.nbas, mol.nbas, mol.nbas+auxmol.nbas) buf = _ri.nr_auxe2(int3c, atm, bas, env, shls_slice, ao_loc, aosym, comp, cintopt, bufs1) if comp == 1: store(buf.T, '%s/0/%d'%(dataname,istep)) else: for icomp in range(comp): store(buf[icomp].T, '%s/%d/%d'%(dataname,icomp,istep)) time1 = log.timer('gen CD eri [%d/%d]' % (istep+1,len(shranges)), *time1) buf = bufs1 = None feri.close() return erifile
def _make_j3c(mydf, mol, auxmol): log = logger.Logger(mydf.stdout, mydf.verbose) atm, bas, env, ao_loc = incore._env_and_aoloc('cint3c2e_sph', mol, auxmol) nao = ao_loc[mol.nbas] naux = ao_loc[-1] - nao nao_pair = nao * (nao+1) // 2 cintopt = gto.moleintor.make_cintopt(atm, bas, env, 'cint3c2e_sph') if mydf.approx_sr_level == 0: get_Lpq = _make_Lpq(mydf, mol, auxmol) else: get_Lpq = _make_Lpq_atomic_approx(mydf, mol, auxmol) feri = h5py.File(mydf._cderi) chunks = (min(256,naux), min(256,nao_pair)) # 512K feri.create_dataset('j3c', (naux,nao_pair), 'f8', chunks=chunks) feri.create_dataset('Lpq', (naux,nao_pair), 'f8', chunks=chunks) def save(label, dat, col0, col1): feri[label][:,col0:col1] = dat Gv, Gvbase, kws = non_uniform_kgrids(mydf.gs) nxyz = [i*2 for i in mydf.gs] gxyz = lib.cartesian_prod([range(i) for i in nxyz]) kk = numpy.einsum('ki,ki->k', Gv, Gv) idx = numpy.argsort(kk)[::-1] kk = kk[idx] Gv = Gv[idx] kws = kws[idx] gxyz = gxyz[idx] coulG = .5/numpy.pi**2 * kws / kk aoaux = ft_ao.ft_ao(auxmol, Gv, None, Gvbase, gxyz, nxyz) kLR = numpy.asarray(aoaux.real, order='C') kLI = numpy.asarray(aoaux.imag, order='C') j2c = auxmol.intor('cint2c2e_sph', hermi=1).T # .T to C-ordr lib.dot(kLR.T*coulG, kLR, -1, j2c, 1) lib.dot(kLI.T*coulG, kLI, -1, j2c, 1) kLR *= coulG.reshape(-1,1) kLI *= coulG.reshape(-1,1) aoaux = coulG = kk = kws = idx = None max_memory = max(2000, mydf.max_memory-lib.current_memory()[0]) buflen = min(max(int(max_memory*.3*1e6/8/naux), 1), nao_pair) shranges = outcore._guess_shell_ranges(mol, buflen, 's2ij') buflen = max([x[2] for x in shranges]) blksize = max(16, int(max_memory*.15*1e6/16/buflen)) pqkbuf = numpy.empty(buflen*blksize) bufs1 = numpy.empty((buflen*naux)) # bufs2 holds either Lpq and ft_aopair bufs2 = numpy.empty(max(buflen*(naux+1),buflen*blksize*2)) # *2 for cmplx col1 = 0 for istep, sh_range in enumerate(shranges): log.debug('int3c2e [%d/%d], AO [%d:%d], ncol = %d', \ istep+1, len(shranges), *sh_range) bstart, bend, ncol = sh_range col0, col1 = col1, col1+ncol shls_slice = (bstart, bend, 0, bend, mol.nbas, mol.nbas+auxmol.nbas) Lpq = get_Lpq(shls_slice, col0, col1, bufs2) save('Lpq', Lpq, col0, col1) j3c = _ri.nr_auxe2('cint3c2e_sph', atm, bas, env, shls_slice, ao_loc, 's2ij', 1, cintopt, bufs1) j3c = j3c.T # -> (L|pq) in C-order lib.dot(j2c, Lpq, -.5, j3c, 1) Lpq = None for p0, p1 in lib.prange(0, Gv.shape[0], blksize): aoao = ft_ao.ft_aopair(mol, Gv[p0:p1], shls_slice[:4], 's2', Gvbase, gxyz[p0:p1], nxyz, buf=bufs2) nG = p1 - p0 pqkR = numpy.ndarray((ncol,nG), buffer=pqkbuf) pqkR[:] = aoao.real.T lib.dot(kLR[p0:p1].T, pqkR.T, -1, j3c, 1) pqkI = numpy.ndarray((ncol,nG), buffer=pqkbuf) pqkI[:] = aoao.imag.T lib.dot(kLI[p0:p1].T, pqkI.T, -1, j3c, 1) aoao = aoaux = None save('j3c', j3c, col0, col1) feri.close()