def _make_eris_outcore(mycc, mo_coeff=None): cput0 = (logger.process_clock(), logger.perf_counter()) log = logger.Logger(mycc.stdout, mycc.verbose) eris = _ChemistsERIs() eris._common_init_(mycc, mo_coeff) mol = mycc.mol mo_coeff = eris.mo_coeff nocc = eris.nocc nao, nmo = mo_coeff.shape nvir = nmo - nocc eris.feri1 = lib.H5TmpFile() eris.oooo = eris.feri1.create_dataset('oooo', (nocc, nocc, nocc, nocc), 'f8') eris.ovoo = eris.feri1.create_dataset('ovoo', (nocc, nvir, nocc, nocc), 'f8', chunks=(nocc, 1, nocc, nocc)) eris.ovov = eris.feri1.create_dataset('ovov', (nocc, nvir, nocc, nvir), 'f8', chunks=(nocc, 1, nocc, nvir)) eris.ovvo = eris.feri1.create_dataset('ovvo', (nocc, nvir, nvir, nocc), 'f8', chunks=(nocc, 1, nvir, nocc)) eris.ovvv = eris.feri1.create_dataset('ovvv', (nocc, nvir, nvir, nvir), 'f8') eris.oovv = eris.feri1.create_dataset('oovv', (nocc, nocc, nvir, nvir), 'f8', chunks=(nocc, nocc, 1, nvir)) max_memory = max(MEMORYMIN, mycc.max_memory - lib.current_memory()[0]) ftmp = lib.H5TmpFile() ao2mo.full(mol, mo_coeff, ftmp, max_memory=max_memory, verbose=log) eri = ftmp['eri_mo'] nocc_pair = nocc * (nocc + 1) // 2 tril2sq = lib.square_mat_in_trilu_indices(nmo) oo = eri[:nocc_pair] eris.oooo[:] = ao2mo.restore(1, oo[:, :nocc_pair], nocc) oovv = lib.take_2d(oo, tril2sq[:nocc, :nocc].ravel(), tril2sq[nocc:, nocc:].ravel()) eris.oovv[:] = oovv.reshape(nocc, nocc, nvir, nvir) oo = oovv = None tril2sq = lib.square_mat_in_trilu_indices(nmo) blksize = min(nvir, max(BLKMIN, int(max_memory * 1e6 / 8 / nmo**3 / 2))) for p0, p1 in lib.prange(0, nvir, blksize): q0, q1 = p0 + nocc, p1 + nocc off0 = q0 * (q0 + 1) // 2 off1 = q1 * (q1 + 1) // 2 buf = lib.unpack_tril(eri[off0:off1]) tmp = buf[tril2sq[q0:q1, :nocc] - off0] eris.ovoo[:, p0:p1] = tmp[:, :, :nocc, :nocc].transpose(1, 0, 2, 3) eris.ovvo[:, p0:p1] = tmp[:, :, nocc:, :nocc].transpose(1, 0, 2, 3) eris.ovov[:, p0:p1] = tmp[:, :, :nocc, nocc:].transpose(1, 0, 2, 3) eris.ovvv[:, p0:p1] = tmp[:, :, nocc:, nocc:].transpose(1, 0, 2, 3) buf = tmp = None log.timer('GW integral transformation', *cput0) return eris
def create_t3_eris(mycc, kconserv, eris, tmpfile='tmp_t3_eris.h5'): '''Create/transpose necessary eri integrals needed for fast read-in by CCSD(T).''' eris_vovv, eris_oovv, eris_ooov, t2 = eris nkpts = mycc.nkpts nocc = mycc.nocc nmo = mycc.nmo nvir = nmo - nocc nmo = nocc + nvir feri_tmp = None h5py_kwargs = {} feri_tmp_filename = tmpfile dtype = np.result_type(eris_vovv, eris_oovv, eris_ooov, t2) if not check_read_success(feri_tmp_filename): feri_tmp = lib.H5TmpFile(feri_tmp_filename, 'w', **h5py_kwargs) t2T_out = feri_tmp.create_dataset( 't2T', (nkpts, nkpts, nkpts, nvir, nvir, nocc, nocc), dtype=dtype) # noqa: E501 eris_vvop_out = feri_tmp.create_dataset( 'vvop', (nkpts, nkpts, nkpts, nvir, nvir, nocc, nmo), dtype=dtype) # noqa: E501 eris_vooo_C_out = feri_tmp.create_dataset( 'vooo_C', (nkpts, nkpts, nkpts, nvir, nocc, nocc, nocc), dtype=dtype) # noqa: E501 transpose_t2(t2, nkpts, nocc, nvir, kconserv, out=t2T_out) create_eris_vvop(eris_vovv, eris_oovv, nkpts, nocc, nvir, kconserv, out=eris_vvop_out) create_eris_vooo(eris_ooov, nkpts, nocc, nvir, kconserv, out=eris_vooo_C_out) feri_tmp.attrs['completed'] = True feri_tmp.close() feri_tmp = lib.H5TmpFile(feri_tmp_filename, 'r', **h5py_kwargs) t2T = feri_tmp['t2T'] eris_vvop = feri_tmp['vvop'] eris_vooo_C = feri_tmp['vooo_C'] mem_now = lib.current_memory()[0] max_memory = max(0, mycc.max_memory - mem_now) unit = nkpts**3 * (nvir**2 * nocc**2 + nvir**2 * nmo * nocc + nvir * nocc**3) if (unit * 16 < max_memory): # Store all in memory t2T = t2T[:] eris_vvop = eris_vvop[:] eris_vooo_C = eris_vooo_C[:] return feri_tmp, t2T, eris_vvop, eris_vooo_C
def _store(self, key, value): if self._diisfile is None: if isinstance(self.filename, str): filename = self.filename + '__rank' + str(mpi.rank) self._diisfile = lib.H5TmpFile(filename, 'w') elif not (self.incore or value.size < lib.diis.INCORE_SIZE): self._diisfile = lib.H5TmpFile(self.filename, 'w') return lib.diis.DIIS._store(self, key, value)
def test_r_outcore_eri(self): n2c = mol.nao_2c() numpy.random.seed(1) mo = numpy.random.random((n2c,n2c)) + numpy.random.random((n2c,n2c))*1j eriref = trans(eri0, [mo]*4) ftmp = tempfile.NamedTemporaryFile() ao2mo.kernel(mol, mo, erifile=ftmp.name, intor='int2e_spinor', max_memory=10, ioblk_size=5) with ao2mo.load(ftmp) as eri1: self.assertAlmostEqual(lib.finger(eri1), -550.72966498073129-1149.3561026721848j, 8) self.assertAlmostEqual(abs(eri1-eriref.reshape(n2c**2,n2c**2)).max(), 0, 9) eri1 = ao2mo.kernel(mol, (mo[:,:2], mo[:,:4], mo[:,:2], mo[:,:4]), erifile=ftmp.name, intor='int2e_spinor') with ao2mo.load(ftmp) as eri1: self.assertAlmostEqual(abs(eri1-eriref[:2,:4,:2,:4].reshape(8,8)).max(), 0, 9) ftmp = lib.H5TmpFile() ao2mo.kernel(mol, (mo[:,:2], mo[:,:4], mo[:,:2], mo[:,:4]), erifile=ftmp, intor='int2e_spinor', aosym='s1') with ao2mo.load(ftmp) as eri1: self.assertAlmostEqual(abs(eri1-eriref[:2,:4,:2,:4].reshape(8,8)).max(), 0, 9) eri1 = ao2mo.kernel(mol, (mo[:,:2], mo[:,:4], mo[:,:4], mo[:,:2]), intor='int2e_spinor', aosym='s2ij') self.assertAlmostEqual(abs(eri1-eriref[:2,:4,:4,:2].reshape(8,8)).max(), 0, 9) eri1 = ao2mo.kernel(mol, (mo[:,:2], mo[:,:4], mo[:,:2], mo[:,:4]), intor='int2e_spinor', aosym='s2kl') self.assertAlmostEqual(abs(eri1-eriref[:2,:4,:2,:4].reshape(8,8)).max(), 0, 9) eri1 = ao2mo.kernel(mol, mo[:,:0], intor='int2e_spinor') self.assertTrue(eri1.size == 0)
def _gamma2_intermediates(mycc, t1, t2, l1, l2, compress_vvvv=False): f = lib.H5TmpFile() _gamma2_outcore(mycc, t1, t2, l1, l2, f, compress_vvvv) d2 = (f['dovov'].value, f['dvvvv'].value, f['doooo'].value, f['doovv'].value, f['dovvo'].value, None, f['dovvv'].value, f['dooov'].value) return d2
def grad_elec(cc_grad, t1=None, t2=None, l1=None, l2=None, eris=None, atmlst=None, verbose=lib.logger.INFO): mycc = cc_grad.base if t1 is None: t1 = mycc.t1 if t2 is None: t2 = mycc.t2 if l1 is None: l1 = mycc.l1 if l2 is None: l2 = mycc.l2 if eris is None: eris = mycc.ao2mo() d1 = uccsd_t_rdm._gamma1_intermediates(mycc, t1, t2, l1, l2, eris, for_grad=True) fd2intermediate = lib.H5TmpFile() d2 = uccsd_t_rdm._gamma2_outcore(mycc, t1, t2, l1, l2, eris, fd2intermediate, True) cc_grad = uccsd_grad.Gradients(mycc) de = uccsd_grad.grad_elec(cc_grad, t1, t2, l1, l2, eris, atmlst, d1, d2, verbose) return de
def _make_mo_eris_outcore(agf2, mo_coeff=None): ''' Returns _ChemistsERIs ''' cput0 = (logger.process_clock(), logger.perf_counter()) log = logger.Logger(agf2.stdout, agf2.verbose) eris = _ChemistsERIs() eris._common_init_(agf2, mo_coeff) mol = agf2.mol mo_coeff = np.asarray(eris.mo_coeff, order='F') eris.feri = lib.H5TmpFile() ao2mo.outcore.full(mol, mo_coeff, eris.feri, dataname='mo', max_memory=agf2.max_memory, verbose=log) eris.eri = eris.feri['mo'] log.timer('MO integral transformation', *cput0) return eris
def setweights(self): logger.info(self, 'Getting atomic data and weigths from tabulated densities') npoints = len(self.grids.weights) output = numpy.zeros(npoints) promol = numpy.zeros(npoints) ftmp = lib.H5TmpFile() rhoat = 0.0 for i in range(self.natm): libaim.eval_atomic(ctypes.c_int(npoints), ctypes.c_int(self.charges[i]), self.coords[i].ctypes.data_as(ctypes.c_void_p), self.grids.coords.ctypes.data_as(ctypes.c_void_p), output.ctypes.data_as(ctypes.c_void_p)) h5dat = ftmp.create_dataset('atom' + str(i), (npoints, ), 'f8') h5dat[:] = output[:] rhoa = numpy.einsum('i,i->', output, self.grids.weights) rhoat += rhoa promol += output logger.info(self, 'Integral of rho for atom %d %f' % (i, rhoa)) logger.info(self, 'Integral of rho promolecular %f' % rhoat) for i in range(self.natm): h5dat = ftmp.create_dataset('weight' + str(i), (npoints, ), 'f8') h5dat[:] = ftmp['atom' + str(i)][:] / (promol + HMINIMAL) return ftmp
def _make_mo_eris_outcore(agf2, mo_coeff=None): ''' Returns _ChemistsERIs ''' cput0 = (time.clock(), time.time()) log = logger.Logger(agf2.stdout, agf2.verbose) eris = _ChemistsERIs() eris._common_init_(agf2, mo_coeff) mol = agf2.mol moa = np.asarray(eris.mo_coeff[0], order='F') mob = np.asarray(eris.mo_coeff[1], order='F') naoa, naob = moa.shape[0], mob.shape[0] nmoa, nmob = eris.nmo eris.feri = lib.H5TmpFile() ao2mo.outcore.full(mol, moa, eris.feri, dataname='mo/aa') ao2mo.outcore.full(mol, mob, eris.feri, dataname='mo/bb') ao2mo.outcore.general(mol, (moa,moa,mob,mob), eris.feri, dataname='mo/ab', verbose=log) ao2mo.outcore.general(mol, (mob,mob,moa,moa), eris.feri, dataname='mo/ba', verbose=log) eris.eri_aa = eris.feri['mo/aa'] eris.eri_ab = eris.feri['mo/ab'] eris.eri_ba = eris.feri['mo/ba'] eris.eri_bb = eris.feri['mo/bb'] eris.eri = ((eris.eri_aa, eris.eri_ab), (eris.eri_ba, eris.eri_bb)) return eris
def _make_eris_outcore(mp, mo_coeff=None, verbose=None): cput0 = (time.clock(), time.time()) log = logger.Logger(mp.stdout, mp.verbose) eris = _PhysicistsERIs(mp, mo_coeff) nocc = mp.nocc nao, nmo = eris.mo_coeff.shape nvir = nmo - nocc orbo = eris.mo_coeff[:, :nocc] orbv = eris.mo_coeff[:, nocc:] fswap = eris.feri = lib.H5TmpFile() eris.oovv = fswap.create_dataset('oovv', (nocc, nocc, nvir, nvir), 'c8') max_memory = mp.max_memory - lib.current_memory()[0] blksize = min(nocc, max(2, int(max_memory * 1e6 / 8 / (nocc * nvir**2 * 2)))) max_memory = max(2000, max_memory) ao2mo.kernel(mp.mol, (orbo, orbv, orbo, orbv), fswap, max_memory=max_memory, verbose=log, intor='int2e_spinor') for p0, p1 in lib.prange(0, nocc, blksize): tmp = numpy.asarray(fswap['eri_mo'][p0 * nvir:p1 * nvir]) tmp = tmp.reshape(p1 - p0, nvir, nocc, nvir) eris.oovv[p0:p1] = tmp.transpose(0, 2, 1, 3) - tmp.transpose( 0, 2, 3, 1) tmp = None cput0 = log.timer_debug1('transforming oovv', *cput0) return eris
def __init__(self, casscf, mo, method='incore', level=1): mol = casscf.mol nao, nmo = mo.shape ncore = casscf.ncore ncas = casscf.ncas dm_core = numpy.dot(mo[:,:ncore], mo[:,:ncore].T) vj, vk = casscf._scf.get_jk(mol, dm_core) self.vhf_c = reduce(numpy.dot, (mo.T, vj*2-vk, mo)) mem_incore, mem_outcore, mem_basic = _mem_usage(ncore, ncas, nmo) mem_now = lib.current_memory()[0] eri = casscf._scf._eri if (method == 'incore' and eri is not None and (mem_incore+mem_now < casscf.max_memory*.9) or mol.incore_anyway): if eri is None: eri = mol.intor('int2e', aosym='s8') self.j_pc, self.k_pc, self.ppaa, self.papa = \ trans_e1_incore(eri, mo, ncore, ncas) else: import gc gc.collect() log = logger.Logger(casscf.stdout, casscf.verbose) self.feri = lib.H5TmpFile() max_memory = max(3000, casscf.max_memory*.9-mem_now) if max_memory < mem_basic: log.warn('Calculation needs %d MB memory, over CASSCF.max_memory (%d MB) limit', (mem_basic+mem_now)/.9, casscf.max_memory) self.j_pc, self.k_pc = \ trans_e1_outcore(mol, mo, ncore, ncas, self.feri, max_memory=max_memory, level=level, verbose=log) self.ppaa = self.feri['ppaa'] self.papa = self.feri['papa']
def kernel(mycc, t1=None, t2=None, l1=None, l2=None, eris=None, atmlst=None, mf_grad=None, verbose=lib.logger.INFO): if t1 is None: t1 = mycc.t1 if t2 is None: t2 = mycc.t2 if l1 is None: l1 = mycc.l1 if l2 is None: l2 = mycc.l2 d1 = uccsd_t_rdm._gamma1_intermediates(mycc, t1, t2, l1, l2, eris, for_grad=True) fd2intermediate = lib.H5TmpFile() d2 = uccsd_t_rdm._gamma2_outcore(mycc, t1, t2, l1, l2, eris, fd2intermediate, True) return uccsd_grad.kernel(mycc, t1, t2, l1, l2, eris, atmlst, mf_grad, d1, d2, verbose)
def _gamma2_intermediates(myci, civec, nmo, nocc, compress_vvvv=False): f = lib.H5TmpFile() _gamma2_outcore(myci, civec, nmo, nocc, f, compress_vvvv) d2 = (f['dovov'].value, f['dvvvv'].value, f['doooo'].value, f['doovv'].value, f['dovvo'].value, None, f['dovvv'].value, f['dooov'].value) return d2
def test_mp2_ao2mo_ovov(self): pt = mp.mp2.MP2(mf) orbo = mf.mo_coeff[:, :8] orbv = mf.mo_coeff[:, 8:] ftmp = lib.H5TmpFile() h5dat = mp.mp2._ao2mo_ovov(pt, orbo, orbv, ftmp, 1) ovov = numpy.asarray(h5dat) ovov_ref = ao2mo.general(mf._eri, (orbo, orbv, orbo, orbv)) self.assertAlmostEqual(numpy.linalg.norm(ovov_ref - ovov), 0, 9)
def start(self, interval=0.02): mycc = self._cc log = logger.new_logger(mycc) cpu1 = (logger.process_clock(), logger.perf_counter()) eris = mycc._eris t2T = mycc.t2.transpose(2, 3, 0, 1) nocc, nvir = mycc.t1.shape nmo = nocc + nvir vloc0, vloc1 = self.vranges[rank] nvir_seg = vloc1 - vloc0 max_memory = min(24000, mycc.max_memory - lib.current_memory()[0]) blksize = min( nvir_seg // 4 + 1, max(16, int(max_memory * .3e6 / 8 / (nvir * nocc * nmo)))) self.eri_tmp = lib.H5TmpFile() vvop = self.eri_tmp.create_dataset('vvop', (nvir_seg, nvir, nocc, nmo), 'f8') def save_vvop(j0, j1, vvvo): buf = numpy.empty((j1 - j0, nvir, nocc, nmo), dtype=t2T.dtype) buf[:, :, :, :nocc] = eris.ovov[:, j0:j1].conj().transpose( 1, 3, 0, 2) for k, (q0, q1) in enumerate(self.vranges): blk = vvvo[k].reshape(q1 - q0, nvir, j1 - j0, nocc) buf[:, q0:q1, :, nocc:] = blk.transpose(2, 0, 3, 1) vvop[j0:j1] = buf with lib.call_in_background(save_vvop) as save_vvop: for p0, p1 in mpi.prange(vloc0, vloc1, blksize): j0, j1 = p0 - vloc0, p1 - vloc0 sub_locs = comm.allgather((p0, p1)) vvvo = mpi.alltoall_new( [eris.vvvo[:, :, q0:q1] for q0, q1 in sub_locs], split_recvbuf=True) save_vvop(j0, j1, vvvo) cpu1 = log.timer_debug1('transpose %d:%d' % (p0, p1), *cpu1) def send_data(): while True: while comm.Iprobe(source=MPI.ANY_SOURCE, tag=INQUIRY): tensors, dest = comm.recv(source=MPI.ANY_SOURCE, tag=INQUIRY) for task, slices in tensors: if task == 'Done': return else: mpi.send(self._get_tensor(task, slices), dest, tag=TRANSFER_DATA) time.sleep(interval) daemon = threading.Thread(target=send_data) daemon.start() return daemon
def _make_eris(mp, mo_coeff=None, ao2mofn=None, verbose=None): log = logger.new_logger(mp, verbose) time0 = (time.clock(), time.time()) eris = _ChemistsERIs() eris._common_init_(mp, mo_coeff) nocca, noccb = mp.get_nocc() nmoa, nmob = mp.get_nmo() nvira, nvirb = nmoa-nocca, nmob-noccb nao = eris.mo_coeff[0].shape[0] nmo_pair = nmoa * (nmoa+1) // 2 nao_pair = nao * (nao+1) // 2 mem_incore = (nao_pair**2 + nmo_pair**2) * 8/1e6 mem_now = lib.current_memory()[0] max_memory = max(0, mp.max_memory-mem_now) moa = eris.mo_coeff[0] mob = eris.mo_coeff[1] orboa = moa[:,:nocca] orbob = mob[:,:noccb] orbva = moa[:,nocca:] orbvb = mob[:,noccb:] if (mp.mol.incore_anyway or (mp._scf._eri is not None and mem_incore+mem_now < mp.max_memory)): log.debug('transform (ia|jb) incore') if callable(ao2mofn): eris.ovov = ao2mofn((orboa,orbva,orboa,orbva)).reshape(nocca*nvira,nocca*nvira) eris.ovOV = ao2mofn((orboa,orbva,orbob,orbvb)).reshape(nocca*nvira,noccb*nvirb) eris.OVOV = ao2mofn((orbob,orbvb,orbob,orbvb)).reshape(noccb*nvirb,noccb*nvirb) else: eris.ovov = ao2mo.general(mp._scf._eri, (orboa,orbva,orboa,orbva)) eris.ovOV = ao2mo.general(mp._scf._eri, (orboa,orbva,orbob,orbvb)) eris.OVOV = ao2mo.general(mp._scf._eri, (orbob,orbvb,orbob,orbvb)) elif getattr(mp._scf, 'with_df', None): logger.warn(mp, 'UMP2 detected DF being used in the HF object. ' 'MO integrals are computed based on the DF 3-index tensors.\n' 'It\'s recommended to use DF-UMP2 module.') log.debug('transform (ia|jb) with_df') eris.ovov = mp._scf.with_df.ao2mo((orboa,orbva,orboa,orbva)) eris.ovOV = mp._scf.with_df.ao2mo((orboa,orbva,orbob,orbvb)) eris.OVOV = mp._scf.with_df.ao2mo((orbob,orbvb,orbob,orbvb)) else: log.debug('transform (ia|jb) outcore') eris.feri = lib.H5TmpFile() _ao2mo_ovov(mp, (orboa,orbva,orbob,orbvb), eris.feri, max(2000, max_memory), log) eris.ovov = eris.feri['ovov'] eris.ovOV = eris.feri['ovOV'] eris.OVOV = eris.feri['OVOV'] time1 = log.timer('Integral transformation', *time0) return eris
def _make_qmo_eris_outcore(agf2, eri, coeffs): ''' Returns H5 dataset ''' cput0 = (logger.process_clock(), logger.perf_counter()) log = logger.Logger(agf2.stdout, agf2.verbose) nmo = eri.nmo ci, cj, ca = coeffs ni = ci.shape[1] nj = cj.shape[1] na = ca.shape[1] npair = nmo * (nmo + 1) // 2 mask = get_frozen_mask(agf2) frozen = np.sum(~mask) # possible to have incore MO, outcore QMO if getattr(eri, 'feri', None) is None: eri.feri = lib.H5TmpFile() elif 'qmo' in eri.feri: del eri.feri['qmo'] eri.feri.create_dataset('qmo', (nmo - frozen, ni, nj, na), 'f8') blksize = _agf2.get_blksize(agf2.max_memory, (nmo * npair, nj * na, npair), (nmo * ni, nj * na)) blksize = min(nmo, max(BLKMIN, blksize)) log.debug1('blksize (ragf2._make_qmo_eris_outcore) = %d', blksize) tril2sq = lib.square_mat_in_trilu_indices(nmo) q1 = 0 for p0, p1 in lib.prange(0, nmo, blksize): if not np.any(mask[p0:p1]): # block is fully frozen continue inds = np.arange(p0, p1)[mask[p0:p1]] q0, q1 = q1, q1 + len(inds) idx = list(np.concatenate(tril2sq[inds])) buf = eri.eri[idx] # (blk, nmo, npair) buf = buf.reshape((q1 - q0) * nmo, -1) # (blk*nmo, npair) jasym, nja, cja, sja = ao2mo.incore._conc_mos(cj, ca, compact=True) buf = ao2mo._ao2mo.nr_e2(buf, cja, sja, 's2kl', 's1') buf = buf.reshape(q1 - q0, nmo, nj, na) buf = lib.einsum('xpja,pi->xija', buf, ci) eri.feri['qmo'][q0:q1] = np.asarray(buf, order='C') log.timer('QMO integral transformation', *cput0) return eri.feri['qmo']
def grad_elec(cigrad, civec=None, eris=None, atmlst=None, verbose=logger.INFO): myci = cigrad.base if civec is None: civec = myci.ci assert(not isinstance(civec, (list, tuple))) nocc = myci.nocc nmo = myci.nmo d1 = cisd._gamma1_intermediates(myci, civec, nmo, nocc) fd2intermediate = lib.H5TmpFile() d2 = cisd._gamma2_outcore(myci, civec, nmo, nocc, fd2intermediate, True) t1 = t2 = l1 = l2 = civec return ccsd_grad.grad_elec(cigrad, t1, t2, l1, l2, eris, atmlst, d1, d2, verbose)
def make_rdm2(mycc, t1, t2, l1, l2): r''' Spin-traced two-particle density matrix in MO basis dm2[p,q,r,s] = \sum_{sigma,tau} <p_sigma^\dagger r_tau^\dagger s_tau q_sigma> Note the contraction between ERIs (in Chemist's notation) and rdm2 is E = einsum('pqrs,pqrs', eri, rdm2) ''' d1 = _gamma1_intermediates(mycc, t1, t2, l1, l2) f = lib.H5TmpFile() d2 = _gamma2_outcore(mycc, t1, t2, l1, l2, f, False) return _make_rdm2(mycc, d1, d2, with_dm1=True, with_frozen=True)
def __init__(self, mp, mo_coeff=None, verbose=None): cput0 = (time.clock(), time.time()) if mo_coeff is None: self.mo_coeff = mo_coeff = _mo_without_core(mp, mp.mo_coeff) else: self.mo_coeff = mo_coeff = _mo_without_core(mp, mo_coeff) nocc = mp.nocc nmo = mp.nmo nvir = nmo - nocc mem_incore, mem_outcore, mem_basic = _mem_usage(nocc, nvir) mem_now = lib.current_memory()[0] max_memory = max(2000, mp.max_memory * .9 - mem_now) log = logger.Logger(mp.stdout, mp.verbose) if max_memory < mem_basic: log.warn( 'Not enough memory for integral transformation. ' 'Available mem %s MB, required mem %s MB', max_memory, mem_basic) time0 = (time.clock(), time.time()) co = numpy.asarray(mo_coeff[:, :nocc], order='F') cv = numpy.asarray(mo_coeff[:, nocc:], order='F') if hasattr(mp._scf, 'with_df') and mp._scf.with_df: # To handle the PBC or custom 2-electron with 3-index tensor. # Call dfmp2.MP2 for efficient DF-MP2 implementation. log.warn( 'MP2 detected DF being bound to the HF object. ' '(ia|jb) is computed based on the DF 3-tensor integrals.\n' 'You can switch to dfmp2.MP2 for better efficiency') log.debug('transform (ia|jb) with_df') self.ovov = mp._scf.with_df.ao2mo((co, cv, co, cv)) elif (mp.mol.incore_anyway or (mp._scf._eri is not None and mem_incore + mem_now < mp.max_memory)): log.debug('transform (ia|jb) incore') self.ovov = ao2mo.incore.general(mp._scf._eri, (co, cv, co, cv)) else: log.debug('transform (ia|jb) outcore') self.feri = lib.H5TmpFile() #ao2mo.outcore.general(mp.mol, (co,cv,co,cv), self.feri, # max_memory=max_memory, verbose=mp.verbose) #self.ovov = self.feri['eri_mo'] self.ovov = _ao2mo_ovov(mp, co, cv, self.feri, max_memory, mp.verbose) time1 = log.timer('Integral transformation', *time0)
def transform_integrals_df(myadc): cput0 = (time.clock(), time.time()) log = logger.Logger(myadc.stdout, myadc.verbose) mo_coeff = np.asarray(myadc.mo_coeff, order='F') nocc = myadc._nocc nao, nmo = mo_coeff.shape nvir = myadc._nmo - myadc._nocc nvir_pair = nvir*(nvir+1)//2 with_df = myadc.with_df naux = with_df.get_naoaux() eris = lambda:None eris.vvvv = None eris.ovvv = None Loo = np.empty((naux,nocc,nocc)) Lvo = np.empty((naux,nvir,nocc)) eris.Lvv = np.empty((naux,nvir,nvir)) eris.Lov = np.empty((naux,nocc,nvir)) ijslice = (0, nmo, 0, nmo) Lpq = None p1 = 0 for eri1 in with_df.loop(): Lpq = ao2mo._ao2mo.nr_e2(eri1, mo_coeff, ijslice, aosym='s2', out=Lpq).reshape(-1,nmo,nmo) p0, p1 = p1, p1 + Lpq.shape[0] Loo[p0:p1] = Lpq[:,:nocc,:nocc] #Lov[p0:p1] = Lpq[:,:nocc,nocc:] eris.Lov[p0:p1] = Lpq[:,:nocc,nocc:] Lvo[p0:p1] = Lpq[:,nocc:,:nocc] eris.Lvv[p0:p1] = Lpq[:,nocc:,nocc:] Loo = Loo.reshape(naux,nocc*nocc) eris.Lov = eris.Lov.reshape(naux,nocc*nvir) Lvo = Lvo.reshape(naux,nocc*nvir) eris.Lvv = eris.Lvv.reshape(naux,nvir*nvir) eris.feri1 = lib.H5TmpFile() eris.oooo = eris.feri1.create_dataset('oooo', (nocc,nocc,nocc,nocc), 'f8') eris.oovv = eris.feri1.create_dataset('oovv', (nocc,nocc,nvir,nvir), 'f8', chunks=(nocc,nocc,1,nvir)) eris.ovoo = eris.feri1.create_dataset('ovoo', (nocc,nvir,nocc,nocc), 'f8', chunks=(nocc,1,nocc,nocc)) eris.ovvo = eris.feri1.create_dataset('ovvo', (nocc,nvir,nvir,nocc), 'f8', chunks=(nocc,1,nvir,nocc)) eris.oooo[:] = lib.ddot(Loo.T, Loo).reshape(nocc,nocc,nocc,nocc) eris.ovoo[:] = lib.ddot(eris.Lov.T, Loo).reshape(nocc,nvir,nocc,nocc) eris.oovv[:] = lib.ddot(Loo.T, eris.Lvv).reshape(nocc,nocc,nvir,nvir) eris.ovvo[:] = lib.ddot(eris.Lov.T, Lvo).reshape(nocc,nvir,nvir,nocc) log.timer('DF-ADC integral transformation', *cput0) return eris
def _make_eris(mp, mo_coeff=None, ao2mofn=None, verbose=None): log = logger.new_logger(mp, verbose) time0 = (time.clock(), time.time()) eris = _ChemistsERIs() eris._common_init_(mp, mo_coeff) mo_coeff = eris.mo_coeff nocc = mp.nocc nmo = mp.nmo nvir = nmo - nocc mem_incore, mem_outcore, mem_basic = _mem_usage(nocc, nvir) mem_now = lib.current_memory()[0] max_memory = max(0, mp.max_memory - mem_now) if max_memory < mem_basic: log.warn( 'Not enough memory for integral transformation. ' 'Available mem %s MB, required mem %s MB', max_memory, mem_basic) co = numpy.asarray(mo_coeff[:, :nocc], order='F') cv = numpy.asarray(mo_coeff[:, nocc:], order='F') if (mp.mol.incore_anyway or (mp._scf._eri is not None and mem_incore < max_memory)): log.debug('transform (ia|jb) incore') if callable(ao2mofn): eris.ovov = ao2mofn( (co, cv, co, cv)).reshape(nocc * nvir, nocc * nvir) else: eris.ovov = ao2mo.general(mp._scf._eri, (co, cv, co, cv)) elif getattr(mp._scf, 'with_df', None): # To handle the PBC or custom 2-electron with 3-index tensor. # Call dfmp2.MP2 for efficient DF-MP2 implementation. log.warn('DF-HF is found. (ia|jb) is computed based on the DF ' '3-tensor integrals.\n' 'You can switch to dfmp2.MP2 for better performance') log.debug('transform (ia|jb) with_df') eris.ovov = mp._scf.with_df.ao2mo((co, cv, co, cv)) else: log.debug('transform (ia|jb) outcore') eris.feri = lib.H5TmpFile() #ao2mo.outcore.general(mp.mol, (co,cv,co,cv), eris.feri, # max_memory=max_memory, verbose=log) #eris.ovov = eris.feri['eri_mo'] eris.ovov = _ao2mo_ovov(mp, co, cv, eris.feri, max(2000, max_memory), log) time1 = log.timer('Integral transformation', *time0) return eris
def __init__(self, cc): self.verbose = cc.verbose self.stdout = cc.stdout self.t1 = cc.t1 self.t2 = cc.t2 self.eris = cc.eris self.kconserv = cc.khelper.kconserv self.made_ip_imds = False self.made_ea_imds = False self._made_shared_2e = False # TODO: check whether to hold all stuff in memory if getattr(self.eris, "feri1", None): self._fimd = lib.H5TmpFile() else: self._fimd = None
def test_uccsd_rdm2_mo2ao(self): mol = gto.Mole() mol.verbose = 0 mol.atom = [[8, (0., 0., 0.)], [1, (0., -0.757, 0.587)], [1, (0., 0.757, 0.587)]] mol.spin = 2 mol.basis = '631g' mol.build(0, 0) mf = scf.UHF(mol) mf.conv_tol_grad = 1e-8 mf.kernel() mycc = cc.UCCSD(mf) mycc.diis_start_cycle = 1 mycc.conv_tol = 1e-10 eris = mycc.ao2mo() ecc, t1, t2 = mycc.kernel(eris=eris) l1, l2 = mycc.solve_lambda(eris=eris) fdm2 = lib.H5TmpFile() d2 = cc.uccsd_rdm._gamma2_outcore(mycc, t1, t2, l1, l2, fdm2, True) nao = mycc.mo_coeff[0].shape[0] ref = cc.uccsd_rdm._make_rdm2(mycc, None, d2, with_dm1=False) aa = lib.einsum('ijkl,pi,qj,rk,sl->pqrs', ref[0], mycc.mo_coeff[0], mycc.mo_coeff[0], mycc.mo_coeff[0], mycc.mo_coeff[0]) ab = lib.einsum('ijkl,pi,qj,rk,sl->pqrs', ref[1], mycc.mo_coeff[0], mycc.mo_coeff[0], mycc.mo_coeff[1], mycc.mo_coeff[1]) bb = lib.einsum('ijkl,pi,qj,rk,sl->pqrs', ref[2], mycc.mo_coeff[1], mycc.mo_coeff[1], mycc.mo_coeff[1], mycc.mo_coeff[1]) aa = aa + aa.transpose(0, 1, 3, 2) aa = aa + aa.transpose(1, 0, 2, 3) aa = ao2mo.restore(4, aa, nao) * .5 bb = bb + bb.transpose(0, 1, 3, 2) bb = bb + bb.transpose(1, 0, 2, 3) bb = ao2mo.restore(4, bb, nao) * .5 ab = ab + ab.transpose(0, 1, 3, 2) ab = ab + ab.transpose(1, 0, 2, 3) ab = ao2mo.restore(4, ab, nao) * .5 ref = (aa + ab, bb + ab.T) rdm2 = uccsd_grad._rdm2_mo2ao(mycc, d2, mycc.mo_coeff) self.assertAlmostEqual(abs(ref[0] - rdm2[0]).max(), 0, 10) self.assertAlmostEqual(abs(ref[1] - rdm2[1]).max(), 0, 10) uccsd_grad._rdm2_mo2ao(mycc, d2, mycc.mo_coeff, fdm2) self.assertAlmostEqual( abs(ref[0] - fdm2['dm2aa+ab'].value).max(), 0, 10) self.assertAlmostEqual( abs(ref[1] - fdm2['dm2bb+ab'].value).max(), 0, 10) self.assertAlmostEqual(lib.finger(rdm2[0]), -1.6247203743431637, 7) self.assertAlmostEqual(lib.finger(rdm2[1]), -0.44062825991527471, 7)
def make_rdm2(myci, civec=None, nmo=None, nocc=None): r''' Spin-traced two-particle density matrix in MO basis dm2[p,q,r,s] = \sum_{sigma,tau} <p_sigma^\dagger r_tau^\dagger s_tau q_sigma> Note the contraction between ERIs (in Chemist's notation) and rdm2 is E = einsum('pqrs,pqrs', eri, rdm2) ''' if civec is None: civec = myci.ci if nmo is None: nmo = myci.nmo if nocc is None: nocc = myci.nocc d1 = _gamma1_intermediates(myci, civec, nmo, nocc) f = lib.H5TmpFile() d2 = _gamma2_outcore(myci, civec, nmo, nocc, f, False) return ccsd_rdm._make_rdm2(myci, d1, d2, with_dm1=True, with_frozen=True)
def test_general(self): nao = mol.nao mo = numpy.random.random((nao, 4)) eri = mol.intor('int2e', aosym='s8') eri = ao2mo.kernel(eri, [mo] * 4, compact=False) self.assertEqual(eri.shape, (16, 16)) h5file = lib.H5TmpFile() ao2mo.kernel(mol, [mo] * 4, h5file, intor='int2e', dataname='eri') self.assertEqual(h5file['eri'].shape, (10, 10)) ftmp = tempfile.NamedTemporaryFile() ao2mo.kernel(mol, [mo] * 4, ftmp, intor='int2e', dataname='eri') with ao2mo.load(ftmp.name, 'eri') as eri: self.assertEqual(eri.shape, (10, 10))
def test_ump2_ao2mo_ovov(self): pt = mp.UMP2(mf) nocca, noccb = mol.nelec orboa = mf.mo_coeff[0][:, :nocca] orbva = mf.mo_coeff[0][:, nocca:] orbob = mf.mo_coeff[1][:, :noccb] orbvb = mf.mo_coeff[1][:, noccb:] orbs = (orboa, orbva, orbob, orbvb) ftmp = lib.H5TmpFile() mp.ump2._ao2mo_ovov(pt, orbs, ftmp, 1) ovov = numpy.asarray(ftmp['ovov']) ovOV = numpy.asarray(ftmp['ovOV']) OVOV = numpy.asarray(ftmp['OVOV']) ovov_ref = ao2mo.general(mf._eri, (orboa, orbva, orboa, orbva)) ovOV_ref = ao2mo.general(mf._eri, (orboa, orbva, orbob, orbvb)) OVOV_ref = ao2mo.general(mf._eri, (orbob, orbvb, orbob, orbvb)) self.assertAlmostEqual(numpy.linalg.norm(ovov_ref - ovov), 0, 9) self.assertAlmostEqual(numpy.linalg.norm(ovOV_ref - ovOV), 0, 9) self.assertAlmostEqual(numpy.linalg.norm(OVOV_ref - OVOV), 0, 9)
def save_amps(mycc, fname="fcc"): """ Save amplitudes to a file. """ _sync_(mycc) if fname.endswith(".h5"): fname = fname[:-3] filename = fname + '__rank' + str(mpi.rank) + ".h5" fcc = lib.H5TmpFile(filename, 'w') fcc['mo_coeff'] = mycc.mo_coeff if (getattr(mycc, "t1", None) is not None) and \ (getattr(mycc, "t2", None) is not None): tvec = mycc.amplitudes_to_vector(mycc.t1, mycc.t2) fcc['tvec'] = tvec tvec = None if (getattr(mycc, "l1", None) is not None) and \ (getattr(mycc, "l2", None) is not None): lvec = mycc.amplitudes_to_vector(mycc.l1, mycc.l2) fcc['lvec'] = lvec lvec = None
def _make_shared_2e(self): cput0 = (time.clock(), time.time()) log = logger.Logger(self.stdout, self.verbose) t1,t2,eris = self.t1, self.t2, self.eris kconserv = self.kconserv # TODO: check whether to hold Wovov Wovvo in memory if self._fimd is None: self._fimd = lib.H5TmpFile() nkpts, nocc, nvir = t1.shape self._fimd.create_dataset('ovov', (nkpts,nkpts,nkpts,nocc,nvir,nocc,nvir), t1.dtype.char) self._fimd.create_dataset('ovvo', (nkpts,nkpts,nkpts,nocc,nvir,nvir,nocc), t1.dtype.char) # 2 virtuals self.Wovov = imd.Wovov(t1,t2,eris,kconserv, self._fimd['ovov']) self.Wovvo = imd.Wovvo(t1,t2,eris,kconserv, self._fimd['ovvo']) self.Woovv = eris.oovv log.timer('EOM-CCSD shared two-electron intermediates', *cput0)
def test_rdm2_mo2ao(self): mycc = cc.ccsd.CCSD(mf) mycc.conv_tol = 1e-10 mycc.diis_start_cycle = 1 eris = mycc.ao2mo() ecc, t1, t2 = mycc.kernel(eris=eris) l1, l2 = mycc.solve_lambda(eris=eris) fdm2 = lib.H5TmpFile() d2 = cc.ccsd_rdm._gamma2_outcore(mycc, t1, t2, l1, l2, fdm2, True) nao = mycc.mo_coeff.shape[0] ref = cc.ccsd_rdm._make_rdm2(mycc, None, d2, with_dm1=False) ref = lib.einsum('ijkl,pi,qj,rk,sl->pqrs', ref, mycc.mo_coeff, mycc.mo_coeff, mycc.mo_coeff, mycc.mo_coeff) ref = ref + ref.transpose(0, 1, 3, 2) ref = ref + ref.transpose(1, 0, 2, 3) ref = ao2mo.restore(4, ref, nao) * .5 rdm2 = ccsd_grad._rdm2_mo2ao(mycc, d2, mycc.mo_coeff) ccsd_grad._rdm2_mo2ao(mycc, d2, mycc.mo_coeff, fdm2) self.assertAlmostEqual(abs(ref - rdm2).max(), 0, 10) self.assertAlmostEqual(abs(ref - fdm2['dm2'].value).max(), 0, 10) self.assertAlmostEqual(lib.finger(rdm2), -0.32532303057849454, 6)