def test_ecp_keyword_in_pseudo(self): cell = pgto.M( a = np.eye(3)*5, gs = [4]*3, atom = 'S 0 0 1', ecp = 'lanl2dz', pseudo = {'O': 'gthbp', 'Cu': 'stuttgartrsc'}) self.assertEqual(cell.ecp, 'lanl2dz') self.assertEqual(cell.pseudo, {'O': 'gthbp'}) cell = pgto.M( a = np.eye(3)*5, gs = [4]*3, atom = 'S 0 0 1', ecp = {'na': 'lanl2dz'}, pseudo = {'O': 'gthbp', 'Cu': 'stuttgartrsc'}) self.assertEqual(cell.ecp, {'na': 'lanl2dz', 'Cu': 'stuttgartrsc'}) self.assertEqual(cell.pseudo, {'O': 'gthbp'}) cell = pgto.M( a = np.eye(3)*5, gs = [4]*3, atom = 'S 0 0 1', pseudo = {'O': 'gthbp', 'Cu': 'stuttgartrsc'}) self.assertEqual(cell.ecp, {'Cu': 'stuttgartrsc'}) self.assertEqual(cell.pseudo, {'O': 'gthbp'}) cell = pgto.M( a = np.eye(3)*5, gs = [4]*3, atom = 'S 0 0 1', ecp = {'S': 'gthbp', 'na': 'lanl2dz'}, pseudo = {'O': 'gthbp', 'Cu': 'stuttgartrsc'}) self.assertEqual(cell.ecp, {'na': 'lanl2dz', 'Cu': 'stuttgartrsc'}) self.assertEqual(cell.pseudo, {'S': 'gthbp', 'O': 'gthbp'})
def test_pseudo_suffix(self): cell = pgto.M(a=np.eye(3) * 5, mesh=[9] * 3, atom='Mg 0 0 1', pseudo={'Mg': 'gth-lda'}) self.assertEqual(cell.atom_nelec_core(0), 2) cell = pgto.M(a=np.eye(3) * 5, mesh=[9] * 3, atom='Mg 0 0 1', pseudo={'Mg': 'gth-lda q2'}) self.assertEqual(cell.atom_nelec_core(0), 10)
def test_conc_cell(self): cl1 = pgto.M(a=np.eye(3) * 5, atom='Cu', basis='lanl2dz', ecp='lanl2dz', spin=None) cl2 = pgto.M(a=np.eye(3) * 5, atom='Cs', basis='lanl2dz', ecp='lanl2dz', spin=None) cl3 = cl1 + cl2 self.assertTrue(len(cl3._ecpbas), 20) self.assertTrue(len(cl3._bas), 12) self.assertTrue(len(cl3._atm), 8)
def test(): from pyscf.pbc import gto, scf import pyqmc import pandas as pd L = 4 mol = gto.M( atom="""H {0} {0} {0}""".format(0.0), basis="sto-3g", a=np.eye(3) * L, spin=1, unit="bohr", ) mf = scf.UKS(mol) mf.xc = "pbe" mf = mf.density_fit().run() wf = pyqmc.PySCFSlaterUHF(mol, mf) ##################################### ## evaluate KE in PySCF ##################################### ke_mat = mol.pbc_intor("int1e_kin", hermi=1, kpts=np.array([0, 0, 0])) dm = mf.make_rdm1() pyscfke = np.einsum("ij,ji", ke_mat, dm[0]) print("PySCF kinetic energy: {0}".format(pyscfke)) ##################################### ## evaluate KE integral on grid ##################################### X = np.linspace(0, 1, 20, endpoint=False) XYZ = np.meshgrid(X, X, X, indexing="ij") pts = [np.outer(p.ravel(), mol.a[i]) for i, p in enumerate(XYZ)] coords = np.sum(pts, axis=0).reshape((-1, 1, 3)) phase, logdet = wf.recompute(coords) psi = phase * np.exp(logdet) lap = wf.laplacian(0, coords.reshape((-1, 3))) gridke = np.sum(-0.5 * lap * psi ** 2) / np.sum(psi ** 2) print("grid kinetic energy: {0}".format(gridke)) ##################################### ## evaluate KE integral with VMC ##################################### coords = pyqmc.initial_guess(mol, 600, 0.7) coords = PeriodicConfigs(coords, mol.a) warmup = 10 df, coords = pyqmc.vmc( wf, coords, nsteps=128 + warmup, tstep=L * 0.6, accumulators={"energy": pyqmc.accumulators.EnergyAccumulator(mol)}, ) df = pd.DataFrame(df) reblocked = pyqmc.reblock.optimally_reblocked(df["energyke"][warmup:]) print( "VMC kinetic energy: {0} $\pm$ {1}".format( reblocked["mean"], reblocked["standard error"] ) )
def test_cell_plus_imgs(self): numpy.random.seed(2) cl1 = pbcgto.M(a = numpy.random.random((3,3))*3, mesh = [3]*3, atom ='''He .1 .0 .0''', basis = 'ccpvdz') cl2 = tools.cell_plus_imgs(cl1, cl1.nimgs) self.assertAlmostEqual(lib.finger(cl2.atom_coords()), 465.86333525744129, 9)
def setUpModule(): global cell_orth, cell_nonorth, cell_he, mydf global kpts, nao, dm, dm1, vj_uks_orth, he_nao, dm_he numpy.random.seed(2) cell_orth = gto.M( verbose=7, output='/dev/null', a=numpy.eye(3) * 3.5668, atom='''C 0. 0. 0. C 1.8 1.8 1.8 ''', basis='gth-dzv', pseudo='gth-pade', precision=1e-9, mesh=[48] * 3, ) cell_nonorth = gto.M( a=numpy.eye(3) * 3.5668 + numpy.random.random((3, 3)), atom='''C 0. 0. 0. C 0.8917 0.8917 0.8917''', basis='gth-dzv', pseudo='gth-pade', precision=1e-9, mesh=[44, 43, 42], ) cell_he = gto.M(atom='He 0 0 0', basis=[[0, (1, 1, .1), (.5, .1, 1)], [1, (.8, 1)]], unit='B', precision=1e-9, mesh=[18] * 3, a=numpy.eye(3) * 5) kptsa = numpy.random.random((2, 3)) kpts = kptsa.copy() kpts[1] = -kpts[0] nao = cell_orth.nao_nr() dm = numpy.random.random((len(kpts), nao, nao)) * .2 dm1 = dm + numpy.eye(nao) dm = dm1 + dm1.transpose(0, 2, 1) mydf = df.FFTDF(cell_orth) vj_uks_orth = mydf.get_jk(dm1, with_k=False)[0] he_nao = cell_he.nao dm_he = numpy.random.random((len(kpts), he_nao, he_nao)) dm_he = dm_he + dm_he.transpose(0, 2, 1) dm_he = dm_he * .2 + numpy.eye(he_nao)
def test_super_cell(self): numpy.random.seed(2) cl1 = pbcgto.M(a = numpy.random.random((3,3))*3, mesh = [3]*3, atom ='''He .1 .0 .0''', basis = 'ccpvdz') cl2 = tools.super_cell(cl1, [2,3,4]) self.assertAlmostEqual(lib.finger(cl2.atom_coords()), -18.946080642714836, 9)
def test_get_lattice_Ls(self): numpy.random.seed(2) cl1 = pbcgto.M(a=numpy.random.random((3, 3)) * 3, gs=[1] * 3, atom='''He .1 .0 .0''', basis='ccpvdz') Ls = tools.get_lattice_Ls(cl1) self.assertEqual(Ls.shape, (1509, 3))
def setUpModule(): global cell, kband cell = gto.M(atom='H 1 2 1; H 1 1 1', basis=[[0, (.8, 1)], [1, (0.5, 1)]], a=numpy.eye(3) * 2.5, verbose=0, mesh=[11] * 3) numpy.random.seed(1) kband = numpy.random.random((2, 3))
def test_cell_plus_imgs(self): numpy.random.seed(2) cl1 = pbcgto.M(a=numpy.random.random((3, 3)) * 3, gs=[1] * 3, atom='''He .1 .0 .0''', basis='ccpvdz') cl2 = tools.cell_plus_imgs(cl1, cl1.nimgs) self.assertAlmostEqual(finger(cl2.atom_coords()), 22.233540464902909, 9)
def test_ghost(self): cell = pgto.Cell( atom='C 0 0 0; ghost 0 0 2', basis={ 'C': 'sto3g', 'ghost': gto.basis.load('sto3g', 'H') }, a=np.eye(3) * 3, pseudo='gth-pade', ).run() self.assertEqual(cell.nao_nr(), 6) cell = pgto.M(atom=''' ghost-O 0.000000000 0.000000000 2.500000000 X_H -0.663641000 -0.383071000 3.095377000 ghost.H 0.663588000 0.383072000 3.095377000 O 1.000000000 0.000000000 2.500000000 H -1.663641000 -0.383071000 3.095377000 H 1.663588000 0.383072000 3.095377000 ''', a=np.eye(3) * 3, pseudo={ 'default': 'gth-pade', 'ghost-O': 'gth-pade' }, basis='gth-dzv') self.assertEqual(cell.nao_nr(), 24) # 8 + 2 + 2 + 8 + 2 + 2 self.assertTrue(len(cell._pseudo) == 3) # O, H, ghost-O in ecp cell = pgto.M(atom=''' ghost-O 0.000000000 0.000000000 2.500000000 X_H -0.663641000 -0.383071000 3.095377000 ghost.H 0.663588000 0.383072000 3.095377000 O 1.000000000 0.000000000 2.500000000 ''', a=np.eye(3) * 3, pseudo='gth-pade', basis={ 'H': 'gth-dzv', 'o': 'gth-dzvp', 'ghost-O': 'gth-szv' }) self.assertEqual(cell.nao_nr(), 21) # 4 + 2 + 2 + 13 self.assertTrue(len(cell._pseudo) == 1) # only O in ecp
def test_cell_plus_imgs(self): numpy.random.seed(2) cl1 = pbcgto.M(a=numpy.random.random((3, 3)) * 3, mesh=[3] * 3, atom='''He .1 .0 .0''', basis='ccpvdz') self.assertTrue(numpy.all(cl1.nimgs == numpy.array([7, 14, 10]))) cl2 = tools.cell_plus_imgs(cl1, [3, 4, 5]) self.assertAlmostEqual(lib.finger(cl2.atom_coords()), 4.791699273649499, 9)
def test_cell_plus_imgs(self): numpy.random.seed(2) cl1 = pbcgto.M(a = numpy.random.random((3,3))*3, mesh = [3]*3, atom ='''He .1 .0 .0''', basis = 'ccpvdz') self.assertTrue(numpy.all(cl1.nimgs == numpy.array([8, 15, 11]))) cl2 = tools.cell_plus_imgs(cl1, [3,4,5]) self.assertAlmostEqual(lib.fp(cl2.atom_coords()), 4.791699273649499, 9) self.assertAlmostEqual(lib.fp(cl2._bas[:,gto.ATOM_OF]), -681.993543446207, 9)
def setUp(self): self.cell = pbcgto.M( unit = 'Angstrom', atom = list(zip(['B','N'],self.atomic_coordinates_cartesian_angstrom)), a = self.unit_cell_angstrom, basis = 'gth-szv', pseudo = 'gth-lda', gs = [16,16,75], verbose = 4, )
def test_get_lattice_Ls(self): numpy.random.seed(2) cl1 = pbcgto.M(a = numpy.random.random((3,3))*3, mesh = [3]*3, atom ='''He .1 .0 .0''', basis = 'ccpvdz') Ls = tools.get_lattice_Ls(cl1) self.assertEqual(Ls.shape, (1725,3)) Ls = tools.get_lattice_Ls(cl1, rcut=0) self.assertEqual(Ls.shape, (1,3))
def generate_test_inputs(): import pyqmc from pyqmc.coord import PeriodicConfigs from pyscf.pbc import gto, scf from pyscf.pbc.dft.multigrid import multigrid from pyscf.pbc import tools from pyscf import lib from_chkfile = True if from_chkfile: def loadchkfile(chkfile): cell = gto.cell.loads(lib.chkfile.load(chkfile, "mol")) kpts = cell.make_kpts([1, 1, 1]) mf = scf.KRKS(cell, kpts) mf.__dict__.update(lib.chkfile.load(chkfile, "scf")) return cell, mf cell1, mf1 = loadchkfile("mf1.chkfile") cell2, mf2 = loadchkfile("mf2.chkfile") else: L = 4 cell2 = gto.M( atom="""H {0} {0} {0} H {1} {1} {1}""".format(0.0, L * 0.25), basis="sto-3g", a=np.eye(3) * L, spin=0, unit="bohr", ) print("Primitive cell") kpts = cell2.make_kpts((2, 2, 2)) mf2 = scf.KRKS(cell2, kpts) mf2.xc = "pbe" mf2.chkfile = "mf2.chkfile" mf2 = mf2.run() print("Supercell") cell1 = tools.super_cell(cell2, [2, 2, 2]) kpts = [[0, 0, 0]] mf1 = scf.KRKS(cell1, kpts) mf1.xc = "pbe" mf1.chkfile = "mf1.chkfile" mf1 = mf1.run() # wf1 = pyqmc.PySCFSlaterUHF(cell1, mf1) wf1 = PySCFSlaterPBC(cell1, mf1, supercell=1 * np.eye(3)) wf2 = PySCFSlaterPBC(cell2, mf2, supercell=2 * np.eye(3)) configs = pyqmc.initial_guess(cell1, 10, 0.1) return wf1, wf2, configs
def test_RKS(kind=0, nk=(1, 1, 1)): L = 2 mol = gto.M( atom='''He {0} {0} {0}'''.format(0.0), basis='sto-3g', a=np.eye(3) * L, unit='bohr', ) kpts = mol.make_kpts(nk) mf = scf.KRKS(mol, kpts) mf.xc = "pbe" #mf = mf.density_fit() mf = mf.run() runtest(mol, mf, kind)
def test_RKS(kind=1, nk=(2, 2, 2)): L = 2 mol = gto.M( atom="""He {0} {0} {0}""".format(0.0), basis="sto-3g", a=np.eye(3) * L, unit="bohr", ) kpts = mol.make_kpts(nk) mf = scf.KRKS(mol, kpts) mf.xc = "pbe" # mf = mf.density_fit() mf = mf.run() runtest(mol, mf, kind=kind)
def __prepare__(self, kpts=[1, 1, 1], xc="lda", **kwargs): params = dict( unit='Angstrom', atom=list( zip(['B', 'N'], self.atomic_coordinates_cartesian_angstrom)), a=self.unit_cell_angstrom, basis='gth-szv', pseudo='gth-lda', gs=[16, 16, 75], verbose=0, ) params.update(kwargs) self.cell = pbcgto.M(**params) self.model = pbcdft.KRKS(self.cell, self.cell.make_kpts(kpts)) self.model.xc = xc
def test_pbc_nonorth_lda_rho_submesh(self): cell = gto.M(atom='H 2 3 4; H 3 4 3', basis=[[0, (2.2, 1)], [1, (1.9, 1)]], unit='B', mesh=[7, 6, 5], a=numpy.eye(3) * 8 + numpy.random.rand(3, 3)) grids = cell.get_uniform_grids() ao = cell.pbc_eval_gto('GTOval', grids) nao = cell.nao_nr() dm = numpy.random.random((nao, nao)) dm = dm + dm.T ref = numpy.einsum('gi,ij,gj->g', ao, dm, ao.conj()) ref = ref.reshape(cell.mesh)[1:6, 2:5, 2:4].ravel() out = eval_rho(cell, dm, offset=[1, 2, 2], submesh=[5, 3, 2]) self.assertAlmostEqual(abs(out - ref).max(), 0, 9)
def test_noncubic(kind=0, nk=(1, 1, 1)): L = 3 mol = gto.M( atom='''H {0} {0} {0} H {1} {1} {1}'''.format(0.0, L / 4), basis='sto-3g', a=(np.ones((3, 3)) - np.eye(3)) * L / 2, spin=0, unit='bohr', ) kpts = mol.make_kpts(nk) mf = scf.KUKS(mol, kpts) mf.xc = "pbe" #mf = mf.density_fit() mf = mf.run() runtest(mol, mf, kind)
def test_noncubic(kind=1, nk=(2, 2, 2)): L = 3 mol = gto.M( atom="""H {0} {0} {0} H {1} {1} {1}""".format(0.0, L / 4), basis="sto-3g", a=(np.ones((3, 3)) - np.eye(3)) * L / 2, spin=0, unit="bohr", ) kpts = mol.make_kpts(nk) mf = scf.KRKS(mol, kpts) mf.xc = "pbe" # mf = mf.density_fit() mf = mf.run() runtest(mol, mf, kind=kind)
def test_RKS(kind=0, nk=(1, 1, 1)): L = 2 mol = gto.M( atom="""He {0} {0} {0}""".format(0.0), basis="sto-3g", a=np.eye(3) * L, unit="bohr", ) kpts = mol.make_kpts(nk) mf = scf.KRKS(mol, kpts) mf.xc = "pbe" # mf = mf.density_fit() mf = mf.run() supercell = get_supercell(mol, np.diag(nk)) runtest(supercell, mf, kind=kind)
def noncubic(kind=0, nk=(1, 1, 1)): L = 3 mol = gto.M( atom="""H {0} {0} {0} H {1} {1} {1}""".format(0.0, L / 4), basis="sto-3g", a=(np.ones((3, 3)) - np.eye(3)) * L / 2, spin=0, unit="bohr", ) kpts = mol.make_kpts(nk) mf = scf.KUKS(mol, kpts) mf.xc = "pbe" # mf = mf.density_fit() mf = mf.run() supercell = get_supercell(mol, np.diag(nk)) runtest(supercell, mf, kind=kind)
def setUpModule(): global cell cell = gto.M(atom=''' C 4.826006352031 3.412501814582 8.358888185226 C 0.689429478862 0.487500259226 1.194126883604 ''', a=''' 4.136576868, 0.000000000, 2.388253772 1.378858962, 3.900002074, 2.388253772 0.000000000, 0.000000000, 4.776507525 ''', unit='B', precision=1e-14, basis='gth-tzv2p', pseudo='gth-lda', mesh=[15]*3, verbose=0)
def test_kuccsd_openshell(): cell = gto.M( unit='B', a=[[0., 6.74027466, 6.74027466], [6.74027466, 0., 6.74027466], [6.74027466, 6.74027466, 0.]], mesh=[13] * 3, atom='''H 0 0 0 H 1.68506866 1.68506866 1.68506866 H 3.37013733 3.37013733 3.37013733''', basis=[[0, (1., 1.)], [0, (.5, 1.)]], verbose=1, charge=0, spin=1, ) nmp = [3, 1, 1] # cell spin multiplied by nkpts cell.spin = cell.spin * 3 # treating 3*1*1 supercell at gamma point supcell = super_cell(cell, nmp) umf = scf.UHF(supcell, exxdiv=None) umf.conv_tol = 1e-11 ehf = umf.kernel() ucc = cc.UCCSD(umf) ucc.conv_tol = 1e-12 ecc, t1, t2 = ucc.kernel() print('UHF energy (supercell) %.9f \n' % (float(ehf) / 3.)) print('UCCSD correlation energy (supercell) %.9f \n' % (float(ecc) / 3.)) assert abs(ehf / 3 - -1.003789445) < 1e-7 assert abs(ecc / 3 - -0.029056542) < 1e-6 # kpts calculations kpts = cell.make_kpts(nmp) kpts -= kpts[0] kmf = scf.KUHF(cell, kpts, exxdiv=None) kmf.conv_tol = 1e-11 ehf = kmf.kernel() kcc = cc.KUCCSD(kmf) kcc.conv_tol = 1e-12 ecc, t1, t2 = kcc.kernel() print('UHF energy (kpts) %.9f \n' % (float(ehf))) print('UCCSD correlation energy (kpts) %.9f \n' % (float(ecc))) assert abs(ehf - -1.003789445) < 1e-7 assert abs(ecc - -0.029056542) < 1e-6
def test_getattr(self): from pyscf.pbc import scf, dft, cc, tdscf cell = pgto.M(atom='He', a=np.eye(3) * 4, basis={'He': [[0, (1, 1)]]}) self.assertEqual(cell.HF().__class__, scf.HF(cell).__class__) self.assertEqual(cell.KS().__class__, dft.KS(cell).__class__) self.assertEqual(cell.UKS().__class__, dft.UKS(cell).__class__) self.assertEqual(cell.KROHF().__class__, scf.KROHF(cell).__class__) self.assertEqual(cell.KKS().__class__, dft.KKS(cell).__class__) self.assertEqual(cell.CCSD().__class__, cc.ccsd.RCCSD) self.assertEqual(cell.TDA().__class__, tdscf.rhf.TDA) self.assertEqual(cell.TDBP86().__class__, tdscf.rks.TDDFTNoHybrid) self.assertEqual(cell.TDB3LYP().__class__, tdscf.rks.TDDFT) self.assertEqual(cell.KCCSD().__class__, cc.kccsd_rhf.KRCCSD) self.assertEqual(cell.KTDA().__class__, tdscf.krhf.TDA) self.assertEqual(cell.KTDBP86().__class__, tdscf.krks.TDDFTNoHybrid) self.assertRaises(AttributeError, lambda: cell.xyz) self.assertRaises(AttributeError, lambda: cell.TDxyz)
def test_cell_with_cart(self): cell = pgto.M( atom='Li 0 0 0; H 2 2 2', a=(numpy.ones([3, 3]) - numpy.eye(3)) * 2, cart=True, basis={'H': ''' H S 0.5 1''', 'Li': ''' Li S 0.8 1 0.4 1 Li P 0.8 1 0.4 1'''}) eri0 = df.FFTDF(cell).get_eri() eri1 = mdf.MDF(cell).set(auxbasis=df.aug_etb(cell)).get_eri() self.assertAlmostEqual(abs(eri1-eri0).max(), 0, 5)
def test_ewald_2d(self): cell = pgto.Cell() cell.a = numpy.eye(3) * 4 cell.atom = 'He 0 0 0; He 0 1 1' cell.unit = 'B' cell.mesh = [9, 9, 60] cell.verbose = 0 cell.dimension = 2 cell.rcut = 3.6 cell.build() self.assertAlmostEqual(cell.ewald(), -5.1194779101355596, 9) a = numpy.eye(3) * 3 a[0, 1] = .2 c = pgto.M(atom='H 0 0.1 0; H 1.1 2.0 0; He 1.2 .3 0.2', a=a, dimension=2, verbose=0) self.assertAlmostEqual(c.ewald(), -3.0902098018260418, 9)
def test_quad_nuc(self): np.random.seed(2) cell = gto.M(atom='''H .0 0 0 He 0 0.1 1 He 1 0.1 1 H 0 1.1 1 ''', a = np.eye(3)*2 + np.random.rand(3,3)*.1, basis = [[0, (1, 1)]], unit='B') def deriv1(cell, d1, atm_id=0): cell._env[cell._atm[atm_id,PTR_COORD]+d1] += .0001 e1 = cell.energy_nuc() cell._env[cell._atm[atm_id,PTR_COORD]+d1] -= .0002 e2 = cell.energy_nuc() cell._env[cell._atm[atm_id,PTR_COORD]+d1] += .0001 de = (e1 - e2) / .0002 return de def deriv2(cell, d1, atm_id=0): cell._env[cell._atm[atm_id,PTR_COORD]+d1] += .0001 e1 = ewald_deriv1(cell, atm_id) cell._env[cell._atm[atm_id,PTR_COORD]+d1] -= .0002 e2 = ewald_deriv1(cell, atm_id) cell._env[cell._atm[atm_id,PTR_COORD]+d1] += .0001 de = (e1 - e2) / .0002 return de for at in range(4): g0 = ewald_deriv1(cell, at) g1 = (deriv1(cell, 0, at), deriv1(cell, 1, at), deriv1(cell, 2, at)) self.assertAlmostEqual(abs(g0-g1).max(), 0, 7) for at in range(4): h0 = ewald_deriv2(cell, at) h1 = (deriv2(cell, 0, at), deriv2(cell, 1, at), deriv2(cell, 2, at)) self.assertAlmostEqual(abs(g0-g1).max(), 0, 6)