def test_jk_pbc_local_fit(self): numpy.random.seed(12) nao = cell.nao_nr() dm = numpy.random.random((nao, nao)) dm = dm + dm.T jkdf = mdf.MDF(cell) jkdf.metric = 'S' jkdf.auxbasis = 'weigend' jkdf.approx_sr_level = 2 jkdf.gs = (5, ) * 3 mf = mdf_jk.density_fit(mf0, with_df=jkdf) vj1, vk1 = mf.get_jk(cell, dm) ej1 = numpy.einsum('ij,ji->', vj1, dm) ek1 = numpy.einsum('ij,ji->', vk1, dm) self.assertAlmostEqual(ej1, 242.17560414134402, 8) self.assertAlmostEqual(ek1, 280.27525544272237, 8) jkdf = mdf.MDF(cell) jkdf.metric = 'T' jkdf.approx_sr_level = 3 jkdf.auxbasis = 'weigend' jkdf.gs = (5, ) * 3 mf = mdf_jk.density_fit(mf0, with_df=jkdf) vj1, vk1 = mf.get_jk(cell, dm) ej1 = numpy.einsum('ij,ji->', vj1, dm) ek1 = numpy.einsum('ij,ji->', vk1, dm) self.assertAlmostEqual(ej1, 242.1790506110278, 8) self.assertAlmostEqual(ek1, 280.2763653230468, 8)
def test_jk_pbc_local_fit(self): numpy.random.seed(12) nao = cell.nao_nr() dm = numpy.random.random((nao, nao)) dm = dm + dm.T jkdf = mdf.MDF(cell) jkdf.metric = 'S' jkdf.auxbasis = 'weigend' jkdf.approx_sr_level = 2 jkdf.gs = (5, ) * 3 mf = mdf_jk.density_fit(mf0, with_df=jkdf) vj1, vk1 = mf.get_jk(cell, dm) ej1 = numpy.einsum('ij,ji->', vj1, dm) ek1 = numpy.einsum('ij,ji->', vk1, dm) self.assertAlmostEqual(ej1, 242.15904061299113, 9) self.assertAlmostEqual(ek1, 280.2553652304353, 9) jkdf = mdf.MDF(cell) jkdf.metric = 'T' jkdf.approx_sr_level = 3 jkdf.auxbasis = 'weigend' jkdf.gs = (5, ) * 3 mf = mdf_jk.density_fit(mf0, with_df=jkdf) vj1, vk1 = mf.get_jk(cell, dm) ej1 = numpy.einsum('ij,ji->', vj1, dm) ek1 = numpy.einsum('ij,ji->', vk1, dm) self.assertAlmostEqual(ej1, 242.17554856213894, 9) self.assertAlmostEqual(ek1, 280.27281092754305, 9)
def test_ao2mo_7d(self): L = 3. n = 6 cell = pgto.Cell() cell.a = numpy.diag([L, L, L]) cell.mesh = [n, n, n] cell.atom = '''He 2. 2.2 2. He 1.2 1. 1.''' cell.basis = {'He': [[0, (1.2, 1)], [1, (0.6, 1)]]} cell.verbose = 0 cell.build(0, 0) kpts = cell.make_kpts([1, 3, 1]) nkpts = len(kpts) nao = cell.nao_nr() numpy.random.seed(1) mo = (numpy.random.random((nkpts, nao, nao)) + numpy.random.random( (nkpts, nao, nao)) * 1j) with_df = mdf.MDF(cell, kpts) out = with_df.ao2mo_7d(mo, kpts) ref = numpy.empty_like(out) kconserv = kpts_helper.get_kconserv(cell, kpts) for ki, kj, kk in kpts_helper.loop_kkk(nkpts): kl = kconserv[ki, kj, kk] tmp = with_df.ao2mo((mo[ki], mo[kj], mo[kk], mo[kl]), kpts[[ki, kj, kk, kl]]) ref[ki, kj, kk] = tmp.reshape([nao] * 4) self.assertAlmostEqual(abs(out - ref).max(), 0, 12)
def test_get_eri_gamma(self): odf = mdf.MDF(cell) odf.auxbasis = 'weigend' odf.gs = (5, ) * 3 eri0000 = odf.get_eri() self.assertTrue(eri0000.dtype == numpy.double) self.assertAlmostEqual(eri0000.real.sum(), 140.52119757983661, 9) self.assertAlmostEqual(finger(eri0000), -1.2233310290052046, 9) eri1111 = kmdf.get_eri((kpts[0], kpts[0], kpts[0], kpts[0])) self.assertTrue(eri1111.dtype == numpy.double) self.assertAlmostEqual(eri1111.real.sum(), 140.52119757983661, 9) self.assertAlmostEqual(eri1111.imag.sum(), 0, 9) self.assertAlmostEqual(finger(eri1111), -1.2233310290052046, 9) self.assertTrue(numpy.allclose(eri1111, eri0000)) eri4444 = kmdf.get_eri((kpts[4], kpts[4], kpts[4], kpts[4])) self.assertTrue(eri4444.dtype == numpy.complex128) self.assertAlmostEqual(eri4444.real.sum(), 259.45414408787667, 9) self.assertAlmostEqual( abs(eri4444.imag).sum(), 0.00044186526697869387, 9) self.assertAlmostEqual(finger(eri4444), 1.968178030116118 - 3.6128363791369754e-07j, 8) eri0000 = ao2mo.restore(1, eri0000, cell.nao_nr()).reshape(eri4444.shape) self.assertTrue(numpy.allclose(eri0000, eri4444, atol=1e-7))
def test_jk(self): mf = mdf_jk.density_fit(mf0, auxbasis='weigend', gs=(5, ) * 3) dm = mf.get_init_guess() vj, vk = mf.get_jk(cell, dm) ej1 = numpy.einsum('ij,ji->', vj, dm) ek1 = numpy.einsum('ij,ji->', vk, dm) self.assertAlmostEqual(ej1, 46.698952864038205, 8) self.assertAlmostEqual(ek1, 31.724297969654103, 8) numpy.random.seed(12) nao = cell.nao_nr() dm = numpy.random.random((nao, nao)) dm = dm + dm.T vj1, vk1 = mf.get_jk(cell, dm, hermi=0) ej1 = numpy.einsum('ij,ji->', vj1, dm) ek1 = numpy.einsum('ij,ji->', vk1, dm) self.assertAlmostEqual(ej1, 242.19367653513686, 8) self.assertAlmostEqual(ek1, 280.28452000317549, 8) numpy.random.seed(1) kpt = numpy.random.random(3) mydf = mdf.MDF(cell, [kpt]) vj, vk = mydf.get_jk(dm, 1, kpt) ej1 = numpy.einsum('ij,ji->', vj, dm) ek1 = numpy.einsum('ij,ji->', vk, dm) self.assertAlmostEqual(ej1, 241.29943526257196 + 0j, 8) self.assertAlmostEqual(ek1, 691.7685639227974 + 0j, 8)
def test_k_kpts_1(self): cell = pgto.Cell() cell.atom = 'He 1. .5 .5; He .1 1.3 2.1' cell.basis = {'He': [(0, (2.5, 1)), (0, (1., 1))]} cell.a = numpy.eye(3) * 2.5 cell.mesh = [11] * 3 cell.build() kpts = cell.get_abs_kpts([[-.25,-.25,-.25], [-.25,-.25, .25], [-.25, .25,-.25], [-.25, .25, .25], [ .25,-.25,-.25], [ .25,-.25, .25], [ .25, .25,-.25], [ .25, .25, .25]]) numpy.random.seed(1) nao = cell.nao_nr() dm = numpy.random.random((8,nao,nao)) mydf = mdf.MDF(cell).set(auxbasis='weigend') mydf.linear_dep_threshold = 1e-7 mydf.kpts = kpts mydf.auxbasis = 'weigend' mydf.exxdiv = None mydf.mesh = numpy.asarray((11,)*3) mydf.eta = 0.3 vk = mdf_jk.get_k_kpts(mydf, dm, 0, mydf.kpts) self.assertAlmostEqual(finger(vk[0]), (0.54208542933016668-0.007872205456027636j ), 9) self.assertAlmostEqual(finger(vk[1]), (0.35976730327192219+0.0036055469686362227j), 9) self.assertAlmostEqual(finger(vk[2]), (0.46276307618592272-0.006504349523994527j ), 9) self.assertAlmostEqual(finger(vk[3]), (0.63667731843923825+0.0075118647005158034j), 9) self.assertAlmostEqual(finger(vk[4]), (0.53670632359622572-0.00764236264065816j ), 9) self.assertAlmostEqual(finger(vk[5]), (0.4960454361832054 +0.0060590376596187257j), 9) self.assertAlmostEqual(finger(vk[6]), (0.45421052168235576-0.006861624162215218j ), 9) self.assertAlmostEqual(finger(vk[7]), (0.41848054629487041+0.0051096775483082746j), 9)
def setUpModule(): global cell, cell1, kdf0, kpts, kpt0 cell = pgto.Cell() cell.atom = 'He 1. .5 .5; C .1 1.3 2.1' cell.basis = { 'He': [(0, (2.5, 1)), (0, (1., 1))], 'C': 'gth-szv', } cell.pseudo = {'C': 'gth-pade'} cell.a = np.eye(3) * 2.5 cell.mesh = [21] * 3 cell.build() np.random.seed(1) kpts = np.random.random((4, 3)) kpts[3] = kpts[0] - kpts[1] + kpts[2] kpt0 = np.zeros(3) cell1 = pgto.Cell() cell1.atom = 'He 1. .5 .5; He .1 1.3 2.1' cell1.basis = {'He': [(0, (2.5, 1)), (0, (1., 1))]} cell1.a = np.eye(3) * 2.5 cell1.mesh = [21] * 3 cell1.build() kdf0 = mdf.MDF(cell1) kdf0.auxbasis = 'weigend' kdf0.mesh = [21] * 3 kdf0.kpts = kpts
def test_jk_single_kpt(self): mf = mdf_jk.density_fit(mf0, auxbasis='weigend', gs=(5, ) * 3) mf.with_df.gs = [5] * 3 mf.with_df.eta = 0.3 dm = mf.get_init_guess() vj, vk = mf.get_jk(cell, dm) ej1 = numpy.einsum('ij,ji->', vj, dm) ek1 = numpy.einsum('ij,ji->', vk, dm) self.assertAlmostEqual(ej1, 46.698952864035398, 8) self.assertAlmostEqual(ek1, 31.724297969652138, 8) numpy.random.seed(12) nao = cell.nao_nr() dm = numpy.random.random((nao, nao)) dm = dm + dm.T vj1, vk1 = mf.get_jk(cell, dm, hermi=0) ej1 = numpy.einsum('ij,ji->', vj1, dm) ek1 = numpy.einsum('ij,ji->', vk1, dm) self.assertAlmostEqual(ej1, 242.19367653513683, 8) self.assertAlmostEqual(ek1, 280.28452000317401, 8) numpy.random.seed(1) kpt = numpy.random.random(3) mydf = mdf.MDF(cell, [kpt]).set(auxbasis='weigend') mydf.gs = [5] * 3 mydf.eta = 0.3 vj, vk = mydf.get_jk(dm, 1, kpt) ej1 = numpy.einsum('ij,ji->', vj, dm) ek1 = numpy.einsum('ij,ji->', vk, dm) self.assertAlmostEqual(ej1, 241.29943526257165 + 0j, 8) self.assertAlmostEqual(ek1, 691.76856392279456 + 0j, 8)
def density_fit(mf, auxbasis=None, gs=None, with_df=None): '''Generte density-fitting SCF object Args: auxbasis : str or basis dict Same format to the input attribute mol.basis. The default basis 'weigend+etb' means weigend-coulomb-fit basis for light elements and even-tempered basis for heavy elements. gs : tuple number of grids in each (+)direction with_df : MDF object ''' from pyscf.pbc.df import mdf if with_df is None: if hasattr(mf, 'kpts'): kpts = mf.kpts else: kpts = numpy.reshape(mf.kpt, (1,3)) with_df = mdf.MDF(mf.cell, kpts) with_df.max_memory = mf.max_memory with_df.stdout = mf.stdout with_df.verbose = mf.verbose with_df.auxbasis = auxbasis if gs is not None: with_df.gs = gs mf = copy.copy(mf) mf.with_df = with_df return mf
def test_k_kpts_1(self): cell = pgto.Cell() cell.atom = 'He 1. .5 .5; He .1 1.3 2.1' cell.basis = {'He': [(0, (2.5, 1)), (0, (1., 1))]} cell.a = numpy.eye(3) * 2.5 cell.gs = [5] * 3 cell.build() kpts = cell.get_abs_kpts([[-.25,-.25,-.25], [-.25,-.25, .25], [-.25, .25,-.25], [-.25, .25, .25], [ .25,-.25,-.25], [ .25,-.25, .25], [ .25, .25,-.25], [ .25, .25, .25]]) numpy.random.seed(1) nao = cell.nao_nr() dm = numpy.random.random((8,nao,nao)) mydf = mdf.MDF(cell) mydf.kpts = kpts mydf.auxbasis = 'weigend' mydf.exxdiv = None vk = mdf_jk.get_k_kpts(mydf, dm, 0, mydf.kpts) self.assertAlmostEqual(finger(vk[0]), (0.54208523897216732-0.0078722056426767627j), 9) self.assertAlmostEqual(finger(vk[1]), (0.35976712320322424+0.0036055471233112256j), 9) self.assertAlmostEqual(finger(vk[2]), (0.46276289905549617-0.0065043496646110801j), 9) self.assertAlmostEqual(finger(vk[3]), (0.63667714700745526+0.0075118648848031978j), 9) self.assertAlmostEqual(finger(vk[4]), (0.53670615076993111-0.0076423628684680065j), 9) self.assertAlmostEqual(finger(vk[5]), (0.49604529705337863+0.0060590377882828398j), 9) self.assertAlmostEqual(finger(vk[6]), (0.45421039104866701-0.0068616242937238692j), 9) self.assertAlmostEqual(finger(vk[7]), (0.41848041777106704+0.0051096776806240215j), 9)
def test_jk_single_kpt(self): mf = mdf_jk.density_fit(mf0, auxbasis='weigend', mesh=(11,)*3) mf.with_df.mesh = [11]*3 mf.with_df.eta = 0.3 mf.with_df.linear_dep_threshold = 1e-7 dm = mf.get_init_guess() vj, vk = mf.get_jk(cell, dm) ej1 = numpy.einsum('ij,ji->', vj, dm) ek1 = numpy.einsum('ij,ji->', vk, dm) self.assertAlmostEqual(ej1, 46.698950904264514, 8) self.assertAlmostEqual(ek1, 31.724297945923094, 8) numpy.random.seed(12) nao = cell.nao_nr() dm = numpy.random.random((nao,nao)) dm = dm + dm.T vj1, vk1 = mf.get_jk(cell, dm, hermi=0) ej1 = numpy.einsum('ij,ji->', vj1, dm) ek1 = numpy.einsum('ij,ji->', vk1, dm) self.assertAlmostEqual(ej1, 242.19379703364774, 8) self.assertAlmostEqual(ek1, 280.28450527230103, 8) numpy.random.seed(1) kpt = numpy.random.random(3) mydf = mdf.MDF(cell, [kpt]).set(auxbasis='weigend') mydf.linear_dep_threshold = 1e-7 mydf.mesh = [11]*3 mydf.eta = 0.3 vj, vk = mydf.get_jk(dm, 1, kpt, exxdiv='ewald') ej1 = numpy.einsum('ij,ji->', vj, dm) ek1 = numpy.einsum('ij,ji->', vk, dm) self.assertAlmostEqual(ej1, 241.29955504573206+0j, 8) self.assertAlmostEqual(ek1, 691.76854602384913+0j, 8)
def test_k_kpts_2(self): cell = pgto.Cell() cell.atom = 'He 1. .5 .5; He .1 1.3 2.1' cell.basis = {'He': [(0, (2.5, 1)), (0, (1., 1))]} cell.a = numpy.eye(3) * 2.5 cell.mesh = [11] * 3 cell.build() kpts = cell.get_abs_kpts([[-.25,-.25,-.25], [-.25,-.25, .25], [-.25, .25,-.25], [-.25, .25, .25], [ .25,-.25,-.25], [ .25,-.25, .25], [ .25, .25,-.25], [ .25, .25, .25]]) mydf = mdf.MDF(cell).set(auxbasis='weigend') mydf.linear_dep_threshold = 1e-7 mydf.kpts = kpts mydf.auxbasis = 'weigend' mydf.exxdiv = None mydf.mesh = numpy.asarray((11,)*3) mydf.eta = 0.3 nao = cell.nao_nr() numpy.random.seed(1) dm = numpy.random.random((8,nao,nao)) dm = dm + dm.transpose(0,2,1) vk = mdf_jk.get_k_kpts(mydf, dm, 1, mydf.kpts) self.assertAlmostEqual(finger(vk[0]), (1.0938028454012594 -0.014742352047969521j), 9) self.assertAlmostEqual(finger(vk[1]), (0.72086205228975953+0.008685417852198867j), 9) self.assertAlmostEqual(finger(vk[2]), (0.89846608130483796-0.011091006902191843j), 9) self.assertAlmostEqual(finger(vk[3]), (1.260302267937254 +0.015976908047169756j), 9) self.assertAlmostEqual(finger(vk[4]), (1.0490207113210688 -0.012426436820904021j), 9) self.assertAlmostEqual(finger(vk[5]), (0.99252601243537697+0.012694645170334074j), 9) self.assertAlmostEqual(finger(vk[6]), (0.92165252496655681-0.012036431811316108j), 9) self.assertAlmostEqual(finger(vk[7]), (0.85167195537981 +0.010089165459944484j), 9)
def density_fit(mf, auxbasis=None, mesh=None, with_df=None): '''Generte density-fitting SCF object Args: auxbasis : str or basis dict Same format to the input attribute mol.basis. If auxbasis is None, auxiliary basis based on AO basis (if possible) or even-tempered Gaussian basis will be used. mesh : tuple number of grids in each direction with_df : MDF object ''' from pyscf.pbc.df import mdf if with_df is None: if getattr(mf, 'kpts', None) is not None: kpts = mf.kpts else: kpts = numpy.reshape(mf.kpt, (1, 3)) with_df = mdf.MDF(mf.cell, kpts) with_df.max_memory = mf.max_memory with_df.stdout = mf.stdout with_df.verbose = mf.verbose with_df.auxbasis = auxbasis if mesh is not None: with_df.mesh = mesh mf = copy.copy(mf) mf.with_df = with_df mf._eri = None return mf
def test_jk(self): mf = mdf_jk.density_fit(mf0, auxbasis='weigend', gs=(5, ) * 3) dm = mf.get_init_guess() vj, vk = mf.get_jk(cell, dm) ej1 = numpy.einsum('ij,ji->', vj, dm) ek1 = numpy.einsum('ij,ji->', vk, dm) self.assertAlmostEqual(ej1, 46.698944426560104, 8) self.assertAlmostEqual(ek1, 31.723136985575099, 8) numpy.random.seed(12) nao = cell.nao_nr() dm = numpy.random.random((nao, nao)) dm = dm + dm.T vj1, vk1 = mf.get_jk(cell, dm, hermi=0) ej1 = numpy.einsum('ij,ji->', vj1, dm) ek1 = numpy.einsum('ij,ji->', vk1, dm) self.assertAlmostEqual(ej1, 242.18066893105836, 8) self.assertAlmostEqual(ek1, 280.27749933492305, 8) numpy.random.seed(1) kpt = numpy.random.random(3) mydf = mdf.MDF(cell, [kpt]) vj, vk = mydf.get_jk(dm, 1, kpt) ej1 = numpy.einsum('ij,ji->', vj, dm) ek1 = numpy.einsum('ij,ji->', vk, dm) self.assertAlmostEqual(ej1, 240.96370951556852, 8) self.assertAlmostEqual(ek1, 691.41117108086291, 8)
def test_get_eri_gamma_high_cost(self): odf = mdf.MDF(cell) odf.linear_dep_threshold = 1e-7 odf.auxbasis = 'weigend' odf.mesh = (11, ) * 3 odf.eta = 0.154728892598 eri0000 = odf.get_eri() self.assertTrue(eri0000.dtype == numpy.double) self.assertAlmostEqual(eri0000.real.sum(), 140.52553833398147, 6) self.assertAlmostEqual(finger(eri0000), -1.2234059928846319, 6) eri1111 = kmdf.get_eri((kpts[0], kpts[0], kpts[0], kpts[0])) self.assertTrue(eri1111.dtype == numpy.double) self.assertAlmostEqual(eri1111.real.sum(), 140.52553833398153, 6) self.assertAlmostEqual(eri1111.imag.sum(), 0, 7) self.assertAlmostEqual(finger(eri1111), -1.2234059928846333, 6) self.assertTrue(numpy.allclose(eri1111, eri0000)) eri4444 = kmdf.get_eri((kpts[4], kpts[4], kpts[4], kpts[4])) self.assertTrue(eri4444.dtype == numpy.complex128) self.assertAlmostEqual(eri4444.real.sum(), 259.46539833377523, 6) self.assertAlmostEqual( abs(eri4444.imag).sum(), 0.00044187056294873458, 9) self.assertAlmostEqual(finger(eri4444), 1.9705270829923354 - 3.6097479693720031e-07j, 6) eri0000 = ao2mo.restore(1, eri0000, cell.nao_nr()).reshape(eri4444.shape) self.assertTrue(numpy.allclose(eri0000, eri4444, atol=1e-7))
def test_jk(self): mf = mdf_jk.density_fit(mf0, auxbasis='weigend', gs=(5,)*3) dm = mf.get_init_guess() vj, vk = mf.get_jk(cell, dm) ej1 = numpy.einsum('ij,ji->', vj, dm) ek1 = numpy.einsum('ij,ji->', vk, dm) self.assertAlmostEqual(ej1, 46.698951249368726, 8) self.assertAlmostEqual(ek1, 31.724431026527277, 8) numpy.random.seed(12) nao = cell.nao_nr() dm = numpy.random.random((nao,nao)) dm = dm + dm.T vj1, vk1 = mf.get_jk(cell, dm, hermi=0) ej1 = numpy.einsum('ij,ji->', vj1, dm) ek1 = numpy.einsum('ij,ji->', vk1, dm) self.assertAlmostEqual(ej1, 242.19444220341163, 8) self.assertAlmostEqual(ek1, 280.28473250798748, 8) numpy.random.seed(1) kpt = numpy.random.random(3) mydf = mdf.MDF(cell, [kpt]) vj, vk = mydf.get_jk(dm, 1, kpt) ej1 = numpy.einsum('ij,ji->', vj, dm) ek1 = numpy.einsum('ij,ji->', vk, dm) self.assertAlmostEqual(ej1, 241.30019902835264+0j, 8) self.assertAlmostEqual(ek1, 691.76877471308376+0j, 8)
def test_k_kpts_2(self): cell = pgto.Cell() cell.atom = 'He 1. .5 .5; He .1 1.3 2.1' cell.basis = {'He': [(0, (2.5, 1)), (0, (1., 1))]} cell.a = numpy.eye(3) * 2.5 cell.gs = [5] * 3 cell.build() kpts = cell.get_abs_kpts([[-.25,-.25,-.25], [-.25,-.25, .25], [-.25, .25,-.25], [-.25, .25, .25], [ .25,-.25,-.25], [ .25,-.25, .25], [ .25, .25,-.25], [ .25, .25, .25]]) mydf = mdf.MDF(cell) mydf.kpts = kpts mydf.auxbasis = 'weigend' mydf.exxdiv = None nao = cell.nao_nr() numpy.random.seed(1) dm = numpy.random.random((8,nao,nao)) dm = dm + dm.transpose(0,2,1) vk = mdf_jk.get_k_kpts(mydf, dm, 1, mydf.kpts) self.assertAlmostEqual(finger(vk[0]), (1.093802464678856 -0.014742352431919676j), 9) self.assertAlmostEqual(finger(vk[1]), (0.72086169197091587+0.008685418217133528j), 9) self.assertAlmostEqual(finger(vk[2]), (0.89846572708244654-0.011091007223188223j), 9) self.assertAlmostEqual(finger(vk[3]), (1.2603019232380572 +0.015976908426091924j), 9) self.assertAlmostEqual(finger(vk[4]), (1.0490203655653567 -0.012426437226316521j), 9) self.assertAlmostEqual(finger(vk[5]), (0.99252573352507678+0.012694645461974137j), 9) self.assertAlmostEqual(finger(vk[6]), (0.92165226277159285-0.012036432073368958j), 9) self.assertAlmostEqual(finger(vk[7]), (0.85167169839559342+0.010089165714668009j), 9)
def test_get_eri_gamma(self): odf = mdf.MDF(cell) odf.auxbasis = 'weigend' odf.gs = (5, ) * 3 eri0000 = odf.get_eri() self.assertTrue(eri0000.dtype == numpy.double) self.assertAlmostEqual(eri0000.real.sum(), 140.52297323243539, 9) self.assertAlmostEqual(finger(eri0000), -1.2233877452643904, 9) eri1111 = kmdf.get_eri((kpts[0], kpts[0], kpts[0], kpts[0])) self.assertTrue(eri1111.dtype == numpy.double) self.assertAlmostEqual(eri1111.real.sum(), 140.52297323243539, 9) self.assertAlmostEqual(eri1111.imag.sum(), 0, 9) self.assertAlmostEqual(finger(eri1111), -1.2233877452643904, 9) self.assertTrue(numpy.allclose(eri1111, eri0000)) eri4444 = kmdf.get_eri((kpts[4], kpts[4], kpts[4], kpts[4])) self.assertTrue(eri4444.dtype == numpy.complex128) self.assertAlmostEqual(eri4444.real.sum(), 259.45730831500038, 9) self.assertAlmostEqual( abs(eri4444.imag).sum(), 0.00044186906554179585, 9) self.assertAlmostEqual(finger(eri4444), 1.9676993803147795 - 3.6138386194340396e-07j, 8) eri0000 = ao2mo.restore(1, eri0000, cell.nao_nr()).reshape(eri4444.shape) self.assertTrue(numpy.allclose(eri0000, eri4444, atol=1e-7))
def test_jk(self): mf = mdf_jk.density_fit(mf0, auxbasis='weigend', gs=(5, ) * 3) dm = mf.get_init_guess() vj, vk = mf.get_jk(cell, dm) ej1 = numpy.einsum('ij,ji->', vj, dm) ek1 = numpy.einsum('ij,ji->', vk, dm) self.assertAlmostEqual(ej1, 46.698923699173982, 9) self.assertAlmostEqual(ek1, 31.722934801807845, 9) numpy.random.seed(12) nao = cell.nao_nr() dm = numpy.random.random((nao, nao)) dm = dm + dm.T vj1, vk1 = mf.get_jk(cell, dm, hermi=0) ej1 = numpy.einsum('ij,ji->', vj1, dm) ek1 = numpy.einsum('ij,ji->', vk1, dm) self.assertAlmostEqual(ej1, 242.17738846073865, 9) self.assertAlmostEqual(ek1, 280.27434674577881, 9) numpy.random.seed(1) kpt = numpy.random.random(3) mydf = mdf.MDF(cell, [kpt]) vj, vk = mydf.get_jk(dm, 0, kpt) ej1 = numpy.einsum('ij,ji->', vj, dm) ek1 = numpy.einsum('ij,ji->', vk, dm) self.assertAlmostEqual(ej1, 240.96357256500787, 9) self.assertAlmostEqual(ek1, 691.41111361623132, 9)
def setUpModule(): global cell, cell1, mf0, kmdf, kmdf1, kpts L = 5. n = 11 cell = pgto.Cell() cell.a = numpy.diag([L,L,L]) cell.mesh = numpy.array([n,n,n]) cell.atom = '''C 3. 2. 3. C 1. 1. 1.''' cell.basis = 'ccpvdz' cell.verbose = 0 cell.rcut = 17 cell.build(0,0) mf0 = pscf.RHF(cell) mf0.exxdiv = 'vcut_sph' numpy.random.seed(1) kpts = numpy.random.random((5,3)) kpts[0] = 0 kpts[3] = kpts[0]-kpts[1]+kpts[2] kpts[4] *= 1e-5 kmdf = mdf.MDF(cell) kmdf.linear_dep_threshold = 1e-7 kmdf.auxbasis = 'weigend' kmdf.kpts = kpts kmdf.mesh = (11,)*3 kmdf.eta = 0.154728892598 cell1 = pgto.Cell() cell1.a = numpy.eye(3) * 3. cell1.mesh = [10]*3 cell1.atom = '''C 3. 2. 3. C 1. 1. 1.''' cell1.basis = [[0, (3.5, 1)], [0, (1.0, 1)], [1, (0.6, 1)]] cell1.rcut = 9.5 cell1.build(0,0) kmdf1 = mdf.MDF(cell1) kmdf1.linear_dep_threshold = 1e-7 kmdf1.auxbasis = df.aug_etb(cell1, 1.8) kmdf1.kpts = kpts kmdf1.mesh = [6]*3 kmdf1.eta = 0.1
def test_get_eri_gamma(self): odf0 = mdf.MDF(cell1) odf = aft.AFTDF(cell1) ref = odf0.get_eri() eri0000 = odf.get_eri(compact=True) self.assertTrue(eri0000.dtype == numpy.double) self.assertTrue(np.allclose(eri0000, ref, atol=1e-6, rtol=1e-6)) self.assertAlmostEqual(finger(eri0000), 0.23714016293926865, 9)
def test_hcore(self): mf = pscf.RHF(cell) odf = mdf.MDF(cell) odf.auxbasis = 'weigend' odf.gs = (10, ) * 3 dm = mf.get_init_guess() self.assertAlmostEqual(numpy.einsum('ij,ji->', odf.get_nuc(), dm), -150.61096872500249, 9)
def test_eri1111(self): kpts = numpy.random.random((4,3)) * .25 kpts[3] = -numpy.einsum('ij->j', kpts[:3]) with_df = mdf.MDF(cell) with_df.kpts = kpts mo =(numpy.random.random((nao,nao)) + numpy.random.random((nao,nao))*1j) eri = with_df.get_eri(kpts).reshape((nao,)*4) eri0 = numpy.einsum('pjkl,pi->ijkl', eri , mo.conj()) eri0 = numpy.einsum('ipkl,pj->ijkl', eri0, mo ) eri0 = numpy.einsum('ijpl,pk->ijkl', eri0, mo.conj()) eri0 = numpy.einsum('ijkp,pl->ijkl', eri0, mo ) eri1 = with_df.ao2mo(mo, kpts) self.assertAlmostEqual(abs(eri1.reshape(eri0.shape)-eri0).sum(), 0, 9)
def test_j_kpts(self): numpy.random.seed(1) nao = cell.nao_nr() dm = numpy.random.random((4,nao,nao)) dm = dm + dm.transpose(0,2,1) mydf = mdf.MDF(cell) mydf.kpts = numpy.random.random((4,3)) mydf.gs = numpy.asarray((5,)*3) mydf.auxbasis = 'weigend' vj = mdf_jk.get_j_kpts(mydf, dm, 1, mydf.kpts) self.assertAlmostEqual(finger(vj[0]), (0.48227656190426815-0.11872564582011846j ), 9) self.assertAlmostEqual(finger(vj[1]), (0.54073739901923457-0.046133364517070521j), 9) self.assertAlmostEqual(finger(vj[2]), (0.52806808175231224-0.083705005021225326j), 9) self.assertAlmostEqual(finger(vj[3]), (0.54351987747190522+0.008843499422802630j), 9)
def test_jk_hermi0(self): numpy.random.seed(12) nao = cell.nao_nr() dm = numpy.random.random((nao, nao)) dm = dm + dm.T dm[:2, -3:] *= .5 jkdf = mdf.MDF(cell).set(auxbasis='weigend') jkdf.gs = (5, ) * 3 jkdf.eta = 0.3 vj0, vk0 = jkdf.get_jk(dm, hermi=0, exxdiv=None) ej0 = numpy.einsum('ij,ji->', vj0, dm) ek0 = numpy.einsum('ij,ji->', vk0, dm) self.assertAlmostEqual(ej0, 242.18855706416096, 8) self.assertAlmostEqual(ek0, 280.70982164657647, 8)
def test_jk_hermi0(self): numpy.random.seed(12) nao = cell.nao_nr() dm = numpy.random.random((nao, nao)) dm = dm + dm.T dm[:2, -3:] *= .5 jkdf = mdf.MDF(cell) jkdf.auxbasis = 'weigend' jkdf.gs = (5, ) * 3 vj0, vk0 = jkdf.get_jk(dm, hermi=0, exxdiv=None) ej0 = numpy.einsum('ij,ji->', vj0, dm) ek0 = numpy.einsum('ij,ji->', vk0, dm) self.assertAlmostEqual(ej0, 242.18843656969429, 8) self.assertAlmostEqual(ek0, 280.70983914362279, 8)
def test_jk_hermi0(self): numpy.random.seed(12) nao = cell.nao_nr() dm = numpy.random.random((nao, nao)) dm = dm + dm.T dm[:2, -3:] *= .5 jkdf = mdf.MDF(cell) jkdf.auxbasis = 'weigend' jkdf.gs = (5, ) * 3 vj0, vk0 = jkdf.get_jk(dm, hermi=0, exxdiv=None) ej0 = numpy.einsum('ij,ji->', vj0, dm) ek0 = numpy.einsum('ij,ji->', vk0, dm) self.assertAlmostEqual(ej0, 242.17214791834408, 9) self.assertAlmostEqual(ek0, 280.69967132048043, 9)
def test_eri0110(self): kpts = numpy.random.random((4,3)) * .25 kpts[3] = kpts[0] kpts[2] = kpts[1] with_df = mdf.MDF(cell).set(auxbasis='weigend') with_df.kpts = kpts mo =(numpy.random.random((nao,nao)) + numpy.random.random((nao,nao))*1j) eri = with_df.get_eri(kpts).reshape((nao,)*4) eri0 = numpy.einsum('pjkl,pi->ijkl', eri , mo.conj()) eri0 = numpy.einsum('ipkl,pj->ijkl', eri0, mo ) eri0 = numpy.einsum('ijpl,pk->ijkl', eri0, mo.conj()) eri0 = numpy.einsum('ijkp,pl->ijkl', eri0, mo ) eri1 = with_df.ao2mo(mo, kpts) self.assertAlmostEqual(abs(eri1.reshape(eri0.shape)-eri0).sum(), 0, 8)
def test_jk_metric(self): numpy.random.seed(12) nao = cell.nao_nr() dm = numpy.random.random((nao, nao)) dm = dm + dm.T jkdf = mdf.MDF(cell) jkdf.metric = 'S' jkdf.auxbasis = 'weigend' jkdf.gs = (5, ) * 3 vj1, vk1 = jkdf.get_jk(dm, exxdiv=None) ej1 = numpy.einsum('ij,ji->', vj1, dm) ek1 = numpy.einsum('ij,ji->', vk1, dm) self.assertAlmostEqual(ej1, 242.1754481772021, 8) self.assertAlmostEqual(ek1, 280.2751263818186, 8) jkdf = mdf.MDF(cell) jkdf.metric = 'T' jkdf.auxbasis = 'weigend' jkdf.gs = (5, ) * 3 vj1, vk1 = jkdf.get_jk(dm, exxdiv=None) ej1 = numpy.einsum('ij,ji->', vj1, dm) ek1 = numpy.einsum('ij,ji->', vk1, dm) self.assertAlmostEqual(ej1, 242.18066893105842, 8) self.assertAlmostEqual(ek1, 280.27749933492305, 8)
def test_jk_hermi0(self): numpy.random.seed(12) nao = cell0.nao_nr() dm = numpy.random.random((nao,nao)) dm = dm + dm.T dm[:2,-3:] *= .5 jkdf = mdf.MDF(cell0).set(auxbasis='weigend') jkdf.linear_dep_threshold = 1e-7 jkdf.mesh = (11,)*3 jkdf.eta = 0.3 vj0, vk0 = jkdf.get_jk(dm, hermi=0, exxdiv=None) ej0 = numpy.einsum('ij,ji->', vj0, dm) ek0 = numpy.einsum('ij,ji->', vk0, dm) self.assertAlmostEqual(ej0, 25.77582611607388 , 8) self.assertAlmostEqual(ek0, 30.814555613338555, 8)