Skip to content

Commit

Permalink
Using lr_eigh in PBC-tdscf modules
Browse files Browse the repository at this point in the history
  • Loading branch information
sunqm committed Aug 27, 2024
1 parent a033b14 commit 5941338
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 39 deletions.
25 changes: 10 additions & 15 deletions pyscf/pbc/tdscf/krhf.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from pyscf.lib import linalg_helper
from pyscf.lib import logger
from pyscf.tdscf import rhf
from pyscf.tdscf._lr_eig import eig as lr_eig
from pyscf.tdscf._lr_eig import eigh as lr_eigh, eig as lr_eig
from pyscf.pbc import scf
from pyscf.pbc.tdscf.rhf import TDBase
from pyscf.pbc.scf import _response_functions # noqa
Expand Down Expand Up @@ -155,6 +155,8 @@ def add_hf_(a, b, hyb=1):
return a, b

class KTDBase(TDBase):
conv_tol = getattr(__config__, 'pbc_tdscf_rhf_TDA_conv_tol', 1e-4)

_keys = {'kconserv', 'kshift_lst'}

def __init__(self, mf, kshift_lst=None):
Expand Down Expand Up @@ -184,7 +186,6 @@ def dump_flags(self, verbose=None):
log.info('conv_tol = %g', self.conv_tol)
log.info('eigh lindep = %g', self.lindep)
log.info('eigh level_shift = %g', self.level_shift)
log.info('eigh max_space = %d', self.max_space)
log.info('eigh max_cycle = %d', self.max_cycle)
log.info('chkfile = %s', self.chkfile)
log.info('max_memory %d MB (current use %d MB)',
Expand Down Expand Up @@ -215,7 +216,6 @@ def _finalize(self):
get_nto = lib.invalid_method('get_nto')

class TDA(KTDBase):
conv_tol = getattr(__config__, 'pbc_tdscf_rhf_TDA_conv_tol', 1e-6)

@lib.with_doc(get_ab.__doc__)
def get_ab(self, mf=None, kshift=0):
Expand Down Expand Up @@ -325,13 +325,10 @@ def pickeig(w, v, nroots, envs):
else:
x0k = x0[i]

converged, e, x1 = \
lib.davidson1(vind, x0k, precond,
tol=self.conv_tol,
max_cycle=self.max_cycle,
nroots=self.nstates, lindep=self.lindep,
max_space=self.max_space, pick=pickeig,
verbose=self.verbose)
converged, e, x1 = lr_eigh(
vind, x0k, precond, tol_residual=self.conv_tol, lindep=self.lindep,
nroots=self.nstates, pick=pickeig, max_cycle=self.max_cycle,
max_memory=self.max_memory, verbose=log)
self.converged.append( converged )
self.e.append( e )
# 1/sqrt(2) because self.x is for alpha excitation amplitude and 2(X^+*X) = 1
Expand All @@ -345,8 +342,6 @@ def pickeig(w, v, nroots, envs):

class TDHF(KTDBase):

conv_tol = 1e-5

@lib.with_doc(get_ab.__doc__)
def get_ab(self, mf=None, kshift=0):
if mf is None: mf = self._scf
Expand Down Expand Up @@ -483,9 +478,9 @@ def norm_xy(z, kconserv):
x0k = x0[i]

converged, e, x1 = lr_eig(
vind, x0k, precond, tol_residual=self.conv_tol, nroots=self.nstates,
lindep=self.lindep, max_cycle=self.max_cycle,
max_space=self.max_space, pick=pickeig, verbose=log)
vind, x0k, precond, tol_residual=self.conv_tol, lindep=self.lindep,
nroots=self.nstates, pick=pickeig, max_cycle=self.max_cycle,
max_memory=self.max_memory, verbose=log)
self.converged.append( converged )
self.e.append( e )
self.xy.append( [norm_xy(z, kconserv) for z in x1] )
Expand Down
20 changes: 8 additions & 12 deletions pyscf/pbc/tdscf/kuhf.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from pyscf import lib
from pyscf.lib import logger
from pyscf.tdscf import uhf
from pyscf.tdscf._lr_eig import eig as lr_eig
from pyscf.tdscf._lr_eig import eigh as lr_eigh, eig as lr_eig
from pyscf.pbc import scf
from pyscf.pbc.tdscf.krhf import KTDBase, _get_e_ia
from pyscf.pbc.lib.kpts_helper import is_gamma_point
Expand Down Expand Up @@ -146,14 +146,10 @@ def pickeig(w, v, nroots, envs):
else:
x0k = x0[i]

