def test_rsh_df(self): mf = pbcdft.KRKS(cell) mf.xc = 'camb3lyp' mf.kernel() self.assertAlmostEqual(mf.e_tot, -2.3032261128220544, 7) mf = pbcdft.KRKS(cell).density_fit() mf.xc = 'camb3lyp' mf.omega = .15 mf.kernel() self.assertAlmostEqual(mf.e_tot, -2.3987656490734555, 7)
def test_kpt_vs_supercell(self): ngs = 5 nk = (3, 1, 1) # Comparison is only perfect for odd-numbered supercells and kpt sampling assert all(np.array(nk) % 2 == np.array([1, 1, 1])) cell = make_primitive_cell(ngs) scaled_kpts = ase.dft.kpoints.monkhorst_pack(nk) abs_kpts = cell.get_abs_kpts(scaled_kpts) kmf = pbcdft.KRKS(cell, abs_kpts) kmf.xc = 'lda,vwn' #kmf.analytic_int = False #kmf.verbose = 7 ekpt = kmf.scf() supcell = pyscf.pbc.tools.super_cell(cell, nk) supcell.gs = np.array([ nk[0] * ngs + (nk[0] - 1) // 2, nk[1] * ngs + (nk[1] - 1) // 2, nk[2] * ngs + (nk[2] - 1) // 2 ]) #supcell.verbose = 7 supcell.build() mf = pbcdft.RKS(supcell) mf.xc = 'lda,vwn' #mf.verbose = 7 esup = mf.scf() / np.prod(nk) #print("kpt sampling energy =", ekpt) #print("supercell energy =", esup) self.assertAlmostEqual(ekpt, esup, 5)
def test_gwcd_high_cost(self): kpts = cell.make_kpts([3,1,1],scaled_center=[0,0,0]) gdf = df.GDF(cell, kpts) gdf._cderi = gdf_fname = os.path.realpath(os.path.join(__file__, '..', 'gdf_ints_311.h5')) if 1: gdf._cderi_to_save = gdf_fname gdf.build() kmf = dft.KRKS(cell, kpts).density_fit(with_df=gdf) kmf.xc = 'pbe' kmf.kernel() gw = krgw_cd.KRGWCD(kmf) gw.linearized = False # without finite size corrections gw.fc = False nocc = gw.nocc gw.kernel(kptlist=[0,1,2],orbs=range(0,nocc+3)) self.assertAlmostEqual(gw.mo_energy[0][nocc-1], 0.62045796, 4) self.assertAlmostEqual(gw.mo_energy[0][nocc], 0.96574426, 4) self.assertAlmostEqual(gw.mo_energy[1][nocc-1], 0.52639129, 4) self.assertAlmostEqual(gw.mo_energy[1][nocc], 1.07442235, 4) # with finite size corrections gw.fc = True gw.kernel(kptlist=[0,1,2],orbs=range(0,nocc+3)) self.assertAlmostEqual(gw.mo_energy[0][nocc-1], 0.5427707 , 4) self.assertAlmostEqual(gw.mo_energy[0][nocc], 0.80148557, 4) self.assertAlmostEqual(gw.mo_energy[1][nocc-1], 0.45073751, 4) self.assertAlmostEqual(gw.mo_energy[1][nocc], 0.92910117, 4)
def test_nr_krks_lda(self): mf = dft.KRKS(cell, cell.make_kpts([2, 1, 1])) mf.xc = 'lda,' mf = scf.newton(mf) mf.conv_tol_grad = 1e-4 mf.kernel() self.assertAlmostEqual(mf.e_tot, -10.307756038726733, 8)
def setUp(self): cell = gto.Cell() alat0 = 3.6 cell.a = (numpy.ones((3, 3)) - numpy.eye(3)) * alat0 / 2.0 cell.atom = (('C', 0, 0, 0), ('C', numpy.array([0.25, 0.25, 0.25]) * alat0)) cell.basis = 'gth-szv' cell.pseudo = 'gth-pade' cell.mesh = [ 25 ] * 3 # 10 grids on postive x direction, => 21^3 grids in total cell.verbose = 0 cell.build() self.cell = cell nk = [2, 2, 2] kpts = cell.make_kpts(nk) mf = dft.KRKS(cell, kpts=kpts) mf.chkfile = 'scf.dump' ehf = mf.kernel() self.mf = mf self.nmo_pk = numpy.array([C.shape[-1] for C in mf.mo_coeff]) self.nkpts = len(kpts) self.kpts = kpts self.nmo_max = numpy.max(self.nmo_pk)
def test_nr_krks_gga(self): mf = dft.KRKS(cell, cell.make_kpts([2, 1, 1])) mf.xc = 'b88,' mf = scf.newton(mf) mf.conv_tol_grad = 1e-4 mf.kernel() self.assertAlmostEqual(mf.e_tot, -10.446717855794008, 8)
def test_multigrid_krks(self): mf = dft.KRKS(cell_he) mf.xc = 'lda,' ref = mf.get_veff(cell_he, dm_he, kpts=kpts) out = multigrid.multigrid(mf).get_veff(cell_he, dm_he, kpts=kpts) self.assertAlmostEqual(float(abs(ref - out).max()), 0, 9) self.assertAlmostEqual(abs(ref.exc - out.exc).max(), 0, 9) self.assertAlmostEqual(abs(ref.ecoul - out.ecoul).max(), 0, 9)
def test_klda8_primitive_kpt_222(self): cell = make_primitive_cell([17]*3) abs_kpts = cell.make_kpts([2]*3, with_gamma_point=False) mf = pbcdft.KRKS(cell, abs_kpts) #mf.analytic_int = False mf.xc = 'lda,vwn' #mf.verbose = 7 e1 = mf.scf() self.assertAlmostEqual(e1, -11.353643583707452, 8)
def xtest_kpt_222(self): cell = make_primitive_cell(8) abs_kpts = cell.make_kpts([2, 2, 2], wrap_around=True) kmf = pbcdft.KRKS(cell, abs_kpts) kmf.xc = 'lda,vwn' #kmf.analytic_int = False #kmf.verbose = 7 e1 = kmf.scf() self.assertAlmostEqual(e1, -11.3536435234900, 8)
def test_klda8_cubic_kpt_222(self): cell = build_cell([17]*3) abs_kpts = cell.make_kpts([2]*3, with_gamma_point=False) mf = pbcdft.KRKS(cell, abs_kpts) #mf.analytic_int = False mf.xc = 'lda,vwn' #mf.verbose = 7 e1 = mf.scf() self.assertAlmostEqual(e1, -45.425834895129569, 8)
def test_gamma_vs_ks(self): mf = pdft.KRKS(cell) mf.kpts = cell.make_kpts([1, 1, 3]) ek = mf.kernel() scell = ptools.super_cell(cell, [1, 1, 3]) scell.gs = [12, 12, 36] mf = pdft.RKS(scell) eg = mf.kernel() self.assertAlmostEqual(ek, eg / 3, 5)
def test_band_kscf(self): kpts = cell.make_kpts([2, 1, 1]) kmf = dft.KRKS(cell, kpts=kpts).run() bands = [] h1 = kmf.get_hcore() s1 = kmf.get_ovlp() for i, kpt in enumerate(kpts): fock = h1[i] + kmf.get_veff(kpt_band=kpt) bands.append(scipy.linalg.eigh(fock, s1[i])[0]) self.assertAlmostEqual(finger(bands), -0.76745129086774599, 8)
def test_gamma_vs_ks_high_cost(self): mf = pdft.KRKS(cell) mf.kpts = cell.make_kpts([1, 1, 3]) ek = mf.kernel() scell = ptools.super_cell(cell, [1, 1, 3]) scell.mesh = [25, 25, 73] mf = pdft.RKS(scell) eg = mf.kernel() self.assertAlmostEqual(ek, eg / 3, 5)
def xtest_kpt_222(self): cell = make_primitive_cell(8) scaled_kpts = ase.dft.kpoints.monkhorst_pack((2, 2, 2)) abs_kpts = cell.get_abs_kpts(scaled_kpts) kmf = pbcdft.KRKS(cell, abs_kpts) kmf.xc = 'lda,vwn' #kmf.analytic_int = False #kmf.verbose = 7 e1 = kmf.scf() self.assertAlmostEqual(e1, -11.3536435234900, 8)
def test_bands(self): model = pbcdft.KRKS(self.cell, self.cell.make_kpts([3,3,1])) model.xc = 'lda' model.kernel() e,w = model.get_bands(self.k_points_cartesian_bohr) avg = numpy.mean(e-self.bands_hartree) delta = e-self.bands_hartree-avg dev_max = numpy.abs(delta).max() print "Maximum deviation:", dev_max, " Hartree" assert dev_max < 1e-4
def test_klda8_primitive_kpt_222(self): ase_atom = bulk('C', 'diamond', a=LATTICE_CONST) scaled_kpts = ase.dft.kpoints.monkhorst_pack((2, 2, 2)) cell = build_cell(ase_atom, 8) abs_kpts = cell.get_abs_kpts(scaled_kpts) mf = pbcdft.KRKS(cell, abs_kpts) #mf.analytic_int = False mf.xc = 'lda,vwn' #mf.verbose = 7 e1 = mf.scf() #print "mf._ecoul =", mf._ecoul #print "mf._exc =", mf._exc self.assertAlmostEqual(e1, -11.353643738291005, 8)
def test_klda8_cubic_kpt_222(self): ase_atom = Diamond(symbol='C', latticeconstant=LATTICE_CONST) scaled_kpts = ase.dft.kpoints.monkhorst_pack((2, 2, 2)) cell = build_cell(ase_atom, 8) abs_kpts = cell.get_abs_kpts(scaled_kpts) mf = pbcdft.KRKS(cell, abs_kpts) #mf.analytic_int = False mf.xc = 'lda,vwn' #mf.verbose = 7 e1 = mf.scf() #print "mf._ecoul =", mf._ecoul #print "mf._exc =", mf._exc self.assertAlmostEqual(e1, -45.425834895129569, 8)
def run_kdft(cell, nmp=[1, 1, 1], gamma=False): """Run a k-point-sampling DFT (LDA) calculation.""" scaled_kpts = ase.dft.kpoints.monkhorst_pack(nmp) if gamma: for i in range(3): if nmp[i] % 2 == 0: scaled_kpts[:, i] += 0.5 / nmp[i] abs_kpts = cell.get_abs_kpts(scaled_kpts) kmf = pbcdft.KRKS(cell, abs_kpts) kmf.xc = 'lda,vwn' kmf.verbose = 7 print kmf.scf() return kmf
def test_rks_gen_g_hop(self): mf = dft.KRKS(cell, cell.make_kpts([2,1,1])) mf.xc = 'b3lyp' nao = cell.nao_nr() numpy.random.seed(1) mo = numpy.random.random((2,nao,nao)) + 0j mo_occ = numpy.zeros((2,nao)) mo_occ[:,:5] = 2 nocc, nvir = 5, nao-5 dm1 = numpy.random.random(2*nvir*nocc) + .1j mf = scf.newton(mf) mf.grids.build() g, hop, hdiag = mf.gen_g_hop(mo, mo_occ, mf.get_hcore()) self.assertAlmostEqual(numpy.linalg.norm(hop(dm1)), 37.967972033738519, 7)
def test_band_kscf(self): kpts = cell.make_kpts([2,1,1]) kmf = dft.KRKS(cell, kpts=kpts).run() np.random.seed(11) kpts_band = np.random.random((4,3)) bands = kmf.get_bands(kpts_band)[0] bands_ref = [] h1 = kmf.get_hcore(kpts=kpts_band) s1 = kmf.get_ovlp(kpts=kpts_band) vhf = kmf.get_veff(kpts_band=kpts_band) for i, kpt in enumerate(kpts_band): fock = h1[i] + vhf[i] bands_ref.append(scipy.linalg.eigh(fock, s1[i])[0]) self.assertAlmostEqual(abs(np.array(bands_ref) - np.array(bands)).max(), 0, 9) self.assertAlmostEqual(lib.fp(bands), -0.61562245312227049, 8)
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 copy_mf(mf, cell): if mf.__class__.__name__ == 'KRHF': mf1 = scf.KRHF(cell) elif mf.__class__.__name__ == 'KUHF': mf1 = scf.KUHF(cell) elif mf.__class__.__name__ == 'KRKS': mf1 = dft.KRKS(cell) mf1.xc = mf.xc elif mf.__class__.__name__ == 'KUKS': mf1 = dft.KUKS(cell) mf1.xc = mf.xc mf1.kpts = mf.kpts mf1.exxdiv = getattr(mf, 'exxdiv', None) mf1.conv_tol = mf.conv_tol mf1.conv_tol_grad = mf.conv_tol_grad return mf1
def test_ccsd_t_non_hf_frozen(self): '''Tests ccsd and ccsd_t for non-Hartree-Fock references with frozen orbitals using supercell vs k-point calculation.''' n = 14 cell = make_test_cell.test_cell_n3([n] * 3) #import sys #cell.stdout = sys.stdout #cell.verbose = 7 nk = [2, 1, 1] kpts = cell.make_kpts(nk) kpts -= kpts[0] kks = pbcdft.KRKS(cell, kpts=kpts) ekks = kks.kernel() khf = pbcscf.KRHF(cell) khf.__dict__.update(kks.__dict__) mycc = pbcc.KRCCSD(khf, frozen=1) eris = mycc.ao2mo() ekcc, t1, t2 = mycc.kernel(eris=eris) ekcc_t = mycc.ccsd_t(eris=eris) # Run supercell from pyscf.pbc.tools.pbc import super_cell supcell = super_cell(cell, nk) rks = pbcdft.RKS(supcell) erks = rks.kernel() rhf = pbcscf.RHF(supcell) rhf.__dict__.update(rks.__dict__) mycc = pbcc.RCCSD(rhf, frozen=2) eris = mycc.ao2mo() ercc, t1, t2 = mycc.kernel(eris=eris) self.assertAlmostEqual(ercc / np.prod(nk), -0.11467718013872311, 6) self.assertAlmostEqual(ercc / np.prod(nk), ekcc, 6) ercc_t = mycc.ccsd_t(eris=eris) self.assertAlmostEqual(ercc_t / np.prod(nk), -0.00066503872045200996, 6) self.assertAlmostEqual(ercc_t / np.prod(nk), ekcc_t, 6)
def test_gen_rhf_response(self): numpy.random.seed(9) dm1 = numpy.random.random(dm_he.shape) dm1 = dm1 + dm1.transpose(0, 2, 1) dm1[1] = dm1[0] mydf = df.FFTDF(cell_he) ni = dft.numint.KNumInt() mf = dft.KRKS(cell_he) mf.with_df = multigrid.MultiGridFFTDF(cell_he) mf.kpts = kpts mf.xc = 'lda,' ref = dft.numint.nr_rks_fxc(ni, cell_he, mydf.grids, mf.xc, dm_he, dm1, hermi=1, kpts=kpts) vj = mydf.get_jk(dm1, with_k=False, kpts=kpts)[0] ref += vj v = multigrid._gen_rhf_response(mf, dm_he, hermi=1)(dm1) self.assertEqual(ref.dtype, v.dtype) self.assertEqual(ref.shape, v.shape) self.assertAlmostEqual(abs(v - ref).max(), 0, 9) mf.xc = 'b88,' ref = dft.numint.nr_rks_fxc(ni, cell_he, mydf.grids, mf.xc, dm_he, dm1, hermi=1, kpts=kpts) ref += vj v = multigrid._gen_rhf_response(mf, dm_he, hermi=1)(dm1) self.assertEqual(ref.dtype, v.dtype) self.assertEqual(ref.shape, v.shape) self.assertAlmostEqual(abs(v - ref).max(), 0, 6)
def test_write_wfn_pbc(self): cell = gto.Cell() alat0 = 3.6 cell.a = (numpy.ones((3,3))-numpy.eye(3))*alat0/2.0 cell.atom = (('C',0,0,0),('C',numpy.array([0.25,0.25,0.25])*alat0)) cell.basis = 'gth-szv' cell.pseudo = 'gth-pade' cell.mesh = [12]*3 # 10 grids on postive x direction, => 21^3 grids in total cell.verbose = 0 cell.build() nk = [2,2,1] kpts = cell.make_kpts(nk) mf = dft.KRKS(cell,kpts=kpts) mf.chkfile = 'scf.dump' emf = mf.kernel() nmo_pk = numpy.array([C.shape[-1] for C in mf.mo_coeff]) nkpts = len(kpts) kpts = kpts nmo_max = numpy.max(nmo_pk) hcore = mf.get_hcore() fock = hcore + mf.get_veff() scf_data = {'cell': cell, 'mo_coeff': mf.mo_coeff, 'Xocc': mf.mo_occ, 'X': mf.mo_coeff, 'fock': fock, 'isUHF': False, 'hcore': hcore, 'nmo_pk': nmo_pk, 'mo_energy': mf.mo_energy, 'kpts': kpts} with h5py.File('wfn.h5', 'w') as fh5: pass pbc.write_wfn_pbc(scf_data, True, 'wfn.h5', rediag=True) with h5py.File('wfn.h5', 'r') as fh5: dims = fh5['Wavefunction/NOMSD/dims'][:] print(dims) self.assertTrue(numpy.allclose(dims, [32,16,16,1,1]))
def test_lda_grad(self): mf = dft.KRKS(cell, kpts) mf.xc = 'lda,vwn' mf.conv_tol = 1e-10 mf.conv_tol_grad = 1e-6 g_scan = mf.nuc_grad_method().as_scanner() g = g_scan(cell)[1] self.assertAlmostEqual(lib.fp(g), -0.22166962318360375, 6) mfs = g_scan.base.as_scanner() e1 = mfs( [['C', [0.0, 0.0, 0.0]], [ 'C', [1.685068664391, 1.685068664391, 1.685068664391 + disp / 2.0] ]]) e2 = mfs( [['C', [0.0, 0.0, 0.0]], [ 'C', [1.685068664391, 1.685068664391, 1.685068664391 - disp / 2.0] ]]) self.assertAlmostEqual(g[1, 2], (e1 - e2) / disp, 6)
def test_ccsd_t_non_hf(self): '''Tests ccsd and ccsd_t for non-Hartree-Fock references using supercell vs k-point calculation.''' n = 14 cell = make_test_cell.test_cell_n3([n] * 3) nk = [2, 1, 1] kpts = cell.make_kpts(nk) kpts -= kpts[0] kks = pbcdft.KRKS(cell, kpts=kpts) ekks = kks.kernel() khf = pbcscf.KRHF(cell) khf.__dict__.update(kks.__dict__) mycc = pbcc.KRCCSD(khf) eris = mycc.ao2mo() ekcc, t1, t2 = mycc.kernel(eris=eris) ekcc_t = mycc.ccsd_t(eris=eris) # Run supercell from pyscf.pbc.tools.pbc import super_cell supcell = super_cell(cell, nk) rks = pbcdft.RKS(supcell) erks = rks.kernel() rhf = pbcscf.RHF(supcell) rhf.__dict__.update(rks.__dict__) mycc = pbcc.RCCSD(rhf) eris = mycc.ao2mo() ercc, t1, t2 = mycc.kernel(eris=eris) self.assertAlmostEqual(ercc / np.prod(nk), -0.15632445245405927, 6) self.assertAlmostEqual(ercc / np.prod(nk), ekcc, 6) ercc_t = mycc.ccsd_t(eris=eris) self.assertAlmostEqual(ercc_t / np.prod(nk), -0.00114619248449, 6) self.assertAlmostEqual(ercc_t / np.prod(nk), ekcc_t, 6)
def test_gga_grad(self): mf = dft.KRKS(cell, kpts) mf.xc = 'pbe,pbe' mf.conv_tol = 1e-10 mf.conv_tol_grad = 1e-6 g_scan = mf.nuc_grad_method().as_scanner() g = g_scan(cell)[1] self.assertAlmostEqual(lib.fp(g), -0.21844074846755882, 6) mfs = g_scan.base.as_scanner() e1 = mfs( [['C', [0.0, 0.0, 0.0]], [ 'C', [1.685068664391, 1.685068664391, 1.685068664391 + disp / 2.0] ]]) e2 = mfs( [['C', [0.0, 0.0, 0.0]], [ 'C', [1.685068664391, 1.685068664391, 1.685068664391 - disp / 2.0] ]]) self.assertAlmostEqual(g[1, 2], (e1 - e2) / disp, 6)
def test_kpt_vs_supercell(self): n = 11 nk = (3, 1, 1) # Comparison is only perfect for odd-numbered supercells and kpt sampling assert all(np.array(nk) % 2 == np.array([1, 1, 1])) cell = make_primitive_cell([n] * 3) abs_kpts = cell.make_kpts(nk, wrap_around=True) kmf = pbcdft.KRKS(cell, abs_kpts) kmf.xc = 'lda,vwn' #kmf.analytic_int = False #kmf.verbose = 7 ekpt = kmf.scf() supcell = pyscf.pbc.tools.super_cell(cell, nk) supcell.build() mf = pbcdft.RKS(supcell) mf.xc = 'lda,vwn' #mf.verbose = 7 esup = mf.scf() / np.prod(nk) #print("kpt sampling energy =", ekpt) #print("supercell energy =", esup) self.assertAlmostEqual(ekpt, esup, 5)
def test_hybrid_grad(self): mf = dft.KRKS(cell, kpts) mf.xc = 'b3lyp' mf.exxdiv = None mf.conv_tol = 1e-10 mf.conv_tol_grad = 1e-6 g_scan = mf.nuc_grad_method().as_scanner() g = g_scan(cell)[1] self.assertAlmostEqual(lib.fp(g), -0.19544969829285652, 6) mfs = g_scan.base.as_scanner() e1 = mfs( [['C', [0.0, 0.0, 0.0]], [ 'C', [1.685068664391, 1.685068664391, 1.685068664391 + disp / 2.0] ]]) e2 = mfs( [['C', [0.0, 0.0, 0.0]], [ 'C', [1.685068664391, 1.685068664391, 1.685068664391 - disp / 2.0] ]]) self.assertAlmostEqual(g[1, 2], (e1 - e2) / disp, 6)