diff --git a/gpu4pyscf/df/tests/test_df_ecp.py b/gpu4pyscf/df/tests/test_df_ecp.py index e6614d53..3f73296b 100644 --- a/gpu4pyscf/df/tests/test_df_ecp.py +++ b/gpu4pyscf/df/tests/test_df_ecp.py @@ -51,11 +51,19 @@ def run_dft(xc): e_dft = mf.kernel() return e_dft +def _make_mf(): + mf = rks.RKS(mol, xc=xc).density_fit(auxbasis=auxbasis) + mf.conv_tol = 1e-12 + mf.grids.level = grids_level + mf.verbose = 1 + mf.kernel() + return mf + def _check_grad(grid_response=False, tol=1e-5): mf = rks.RKS(mol, xc=xc).density_fit(auxbasis=auxbasis) mf.grids.level = grids_level mf.conv_tol = 1e-12 - e_tot = mf.kernel() + mf.kernel() g = mf.nuc_grad_method() g.auxbasis_response = True g.grid_response = grid_response @@ -85,22 +93,14 @@ def _check_grad(grid_response=False, tol=1e-5): mol.set_geom_(coords, unit='Bohr') grad_fd[i,j] = (e0-e1)/2.0/eps grad_fd = np.array(grad_fd).reshape(-1,3) - print('finite difference gradient:') - print(grad_fd) - print('difference between analytical and finite difference gradient:', np.linalg.norm(g_analy - grad_fd)) + print('norm of analytical - finite difference gradient:', np.linalg.norm(g_analy - grad_fd)) assert(np.linalg.norm(g_analy - grad_fd) < tol) -def _check_dft_hessian(disp=None, ix=0, iy=0, tol=1e-3): - pmol = mol.copy() +def _check_dft_hessian(mf, h, ix=0, iy=0, tol=1e-3): + pmol = mf.mol.copy() pmol.build() - mf = rks.RKS(pmol, xc=xc).density_fit(auxbasis=auxbasis) - mf.conv_tol = 1e-12 - mf.grids.level = grids_level - mf.verbose = 1 - mf.kernel() - g = mf.nuc_grad_method() g.auxbasis_response = True g.kernel() @@ -112,31 +112,19 @@ def _check_dft_hessian(disp=None, ix=0, iy=0, tol=1e-3): pmol.set_geom_(coords + v, unit='Bohr') pmol.build() _, g0 = g_scanner(pmol) - + pmol.set_geom_(coords - v, unit='Bohr') pmol.build() _, g1 = g_scanner(pmol) - + h_fd = (g0 - g1)/2.0/eps pmol.set_geom_(coords, unit='Bohr') pmol.build() - mf = rks.RKS(pmol, xc=xc, disp=disp).density_fit(auxbasis=auxbasis) - mf.grids.level = grids_level - mf.kernel() - hobj = mf.Hessian() - hobj.set(auxbasis_response=2) - hobj.verbose=0 - h = hobj.kernel() - - print(f"analytical Hessian H({ix},{iy})") - print(h[ix,:,iy,:]) - print(f"finite different Hessian H({ix},{iy})") - print(h_fd) - print('Norm of diff', np.linalg.norm(h[ix,:,iy,:] - h_fd)) + print(f'Norm of finite difference - analytical Hessian({ix},{iy})', np.linalg.norm(h[ix,:,iy,:] - h_fd)) assert(np.linalg.norm(h[ix,:,iy,:] - h_fd) < tol) -class KnownValues(unittest.TestCase): +class KnownValues(unittest.TestCase): def test_rks_scf(self): mf = rks.RKS(mol, xc=xc).density_fit(auxbasis=auxbasis) mf.conv_tol = 1e-12 @@ -145,10 +133,16 @@ def test_rks_scf(self): def test_rks_gradient(self): _check_grad() - + def test_rks_hessian(self): - _check_dft_hessian(ix=0, iy=0) - _check_dft_hessian(ix=0, iy=1) + mf = _make_mf() + hobj = mf.Hessian() + hobj.set(auxbasis_response=2) + hobj.verbose=0 + h = hobj.kernel() + + _check_dft_hessian(mf, h, ix=0, iy=0) + _check_dft_hessian(mf, h, ix=0, iy=1) if __name__ == "__main__": print("Full Tests for DF ECP") diff --git a/gpu4pyscf/df/tests/test_df_hessian.py b/gpu4pyscf/df/tests/test_df_hessian.py index 0b0d3f20..fd5a8bd7 100644 --- a/gpu4pyscf/df/tests/test_df_hessian.py +++ b/gpu4pyscf/df/tests/test_df_hessian.py @@ -42,19 +42,29 @@ def tearDownModule(): global mol del mol -def _check_rhf_hessian(ix=0, iy=0, tol=1e-3): - pmol = mol.copy() - pmol.build() - - mf = scf.RHF(pmol).density_fit(auxbasis='ccpvtz-jkfit') +def _make_rhf(): + mf = scf.RHF(mol).density_fit(auxbasis='ccpvtz-jkfit') + mf.conv_tol = 1e-12 + mf.kernel() + return mf + +def _make_rks(xc, disp=None): + mf = dft.rks.RKS(mol, xc=xc).density_fit(auxbasis=auxbasis0) mf.conv_tol = 1e-12 + mf.disp = disp + mf.grids.level = grids_level + mf.verbose = 1 mf.kernel() + return mf + +def _check_rhf_hessian(mf, h, ix=0, iy=0, tol=1e-3): + pmol = mf.mol.copy() + pmol.build() + g = mf.nuc_grad_method() g.kernel() g_scanner = g.as_scanner() - hobj = mf.Hessian() - hobj.set(auxbasis_response=2) - + coords = pmol.atom_coords() v = np.zeros_like(coords) v[ix,iy] = eps @@ -67,25 +77,13 @@ def _check_rhf_hessian(ix=0, iy=0, tol=1e-3): _, g1 = g_scanner(pmol) h_fd = (g0 - g1)/2.0/eps - pmol.set_geom_(coords, unit='Bohr') - h = hobj.kernel() - print(f"analytical Hessian H({ix},{iy})") - print(h[ix,:,iy,:]) - print(f"finite different Hessian H({ix},{iy})") - print(h_fd) - print('Norm of diff', np.linalg.norm(h[ix,:,iy,:] - h_fd)) + print(f'Norm of analytical - finite difference Hessian H({ix},{iy})', np.linalg.norm(h[ix,:,iy,:] - h_fd)) assert(np.linalg.norm(h[ix,:,iy,:] - h_fd) < tol) -def _check_dft_hessian(xc=xc0, disp=None, ix=0, iy=0, tol=1e-3): - pmol = mol.copy() +def _check_dft_hessian(mf, h, ix=0, iy=0, tol=1e-3): + pmol = mf.mol.copy() pmol.build() - mf = dft.rks.RKS(pmol, xc=xc, disp=disp).density_fit(auxbasis=auxbasis0) - mf.conv_tol = 1e-12 - mf.grids.level = grids_level - mf.verbose = 1 - mf.kernel() - g = mf.nuc_grad_method() g.auxbasis_response = True g.kernel() @@ -101,57 +99,67 @@ def _check_dft_hessian(xc=xc0, disp=None, ix=0, iy=0, tol=1e-3): pmol.set_geom_(coords - v, unit='Bohr') pmol.build() _, g1 = g_scanner(pmol) - + h_fd = (g0 - g1)/2.0/eps - pmol.set_geom_(coords, unit='Bohr') - pmol.build() - mf = dft.rks.RKS(pmol, xc=xc, disp=disp).density_fit(auxbasis=auxbasis0) - mf.grids.level = grids_level - mf.kernel() - hobj = mf.Hessian() - hobj.set(auxbasis_response=2) - hobj.verbose=0 - h = hobj.kernel() - - print(f"analytical Hessian H({ix},{iy})") - print(h[ix,:,iy,:]) - print(f"finite different Hessian H({ix},{iy})") - print(h_fd) - print('Norm of diff', np.linalg.norm(h[ix,:,iy,:] - h_fd)) + print(f'Norm of analytical - finite difference Hessian H({ix},{iy})', np.linalg.norm(h[ix,:,iy,:] - h_fd)) assert(np.linalg.norm(h[ix,:,iy,:] - h_fd) < tol) - + class KnownValues(unittest.TestCase): def test_hessian_rhf(self): print('-----testing DF RHF Hessian----') - _check_rhf_hessian(ix=0, iy=0) - _check_rhf_hessian(ix=0, iy=1) - + mf = _make_rhf() + hobj = mf.Hessian() + hobj.set(auxbasis_response=2) + h = hobj.kernel() + _check_rhf_hessian(mf, h, ix=0, iy=0) + _check_rhf_hessian(mf, h, ix=0, iy=1) + def test_hessian_lda(self): print('-----testing DF LDA Hessian----') - _check_dft_hessian(xc='LDA', disp=None, ix=0,iy=0) - _check_dft_hessian(xc='LDA', disp=None, ix=0,iy=1) - + mf = _make_rks('LDA') + hobj = mf.Hessian() + hobj.set(auxbasis_response=2) + h = hobj.kernel() + _check_dft_hessian(mf, h, ix=0,iy=0) + _check_dft_hessian(mf, h, ix=0,iy=1) + def test_hessian_gga(self): print('-----testing DF PBE Hessian----') - _check_dft_hessian(xc='PBE', disp=None, ix=0,iy=0) - _check_dft_hessian(xc='PBE', disp=None, ix=0,iy=1) - + mf = _make_rks('PBE') + hobj = mf.Hessian() + hobj.set(auxbasis_response=2) + h = hobj.kernel() + _check_dft_hessian(mf, h, ix=0,iy=0) + _check_dft_hessian(mf, h, ix=0,iy=1) + def test_hessian_hybrid(self): print('-----testing DF B3LYP Hessian----') - _check_dft_hessian(xc='B3LYP', disp=None, ix=0,iy=0) - _check_dft_hessian(xc='B3LYP', disp=None, ix=0,iy=1) + mf = _make_rks('b3lyp') + hobj = mf.Hessian() + hobj.set(auxbasis_response=2) + h = hobj.kernel() + _check_dft_hessian(mf, h, ix=0,iy=0) + _check_dft_hessian(mf, h, ix=0,iy=1) def test_hessian_mgga(self): print('-----testing DF M06 Hessian----') - _check_dft_hessian(xc='m06', disp=None, ix=0,iy=0) - _check_dft_hessian(xc='m06', disp=None, ix=0,iy=1) - + mf = _make_rks('m06') + hobj = mf.Hessian() + hobj.set(auxbasis_response=2) + h = hobj.kernel() + _check_dft_hessian(mf, h, ix=0,iy=0) + _check_dft_hessian(mf, h, ix=0,iy=1) + def test_hessian_rsh(self): print('-----testing DF wb97 Hessian----') - _check_dft_hessian(xc='wb97', disp=None, ix=0,iy=0) - _check_dft_hessian(xc='wb97', disp=None, ix=0,iy=1) - + mf = _make_rks('wb97') + hobj = mf.Hessian() + hobj.set(auxbasis_response=2) + h = hobj.kernel() + _check_dft_hessian(mf, h, ix=0,iy=0) + _check_dft_hessian(mf, h, ix=0,iy=1) + def test_hessian_D3(self): pmol = mol.copy() pmol.build() @@ -161,11 +169,11 @@ def test_hessian_D3(self): mf.grids.level = grids_level mf.verbose = 1 mf.kernel() - + hobj = mf.Hessian() hobj.set(auxbasis_response=2) hobj.verbose=0 - h = hobj.kernel() + hobj.kernel() def test_hessian_D4(self): pmol = mol.copy() @@ -180,7 +188,7 @@ def test_hessian_D4(self): hobj = mf.Hessian() hobj.set(auxbasis_response=2) hobj.verbose=0 - h = hobj.kernel() + hobj.kernel() if __name__ == "__main__": print("Full Tests for DF Hessian") diff --git a/gpu4pyscf/solvent/hessian/pcm.py b/gpu4pyscf/solvent/hessian/pcm.py index d0a3e702..394386cd 100644 --- a/gpu4pyscf/solvent/hessian/pcm.py +++ b/gpu4pyscf/solvent/hessian/pcm.py @@ -193,7 +193,7 @@ def pcm_vmat_scanner(mol): pcmobj.reset(pmol) return vmat -''' +""" def analytic_grad_vmat(pcmobj, mo_coeff, mo_occ, atmlst=None, verbose=None): ''' dv_solv / da @@ -221,7 +221,7 @@ def analytic_grad_vmat(pcmobj, mo_coeff, mo_occ, atmlst=None, verbose=None): t1 = log.timer_debug1('computing solvent grad veff', *t1) pcmobj.reset(pmol) return vmat -''' +""" def make_hess_object(hess_method): ''' return solvent hessian object diff --git a/gpu4pyscf/solvent/tests/test_pcm_hessian.py b/gpu4pyscf/solvent/tests/test_pcm_hessian.py index 075d87f0..b6c650a3 100644 --- a/gpu4pyscf/solvent/tests/test_pcm_hessian.py +++ b/gpu4pyscf/solvent/tests/test_pcm_hessian.py @@ -54,7 +54,7 @@ def _make_mf(method='C-PCM'): return mf def _check_hessian(mf, h, ix=0, iy=0): - pmol = mol.copy() + pmol = mf.mol.copy() pmol.build() g = mf.nuc_grad_method() @@ -81,7 +81,6 @@ def _check_hessian(mf, h, ix=0, iy=0): assert(np.linalg.norm(h[ix,:,iy,:] - h_fd) < tol) class KnownValues(unittest.TestCase): - def test_hess_cpcm(self): mf = _make_mf(method='C-PCM') hobj = mf.Hessian()