Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Examples:
>>> from pyscf import gto, scf
>>> mol = gto.M(atom='H 0 0 0; H 0 0 1.1')
>>> mf = scf.RHF(mol)
>>> mf.scf()
>>> dm = mf.make_rdm1()
>>> scf.hf.energy_elec(mf, dm)
(-1.5176090667746334, 0.60917167853723675)
'''
if dm is None: dm = mf.make_rdm1()
if h1e is None: h1e = mf.get_hcore()
if vhf is None: vhf = mf.get_veff(mf.mol, dm)
e1 = numpy.einsum('ji,ji', h1e.conj(), dm).real
e_coul = numpy.einsum('ji,ji', vhf.conj(), dm).real * .5
logger.debug(mf, 'E_coul = %.15g', e_coul)
return e1+e_coul, e_coul
>>> mf = scf.RHF(mol)
>>> mf.scf()
>>> dm = mf.make_rdm1()
>>> scf.hf.energy_elec(mf, dm)
(-1.5176090667746334, 0.60917167853723675)
>>> mf.energy_elec(dm)
(-1.5176090667746334, 0.60917167853723675)
'''
if dm is None: dm = mf.make_rdm1()
if h1e is None: h1e = mf.get_hcore()
if vhf is None: vhf = mf.get_veff(mf.mol, dm)
e1 = numpy.einsum('ij,ji->', h1e, dm)
e_coul = numpy.einsum('ij,ji->', vhf, dm) * .5
mf.scf_summary['e1'] = e1.real
mf.scf_summary['e2'] = e_coul.real
logger.debug(mf, 'E1 = %s E_coul = %s', e1, e_coul)
return (e1+e_coul).real, e_coul
tol=tol, max_memory=self.max_memory)[1]
return ci1, None
elif not (hasattr(self.fcisolver, 'contract_2e') and
hasattr(self.fcisolver, 'absorb_h1e')):
fn = self.fcisolver.kernel
ci1 = fn(h1, h2, ncas, nelecas, ci0=ci0,
tol=tol, max_memory=self.max_memory,
max_cycle=self.ci_response_space)[1]
return ci1, None
h2eff = self.fcisolver.absorb_h1e(h1, h2, ncas, nelecas, .5)
hc = self.fcisolver.contract_2e(h2eff, ci0, ncas, nelecas).ravel()
g = hc - (e_ci-ecore) * ci0.ravel()
if self.ci_response_space > 7:
logger.debug(self, 'CI step by full response')
# full response
max_memory = max(400, self.max_memory-lib.current_memory()[0])
e, ci1 = self.fcisolver.kernel(h1, h2, ncas, nelecas, ci0=ci0,
tol=tol, max_memory=max_memory)
else:
nd = min(max(self.ci_response_space, 2), ci0.size)
logger.debug(self, 'CI step by %dD subspace response', nd)
xs = [ci0.ravel()]
ax = [hc]
heff = numpy.empty((nd,nd))
seff = numpy.empty((nd,nd))
heff[0,0] = numpy.dot(xs[0], ax[0])
seff[0,0] = 1
for i in range(1, nd):
xs.append(ax[i-1] - xs[i-1] * e_ci)
ax.append(self.fcisolver.contract_2e(h2eff, xs[i], ncas,
mol = self.mol
natm = mol.natm
lmax = self.lmax
r_vdw = self.get_atomic_radii()
coords_1sph, weights_1sph = make_grids_one_sphere(self.lebedev_order)
ylm_1sph = numpy.vstack(sph.real_sph_vec(coords_1sph, lmax, True))
fi = make_fi(self, r_vdw)
ui = 1 - fi
ui[ui<0] = 0
nexposed = numpy.count_nonzero(ui==1)
nbury = numpy.count_nonzero(ui==0)
on_shell = numpy.count_nonzero(ui>0) - nexposed
logger.debug(self, 'Num points exposed %d', nexposed)
logger.debug(self, 'Num points buried %d', nbury)
logger.debug(self, 'Num points on shell %d', on_shell)
nlm = (lmax+1)**2
Lmat = make_L(self, r_vdw, ylm_1sph, fi)
Lmat = Lmat.reshape(natm*nlm,-1)
cached_pol = cache_fake_multipoles(self.grids, r_vdw, lmax)
self._intermediates = {
'r_vdw': r_vdw,
'ylm_1sph': ylm_1sph,
'ui': ui,
'Lmat': Lmat,
'cached_pol': cached_pol,
}
nelecas = casscf.nelecas
if isinstance(ncore, (int, numpy.integer)):
return fci.spin_op.spin_square0(ci, ncas, nelecas)
else:
if mo_coeff is None: mo_coeff = casscf.mo_coeff
if ovlp is None: ovlp = casscf._scf.get_ovlp()
nocc = (ncore[0] + ncas, ncore[1] + ncas)
mocas = (mo_coeff[0][:,ncore[0]:nocc[0]], mo_coeff[1][:,ncore[1]:nocc[1]])
if isinstance(ci, (list, tuple, RANGE_TYPE)):
sscas = numpy.array([fci.spin_op.spin_square(c, ncas, nelecas, mocas, ovlp)[0]
for c in ci])
else:
sscas = fci.spin_op.spin_square(ci, ncas, nelecas, mocas, ovlp)[0]
mocore = (mo_coeff[0][:,:ncore[0]], mo_coeff[1][:,:ncore[1]])
sscore = casscf._scf.spin_square(mocore, ovlp)[0]
logger.debug(casscf, 'S^2 of core %s S^2 of cas %s', sscore, sscas)
ss = sscas+sscore
s = numpy.sqrt(ss+.25) - .5
return ss, s*2+1
def calc_tot_elec_energy(self, veff, dm, mo_energy, mo_occ):
sum_mo_energy = numpy.dot(mo_energy, mo_occ)
coul_dup = numpy.einsum('ij,ji', dm, veff)
tot_e = sum_mo_energy - coul_dup + self._ecoul + self._exc
log.debug(self, 'Ecoul = %s Exc = %s', self._ecoul, self._exc)
return tot_e, self._ecoul, self._exc
logger.debug(mol, 'irreps of each MO %s', orbsym)
if check:
largest_norm = norm[iridx,numpy.arange(nmo)]
orbidx = numpy.where(largest_norm < 1-tol)[0]
if orbidx.size > 0:
idx = numpy.where(largest_norm < 1-tol*1e2)[0]
if idx.size > 0:
raise ValueError('orbitals %s not symmetrized, norm = %s' %
(idx, largest_norm[idx]))
else:
logger.warn(mol, 'orbitals %s not strictly symmetrized.',
numpy.unique(orbidx))
logger.warn(mol, 'They can be symmetrized with '
'pyscf.symm.symmetrize_space function.')
logger.debug(mol, 'norm = %s', largest_norm[orbidx])
return orbsym
with open(configfile) as f:
# filter out comments
raw_js = []
balance = 0
data = [x for x in f.readlines()
if not x.startswith('#') and x.rstrip()]
for n, line in enumerate(data):
if not line.lstrip().startswith('#'):
raw_js.append(line)
balance += line.count('{') - line.count('}')
if balance == 0:
break
raw_py = ''.join(data[n+1:])
raw_js = ''.join(raw_js)
logger.debug(casscf, 'Reading CASSCF parameters from config file %s',
os.path.realpath(configfile))
logger.debug1(casscf, ' Inject casscf settings %s', raw_js)
conf = json.loads(raw_js)
casscf.__dict__.update(conf.pop('casscf'))
# Not yet found a way to update locals() on the runtime
# https://docs.python.org/2/library/functions.html#locals
#for k in conf:
# if k in envs:
# logger.info(casscf, 'Update envs[%s] = %s', k, conf[k])
# envs[k] = conf[k]
logger.debug1(casscf, ' Inject python script\n%s\n', raw_py)
if len(raw_py.strip()) > 0:
if sys.version_info >= (3,):
# A hacky call using eval because exec are so different in python2 and python3
# DO NOT use numpy.array for mo_occ_kpts and mo_energy_kpts, they may
# have different dimensions for different k-points
if is_uhf:
if is_khf:
nao_tot = mo_occs.size//2
mo_occ_kpts =(partition_occ(mo_occs[:nao_tot], mo_energy_kpts[0]),
partition_occ(mo_occs[nao_tot:], mo_energy_kpts[1]))
else:
mo_occ_kpts = partition_occ(mo_occs, mo_energy_kpts)
else: # rhf and ghf
if is_khf:
mo_occ_kpts = partition_occ(mo_occs, mo_energy_kpts)
else:
mo_occ_kpts = mo_occs
logger.debug(mf, ' Fermi level %g Sum mo_occ_kpts = %s should equal nelec = %s',
fermi, mo_occs.sum(), nelectron)
logger.info(mf, ' sigma = %g Optimized mu = %.12g entropy = %.12g',
mf.sigma, mu, mf.entropy)
return mo_occ_kpts