Skip to content

Commit

Permalink
max_cycle=0 bug in SOSCF (fix pyscf#2529)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunqm committed Nov 25, 2024
1 parent f2671ad commit 40c17bb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pyscf/soscf/newton_ah.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,11 +508,15 @@ def kernel(mf, mo_coeff=None, mo_occ=None, dm=None,
# Save mo_coeff and mo_occ because they are needed by function rotate_mo
mf.mo_coeff, mf.mo_occ = mo_coeff, mo_occ

scf_conv = False
e_tot = mf._scf.energy_tot(dm, h1e, vhf)
fock = mf.get_fock(h1e, s1e, vhf, dm, level_shift_factor=0)
log.info('Initial guess E= %.15g |g|= %g', e_tot,
numpy.linalg.norm(mf._scf.get_grad(mo_coeff, mo_occ, fock)))

if mf.max_cycle <= 0:
return scf_conv, e_tot, mo_energy, mo_coeff, mo_occ

if dump_chk and mf.chkfile:
chkfile.save_mol(mol, mf.chkfile)

Expand All @@ -525,7 +529,6 @@ def kernel(mf, mo_coeff=None, mo_occ=None, dm=None,
next(rotaiter) # start the iterator
kftot = jktot = 0
norm_gorb = 0.
scf_conv = False
cput1 = log.timer('initializing second order scf', *cput0)

for imacro in range(max_cycle):
Expand Down
9 changes: 9 additions & 0 deletions pyscf/soscf/test/test_newton_ah.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,15 @@ def test_nr_uhf(self):
nr.conv_tol_grad = 1e-5
self.assertAlmostEqual(nr.kernel(), -75.58051984397145, 9)

nr1 = scf.newton(mf)
nr1.max_cycle = 0 # Should reproduce energy of the initial guess
nr1.kernel()
self.assertAlmostEqual(nr1.e_tot, mf.e_tot, 12)

nr1.max_cycle = 2
nr1.kernel()
self.assertAlmostEqual(nr1.e_tot, nr.e_tot, 12)

def test_nr_uhf_cart(self):
mol = h2o_z1.copy()
mol.cart = True
Expand Down

0 comments on commit 40c17bb

Please sign in to comment.