Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if isinstance(nelec, (int, numpy.integer)):
if shciobj.spin is None:
nelecb = nelec // 2
else:
nelecb = (nelec - shciobj.spin) // 2
neleca = nelec - nelecb
else :
neleca, nelecb = nelec
if shciobj.groupname is not None and shciobj.orbsym is not []:
# First removing the symmetry forbidden integrals. This has been done using
# the pyscf internal irrep-IDs (stored in shciobj.orbsym)
orbsym = numpy.asarray(shciobj.orbsym) % 10
pair_irrep = (orbsym.reshape(-1,1) ^ orbsym)[numpy.tril_indices(ncas)]
sym_forbid = pair_irrep.reshape(-1,1) != pair_irrep.ravel()
eri_cas = ao2mo.restore(4, eri_cas, ncas)
eri_cas[sym_forbid] = 0
eri_cas = ao2mo.restore(8, eri_cas, ncas)
# Convert the pyscf internal irrep-ID to molpro irrep-ID
orbsym = numpy.asarray(symmetry.convert_orbsym(shciobj.groupname, orbsym))
else:
orbsym = []
eri_cas = ao2mo.restore(8, eri_cas, ncas)
if not os.path.exists(shciobj.runtimedir):
os.makedirs(shciobj.runtimedir)
# The name of the FCIDUMP file, default is "FCIDUMP".
integralFile = os.path.join(shciobj.runtimedir, shciobj.integralfile)
tools.fcidump.from_integrals(integralFile, h1eff, eri_cas, ncas,
neleca+nelecb, ecore, ms=abs(neleca-nelecb),
orbsym=orbsym)
def pspace(h1e, eri, norb, nelec, hdiag, np=400):
if isinstance(nelec, int):
neleca = nelec/2
else:
neleca, nelecb = nelec
assert(neleca == nelecb)
eri = pyscf.ao2mo.restore(1, eri, norb)
na = cistring.num_strings(norb, neleca)
addr = numpy.argsort(hdiag)[:np]
# symmetrize addra/addrb
addra = addr / na
addrb = addr % na
stra = numpy.array([cistring.addr2str(norb,neleca,ia) for ia in addra],
dtype=numpy.long)
strb = numpy.array([cistring.addr2str(norb,neleca,ib) for ib in addrb],
dtype=numpy.long)
np = len(addr)
h0 = numpy.zeros((np,np))
libfci.FCIpspace_h0tril(h0.ctypes.data_as(ctypes.c_void_p),
h1e.ctypes.data_as(ctypes.c_void_p),
eri.ctypes.data_as(ctypes.c_void_p),
stra.ctypes.data_as(ctypes.c_void_p),
strb.ctypes.data_as(ctypes.c_void_p),
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.basis = '631g'
mol.build()
mf = scf.RHF(mol)
mf.conv_tol = 1e-16
mf.scf()
mo_coeff = mf.mo_coeff + np.sin(mf.mo_coeff) * .01j
nao = mo_coeff.shape[0]
eri = ao2mo.restore(1, mf._eri, nao)
eri0 = lib.einsum('pqrs,pi,qj,rk,sl->ijkl', eri, mo_coeff.conj(), mo_coeff,
mo_coeff.conj(), mo_coeff)
nocc, nvir = 5, nao-5
eris = _ChemistsERIs(mol)
eris.oooo = eri0[:nocc,:nocc,:nocc,:nocc].copy()
eris.ovoo = eri0[:nocc,nocc:,:nocc,:nocc].copy()
eris.oovv = eri0[:nocc,:nocc,nocc:,nocc:].copy()
eris.ovvo = eri0[:nocc,nocc:,nocc:,:nocc].copy()
eris.ovov = eri0[:nocc,nocc:,:nocc,nocc:].copy()
eris.ovvv = eri0[:nocc,nocc:,nocc:,nocc:].copy()
eris.vvvv = eri0[nocc:,nocc:,nocc:,nocc:].copy()
eris.fock = np.diag(mf.mo_energy)
eris.mo_energy = mf.mo_energy
np.random.seed(1)
blksize = min(nocc, max(ccsd.BLKMIN, int(max_memory*1e6/8/(nvir**3*3))))
tmp = np.zeros((nvir,nvir), dtype=dtype)
for p0,p1 in lib.prange(0, nocc, blksize):
ovvv = eris.get_ovvv(slice(p0,p1)) # ovvv = eris.ovvv[p0:p1]
tmp += np.einsum('mb,mbaa->ab', t1[p0:p1], ovvv)
Wvvaa += np.einsum('mb,maab->ab', t1[p0:p1], ovvv)
ovvv = None
Wvvaa -= tmp
Wvvab -= tmp
Wvvab -= tmp.T
Wvvaa = Wvvaa + Wvvaa.T
if eris.vvvv is None: # AO-direct CCSD, vvvv is not generated.
pass
elif len(eris.vvvv.shape) == 4: # DO NOT use .ndim here for h5py library
# backward compatbility
eris_vvvv = ao2mo.restore(1,np.asarray(eris.vvvv), t1.shape[1])
tmp = np.einsum('aabb->ab', eris_vvvv)
Wvvaa += tmp
Wvvaa -= np.einsum('abba->ab', eris_vvvv)
Wvvab += tmp
else:
for i in range(nvir):
i0 = i*(i+1)//2
vvv = lib.unpack_tril(np.asarray(eris.vvvv[i0:i0+i+1]))
tmp = np.einsum('bb->b', vvv[i])
Wvvaa[i] += tmp
Wvvab[i] += tmp
tmp = np.einsum('bb->b', vvv[:,:i+1,i])
Wvvaa[i,:i+1] -= tmp
Wvvaa[:i ,i] -= tmp[:i]
vvv = None
def contract_2e(eri, fcivec, norb, nelec, link_index=None, orbsym=None, wfnsym=0):
if orbsym is None:
return direct_spin0.contract_2e(eri, fcivec, norb, nelec, link_index)
eri = ao2mo.restore(4, eri, norb)
neleca, nelecb = direct_spin1._unpack_nelec(nelec)
assert(neleca == nelecb)
link_indexa = direct_spin0._unpack(norb, nelec, link_index)
na, nlinka = link_indexa.shape[:2]
eri_irs, rank_eri, irrep_eri = direct_spin1_symm.reorder_eri(eri, norb, orbsym)
strsa = numpy.asarray(cistring.gen_strings4orblist(range(norb), neleca))
aidx, link_indexa = direct_spin1_symm.gen_str_irrep(strsa, orbsym, link_indexa,
rank_eri, irrep_eri)
Tirrep = ctypes.c_void_p*TOTIRREPS
linka_ptr = Tirrep(*[x.ctypes.data_as(ctypes.c_void_p) for x in link_indexa])
eri_ptrs = Tirrep(*[x.ctypes.data_as(ctypes.c_void_p) for x in eri_irs])
dimirrep = (ctypes.c_int*TOTIRREPS)(*[x.shape[0] for x in eri_irs])
fcivec_shape = fcivec.shape
fcivec = fcivec.reshape((na,na), order='C')
log = logger.new_logger(myci, verbose)
if tol is None: tol = myci.conv_tol
if lindep is None: lindep = myci.lindep
if max_cycle is None: max_cycle = myci.max_cycle
if max_space is None: max_space = myci.max_space
if max_memory is None: max_memory = myci.max_memory
if nroots is None: nroots = myci.nroots
if myci.verbose >= logger.WARN:
myci.check_sanity()
nelec = direct_spin1._unpack_nelec(nelec, myci.spin)
ci0, nelec, ci_strs = _unpack(ci0, nelec, ci_strs)
na = len(ci_strs[0])
nb = len(ci_strs[1])
h2e = direct_spin1.absorb_h1e(h1e, eri, norb, nelec, .5)
h2e = ao2mo.restore(1, h2e, norb)
link_index = _all_linkstr_index(ci_strs, norb, nelec)
hdiag = myci.make_hdiag(h1e, eri, ci_strs, norb, nelec)
if isinstance(ci0, _SCIvector):
if ci0.size == na*nb:
ci0 = [ci0.ravel()]
else:
ci0 = [x.ravel() for x in ci0]
else:
ci0 = myci.get_init_guess(ci_strs, norb, nelec, nroots, hdiag)
def hop(c):
hc = myci.contract_2e(h2e, _as_SCIvector(c, ci_strs), norb, nelec, link_index)
return hc.reshape(-1)
precond = lambda x, e, *args: x/(hdiag-e+1e-4)
from pyscf import symm
from pyscf.dmrgscf import dmrg_sym
if (VMC.groupname == 'Dooh'
or VMC.groupname == 'Coov') and VMC.useExtraSymm:
coeffs, nRows, rowIndex, rowCoeffs, orbsym = D2htoDinfh(
VMC, norb, nelec)
newintt = numpy.tensordot(coeffs.conj(), h1eff, axes=([1], [0]))
newint1 = numpy.tensordot(newintt, coeffs, axes=([1], [1]))
newint1r = numpy.zeros(shape=(norb, norb), order='C')
for i in range(norb):
for j in range(norb):
newint1r[i, j] = newint1[i, j].real
int2 = pyscf.ao2mo.restore(1, eri_cas, norb)
eri_cas = numpy.zeros_like(int2)
transformDinfh(norb, numpy.ascontiguousarray(nRows, numpy.int32),
numpy.ascontiguousarray(rowIndex, numpy.int32),
numpy.ascontiguousarray(rowCoeffs, numpy.float64),
numpy.ascontiguousarray(int2, numpy.float64),
numpy.ascontiguousarray(eri_cas, numpy.float64))
writeIntNoSymm(norb, numpy.ascontiguousarray(newint1r, numpy.float64),
numpy.ascontiguousarray(eri_cas, numpy.float64),
ecore, neleca + nelecb,
numpy.asarray(orbsym, dtype=numpy.int32))
else:
if VMC.groupname is not None and VMC.orbsym is not []:
orbsym = dmrg_sym.convert_orbsym(VMC.groupname, VMC.orbsym)
def make_diagonal(eris):
mo_energy = eris.fock.diagonal()
nocc = eris.nocc
nmo = mo_energy.size
nvir = nmo - nocc
jdiag = numpy.zeros((nmo,nmo))
kdiag = numpy.zeros((nmo,nmo))
eris_vvvv = ao2mo.restore(1, eris.vvvv, nvir)
jdiag[:nocc,:nocc] = numpy.einsum('iijj->ij', eris.oooo)
kdiag[:nocc,:nocc] = numpy.einsum('jiij->ij', eris.oooo)
jdiag[nocc:,nocc:] = numpy.einsum('iijj->ij', eris_vvvv)
kdiag[nocc:,nocc:] = numpy.einsum('jiij->ij', eris_vvvv)
jdiag[:nocc,nocc:] = numpy.einsum('iijj->ji', eris.vvoo)
kdiag[:nocc,nocc:] = numpy.einsum('jiij->ij', eris.voov)
jksum = (jdiag[:nocc,:nocc] * 2 - kdiag[:nocc,:nocc]).sum()
ehf = mo_energy[:nocc].sum() * 2 - jksum
e1diag = numpy.empty((nocc,nvir))
e2diag = numpy.empty((nocc,nocc,nvir,nvir))
for i in range(nocc):
for a in range(nocc, nmo):
e1diag[i,a-nocc] = ehf - mo_energy[i] + mo_energy[a] \
- jdiag[i,a] + kdiag[i,a]
for j in range(nocc):
for b in range(nocc, nmo):
if self._scf._eri is not None and \
(nao*nao*nmo*nmo*12+self._scf._eri.size)*8/1e6 < self.max_memory*.95:
moab = numpy.hstack((mo_coeff[0], mo_coeff[1]))
na = mo_coeff[0].shape[1]
nab = moab.shape[1]
eri = pyscf.ao2mo.incore.full(self._scf._eri, moab)
eri = pyscf.ao2mo.restore(1, eri, nab)
eri_aa = eri[:na,:na,:na,:na].copy()
eri_ab = eri[:na,:na,na:,na:].copy()
eri_bb = eri[na:,na:,na:,na:].copy()
else:
moab = numpy.hstack((mo_coeff[0], mo_coeff[1]))
eri = pyscf.ao2mo.full(self.mol, mo_coeff, verbose=self.verbose)
na = mo_coeff[0].shape[1]
nab = moab.shape[1]
eri = pyscf.ao2mo.restore(1, eri, nab)
eri_aa = eri[:na,:na,:na,:na].copy()
eri_ab = eri[:na,:na,na:,na:].copy()
eri_bb = eri[na:,na:,na:,na:].copy()
return (eri_aa, eri_ab, eri_bb)