Skip to content

A replay backend for the tests, and Dimer.from_backend: the SAPT core needs nothing beyond the backend contract - #15

Merged
btyrcha merged 4 commits into
mainfrom
feature/replay-backend
Nov 30, 2026
Merged

btyrcha merged 4 commits into
mainfrom
feature/replay-backend

Conversation

@btyrcha

@btyrcha btyrcha commented Nov 29, 2026

Copy link
Copy Markdown
Owner

Step 1.4 of the backend work: check that propSAPT needs nothing from a backend beyond the contract of prop_sapt/backends/base.py. Psi4 stays the only real backend. The check uses a replay backend, a test double in tests/ and not a backend option: it replays a recorded Psi4 RHF calculation and redoes only the contract's operations in NumPy.

  • Dimer.from_backend(backend). A dimer of a backend calculation that has run already, such as a Psi4Backend built beforehand. Later backends will reach the SAPT code the same way.
    • The backend-neutral part of Dimer.__init__ (the SAPT arrays and the empty amplitude caches) moves into _setup(backend).
    • Dimer(...) still builds a Psi4Backend inside Psi4's timer. Its output.dat matches main's except for timings and date stamps.
    • from_backend times the construction with the backend's own timer() and sets no geometry string. It raises a TypeError for anything that is not a Backend.
    • A changelog line, a sentence in the usage guide, and the docstring in the API reference.
  • The replay backend (tests/replay_backend.py). ReplayBackend.record(backend) copies every array of an RHF backend. It builds J/K from Qpq, and the orbital-Hessian product from that J/K: (eps_r - eps_a) x + C_occ^T (4J - K - K^T) C_vir, for the density C_occ x C_vir^T. It rejects RKS, whose Hessian needs the XC kernel, and the CPSCF J/K types.
    • The J/K maths is df_jk in tests/matched_integrals.py, which df_compute_jk now calls too.
    • no_psi4_calls() makes every call into psi4.core and Psi4's driver fail inside its block. The exception is psi4.core.print_out, which the package-wide log handler writes every record through, whatever the backend.
  • Fixtures. matched_psi4_dimer is He–H2 with scf_type df and df_basis_scf set to the DF_BASIS_SAPT basis, so Psi4's SCF, J/K and cphf_Hx all use the fitted integrals that Qpq factors. It restores the two options afterwards. replay_dimer is Dimer.from_backend on its replay.

What it found:

  • The SAPT core needs nothing beyond the contract. These ran on the replay dimer with Psi4 blocked, and no package change was needed for them:
    • calc_sapt_energy (coupled and uncoupled),
    • calc_property (the dipole and an AO matrix),
    • calc_densities,
    • the CPSCF solves.
  • The saving paths still read the dimer's Psi4 objects. With another backend they fail with the clear "belongs to the Psi4 backend" error of the Psi4 attributes. Tests now record them; moving them onto the backend is step 1.5:
    • the bundle metadata and save_densities,
    • the deprecated .npy matrices with their wavefunction files,
    • the runner's monomer cubes and .fchk files,
    • the TREXIO export.
  • Psi4's Qpq is symmetric in p and q only to round-off (8e-13 on He–H2). The diffuse virtual orbitals amplify that to about 2e-11 in the Hessian, by an amount that varies from run to run. The symmetry test therefore uses Qpq symmetrized; positive definiteness is checked on the recorded Qpq.

Out of scope (step 1.5): the analysis modules (evaluate, partition, cubes, forces), the saving paths above, and the log handler's print_out.

Tested:

  • New tests in tests/test_replay_backend.py, against the matched Psi4 dimer on He–H2:

    Largest gap Tolerance
    J/K 6e-13 1e-11
    Hessian products (MemDFJK in cphf_Hx against DiskDFJK in compute_jk) 1.1e-10 1e-9
    SAPT energies, all 26 terms 3e-15 1e-12
    Properties 5e-14 1e-12
    Induction amplitudes 5e-13 1e-11
    AO density matrices 4.4e-11 1e-9
  • Also new:

    • the replay copies the recorded arrays;
    • its Hessian is symmetric and positive definite;
    • the guard blocks Psi4 and gives it back;
    • the saving paths and the unsupported options fail as described;
    • Dimer.from_backend reproduces a dimer's arrays and induction amplitudes without running an SCF, and rejects anything that is not a Backend.
  • The full suite passes locally (612 passed, 2 skipped, 2 xfailed). The replay tests add about a minute, mostly one fixture that runs the full calculations on both dimers.

  • The docs build with -W.

  • output.dat of a He–H2 Dimer(...) (single-threaded) differs from main's only in timings and date stamps.

🤖 Generated with Claude Code

btyrcha and others added 4 commits November 29, 2026 15:18
…eady

The backend-neutral part of Dimer.__init__ (the SAPT arrays and the empty amplitude
caches) moves into _setup(backend). __init__ still builds a Psi4Backend inside Psi4's
timer, with the same output; from_backend takes any finished Backend, times the
construction with the backend's own timer and sets no geometry string. The replay
backend of the tests and later backends reach the SAPT code through it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ReplayBackend replays the arrays of a recorded RHF calculation and redoes only the
operations of the contract in NumPy: J/K from Qpq (df_jk, now shared with
matched_integrals) and the orbital-Hessian product from that J/K. It is a test double,
not a backend option. no_psi4_calls makes every call into psi4.core and Psi4's driver
fail, but print_out, which the package-wide log handler writes through.

The session fixtures matched_psi4_dimer (Psi4's J/K fitted in the DF_BASIS_SAPT basis,
options restored afterwards) and replay_dimer (Dimer.from_backend on its replay) set up
the comparison. On He-H2 the J/K agree to 6e-13 and the Hessian products to 1.1e-10;
the Hessian's symmetry is checked on Qpq symmetrized, as Psi4's is symmetric in p and q
only to round-off.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…kend

A dimer on the replay backend runs calc_sapt_energy (coupled and uncoupled),
calc_property (the dipole and an AO matrix), calc_densities and the CPSCF solves with
every call into Psi4 blocked, and matches the Psi4 dimer on the same fitted integrals:
on He-H2 to 3e-15 in the energies, 5e-14 in the properties, 5e-13 in the induction
amplitudes and 4.4e-11 in the AO density matrices. None of these calculations needed a
change in the package. A test of the guard checks that it blocks Psi4 and gives it back.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…support

With the replay backend, the saving paths that read Psi4 objects of the dimer fail with
the error of its Psi4 attributes: the bundle metadata and save_densities, the deprecated
.npy matrices with their wavefunction files, the runner's monomer cubes and .fchk files,
and the TREXIO export. Step 1.5 of the backend work moves them onto the backend; these
tests record them until then. The replay also rejects RKS calculations and CPSCF J/K
types, leaving the dimer's CPSCF state as it was.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@btyrcha
btyrcha merged commit dd5a898 into main Nov 30, 2026
4 checks passed
@btyrcha
btyrcha deleted the feature/replay-backend branch November 30, 2026 16:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant