nao = mol.nao_nr() npair = nao*(nao+1)//2 rhf = scf.RHF(mol) rhf.scf() print(logger.process_clock()) full(mol, rhf.mo_coeff, 'h2oeri.h5', max_memory=10, ioblk_size=5) print(logger.process_clock()) eri0 = incore.full(rhf._eri, rhf.mo_coeff) feri = h5py.File('h2oeri.h5', 'r') print('full', abs(eri0-feri['eri_mo']).sum()) feri.close() print(logger.process_clock()) c = rhf.mo_coeff general(mol, (c,c,c,c), 'h2oeri.h5', max_memory=10, ioblk_size=5) print(logger.process_clock()) feri = h5py.File('h2oeri.h5', 'r') print('general', abs(eri0-feri['eri_mo']).sum()) feri.close() # set ijsame and klsame to False, then check c = rhf.mo_coeff n = c.shape[1] general(mol, (c,c,c,c), 'h2oeri.h5', max_memory=10, ioblk_size=5, compact=False) feri = h5py.File('h2oeri.h5', 'r') eri1 = numpy.array(feri['eri_mo']).reshape(n,n,n,n) eri1 = addons.restore(4, eri1, n) print('general', abs(eri0-eri1).sum())
def half_e1(eri_ao, mo_coeffs, compact=True): r'''Given two set of orbitals, half transform the (ij| pair of 8-fold or 4-fold AO integrals (ij|kl) Args: eri_ao : ndarray AO integrals, can be either 8-fold or 4-fold symmetry. mo_coeffs : list of ndarray Two sets of orbital coefficients, corresponding to the i, j indices of (ij|kl) Kwargs: compact : bool When compact is True, the returned MO integrals uses the highest possible permutation symmetry. If it's False, the function will abandon any permutation symmetry, and return the "plain" MO integrals Returns: ndarray of transformed MO integrals. The MO integrals may or may not have the permutation symmetry, depending on the given orbitals, and the kwargs compact. Examples: >>> from pyscf import gto >>> from pyscf import ao2mo >>> mol = gto.M(atom='O 0 0 0; H 0 1 0; H 0 0 1', basis='sto3g') >>> eri = mol.intor('int2e_sph', aosym='s8') >>> mo1 = numpy.random.random((mol.nao_nr(), 10)) >>> mo2 = numpy.random.random((mol.nao_nr(), 8)) >>> eri1 = ao2mo.incore.half_e1(eri, (mo1,mo2)) >>> eri1 = ao2mo.incore.half_e1(eri, (mo1,mo2)) >>> print(eri1.shape) (80, 28) >>> eri1 = ao2mo.incore.half_e1(eri, (mo1,mo2), compact=False) >>> print(eri1.shape) (80, 28) >>> eri1 = ao2mo.incore.half_e1(eri, (mo1,mo1)) >>> print(eri1.shape) (55, 28) ''' eri_ao = numpy.asarray(eri_ao, order='C') nao, nmoi = mo_coeffs[0].shape nmoj = mo_coeffs[1].shape[1] nao_pair = nao*(nao+1)//2 ijmosym, nij_pair, moij, ijshape = _conc_mos(mo_coeffs[0], mo_coeffs[1], compact) ijshape = (ijshape[0], ijshape[1]-ijshape[0], ijshape[2], ijshape[3]-ijshape[2]) eri1 = numpy.empty((nij_pair,nao_pair)) if nij_pair == 0: return eri1 if eri_ao.size == nao_pair**2: # 4-fold symmetry ftrans = _ao2mo.libao2mo.AO2MOtranse1_incore_s4 elif eri_ao.size == nao_pair*(nao_pair+1)//2: ftrans = _ao2mo.libao2mo.AO2MOtranse1_incore_s8 else: from pyscf.ao2mo.addons import restore eri_ao = restore(4, eri_ao, nao) ftrans = _ao2mo.libao2mo.AO2MOtranse1_incore_s4 if ijmosym == 's2': fmmm = _ao2mo.libao2mo.AO2MOmmm_nr_s2_s2 elif nmoi <= nmoj: fmmm = _ao2mo.libao2mo.AO2MOmmm_nr_s2_iltj else: fmmm = _ao2mo.libao2mo.AO2MOmmm_nr_s2_igtj fdrv = getattr(_ao2mo.libao2mo, 'AO2MOnr_e1incore_drv') bufs = numpy.empty((BLOCK, nij_pair)) for blk0 in range(0, nao_pair, BLOCK): blk1 = min(blk0+BLOCK, nao_pair) buf = bufs[:blk1-blk0] fdrv(ftrans, fmmm, buf.ctypes.data_as(ctypes.c_void_p), eri_ao.ctypes.data_as(ctypes.c_void_p), moij.ctypes.data_as(ctypes.c_void_p), ctypes.c_int(blk0), ctypes.c_int(blk1-blk0), ctypes.c_int(nao), ctypes.c_int(ijshape[0]), ctypes.c_int(ijshape[1]), ctypes.c_int(ijshape[2]), ctypes.c_int(ijshape[3])) eri1[:,blk0:blk1] = buf.T return eri1
nao = mol.nao_nr() npair = nao*(nao+1)//2 rhf = scf.RHF(mol) rhf.scf() print(time.clock()) full(mol, rhf.mo_coeff, 'h2oeri.h5', max_memory=10, ioblk_size=5) print(time.clock()) eri0 = incore.full(rhf._eri, rhf.mo_coeff) feri = h5py.File('h2oeri.h5', 'r') print('full', abs(eri0-feri['eri_mo']).sum()) feri.close() print(time.clock()) c = rhf.mo_coeff general(mol, (c,c,c,c), 'h2oeri.h5', max_memory=10, ioblk_size=5) print(time.clock()) feri = h5py.File('h2oeri.h5', 'r') print('general', abs(eri0-feri['eri_mo']).sum()) feri.close() # set ijsame and klsame to False, then check c = rhf.mo_coeff n = c.shape[1] general(mol, (c,c,c,c), 'h2oeri.h5', max_memory=10, ioblk_size=5, compact=False) feri = h5py.File('h2oeri.h5', 'r') eri1 = numpy.array(feri['eri_mo']).reshape(n,n,n,n) eri1 = addons.restore(4, eri1, n) print('general', abs(eri0-eri1).sum())
def half_e1(eri_ao, mo_coeffs, compact=True): r'''Given two set of orbitals, half transform the (ij| pair of 8-fold or 4-fold AO integrals (ij|kl) Args: eri_ao : ndarray AO integrals, can be either 8-fold or 4-fold symmetry. mo_coeffs : list of ndarray Two sets of orbital coefficients, corresponding to the i, j indices of (ij|kl) Kwargs: compact : bool When compact is True, the returned MO integrals uses the highest possible permutation symmetry. If it's False, the function will abandon any permutation symmetry, and return the "plain" MO integrals Returns: ndarray of transformed MO integrals. The MO integrals may or may not have the permutation symmetry, depending on the given orbitals, and the kwargs compact. Examples: >>> from pyscf import gto >>> from pyscf import ao2mo >>> mol = gto.M(atom='O 0 0 0; H 0 1 0; H 0 0 1', basis='sto3g') >>> eri = mol.intor('int2e_sph', aosym='s8') >>> mo1 = numpy.random.random((mol.nao_nr(), 10)) >>> mo2 = numpy.random.random((mol.nao_nr(), 8)) >>> eri1 = ao2mo.incore.half_e1(eri, (mo1,mo2)) >>> eri1 = ao2mo.incore.half_e1(eri, (mo1,mo2)) >>> print(eri1.shape) (80, 28) >>> eri1 = ao2mo.incore.half_e1(eri, (mo1,mo2), compact=False) >>> print(eri1.shape) (80, 28) >>> eri1 = ao2mo.incore.half_e1(eri, (mo1,mo1)) >>> print(eri1.shape) (55, 28) ''' eri_ao = numpy.asarray(eri_ao, order='C') nao, nmoi = mo_coeffs[0].shape nmoj = mo_coeffs[1].shape[1] nao_pair = nao * (nao + 1) // 2 ijmosym, nij_pair, moij, ijshape = _conc_mos(mo_coeffs[0], mo_coeffs[1], compact) ijshape = (ijshape[0], ijshape[1] - ijshape[0], ijshape[2], ijshape[3] - ijshape[2]) eri1 = numpy.empty((nij_pair, nao_pair)) if nij_pair == 0: return eri1 if eri_ao.size == nao_pair**2: # 4-fold symmetry ftrans = _ao2mo.libao2mo.AO2MOtranse1_incore_s4 elif eri_ao.size == nao_pair * (nao_pair + 1) // 2: ftrans = _ao2mo.libao2mo.AO2MOtranse1_incore_s8 else: from pyscf.ao2mo.addons import restore eri_ao = restore(4, eri_ao, nao) ftrans = _ao2mo.libao2mo.AO2MOtranse1_incore_s4 if ijmosym == 's2': fmmm = _ao2mo.libao2mo.AO2MOmmm_nr_s2_s2 elif nmoi <= nmoj: fmmm = _ao2mo.libao2mo.AO2MOmmm_nr_s2_iltj else: fmmm = _ao2mo.libao2mo.AO2MOmmm_nr_s2_igtj fdrv = getattr(_ao2mo.libao2mo, 'AO2MOnr_e1incore_drv') bufs = numpy.empty((BLOCK, nij_pair)) for blk0 in range(0, nao_pair, BLOCK): blk1 = min(blk0 + BLOCK, nao_pair) buf = bufs[:blk1 - blk0] fdrv(ftrans, fmmm, buf.ctypes.data_as(ctypes.c_void_p), eri_ao.ctypes.data_as(ctypes.c_void_p), moij.ctypes.data_as(ctypes.c_void_p), ctypes.c_int(blk0), ctypes.c_int(blk1 - blk0), ctypes.c_int(nao), ctypes.c_int(ijshape[0]), ctypes.c_int(ijshape[1]), ctypes.c_int(ijshape[2]), ctypes.c_int(ijshape[3])) eri1[:, blk0:blk1] = buf.T return eri1