#!/usr/bin/env python ''' Gamma point Hartree-Fock/DFT The 2-electron integrals are computed using Poisson solver with FFT by default. In most scenario, it should be used with pseudo potential. ''' # Note import path which is different to molecule code from pyscf.pbc import gto, scf, dft import numpy cell = gto.Cell() # .a is a matrix for lattice vectors. cell.a = ''' 3.5668 0 0 0 3.5668 0 0 0 3.5668''' cell.atom = '''C 0. 0. 0. C 0.8917 0.8917 0.8917 C 1.7834 1.7834 0. C 2.6751 2.6751 0.8917 C 1.7834 0. 1.7834 C 2.6751 0.8917 2.6751 C 0. 1.7834 1.7834 C 0.8917 2.6751 2.6751''' cell.basis = 'gth-szv' cell.pseudo = 'gth-pade' cell.verbose = 4 cell.build()
-1, 1) Lpq = numpy.empty((self.blockdim, ao_pairs_G.shape[1])) for p0, p1 in lib.prange(0, ngrids, blksize): Lpq[:p1 - p0] = ao_pairs_G[p0:p1].real yield Lpq[:p1 - p0] Lpq[:p1 - p0] = ao_pairs_G[p0:p1].imag yield Lpq[:p1 - p0] def get_naoaux(self): mesh = numpy.asarray(self.mesh) ngrids = numpy.prod(mesh) return ngrids * 2 if __name__ == '__main__': from pyscf.pbc import gto as pbcgto cell = pbcgto.Cell() cell.verbose = 0 cell.atom = 'C 0 0 0; C 1 1 1; C 0 2 2; C 2 0 2' cell.a = numpy.diag([4, 4, 4]) cell.basis = 'gth-szv' cell.pseudo = 'gth-pade' cell.mesh = [20] * 3 cell.build() k = numpy.ones(3) * .25 df = FFTDF(cell) v1 = get_pp(df, k) print(lib.finger(v1) - (1.8428463642697195 - 0.10478381725330854j)) v1 = get_nuc(df, k) print(lib.finger(v1) - (2.3454744614944714 - 0.12528407127454744j))
vk = vkR + vkI * 1j if cell.dimension != 3 and exxdiv: assert (exxdiv.lower() == 'ewald') _ewald_exxdiv_for_G0(cell, kpt, dms, vk) vk = vk.reshape(dm.shape) return vj, vk if __name__ == '__main__': from pyscf.pbc import gto as pgto from pyscf.pbc import scf as pscf from pyscf.pbc.df import aft L = 5. n = 5 cell = pgto.Cell() cell.a = numpy.diag([L, L, L]) cell.gs = numpy.array([n, n, n]) cell.atom = '''He 3. 2. 3. He 1. 1. 1.''' #cell.basis = {'He': [[0, (1.0, 1.0)]]} #cell.basis = '631g' #cell.basis = {'He': [[0, (2.4, 1)], [1, (1.1, 1)]]} cell.basis = 'ccpvdz' cell.verbose = 0 cell.build(0, 0) cell.verbose = 5 df = aft.AFTDF(cell) df.gs = (15, ) * 3
#!/usr/bin/env python from pyscf.pbc import df, scf ### generated system text ### from numpy import array from pyscf.pbc import gto as gto_loc cell = gto_loc.Cell() cell.a = ''' 1.78500000 1.78500000 0.00000000 0.00000000 1.78500000 1.78500000 1.78500000 0.00000000 1.78500000 ''' cell.basis = 'bfd-vdz' cell.dimension = 3 cell.ecp = 'bfd' cell.unit = 'A' cell.atom = ''' C 0.00000000 0.00000000 0.00000000 C 0.89250000 0.89250000 0.89250000 ''' cell.drop_exponent = 0.1 cell.verbose = 5 cell.charge = 0 cell.spin = 0 cell.build() kpts = array([ [0.0, 0.0, 0.0]]) ### end generated system text ###
def setUpClass(cls): k_grid = [2, 1, 1] # Unit cell cls.cell = gto.Cell() cls.cell.atom = ''' C 0.000000000000 0.000000000000 0.000000000000 C 1.685068664391 1.685068664391 1.685068664391 ''' cls.cell.basis = 'gth-szv' cls.cell.pseudo = 'gth-pade' cls.cell.a = ''' 0.000000000, 3.370137329, 3.370137329 3.370137329, 0.000000000, 3.370137329 3.370137329, 3.370137329, 0.000000000 ''' cls.cell.unit = 'B' cls.cell.verbose = 7 cls.cell.mesh = [24] * 3 cls.cell.build() # MF cls.mf = scf.KRHF(cls.cell, kpts=cls.cell.make_kpts(k_grid), exxdiv=None) cls.mf.chkfile = 'test_kgf_proper_rhf_chk.dat' if os.path.exists(cls.mf.chkfile): cls.mf.update() else: cls.mf.kernel() # Supercell cls.cell2 = super_cell(cls.cell, k_grid) cls.mf2 = scf.KRHF(cls.cell2, exxdiv=None) cls.order = numpy.argsort(numpy.hstack(cls.mf.mo_energy)) c1 = cls.mf.mo_coeff[0] c2 = cls.mf.mo_coeff[1] c1, c2 = numpy.concatenate((c1, c1), axis=0) / 2.**.5, numpy.concatenate((c2, -c2), axis=0) / 2.**.5 cls.mf2.mo_coeff = [numpy.concatenate((c1, c2), axis=1)[:, cls.order]] cls.mf2.mo_energy = [numpy.concatenate(cls.mf.mo_energy)[cls.order]] cls.mf2.mo_occ = [numpy.concatenate(cls.mf.mo_occ)[cls.order]] cls.mf2.make_rdm1() # CCSD cls.ccsd = cc.KRCCSD(cls.mf) cls.ccsd.conv_tol_normt = 1e-8 cls.ccsd.kernel() cls.ccsd2 = cc.KRCCSD(cls.mf2) cls.ccsd2.conv_tol_normt = 1e-8 cls.ccsd2.kernel() # TODO: lambda iterations cls.ccsd.l1 = cls.ccsd.t1 cls.ccsd.l2 = cls.ccsd.t2 # cls.ccsd.ipccsd(nroots=2) # cls.ccsd.eaccsd(nroots=2) # cls.ip_samples = [-0.71, -1] cls.ip_samples = [-0.71] cls.ea_samples = [1.13, 0.3] cls.eta = 0.01 # ===== cls.nocc, cls.nvirt = cls.ccsd.t1[0].shape cls.nmo = cls.nocc + cls.nvirt cls.o1 = cls.order[:2*cls.nocc] < cls.nmo cls.o2 = cls.order[:2*cls.nocc] >= cls.nmo cls.v1 = cls.order[2*cls.nocc:] < cls.nmo cls.v2 = cls.order[2*cls.nocc:] >= cls.nmo cls.a1 = cls.order < cls.nmo cls.a2 = cls.order >= cls.nmo # ===== cls.ccsd2.l1 = cls.ccsd2.t1 cls.ccsd2.l2 = cls.ccsd2.t2
def test_h4_fcc_k2_frozen(self): '''Metallic hydrogen fcc lattice with frozen lowest lying occupied and highest lying virtual orbitals. Checks versus a corresponding supercell calculation. NOTE: different versions of the davidson may converge to a different solution for the k-point IP/EA eom. If you're getting the wrong root, check to see if it's contained in the supercell set of eigenvalues.''' cell = pbcgto.Cell() cell.atom = [['H', (0.000000000, 0.000000000, 0.000000000)], ['H', (0.000000000, 0.500000000, 0.250000000)], ['H', (0.500000000, 0.500000000, 0.500000000)], ['H', (0.500000000, 0.000000000, 0.750000000)]] cell.unit = 'Bohr' cell.a = [[1., 0., 0.], [0., 1., 0], [0, 0, 2.2]] cell.verbose = 7 cell.spin = 0 cell.charge = 0 cell.basis = [ [0, [1.0, 1]], ] cell.pseudo = 'gth-pade' cell.output = '/dev/null' #cell.max_memory = 1000 for i in range(len(cell.atom)): cell.atom[i][1] = tuple( np.dot(np.array(cell.atom[i][1]), np.array(cell.a))) cell.build() nmp = [2, 1, 1] kmf = pbcscf.KRHF(cell) kmf.kpts = cell.make_kpts(nmp, scaled_center=[0.0, 0.0, 0.0]) e = kmf.kernel() frozen = [[0, 3], []] mycc = pbcc.KCCSD(kmf, frozen=frozen) ekccsd, _, _ = mycc.kernel() self.assertAlmostEqual(ekccsd, -0.04683399814247455, 6) # Getting more roots than 1 is difficult e = mycc.eaccsd(nroots=1, koopmans=False, kptlist=(0, ))[0] self.assertAlmostEqual(e[0][0], 5.060562738181741, 6) e = mycc.eaccsd(nroots=1, koopmans=False, kptlist=(1, ))[0] self.assertAlmostEqual(e[0][0], 4.188511644938458, 6) e = mycc.ipccsd(nroots=1, koopmans=False, kptlist=(0, ))[0] self.assertAlmostEqual(e[0][0], -3.477663551987023, 6) e = mycc.ipccsd(nroots=1, koopmans=False, kptlist=(1, ))[0] self.assertAlmostEqual(e[0][0], -4.23523412155825, 6) # Start of supercell calculations from pyscf.pbc.tools.pbc import super_cell supcell = super_cell(cell, nmp) supcell.build() mf = pbcscf.KRHF(supcell) e = mf.kernel() myscc = pbcc.KCCSD(mf, frozen=[0, 7]) eccsd, _, _ = myscc.kernel() eccsd /= np.prod(nmp) self.assertAlmostEqual(eccsd, -0.04683401678904569, 6) e = myscc.eaccsd(nroots=4, koopmans=False, kptlist=(0, ))[0] self.assertAlmostEqual(e[0][0], 4.188511680212755, 6) self.assertAlmostEqual(e[0][1], 4.205924087610756, 6) self.assertAlmostEqual(e[0][2], 5.060562771978923, 6) self.assertAlmostEqual(e[0][3], 5.077249823137741, 6) e = myscc.ipccsd(nroots=4, koopmans=False, kptlist=(0, ))[0] self.assertAlmostEqual(e[0][0], -4.261818242746091, 6) self.assertAlmostEqual(e[0][1], -4.235233956876479, 6) self.assertAlmostEqual(e[0][2], -3.477663568390151, 6) self.assertAlmostEqual(e[0][3], -3.459133332687474, 6)
def test_h4_fcc_k2(self): '''Metallic hydrogen fcc lattice. Checks versus a corresponding supercell calculation. NOTE: different versions of the davidson may converge to a different solution for the k-point IP/EA eom. If you're getting the wrong root, check to see if it's contained in the supercell set of eigenvalues.''' cell = pbcgto.Cell() cell.atom = [['H', (0.000000000, 0.000000000, 0.000000000)], ['H', (0.000000000, 0.500000000, 0.250000000)], ['H', (0.500000000, 0.500000000, 0.500000000)], ['H', (0.500000000, 0.000000000, 0.750000000)]] cell.unit = 'Bohr' cell.a = [[1., 0., 0.], [0., 1., 0], [0, 0, 2.2]] cell.verbose = 7 cell.spin = 0 cell.charge = 0 cell.basis = [ [0, [1.0, 1]], ] cell.pseudo = 'gth-pade' cell.output = '/dev/null' #cell.max_memory = 1000 for i in range(len(cell.atom)): cell.atom[i][1] = tuple( np.dot(np.array(cell.atom[i][1]), np.array(cell.a))) cell.build() nmp = [2, 1, 1] kmf = pbcscf.KRHF(cell) kmf.kpts = cell.make_kpts(nmp, scaled_center=[0.0, 0.0, 0.0]) e = kmf.kernel() mycc = pbcc.KCCSD(kmf) ekccsd, _, _ = mycc.kernel() self.assertAlmostEqual(ekccsd, -0.06146759560406628, 6) # Getting more roots than 1 is difficult e = mycc.eaccsd(nroots=1, koopmans=False, kptlist=(0, ))[0] self.assertAlmostEqual(e[0][0], 5.079427283440857, 6) e = mycc.eaccsd(nroots=1, koopmans=False, kptlist=(1, ))[0] self.assertAlmostEqual(e[0][0], 4.183328878177331, 6) e = mycc.ipccsd(nroots=1, koopmans=False, kptlist=(0, ))[0] self.assertAlmostEqual(e[0][0], -3.471710821544506, 6) e = mycc.ipccsd(nroots=1, koopmans=False, kptlist=(1, ))[0] self.assertAlmostEqual(e[0][0], -4.272015727359054, 6) # Start of supercell calculations from pyscf.pbc.tools.pbc import super_cell supcell = super_cell(cell, nmp) supcell.build() mf = pbcscf.KRHF(supcell) e = mf.kernel() myscc = pbcc.KCCSD(mf) eccsd, _, _ = myscc.kernel() eccsd /= np.prod(nmp) self.assertAlmostEqual(eccsd, -0.06146759560406628, 6) e = myscc.eaccsd(nroots=4, koopmans=False, kptlist=(0, ))[0] self.assertAlmostEqual(e[0][0], 4.183328873793568, 6) self.assertAlmostEqual(e[0][1], 4.225034294249784, 6) self.assertAlmostEqual(e[0][2], 5.068962665511664, 6) self.assertAlmostEqual(e[0][3], 5.07942727935064, 6) e = myscc.ipccsd(nroots=4, koopmans=False, kptlist=(0, ))[0] self.assertAlmostEqual(e[0][0], -4.272015724869052, 6) self.assertAlmostEqual(e[0][1], -4.254298274388934, 6) self.assertAlmostEqual(e[0][2], -3.471710821688812, 6) self.assertAlmostEqual(e[0][3], -3.462817764320668, 6)
# limitations under the License. # # Author: Qiming Sun <*****@*****.**> # import unittest import ctypes import numpy import numpy as np from pyscf import gto from pyscf import lib from pyscf.pbc import gto as pgto L = 1.5 n = 41 cl = pgto.Cell() cl.build(a=[[L, 0, 0], [0, L, 0], [0, 0, L]], mesh=[n, n, n], atom='He %f %f %f' % ((L / 2., ) * 3), basis='ccpvdz') numpy.random.seed(1) cl1 = pgto.Cell() cl1.build(a=numpy.random.random((3, 3)).T, precision=1e-9, mesh=[n, n, n], atom='''He .1 .0 .0 He .5 .1 .0 He .0 .5 .0 He .1 .3 .2''', basis='ccpvdz')
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.h = numpy.eye(3) * 2.5 cell.gs = [5] * 3 cell.build() kpts = cell.make_kpts((2, 2, 2)) 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.54167993564799133 - 0.0078722013632614562j), 9) self.assertAlmostEqual(finger(vk[1]), (0.3593714867663208 + 0.0036054974436528706j), 9) self.assertAlmostEqual(finger(vk[2]), (0.46234927585203783 - 0.0065043520189423856j), 9) self.assertAlmostEqual(finger(vk[3]), (0.63626519337979948 + 0.0075118282426430818j), 9) self.assertAlmostEqual(finger(vk[4]), (0.53630228285345427 - 0.0076423773076679524j), 9) self.assertAlmostEqual(finger(vk[5]), (0.4956577130753681 + 0.0060590034846796439j), 9) self.assertAlmostEqual(finger(vk[6]), (0.45380714659933519 - 0.0068616367194157396j), 9) self.assertAlmostEqual(finger(vk[7]), (0.41808297427780505 + 0.0051096506509061114j), 9) 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.0929902110020242 - 0.014742288503285355j), 9) self.assertAlmostEqual(finger(vk[1]), (0.72006803096300986 + 0.0086853082996361259j), 9) self.assertAlmostEqual(finger(vk[2]), (0.89763598759314234 - 0.011090943227218j), 9) self.assertAlmostEqual(finger(vk[3]), (1.2594746237554506 + 0.015976836949610319j), 9) self.assertAlmostEqual(finger(vk[4]), (1.0482108645807622 - 0.012426406003793576j), 9) self.assertAlmostEqual(finger(vk[5]), (0.99174830962018801 + 0.012694552375379626j), 9) self.assertAlmostEqual(finger(vk[6]), (0.9208432538335829 - 0.01203638564702492j), 9) self.assertAlmostEqual(finger(vk[7]), (0.8508739289672812 + 0.010089099047683993j), 9)
def test_eomea_matvec(self): cell = gto.Cell() cell.atom = ''' He 0.000000000000 0.000000000000 0.000000000000 He 1.685068664391 1.685068664391 1.685068664391 ''' cell.basis = [[0, (1., 1.)], [0, (.5, 1.)]] cell.a = ''' 0.000000000, 3.370137329, 3.370137329 3.370137329, 0.000000000, 3.370137329 3.370137329, 3.370137329, 0.000000000''' cell.unit = 'B' cell.mesh = [13] * 3 cell.build() np.random.seed(2) kmf = pbcscf.KUHF(cell, kpts=cell.make_kpts([1, 1, 3]), exxdiv=None) nmo = cell.nao_nr() kmf.mo_occ = np.zeros((2, 3, nmo)) kmf.mo_occ[0, :, :3] = 1 kmf.mo_occ[1, :, :1] = 1 kmf.mo_energy = np.arange(nmo) + np.random.random((2, 3, nmo)) * .3 kmf.mo_energy[kmf.mo_occ == 0] += 2 mo = (np.random.random((2, 3, nmo, nmo)) + np.random.random( (2, 3, nmo, nmo)) * 1j - .5 - .5j) s = kmf.get_ovlp() kmf.mo_coeff = np.empty_like(mo) nkpts = len(kmf.kpts) for k in range(nkpts): kmf.mo_coeff[0, k] = lo.orth.vec_lowdin(mo[0, k], s[k]) kmf.mo_coeff[1, k] = lo.orth.vec_lowdin(mo[1, k], s[k]) def rand_t1_t2(mycc): nkpts = mycc.nkpts nocca, noccb = mycc.nocc nmoa, nmob = mycc.nmo nvira, nvirb = nmoa - nocca, nmob - noccb np.random.seed(1) t1a = (np.random.random((nkpts, nocca, nvira)) + np.random.random( (nkpts, nocca, nvira)) * 1j - .5 - .5j) t1b = (np.random.random((nkpts, noccb, nvirb)) + np.random.random( (nkpts, noccb, nvirb)) * 1j - .5 - .5j) t2aa = ( np.random.random( (nkpts, nkpts, nkpts, nocca, nocca, nvira, nvira)) + np.random.random( (nkpts, nkpts, nkpts, nocca, nocca, nvira, nvira)) * 1j - .5 - .5j) kconserv = kpts_helper.get_kconserv(kmf.cell, kmf.kpts) t2aa = t2aa - t2aa.transpose(1, 0, 2, 4, 3, 5, 6) tmp = t2aa.copy() for ki, kj, kk in kpts_helper.loop_kkk(nkpts): kl = kconserv[ki, kk, kj] t2aa[ki, kj, kk] = t2aa[ki, kj, kk] - tmp[ki, kj, kl].transpose( 0, 1, 3, 2) t2ab = ( np.random.random( (nkpts, nkpts, nkpts, nocca, noccb, nvira, nvirb)) + np.random.random( (nkpts, nkpts, nkpts, nocca, noccb, nvira, nvirb)) * 1j - .5 - .5j) t2bb = ( np.random.random( (nkpts, nkpts, nkpts, noccb, noccb, nvirb, nvirb)) + np.random.random( (nkpts, nkpts, nkpts, noccb, noccb, nvirb, nvirb)) * 1j - .5 - .5j) t2bb = t2bb - t2bb.transpose(1, 0, 2, 4, 3, 5, 6) tmp = t2bb.copy() for ki, kj, kk in kpts_helper.loop_kkk(nkpts): kl = kconserv[ki, kk, kj] t2bb[ki, kj, kk] = t2bb[ki, kj, kk] - tmp[ki, kj, kl].transpose( 0, 1, 3, 2) t1 = (t1a, t1b) t2 = (t2aa, t2ab, t2bb) return t1, t2 mycc = pbcc.KUCCSD(kmf) t1, t2 = rand_t1_t2(mycc) mycc.t1 = t1 mycc.t2 = t2 eris = mycc.ao2mo() eom = EOMEA(mycc) imds = eom.make_imds(eris) np.random.seed(9) vector = np.random.random(eom.vector_size()) hc = eom.matvec(vector, 0, imds) self.assertAlmostEqual(lib.finger(hc), (4.126336947439054 + 0.5931985341760211j), 9) hc = eom.matvec(vector, 1, imds) self.assertAlmostEqual(lib.finger(hc), (1.248516714348047 + 2.310336141756983j), 9) hc = eom.matvec(vector, 2, imds) self.assertAlmostEqual(lib.finger(hc), (-3.4529892564020126 - 5.093287166283228j), 9) kmf = kmf.density_fit(auxbasis=[[0, (2., 1.)], [0, (1., 1.)], [0, (.5, 1.)]]) mycc._scf = kmf mycc.max_memory = 0 eris = mycc.ao2mo() imds = eom.make_imds(eris) hc = eom.matvec(vector, 0, imds) self.assertAlmostEqual(lib.finger(hc), (4.045928342346641 + 0.5861843966140339j), 9) hc = eom.matvec(vector, 1, imds) self.assertAlmostEqual(lib.finger(hc), (1.2695743252320795 + 2.28060203958305j), 9) hc = eom.matvec(vector, 2, imds) self.assertAlmostEqual(lib.finger(hc), (-3.435385905375094 - 5.0991524119952505j), 9) mycc.max_memory = 4000 eris = mycc.ao2mo() imds = eom.make_imds(eris) hc = eom.matvec(vector, 0, imds) self.assertAlmostEqual(lib.finger(hc), (4.045928342346641 + 0.5861843966140339j), 9) hc = eom.matvec(vector, 1, imds) self.assertAlmostEqual(lib.finger(hc), (1.2695743252320795 + 2.28060203958305j), 9) hc = eom.matvec(vector, 2, imds) self.assertAlmostEqual(lib.finger(hc), (-3.435385905375094 - 5.0991524119952505j), 9)