def test_ccsd_grad(self): mycc = cc.ccsd.CCSD(mf) mycc.max_memory = 1 mycc.conv_tol = 1e-10 eris = mycc.ao2mo() ecc, t1, t2 = mycc.kernel(eris=eris) l1, l2 = mycc.solve_lambda(eris=eris) g1 = ccsd_grad.kernel(mycc, t1, t2, l1, l2, mf_grad=grad.RHF(mf)) self.assertAlmostEqual(lib.finger(g1), -0.036999389889460096, 6) mol = gto.M( verbose = 5, output = '/dev/null', atom = 'H 0 0 0; H 0 0 1.706', basis = '631g', unit='Bohr') mf0 = scf.RHF(mol).run(conv_tol=1e-14) mycc0 = cc.ccsd.CCSD(mf0).run(conv_tol=1e-10) mol.set_geom_('H 0 0 0; H 0 0 1.704', unit='Bohr') mf1 = scf.RHF(mol).run(conv_tol=1e-14) mycc1= cc.ccsd.CCSD(mf1).run(conv_tol=1e-10) mol.set_geom_('H 0 0 0; H 0 0 1.705', unit='Bohr') mycc2 = cc.ccsd.CCSD(scf.RHF(mol)) g_scanner = mycc2.nuc_grad_method().as_scanner().as_scanner() g1 = g_scanner(mol)[1] self.assertTrue(g_scanner.converged) self.assertAlmostEqual(g1[0,2], (mycc1.e_tot-mycc0.e_tot)*500, 6)
def kernel(mycc, t1=None, t2=None, l1=None, l2=None, eris=None, atmlst=None, mf_grad=None, verbose=lib.logger.INFO): if t1 is None: t1 = mycc.t1 if t2 is None: t2 = mycc.t2 if l1 is None: l1 = mycc.l1 if l2 is None: l2 = mycc.l2 d1 = ccsd_t_rdm._gamma1_intermediates(mycc, t1, t2, l1, l2, eris, for_grad=True) fd2intermediate = lib.H5TmpFile() d2 = ccsd_t_rdm._gamma2_outcore(mycc, t1, t2, l1, l2, eris, fd2intermediate, True) return ccsd_grad.kernel(mycc, t1, t2, l1, l2, eris, atmlst, mf_grad, d1, d2, verbose)
def casci_grad_with_ccsd_solver(mc, mo_coeff=None, ci=None, atmlst=None, mf_grad=None, verbose=None): if mo_coeff is None: mo_coeff = mc._scf.mo_coeff if ci is None: ci = mc.ci if mf_grad is None: mf_grad = mc._scf.nuc_grad_method() mol = mc.mol ncore = mc.ncore ncas = mc.ncas nocc = ncore + ncas nelecas = mc.nelecas nao, nmo = mo_coeff.shape nao_pair = nao * (nao + 1) // 2 mo_occ = mo_coeff[:, :nocc] mo_core = mo_coeff[:, :ncore] mo_cas = mo_coeff[:, ncore:nocc] casdm1, casdm2 = mc.fcisolver.make_rdm12(mc.ci, ncas, nelecas) no = mc.nelecas[0] for i in range(no): casdm1[i, i] -= 2 for i in range(no): for j in range(no): casdm2[i, i, j, j] -= 4 casdm2[i, j, j, i] += 2 for i in range(no): casdm2[i, i, :, :] -= casdm1 * 2 casdm2[:, :, i, i] -= casdm1 * 2 casdm2[:, i, i, :] += casdm1 casdm2[i, :, :, i] += casdm1 mc.mo_occ = mc._scf.mo_occ mask = numpy.zeros(nmo, dtype=bool) mask[ncore:nocc] = True mc.frozen = numpy.where(~mask)[0] mc.get_frozen_mask = lambda *args: mask d1 = (casdm1[:no, :no] * .5, casdm1[:no, no:] * .5, casdm1[no:, :no] * .5, casdm1[no:, no:] * .5) casdm2 = (casdm2 + casdm2.transpose(1, 0, 2, 3)) * .5 vvvv = casdm2[no:, no:, no:, no:] d2 = (casdm2[:no, no:, :no, no:] * .5, ao2mo.restore(4, vvvv, ncas - no) * .25, casdm2[:no, :no, :no, :no] * .25, casdm2[:no, :no, no:, no:] * .5, casdm2[:no, no:, no:, :no] * .5, casdm2, casdm2[:no, no:, no:, no:], casdm2[:no, :no, :no, no:]) mc.mo_coeff = mo_coeff t1 = t2 = l1 = l2 = ci return ccsd_grad.kernel(mc, t1, t2, l1, l2, None, atmlst, mf_grad, d1, d2, verbose)
def kernel(myci, civec=None, eris=None, atmlst=None, mf_grad=None, verbose=logger.INFO): if civec is None: civec = myci.ci assert(not isinstance(civec, (list, tuple))) nocc = myci.nocc nmo = myci.nmo d1 = cisd._gamma1_intermediates(myci, civec, nmo, nocc) fd2intermediate = lib.H5TmpFile() d2 = cisd._gamma2_outcore(myci, civec, nmo, nocc, fd2intermediate, True) t1 = t2 = l1 = l2 = civec return ccsd_grad.kernel(myci, t1, t2, l1, l2, eris, atmlst, mf_grad, d1, d2, verbose)
def casci_grad_with_ccsd_solver(mc, mo_coeff=None, ci=None, atmlst=None, mf_grad=None, verbose=None): if mo_coeff is None: mo_coeff = mc._scf.mo_coeff if ci is None: ci = mc.ci if mf_grad is None: mf_grad = mc._scf.nuc_grad_method() mol = mc.mol ncore = mc.ncore ncas = mc.ncas nocc = ncore + ncas nelecas = mc.nelecas nao, nmo = mo_coeff.shape nao_pair = nao * (nao+1) // 2 mo_occ = mo_coeff[:,:nocc] mo_core = mo_coeff[:,:ncore] mo_cas = mo_coeff[:,ncore:nocc] casdm1, casdm2 = mc.fcisolver.make_rdm12(mc.ci, ncas, nelecas) no = mc.nelecas[0] for i in range(no): casdm1[i,i] -= 2 for i in range(no): for j in range(no): casdm2[i,i,j,j] -= 4 casdm2[i,j,j,i] += 2 for i in range(no): casdm2[i,i,:,:] -= casdm1 * 2 casdm2[:,:,i,i] -= casdm1 * 2 casdm2[:,i,i,:] += casdm1 casdm2[i,:,:,i] += casdm1 mc.mo_occ = mc._scf.mo_occ mask = numpy.zeros(nmo, dtype=bool) mask[ncore:nocc] = True mc.frozen = numpy.where(~mask)[0] mc.get_frozen_mask = lambda *args: mask d1 = (casdm1[:no,:no] * .5, casdm1[:no,no:] * .5, casdm1[no:,:no] * .5, casdm1[no:,no:] * .5) casdm2 = (casdm2 + casdm2.transpose(1,0,2,3)) * .5 vvvv = casdm2[no:,no:,no:,no:] d2 = (casdm2[:no,no:,:no,no:] * .5, ao2mo.restore(4, vvvv, ncas-no) * .25, casdm2[:no,:no,:no,:no] * .25, casdm2[:no,:no,no:,no:] * .5, casdm2[:no,no:,no:,:no] * .5, casdm2, casdm2[:no,no:,no:,no:], casdm2[:no,:no,:no,no:]) mc.mo_coeff = mo_coeff t1 = t2 = l1 = l2 = ci return ccsd_grad.kernel(mc, t1, t2, l1, l2, None, atmlst, mf_grad, d1, d2, verbose)