|
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | 15 | import unittest |
| 16 | +import pytest |
16 | 17 | import numpy as np |
17 | 18 | import cupy as cp |
18 | 19 | import pyscf |
19 | 20 | from gpu4pyscf import scf, dft |
20 | 21 |
|
21 | 22 | def setUpModule(): |
22 | | - global mol_minimal_one_atom, mol_minimal_two_atom, auxbasis_minimal_good, auxbasis_minimal_bad #, mol_real |
| 23 | + global mol_minimal_one_atom, mol_minimal_two_atom, auxbasis_minimal_good, auxbasis_minimal_bad |
23 | 24 | mol_minimal_one_atom = pyscf.M( |
24 | 25 | atom = """ |
25 | 26 | He 100 200 300 |
@@ -52,27 +53,12 @@ def setUpModule(): |
52 | 53 | 2.0 1.0 |
53 | 54 | """ |
54 | 55 |
|
55 | | - # mol_real = pyscf.M( |
56 | | - # atom = """ |
57 | | - # C 0.000000 0.000000 0.000000 |
58 | | - # Br 0.000000 0.000000 1.940000 |
59 | | - # H 1.027662 0.000000 -0.363333 |
60 | | - # H -0.513831 0.889981 -0.363333 |
61 | | - # H -0.513831 -0.889981 -0.363333 |
62 | | - # """, |
63 | | - # basis = "def2-svp", |
64 | | - # verbose = 4, |
65 | | - # output='/dev/null', |
66 | | - # ) |
67 | | - |
68 | 56 | def tearDownModule(): |
69 | | - global mol_minimal_one_atom, mol_minimal_two_atom #, mol_real |
| 57 | + global mol_minimal_one_atom, mol_minimal_two_atom |
70 | 58 | mol_minimal_one_atom.stdout.close() |
71 | 59 | del mol_minimal_one_atom |
72 | 60 | mol_minimal_two_atom.stdout.close() |
73 | 61 | del mol_minimal_two_atom |
74 | | - # mol_real.stdout.close() |
75 | | - # del mol_real |
76 | 62 |
|
77 | 63 | class KnownValues(unittest.TestCase): |
78 | 64 | def test_hessian_large_exp_one_atom_rhf(self): |
@@ -170,64 +156,81 @@ def test_hessian_large_exp_two_atom_rhf_density_fit_bad_auxbasis(self): |
170 | 156 |
|
171 | 157 | assert np.max(np.abs(translation_invariance)) < 1e-4 |
172 | 158 |
|
173 | | - # TODO: Fix DFT Hessian |
174 | | - |
175 | | - # def test_hessian_large_exp_one_atom_rks(self): |
176 | | - # mf = dft.RKS(mol_minimal_one_atom, xc = "LDA") |
177 | | - # mf.grids.atom_grid = (200,194) |
178 | | - # mf.conv_tol = 1e-10 |
| 159 | + def test_hessian_large_exp_one_atom_rks(self): |
| 160 | + mf = dft.RKS(mol_minimal_one_atom, xc = "r2SCAN") |
| 161 | + mf.grids.atom_grid = (50,194) |
| 162 | + mf.conv_tol = 1e-10 |
179 | 163 |
|
180 | | - # mf.kernel() |
181 | | - # assert mf.converged |
| 164 | + mf.kernel() |
| 165 | + assert mf.converged |
182 | 166 |
|
183 | | - # hobj = mf.Hessian() |
184 | | - # hessian = hobj.kernel() |
| 167 | + hobj = mf.Hessian() |
| 168 | + hobj.grid_response = True |
| 169 | + hessian = hobj.kernel() |
185 | 170 |
|
186 | | - # natm = mf.mol.natm |
187 | | - # hessian = hessian.transpose(0,2,1,3) |
188 | | - # hessian = hessian.reshape((natm * 3, natm * 3)) |
189 | | - # translation_invariance = np.sum(hessian, axis = 0).reshape(natm, 3) |
| 171 | + natm = mf.mol.natm |
| 172 | + hessian = hessian.transpose(0,2,1,3) |
| 173 | + hessian = hessian.reshape((natm * 3, natm * 3)) |
| 174 | + translation_invariance = np.sum(hessian, axis = 0).reshape(natm, 3) |
190 | 175 |
|
191 | | - # # Zero hessian for only one atom |
192 | | - # assert np.max(np.abs(hessian)) < 1e-3 |
193 | | - # assert np.max(np.abs(translation_invariance)) < 1e-3 |
| 176 | + # Zero hessian for only one atom |
| 177 | + assert np.max(np.abs(hessian)) < 1e-4 |
| 178 | + assert np.max(np.abs(translation_invariance)) < 1e-4 |
194 | 179 |
|
195 | | - # def test_hessian_large_exp_two_atom_rks(self): |
196 | | - # mf = dft.RKS(mol_minimal_two_atom, xc = "LDA") |
197 | | - # mf.grids.atom_grid = (200,194) |
198 | | - # mf.conv_tol = 1e-10 |
| 180 | + def test_hessian_large_exp_two_atom_rks(self): |
| 181 | + mf = dft.RKS(mol_minimal_two_atom, xc = "wB97X") |
| 182 | + mf.grids.atom_grid = (50,194) |
| 183 | + mf.conv_tol = 1e-10 |
| 184 | + mf.level_shift = 0.001 |
199 | 185 |
|
200 | | - # mf.kernel() |
201 | | - # assert mf.converged |
| 186 | + mf.kernel() |
| 187 | + assert mf.converged |
202 | 188 |
|
203 | | - # hobj = mf.Hessian() |
204 | | - # hessian = hobj.kernel() |
| 189 | + hobj = mf.Hessian() |
| 190 | + hobj.grid_response = True |
| 191 | + hessian = hobj.kernel() |
205 | 192 |
|
206 | | - # natm = mf.mol.natm |
207 | | - # hessian = hessian.transpose(0,2,1,3) |
208 | | - # hessian = hessian.reshape((natm * 3, natm * 3)) |
209 | | - # translation_invariance = np.sum(hessian, axis = 0).reshape(natm, 3) |
| 193 | + natm = mf.mol.natm |
| 194 | + hessian = hessian.transpose(0,2,1,3) |
| 195 | + hessian = hessian.reshape((natm * 3, natm * 3)) |
| 196 | + translation_invariance = np.sum(hessian, axis = 0).reshape(natm, 3) |
210 | 197 |
|
211 | | - # assert np.max(np.abs(translation_invariance)) < 1e-3 |
| 198 | + assert np.max(np.abs(translation_invariance)) < 1e-4 |
212 | 199 |
|
213 | | - # def test_hessian_large_exp_methylbromide_rks(self): |
214 | | - # mf = dft.RKS(mol_real, xc = "wB97X") |
215 | | - # mf.grids.atom_grid = (99,590) |
216 | | - # mf = mf.density_fit(auxbasis = "def2-universal-jkfit") |
217 | | - # mf.conv_tol = 1e-10 |
| 200 | + @pytest.mark.skip("Too slow, functionality covered by corresponding tests above") |
| 201 | + def test_hessian_large_exp_methylbromide_rks(self): |
| 202 | + mol_real = pyscf.M( |
| 203 | + atom = """ |
| 204 | + C 0.000000 0.000000 0.000000 |
| 205 | + Br 0.000000 0.000000 1.940000 |
| 206 | + H 1.027662 0.000000 -0.363333 |
| 207 | + H -0.513831 0.889981 -0.363333 |
| 208 | + H -0.513831 -0.889981 -0.363333 |
| 209 | + """, |
| 210 | + basis = "def2-svp", |
| 211 | + verbose = 4, |
| 212 | + output='/dev/null', |
| 213 | + ) |
| 214 | + |
| 215 | + mf = dft.RKS(mol_real, xc = "wB97X") |
| 216 | + mf.grids.atom_grid = (99,590) |
| 217 | + mf = mf.density_fit(auxbasis = "def2-universal-jkfit") |
| 218 | + mf.conv_tol = 1e-10 |
218 | 219 |
|
219 | | - # mf.kernel() |
220 | | - # assert mf.converged |
| 220 | + mf.kernel() |
| 221 | + assert mf.converged |
221 | 222 |
|
222 | | - # hobj = mf.Hessian() |
223 | | - # hessian = hobj.kernel() |
| 223 | + hobj = mf.Hessian() |
| 224 | + hobj.auxbasis_response = 2 |
| 225 | + hobj.grid_response = True |
| 226 | + hessian = hobj.kernel() |
224 | 227 |
|
225 | | - # natm = mf.mol.natm |
226 | | - # hessian = hessian.transpose(0,2,1,3) |
227 | | - # hessian = hessian.reshape((natm * 3, natm * 3)) |
228 | | - # translation_invariance = np.sum(hessian, axis = 0).reshape(natm, 3) |
| 228 | + natm = mf.mol.natm |
| 229 | + hessian = hessian.transpose(0,2,1,3) |
| 230 | + hessian = hessian.reshape((natm * 3, natm * 3)) |
| 231 | + translation_invariance = np.sum(hessian, axis = 0).reshape(natm, 3) |
229 | 232 |
|
230 | | - # assert np.max(np.abs(translation_invariance)) < 1e-10 |
| 233 | + assert np.max(np.abs(translation_invariance)) < 1e-7 |
231 | 234 |
|
232 | 235 | if __name__ == "__main__": |
233 | 236 | print("Edge Case Tests for Hessian Calculation with Large Exponent in Atomic Orbitals") |
|
0 commit comments