Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sunqm committed Sep 8, 2023
1 parent 5539c86 commit 5b06016
Show file tree
Hide file tree
Showing 24 changed files with 732 additions and 743 deletions.
9 changes: 8 additions & 1 deletion examples/cc/20-ip_ea_eom_ccsd.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python

'''
Ground-state and IP/EA-EOM-CCSD for singlet (RHF) and triplet (UHF) O2.
Ground-state, EOM-EE-GCCSD and IP/EA-EOM-CCSD for singlet (RHF) and triplet (UHF) O2.
'''

from pyscf import gto, scf, cc
Expand Down Expand Up @@ -49,3 +49,10 @@
eip,cip = mycc.ipccsd(nroots=1)
eea,cea = mycc.eaccsd(nroots=1)

# EOM-GCCSD
mf = mf.to_ghf()
mycc = mf.GCCSD()
ecc, t1, t2 = mycc.kernel()
e,v = mycc.ipccsd(nroots=6)
e,v = mycc.eaccsd(nroots=6)
e,v = mycc.eeccsd(nroots=6)
200 changes: 130 additions & 70 deletions pyscf/cc/ccsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ def kernel(mycc, eris=None, t1=None, t2=None, max_cycle=50, tol=1e-8,
elif t2 is None:
t2 = mycc.get_init_guess(eris)[1]

name = mycc.__class__.__name__
cput1 = cput0 = (logger.process_clock(), logger.perf_counter())
eold = 0
eccsd = mycc.energy(t1, t2, eris)
log.info('Init E_corr(CCSD) = %.15g', eccsd)
log.info('Init E_corr(%s) = %.15g', name, eccsd)

if isinstance(mycc.diis, lib.diis.DIIS):
adiis = mycc.diis
Expand Down Expand Up @@ -82,13 +83,13 @@ def kernel(mycc, eris=None, t1=None, t2=None, max_cycle=50, tol=1e-8,
t1new = t2new = None
t1, t2 = mycc.run_diis(t1, t2, istep, normt, eccsd-eold, adiis)
eold, eccsd = eccsd, mycc.energy(t1, t2, eris)
log.info('cycle = %d E_corr(CCSD) = %.15g dE = %.9g norm(t1,t2) = %.6g',
istep+1, eccsd, eccsd - eold, normt)
cput1 = log.timer('CCSD iter', *cput1)
log.info('cycle = %d E_corr(%s) = %.15g dE = %.9g norm(t1,t2) = %.6g',
istep+1, name, eccsd, eccsd - eold, normt)
cput1 = log.timer(f'{name} iter', *cput1)
if abs(eccsd-eold) < tol and normt < tolnormt:
conv = True
break
log.timer('CCSD', *cput0)
log.timer(name, *cput0)
return conv, eccsd, t1, t2


Expand Down Expand Up @@ -713,7 +714,8 @@ def energy(mycc, t1=None, t2=None, eris=None):
e += 2 * numpy.einsum('ijab,iabj', tau, eris_ovvo)
e -= numpy.einsum('jiab,iabj', tau, eris_ovvo)
if abs(e.imag) > 1e-4:
logger.warn(mycc, 'Non-zero imaginary part found in CCSD energy %s', e)
logger.warn(mycc, 'Non-zero imaginary part found in %s energy %s',
mycc.__class__.__name__, e)
return e.real

def restore_from_diis_(mycc, diis_file, inplace=True):
Expand Down Expand Up @@ -841,7 +843,7 @@ def __call__(self, mol_or_geom, **kwargs):
return self.e_tot


class CCSD(lib.StreamObject):
class CCSDBase(lib.StreamObject):
'''restricted CCSD
Attributes:
Expand Down Expand Up @@ -919,6 +921,7 @@ class CCSD(lib.StreamObject):
async_io = getattr(__config__, 'cc_ccsd_CCSD_async_io', True)
incore_complete = getattr(__config__, 'cc_ccsd_CCSD_incore_complete', False)
cc2 = getattr(__config__, 'cc_ccsd_CCSD_cc2', False)
callback = None

_keys = set((
'max_cycle', 'conv_tol', 'iterative_damping',
Expand All @@ -927,7 +930,7 @@ class CCSD(lib.StreamObject):
'async_io', 'incore_complete', 'cc2', 'callback',
'mol', 'verbose', 'stdout', 'frozen', 'level_shift',
'mo_coeff', 'mo_occ', 'converged', 'converged_lambda', 'emp2', 'e_hf',
'e_corr', 't1', 't2', 'l1', 'l2', 'chkfile', 'callback',
'e_corr', 't1', 't2', 'l1', 'l2', 'chkfile',
))

def __init__(self, mf, frozen=None, mo_coeff=None, mo_occ=None):
Expand Down Expand Up @@ -977,7 +980,6 @@ def __init__(self, mf, frozen=None, mo_coeff=None, mo_occ=None):
self._nocc = None
self._nmo = None
self.chkfile = mf.chkfile
self.callback = None

@property
def ecc(self):
Expand Down Expand Up @@ -1035,12 +1037,6 @@ def dump_flags(self, verbose=None):
log.info('diis_start_energy_diff = %g', self.diis_start_energy_diff)
log.info('max_memory %d MB (current use %d MB)',
self.max_memory, lib.current_memory()[0])
if (log.verbose >= logger.DEBUG1 and
self.__class__ == CCSD):
nocc = self.nocc
nvir = self.nmo - self.nocc
flops = _flops(nocc, nvir)
log.debug1('total FLOPs %s', flops)
return self

def get_init_guess(self, eris=None):
Expand Down Expand Up @@ -1113,78 +1109,45 @@ def _finalize(self):
as_scanner = as_scanner
restore_from_diis_ = restore_from_diis_


def solve_lambda(self, t1=None, t2=None, l1=None, l2=None,
eris=None):
from pyscf.cc import ccsd_lambda
if t1 is None: t1 = self.t1
if t2 is None: t2 = self.t2
if eris is None: eris = self.ao2mo(self.mo_coeff)
self.converged_lambda, self.l1, self.l2 = \
ccsd_lambda.kernel(self, eris, t1, t2, l1, l2,
max_cycle=self.max_cycle,
tol=self.conv_tol_normt,
verbose=self.verbose)
return self.l1, self.l2
def solve_lambda(self, t1=None, t2=None, l1=None, l2=None, eris=None):
raise NotImplementedError

def ccsd_t(self, t1=None, t2=None, eris=None):
from pyscf.cc import ccsd_t
if t1 is None: t1 = self.t1
if t2 is None: t2 = self.t2
if eris is None: eris = self.ao2mo(self.mo_coeff)
return ccsd_t.kernel(self, eris, t1, t2, self.verbose)
raise NotImplementedError

def ipccsd(self, nroots=1, left=False, koopmans=False, guess=None,
partition=None, eris=None):
from pyscf.cc import eom_rccsd
return eom_rccsd.EOMIP(self).kernel(nroots, left, koopmans, guess,
partition, eris)
raise NotImplementedError

def eaccsd(self, nroots=1, left=False, koopmans=False, guess=None,
partition=None, eris=None):
from pyscf.cc import eom_rccsd
return eom_rccsd.EOMEA(self).kernel(nroots, left, koopmans, guess,
partition, eris)
raise NotImplementedError

def eeccsd(self, nroots=1, koopmans=False, guess=None, eris=None):
from pyscf.cc import eom_rccsd
return eom_rccsd.EOMEE(self).kernel(nroots, koopmans, guess, eris)
raise NotImplementedError

def eomee_ccsd_singlet(self, nroots=1, koopmans=False, guess=None, eris=None):
from pyscf.cc import eom_rccsd
return eom_rccsd.EOMEESinglet(self).kernel(nroots, koopmans, guess, eris)
raise NotImplementedError

def eomee_ccsd_triplet(self, nroots=1, koopmans=False, guess=None, eris=None):
from pyscf.cc import eom_rccsd
return eom_rccsd.EOMEETriplet(self).kernel(nroots, koopmans, guess, eris)
raise NotImplementedError

def eomsf_ccsd(self, nroots=1, koopmans=False, guess=None, eris=None):
from pyscf.cc import eom_rccsd
return eom_rccsd.EOMEESpinFlip(self).kernel(nroots, koopmans, guess, eris)
raise NotImplementedError

def eomip_method(self):
from pyscf.cc import eom_rccsd
return eom_rccsd.EOMIP(self)
raise NotImplementedError

def eomea_method(self):
from pyscf.cc import eom_rccsd
return eom_rccsd.EOMEA(self)
raise NotImplementedError

def eomee_method(self):
from pyscf.cc import eom_rccsd
return eom_rccsd.EOMEE(self)
raise NotImplementedError

def make_rdm1(self, t1=None, t2=None, l1=None, l2=None, ao_repr=False,
with_frozen=True, with_mf=True):
'''Un-relaxed 1-particle density matrix in MO space'''
from pyscf.cc import ccsd_rdm
if t1 is None: t1 = self.t1
if t2 is None: t2 = self.t2
if l1 is None: l1 = self.l1
if l2 is None: l2 = self.l2
if l1 is None: l1, l2 = self.solve_lambda(t1, t2)
return ccsd_rdm.make_rdm1(self, t1, t2, l1, l2, ao_repr=ao_repr,
with_frozen=with_frozen, with_mf=with_mf)
raise NotImplementedError

def make_rdm2(self, t1=None, t2=None, l1=None, l2=None, ao_repr=False,
with_frozen=True, with_dm1=True):
Expand All @@ -1193,14 +1156,7 @@ def make_rdm2(self, t1=None, t2=None, l1=None, l2=None, ao_repr=False,
dm2[p,r,q,s] = <p^+ q^+ s r>
'''
from pyscf.cc import ccsd_rdm
if t1 is None: t1 = self.t1
if t2 is None: t2 = self.t2
if l1 is None: l1 = self.l1
if l2 is None: l2 = self.l2
if l1 is None: l1, l2 = self.solve_lambda(t1, t2)
return ccsd_rdm.make_rdm2(self, t1, t2, l1, l2, ao_repr=ao_repr,
with_frozen=with_frozen, with_dm1=with_dm1)
raise NotImplementedError

def ao2mo(self, mo_coeff=None):
# Pseudo code how eris are implemented:
Expand Down Expand Up @@ -1288,6 +1244,111 @@ def dump_chk(self, t1_t2=None, frozen=None, mo_coeff=None, mo_occ=None):

lib.chkfile.save(self.chkfile, 'ccsd', cc_chk)

def density_fit(self, auxbasis=None, with_df=None):
raise NotImplementedError

def nuc_grad_method(self):
raise NotImplementedError

class CCSD(CCSDBase):
__doc__ = CCSDBase.__doc__

def dump_flags(self, verbose=None):
CCSDBase.dump_flags(self, verbose)
if self.verbose >= logger.DEBUG1 and self.__class__ == CCSD:
nocc = self.nocc
nvir = self.nmo - self.nocc
flops = _flops(nocc, nvir)
logger.debug1(self, 'total FLOPs %s', flops)
return self

def solve_lambda(self, t1=None, t2=None, l1=None, l2=None, eris=None):
from pyscf.cc import ccsd_lambda
if t1 is None: t1 = self.t1
if t2 is None: t2 = self.t2
if eris is None: eris = self.ao2mo(self.mo_coeff)
self.converged_lambda, self.l1, self.l2 = \
ccsd_lambda.kernel(self, eris, t1, t2, l1, l2,
max_cycle=self.max_cycle,
tol=self.conv_tol_normt,
verbose=self.verbose)
return self.l1, self.l2

def ccsd_t(self, t1=None, t2=None, eris=None):
from pyscf.cc import ccsd_t
if t1 is None: t1 = self.t1
if t2 is None: t2 = self.t2
if eris is None: eris = self.ao2mo(self.mo_coeff)
return ccsd_t.kernel(self, eris, t1, t2, self.verbose)

def ipccsd(self, nroots=1, left=False, koopmans=False, guess=None,
partition=None, eris=None):
from pyscf.cc import eom_rccsd
return eom_rccsd.EOMIP(self).kernel(nroots, left, koopmans, guess,
partition, eris)

def eaccsd(self, nroots=1, left=False, koopmans=False, guess=None,
partition=None, eris=None):
from pyscf.cc import eom_rccsd
return eom_rccsd.EOMEA(self).kernel(nroots, left, koopmans, guess,
partition, eris)

def eeccsd(self, nroots=1, koopmans=False, guess=None, eris=None):
from pyscf.cc import eom_rccsd
return eom_rccsd.EOMEE(self).kernel(nroots, koopmans, guess, eris)

def eomee_ccsd_singlet(self, nroots=1, koopmans=False, guess=None, eris=None):
from pyscf.cc import eom_rccsd
return eom_rccsd.EOMEESinglet(self).kernel(nroots, koopmans, guess, eris)

def eomee_ccsd_triplet(self, nroots=1, koopmans=False, guess=None, eris=None):
from pyscf.cc import eom_rccsd
return eom_rccsd.EOMEETriplet(self).kernel(nroots, koopmans, guess, eris)

def eomsf_ccsd(self, nroots=1, koopmans=False, guess=None, eris=None):
from pyscf.cc import eom_rccsd
return eom_rccsd.EOMEESpinFlip(self).kernel(nroots, koopmans, guess, eris)

def eomip_method(self):
from pyscf.cc import eom_rccsd
return eom_rccsd.EOMIP(self)

def eomea_method(self):
from pyscf.cc import eom_rccsd
return eom_rccsd.EOMEA(self)

def eomee_method(self):
from pyscf.cc import eom_rccsd
return eom_rccsd.EOMEE(self)

def make_rdm1(self, t1=None, t2=None, l1=None, l2=None, ao_repr=False,
with_frozen=True, with_mf=True):
'''Un-relaxed 1-particle density matrix in MO space'''
from pyscf.cc import ccsd_rdm
if t1 is None: t1 = self.t1
if t2 is None: t2 = self.t2
if l1 is None: l1 = self.l1
if l2 is None: l2 = self.l2
if l1 is None: l1, l2 = self.solve_lambda(t1, t2)
return ccsd_rdm.make_rdm1(self, t1, t2, l1, l2, ao_repr=ao_repr,
with_frozen=with_frozen, with_mf=with_mf)

def make_rdm2(self, t1=None, t2=None, l1=None, l2=None, ao_repr=False,
with_frozen=True, with_dm1=True):
'''2-particle density matrix in MO space. The density matrix is
stored as
dm2[p,r,q,s] = <p^+ q^+ s r>
'''
from pyscf.cc import ccsd_rdm
if t1 is None: t1 = self.t1
if t2 is None: t2 = self.t2
if l1 is None: l1 = self.l1
if l2 is None: l2 = self.l2
if l1 is None: l1, l2 = self.solve_lambda(t1, t2)
return ccsd_rdm.make_rdm2(self, t1, t2, l1, l2, ao_repr=ao_repr,
with_frozen=with_frozen, with_dm1=with_dm1)

def density_fit(self, auxbasis=None, with_df=None):
from pyscf.cc import dfccsd
mycc = dfccsd.RCCSD(self._scf, self.frozen, self.mo_coeff, self.mo_occ)
Expand Down Expand Up @@ -1315,7 +1376,6 @@ def get_d2_diagnostic(self, t2=None):
if t2 is None: t2 = self.t2
return get_d2_diagnostic(t2)


CC = RCCSD = CCSD


Expand Down
5 changes: 3 additions & 2 deletions pyscf/cc/ccsd_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ def kernel(mycc, eris=None, t1=None, t2=None, l1=None, l2=None,
adiis.space = mycc.diis_space
else:
adiis = None
cput0 = log.timer('CCSD lambda initialization', *cput0)
name = mycc.__class__.__name__
cput0 = log.timer(f'{name} lambda initialization', *cput0)

conv = False
for istep in range(max_cycle):
Expand All @@ -68,7 +69,7 @@ def kernel(mycc, eris=None, t1=None, t2=None, l1=None, l2=None,
l1new = l2new = None
l1, l2 = mycc.run_diis(l1, l2, istep, normt, 0, adiis)
log.info('cycle = %d norm(lambda1,lambda2) = %.6g', istep+1, normt)
cput0 = log.timer('CCSD iter', *cput0)
cput0 = log.timer(f'{name} iter', *cput0)
if normt < tol:
conv = True
break
Expand Down
11 changes: 6 additions & 5 deletions pyscf/cc/ccsd_t.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def kernel(mycc, eris, t1=None, t2=None, verbose=logger.NOTE):
if t1 is None: t1 = mycc.t1
if t2 is None: t2 = mycc.t2

name = mycc.__class__.__name__
nocc, nvir = t1.shape
nmo = nocc + nvir

Expand All @@ -52,7 +53,7 @@ def kernel(mycc, eris, t1=None, t2=None, verbose=logger.NOTE):

mo_energy, t1T, t2T, vooo, fvo, restore_t2_inplace = \
_sort_t2_vooo_(mycc, orbsym, t1, t2, eris)
cpu1 = log.timer_debug1('CCSD(T) sort_eri', *cpu1)
cpu1 = log.timer_debug1(f'{name}(T) sort_eri', *cpu1)

cpu2 = list(cpu1)
orbsym = numpy.hstack((numpy.sort(orbsym[:nocc]),numpy.sort(orbsym[nocc:])))
Expand Down Expand Up @@ -124,11 +125,11 @@ def contract(a0, a1, b0, b1, cache):
t2 = restore_t2_inplace(t2T)
et_sum *= 2
if abs(et_sum[0].imag) > 1e-4:
logger.warn(mycc, 'Non-zero imaginary part of CCSD(T) energy was found %s',
et_sum[0])
logger.warn(mycc, 'Non-zero imaginary part of %s(T) energy was found %s',
name, et_sum[0])
et = et_sum[0].real
log.timer('CCSD(T)', *cpu0)
log.note('CCSD(T) correction = %.15g', et)
log.timer(f'{name}(T)', *cpu0)
log.note('%s(T) correction = %.15g', name, et)
return et

def _sort_eri(mycc, eris, nocc, nvir, vvop, log):
Expand Down
Loading

0 comments on commit 5b06016

Please sign in to comment.