def test_orth_uks_gga_hermi0(self): xc = 'b88,' mydf = df.FFTDF(cell_orth) ni = dft.numint.NumInt() n, exc0, ref = ni.nr_uks(cell_orth, mydf.grids, xc, dm1, 0) ref += vj_uks_orth[0] + vj_uks_orth[1] mydf = multigrid.MultiGridFFTDF(cell_orth) n, exc1, vxc = multigrid.nr_uks(mydf, xc, dm1, hermi=0, with_j=True) self.assertAlmostEqual(float(abs(ref-vxc).max()), 0, 8) self.assertAlmostEqual(abs(exc0-exc1).max(), 0, 7)
def test_orth_uks_gga_hermi0(self): xc = 'b88,' mydf = df.FFTDF(cell_orth) ni = dft.numint.NumInt() n, exc0, ref = ni.nr_uks(cell_orth, mydf.grids, xc, dm1, 0) ref += vj_uks_orth[0] + vj_uks_orth[1] mydf = multigrid.MultiGridFFTDF(cell_orth) n, exc1, vxc = multigrid.nr_uks(mydf, xc, dm1, hermi=0, with_j=True) self.assertAlmostEqual(float(abs(ref - vxc).max()), 0, 8) self.assertAlmostEqual(abs(exc0 - exc1).max(), 0, 8)
def get_veff(ks, cell=None, dm=None, dm_last=0, vhf_last=0, hermi=1, kpt=None, kpts_band=None): '''Coulomb + XC functional for UKS. See pyscf/pbc/dft/uks.py :func:`get_veff` fore more details. ''' if cell is None: cell = ks.cell if dm is None: dm = ks.make_rdm1() if kpt is None: kpt = ks.kpt t0 = (time.clock(), time.time()) omega, alpha, hyb = ks._numint.rsh_and_hybrid_coeff(ks.xc, spin=cell.spin) if abs(omega) > 1e-10: raise NotImplementedError hybrid = abs(hyb) > 1e-10 if not hybrid and isinstance(ks.with_df, multigrid.MultiGridFFTDF): n, exc, vxc = multigrid.nr_uks(ks.with_df, ks.xc, dm, hermi, kpt.reshape(1, 3), kpts_band, with_j=True, return_j=False) logger.debug(ks, 'nelec by numeric integration = %s', n) t0 = logger.timer(ks, 'vxc', *t0) return vxc # ndim = 3 : dm.shape = ([alpha,beta], nao, nao) ground_state = (dm.ndim == 3 and dm.shape[0] == 2 and kpts_band is None) if ks.grids.non0tab is None: ks.grids.build(with_non0tab=True) if (isinstance(ks.grids, gen_grid.BeckeGrids) and ks.small_rho_cutoff > 1e-20 and ground_state): ks.grids = rks.prune_small_rho_grids_(ks, cell, dm, ks.grids, kpt) t0 = logger.timer(ks, 'setting up grids', *t0) if not isinstance(dm, numpy.ndarray): dm = numpy.asarray(dm) if dm.ndim == 2: # RHF DM dm = numpy.asarray((dm * .5, dm * .5)) if hermi == 2: # because rho = 0 n, exc, vxc = (0, 0), 0, 0 else: n, exc, vxc = ks._numint.nr_uks(cell, ks.grids, ks.xc, dm, 0, kpt, kpts_band) logger.debug(ks, 'nelec by numeric integration = %s', n) t0 = logger.timer(ks, 'vxc', *t0) if not hybrid: vj = ks.get_j(cell, dm[0] + dm[1], hermi, kpt, kpts_band) vxc += vj else: if getattr(ks.with_df, '_j_only', False): # for GDF and MDF ks.with_df._j_only = False vj, vk = ks.get_jk(cell, dm, hermi, kpt, kpts_band) vj = vj[0] + vj[1] vxc += vj - vk * hyb if ground_state: exc -= (numpy.einsum('ij,ji', dm[0], vk[0]) + numpy.einsum('ij,ji', dm[1], vk[1])).real * hyb * .5 if ground_state: ecoul = numpy.einsum('ij,ji', dm[0] + dm[1], vj).real * .5 else: ecoul = None vxc = lib.tag_array(vxc, ecoul=ecoul, exc=exc, vj=None, vk=None) return vxc
def get_veff(ks, cell=None, dm=None, dm_last=0, vhf_last=0, hermi=1, kpts=None, kpts_band=None): '''Coulomb + XC functional for UKS. See pyscf/pbc/dft/uks.py :func:`get_veff` fore more details. ''' if cell is None: cell = ks.cell if dm is None: dm = ks.make_rdm1() if kpts is None: kpts = ks.kpts t0 = (logger.process_clock(), logger.perf_counter()) omega, alpha, hyb = ks._numint.rsh_and_hybrid_coeff(ks.xc, spin=cell.spin) hybrid = abs(hyb) > 1e-10 if not hybrid and isinstance(ks.with_df, multigrid.MultiGridFFTDF): n, exc, vxc = multigrid.nr_uks(ks.with_df, ks.xc, dm, hermi, kpts, kpts_band, with_j=True, return_j=False) logger.debug(ks, 'nelec by numeric integration = %s', n) t0 = logger.timer(ks, 'vxc', *t0) return vxc # ndim = 4 : dm.shape = ([alpha,beta], nkpts, nao, nao) ground_state = (dm.ndim == 4 and dm.shape[0] == 2 and kpts_band is None) if ks.grids.non0tab is None: ks.grids.build(with_non0tab=True) if (isinstance(ks.grids, gen_grid.BeckeGrids) and ks.small_rho_cutoff > 1e-20 and ground_state): ks.grids = rks.prune_small_rho_grids_(ks, cell, dm, ks.grids, kpts) t0 = logger.timer(ks, 'setting up grids', *t0) if hermi == 2: # because rho = 0 n, exc, vxc = (0, 0), 0, 0 else: n, exc, vxc = ks._numint.nr_uks(cell, ks.grids, ks.xc, dm, 0, kpts, kpts_band) logger.debug(ks, 'nelec by numeric integration = %s', n) t0 = logger.timer(ks, 'vxc', *t0) weight = 1. / len(kpts) if not hybrid: vj = ks.get_j(cell, dm[0] + dm[1], hermi, kpts, kpts_band) vxc += vj else: if getattr(ks.with_df, '_j_only', False): # for GDF and MDF ks.with_df._j_only = False vj, vk = ks.get_jk(cell, dm, hermi, kpts, kpts_band) vj = vj[0] + vj[1] vk *= hyb if abs(omega) > 1e-10: vklr = ks.get_k(cell, dm, hermi, kpts, kpts_band, omega=omega) vklr *= (alpha - hyb) vk += vklr vxc += vj - vk if ground_state: exc -= (np.einsum('Kij,Kji', dm[0], vk[0]) + np.einsum('Kij,Kji', dm[1], vk[1])).real * .5 * weight if ground_state: ecoul = np.einsum('Kij,Kji', dm[0] + dm[1], vj).real * .5 * weight else: ecoul = None vxc = lib.tag_array(vxc, ecoul=ecoul, exc=exc, vj=None, vk=None) return vxc