def _make_Lpq(mydf, mol, auxmol): atm, bas, env, ao_loc = incore._env_and_aoloc('cint3c1e_sph', mol, auxmol) nao = ao_loc[mol.nbas] naux = ao_loc[-1] - nao nao_pair = nao * (nao+1) // 2 if mydf.metric.upper() == 'S': intor = 'cint3c1e_sph' s_aux = auxmol.intor_symmetric('cint1e_ovlp_sph') elif mydf.metric.upper() == 'T': intor = 'cint3c1e_p2_sph' s_aux = auxmol.intor_symmetric('cint1e_kin_sph') * 2 else: # metric.upper() == 'J' intor = 'cint3c2e_sph' s_aux = incore.fill_2c2e(mol, auxmol) cintopt = gto.moleintor.make_cintopt(atm, bas, env, intor) if mydf.charge_constraint: ovlp = lib.pack_tril(mol.intor_symmetric('cint1e_ovlp_sph')) aux_loc = auxmol.ao_loc_nr() s_index = numpy.hstack([range(aux_loc[i],aux_loc[i+1]) for i,l in enumerate(auxmol._bas[:,ANG_OF]) if l == 0]) a = numpy.zeros((naux+1,naux+1)) a[:naux,:naux] = s_aux a[naux,s_index] = a[s_index,naux] = 1 try: cd = scipy.linalg.cho_factor(a) def solve(Lpq): return scipy.linalg.cho_solve(cd, Lpq) except scipy.linalg.LinAlgError: def solve(Lpq): return scipy.linalg.solve(a, Lpq) else: cd = scipy.linalg.cho_factor(s_aux) def solve(Lpq): return scipy.linalg.cho_solve(cd, Lpq, overwrite_b=True) 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) return get_Lpq
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()