Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ requests of the private repository propSAPT was developed in before it was publi
usually carry most of a change, in the spirit of NOCV.
- `Dimer.from_backend`: a dimer of a backend calculation that has run already, such as a
`Psi4Backend` built beforehand.
- `Dimer.basis`: the dimer's basis set for the functions of `evaluate`, `partition`,
`cubes` and `forces`, which still take Psi4 basis sets. `make_cube`,
`calc_force_at_nuc` and `calc_force_nuc_nuc` take it in place of the Psi4 molecule.

### Changed

Expand All @@ -41,9 +44,18 @@ requests of the private repository propSAPT was developed in before it was publi
`dimer.naux`, the number of functions of the SAPT fitting basis: `nao_aux` counted
Cartesian functions even for a spherical basis. `changed_psi4_options` moved
from `prop_sapt.utils` to `prop_sapt.backends.psi4_backend`.
- `make_cube` and `calc_force_at_nuc` write one line on the basis set into the output, where
Psi4 printed it. Psi4's timings are around `Dimer.make_cube` and no longer come with
`prop_sapt.cubes.make_cube`.
- `Dimer` warns about a basis set with effective core potentials (ECPs), which propSAPT
does not support. See the
[usage guide](https://btyrcha.github.io/propSAPT/usage.html#known-limitations).

### Fixed

- `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, whose functions
the matrices may not be in.
- The cube and `.npy` files of the `Ind` and `Disp` densities are now named
`delta_dm_ind-total*` and `delta_dm_disp-total*`. Their old names differ from those of
`ind` and `disp` only in case, so on a case-insensitive file system (macOS and Windows by
Expand Down
8 changes: 8 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ Backends
:members:
:show-inheritance:

.. automodule:: prop_sapt.backends.ao_basis
:members:
:show-inheritance:

.. automodule:: prop_sapt.backends.psi4_basis
:members:
:show-inheritance:

Properties
----------

Expand Down
37 changes: 20 additions & 17 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ These attributes are read-only, since replacing one would not change the calcula
:meth:`Dimer.from_backend <prop_sapt.molecule.Dimer.from_backend>` makes a dimer of a
backend that has run already, such as a ``Psi4Backend`` built beforehand.

``dimer.basis`` is the dimer's basis set as the analysis functions below take it (an
:class:`~prop_sapt.backends.base.AOBasis`): point evaluation, fragment operators, cubes and
forces. They take Psi4 basis sets too.

The fragment structure is kept on the dimer as ``dimer.fragments``
(:class:`~prop_sapt.fragments.DimerFragments`): the atoms of monomers A and B and of any
extra fragments, their charges and multiplicities, the ghost atoms, and the coordinates in
Expand Down Expand Up @@ -613,25 +617,22 @@ To compute Hellmann-Feynman forces on nuclei from one-particle AO density matric

from prop_sapt.forces import calc_force_at_nuc, calc_force_nuc_nuc

mol = dimer.get_psi4_molecule()
basisset = dimer.get_psi4_basisset()

# Force on nucleus 0 from the interaction-induced density of monomer A
force = calc_force_at_nuc(mol, 2 * rho_A, atidx=0, basisset=basisset, add_nuclear=False)
force = calc_force_at_nuc(dimer.basis, 2 * rho_A, atidx=0, add_nuclear=False)

# Pass multiple density matrices — integrals are computed only once
forces = calc_force_at_nuc(
mol, [2 * rho_A_elst, 2 * rho_A_exch], atidx=0, basisset=basisset, add_nuclear=False
dimer.basis, [2 * rho_A_elst, 2 * rho_A_exch], atidx=0, add_nuclear=False
)

# Nuclear-nuclear repulsion only (no electron density needed)
force_nuc = calc_force_nuc_nuc(mol, atidx=0)
force_nuc = calc_force_nuc_nuc(dimer.basis, atidx=0)

All forces are in atomic units (Hartree/Bohr). The nuclear-nuclear repulsion contribution
is added by default; pass ``add_nuclear=False`` to obtain the electronic part only, as for
interaction-induced densities above. The matrices must be in the AO basis of ``basisset``;
without it, a basis set is built for ``mol`` from the current ``BASIS`` option, so pass the
dimer's for its density matrices. As elsewhere, no spin factor is applied: pass
interaction-induced densities above. A Psi4 molecule in place of ``dimer.basis`` needs the
basis set the matrices are in as ``basisset=``, since without it one is built from the
current ``BASIS`` option. As elsewhere, no spin factor is applied: pass
``2 * rho`` for the matrices of :func:`~prop_sapt.densities.calc_densities`.

Evaluating at Arbitrary Points
Expand All @@ -647,7 +648,7 @@ points without the last axis.
import numpy as np
from prop_sapt.evaluate import evaluate_density, evaluate_orbital, evaluate_esp

basisset = dimer.get_psi4_basisset()
basisset = dimer.basis # or a Psi4 basis set

# density along the x axis
line = np.zeros((200, 3))
Expand All @@ -661,14 +662,14 @@ points without the last axis.
C = dimer.wfnA.Ca().np
psi = evaluate_orbital(C[:, :3], line, basisset) # shape (3, 200)

# ESP of the electrons; nuclei=True adds the nuclei of the basis set's molecule
# ESP of the electrons; nuclei=True adds the nuclei of the basis set's atoms
esp = evaluate_esp(rho_A, line, basisset)

The same functions are :class:`~prop_sapt.molecule.Dimer` methods that use the dimer basis set:
``dimer.evaluate_density(rho, points)``, ``dimer.evaluate_orbital(C, points)`` and
``dimer.evaluate_esp(rho, points)``.

* Matrices must be in the AO basis of ``basisset``, in Psi4 AO order.
* Matrices must be in the AO basis of ``basisset``, in its AO order (Psi4's).
* No spin factor is applied. The density matrices returned by
:func:`~prop_sapt.densities.calc_densities` follow the ``Da`` convention, so pass ``2 * rho``
for the total (RHF) density, as the density cubes do.
Expand Down Expand Up @@ -727,15 +728,15 @@ and the electrons any density matrix :math:`D` puts on :math:`X` are the trace
Fragment weights from any other partitioning (e.g. LISA or MBIS weights from
horton_part) work as well: give them at the points of the grid they were computed on,
with that grid's quadrature weights. Only these arrays are needed; the basis functions
are evaluated by Psi4 in the given basis set:
are those of the given basis set:

.. code-block:: python

from prop_sapt.partition import FragmentOperators

# points (n, 3) in bohr, quadrature weights (n,), fragment weights w_A, w_B (n,)
ops = FragmentOperators.from_grid(
points, weights, {"A": w_A, "B": w_B}, dimer.get_psi4_basisset()
points, weights, {"A": w_A, "B": w_B}, dimer.basis
)
q_A = ops.charge("A", 2 * rho_pol)

Expand Down Expand Up @@ -790,8 +791,8 @@ It supports three matrix types:
cube = dimer.make_cube(2 * rho_B, grid_step=0.1)

:func:`prop_sapt.cubes.make_cube` does the same for any Psi4 molecule; pass the basis set
the matrices are in as ``basisset=``, e.g. ``dimer.get_psi4_basisset()``, since without it
a basis set is built from the current ``BASIS`` option. A grid from
the matrices are in as ``basisset=``, e.g. ``dimer.basis``, since without it a basis set is
built from the current ``BASIS`` option. A grid from
:func:`prop_sapt.cubes.prepare_grid` can be reused across calls with ``grid=``.

Working with cubes
Expand Down Expand Up @@ -869,6 +870,8 @@ Known limitations
* **Closed-shell references only.** The monomers must be closed-shell singlets with an
``RHF`` or ``RKS`` reference; unrestricted references are not implemented.
* **All-electron only.** Frozen core is not supported, and :class:`~prop_sapt.molecule.Dimer`
rejects the ``FREEZE_CORE`` option.
rejects the ``FREEZE_CORE`` option. It warns about a basis set with effective core
potentials (ECPs): the SCFs use them, but the potentials in the SAPT terms do not, so the
electrostatic energies are wrong and the other results less accurate.
* **Finite-field files.** :func:`~prop_sapt.finite_field.finite_field_sapt` writes the SAPT energies of
every field into the working directory, :file:`results-<axis>-<sign><field>.csv`.
4 changes: 2 additions & 2 deletions prop_sapt/backends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
for propSAPT, see `prop_sapt.backends.base` for the contract. Psi4
(`prop_sapt.backends.psi4_backend.Psi4Backend`) is the only one so far."""

from .base import MONOMERS, Backend, MonomerReference, check_reference
from .base import MONOMERS, AOBasis, Backend, MonomerReference, check_reference

__all__ = ["MONOMERS", "Backend", "MonomerReference", "check_reference"]
__all__ = ["MONOMERS", "AOBasis", "Backend", "MonomerReference", "check_reference"]
111 changes: 111 additions & 0 deletions prop_sapt/backends/ao_basis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
"""The `mol` and `basisset` arguments of propSAPT's analysis functions
(`prop_sapt.evaluate`, `prop_sapt.partition`, `prop_sapt.cubes`, `prop_sapt.forces`): an
AO basis of any code, or a Psi4 molecule or basis set. Psi4 is imported only for the
latter, so the analysis modules load without it.
"""

import sys
from typing import TYPE_CHECKING, Union

import numpy as np

from .base import AOBasis

if TYPE_CHECKING:
import psi4

#: The basis set an analysis function takes, see `as_ao_basis`.
BasisLike = Union[AOBasis, "psi4.core.BasisSet"]
#: The atoms an analysis function takes, see `atoms_of`.
AtomsLike = Union[AOBasis, "psi4.core.Molecule"]


def as_ao_basis(basisset: BasisLike) -> AOBasis:
"""
The AO basis of an analysis function's `basisset` argument: an `AOBasis` as it is, a
Psi4 basis set as a `Psi4Basis`.

Raises:
TypeError: If `basisset` is neither.
"""

if isinstance(basisset, AOBasis):
return basisset

if _is_psi4(basisset, "BasisSet"):
from .psi4_basis import Psi4Basis

return Psi4Basis(basisset)

raise TypeError(
"`basisset` should be of type `psi4.core.BasisSet` or "
f"`prop_sapt.backends.AOBasis` was `{type(basisset)}`!"
)


def atoms_of(mol: AtomsLike) -> tuple[np.ndarray, np.ndarray]:
"""
The atoms of an analysis function's `mol` argument: a Psi4 molecule, whose geometry
is updated first (`Molecule.update_geometry`), or an AO basis.

Raises:
TypeError: If `mol` is neither.

Returns:
tuple[np.ndarray, np.ndarray]: Coordinates in bohr, (natom, 3), and charges,
(natom,), 0 for ghost atoms.
"""

if isinstance(mol, AOBasis):
return mol.coordinates, mol.charges

if _is_psi4(mol, "Molecule"):
from .psi4_basis import molecule_arrays

mol.update_geometry()
return molecule_arrays(mol)

raise TypeError(
"`mol` should be of type `psi4.core.Molecule` or "
f"`prop_sapt.backends.AOBasis` was `{type(mol)}`!"
)


def atoms_and_basis(
mol: AtomsLike, basisset: BasisLike | None
) -> tuple[np.ndarray, np.ndarray, AOBasis]:
"""
The atoms and the AO basis of an analysis function's `mol` and `basisset` arguments.

Args:
mol (AOBasis | psi4.core.Molecule): The atoms, see `atoms_of`.
basisset (AOBasis | psi4.core.BasisSet | None): The basis set. None is `mol`
itself if that is an AO basis, and otherwise the basis set of the current
`BASIS` option, built for the molecule.

Raises:
TypeError: If `mol` or `basisset` is of another type.

Returns:
tuple[np.ndarray, np.ndarray, AOBasis]: Coordinates in bohr, (natom, 3), and
charges, (natom,), of the atoms, and the AO basis.
"""

coordinates, charges = atoms_of(mol)
if basisset is None:
if isinstance(mol, AOBasis):
return coordinates, charges, mol
from .psi4_basis import option_basis

return coordinates, charges, option_basis(mol)

return coordinates, charges, as_ao_basis(basisset)


def _is_psi4(argument: object, name: str) -> bool:
"""Whether `argument` is of the class `name` of `psi4.core`. Nothing is where Psi4
has not been imported."""

psi4 = sys.modules.get("psi4")

return psi4 is not None and isinstance(argument, getattr(psi4.core, name))
Loading