Skip to content

AOBasis: the analysis functions on a basis contract, with Psi4 still the only backend - #16

Open
btyrcha wants to merge 12 commits into
mainfrom
feature/ao-basis
Open

btyrcha wants to merge 12 commits into
mainfrom
feature/ao-basis

Conversation

@btyrcha

@btyrcha btyrcha commented Nov 30, 2026 •

Copy link
Copy Markdown
Owner

Step 1.5a of the backend work: the analysis of AO matrices (point values, fragment operators, cubes, forces) works from a small contract, AOBasis, instead of a Psi4 basis set and molecule. Psi4 stays the only real backend, its objects are still accepted, and the numbers are the same.

  • AOBasis (prop_sapt/backends/base.py): the AOs of a code on their atoms. It has nbf, coordinates, charges, values(points), overlap(), potential(dms, points) and field_integrals(origin). molecular_grid(...) is optional and raises NotImplementedError by default, since the caller can bring another grid.
    • Backend.basis gives a backend's AOBasis, and raises NotImplementedError by default: energies, properties and density matrices don't need it.
    • AOBasis.value_blocks(points, block_size) gives the values of a loop over blocks of points, by default with one values call per block.
  • Psi4Basis (new prop_sapt/backends/psi4_basis.py): the Psi4 calls that were in evaluate, partition, cubes and forces. It makes Psi4's BasisFunctions once per loop over the points, as evaluate did on main: once per block made the AO values about 1.5 times slower.
  • as_ao_basis, atoms_of, atoms_and_basis (new prop_sapt/backends/ao_basis.py): the basisset and mol arguments of the analysis functions, an AOBasis or a Psi4 object. Psi4's basis is imported only for a Psi4 object.
  • The analysis modules (evaluate, partition, cubes, forces and the partition check in checks) load without Psi4; tests/test_imports.py imports them, and the SAPT core, in a Python without Psi4. Chunking, contractions, fragment weights and nuclear terms stay where they were. make_cube, calc_force_at_nuc and calc_force_nuc_nuc take an AOBasis in place of the molecule.
  • Dimer.basis: the basis of the dimer's backend, which make_cube, save_cube, evaluate_* and fragment_operators use. It raises a ValueError for a basis whose atoms are not the dimer's in the order of its fragments, or whose size is not the dimer's. The Hirshfeld weights take the monomer densities from the occupied orbitals instead of wfnA.Da(). With a damped SCF, Psi4's Da mixes the last two iterations; Hirshfeld operators from it differ from those of the orbitals by about what the SCF convergence leaves.

Changes a user can see:

  • make_cube and calc_force_at_nuc raise a TypeError for a basisset that is not a basis set. They ignored it and used one built from the BASIS option.
  • Output. One line on the basis set replaces Psi4's printout. Psi4's timer is around Dimer.make_cube, through backend.timer(), and no longer in prop_sapt.cubes.make_cube.
  • Each cube has its own copy of the atom coordinates.
  • ECPs. Dimer warns about a basis set with effective core potentials, and the usage guide lists them under the known limitations. propSAPT is all-electron: the monomer potentials leave out the ECP operators, and Psi4 resets the dimer's nuclear charges to the full ones when it builds the fitting basis sets. So the electrostatic energy is wrong, by 9.3 Eh in ELST1 for Xe–He/def2-SVP at 6 bohr, as on main, which doesn't warn. The warning is all this PR does about it.

After a code review of this PR, the last commits fix three bugs, with tests that failed before:

  • The analysis modules still imported Psi4, through psi4_basis.py; the old import test checked direct imports only.
  • Psi4Basis kept its BasisFunctions as long as the dimer. Two threads analysing one dimer got each other's values (about 1 call in 400 with two threads), and a large chunk_size kept its memory after the call.
  • A backend's basis on the atoms in another order gave swapped fragment weights without an error.

Known limit: matrices of one code with the basis of another give wrong numbers without an error if the sizes match but the AO orders differ. Dimer.basis avoids this. The tests check basis.overlap() against the backend's S.

