def test_157_bse_h2o_rhf_cis(self): """ This """ mol = gto.M(verbose=1, atom='O 0 0 0; H 0 0.489 1.074; H 0 0.489 -1.074', basis='cc-pvdz') gto_mf = scf.RHF(mol) gto_mf.kernel() gto_td = tddft.TDDFT(gto_mf) gto_td.nstates = 190 gto_td.kernel() omegas = np.arange(0.0, 2.0, 0.01) + 1j * 0.03 p_ave = -polariz_freq_osc_strength( gto_td.e, gto_td.oscillator_strength(), omegas).imag data = np.array([omegas.real * HARTREE2EV, p_ave]) np.savetxt('test_0157_bse_h2o_rhf_cis_pyscf.txt', data.T, fmt=['%f', '%f']) data_ref = np.loadtxt('test_0157_bse_h2o_rhf_cis_pyscf.txt-ref').T self.assertTrue(np.allclose(data_ref, data, atol=1e-6, rtol=1e-3)) nao_td = bse_iter(mf=gto_mf, gto=mol, verbosity=0, xc_code='CIS') polariz = -nao_td.comp_polariz_inter_ave(omegas).imag data = np.array([omegas.real * HARTREE2EV, polariz]) np.savetxt('test_0157_bse_h2o_rhf_cis_nao.txt', data.T, fmt=['%f', '%f']) data_ref = np.loadtxt('test_0157_bse_h2o_rhf_cis_nao.txt-ref').T self.assertTrue(np.allclose(data_ref, data, atol=5e-5, rtol=5e-2), msg="{}".format( abs(data_ref - data).sum() / data.size))
def test_152_bse_h2b_uks_pz(self): """ This """ mol = gto.M(verbose=1, atom='B 0 0 0; H 0 0.489 1.074; H 0 0.489 -1.074', basis='cc-pvdz', spin=3) gto_mf = scf.UKS(mol) gto_mf.kernel() gto_td = tddft.TDDFT(gto_mf) gto_td.nstates = 190 gto_td.kernel() omegas = np.arange(0.0, 2.0, 0.01) + 1j * 0.03 p_ave = -polariz_freq_osc_strength( gto_td.e, gto_td.oscillator_strength(), omegas).imag data = np.array([omegas.real * HARTREE2EV, p_ave]) np.savetxt('test_0152_bse_h2b_uks_pz_pyscf.txt', data.T, fmt=['%f', '%f']) data_ref = np.loadtxt('test_0152_bse_h2b_uks_pz_pyscf.txt-ref').T self.assertTrue(np.allclose(data_ref, data, atol=1e-6, rtol=1e-3)) nao_td = bse_iter(mf=gto_mf, gto=mol, verbosity=0) polariz = -nao_td.comp_polariz_inter_ave(omegas).imag data = np.array([omegas.real * HARTREE2EV, polariz]) np.savetxt('test_0152_bse_h2b_uks_pz_nao.txt', data.T, fmt=['%f', '%f']) data_ref = np.loadtxt('test_0152_bse_h2b_uks_pz_nao.txt-ref').T self.assertTrue(np.allclose(data_ref, data, atol=1e-6, rtol=1e-3))
def test_tddft_b3lyp(self): mf = dft.RKS(mol) mf.xc = 'b3lyp' mf.grids.prune = False mf.scf() td = tddft.TDDFT(mf).run(nstates=3) tdg = rks_grad.Gradients(td) g1 = tdg.kernel(state=2) self.assertAlmostEqual(g1[0,2], -1.55778110e-01, 8)
def test_tddft_lda(self): mf = dft.RKS(mol) mf.xc = 'LDA' mf.grids.prune = False mf.scf() td = tddft.TDDFT(mf).run(nstates=3) tdg = rks_grad.Gradients(td) g1 = tdg.kernel(state=2) self.assertAlmostEqual(g1[0,2], -1.31315477e-01, 9)
def test_0147_bse_h2o_rks_pz(self): """ Interacting case """ mol = gto.M( verbose=0, atom='O 0 0 0;H 0 0.489 1.074;H 0 0.489 -1.074', basis='cc-pvdz', ) gto_hf = scf.RKS(mol) gto_hf.kernel() gto_td = tddft.TDDFT(gto_hf) gto_td.nstates = 95 gto_td.kernel() omegas = np.arange(0.0, 2.0, 0.01) + 1j * 0.03 p_ave = -polariz_freq_osc_strength( gto_td.e, gto_td.oscillator_strength(), omegas).imag data = np.array([omegas.real * HARTREE2EV, p_ave]) np.savetxt('test_0147_bse_h2o_rks_pz_pyscf.txt', data.T, fmt=['%f', '%f']) data_ref = np.loadtxt('test_0147_bse_h2o_rks_pz_pyscf.txt-ref').T self.assertTrue(np.allclose(data_ref, data, atol=1e-6, rtol=1e-3)) nao_td = bse_iter( mf=gto_hf, gto=mol, verbosity=0, xc_code='LDA', ) p_iter = -nao_td.comp_polariz_inter_ave(omegas).imag data = np.array([omegas.real * HARTREE2EV, p_iter]) np.savetxt('test_0147_bse_h2o_rks_pz_nao.txt', data.T, fmt=['%f', '%f']) data_ref = np.loadtxt('test_0147_bse_h2o_rks_pz_nao.txt-ref').T self.assertTrue(np.allclose(data_ref, data, atol=1e-6, rtol=1e-3))
from __future__ import print_function, division import unittest, numpy as np from pyscf import gto, tddft, scf from pyscf.nao import bse_iter from pyscf.nao import polariz_inter_ave, polariz_nonin_ave mol = gto.M( verbose = 0, atom = '''H 0 0 0; H 0.17 0.7 0.587''', basis = 'cc-pvdz',) gto_mf = scf.RHF(mol) gto_mf.kernel() #print(gto_mf.mo_energy) gto_td = tddft.TDDFT(gto_mf) gto_td.nstates = 9 gto_td.singlet = True # False gto_td.kernel() nao_td = bse_iter(mf=gto_mf, gto=mol, verbosity=0) class KnowValues(unittest.TestCase): #def test_bse_iter_vs_tdhf_pyscf(self): # """ Interacting case test """ # cdip = np.random.rand(nao_td.norbs,nao_td.norbs)+1j*np.random.rand(nao_td.norbs,nao_td.norbs) # nao_td.apply_l0_exp(cdip, comega=0.2+1j*0.01) def test_tddft_gto_vs_nao_inter(self): """ Interacting case """ omegas = np.linspace(0.0,2.0,450)+1j*0.04 p_ave = -polariz_inter_ave(gto_mf, mol, gto_td, omegas).imag data = np.array([omegas.real*27.2114, p_ave]) np.savetxt('hydrogen.tdhf.omega.inter.pav.txt', data.T, fmt=['%f','%f'])
# # Author: Qiming Sun <*****@*****.**> # ''' A simple example to run TDDFT calculation. ''' from pyscf import gto, scf, dft, tddft mol = gto.Mole() mol.build( atom='H 0 0 0; F 0 0 1.1', # in Angstrom basis='631g', symmetry=True, ) mf = dft.RKS(mol) mf.xc = 'b3lyp' mf.kernel() mytd = tddft.TDDFT(mf) #mytd.nstates = 10 mytd.kernel() mytd.analyze() # PySCF-1.6.1 and newer supports the .TDDFT method to create a TDDFT # object after importing tdscf module. from pyscf import tddft mytd = mf.TDDFT().run() mytd = scf.RHF(mol).run().TDHF().run()
sys.argv = [sys.argv[0]] # ----------------------- # PYSCF # ----------------------- mol = gto.M(atom=atomic_coords, basis=basis_set, spin=spin, charge=charge, verbose=5) my_dft = dft.RKS(mol) my_dft.xc = functional if density_fit: my_dft = my_dft.density_fit() my_dft.with_df.auxbasis = aux_basis_set my_dft.run() my_tddft = tddft.TDDFT(my_dft) # my_tddft.nstates = 10 my_tddft.kernel() excitation_energies = (my_tddft.e * 27.2114).tolist() properties = { "excitationEnergies": excitation_energies, } stringified_properties = json.dumps(properties) print('atoms+bits properties:\n~{}~'.format(stringified_properties))
can be set in TDDFT. ''' import copy from pyscf import gto, dft, tddft mol = gto.M(atom='N 0 0 0; N 0 0 1', basis='6-31g*') mf = dft.RKS(mol).run(xc='pbe0') # # A common change for TDDFT is to use different XC functional library. For # example, PBE0 is not supported by the default XC library (libxc) in the TDDFT # calculation. Changing to xcfun library for TDDFT can solve this problem # mf._numint.libxc = dft.xcfun td = tddft.TDDFT(mf) print(td.kernel()[0] * 27.2114) # # Overwriting the relevant attributes of the ground state mf object, # the TDDFT calculations can be run with different XC, grids. # mf.xc = 'lda,vwn' mf.grids.set(level=2).kernel(with_non0tab=True) td = tddft.TDDFT(mf) print(td.kernel()[0] * 27.2114) # # Overwriting the ground state SCF object is unsafe. A better solution is to # create a new fake SCF object to hold different XC, grids parameters. #
TDDFT analytical nuclear gradients. ''' from pyscf import gto, scf, dft, tddft mol = gto.M(atom=[['O', 0., 0., 0], ['H', 0., -0.757, 0.587], ['H', 0., 0.757, 0.587]], basis='ccpvdz') mf = scf.RHF(mol).run() postmf = tddft.TDHF(mf).run() g = postmf.nuc_grad_method() g.kernel(state=1) mf = dft.RKS(mol).x2c().set(xc='pbe0').run() # Switch to xcfun because 3rd order GGA functional derivative is not # available in libxc mf._numint.libxc = dft.xcfun postmf = tddft.TDDFT(mf).run() # PySCF-1.6.1 and newer supports the .Gradients method to create a grad # object after grad module was imported. It is equivalent to call the # .nuc_grad_method method. from pyscf import grad g = postmf.Gradients() g.kernel(state=1) #mf = scf.UHF(mol).x2c().run() #postmf = tddft.TDHF(mf).run() #g = postmf.nuc_grad_method() #g.kernel()
def diagonalize(a, b, nroots=5): nocc, nvir = a.shape[:2] a = a.reshape(nocc * nvir, nocc * nvir) b = b.reshape(nocc * nvir, nocc * nvir) e = numpy.linalg.eig(numpy.bmat([[a, b], [-b.conj(), -a.conj()]]))[0] lowest_e = numpy.sort(e[e > 0])[:nroots] return lowest_e mf = scf.RHF(mol).run() a, b = tddft.TDHF(mf).get_ab() print('Direct diagoanlization:', diagonalize(a, b)) print('Reference:', tddft.TDHF(mf).kernel(nstates=5)[0]) mf = dft.RKS(mol).run(xc='lda,vwn') a, b = tddft.TDDFT(mf).get_ab() print('Direct diagoanlization:', diagonalize(a, b)) print('Reference:', tddft.TDDFT(mf).kernel(nstates=5)[0]) mf = dft.RKS(mol).run(xc='b3lyp') a, b = tddft.TDDFT(mf).get_ab() print('Direct diagoanlization:', diagonalize(a, b)) print('Reference:', tddft.TDDFT(mf).kernel(nstates=5)[0]) # # UHF/UKS-TDDFT # def diagonalize(a, b, nroots=4): a_aa, a_ab, a_bb = a b_aa, b_ab, b_bb = b