Skip to content

Commit 12f534c

Browse files
committed
mcscf: register GASSCF spin-energy result attributes
1 parent d5a5080 commit 12f534c

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

‎pyscf/mcscf/gasscf.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,9 @@ class GASSCF(newton_casscf.CASSCF):
751751
"""
752752

753753
_keys = set(newton_casscf.CASSCF._keys) | {
754-
"gas_orbs", "gas_restr", "gas_restr_type", "cache_plans"}
754+
"gas_orbs", "gas_restr", "gas_restr_type", "cache_plans",
755+
"e_spin_penalty", "e_tot_physical", "e_gas_physical",
756+
"spin_penalty_method"}
755757

756758
def __init__(self, mf, ncas=None, nelecas=None, gas_orbs=None,
757759
gas_restr=None, gas_restr_type=None, *, ncore=None, frozen=None,
@@ -789,6 +791,10 @@ def __init__(self, mf, ncas=None, nelecas=None, gas_orbs=None,
789791
super().__init__(mf, ncas, nelecas, ncore=ncore, frozen=frozen)
790792
self.fcisolver = solver
791793
self.fcisolver.mol = self.mol
794+
self.e_spin_penalty = None
795+
self.e_tot_physical = None
796+
self.e_gas_physical = None
797+
self.spin_penalty_method = None
792798

793799

794800
def _push_gasscf_log_labels(self):

‎pyscf/mcscf/test/test_gasscf.py‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2421,6 +2421,49 @@ def test_as_scanner_runs_energy_scan_with_fixed_gas_model(self):
24212421
self.assertIn("GASSCF", scanner.__class__.__name__)
24222422
self.assertIs(scanner.as_scanner(), scanner)
24232423

2424+
def test_repeated_kernel_and_scanner_spin_results_pass_sanity(self):
2425+
mol = gto.M(
2426+
atom="H 0 0 0; H 0 0 0.9; H 0 0 2.2; H 0 0 3.1",
2427+
basis="sto-3g", verbose=0)
2428+
mf = scf.RHF(mol).run()
2429+
for spin_penalty in (False, True):
2430+
with self.subTest(spin_penalty=spin_penalty):
2431+
mc = gasscf.GASSCF(
2432+
mf, 2, (1, 1), gas_orbs=(2,), ncore=1)
2433+
mc.max_cycle_macro = 1
2434+
mc.max_cycle_micro = 1
2435+
mc.canonicalization = False
2436+
if spin_penalty:
2437+
mc.fix_spin_(shift=.2, ss=0)
2438+
mc.verbose = 4
2439+
mc.stdout = io.StringIO()
2440+
errors = io.StringIO()
2441+
# Do not let earlier tests hide a warning through warn-once.
2442+
with mock.patch.dict(gasscf.lib.misc._warn_once_registry,
2443+
{}, clear=True), \
2444+
mock.patch.object(sys, "stderr", errors):
2445+
mc.kernel(mf.mo_coeff)
2446+
mc.kernel(mc.mo_coeff, mc.ci)
2447+
scanner = mc.as_scanner()
2448+
for distance in (.92, .94):
2449+
energy = scanner(
2450+
"H 0 0 0; H 0 0 %s; H 0 0 2.2; H 0 0 3.1"
2451+
% distance)
2452+
self.assertTrue(numpy.isfinite(energy))
2453+
penalty = scanner.e_spin_penalty
2454+
if spin_penalty:
2455+
self.assertIsNotNone(penalty)
2456+
else:
2457+
self.assertIsNone(penalty)
2458+
penalty = 0.
2459+
self.assertAlmostEqual(
2460+
scanner.e_tot_physical + penalty, energy, 10)
2461+
self.assertAlmostEqual(
2462+
scanner.e_gas_physical + penalty,
2463+
scanner.e_gas, 10)
2464+
self.assertNotIn("does not have attributes", errors.getvalue())
2465+
self.assertNotIn("does not have attributes", mc.stdout.getvalue())
2466+
24242467
def test_as_scanner_rejects_changed_extrasym_constraint(self):
24252468
mol = gto.M(
24262469
atom="H 0 0 0; H 0 0 0.9; H 0 0 2.2; H 0 0 3.1",

0 commit comments

Comments
 (0)