def test_rhf_2d_fft(self): L = 4 cell = pbcgto.Cell() cell.build(unit = 'B', a = [[L,0,0],[0,L,0],[0,0,L*5]], mesh = [11,11,20], atom = '''He 2 0 0; He 3 0 0''', dimension = 2, verbose = 0, basis = { 'He': [[0, (0.8, 1.0)], [0, (1.2, 1.0)] ]}) mf = pbchf.RHF(cell, exxdiv='ewald') mf.with_df = pdf.FFTDF(cell) mf.with_df.mesh = cell.mesh e1 = mf.kernel() self.assertAlmostEqual(e1, -3.5797041803667593, 5) mf1 = pbchf.RHF(cell, exxdiv='ewald') mf1.with_df = pdf.FFTDF(cell) mf1.with_df.mesh = cell.mesh mf1.direct_scf = True e1 = mf1.kernel() self.assertAlmostEqual(e1, -3.5797041803667593, 5) mf2 = pbchf.RHF(cell, exxdiv=None) mf2.with_df = pdf.FFTDF(cell) mf2.with_df.mesh = cell.mesh mf2.direct_scf = True e2 = mf2.kernel() self.assertAlmostEqual(e2, -1.629571720365774, 5)
def test_init_guess_by_chkfile(self): numpy.random.seed(1) k = numpy.random.random(3) mf = pbchf.RHF(cell, k, exxdiv='vcut_sph') e1 = mf.kernel() dm1 = mf.make_rdm1() mf1 = pbchf.RHF(cell, exxdiv='vcut_sph') mf1.chkfile = mf.chkfile mf1.init_guess = 'chkfile' e1 = mf1.kernel() self.assertAlmostEqual(e1, -4.29190260870812, 8) self.assertTrue(mf1.mo_coeff.dtype == numpy.double)
def test_rhf_1d(self): L = 4 cell = pbcgto.Cell() cell.build( unit='B', a=[[L, 0, 0], [0, L * 5, 0], [0, 0, L * 5]], mesh=[11, 20, 20], atom='''He 2 0 0; He 3 0 0''', dimension=1, low_dim_ft_type='inf_vacuum', verbose=0, basis={ 'He': [ [0, (0.8, 1.0)], #[0, (1.0, 1.0)], [0, (1.2, 1.0)] ] }) mf = pbchf.RHF(cell) mf.with_df = pdf.AFTDF(cell) mf.with_df.eta = 0.3 mf.with_df.mesh = cell.mesh mf.init_guess = 'hcore' e1 = mf.kernel() self.assertAlmostEqual(e1, -3.24497234871167, 5)
def test_hf(pseudo=None): # The molecular calculation mol = gto.Mole() mol.unit = 'B' L = 60 mol.atom.extend([ ['He', (L / 2., L / 2., L / 2.)], ]) # these are some exponents which are not hard to integrate mol.basis = {'He': [[0, (0.8, 1.0)], [0, (1.0, 1.0)], [0, (1.2, 1.0)]]} mol.build() m = hf.RHF(mol) print "Molecular HF energy" print(m.scf()) # -2.63502450321874 # The periodic calculation cell = pbcgto.Cell() cell.unit = 'B' cell.h = np.diag([L, L, L]) cell.gs = np.array([60, 60, 60]) cell.nimgs = [0, 0, 0] cell.atom = mol.atom cell.basis = mol.basis cell.pseudo = pseudo # cell.verbose = 4 cell.build(None, None) mf = pbchf.RHF(cell) print(mf.scf()) # -2.58766850182551: doesn't look good, but this is due
def test_band_kscf(): L = 1 cell = pbcgto.Cell() cell.unit = 'B' cell.h = np.diag([L, L, L]) cell.gs = np.array([10, 10, 10]) cell.atom.extend([['He', (L / 2., L / 2., L / 2.)]]) cell.basis = {'He': [[0, (1.0, 1.0)]]} cell.build() mf = pbchf.RHF(cell) mf.scf() auxcell = cell.copy() auxcell.gs = np.array([1, 1, 1]) auxcell.build() invhT = scipy.linalg.inv(np.asarray(cell._h).T) ncells = 2 kGvs = [] for i in range(ncells): kGvs.append(i * 1. / ncells * 2 * pi * np.dot(invhT, (1, 0, 0))) kpts = np.vstack(kGvs) kmf = pbckscf.KRKS(cell, cell.gs, cell.ew_eta, cell.ew_cut, kpts) kmf.init_guess = "atom" print kmf.scf() for i in range(1, 10): band_kpt = 1. / i * auxcell.Gv[-1, :] print pbchf.get_eig_kpt(kmf, band_kpt)[0]
def test_rhf_2d(self): L = 4 cell = pbcgto.Cell() cell.build( unit='B', a=[[L, 0, 0], [0, L, 0], [0, 0, L * 5]], mesh=[11, 11, 20], atom='''He 2 0 0; He 3 0 0''', dimension=2, low_dim_ft_type='inf_vacuum', verbose=0, rcut=7.427535697575829, basis={ 'He': [ [0, (0.8, 1.0)], #[0, (1.0, 1.0)], [0, (1.2, 1.0)] ] }) mf = pbchf.RHF(cell) mf.with_df = pdf.AFTDF(cell) mf.with_df.eta = 0.3 mf.with_df.mesh = cell.mesh e1 = mf.kernel() self.assertAlmostEqual(e1, -3.2681555164454039, 5)
def test_moints(): # not yet working # The molecular calculation mol = gto.Mole() mol.unit = 'B' L = 60 mol.atom.extend([ ['He', (L / 2., L / 2., L / 2.)], ]) mol.basis = 'cc-pvdz' mol.build() # The periodic calculation cell = pbcgto.Cell() cell.unit = 'B' cell.h = np.diag([L, L, L]) cell.gs = np.array([40, 40, 40]) cell.nimgs = [1, 1, 1] cell.atom = mol.atom cell.basis = mol.basis cell.build() #mf = hf.RHF(mol) mf = pbchf.RHF(cell) print(mf.scf()) print "mo coeff shape", mf.mo_coeff.shape nmo = mf.mo_coeff.shape[1] print mf.mo_coeff eri_mo = pbcao2mo.get_mo_eri(cell, [mf.mo_coeff, mf.mo_coeff], [mf.mo_coeff, mf.mo_coeff]) eri_ao = pbcao2mo.get_ao_eri(cell) eri_mo2 = ao2mo.incore.general( np.real(eri_ao), (mf.mo_coeff, mf.mo_coeff, mf.mo_coeff, mf.mo_coeff), compact=False) print eri_mo.shape print eri_mo2.shape for i in range(nmo * nmo): for j in range(nmo * nmo): print i, j, np.real(eri_mo[i, j]), eri_mo2[i, j] print("ERI dimension") print(eri_mo.shape), nmo Ecoul = 0. Ecoul2 = 0. nocc = 1 print "diffs" for i in range(nocc): for j in range(nocc): Ecoul += 2 * eri_mo[i * nmo + i, j * nmo + j] - eri_mo[i * nmo + j, i * nmo + j] Ecoul2 += 2 * eri_mo2[i * nmo + i, j * nmo + j] - eri_mo2[i * nmo + j, i * nmo + j] print Ecoul, Ecoul2
def test_rhf_0d(self): from pyscf.df import mdf_jk from pyscf.scf import hf L = 4 cell = pbcgto.Cell() cell.build( unit='B', a=numpy.eye(3) * L * 5, gs=[10] * 3, atom='''He 2 2 2; He 2 2 3''', dimension=0, verbose=0, basis={'He': [[0, (0.8, 1.0)], [0, (1.0, 1.0)], [0, (1.2, 1.0)]]}) mol = cell.to_mol() mf = mdf_jk.density_fit(hf.RHF(mol)) mf.with_df.gs = [10] * 3 mf.with_df.auxbasis = {'He': [[0, (1e6, 1)]]} mf.with_df.charge_constraint = False mf.with_df.metric = 'S' eref = mf.kernel() mf = pbchf.RHF(cell) mf.with_df = pdf.PWDF(cell) mf.exxdiv = None mf.get_hcore = lambda *args: hf.get_hcore(mol) mf.energy_nuc = lambda *args: mol.energy_nuc() e1 = mf.kernel() self.assertAlmostEqual(e1, eref, 8)
def test_init_guess_by_chkfile(self): numpy.random.seed(1) k = numpy.random.random(3) mf = pbchf.RHF(cell, k, exxdiv='vcut_sph') mf.max_cycle = 1 mf.diis = None e1 = mf.kernel() self.assertAlmostEqual(e1, -4.132914531737784, 9) mf1 = pbchf.RHF(cell, exxdiv='vcut_sph') mf1.chkfile = mf.chkfile mf1.init_guess = 'chkfile' mf1.diis = None mf1.max_cycle = 1 e1 = mf1.kernel() self.assertAlmostEqual(e1, -4.291691793916943, 9) self.assertTrue(mf1.mo_coeff.dtype == numpy.double)
def test_hcore(self): h1ref = pbchf.get_hcore(cell) h1 = pbchf.RHF(cell).get_hcore() self.assertAlmostEqual(abs(h1-h1ref).max(), 0, 9) self.assertAlmostEqual(lib.finger(h1), 0.14116483012673137, 9) cell1 = cell.copy() cell1.ecp = {'He': (2, ((-1, (((7.2, .3),),)),))} cell1.build(0, 0) kpt = numpy.ones(3) * .5 h1ref = pbchf.get_hcore(cell1, kpt) h1 = pbchf.RHF(cell1).get_hcore(kpt=kpt) self.assertAlmostEqual(abs(h1-h1ref).max(), 0, 9) self.assertAlmostEqual(lib.finger(h1), -2.708431894877279-0.395390980665125j, 9) h1 = pscf.KRHF(cell1).get_hcore(kpts=[kpt]) self.assertEqual(h1.ndim, 3) self.assertAlmostEqual(abs(h1[0]-h1ref).max(), 0, 9)
def test_init_guess_by_chkfile(self): numpy.random.seed(1) k = numpy.random.random(3) mf = pbchf.RHF(cell, k, exxdiv='vcut_sph') mf.chkfile = tempfile.NamedTemporaryFile().name mf.max_cycle = 1 mf.diis = None e1 = mf.kernel() self.assertAlmostEqual(e1, -4.132445328608581, 9) mf1 = pbchf.RHF(cell, exxdiv='vcut_sph') mf1.chkfile = mf.chkfile mf1.init_guess = 'chkfile' mf1.diis = None mf1.max_cycle = 1 e1 = mf1.kernel() self.assertAlmostEqual(e1, -4.291854736401251, 9) self.assertTrue(mf1.mo_coeff.dtype == numpy.double)
def test_rhf_vcut_sph(self): mf = pbchf.RHF(cell, exxdiv='vcut_sph') e1 = mf.kernel() self.assertAlmostEqual(e1, -4.29190260870812, 8) self.assertTrue(mf.mo_coeff.dtype == numpy.double) mf = pscf.KRHF(cell, [[0,0,0]], exxdiv='vcut_sph') e0 = mf.kernel() self.assertTrue(numpy.allclose(e0,e1)) numpy.random.seed(1) k = numpy.random.random(3) mf = pbchf.RHF(cell, k, exxdiv='vcut_sph') e1 = mf.kernel() self.assertAlmostEqual(e1, -4.1379172088570595, 8) self.assertTrue(mf.mo_coeff.dtype == numpy.complex128) mf = pscf.KRHF(cell, k, exxdiv='vcut_sph') e0 = mf.kernel() self.assertTrue(numpy.allclose(e0,e1))
def test_rhf_exx_ewald(self): mf = pbchf.RHF(cell, exxdiv='ewald') e1 = mf.kernel() self.assertAlmostEqual(e1, -4.3511582284698633, 9) self.assertTrue(mf.mo_coeff.dtype == numpy.double) mf = pscf.KRHF(cell, [[0, 0, 0]], exxdiv='ewald') e0 = mf.kernel() self.assertTrue(numpy.allclose(e0, e1)) numpy.random.seed(1) k = numpy.random.random(3) mf = pbchf.RHF(cell, k, exxdiv='ewald') e1 = mf.kernel() self.assertAlmostEqual(e1, -4.2048655827967139, 9) self.assertTrue(mf.mo_coeff.dtype == numpy.complex128) mf = pscf.KRHF(cell, k, exxdiv='ewald') e0 = mf.kernel() self.assertTrue(numpy.allclose(e0, e1))
def test_rhf_exx_None(self): mf = pbchf.RHF(cell, exxdiv=None) e1 = mf.kernel() self.assertAlmostEqual(e1, -2.9325094887283196, 9) self.assertTrue(mf.mo_coeff.dtype == numpy.double) mf = pscf.KRHF(cell, [[0, 0, 0]], exxdiv=None) e0 = mf.kernel() self.assertTrue(numpy.allclose(e0, e1)) numpy.random.seed(1) k = numpy.random.random(3) mf = pbchf.RHF(cell, k, exxdiv=None) e1 = mf.kernel() self.assertAlmostEqual(e1, -2.7862168430230341, 9) self.assertTrue(mf.mo_coeff.dtype == numpy.complex128) mf = pscf.KRHF(cell, k, exxdiv=None) e0 = mf.kernel() self.assertTrue(numpy.allclose(e0, e1))
def test_uhf_exx_ewald(self): mf = pscf.UHF(cell, exxdiv=None) e1 = mf.kernel() self.assertAlmostEqual(e1, -2.9325094887283196, 9) self.assertTrue(mf.mo_coeff[0].dtype == numpy.double) numpy.random.seed(1) k = numpy.random.random(3) mf = pbchf.RHF(cell, k, exxdiv=None) e1 = mf.kernel() self.assertAlmostEqual(e1, -2.7862168430230341, 9) self.assertTrue(mf.mo_coeff[0].dtype == numpy.complex128)
def test_vkR(self): cell = make_cell1(4, 20) mf = phf.RHF(cell) numpy.random.seed(1) kpt1, kpt2 = numpy.random.random((2,3)) coords = pdft.gen_grid.gen_uniform_grids(cell) aoR_k1 = pdft.numint.eval_ao(cell, coords, kpt1) aoR_k2 = pdft.numint.eval_ao(cell, coords, kpt2) gs = cell.gs coords = pdft.gen_grid.gen_uniform_grids(cell, gs) vkR = fft_jk.get_vkR(mf, cell, aoR_k1, aoR_k2, kpt1, kpt2, coords, gs, None) self.assertAlmostEqual(finger(vkR), -0.24716036343105258+0.078117253956143579j, 9)
def test_rhf_exx_ewald(self): mf = pbchf.RHF(cell, exxdiv='ewald') e1 = mf.kernel() self.assertAlmostEqual(e1, -4.3511582284698633, 8) self.assertTrue(mf.mo_coeff.dtype == numpy.double) kmf = pscf.KRHF(cell, [[0, 0, 0]], exxdiv='ewald') e0 = kmf.kernel() self.assertTrue(numpy.allclose(e0, e1)) # test bands numpy.random.seed(1) kpts_band = numpy.random.random((2, 3)) e1, c1 = mf.get_bands(kpts_band) e0, c0 = kmf.get_bands(kpts_band) self.assertAlmostEqual(abs(e0[0] - e1[0]).max(), 0, 7) self.assertAlmostEqual(abs(e0[1] - e1[1]).max(), 0, 7) self.assertAlmostEqual(lib.finger(e1[0]), -6.2986775452228283, 7) self.assertAlmostEqual(lib.finger(e1[1]), -7.6616273746782362, 7) numpy.random.seed(1) k = numpy.random.random(3) mf = pbchf.RHF(cell, k, exxdiv='ewald') e1 = mf.kernel() self.assertAlmostEqual(e1, -4.2048655827967139, 8) self.assertTrue(mf.mo_coeff.dtype == numpy.complex128) kmf = pscf.KRHF(cell, k, exxdiv='ewald') e0 = kmf.kernel() self.assertTrue(numpy.allclose(e0, e1)) # test bands numpy.random.seed(1) kpts_band = numpy.random.random((2, 3)) e1, c1 = mf.get_bands(kpts_band) e0, c0 = kmf.get_bands(kpts_band) self.assertAlmostEqual(abs(e0[0] - e1[0]).max(), 0, 7) self.assertAlmostEqual(abs(e0[1] - e1[1]).max(), 0, 7) self.assertAlmostEqual(lib.finger(e1[0]), -6.8312867098806249, 7) self.assertAlmostEqual(lib.finger(e1[1]), -6.1120214505413086, 7)
def setUpModule(): global cell, mf, kmf L = 4 n = 21 cell = pbcgto.Cell() cell.build( unit='B', verbose=7, output='/dev/null', a=((L, 0, 0), (0, L, 0), (0, 0, L)), mesh=[n, n, n], atom=[['He', (L / 2. - .5, L / 2., L / 2. - .5)], ['He', (L / 2., L / 2., L / 2. + .5)]], basis={'He': [[0, (0.8, 1.0)], [0, (1.0, 1.0)], [0, (1.2, 1.0)]]}) mf = pbchf.RHF(cell, exxdiv='ewald').run() kmf = pscf.KRHF(cell, [[0, 0, 0]], exxdiv='ewald').run()
def test_rhf_2d(self): L = 4 cell = pbcgto.Cell() cell.build(unit = 'B', a = [[L,0,0],[0,L,0],[0,0,L*5]], gs = [5,5,10], atom = '''He 2 0 0; He 3 0 0''', dimension = 2, verbose = 0, basis = { 'He': [[0, (0.8, 1.0)], #[0, (1.0, 1.0)], [0, (1.2, 1.0)] ]}) mf = pbchf.RHF(cell) mf.with_df = pdf.PWDF(cell) e1 = mf.kernel() self.assertAlmostEqual(e1, -3.2683014249123516, 5)
def test_rhf_exx_ewald_with_kpt(self): numpy.random.seed(1) k = numpy.random.random(3) mf = pbchf.RHF(cell, k, exxdiv='ewald') e1 = mf.kernel() self.assertAlmostEqual(e1, -4.2048655827967139, 8) self.assertTrue(mf.mo_coeff.dtype == numpy.complex128) kmf = pscf.KRHF(cell, k, exxdiv='ewald') e0 = kmf.kernel() self.assertTrue(numpy.allclose(e0,e1)) # test bands numpy.random.seed(1) kpt_band = numpy.random.random(3) e1, c1 = mf.get_bands(kpt_band) e0, c0 = kmf.get_bands(kpt_band) self.assertAlmostEqual(abs(e0-e1).max(), 0, 7) self.assertAlmostEqual(lib.finger(e1), -6.8312867098806249, 7)
def test_rhf_1d(self): L = 4 cell = pbcgto.Cell() cell.build( unit='B', a=[[L, 0, 0], [0, L * 5, 0], [0, 0, L * 5]], gs=[5, 10, 10], atom='''He 2 0 0; He 3 0 0''', dimension=1, verbose=0, basis={ 'He': [ [0, (0.8, 1.0)], #[0, (1.0, 1.0)], [0, (1.2, 1.0)] ] }) mf = pbchf.RHF(cell) mf.with_df = pdf.AFTDF(cell) e1 = mf.kernel() self.assertAlmostEqual(e1, -3.2455039593257098, 5)
def test_band(): L = 1 cell = pbcgto.Cell() cell.unit = 'B' cell.h = np.diag([L, L, L]) cell.gs = np.array([10, 10, 10]) cell.atom.extend([['He', (L / 2., L / 2., L / 2.)]]) cell.basis = {'He': [[0, (1.0, 1.0)]]} cell.build() mf = pbchf.RHF(cell) mf.scf() auxcell = cell.copy() auxcell.gs = np.array([1, 1, 1]) auxcell.build() for i in range(1, 10): kpt = 1. / i * auxcell.Gv[-1, :] print pbchf.get_eig_kpt(mf, kpt)[0]
L = 4 n = 21 cell = pbcgto.Cell() cell.build(unit = 'B', verbose = 7, output = '/dev/null', a = ((L,0,0),(0,L,0),(0,0,L)), mesh = [n,n,n], atom = [['He', (L/2.-.5,L/2.,L/2.-.5)], ['He', (L/2. ,L/2.,L/2.+.5)]], basis = { 'He': [[0, (0.8, 1.0)], [0, (1.0, 1.0)], [0, (1.2, 1.0)]]}) mf = pbchf.RHF(cell, exxdiv='ewald').run() kmf = pscf.KRHF(cell, [[0,0,0]], exxdiv='ewald').run() def tearDownModule(): global cell, mf, kmf cell.stdout.close() del cell, mf, kmf class KnownValues(unittest.TestCase): def test_hcore(self): h1ref = pbchf.get_hcore(cell) h1 = pbchf.RHF(cell).get_hcore() self.assertAlmostEqual(abs(h1-h1ref).max(), 0, 9) self.assertAlmostEqual(lib.finger(h1), 0.14116483012673137, 9) cell1 = cell.copy()
def test_hcore(self): h1ref = pbchf.get_hcore(cell) h1 = pbchf.RHF(cell).get_hcore() self.assertAlmostEqual(abs(h1 - h1ref).max(), 0, 9)