Skip to content

Commit

Permalink
casci grad crash for small max_memory. Fix pyscf#1909
Browse files Browse the repository at this point in the history
  • Loading branch information
sunqm committed Oct 22, 2023
1 parent 231da24 commit cf09e6b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
6 changes: 3 additions & 3 deletions pyscf/grad/casci.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,15 @@ def fvind(x):
eri1tmp = eri1tmp.reshape(p1-p0,nf,nao,nao)
de[k,i] -= numpy.einsum('ijkl,ij,kl', eri1tmp, hf_dm1[p0:p1,q0:q1], zvec_ao) * 2
de[k,i] -= numpy.einsum('ijkl,kl,ij', eri1tmp, hf_dm1, zvec_ao[p0:p1,q0:q1]) * 2
de[k,i] += numpy.einsum('ijkl,il,kj', eri1tmp, hf_dm1[p0:p1], zvec_ao[q0:q1])
de[k,i] += numpy.einsum('ijkl,il,kj', eri1tmp, hf_dm1[p0:p1], zvec_ao[:,q0:q1])
de[k,i] += numpy.einsum('ijkl,jk,il', eri1tmp, hf_dm1[q0:q1], zvec_ao[p0:p1])

#:vhf1c, vhf1a = mc_grad.get_veff(mol, (dm_core, dm_cas))
#:de[k] += numpy.einsum('xij,ij->x', vhf1c[:,p0:p1], casci_dm1[p0:p1]) * 2
#:de[k] += numpy.einsum('xij,ij->x', vhf1a[:,p0:p1], dm_core[p0:p1]) * 2
de[k,i] -= numpy.einsum('ijkl,lk,ij', eri1tmp, dm_core[q0:q1], casci_dm1[p0:p1]) * 2
de[k,i] -= numpy.einsum('ijkl,lk,ij', eri1tmp, dm_core, casci_dm1[p0:p1,q0:q1]) * 2
de[k,i] += numpy.einsum('ijkl,jk,il', eri1tmp, dm_core[q0:q1], casci_dm1[p0:p1])
de[k,i] -= numpy.einsum('ijkl,lk,ij', eri1tmp, dm_cas[q0:q1], dm_core[p0:p1]) * 2
de[k,i] -= numpy.einsum('ijkl,lk,ij', eri1tmp, dm_cas, dm_core[p0:p1,q0:q1]) * 2
de[k,i] += numpy.einsum('ijkl,jk,il', eri1tmp, dm_cas[q0:q1], dm_core[p0:p1])
eri1 = eri1tmp = None

Expand Down
19 changes: 13 additions & 6 deletions pyscf/grad/test/test_casci.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,12 +459,19 @@ def test_symmetrize(self):
g = mol.RHF.run().CASCI(4, 4).run().Gradients().kernel()
self.assertAlmostEqual(lib.fp(g), 0.11555543375018221, 6)

# issue 1909
def test_small_mem(self):
mol = M(atom="""
H -0.00021900 -0.20486000 -2.17721200
H -0.00035900 -1.27718700 -2.17669400
""", basis='6-31G')
casci = mol.CASCI(2, 2).run()
grad = casci.nuc_grad_method()
grad.max_memory = 0
nuc_grad = grad.kernel()
self.assertAlmostEqual(lib.fp(nuc_grad), 0.09424065197659935, 7)


if __name__ == "__main__":
print("Tests for CASCI gradients")
#unittest.main()
if 1:
setUpModule()
mc = mcscf.CASCI(mf, 4, 4)
mc = mcscf.addons.state_average_mix_(mc, [mc.fcisolver, mc.fcisolver], (.5, .5))
gs = mc.nuc_grad_method().as_scanner()
unittest.main()

0 comments on commit cf09e6b

Please sign in to comment.