Exemplo n.º 1
0
def aux_e2(mol, auxmol, intor='cint3c2e_spinor', aosym='s1', comp=1, hermi=0):
    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)

    nao = mol.nao_2c()
    naoaux = auxmol.nao_nr()
    if aosym == 's1':
        eri = numpy.empty((nao * nao, naoaux), dtype=numpy.complex)
        fill = _fpointer('RIfill_r_s1_auxe2')
    else:
        eri = numpy.empty((nao * (nao + 1) // 2, naoaux), dtype=numpy.complex)
        fill = _fpointer('RIfill_r_s2ij_auxe2')
    fintor = _fpointer(intor)
    cintopt = _vhf.make_cintopt(c_atm, c_bas, c_env, intor)
    libri.RIr_3c2e_auxe2_drv(fintor, fill, eri.ctypes.data_as(ctypes.c_void_p),
                             ctypes.c_int(0), ctypes.c_int(mol.nbas),
                             ctypes.c_int(mol.nbas), ctypes.c_int(auxmol.nbas),
                             ctypes.c_int(1), cintopt,
                             c_atm.ctypes.data_as(ctypes.c_void_p), natm,
                             c_bas.ctypes.data_as(ctypes.c_void_p), nbas,
                             c_env.ctypes.data_as(ctypes.c_void_p))
    return eri
Exemplo n.º 2
0
    def __init__(self, mol, intor, prescreen='CVHFnoscreen', qcondname=None):
        self._this = ctypes.POINTER(_vhf._CVHFOpt)()
        #print self._this.contents, expect ValueError: NULL pointer access
        self._intor = _fpointer(intor)

        c_atm = numpy.asarray(mol._atm, dtype=numpy.int32, order='C')
        c_bas = numpy.asarray(mol._bas, dtype=numpy.int32, order='C')
        c_env = numpy.asarray(mol._env, dtype=numpy.double, order='C')
        natm = ctypes.c_int(c_atm.shape[0])
        nbas = ctypes.c_int(c_bas.shape[0])
        self._cintopt = _vhf.make_cintopt(c_atm, c_bas, c_env, intor)

        libao2mo.CVHFinit_optimizer(ctypes.byref(self._this),
                                    c_atm.ctypes.data_as(ctypes.c_void_p),
                                    natm,
                                    c_bas.ctypes.data_as(ctypes.c_void_p),
                                    nbas,
                                    c_env.ctypes.data_as(ctypes.c_void_p))
        self._this.contents.fprescreen = _fpointer(prescreen)

        if prescreen != 'CVHFnoscreen':
            # for cint2e_sph, qcondname is 'CVHFsetnr_direct_scf'
            fsetqcond = getattr(libao2mo, qcondname)
            fsetqcond(self._this, c_atm.ctypes.data_as(ctypes.c_void_p), natm,
                      c_bas.ctypes.data_as(ctypes.c_void_p), nbas,
                      c_env.ctypes.data_as(ctypes.c_void_p))
Exemplo n.º 3
0
def run3c(fill, kpts, shls_slice=None):
    intor = 'int3c2e_sph'
    nao = cell.nao_nr()
    nkpts = len(kpts)
    if fill == 'PBCnr3c_fill_gs2':
        out = numpy.empty((nao * (nao + 1) // 2, nao))
        kptij_idx = numpy.arange(nkpts).astype(numpy.int32)
    elif fill == 'PBCnr3c_fill_gs1':
        out = numpy.empty((nao, nao, nao))
        kptij_idx = numpy.arange(nkpts).astype(numpy.int32)
    elif fill in ('PBCnr3c_fill_kks1', 'PBCnr3c_fill_kks2'):
        kptij_idx = numpy.asarray(
            [i * nkpts + j for i in range(nkpts) for j in range(i + 1)],
            dtype=numpy.int32)
        out = numpy.empty((len(kptij_idx), nao, nao, nao),
                          dtype=numpy.complex128)
    elif fill == 'PBCnr3c_fill_ks1':
        kptij_idx = numpy.arange(nkpts).astype(numpy.int32)
        out = numpy.empty((nkpts, nao, nao, nao), dtype=numpy.complex128)
    elif fill == 'PBCnr3c_fill_ks2':
        out = numpy.empty((nkpts, nao * (nao + 1) // 2, nao),
                          dtype=numpy.complex128)
        kptij_idx = numpy.arange(nkpts).astype(numpy.int32)
    else:
        raise RuntimeError
    nkpts_ij = len(kptij_idx)
    Ls = cell.get_lattice_Ls()
    nimgs = len(Ls)
    expkL = numpy.exp(1j * numpy.dot(kpts, Ls.T))
    comp = 1
    if shls_slice is None:
        shls_slice = (0, cell.nbas, cell.nbas, cell.nbas * 2, cell.nbas * 2,
                      cell.nbas * 3)

    atm, bas, env = gto.conc_env(cell._atm, cell._bas, cell._env, cell._atm,
                                 cell._bas, cell._env)
    atm, bas, env = gto.conc_env(atm, bas, env, cell._atm, cell._bas,
                                 cell._env)
    ao_loc = gto.moleintor.make_loc(bas, 'int3c2e_sph')
    cintopt = _vhf.make_cintopt(atm, bas, env, intor)

    libpbc.PBCnr3c_drv(getattr(libpbc, intor), getattr(libpbc, fill),
                       out.ctypes.data_as(ctypes.c_void_p),
                       ctypes.c_int(nkpts_ij), ctypes.c_int(nkpts),
                       ctypes.c_int(comp), ctypes.c_int(len(Ls)),
                       Ls.ctypes.data_as(ctypes.c_void_p),
                       expkL.ctypes.data_as(ctypes.c_void_p),
                       kptij_idx.ctypes.data_as(ctypes.c_void_p),
                       (ctypes.c_int * 6)(*shls_slice),
                       ao_loc.ctypes.data_as(ctypes.c_void_p), cintopt,
                       atm.ctypes.data_as(ctypes.c_void_p),
                       ctypes.c_int(cell.natm),
                       bas.ctypes.data_as(ctypes.c_void_p),
                       ctypes.c_int(cell.nbas),
                       env.ctypes.data_as(ctypes.c_void_p))
    return out
Exemplo n.º 4
0
def _wrap_int3c(cell, auxcell, intor, comp, Ls, out_lst):
    atm, bas, env = pyscf.gto.conc_env(cell._atm, cell._bas, cell._env,
                                       cell._atm, cell._bas, cell._env)
    atm, bas, env = pyscf.gto.conc_env(atm, bas, env, auxcell._atm,
                                       auxcell._bas, auxcell._env)
    atm = numpy.asarray(atm, dtype=numpy.int32)
    bas = numpy.asarray(bas, dtype=numpy.int32)
    env = numpy.asarray(env, dtype=numpy.double)
    natm = len(atm)
    nbas = len(bas)
    if 'ssc' in intor:
        ao_loc = cell.ao_loc_nr()
        ao_loc = numpy.hstack((ao_loc[:-1], ao_loc[-1] + ao_loc))
        ao_loc = numpy.hstack(
            (ao_loc[:-1], ao_loc[-1] + auxcell.ao_loc_nr(cart=True)))
        ao_loc = numpy.asarray(ao_loc, dtype=numpy.int32)
    else:
        ao_loc = pyscf.gto.moleintor.make_loc(bas, intor)

    cintopt = _vhf.make_cintopt(atm, bas, env, intor)
    fintor = getattr(pyscf.gto.moleintor.libcgto, intor)
    fill = getattr(libpbc, 'PBCnr3c_fill_s1')
    drv = libpbc.PBCnr3c_drv
    outs = (ctypes.c_void_p * len(out_lst))(
        *[out.ctypes.data_as(ctypes.c_void_p) for out in out_lst])
    xyz = numpy.asarray(cell.atom_coords(), order='C')
    ptr_coords = numpy.asarray(atm[cell.natm:cell.natm * 2,
                                   pyscf.gto.PTR_COORD],
                               dtype=numpy.int32,
                               order='C')
    c_ptr_coords = ptr_coords.ctypes.data_as(ctypes.c_void_p)
    c_xyz = xyz.ctypes.data_as(ctypes.c_void_p)
    c_nxyz = ctypes.c_int(len(xyz))
    Ls = numpy.asarray(Ls, order='C')
    c_Ls = Ls.ctypes.data_as(ctypes.c_void_p)
    c_comp = ctypes.c_int(comp)
    c_ao_loc = ao_loc.ctypes.data_as(ctypes.c_void_p)
    c_atm = atm.ctypes.data_as(ctypes.c_void_p)
    c_bas = bas.ctypes.data_as(ctypes.c_void_p)
    c_env = env.ctypes.data_as(ctypes.c_void_p)
    c_natm = ctypes.c_int(natm)
    c_nbas = ctypes.c_int(nbas)

    def ints(facs, c_shls_slice):
        nimgs, nkpts = facs.shape
        drv(fintor, fill, outs, c_xyz, c_ptr_coords, c_nxyz, c_Ls,
            ctypes.c_int(nimgs), facs.ctypes.data_as(ctypes.c_void_p),
            ctypes.c_int(nkpts), c_comp, c_shls_slice, c_ao_loc, cintopt,
            c_atm, c_natm, c_bas, c_nbas, c_env)

    # Save the numpy arrays in envs because ctypes does not increase their
    # reference counting.
    ints._envs = (atm, bas, env, ao_loc, xyz, ptr_coords, Ls)
    return ints
Exemplo n.º 5
0
def nr_e1fill(intor,
              sh_range,
              atm,
              bas,
              env,
              aosym='s1',
              comp=1,
              ao2mopt=None,
              out=None):
    assert (aosym in ('s4', 's2ij', 's2kl', 's1'))

    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, order='C')
    natm = ctypes.c_int(c_atm.shape[0])
    nbas = ctypes.c_int(c_bas.shape[0])
    ao_loc = moleintor.make_loc(bas, intor)
    nao = ao_loc[-1]

    klsh0, klsh1, nkl = sh_range

    if aosym in ('s4', 's2ij'):
        nao_pair = nao * (nao + 1) // 2
    else:
        nao_pair = nao * nao
    if out is None:
        out = numpy.empty((comp, nkl, nao_pair))
    else:
        out = numpy.ndarray((comp, nkl, nao_pair), buffer=out)
    if out.size == 0:
        return out

    if ao2mopt is not None:
        cao2mopt = ao2mopt._this
        cintopt = ao2mopt._cintopt
        cintor = ao2mopt._intor
    else:
        cao2mopt = lib.c_null_ptr()
        cintor = _fpointer(intor)
        cintopt = _vhf.make_cintopt(c_atm, c_bas, c_env, intor)

    fdrv = getattr(libao2mo, 'AO2MOnr_e1fill_drv')
    fill = _fpointer('AO2MOfill_nr_' + aosym)
    fdrv(cintor, fill,
         out.ctypes.data_as(ctypes.c_void_p), ctypes.c_int(klsh0),
         ctypes.c_int(klsh1 - klsh0), ctypes.c_int(nkl), ctypes.c_int(comp),
         ao_loc.ctypes.data_as(ctypes.c_void_p), cintopt, cao2mopt,
         c_atm.ctypes.data_as(ctypes.c_void_p), natm,
         c_bas.ctypes.data_as(ctypes.c_void_p), nbas,
         c_env.ctypes.data_as(ctypes.c_void_p))
    return out
Exemplo n.º 6
0
def getints2e(intor_name, atm, bas, env, shls_slice=None, comp=1,
              aosym='s1', ao_loc=None, cintopt=None, out=None):
    aosym = _stand_sym_code(aosym)

    atm = numpy.asarray(atm, dtype=numpy.int32, order='C')
    bas = numpy.asarray(bas, dtype=numpy.int32, order='C')
    env = numpy.asarray(env, dtype=numpy.double, order='C')
    c_atm = atm.ctypes.data_as(pyscf.lib.c_int_p)
    c_bas = bas.ctypes.data_as(pyscf.lib.c_int_p)
    c_env = env.ctypes.data_as(pyscf.lib.c_double_p)
    natm = atm.shape[0]
    nbas = bas.shape[0]

    if '_cart' in intor_name:
        libcgto.CINTtot_cgto_cart.restype = ctypes.c_int
        nao = libcgto.CINTtot_cgto_cart(c_bas, ctypes.c_int(nbas))
        cgto_in_shell = 'CINTcgto_cart'
    elif '_sph' in intor_name:
        libcgto.CINTtot_cgto_spheric.restype = ctypes.c_int
        nao = libcgto.CINTtot_cgto_spheric(c_bas, ctypes.c_int(nbas))
        cgto_in_shell = 'CINTcgto_spheric'
    else:
        raise NotImplementedError('cint2e spinor AO integrals')

    if intor_name in ('cint2e_sph', 'cint2e_cart') and aosym == 's8':
        assert(shls_slice is None)
        nao_pair = nao*(nao+1)//2
        if out is None:
            out = numpy.empty((nao_pair*(nao_pair+1)//2))
        else:
            out = numpy.ndarray((nao_pair*(nao_pair+1)//2), buffer=out)
        drv = libcvhf.GTO2e_cart_or_sph
        drv(_fpointer(intor_name), _fpointer(cgto_in_shell),
            out.ctypes.data_as(ctypes.c_void_p),
            c_atm, ctypes.c_int(natm), c_bas, ctypes.c_int(nbas), c_env)
        return out

    else:
        from pyscf.scf import _vhf
        if shls_slice is None:
            shls_slice = (0, nbas, 0, nbas)
        else:
            assert(shls_slice[1] <= nbas and shls_slice[3] <= nbas)
        bralst = numpy.arange(shls_slice[0], shls_slice[1], dtype=numpy.int32)
        ketlst = numpy.arange(shls_slice[2], shls_slice[3], dtype=numpy.int32)
        num_cgto_of = getattr(libcgto, cgto_in_shell)
        naoi = sum([num_cgto_of(ctypes.c_int(i), c_bas) for i in bralst])
        naoj = sum([num_cgto_of(ctypes.c_int(i), c_bas) for i in ketlst])
        if aosym in ('s4', 's2ij'):
            nij = naoi * (naoi + 1) // 2
            assert(numpy.alltrue(bralst == ketlst))
        else:
            nij = naoi * naoj
        if aosym in ('s4', 's2kl'):
            nkl = nao * (nao + 1) // 2
        else:
            nkl = nao * nao
        if comp == 1:
            if out is None:
                out = numpy.empty((nij,nkl))
            else:
                out = numpy.ndarray((nij,nkl), buffer=out)
        else:
            if out is None:
                out = numpy.empty((comp,nij,nkl))
            else:
                out = numpy.ndarray((comp,nij,nkl), buffer=out)

        cintopt = _vhf.make_cintopt(atm, bas, env, intor_name)
        prescreen = pyscf.lib.c_null_ptr()
        drv = libcgto.GTOnr2e_fill_drv
        drv(_fpointer(intor_name), _fpointer(cgto_in_shell),
            _fpointer('GTOnr2e_fill_'+aosym), prescreen,
            out.ctypes.data_as(ctypes.c_void_p), ctypes.c_int(comp),
            bralst.ctypes.data_as(ctypes.c_void_p), ctypes.c_int(bralst.size),
            ketlst.ctypes.data_as(ctypes.c_void_p), ctypes.c_int(ketlst.size),
            cintopt, c_atm, ctypes.c_int(natm), c_bas, ctypes.c_int(nbas), c_env)
        cintopt = None
        return out
Exemplo n.º 7
0
def r_e1(intor,
         mo_coeff,
         orbs_slice,
         sh_range,
         atm,
         bas,
         env,
         tao,
         aosym='s1',
         comp=1,
         ao2mopt=None,
         out=None):
    assert (aosym in ('s4', 's2ij', 's2kl', 's1', 'a2ij', 'a2kl', 'a4ij',
                      'a4kl', 'a4'))
    mo_coeff = numpy.asfortranarray(mo_coeff)
    i0, i1, j0, j1 = orbs_slice
    icount = i1 - i0
    jcount = j1 - j0
    ij_count = icount * jcount

    c_atm = numpy.asarray(atm, dtype=numpy.int32)
    c_bas = numpy.asarray(bas, dtype=numpy.int32)
    c_env = numpy.asarray(env)
    natm = ctypes.c_int(c_atm.shape[0])
    nbas = ctypes.c_int(c_bas.shape[0])

    klsh0, klsh1, nkl = sh_range

    if icount <= jcount:
        fmmm = _fpointer('AO2MOmmm_r_iltj')
    else:
        fmmm = _fpointer('AO2MOmmm_r_igtj')

    if out is None:
        out = numpy.empty((comp, nkl, ij_count), dtype=numpy.complex)
    else:
        out = numpy.ndarray((comp, nkl, nao_pair),
                            dtype=numpy.complex,
                            buffer=out)
    if out.size == 0:
        return out

    if ao2mopt is not None:
        cao2mopt = ao2mopt._this
        cintopt = ao2mopt._cintopt
        cintor = ao2mopt._intor
    else:
        cao2mopt = lib.c_null_ptr()
        cintor = _fpointer(intor)
        cintopt = _vhf.make_cintopt(c_atm, c_bas, c_env, intor)

    tao = numpy.asarray(tao, dtype=numpy.int32)
    ao_loc = moleintor.make_loc(bas, 'spinor')

    fdrv = getattr(libao2mo, 'AO2MOr_e1_drv')
    fill = _fpointer('AO2MOfill_r_' + aosym)
    ftrans = _fpointer('AO2MOtranse1_r_' + aosym)
    fdrv(cintor, fill, ftrans, fmmm, out.ctypes.data_as(ctypes.c_void_p),
         mo_coeff.ctypes.data_as(ctypes.c_void_p), ctypes.c_int(klsh0),
         ctypes.c_int(klsh1 - klsh0), ctypes.c_int(nkl), ctypes.c_int(comp),
         (ctypes.c_int * 4)(*orbs_slice), tao.ctypes.data_as(ctypes.c_void_p),
         ao_loc.ctypes.data_as(ctypes.c_void_p), cintopt, cao2mopt,
         c_atm.ctypes.data_as(ctypes.c_void_p), natm,
         c_bas.ctypes.data_as(ctypes.c_void_p), nbas,
         c_env.ctypes.data_as(ctypes.c_void_p))
    return out
Exemplo n.º 8
0
def wrap_int3c(cell,
               auxcell,
               intor='int3c2e',
               aosym='s1',
               comp=1,
               kptij_lst=numpy.zeros((1, 2, 3)),
               cintopt=None,
               pbcopt=None):
    intor = cell._add_suffix(intor)
    pcell = copy.copy(cell)
    pcell._atm, pcell._bas, pcell._env = \
    atm, bas, env = gto.conc_env(cell._atm, cell._bas, cell._env,
                                 cell._atm, cell._bas, cell._env)
    ao_loc = gto.moleintor.make_loc(bas, intor)
    aux_loc = auxcell.ao_loc_nr(auxcell.cart or 'ssc' in intor)
    ao_loc = numpy.asarray(numpy.hstack([ao_loc, ao_loc[-1] + aux_loc[1:]]),
                           dtype=numpy.int32)
    atm, bas, env = gto.conc_env(atm, bas, env, auxcell._atm, auxcell._bas,
                                 auxcell._env)
    Ls = cell.get_lattice_Ls()
    nimgs = len(Ls)

    kpti = kptij_lst[:, 0]
    kptj = kptij_lst[:, 1]
    if gamma_point(kptij_lst):
        kk_type = 'g'
        dtype = numpy.double
        nkpts = nkptij = 1
        kptij_idx = numpy.array([0], dtype=numpy.int32)
        expkL = numpy.ones(1)
    elif is_zero(kpti - kptj):  # j_only
        kk_type = 'k'
        dtype = numpy.complex128
        kpts = kptij_idx = numpy.asarray(kpti, order='C')
        expkL = numpy.exp(1j * numpy.dot(kpts, Ls.T))
        nkpts = nkptij = len(kpts)
    else:
        kk_type = 'kk'
        dtype = numpy.complex128
        kpts = unique(numpy.vstack([kpti, kptj]))[0]
        expkL = numpy.exp(1j * numpy.dot(kpts, Ls.T))
        wherei = numpy.where(
            abs(kpti.reshape(-1, 1, 3) - kpts).sum(axis=2) < KPT_DIFF_TOL)[1]
        wherej = numpy.where(
            abs(kptj.reshape(-1, 1, 3) - kpts).sum(axis=2) < KPT_DIFF_TOL)[1]
        nkpts = len(kpts)
        kptij_idx = numpy.asarray(wherei * nkpts + wherej, dtype=numpy.int32)
        nkptij = len(kptij_lst)

    fill = 'PBCnr3c_fill_%s%s' % (kk_type, aosym[:2])
    drv = libpbc.PBCnr3c_drv
    if cintopt is None:
        cintopt = _vhf.make_cintopt(atm, bas, env, intor)
        # Remove the precomputed pair data because the pair data corresponds to the
        # integral of cell #0 while the lattice sum moves shls to all repeated images.
        if intor[:3] != 'ECP':
            libpbc.CINTdel_pairdata_optimizer(cintopt)
    if pbcopt is None:
        pbcopt = _pbcintor.PBCOpt(pcell).init_rcut_cond(pcell)
    if isinstance(pbcopt, _pbcintor.PBCOpt):
        cpbcopt = pbcopt._this
    else:
        cpbcopt = lib.c_null_ptr()

    nbas = cell.nbas

    def int3c(shls_slice, out):
        shls_slice = (shls_slice[0], shls_slice[1], nbas + shls_slice[2],
                      nbas + shls_slice[3], nbas * 2 + shls_slice[4],
                      nbas * 2 + shls_slice[5])
        drv(
            getattr(libpbc, intor),
            getattr(libpbc, fill),
            out.ctypes.data_as(ctypes.c_void_p),
            ctypes.c_int(nkptij),
            ctypes.c_int(nkpts),
            ctypes.c_int(comp),
            ctypes.c_int(nimgs),
            Ls.ctypes.data_as(ctypes.c_void_p),
            expkL.ctypes.data_as(ctypes.c_void_p),
            kptij_idx.ctypes.data_as(ctypes.c_void_p),
            (ctypes.c_int * 6)(*shls_slice),
            ao_loc.ctypes.data_as(ctypes.c_void_p),
            cintopt,
            cpbcopt,
            atm.ctypes.data_as(ctypes.c_void_p),
            ctypes.c_int(cell.natm),
            bas.ctypes.data_as(ctypes.c_void_p),
            ctypes.c_int(nbas),  # need to pass cell.nbas to libpbc.PBCnr3c_drv
            env.ctypes.data_as(ctypes.c_void_p),
            ctypes.c_int(env.size))
        return out

    return int3c
Exemplo n.º 9
0
def nr_e1fill(intor,
              sh_range,
              atm,
              bas,
              env,
              aosym='s1',
              comp=1,
              ao2mopt=None,
              out=None):
    assert (aosym in ('s4', 's2ij', 's2kl', 's1'))

    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, order='C')
    natm = ctypes.c_int(c_atm.shape[0])
    nbas = ctypes.c_int(c_bas.shape[0])

    klsh0, klsh1, nkl = sh_range

    if '_cart' in intor:
        libao2mo.CINTtot_cgto_cart.restype = ctypes.c_int
        nao = libao2mo.CINTtot_cgto_cart(c_bas.ctypes.data_as(ctypes.c_void_p),
                                         nbas)
        cgto_in_shell = _fpointer('CINTcgto_cart')
    elif '_sph' in intor:
        libao2mo.CINTtot_cgto_spheric.restype = ctypes.c_int
        nao = libao2mo.CINTtot_cgto_spheric(
            c_bas.ctypes.data_as(ctypes.c_void_p), nbas)
        cgto_in_shell = _fpointer('CINTcgto_spheric')
    else:
        raise NotImplementedError('cint2e spinor AO integrals')

    if aosym in ('s4', 's2ij'):
        nao_pair = nao * (nao + 1) // 2
    else:
        nao_pair = nao * nao
    if out is None:
        out = numpy.empty((comp, nkl, nao_pair))
    else:
        out = numpy.ndarray((comp, nkl, nao_pair), buffer=out)
    if out.size == 0:
        return out

    if ao2mopt is not None:
        cao2mopt = ao2mopt._this
        cintopt = ao2mopt._cintopt
        cintor = ao2mopt._intor
    else:
        cao2mopt = pyscf.lib.c_null_ptr()
        cintor = _fpointer(intor)
        cintopt = _vhf.make_cintopt(c_atm, c_bas, c_env, intor)

    fdrv = getattr(libao2mo, 'AO2MOnr_e1fill_drv')
    fill = _fpointer('AO2MOfill_nr_' + aosym)
    fdrv(cintor, cgto_in_shell, fill, out.ctypes.data_as(ctypes.c_void_p),
         ctypes.c_int(klsh0), ctypes.c_int(klsh1 - klsh0), ctypes.c_int(nkl),
         ctypes.c_int(comp), cintopt, cao2mopt,
         c_atm.ctypes.data_as(ctypes.c_void_p), natm,
         c_bas.ctypes.data_as(ctypes.c_void_p), nbas,
         c_env.ctypes.data_as(ctypes.c_void_p))
    return out