def test_init(self): hf = scf.RHF(mol) ks = scf.RKS(mol) kshf = scf.RKS(mol).set(xc='HF') self.assertTrue(isinstance(tdscf.TDA(hf), tdscf.rhf.TDA)) self.assertTrue(isinstance(tdscf.TDA(ks), tdscf.rks.TDA)) self.assertTrue(isinstance(tdscf.TDA(kshf), tdscf.rks.TDA)) self.assertTrue(isinstance(tdscf.RPA(hf), tdscf.rhf.TDHF)) self.assertTrue(isinstance(tdscf.RPA(ks), tdscf.rks.TDDFTNoHybrid)) self.assertTrue(isinstance(tdscf.RPA(kshf), tdscf.rks.TDDFT)) self.assertTrue(isinstance(tdscf.TDDFT(hf), tdscf.rhf.TDHF)) self.assertTrue(isinstance(tdscf.TDDFT(ks), tdscf.rks.TDDFTNoHybrid)) self.assertTrue(isinstance(tdscf.TDDFT(kshf), tdscf.rks.TDDFT)) self.assertRaises(RuntimeError, tdscf.dRPA, hf) self.assertTrue(isinstance(tdscf.dRPA(kshf), tdscf.rks.dRPA)) self.assertTrue(isinstance(tdscf.dRPA(ks), tdscf.rks.dRPA)) self.assertRaises(RuntimeError, tdscf.dTDA, hf) self.assertTrue(isinstance(tdscf.dTDA(kshf), tdscf.rks.dTDA)) self.assertTrue(isinstance(tdscf.dTDA(ks), tdscf.rks.dTDA)) kshf.xc = '' self.assertTrue(isinstance(tdscf.dTDA(kshf), tdscf.rks.dTDA)) self.assertTrue(isinstance(tdscf.dRPA(kshf), tdscf.rks.dRPA))
def test_tdhf(self): td = tdscf.TDDFT(mf).run(nstates=3) tdg = td.nuc_grad_method() g1 = tdg.kernel(td.xy[2]) self.assertAlmostEqual(g1[0, 2], -0.25240005833657309, 8) td_solver = td.as_scanner() e1 = td_solver(pmol.set_geom_('H 0 0 1.805; F 0 0 0', unit='B')) e2 = td_solver(pmol.set_geom_('H 0 0 1.803; F 0 0 0', unit='B')) self.assertAlmostEqual((e1[2] - e2[2]) / .002, g1[0, 2], 6)
def test_tddft_b3lyp_high_cost(self): mf = dft.RKS(mol) mf.xc = 'b3lyp' mf._numint.libxc = dft.xcfun mf.grids.prune = False mf.scf() td = tdscf.TDDFT(mf).run(nstates=3) tdg = td.nuc_grad_method() g1 = tdg.kernel(state=3) self.assertAlmostEqual(g1[0,2], -1.55778110e-01, 7)
def test_tddft_lda(self): td = tdscf.TDDFT(mf_lda).run(nstates=3) tdg = td.nuc_grad_method() g1 = tdg.kernel(state=3) self.assertAlmostEqual(g1[0, 2], -0.39791714992157035, 6) td_solver = td.as_scanner() e1 = td_solver(pmol.set_geom_('H 0 0 1.805; F 0 0 0', unit='B')) e2 = td_solver(pmol.set_geom_('H 0 0 1.803; F 0 0 0', unit='B')) self.assertAlmostEqual((e1[2] - e2[2]) / .002, g1[0, 2], 5)
def test_tddft_lda(self): td = tdscf.TDDFT(mf_lda).run(nstates=3) tdg = td.nuc_grad_method() g1 = tdg.kernel(state=3) self.assertAlmostEqual(g1[0,2], -1.31315477e-01, 8) td_solver = td.as_scanner() pmol = mol.copy() e1 = td_solver(pmol.set_geom_('H 0 0 1.805; F 0 0 0', unit='B')) e2 = td_solver(pmol.set_geom_('H 0 0 1.803; F 0 0 0', unit='B')) self.assertAlmostEqual(abs((e1[2]-e2[2])/.002 - g1[0,2]).max(), 0, 3)
def test_tdhf(self): td = tdscf.TDDFT(mf).run(nstates=3) tdg = td.nuc_grad_method() g1 = tduhf_grad.kernel(tdg, td.xy[2]) g1 += tdg.grad_nuc() self.assertAlmostEqual(g1[0, 2], -0.78969714300299776, 6) td_solver = td.as_scanner() e1 = td_solver(pmol.set_geom_('H 0 0 1.805; F 0 0 0', unit='B')) e2 = td_solver(pmol.set_geom_('H 0 0 1.803; F 0 0 0', unit='B')) self.assertAlmostEqual((e1[2] - e2[2]) / .002, g1[0, 2], 4)
def test_tddft_b3lyp(self): mf = dft.UKS(mol).set(conv_tol=1e-12) mf.xc = '.2*HF + .8*b88, vwn' mf.scf() td = tdscf.TDDFT(mf).run(nstates=3) tdg = td.nuc_grad_method() g1 = tdg.kernel(state=3) self.assertAlmostEqual(g1[0, 2], -0.80446691153291727, 6) td_solver = td.as_scanner() e1 = td_solver(pmol.set_geom_('H 0 0 1.805; F 0 0 0', unit='B')) e2 = td_solver(pmol.set_geom_('H 0 0 1.803; F 0 0 0', unit='B')) self.assertAlmostEqual((e1[2] - e2[2]) / .002, g1[0, 2], 4)
def do_TDDFT(inp, mSCF): '''Do TDHF/TDDFT calculation.''' from pyscf import tdscf mtd = tdscf.TDDFT(mSCF) mtd.nstates = inp.scf.exci mtd.verbose = max(mtd.verbose, 9) mtd.conv_tol = 1e-6 exci, temp = mtd.kernel() mtd.analyze() return mtd
def test_ab_b3lyp(self): mf = mf_b3lyp a, b = tdscf.TDDFT(mf).get_ab() ftda = tdscf.uhf.gen_tda_operation(mf)[0] ftdhf = tdscf.uhf.gen_tdhf_operation(mf)[0] nocc_a = numpy.count_nonzero(mf.mo_occ[0] == 1) nvir_a = numpy.count_nonzero(mf.mo_occ[0] == 0) nocc_b = numpy.count_nonzero(mf.mo_occ[1] == 1) nvir_b = numpy.count_nonzero(mf.mo_occ[1] == 0) numpy.random.seed(2) xa, ya = numpy.random.random((2,nocc_a,nvir_a)) xb, yb = numpy.random.random((2,nocc_b,nvir_b)) x = numpy.hstack((xa.ravel(), xb.ravel())) y = numpy.hstack((ya.ravel(), yb.ravel())) xy = numpy.hstack((x, y)) ax_a = numpy.einsum('iajb,jb->ia', a[0], xa) ax_a+= numpy.einsum('iajb,jb->ia', a[1], xb) ax_b = numpy.einsum('jbia,jb->ia', a[1], xa) ax_b+= numpy.einsum('iajb,jb->ia', a[2], xb) ax = numpy.hstack((ax_a.ravel(), ax_b.ravel())) self.assertAlmostEqual(abs(ax - ftda([x])).max(), 0, 9) ay_a = numpy.einsum('iajb,jb->ia', a[0], ya) ay_a+= numpy.einsum('iajb,jb->ia', a[1], yb) ay_b = numpy.einsum('jbia,jb->ia', a[1], ya) ay_b+= numpy.einsum('iajb,jb->ia', a[2], yb) ay = numpy.hstack((ay_a.ravel(), ay_b.ravel())) bx_a = numpy.einsum('iajb,jb->ia', b[0], xa) bx_a+= numpy.einsum('iajb,jb->ia', b[1], xb) bx_b = numpy.einsum('jbia,jb->ia', b[1], xa) bx_b+= numpy.einsum('iajb,jb->ia', b[2], xb) bx = numpy.hstack((bx_a.ravel(), bx_b.ravel())) by_a = numpy.einsum('iajb,jb->ia', b[0], ya) by_a+= numpy.einsum('iajb,jb->ia', b[1], yb) by_b = numpy.einsum('jbia,jb->ia', b[1], ya) by_b+= numpy.einsum('iajb,jb->ia', b[2], yb) by = numpy.hstack((by_a.ravel(), by_b.ravel())) ab1 = ax + by ab2 =-bx - ay ab12 = numpy.hstack((ab1.ravel(),ab2.ravel())) abxy_ref = ftdhf([xy]) self.assertAlmostEqual(abs(ab12 - abxy_ref).max(), 0, 9)
def test_tddft_lda(self): td = tdscf.TDDFT(mf_lda).run(nstates=3) tdg = td.nuc_grad_method() g1 = tdg.kernel(state=3) self.assertAlmostEqual(g1[0,2], -1.31315477e-01, 8)
#!/usr/bin/env python ''' GW calculation with exact frequency integration and TDDFT screening instead of dRPA ''' from pyscf import gto, dft, gw mol = gto.M(atom='H 0 0 0; F 0 0 1.1', basis='ccpvdz') mf = dft.RKS(mol) mf.xc = 'pbe' mf.kernel() from pyscf import tdscf nocc = mol.nelectron // 2 nmo = mf.mo_energy.size nvir = nmo - nocc td = tdscf.TDDFT(mf) td.nstates = nocc * nvir td.verbose = 0 td.kernel() gw = gw.GW(mf, freq_int='exact', tdmf=td) gw.kernel() print(gw.mo_energy)