def get_veff(ks_grad, mol=None, dm=None): '''Coulomb + XC functional ''' if mol is None: mol = ks_grad.mol if dm is None: dm = ks_grad.base.make_rdm1() t0 = (time.clock(), time.time()) mf = ks_grad.base ni = mf._numint if ks_grad.grids is not None: grids = ks_grad.grids else: grids = mf.grids if grids.coords is None: grids.build(with_non0tab=True) if mf.nlc != '': raise NotImplementedError #enabling range-separated hybrids omega, alpha, hyb = ni.rsh_and_hybrid_coeff(mf.xc, spin=mol.spin) mem_now = lib.current_memory()[0] max_memory = max(2000, ks_grad.max_memory*.9-mem_now) if ks_grad.grid_response: exc, vxc = uks_grad.get_vxc_full_response( ni, mol, grids, mf.xc, dm, max_memory=max_memory, verbose=ks_grad.verbose) logger.debug1(ks_grad, 'sum(grids response) %s', exc.sum(axis=0)) else: exc, vxc = uks_grad.get_vxc( ni, mol, grids, mf.xc, dm, max_memory=max_memory, verbose=ks_grad.verbose) t0 = logger.timer(ks_grad, 'vxc', *t0) if abs(hyb) < 1e-10: vj = ks_grad.get_j(mol, dm) vxc += vj[0] + vj[1] if ks_grad.auxbasis_response: e1_aux = vj.aux else: vj, vk = ks_grad.get_jk(mol, dm) if ks_grad.auxbasis_response: vk_aux = vk.aux * hyb vk *= hyb if abs(omega) > 1e-10: # For range separated Coulomb operator raise NotImplementedError vk_lr = ks_grad.get_k(mol, dm, omega=omega) vk += vk_lr * (alpha - hyb) if ks_grad.auxbasis_response: vk_aux += vk_lr.aux * (alpha - hyb) vxc += vj[0] + vj[1] - vk if ks_grad.auxbasis_response: e1_aux = vj.aux - vk_aux if ks_grad.auxbasis_response: logger.debug1(ks_grad, 'sum(auxbasis response) %s', e1_aux.sum(axis=0)) vxc = lib.tag_array(vxc, exc1_grid=exc, aux=e1_aux) else: vxc = lib.tag_array(vxc, exc1_grid=exc) return vxc
def test_get_vxc(self): mol = gto.Mole() mol.verbose = 0 mol.atom = [['O', (0., 0., 0.)], [1, (0., -0.757, 0.587)], [1, (0., 0.757, 0.587)]] mol.basis = '631g' mol.charge = -1 mol.spin = 1 mol.build() mf = dft.UKS(mol) mf.xc = 'b3lyp' mf.conv_tol = 1e-12 e0 = mf.scf() g = uks.Gradients(mf) g.grid_response = True g0 = g.kernel() dm0 = mf.make_rdm1() denom = 1 / .00001 * lib.param.BOHR mol1 = gto.Mole() mol1.verbose = 0 mol1.atom = [['O', (0., 0., 0.00001)], [1, (0., -0.757, 0.587)], [1, (0., 0.757, 0.587)]] mol1.basis = '631g' mol1.charge = -1 mol1.spin = 1 mol1.build() mf1 = dft.UKS(mol1) mf1.xc = 'b3lyp' mf1.conv_tol = 1e-12 e1 = mf1.scf() self.assertAlmostEqual((e1 - e0) * denom, g0[0, 2], 3) grids0 = dft.gen_grid.Grids(mol) grids0.atom_grid = (20, 86) grids0.build(with_non0tab=False) grids1 = dft.gen_grid.Grids(mol1) grids1.atom_grid = (20, 86) grids1.build(with_non0tab=False) exc0 = dft.numint.nr_uks(mf._numint, mol, grids0, mf.xc, dm0)[1] exc1 = dft.numint.nr_uks(mf1._numint, mol1, grids1, mf1.xc, dm0)[1] grids0_w = copy.copy(grids0) grids0_w.weights = grids1.weights grids0_c = copy.copy(grids0) grids0_c.coords = grids1.coords exc0_w = dft.numint.nr_uks(mf._numint, mol, grids0_w, mf.xc, dm0)[1] exc0_c = dft.numint.nr_uks(mf._numint, mol1, grids0_c, mf.xc, dm0)[1] dexc_t = (exc1 - exc0) * denom dexc_c = (exc0_c - exc0) * denom dexc_w = (exc0_w - exc0) * denom self.assertAlmostEqual(dexc_t, dexc_c + dexc_w, 4) vxc = uks.get_vxc(mf._numint, mol, grids0, mf.xc, dm0)[1] ev1, vxc1 = uks.get_vxc_full_response(mf._numint, mol, grids0, mf.xc, dm0) p0, p1 = mol.aoslice_by_atom()[0][2:] exc1_approx = numpy.einsum('sxij,sij->x', vxc[:, :, p0:p1], dm0[:, p0:p1]) * 2 exc1_full = numpy.einsum('sxij,sij->x', vxc1[:, :, p0:p1], dm0[:, p0:p1]) * 2 + ev1[0] self.assertAlmostEqual(dexc_t, exc1_approx[2], 3) self.assertAlmostEqual(dexc_t, exc1_full[2], 5)
def test_get_vxc(self): mol = gto.Mole() mol.verbose = 0 mol.atom = [ ['O' , (0. , 0. , 0.)], [1 , (0. , -0.757 , 0.587)], [1 , (0. , 0.757 , 0.587)] ] mol.basis = '631g' mol.charge = -1 mol.spin = 1 mol.build() mf = dft.UKS(mol) mf.xc = 'b3lyp' mf.conv_tol = 1e-12 e0 = mf.scf() g = uks.Gradients(mf) g.grid_response = True g0 = g.kernel() dm0 = mf.make_rdm1() denom = 1/.00001 * lib.param.BOHR mol1 = gto.Mole() mol1.verbose = 0 mol1.atom = [ ['O' , (0. , 0. , 0.00001)], [1 , (0. , -0.757 , 0.587)], [1 , (0. , 0.757 , 0.587)] ] mol1.basis = '631g' mol1.charge = -1 mol1.spin = 1 mol1.build() mf1 = dft.UKS(mol1) mf1.xc = 'b3lyp' mf1.conv_tol = 1e-12 e1 = mf1.scf() self.assertAlmostEqual((e1-e0)*denom, g0[0,2], 3) grids0 = dft.gen_grid.Grids(mol) grids0.atom_grid = (20,86) grids0.build(with_non0tab=False) grids1 = dft.gen_grid.Grids(mol1) grids1.atom_grid = (20,86) grids1.build(with_non0tab=False) exc0 = dft.numint.nr_uks(mf._numint, mol, grids0, mf.xc, dm0)[1] exc1 = dft.numint.nr_uks(mf1._numint, mol1, grids1, mf1.xc, dm0)[1] grids0_w = copy.copy(grids0) grids0_w.weights = grids1.weights grids0_c = copy.copy(grids0) grids0_c.coords = grids1.coords exc0_w = dft.numint.nr_uks(mf._numint, mol, grids0_w, mf.xc, dm0)[1] exc0_c = dft.numint.nr_uks(mf._numint, mol1, grids0_c, mf.xc, dm0)[1] dexc_t = (exc1 - exc0) * denom dexc_c = (exc0_c - exc0) * denom dexc_w = (exc0_w - exc0) * denom self.assertAlmostEqual(dexc_t, dexc_c+dexc_w, 4) vxc = uks.get_vxc(mf._numint, mol, grids0, mf.xc, dm0)[1] ev1, vxc1 = uks.get_vxc_full_response(mf._numint, mol, grids0, mf.xc, dm0) p0, p1 = mol.aoslice_by_atom()[0][2:] exc1_approx = numpy.einsum('sxij,sij->x', vxc[:,:,p0:p1], dm0[:,p0:p1])*2 exc1_full = numpy.einsum('sxij,sij->x', vxc1[:,:,p0:p1], dm0[:,p0:p1])*2 + ev1[0] self.assertAlmostEqual(dexc_t, exc1_approx[2], 3) self.assertAlmostEqual(dexc_t, exc1_full[2], 5)