Out of scope (step 1.5b): the saving paths (bundles, .npy matrices, the runner's monomer cubes and .fchk files, the TREXIO export), the shell data they need, and the log handler's print_out. Their pinned error tests are unchanged.

Tested:

  • Against main, on He–H2/aug-cc-pVTZ, single-threaded: 34 analysis arrays (point values, quadrature grid, Becke and Hirshfeld operators, cubes, forces) and 4 cube files are bitwise equal. output.dat differs only in the lines described above, timings and date stamps.

  • Without Psi4. tests/s_gaussian_basis.py is a test double of AOBasis, not a basis option: contracted s functions in NumPy, as STO-3G has on H and He. A replay dimer with it runs the following with every Psi4 call blocked (no_psi4_calls), compared with Psi4:

    Tolerance
    Every operation of the contract 1e-12
    Point values, cubes, fragment operators, forces 1e-12
    Density cubes written by calc_densities 1e-9
  • Psi4Basis against Psi4 itself (tests/test_ao_basis.py): compute_phi, the potential integrals of a unit charge, the electric field as the gradient of the potential, all elements of a non-symmetric matrix in potential, and the charges of ECP and ghost atoms.

  • The review fixes change no numbers: 9 of the arrays above are bitwise equal before and after them. The memory kept after a 300,000-point chunk went from 143 MB to 8 MB.

  • Eight deliberate bugs in the new code, and five more in the review's tests, were each caught by the tests.

  • The full suite passes locally (682 passed, 2 skipped, 2 xfailed), and the tests of the touched modules and black pass on each commit.

  • The docs build without warnings.

🤖 Generated with Claude Code

btyrcha and others added 12 commits November 30, 2026 19:45
An AOBasis is the AOs of a code on their atoms, with the values and integrals the analysis
of AO matrices needs: values at points, overlap, potential and electric field integrals,
and, if the code has one, a molecular quadrature grid. Psi4Basis implements it with the
Psi4 calls of evaluate, partition, cubes and forces, which move onto it next. Psi4 basis
sets are still taken, through as_ao_basis. A backend gives its basis as `basis`, which
raises NotImplementedError by default.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
evaluate_* and check_partition_of_unity take an AOBasis or a Psi4 basis set and no longer
call Psi4 themselves: the basis values, the potential integrals and the atoms come from
the AO basis. Chunking, contractions and the potential of the nuclei stay where they were.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
molecular_grid, hirshfeld_fragment_weights and FragmentOperators.from_grid take an AOBasis
or a Psi4 basis set. The grid comes from the code of the basis set, Psi4's DFTGrid for
Psi4; its defaults and checks stay in partition.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
make_cube, calc_force_at_nuc and calc_force_nuc_nuc take an AOBasis in place of the Psi4
molecule, and make_cube and calc_force_at_nuc as the basis set too. A basis set of another
type raises TypeError: it was ignored, and one built from the BASIS option instead. The
basis set is logged in one line, where Psi4 printed it, and Psi4's timer is around
Dimer.make_cube, through the backend, rather than in make_cube.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
make_cube, evaluate_* and fragment_operators use the basis of the dimer's backend, where
they took Psi4's basis set and molecule, and the Hirshfeld weights take the monomer
densities from the occupied orbitals, where they read Psi4's wavefunctions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The s-Gaussian basis is a test double of the AO basis contract: contracted s functions in
NumPy, as STO-3G has on H and He. A replay dimer with it runs cubes, point values, fragment
operators, forces and the density cubes of calc_densities with no call into Psi4, and
matches Psi4.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
propSAPT is all-electron: the monomer potentials leave out the ECP
operators, and Psi4 resets the dimer's nuclear charges to the full ones
when it builds the fitting basis sets, so the electrostatic energy is
wrong (Xe-He/def2-SVP at 6 bohr: ELST1 +9.3 Eh, on main too).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
From the review of PR #16: the charges of ECP and ghost atoms are those
of `Molecule.to_arrays`; both codes take all elements of a non-symmetric
matrix in `potential`; with a damped SCF, `Dimer.fragment_operators`
takes the densities of the orbitals, and Psi4's `Da` gives Hirshfeld
operators within what the SCF convergence leaves.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
They imported it through `backends/psi4_basis.py`, which the review of
PR #16 found: `as_ao_basis`, `atoms_of`, `atoms_and_basis` and the
`BasisLike`/`AtomsLike` aliases move to `backends/ao_basis.py`, which
imports Psi4's basis only for Psi4 objects, and recognises those only
where Psi4 has been imported. `tests/test_imports.py` imports the SAPT
core and the analysis modules in a Python without Psi4.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`Psi4Basis` kept its `BasisFunctions` for its lifetime, as long as the
dimer's: two threads analysing one dimer overwrote each other's values,
and a large chunk kept its memory after the call (review of PR #16).
`AOBasis.value_blocks` computes the values of a loop over blocks of
points, by default with one `values` call per block; `Psi4Basis` makes
its objects for the loop, as `evaluate` did on main. The results are
bitwise the same.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The fragment weights map grid points to atoms by index, and the nuclear
terms and forces read the atoms by index too: a backend's basis on the
atoms in another order gave wrong fragment operators without an error
(review of PR #16). `Dimer.basis` now raises for a basis whose atoms are
not `fragments.coordinates` in their order, or whose size is not the
dimer's, and the contract says so.

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

This branch has not been deployed

No deployments
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