From cf09e6b394bfdcd055af4f4b8af50d7e1d6e14b1 Mon Sep 17 00:00:00 2001 From: Qiming Sun Date: Sat, 21 Oct 2023 18:31:44 -0700 Subject: [PATCH] casci grad crash for small max_memory. Fix #1909 --- pyscf/grad/casci.py | 6 +++--- pyscf/grad/test/test_casci.py | 19 +++++++++++++------ 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/pyscf/grad/casci.py b/pyscf/grad/casci.py index 1d46856e69..ccc29af896 100644 --- a/pyscf/grad/casci.py +++ b/pyscf/grad/casci.py @@ -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 diff --git a/pyscf/grad/test/test_casci.py b/pyscf/grad/test/test_casci.py index 4fbcd96b32..4309294e16 100644 --- a/pyscf/grad/test/test_casci.py +++ b/pyscf/grad/test/test_casci.py @@ -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()