converged, e, x1 = \
lib.davidson1(vind, x0k, precond,
tol=self.conv_tol,
max_cycle=self.max_cycle,
nroots=self.nstates,
lindep=self.lindep,
max_space=self.max_space, pick=pickeig,
verbose=self.verbose)
converged, e, x1 = lr_eigh(
vind, x0k, precond, tol_residual=self.conv_tol, lindep=self.lindep,
nroots=self.nstates, pick=pickeig, max_cycle=self.max_cycle,
max_memory=self.max_memory, verbose=log)
self.converged.append( converged )
self.e.append( e )
self.xy.append( [(_unpack(xi, mo_occ, kconserv), # (X_alpha, X_beta)
Expand Down Expand Up @@ -296,9 +292,9 @@ def pickeig(w, v, nroots, envs):
x0k = x0[i]

converged, w, x1 = lr_eig(
vind, x0k, precond, tol_residual=self.conv_tol, nroots=self.nstates,
lindep=self.lindep, max_cycle=self.max_cycle,
max_space=self.max_space, pick=pickeig, verbose=log)
vind, x0k, precond, tol_residual=self.conv_tol, lindep=self.lindep,
nroots=self.nstates, pick=pickeig, max_cycle=self.max_cycle,
max_memory=self.max_memory, verbose=log)
self.converged.append( converged )

e = []
Expand Down
1 change: 0 additions & 1 deletion pyscf/tdscf/dhf.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,6 @@ def vind(xys):

class TDHF(TDBase):

conv_tol = 1e-4
singlet = None

@lib.with_doc(gen_tdhf_operation.__doc__)
Expand Down
1 change: 0 additions & 1 deletion pyscf/tdscf/ghf.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,6 @@ def vind(xys):

class TDHF(TDBase):

conv_tol = 1e-4
singlet = None

@lib.with_doc(gen_tdhf_operation.__doc__)
Expand Down
6 changes: 1 addition & 5 deletions pyscf/tdscf/rhf.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,15 +660,14 @@ class TDBase(lib.StreamObject):
singlet = getattr(__config__, 'tdscf_rhf_TDA_singlet', True)
lindep = getattr(__config__, 'tdscf_rhf_TDA_lindep', 1e-12)
level_shift = getattr(__config__, 'tdscf_rhf_TDA_level_shift', 0)
max_space = getattr(__config__, 'tdscf_rhf_TDA_max_space', 50)
max_cycle = getattr(__config__, 'tdscf_rhf_TDA_max_cycle', 100)
# Low excitation filter to avoid numerical instability
positive_eig_threshold = getattr(__config__, 'tdscf_rhf_TDDFT_positive_eig_threshold', 1e-3)
# Threshold to handle degeneracy in init guess
deg_eia_thresh = getattr(__config__, 'tdscf_rhf_TDDFT_deg_eia_thresh', 1e-3)

_keys = {
'conv_tol', 'nstates', 'singlet', 'lindep', 'level_shift', 'max_space',
'conv_tol', 'nstates', 'singlet', 'lindep', 'level_shift',
'max_cycle', 'mol', 'chkfile', 'wfnsym', 'converged', 'e', 'xy',
}

Expand Down Expand Up @@ -716,7 +715,6 @@ def dump_flags(self, verbose=None):
log.info('conv_tol = %g', self.conv_tol)
log.info('eigh lindep = %g', self.lindep)
log.info('eigh level_shift = %g', self.level_shift)
log.info('eigh max_space = %d', self.max_space)
log.info('eigh max_cycle = %d', self.max_cycle)
log.info('chkfile = %s', self.chkfile)
log.info('max_memory %d MB (current use %d MB)',
Expand Down Expand Up @@ -1007,8 +1005,6 @@ class TDHF(TDBase):
normalized to 1 for UHF/UKS methods. In the TDA calculation, Y = 0.
'''

conv_tol = 1e-4

@lib.with_doc(gen_tdhf_operation.__doc__)
def gen_vind(self, mf=None):
if mf is None:
Expand Down
10 changes: 6 additions & 4 deletions pyscf/tdscf/test/test_tddks.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,15 @@ def test_mcol_mgga_ab_ks(self):
mcol_m06l = dft.UDKS(mol).set(xc='m06l', collinear='mcol')
mcol_m06l._numint.spin_samples = 6
mcol_m06l.__dict__.update(scf.chkfile.load(mf_lda.chkfile, 'scf'))
self._check_against_ab_ks(mcol_m06l.TDDFT(), 14.934345395514491, 9.539340104227188)
self._check_against_ab_ks(mcol_m06l.TDDFT())

def _check_against_ab_ks(self, td, refa, refb):
def _check_against_ab_ks(self, td, refa=None, refb=None):
mf = td._scf
a, b = td.get_ab()
self.assertAlmostEqual(lib.fp(abs(a)), refa, 4)
self.assertAlmostEqual(lib.fp(abs(b)), refb, 4)
if refa is not None:
self.assertAlmostEqual(lib.fp(abs(a)), refa, 4)
if refb is not None:
self.assertAlmostEqual(lib.fp(abs(b)), refb, 4)
ftda = mf.TDA().gen_vind()[0]
ftdhf = td.gen_vind()[0]
n2c = mf.mo_occ.size // 2
Expand Down
1 change: 0 additions & 1 deletion pyscf/tdscf/uhf.py
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,6 @@ def vind(xys):

class TDHF(TDBase):

conv_tol = 1e-4
singlet = None

@lib.with_doc(gen_tdhf_operation.__doc__)
Expand Down

0 comments on commit 5941338

Please sign in to comment.