def eval_ao(mol, coords, isgga=False, relativity=0, bastart=0, bascount=None, non0tab=None, verbose=None): assert(coords.flags.c_contiguous) natm = ctypes.c_int(mol._atm.shape[0]) nbas = ctypes.c_int(mol.nbas) nao = mol.nao_nr() ngrids = len(coords) if bascount is None: bascount = mol.nbas - bastart if isgga: ao = numpy.empty((4, ngrids,nao)) # plain, dx, dy, dz feval = _ctypes.dlsym(libdft._handle, 'VXCeval_nr_gto_grad') else: ao = numpy.empty((ngrids,nao)) feval = _ctypes.dlsym(libdft._handle, 'VXCeval_nr_gto') if non0tab is None: non0tab = numpy.ones(((ngrids+BLKSIZE-1)//BLKSIZE,mol.nbas), dtype=numpy.int8) libdft.VXCeval_ao_drv(ctypes.c_void_p(feval), ctypes.c_int(nao), ctypes.c_int(ngrids), ctypes.c_int(bastart), ctypes.c_int(bascount), ctypes.c_int(BLKSIZE), ao.ctypes.data_as(ctypes.c_void_p), coords.ctypes.data_as(ctypes.c_void_p), non0tab.ctypes.data_as(ctypes.c_void_p), mol._atm.ctypes.data_as(ctypes.c_void_p), natm, mol._bas.ctypes.data_as(ctypes.c_void_p), nbas, mol._env.ctypes.data_as(ctypes.c_void_p)) return ao
def runjk(dm1, ncomp, intorname, filldot, *namejk): fdrv = getattr(libcvhf2, 'CVHFnr_direct_drv') intor = ctypes.c_void_p(_ctypes.dlsym(libcvhf2._handle, intorname)) fdot = ctypes.c_void_p(_ctypes.dlsym(libcvhf2._handle, filldot)) njk = len(namejk) if dm1.ndim == 2: n_dm = 1 dm1 = (dm1,) else: n_dm = dm1.shape[0] vjk = numpy.zeros((njk,n_dm*ncomp,nao,nao)) fjk = (ctypes.c_void_p*(njk*n_dm))() dmsptr = (ctypes.c_void_p*(njk*n_dm))() vjkptr = (ctypes.c_void_p*(njk*n_dm))() for i, symb in enumerate(namejk): f1 = ctypes.c_void_p(_ctypes.dlsym(libcvhf2._handle, symb)) for j in range(n_dm): dmsptr[i*n_dm+j] = dm1[j].ctypes.data_as(ctypes.c_void_p) vjkptr[i*n_dm+j] = vjk[i,j*ncomp].ctypes.data_as(ctypes.c_void_p) fjk[i*n_dm+j] = f1 shls_slice = (ctypes.c_int*8)(*([0, mol.nbas]*4)) fdrv(intor, fdot, fjk, dmsptr, vjkptr, ctypes.c_int(njk*n_dm), ctypes.c_int(ncomp), shls_slice, ao_loc.ctypes.data_as(ctypes.c_void_p), cintopt, cvhfopt, 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)) if n_dm * ncomp == 1: vjk = vjk.reshape(njk,nao,nao) return vjk
def _trans(vin, i0, icount, j0, jcount, out=None): nrow = vin.shape[0] fdrv = getattr(_ccsd.libcc, 'AO2MOnr_e2_drv') pao_loc = ctypes.POINTER(ctypes.c_void_p)() ftrans = ctypes.c_void_p(_ctypes.dlsym(_ccsd.libcc._handle, 'AO2MOtranse2_nr_s1')) fmmm = ctypes.c_void_p(_ctypes.dlsym(_ccsd.libcc._handle, 'CCmmm_transpose_sum')) fdrv(ftrans, fmmm, out.ctypes.data_as(ctypes.c_void_p), vin.ctypes.data_as(ctypes.c_void_p), mo_coeff.ctypes.data_as(ctypes.c_void_p), ctypes.c_int(nrow), ctypes.c_int(nao), ctypes.c_int(i0), ctypes.c_int(icount), ctypes.c_int(j0), ctypes.c_int(jcount), pao_loc, ctypes.c_int(0)) return out
def make_dm123(fname, cibra, ciket, norb, nelec): r'''Spin traced 1, 2 and 3-particle density matrices. .. note:: The 2pdm is :math:`\langle p^\dagger q^\dagger s r\rangle` but is stored as [p,r,q,s]; The 3pdm is :math:`\langle p^\dagger q^\dagger r^\dagger u t s\rangle`, stored as [p,s,q,t,r,u]. ''' if isinstance(nelec, (int, numpy.integer)): neleca = nelecb = nelec//2 else: neleca, nelecb = nelec link_indexa = cistring.gen_linkstr_index(range(norb), neleca) link_indexb = cistring.gen_linkstr_index(range(norb), nelecb) na,nlinka = link_indexa.shape[:2] nb,nlinkb = link_indexb.shape[:2] rdm1 = numpy.empty((norb,)*2) rdm2 = numpy.empty((norb,)*4) rdm3 = numpy.empty((norb,)*6) kernel = _ctypes.dlsym(librdm._handle, fname) librdm.FCIrdm3_drv(ctypes.c_void_p(kernel), rdm1.ctypes.data_as(ctypes.c_void_p), rdm2.ctypes.data_as(ctypes.c_void_p), rdm3.ctypes.data_as(ctypes.c_void_p), cibra.ctypes.data_as(ctypes.c_void_p), ciket.ctypes.data_as(ctypes.c_void_p), ctypes.c_int(norb), ctypes.c_int(na), ctypes.c_int(nb), ctypes.c_int(nlinka), ctypes.c_int(nlinkb), link_indexa.ctypes.data_as(ctypes.c_void_p), link_indexb.ctypes.data_as(ctypes.c_void_p)) rdm3 = _complete_dm3_(rdm2, rdm3) return rdm1, rdm2, rdm3
def make_dm123(fname, cibra, ciket, norb, nelec): r'''Spin traced 1, 2 and 3-particle density matrices. .. note:: The 2pdm is :math:`\langle p^\dagger q^\dagger r s\rangle` but is stored as [p,s,q,r]; The 3pdm is :math:`\langle p^\dagger q^\dagger r^\dagger s t u\rangle`, stored as [p,u,q,t,r,s]. ''' cibra = numpy.asarray(cibra, order='C') ciket = numpy.asarray(ciket, order='C') if isinstance(nelec, (int, numpy.number)): neleca = nelecb = nelec // 2 else: neleca, nelecb = nelec link_indexa = cistring.gen_linkstr_index(range(norb), neleca) link_indexb = cistring.gen_linkstr_index(range(norb), nelecb) na, nlinka = link_indexa.shape[:2] nb, nlinkb = link_indexb.shape[:2] rdm1 = numpy.empty((norb, ) * 2) rdm2 = numpy.empty((norb, ) * 4) rdm3 = numpy.empty((norb, ) * 6) kernel = _ctypes.dlsym(librdm._handle, fname) librdm.FCIrdm3_drv(ctypes.c_void_p(kernel), rdm1.ctypes.data_as(ctypes.c_void_p), rdm2.ctypes.data_as(ctypes.c_void_p), rdm3.ctypes.data_as(ctypes.c_void_p), cibra.ctypes.data_as(ctypes.c_void_p), ciket.ctypes.data_as(ctypes.c_void_p), ctypes.c_int(norb), ctypes.c_int(na), ctypes.c_int(nb), ctypes.c_int(nlinka), ctypes.c_int(nlinkb), link_indexa.ctypes.data_as(ctypes.c_void_p), link_indexb.ctypes.data_as(ctypes.c_void_p)) rdm3 = _complete_dm3_(rdm2, rdm3) return rdm1, rdm2, rdm3
def make_rdm12_spin1(fname, cibra, ciket, norb, nelec, link_index=None, symm=0): cibra = numpy.asarray(cibra, order='C') ciket = numpy.asarray(ciket, order='C') if link_index is None: if isinstance(nelec, (int, numpy.number)): nelecb = nelec // 2 neleca = nelec - nelecb else: neleca, nelecb = nelec link_indexa = cistring.gen_linkstr_index(range(norb), neleca) link_indexb = cistring.gen_linkstr_index(range(norb), nelecb) else: link_indexa, link_indexb = link_index na, nlinka = link_indexa.shape[:2] nb, nlinkb = link_indexb.shape[:2] rdm1 = numpy.empty((norb, norb)) rdm2 = numpy.empty((norb, norb, norb, norb)) fn = _ctypes.dlsym(librdm._handle, fname) librdm.FCIrdm12_drv(ctypes.c_void_p(fn), rdm1.ctypes.data_as(ctypes.c_void_p), rdm2.ctypes.data_as(ctypes.c_void_p), cibra.ctypes.data_as(ctypes.c_void_p), ciket.ctypes.data_as(ctypes.c_void_p), ctypes.c_int(norb), ctypes.c_int(na), ctypes.c_int(nb), ctypes.c_int(nlinka), ctypes.c_int(nlinkb), link_indexa.ctypes.data_as(ctypes.c_void_p), link_indexb.ctypes.data_as(ctypes.c_void_p), ctypes.c_int(symm)) return rdm1, rdm2
def condense(opname, a, locs): ''' .. code-block:: python nd = loc[-1] out = numpy.empty((nd,nd)) for i,i0 in enumerate(loc): i1 = loc[i+1] for j,j0 in enumerate(loc): j1 = loc[j+1] out[i,j] = op(a[i0:i1,j0:j1]) return out ''' assert (a.flags.c_contiguous) assert (a.dtype == numpy.double) if not opname.startswith('NP_'): opname = 'NP_' + opname op = ctypes.c_void_p(_ctypes.dlsym(_np_helper._handle, opname)) locs = numpy.asarray(locs, numpy.int32) nloc = locs.size - 1 out = numpy.empty((nloc, nloc)) _np_helper.NPcondense(op, out.ctypes.data_as(ctypes.c_void_p), a.ctypes.data_as(ctypes.c_void_p), locs.ctypes.data_as(ctypes.c_void_p), ctypes.c_int(nloc)) return out
def make_rdm12_spin1(fname, cibra, ciket, norb, nelec, link_index=None, symm=0): assert(cibra.flags.c_contiguous) assert(ciket.flags.c_contiguous) if isinstance(nelec, (int, numpy.integer)): neleca = nelecb = nelec//2 else: neleca, nelecb = nelec if link_index is None: link_indexa = cistring.gen_linkstr_index(range(norb), neleca) link_indexb = cistring.gen_linkstr_index(range(norb), nelecb) else: link_indexa, link_indexb = link_index na,nlinka = link_indexa.shape[:2] nb,nlinkb = link_indexb.shape[:2] rdm1 = numpy.empty((norb,norb)) rdm2 = numpy.empty((norb,norb,norb,norb)) fn = _ctypes.dlsym(librdm._handle, fname) librdm.FCIrdm12_drv(ctypes.c_void_p(fn), rdm1.ctypes.data_as(ctypes.c_void_p), rdm2.ctypes.data_as(ctypes.c_void_p), cibra.ctypes.data_as(ctypes.c_void_p), ciket.ctypes.data_as(ctypes.c_void_p), ctypes.c_int(norb), ctypes.c_int(na), ctypes.c_int(nb), ctypes.c_int(nlinka), ctypes.c_int(nlinkb), link_indexa.ctypes.data_as(ctypes.c_void_p), link_indexb.ctypes.data_as(ctypes.c_void_p), ctypes.c_int(symm)) return rdm1, rdm2
def make_rdm12_spin1(fname, cibra, ciket, norb, nelec, link_index=None, symm=0): cibra = numpy.asarray(cibra, order='C') ciket = numpy.asarray(ciket, order='C') if link_index is None: if isinstance(nelec, (int, numpy.number)): nelecb = nelec//2 neleca = nelec - nelecb else: neleca, nelecb = nelec link_indexa = cistring.gen_linkstr_index(range(norb), neleca) link_indexb = cistring.gen_linkstr_index(range(norb), nelecb) else: link_indexa, link_indexb = link_index na,nlinka = link_indexa.shape[:2] nb,nlinkb = link_indexb.shape[:2] rdm1 = numpy.empty((norb,norb)) rdm2 = numpy.empty((norb,norb,norb,norb)) fn = _ctypes.dlsym(librdm._handle, fname) librdm.FCIrdm12_drv(ctypes.c_void_p(fn), rdm1.ctypes.data_as(ctypes.c_void_p), rdm2.ctypes.data_as(ctypes.c_void_p), cibra.ctypes.data_as(ctypes.c_void_p), ciket.ctypes.data_as(ctypes.c_void_p), ctypes.c_int(norb), ctypes.c_int(na), ctypes.c_int(nb), ctypes.c_int(nlinka), ctypes.c_int(nlinkb), link_indexa.ctypes.data_as(ctypes.c_void_p), link_indexb.ctypes.data_as(ctypes.c_void_p), ctypes.c_int(symm)) return rdm1, rdm2
def make_dm1234(fname, cibra, ciket, norb, nelec): if isinstance(nelec, (int, numpy.integer)): neleca = nelecb = nelec//2 else: neleca, nelecb = nelec link_indexa = cistring.gen_linkstr_index(range(norb), neleca) link_indexb = cistring.gen_linkstr_index(range(norb), nelecb) na,nlinka = link_indexa.shape[:2] nb,nlinkb = link_indexb.shape[:2] rdm1 = numpy.empty((norb,)*2) rdm2 = numpy.empty((norb,)*4) rdm3 = numpy.empty((norb,)*6) rdm4 = numpy.empty((norb,)*8) librdm.FCIrdm4_drv(ctypes.c_void_p(_ctypes.dlsym(librdm._handle, fname)), rdm1.ctypes.data_as(ctypes.c_void_p), rdm2.ctypes.data_as(ctypes.c_void_p), rdm3.ctypes.data_as(ctypes.c_void_p), rdm4.ctypes.data_as(ctypes.c_void_p), cibra.ctypes.data_as(ctypes.c_void_p), ciket.ctypes.data_as(ctypes.c_void_p), ctypes.c_int(norb), ctypes.c_int(na), ctypes.c_int(nb), ctypes.c_int(nlinka), ctypes.c_int(nlinkb), link_indexa.ctypes.data_as(ctypes.c_void_p), link_indexb.ctypes.data_as(ctypes.c_void_p)) rdm3 = _complete_dm3_(rdm2, rdm3) rdm4 = _complete_dm4_(rdm3, rdm4) return rdm1, rdm2, rdm3, rdm4
def _make_rdm2_abba(fcivec, norb, nelec): if isinstance(nelec, (int, numpy.integer)): neleca = nelecb = nelec // 2 else: neleca, nelecb = nelec if nelecb == norb or neleca == 0: # no intermediate determinants return numpy.zeros((norb,norb,norb,norb)) acre_index = cistring.gen_cre_str_index(range(norb), neleca-1) bdes_index = cistring.gen_des_str_index(range(norb), nelecb+1) instra = cistring.num_strings(norb, neleca-1) nb = cistring.num_strings(norb, nelecb) dm1 = numpy.empty((norb,norb)) dm2 = numpy.empty((norb,norb,norb,norb)) fn = _ctypes.dlsym(librdm._handle, 'FCIdm2_abba_kern') librdm.FCIspindm12_drv(ctypes.c_void_p(fn), dm1.ctypes.data_as(ctypes.c_void_p), dm2.ctypes.data_as(ctypes.c_void_p), fcivec.ctypes.data_as(ctypes.c_void_p), fcivec.ctypes.data_as(ctypes.c_void_p), ctypes.c_int(norb), ctypes.c_int(instra), ctypes.c_int(nb), ctypes.c_int(neleca), ctypes.c_int(nelecb), acre_index.ctypes.data_as(ctypes.c_void_p), bdes_index.ctypes.data_as(ctypes.c_void_p)) return dm2
def _contract4pdm(kern, eri, civec, norb, nelec, link_index=None): if isinstance(nelec, (int, numpy.integer)): neleca = nelecb = nelec//2 else: neleca, nelecb = nelec if link_index is None: link_indexa = fci.cistring.gen_linkstr_index(range(norb), neleca) link_indexb = fci.cistring.gen_linkstr_index(range(norb), nelecb) else: link_indexa, link_indexb = link_index na,nlinka = link_indexa.shape[:2] nb,nlinkb = link_indexb.shape[:2] fdm2 = numpy.empty((norb,norb,norb,norb)) fdm3 = numpy.empty((norb,norb,norb,norb,norb,norb)) eri = numpy.ascontiguousarray(eri) libmc.NEVPTcontract(ctypes.c_void_p(_ctypes.dlsym(libmc._handle, kern)), fdm2.ctypes.data_as(ctypes.c_void_p), fdm3.ctypes.data_as(ctypes.c_void_p), eri.ctypes.data_as(ctypes.c_void_p), civec.ctypes.data_as(ctypes.c_void_p), ctypes.c_int(norb), ctypes.c_int(na), ctypes.c_int(nb), ctypes.c_int(nlinka), ctypes.c_int(nlinkb), link_indexa.ctypes.data_as(ctypes.c_void_p), link_indexb.ctypes.data_as(ctypes.c_void_p)) for i in range(norb): for j in range(i): fdm3[j,:,i] = fdm3[i,:,j].transpose(1,0,2,3) fdm3[j,i,i,:] += fdm2[j,:] fdm3[j,:,i,j] -= fdm2[i,:] return fdm3
def condense(opname, a, locs): ''' .. code-block:: python nd = loc[-1] out = numpy.empty((nd,nd)) for i,i0 in enumerate(loc): i1 = loc[i+1] for j,j0 in enumerate(loc): j1 = loc[j+1] out[i,j] = op(a[i0:i1,j0:j1]) return out ''' assert(a.flags.c_contiguous) assert(a.dtype == numpy.double) if not opname.startswith('NP_'): opname = 'NP_' + opname op = ctypes.c_void_p(_ctypes.dlsym(_np_helper._handle, opname)) locs = numpy.asarray(locs, numpy.int32) nloc = locs.size - 1 out = numpy.empty((nloc,nloc)) _np_helper.NPcondense(op, out.ctypes.data_as(ctypes.c_void_p), a.ctypes.data_as(ctypes.c_void_p), locs.ctypes.data_as(ctypes.c_void_p), ctypes.c_int(nloc)) return out
def getints1e(intor_name, atm, bas, env, bras=None, kets=None, comp=1, hermi=0): if bras is None: bralst = numpy.arange(len(bas), dtype=numpy.int32) else: bralst = numpy.asarray(bras, dtype=numpy.int32) assert(bralst.max() < len(bas)) if kets is None: ketlst = numpy.arange(len(bas), dtype=numpy.int32) else: ketlst = numpy.asarray(kets, dtype=numpy.int32) assert(ketlst.max() < len(bas)) 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) c_natm = ctypes.c_int(atm.shape[0]) c_nbas = ctypes.c_int(bas.shape[0]) nbra = len(bralst) nket = len(ketlst) if '_cart' in intor_name: dtype = numpy.double num_cgto_of = libcgto.CINTcgto_cart drv = libcgto.GTO1eintor_cart elif '_sph' in intor_name: dtype = numpy.double num_cgto_of = libcgto.CINTcgto_spheric drv = libcgto.GTO1eintor_sph else: dtype = numpy.complex num_cgto_of = libcgto.CINTcgto_spinor drv = libcgto.GTO1eintor_spinor 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]) mat = numpy.empty((comp,naoi,naoj), dtype) fnaddr = ctypes.c_void_p(_ctypes.dlsym(libcgto._handle, intor_name)) drv(fnaddr, mat.ctypes.data_as(ctypes.c_void_p), ctypes.c_int(comp), ctypes.c_int(hermi), 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), c_atm, c_natm, c_bas, c_nbas, c_env) if comp == 1: mat = mat.reshape(naoi,naoj) if hermi == 0: return mat else: if comp == 1: pyscf.lib.hermi_triu_(mat, hermi=hermi) else: for i in range(comp): pyscf.lib.hermi_triu_(mat[i], hermi=hermi) return mat
def call_with_fpointer(fdistname, coord, charge): import _ctypes coord = numpy.ascontiguousarray(coord) charge = numpy.ascontiguousarray(charge) cenergy = ctypes.CDLL('libcenergy.so') fn = getattr(cenergy, 'runcallback') fn.restype = ctypes.c_double fptr = ctypes.c_void_p(_ctypes.dlsym(cenergy._handle, fdistname)) nc = len(charge) e = 0 for i in range(nc): for j in range(i): e += fn(coord[i].ctypes.data_as(ctypes.c_void_p), coord[j].ctypes.data_as(ctypes.c_void_p), ctypes.c_double(charge[i]), ctypes.c_double(charge[j]), fptr) return e
def make_dm1234(fname, cibra, ciket, norb, nelec): r'''Spin traced 1, 2, 3 and 4-particle density matrices. .. note:: The 2pdm is :math:`\langle p^\dagger q^\dagger s r\rangle` but is stored as [p,r,q,s]; The 3pdm is :math:`\langle p^\dagger q^\dagger r^\dagger u t s\rangle`, stored as [p,s,q,t,r,u]; The 4pdm is :math:`\langle p^\dagger q^\dagger r^\dagger s^dagger w v u t\rangle`, stored as [p,w,q,v,r,u,s,t]. ''' cibra = numpy.asarray(cibra, order='C') ciket = numpy.asarray(ciket, order='C') if isinstance(nelec, (int, numpy.number)): neleca = nelecb = nelec//2 else: neleca, nelecb = nelec link_indexa = cistring.gen_linkstr_index(range(norb), neleca) link_indexb = cistring.gen_linkstr_index(range(norb), nelecb) na,nlinka = link_indexa.shape[:2] nb,nlinkb = link_indexb.shape[:2] rdm1 = numpy.empty((norb,)*2) rdm2 = numpy.empty((norb,)*4) rdm3 = numpy.empty((norb,)*6) rdm4 = numpy.empty((norb,)*8) librdm.FCIrdm4_drv(ctypes.c_void_p(_ctypes.dlsym(librdm._handle, fname)), rdm1.ctypes.data_as(ctypes.c_void_p), rdm2.ctypes.data_as(ctypes.c_void_p), rdm3.ctypes.data_as(ctypes.c_void_p), rdm4.ctypes.data_as(ctypes.c_void_p), cibra.ctypes.data_as(ctypes.c_void_p), ciket.ctypes.data_as(ctypes.c_void_p), ctypes.c_int(norb), ctypes.c_int(na), ctypes.c_int(nb), ctypes.c_int(nlinka), ctypes.c_int(nlinkb), link_indexa.ctypes.data_as(ctypes.c_void_p), link_indexb.ctypes.data_as(ctypes.c_void_p)) rdm3 = _complete_dm3_(rdm2, rdm3) rdm4 = _complete_dm4_(rdm3, rdm4) return rdm1, rdm2, rdm3, rdm4
def _make_rdm2_baab(fcivec, norb, nelec): fcivec = numpy.asarray(fcivec, order='C') neleca, nelecb = _unpack(nelec) if neleca == norb or nelecb == 0: # no intermediate determinants return numpy.zeros((norb,norb,norb,norb)) ades_index = cistring.gen_des_str_index(range(norb), neleca+1) bcre_index = cistring.gen_cre_str_index(range(norb), nelecb-1) instra = cistring.num_strings(norb, neleca+1) nb = cistring.num_strings(norb, nelecb) dm1 = numpy.empty((norb,norb)) dm2 = numpy.empty((norb,norb,norb,norb)) fn = _ctypes.dlsym(librdm._handle, 'FCIdm2_baab_kern') librdm.FCIspindm12_drv(ctypes.c_void_p(fn), dm1.ctypes.data_as(ctypes.c_void_p), dm2.ctypes.data_as(ctypes.c_void_p), fcivec.ctypes.data_as(ctypes.c_void_p), fcivec.ctypes.data_as(ctypes.c_void_p), ctypes.c_int(norb), ctypes.c_int(instra), ctypes.c_int(nb), ctypes.c_int(neleca), ctypes.c_int(nelecb), ades_index.ctypes.data_as(ctypes.c_void_p), bcre_index.ctypes.data_as(ctypes.c_void_p)) return dm2
def _make_rdm2_abba(fcivec, norb, nelec): fcivec = numpy.asarray(fcivec, order='C') neleca, nelecb = _unpack(nelec) if nelecb == norb or neleca == 0: # no intermediate determinants return numpy.zeros((norb,norb,norb,norb)) acre_index = cistring.gen_cre_str_index(range(norb), neleca-1) bdes_index = cistring.gen_des_str_index(range(norb), nelecb+1) instra = cistring.num_strings(norb, neleca-1) nb = cistring.num_strings(norb, nelecb) dm1 = numpy.empty((norb,norb)) dm2 = numpy.empty((norb,norb,norb,norb)) fn = _ctypes.dlsym(librdm._handle, 'FCIdm2_abba_kern') librdm.FCIspindm12_drv(ctypes.c_void_p(fn), dm1.ctypes.data_as(ctypes.c_void_p), dm2.ctypes.data_as(ctypes.c_void_p), fcivec.ctypes.data_as(ctypes.c_void_p), fcivec.ctypes.data_as(ctypes.c_void_p), ctypes.c_int(norb), ctypes.c_int(instra), ctypes.c_int(nb), ctypes.c_int(neleca), ctypes.c_int(nelecb), acre_index.ctypes.data_as(ctypes.c_void_p), bdes_index.ctypes.data_as(ctypes.c_void_p)) return dm2
def eval_ao(mol, coords, isgga=False, relativity=0, bastart=0, bascount=None, non0tab=None, verbose=None): '''Evaluate AO function value on the given grids, for LDA and GGA functional. Args: mol : an instance of :class:`Mole` coords : 2D array, shape (N,3) The coordinates of the grids. Kwargs: isgga : bool Whether to evalute the AO gradients for GGA functional. It affects the shape of the return array. If isgga=False, the returned AO values are stored in a (N,nao) array. Otherwise the AO values are stored in an array of shape (4,N,nao). Here N is the number of grids, nao is the number of AO functions. relativity : bool No effects. bastart, bascount : int If given, only part of AOs (bastart <= shell_id < bastart+bascount) are evaluated. non0tab : 2D bool array mask array to indicate whether the AO values are zero. The mask array can be obtained by calling :func:`make_mask` verbose : int or object of :class:`Logger` No effects. Returns: 2D array of shape (N,nao) for AO values if isgga is False. Or 3D array of shape (4,N,nao) for AO values and AO gradients if isgga is True. In the 3D array, the first (N,nao) elements are the AO values. The following (3,N,nao) are the AO gradients for x,y,z compoents. Examples: >>> mol = gto.M(atom='O 0 0 0; H 0 0 1; H 0 1 0', basis='ccpvdz') >>> coords = numpy.random.random((100,3)) # 100 random points >>> ao_value = eval_ao(mol, coords) >>> print(ao_value.shape) (100, 24) >>> ao_value = eval_ao(mol, coords, isgga=True, bastart=1, bascount=3) >>> print(ao_value.shape) (4, 100, 7) ''' assert(coords.flags.c_contiguous) natm = ctypes.c_int(mol._atm.shape[0]) nbas = ctypes.c_int(mol.nbas) ngrids = len(coords) if bascount is None: bascount = mol.nbas - bastart nao = mol.nao_nr() else: nao_bound = mol.nao_nr_range(bastart, bastart+bascount) nao = nao_bound[1] - nao_bound[0] if isgga: ao = numpy.empty((4, ngrids,nao)) # plain, dx, dy, dz feval = _ctypes.dlsym(libdft._handle, 'VXCeval_nr_gto_grad') else: ao = numpy.empty((ngrids,nao)) feval = _ctypes.dlsym(libdft._handle, 'VXCeval_nr_gto') if non0tab is None: non0tab = numpy.ones(((ngrids+BLKSIZE-1)//BLKSIZE,mol.nbas), dtype=numpy.int8) libdft.VXCeval_ao_drv(ctypes.c_void_p(feval), ctypes.c_int(nao), ctypes.c_int(ngrids), ctypes.c_int(bastart), ctypes.c_int(bascount), ctypes.c_int(BLKSIZE), ao.ctypes.data_as(ctypes.c_void_p), coords.ctypes.data_as(ctypes.c_void_p), non0tab.ctypes.data_as(ctypes.c_void_p), mol._atm.ctypes.data_as(ctypes.c_void_p), natm, mol._bas.ctypes.data_as(ctypes.c_void_p), nbas, mol._env.ctypes.data_as(ctypes.c_void_p)) return ao
def func_address(adll, name): if 'dlsym' in dir(_ctypes): return _ctypes.dlsym(adll._handle, name) else: return _ctypes.GetProcAddress(adll._handle, name)
def set_vkscreen(opt, name): opt._this.contents.r_vkscreen = \ ctypes.c_void_p(_ctypes.dlsym(_vhf.libcvhf._handle, name))
def getints(intor_name, atm, bas, env, bras=None, kets=None, comp=1, hermi=0): r'''One electron integral generator. Args: intor_name : str Name of the 1-electron integral. The list of 1e integrals in current version of libcint (v2.5.1) ========================== ========= ============= Function type Expression ========================== ========= ============= "cint1e_ovlp_sph" spheric ( \| \) "cint1e_nuc_sph" spheric ( \| nuc \| \) "cint1e_kin_sph" spheric (.5 \| p dot p\) "cint1e_ia01p_sph" spheric (#C(0 1) \| nabla-rinv \| cross p\) "cint1e_giao_irjxp_sph" spheric (#C(0 1) \| r cross p\) "cint1e_cg_irxp_sph" spheric (#C(0 1) \| rc cross p\) "cint1e_giao_a11part_sph" spheric (-.5 \| nabla-rinv \| r\) "cint1e_cg_a11part_sph" spheric (-.5 \| nabla-rinv \| rc\) "cint1e_a01gp_sph" spheric (g \| nabla-rinv cross p \|\) "cint1e_igkin_sph" spheric (#C(0 .5) g \| p dot p\) "cint1e_igovlp_sph" spheric (#C(0 1) g \|\) "cint1e_ignuc_sph" spheric (#C(0 1) g \| nuc \|\) "cint1e_z_sph" spheric ( \| zc \| \) "cint1e_zz_sph" spheric ( \| zc zc \| \) "cint1e_r_sph" spheric ( \| rc \| \) "cint1e_r2_sph" spheric ( \| rc dot rc \| \) "cint1e_rr_sph" spheric ( \| rc rc \| \) "cint1e_pnucp_sph" spheric (p* \| nuc dot p \| \) "cint1e_prinvxp_sph" spheric (p* \| rinv cross p \| \) "cint1e_ovlp" spinor ( \| \) "cint1e_nuc" spinor ( \| nuc \|\) "cint1e_srsr" spinor (sigma dot r \| sigma dot r\) "cint1e_sr" spinor (sigma dot r \|\) "cint1e_srsp" spinor (sigma dot r \| sigma dot p\) "cint1e_spsp" spinor (sigma dot p \| sigma dot p\) "cint1e_sp" spinor (sigma dot p \|\) "cint1e_spnucsp" spinor (sigma dot p \| nuc \| sigma dot p\) "cint1e_srnucsr" spinor (sigma dot r \| nuc \| sigma dot r\) "cint1e_govlp" spinor (g \|\) "cint1e_gnuc" spinor (g \| nuc \|\) "cint1e_cg_sa10sa01" spinor (.5 sigma cross rc \| sigma cross nabla-rinv \|\) "cint1e_cg_sa10sp" spinor (.5 rc cross sigma \| sigma dot p\) "cint1e_cg_sa10nucsp" spinor (.5 rc cross sigma \| nuc \| sigma dot p\) "cint1e_giao_sa10sa01" spinor (.5 sigma cross r \| sigma cross nabla-rinv \|\) "cint1e_giao_sa10sp" spinor (.5 r cross sigma \| sigma dot p\) "cint1e_giao_sa10nucsp" spinor (.5 r cross sigma \| nuc \| sigma dot p\) "cint1e_sa01sp" spinor (\| nabla-rinv cross sigma \| sigma dot p\) "cint1e_spgsp" spinor (g sigma dot p \| sigma dot p\) "cint1e_spgnucsp" spinor (g sigma dot p \| nuc \| sigma dot p\) "cint1e_spgsa01" spinor (g sigma dot p \| nabla-rinv cross sigma \|\) "cint1e_spspsp" spinor (sigma dot p \| sigma dot p sigma dot p\) "cint1e_spnuc" spinor (sigma dot p \| nuc \|\) "cint1e_ovlp_cart" cartesian ( \| \) "cint1e_nuc_cart" cartesian ( \| nuc \| \) "cint1e_kin_cart" cartesian (.5 \| p dot p\) "cint1e_ia01p_cart" cartesian (#C(0 1) \| nabla-rinv \| cross p\) "cint1e_giao_irjxp_cart" cartesian (#C(0 1) \| r cross p\) "cint1e_cg_irxp_cart" cartesian (#C(0 1) \| rc cross p\) "cint1e_giao_a11part_cart" cartesian (-.5 \| nabla-rinv \| r\) "cint1e_cg_a11part_cart" cartesian (-.5 \| nabla-rinv \| rc\) "cint1e_a01gp_cart" cartesian (g \| nabla-rinv cross p \|\) "cint1e_igkin_cart" cartesian (#C(0 .5) g \| p dot p\) "cint1e_igovlp_cart" cartesian (#C(0 1) g \|\) "cint1e_ignuc_cart" cartesian (#C(0 1) g \| nuc \|\) "cint1e_ipovlp_sph" spheric (nabla \|\) "cint1e_ipkin_sph" spheric (.5 nabla \| p dot p\) "cint1e_ipnuc_sph" spheric (nabla \| nuc \|\) "cint1e_iprinv_sph" spheric (nabla \| rinv \|\) "cint1e_rinv_sph" spheric (\| rinv \|\) "cint1e_ipovlp" spinor (nabla \|\) "cint1e_ipkin" spinor (.5 nabla \| p dot p\) "cint1e_ipnuc" spinor (nabla \| nuc \|\) "cint1e_iprinv" spinor (nabla \| rinv \|\) "cint1e_ipspnucsp" spinor (nabla sigma dot p \| nuc \| sigma dot p\) "cint1e_ipsprinvsp" spinor (nabla sigma dot p \| rinv \| sigma dot p\) "cint1e_ipovlp_cart" cartesian (nabla \|\) "cint1e_ipkin_cart" cartesian (.5 nabla \| p dot p\) "cint1e_ipnuc_cart" cartesian (nabla \| nuc \|\) "cint1e_iprinv_cart" cartesian (nabla \| rinv \|\) "cint1e_rinv_cart" cartesian (\| rinv \|\) ========================== ========= ============= atm : int32 ndarray libcint integral function argument bas : int32 ndarray libcint integral function argument env : float64 ndarray libcint integral function argument Kwargs: bras : list of int shell ids for bra. Default is all shells given by bas kets : list of int shell ids for ket. Default is all shells given by bas comp : int Components of the integrals, e.g. cint1e_ipovlp has 3 components. hermi : int Symmetry of the integrals | 0 : no symmetry assumed (default) | 1 : hermitian | 2 : anti-hermitian Returns: ndarray of 1-electron integrals, can be either 2-dim or 3-dim, depending on comp Examples: >>> mol.build(atom='H 0 0 0; H 0 0 1.1', basis='sto-3g') >>> gto.moleintor('cint1e_ipnuc_sph', mol._atm, mol._bas, mol._env, comp=3) # <nabla i | V_nuc | j> [[[ 0. 0. ] [ 0. 0. ]] [[ 0. 0. ] [ 0. 0. ]] [[ 0.10289944 0.48176097] [-0.48176097 -0.10289944]]] ''' nbas = len(bas) if bras is None: bras = range(nbas) else: assert(max(bras) < len(bas)) if kets is None: kets = range(nbas) else: assert(max(kets) < len(bas)) 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) c_natm = ctypes.c_int(atm.shape[0]) c_nbas = ctypes.c_int(bas.shape[0]) nbra = len(bras) nket = len(kets) if '_cart' in intor_name: dtype = numpy.double num_cgto_of = _cint.CINTcgto_cart c_intor = _cint.GTO1eintor_cart elif '_sph' in intor_name: dtype = numpy.double num_cgto_of = _cint.CINTcgto_spheric c_intor = _cint.GTO1eintor_sph else: dtype = numpy.complex num_cgto_of = _cint.CINTcgto_spinor c_intor = _cint.GTO1eintor_spinor naoi = sum([num_cgto_of(ctypes.c_int(i), c_bas) for i in bras]) naoj = sum([num_cgto_of(ctypes.c_int(i), c_bas) for i in kets]) bralst = numpy.array(bras, dtype=numpy.int32) ketlst = numpy.array(kets, dtype=numpy.int32) mat = numpy.empty((comp,naoi,naoj), dtype) fnaddr = ctypes.c_void_p(_ctypes.dlsym(_cint._handle, intor_name)) c_intor(fnaddr, mat.ctypes.data_as(ctypes.c_void_p), ctypes.c_int(comp), \ ctypes.c_int(hermi), \ bralst.ctypes.data_as(ctypes.c_void_p), ctypes.c_int(nbra), ketlst.ctypes.data_as(ctypes.c_void_p), ctypes.c_int(nket), c_atm, c_natm, c_bas, c_nbas, c_env) if comp == 1: mat = mat.reshape(naoi,naoj) if hermi == 0: return mat else: if comp == 1: pyscf.lib.hermi_triu(mat, hermi=hermi) else: for i in range(comp): pyscf.lib.hermi_triu(mat[i], hermi=hermi) return mat
def f1pointer(name): return ctypes.c_void_p(_ctypes.dlsym(libri1._handle, name))
def __getitem__(self, name): return Address(dlsym(self.dll._handle, name))
def _fpointer(name): return ctypes.c_void_p(_ctypes.dlsym(libcgto._handle, name))