def test_mgga_xc_unpolarized_against_libxc(self, xc_name, xc_fun): eps_xc_ref, rhograds_ref, _, _ = libxc.eval_xc(xc_name, self.rho_and_derivs, spin=0, relativity=0, deriv=1) vrho_ref, vsigma_ref, _, vtau_ref = rhograds_ref e_xc_ref = eps_xc_ref * self.rho e_xc = xc_fun(self.rho, self.sigma, self.tau) e_xc, grads = jax.vmap(jax.value_and_grad(xc_fun, argnums=(0, 1, 2)))(self.rho, self.sigma, self.tau) vrho, vsigma, vtau = grads np.testing.assert_allclose(np.sum(e_xc * self.weights), np.sum(e_xc_ref * self.weights), atol=1e-5) np.testing.assert_allclose(e_xc, e_xc_ref, rtol=1e-2, atol=1.e-12) np.testing.assert_allclose(vrho, vrho_ref, atol=2e-4) # TODO(leyley,htm): Fix vsigma with libxc 5. del vsigma, vsigma_ref # np.testing.assert_allclose(vsigma, vsigma_ref, atol=1e-2) np.testing.assert_allclose(vtau, vtau_ref, atol=1e-5)
def xc_scalar_ni(me, sp1, R1, sp2, R2, xc_code, deriv, **kw): from pyscf.dft.libxc import eval_xc """ Computes overlap for an atom pair. The atom pair is given by a pair of species indices and the coordinates of the atoms. Args: sp1,sp2 : specie indices, and R1,R2 : respective coordinates in Bohr, atomic units Result: matrix of orbital overlaps The procedure uses the numerical integration in coordinate space. """ grids = build_3dgrid(me, sp1, R1, sp2, R2, **kw) rho = dens_libnao(grids.coords, me.sv.nspin) exc, vxc, fxc, kxc = libxc.eval_xc(xc_code, rho.T, spin=me.sv.nspin - 1, deriv=deriv) ao1 = ao_eval(me.ao1, R1, sp1, grids.coords) if deriv == 1: #print(' vxc[0].shape ', vxc[0].shape) ao1 = ao1 * grids.weights * vxc[0] elif deriv == 2: ao1 = ao1 * grids.weights * fxc[0] else: print(' deriv ', deriv) raise RuntimeError('!deriv!') ao2 = ao_eval(me.ao2, R2, sp2, grids.coords) overlaps = blas.dgemm(1.0, ao1, ao2.T) return overlaps
def test_wb97xv_polarized_against_libxc(self, xc_name, xc_fun): eps_xc_ref, rhograds_ref, _, _ = libxc.eval_xc( xc_name, (self.rho_and_derivs_a, self.rho_and_derivs_b), spin=1, relativity=0, deriv=1) e_xc_ref = eps_xc_ref * self.rho vrho_ref, vsigma_ref, _, vtau_ref = rhograds_ref e_xc, grads = jax.vmap( jax.value_and_grad(xc_fun, argnums=(0, 1, 2, 3, 4, 5, 6)))(self.rhoa, self.rhob, self.sigma_aa, self.sigma_ab, self.sigma_bb, self.tau_a, self.tau_b) vrhoa, vrhob, vsigma_aa, vsigma_ab, vsigma_bb, vtau_a, vtau_b = grads np.testing.assert_allclose(np.sum(e_xc * self.weights), np.sum(e_xc_ref * self.weights), atol=1e-5) np.testing.assert_allclose(e_xc, e_xc_ref, rtol=1e-2, atol=1.e-12) np.testing.assert_allclose(vrhoa, vrho_ref[:, 0], atol=2e-4) np.testing.assert_allclose(vrhob, vrho_ref[:, 1], atol=2e-4) # TODO(leyley,htm): Fix vsigma with libxc 5. del vsigma_aa, vsigma_bb # np.testing.assert_allclose(vsigma_aa, vsigma_ref[:, 0], atol=1e-2) np.testing.assert_allclose(vsigma_ab, vsigma_ref[:, 1], atol=1e-2) # np.testing.assert_allclose(vsigma_bb, vsigma_ref[:, 2], atol=1e-2) np.testing.assert_allclose(vtau_a, vtau_ref[:, 0], atol=1e-5) np.testing.assert_allclose(vtau_b, vtau_ref[:, 1], atol=1e-5)
def test_wb97xv_polarized_against_libxc(self, xc_name, xc_fun): eps_xc_ref, (vrho_ref, vsigma_ref, _, _), _, _ = libxc.eval_xc( xc_name, (self.rho_and_derivs_a, self.rho_and_derivs_b), spin=1, relativity=0, deriv=1) e_xc_ref = eps_xc_ref * self.rho vrhoa_ref, vrhob_ref = vrho_ref[:, 0], vrho_ref[:, 1] vsigma_aa_ref, vsigma_ab_ref, vsigma_bb_ref = (vsigma_ref[:, 0], vsigma_ref[:, 1], vsigma_ref[:, 2]) e_xc, grads = jax.vmap( jax.value_and_grad(xc_fun, argnums=(0, 1, 2, 3, 4)))(self.rhoa, self.rhob, self.sigma_aa, self.sigma_ab, self.sigma_bb) vrhoa, vrhob, vsigma_aa, vsigma_ab, vsigma_bb = grads np.testing.assert_allclose(e_xc, e_xc_ref, atol=1e-12) np.testing.assert_allclose(vrhoa, vrhoa_ref, atol=1e-6) np.testing.assert_allclose(vrhob, vrhob_ref, atol=1e-6) np.testing.assert_allclose(vsigma_aa, vsigma_aa_ref, atol=1.5e-2) np.testing.assert_allclose(vsigma_ab, vsigma_ab_ref, atol=1.5e-2) np.testing.assert_allclose(vsigma_bb, vsigma_bb_ref, atol=1.5e-2)
def get_dft_grid_stuff(code, rho_both, rho1, rho2): """Evaluate energy densities and potentials on a grid. Parameters ---------- code : str String with density functional code for PySCF. rho_both : np.ndarray(npoints, dtype=float) Total density evaluated on n grid points. rho1, rho2 : np.ndarray(npoints, dtype=float) Density of each fragment evaluated on n grid points. """ exc, vxc, fxc, kxc = libxc.eval_xc(code, rho_both) exc2, vxc2, fxc2, kxc2 = libxc.eval_xc(code, rho1) exc3, vxc3, fxc3, kxc3 = libxc.eval_xc(code, rho2) return (exc, exc2, exc3), (vxc, vxc2, vxc3)
def test_lda_xc_unpolarized_against_libxc(self, xc_name, xc_fun): eps_xc_ref, (vrho_ref, *_), _, _ = libxc.eval_xc(xc_name, self.rho, spin=0, relativity=0, deriv=1) e_xc_ref = eps_xc_ref * self.rho e_xc, vrho = jax.vmap(jax.value_and_grad(xc_fun))(self.rho) np.testing.assert_allclose(e_xc, e_xc_ref) np.testing.assert_allclose(vrho, vrho_ref)
def test_gga_xc_unpolarized_against_libxc(self, xc_name, xc_fun): eps_xc_ref, (vrho_ref, vsigma_ref, _, _), _, _ = libxc.eval_xc(xc_name, self.rho, spin=0, relativity=0, deriv=1) e_xc_ref = eps_xc_ref * self.rho e_xc, (vrho, vsigma) = jax.vmap( jax.value_and_grad(xc_fun, argnums=(0, 1)))(self.rho, self.sigma) np.testing.assert_allclose(e_xc, e_xc_ref, atol=1e-12) np.testing.assert_allclose(vrho, vrho_ref, atol=1e-6) np.testing.assert_allclose(vsigma, vsigma_ref, atol=1e-6)
def exc(sv, dm, xc_code, **kvargs): """ Computes the exchange-correlation energy for a given density matrix Args: sv : (System Variables), this must have arrays of coordinates and species, etc xc_code : is a string must comply with pySCF's convention PZ "LDA,PZ" "0.8*LDA+0.2*B88,PZ" Returns: exc x+c energy """ grid = sv.build_3dgrid_pp(**kvargs) dens = sv.dens_elec(grid.coords, dm) exc, vxc, fxc, kxc = libxc.eval_xc(xc_code, dens.T, spin=sv.nspin-1, deriv=0) nelec = np.dot(dens[:,0]*exc, grid.weights) return nelec
def exc(sv, dm, xc_code, **kvargs): """ Computes the exchange-correlation energy for a given density matrix Args: sv : (System Variables), this must have arrays of coordinates and species, etc xc_code : is a string must comply with pySCF's convention PZ "LDA,PZ" "0.8*LDA+0.2*B88,PZ" Returns: exc x+c energy """ grid = sv.build_3dgrid(**kvargs) dens = sv.dens_elec(grid.coords, dm) exc, vxc, fxc, kxc = libxc.eval_xc(xc_code, dens.T, spin=sv.nspin-1, deriv=0) nelec = np.dot(dens[:,0]*exc, grid.weights) return nelec
def test_lda_xc_polarized_against_libxc(self, xc_name, xc_fun): eps_xc_ref, (vrho_ref, *_), _, _ = libxc.eval_xc(xc_name, (self.rhoa, self.rhob), spin=1, relativity=0, deriv=1) e_xc_ref = eps_xc_ref * self.rho vrhoa_ref, vrhob_ref = vrho_ref[:, 0], vrho_ref[:, 1] e_xc, (vrhoa, vrhob) = jax.vmap(jax.value_and_grad(xc_fun, argnums=(0, 1)))(self.rhoa, self.rhob) np.testing.assert_allclose(e_xc, e_xc_ref) np.testing.assert_allclose(vrhoa, vrhoa_ref) np.testing.assert_allclose(vrhob, vrhob_ref)
def compute_ldacorr_pyscf(rho, xc_code=',VWN5'): """Correlation energy functional.""" from pyscf.dft import libxc exc, vxc, fxc, kxc = libxc.eval_xc(xc_code, rho) return (exc, vxc[0])
def get_dft_grid_stuff(code, rho_both, rho1, rho2): # Evaluate energy densities and potentials on a grid exc, vxc, fxc, kxc = libxc.eval_xc(code, rho_both) exc2, vxc2, fxc2, kxc2 = libxc.eval_xc(code, rho1) exc3, vxc3, fxc3, kxc3 = libxc.eval_xc(code, rho2) return (exc, exc2, exc3), (vxc, vxc2, vxc3)