Skip to content

Commit 843ea07

Browse files
test sanity as well as correctness
1 parent d779457 commit 843ea07

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

pyscf/grad/test/test_diatomic_gradients.py

+15-5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from pyscf import gto, scf, df, dft
1717
from pyscf.data.nist import BOHR
1818
from pyscf import mcpdft
19+
from pyscf.fci.addons import _unpack_nelec
1920
import unittest
2021

2122
def diatomic (atom1, atom2, r, fnal, basis, ncas, nelecas, nstates,
@@ -34,7 +35,8 @@ def diatomic (atom1, atom2, r, fnal, basis, ncas, nelecas, nstates,
3435
#if spin is not None: smult = spin+1
3536
#else: smult = (mol.nelectron % 2) + 1
3637
#mc.fcisolver = csf_solver (mol, smult=smult)
37-
if spin is None: spin = mol.nelectron%2
38+
neleca, nelecb = _unpack_nelec (nelecas, spin=spin)
39+
spin = neleca-nelecb
3840
ss=spin*(spin+2)*0.25
3941
mc = mc.multi_state ([1.0/float(nstates),]*nstates, 'cms')
4042
mc.fix_spin_(ss=ss, shift=1)
@@ -189,26 +191,34 @@ def test_grad_lih_cms2ftlda22_sto3g_df (self):
189191
def test_rohf_sanity (self):
190192
mc_grad = diatomic ('Li', 'H', 1.8, 'ftLDA,VWN3', '6-31g', 4, 2, 2, symmetry=True,
191193
cas_irrep={'A1': 4}, spin=2)
192-
de_ref = [-0.039806,-0.024193]
194+
mc_grad_ref = diatomic ('Li', 'H', 1.8, 'ftLDA,VWN3', '6-31g', 4, (2,0), 2,
195+
symmetry=True, cas_irrep={'A1': 4})
196+
de_num_ref = [-0.039806,-0.024193]
193197
# Numerical from this software
194198
# PySCF commit: bee0ce288a655105e27fcb0293b203939b7aecc9
195199
# PySCF-forge commit: 50bc1da117ced9613948bee14a99a02c7b2c5769
196200
for i in range (2):
197201
with self.subTest (state=i):
198202
de = mc_grad.kernel (state=i) [1,0] / BOHR
199-
self.assertAlmostEqual (de, de_ref[i], 4)
203+
self.assertAlmostEqual (de, de_num_ref[i], 4)
204+
de_ref = mc_grad_ref.kernel (state=i) [1,0] / BOHR
205+
self.assertAlmostEqual (de, de_ref, 6)
200206

201207
def test_dfrohf_sanity (self):
202208
mc_grad = diatomic ('Li', 'H', 1.8, 'ftLDA,VWN3', '6-31g', 4, 2, 2, symmetry=True,
203209
density_fit=True, cas_irrep={'A1': 4}, spin=2)
204-
de_ref = [-0.039721,-0.024139]
210+
mc_grad_ref = diatomic ('Li', 'H', 1.8, 'ftLDA,VWN3', '6-31g', 4, (2,0), 2,
211+
symmetry=True, density_fit=True, cas_irrep={'A1': 4})
212+
de_num_ref = [-0.039721,-0.024139]
205213
# Numerical from this software
206214
# PySCF commit: bee0ce288a655105e27fcb0293b203939b7aecc9
207215
# PySCF-forge commit: 50bc1da117ced9613948bee14a99a02c7b2c5769
208216
for i in range (2):
209217
with self.subTest (state=i):
210218
de = mc_grad.kernel (state=i) [1,0] / BOHR
211-
self.assertAlmostEqual (de, de_ref[i], 4)
219+
self.assertAlmostEqual (de, de_num_ref[i], 4)
220+
de_ref = mc_grad_ref.kernel (state=i) [1,0] / BOHR
221+
self.assertAlmostEqual (de, de_ref, 6)
212222

213223
if __name__ == "__main__":
214224
print("Full Tests for CMS-PDFT gradients of diatomic molecules")

pyscf/grad/test/test_grad_lpdft.py

+17-7
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
from pyscf import scf, gto, df, dft, lib
2828
from pyscf import mcpdft
29-
29+
from pyscf.fci.addons import _unpack_nelec
3030

3131
def diatomic(
3232
atom1,
@@ -62,8 +62,8 @@ def diatomic(
6262
mf = mf.density_fit(auxbasis=df.aug_etb(mol))
6363

6464
mc = mcpdft.CASSCF(mf.run(), fnal, ncas, nelecas, grids_level=grids_level)
65-
if spin is None:
66-
spin = mol.nelectron % 2
65+
neleca, nelecb = _unpack_nelec (nelecas, spin=spin)
66+
spin = neleca-nelecb
6767

6868
ss = spin * (spin + 2) * 0.25
6969
mc = mc.multi_state(
@@ -267,34 +267,44 @@ def test_grad_lih_lin2ftpbe22_sto3g_df(self):
267267
self.assertAlmostEqual(de, NUM_REF[i], 5)
268268

269269
def test_rohf_sanity (self):
270-
n_states = 2
270+
n_states = 3
271271
mc_grad = diatomic(
272272
"Li", "H", 1.4, "ftpbe", "6-31g", 4, 2, n_states, density_fit=False, spin=2
273273
)
274+
mc_grad_ref = diatomic(
275+
"Li", "H", 1.4, "ftpbe", "6-31g", 4, (2,0), n_states, density_fit=False
276+
)
274277

275278
# Numerical from this software
276279
# PySCF commit: bee0ce288a655105e27fcb0293b203939b7aecc9
277280
# PySCF-forge commit: 50bc1da117ced9613948bee14a99a02c7b2c5769
278-
NUM_REF = [-0.06264161, -0.05843108]
281+
NUM_REF = [-0.062533, -0.058472, -0.058472]
279282
for i in range(n_states):
280283
with self.subTest(state=i):
281284
de = mc_grad.kernel(state=i)[1, 0]
282285
self.assertAlmostEqual(de, NUM_REF[i], 4)
286+
de_ref = mc_grad_ref.kernel(state=i)[1, 0]
287+
self.assertAlmostEqual (de, de_ref, 8)
283288

284289
def test_dfrohf_sanity (self):
285-
n_states = 2
290+
n_states = 3
286291
mc_grad = diatomic(
287292
"Li", "H", 1.4, "ftpbe", "6-31g", 4, 2, n_states, density_fit=True, spin=2
288293
)
294+
mc_grad_ref = diatomic(
295+
"Li", "H", 1.4, "ftpbe", "6-31g", 4, (2,0), n_states, density_fit=True,
296+
)
289297

290298
# Numerical from this software
291299
# PySCF commit: bee0ce288a655105e27fcb0293b203939b7aecc9
292300
# PySCF-forge commit: 50bc1da117ced9613948bee14a99a02c7b2c5769
293-
NUM_REF = [-0.06263842, -0.05846438]
301+
NUM_REF = [-0.062548, -0.058501, -0.058501]
294302
for i in range(n_states):
295303
with self.subTest(state=i):
296304
de = mc_grad.kernel(state=i)[1, 0]
297305
self.assertAlmostEqual(de, NUM_REF[i], 4)
306+
de_ref = mc_grad_ref.kernel(state=i)[1, 0]
307+
self.assertAlmostEqual (de, de_ref, 6)
298308

299309

300310

0 commit comments

Comments
 (0)