Skip to content
Merged
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
8 changes: 6 additions & 2 deletions AI-USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ The short version, for disclosures; the log below has the detail.
Copilot code review on some pull requests.
- **What the AI assistance did.** Implementation of new features and refactoring, tests,
documentation, CI and packaging, and reviews of code written by the authors. It also
diagnosed numerical discrepancies, and implemented corrections to the
orbital-relaxation terms of the second-order properties.
diagnosed numerical discrepancies, implemented corrections to the
orbital-relaxation terms of the second-order properties, and researched external file
formats and viewers (TREXIO, VIAMD) for new features.

## Log

Expand Down Expand Up @@ -152,8 +153,11 @@ The short version, for disclosures; the log below has the detail.
### 2026-11-14

- Added the second-order properties preprint (arXiv:2611.07993) to the citations: `CITATION.cff` (under `references`, schema-validated), the README, and a new Citing section in the docs with BibTeX entries for both papers.
- TREXIO export of the density changes for interactive viewing in VIAMD, after an investigation of the format and the viewer (its source code) with a prototype: `density_natural_orbitals`, `export_trexio` with `calc_densities(save_trexio=True)`, the runner's `save_trexio` setting and `prop-sapt export trexio`, the `trexio` extra, tests against Psi4's AOs through a TREXIO-spec evaluator, docs and an example; exact to round-off on He–H2, and a density exported back from VIAMD matched propSAPT's to 1.2e-9 rms.

### 2026-11-16

- Prepared the 0.5.0 release (version, `CITATION.cff`, install commands, changelog section), after checking that its package code is the commit the `~/densities` recalculation ran with.
- Fixed density file names that collided on case-insensitive file systems (`Ind`/`ind`, `Disp`/`disp`): a file-name helper with a collision check in `prop_sapt.artifacts`, `Ind`/`Disp` files named `ind-total`/`disp-total`, with tests, an example, docs and a changelog entry.
- Rebased the TREXIO export PR onto `main` after the 0.5.0 release (its changelog entries moved to the Unreleased section) and after the density file-name fix, naming the TREXIO files like the cubes through `density_file_names`, with a test of the collision check.
- Addressed the Copilot review of the TREXIO PR: documented that the bundle's cube and TREXIO entries record the files requested, which are written after it, and made `export_trexio` refuse a `relative_cutoff` outside [0, 1), with tests.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ requests of the private repository propSAPT was developed in before it was publi

## [Unreleased]

### Added

- TREXIO files of the density changes, to view them interactively in
[VIAMD](https://github.com/scanberg/viamd): `calc_densities(save_trexio=True)`, the
`save_trexio` setting of the `densities` task, and `export_trexio` and
`prop-sapt export trexio` for existing `densities.npz` bundles. Each file holds the
natural orbitals of a density change, and VIAMD shows the change as its alpha - beta
density. Needs the `trexio` package (the `trexio` extra). See the
[usage guide](https://btyrcha.github.io/propSAPT/usage.html#viewing-density-changes-in-viamd).
- `density_natural_orbitals`: the natural orbitals of the density changes, a few of which
usually carry most of a change, in the spirit of NOCV.

### Fixed

- The cube and `.npy` files of the `Ind` and `Disp` densities are now named
Expand Down
16 changes: 16 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ Density Artifacts
:undoc-members:
:show-inheritance:

Natural Orbitals of Density Changes
-----------------------------------

.. automodule:: prop_sapt.natural_orbitals
:members:
:undoc-members:
:show-inheritance:

TREXIO Export
-------------

.. automodule:: prop_sapt.trexio_export
:members:
:undoc-members:
:show-inheritance:

Invariant Checks
----------------

Expand Down
2 changes: 2 additions & 0 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Scripts
:func:`~prop_sapt.artifacts.load_densities`; run :file:`save_mo_matrices.py` first
* :file:`read_subtract_save.py` - cube arithmetic on the cubes that :file:`water_dimer.py`
writes to :file:`water-dimer-cubes/`; run :file:`water_dimer.py` first
* :file:`export_trexio.py` - TREXIO files of the density changes of Ne–H2, to view them in
VIAMD, and the natural orbitals of its polarisation; needs ``trexio``
* :file:`run_config.yaml` - a commented configuration for ``prop-sapt run``

For example:
Expand Down
2 changes: 2 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Features
of any Psi4 method, e.g. MP2 or CCSD (a supermolecular reference)
* Densities, orbitals and electrostatic potentials on grids, fragment populations, and
:file:`.cube` file utilities
* The natural orbitals of the density changes, and TREXIO files of them for interactive
views in VIAMD
* A command-line runner, ``prop-sapt run``, for production calculations from a
configuration file

Expand Down
4 changes: 4 additions & 0 deletions docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ basis sets through packages that propSAPT itself does not need:
* the ccRepo source, installed from git:
``pip install git+https://github.com/Sheffield-Theoretical-Chemistry/ccrepo_utility colorlog tqdm requests``.

Writing the density changes into TREXIO files, to view them in VIAMD (see
:ref:`the usage guide <viamd>`), needs the ``trexio`` package: ``pip install trexio``, or
the ``trexio`` extra. The conda environment of :file:`prop-sapt.yaml` includes it.

The ``docs`` extra installs Sphinx and its theme, to build this documentation
(``sphinx-build -W -b html docs docs/_build/html``).

Expand Down
98 changes: 96 additions & 2 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ basis, into one self-describing bundle, :file:`densities.npz`. It also holds the
wavefunctions and the metadata: the geometry and its fragments, the basis set and the
fitting bases, the method, the Psi4 and propSAPT options, the versions, and the
conventions. The basis sets and the Psi4 options are those the dimer was built with.
With :python:`save_cubes` or :python:`save_trexio` the metadata also lists the cube and
TREXIO files requested. They are written after the bundle, so that a failed or
interrupted export cannot lose the matrices; a file listed may then be missing, and TREXIO
files can be written again from the bundle (see :ref:`viamd`).
Nothing in it is pickled. Read it back with :func:`~prop_sapt.artifacts.load_densities`, which checks the
conventions and, if asked, that the matrices belong to a geometry, a dimer or a basis set:

Expand All @@ -260,6 +264,94 @@ still written with :python:`matrix_format="npy"`, but it is deprecated, and so i
``compress_matrices`` argument.


.. _viamd:

Viewing Density Changes in VIAMD
--------------------------------

`VIAMD <https://github.com/scanberg/viamd>`_ shows electron densities and orbitals
interactively. propSAPT writes its density changes for it into
`TREXIO <https://github.com/TREX-CoE/trexio>`_ files, one per density, named
:file:`delta_dm_<key>.h5` like the cube files. Writing them needs the ``trexio`` package
(see :doc:`installation`). They can be written with the calculation, from its matrices, or
from a bundle:

.. code-block:: python

from prop_sapt import calc_densities, export_trexio

densities = calc_densities(dimer, save_trexio=True, trexio_dir="trexio")
export_trexio(densities, "trexio", dimer=dimer, keys=["total", "pol"])
export_trexio("results/geo1/densities.npz", "trexio")

A bundle can also be converted from the command line:

.. code-block:: bash

prop-sapt export trexio results/geo1/densities.npz --keys total pol Ind Disp

In a run configuration, ``densities: {save_trexio: true}`` writes the files into the
output directory next to the cubes. To convert a bundle, Psi4 rebuilds its basis set by
name, so a basis set from ``basis_dirs`` or ``basis_overrides`` has to be made available
to Psi4 first, as for :meth:`~prop_sapt.artifacts.DensitySet.wavefunction`.

**What a file holds.** TREXIO, like VIAMD, gives a density by orbitals and their
occupations, so each file holds the natural orbitals of its density change (see
:ref:`natural-orbitals`). VIAMD shows a density with signs only as the difference of the
alpha and beta densities. The orbitals of positive occupation are therefore written as
alpha orbitals, and those of negative occupation as beta orbitals: the alpha - beta density
of a file is the total density change, as in the cube files.

**Viewing a file in VIAMD:**

#. Open the file. Add a representation of type *Electronic Structure*, with source
*Electron Density* and spin *Difference*. Its isosurfaces show in two colours where the
density grows and where it drops.
#. Set the isovalue. VIAMD draws density isosurfaces at plus and minus the *square* of the
isovalue, and density changes are small: start near the bottom of the slider (0.01
draws the surfaces at 1e-4). Ctrl+click the slider to type a smaller value.
#. To see single natural orbitals, set the source to *Molecular Orbital*. Spin *Alpha*
lists the orbitals that gain density and *Beta* those that lose it, the largest first.

**Limits:**

- The *Total* spin means nothing for these files.
- VIAMD reads basis sets up to g functions. It refuses the files of a basis set with h
functions (e.g. aug-cc-pV5Z), and propSAPT warns when writing them.
- VIAMD's own cube export of a density puts the grid origin in its header half a voxel
away from where it evaluated the density.

.. _natural-orbitals:

Natural orbitals of the density changes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:func:`~prop_sapt.natural_orbitals.density_natural_orbitals` gives the natural orbitals of
the density changes on their own, without trexio. It works on the matrices of a
calculation and on a bundle:

.. code-block:: python

from prop_sapt import density_natural_orbitals

orbitals = density_natural_orbitals(densities, dimer, keys=["pol_A", "Disp"])
orbitals["pol_A"].occupations # one spin, largest first
orbitals["pol_A"].coefficients # AO coefficients, one orbital per column

**How to read them:**

- **Occupations:** a monomer's natural orbitals are orthonormal, and their occupations sum
to zero, because a density change moves charge without changing the number of
electrons. Orbitals of positive occupation show where charge goes, and those of negative
occupation where it comes from.
- **Few orbitals matter:** a few orbitals usually carry most of a density change, so they
picture it in the spirit of natural orbitals for chemical valence (NOCV). First-order
polarisation and exchange come in +/- pairs.
- **Dimer keys:** a dimer key such as ``"Disp"`` lists the orbitals of both monomers. The
two sets are not orthogonal to each other, but their density is exactly the sum.
- **On a grid:** :meth:`~prop_sapt.molecule.Dimer.make_cube` with
:python:`obj_type="orbital"` puts a single orbital on a grid.

Finite-Field Properties
-----------------------

Expand Down Expand Up @@ -376,7 +468,8 @@ recording the resolved configuration, the Psi4 options, the package versions and
fragments of the geometry. The ``densities`` task saves its matrices into
:file:`densities.npz` (``matrix_format: npz``, the default), whose metadata also holds the
run configuration and the basis overrides applied; ``matrix_format: npy`` (deprecated)
writes bare :file:`.npy` files.
writes bare :file:`.npy` files. With ``save_trexio: true`` it also writes TREXIO files of
the densities, to view them in VIAMD (see :ref:`viamd`).
The command exits with status 0 on success, 1 if a calculation failed and 2 for an invalid
configuration, so it can be used directly in batch scripts, e.g. in a SLURM array job:

Expand Down Expand Up @@ -715,7 +808,8 @@ See the Jupyter notebooks in the ``examples/`` directory:
* ``visualise_orbital.ipynb`` - Visualize molecular orbitals
* ``visualise_density.ipynb`` - Visualize density matrices

For more examples, see the :doc:`examples` section.
For interactive views of the density changes and their natural orbitals in VIAMD, see
:ref:`viamd`. For more examples, see the :doc:`examples` section.

.. _known-limitations:

Expand Down
2 changes: 2 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ finite field, and use aug-cc-pVTZ with aug-cc-pVQZ fitting sets.
`load_densities`. Run `save_mo_matrices.py` first.
- **`read_subtract_save.py`**: cube arithmetic on the cubes that `water_dimer.py` writes to
`water-dimer-cubes/`. Run `water_dimer.py` first.
- **`export_trexio.py`**: TREXIO files of the density changes of Ne–H2, to view them in VIAMD,
and the natural orbitals of its polarisation. Needs `trexio` (`pip install trexio`).
- **`run_config.yaml`**: a commented configuration for the `prop-sapt run` command-line runner.

## Notebooks
Expand Down
76 changes: 76 additions & 0 deletions examples/export_trexio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"""
This example calculates the interaction-induced densities of a Ne-H2 dimer and writes them
into TREXIO files, to view them in VIAMD (https://github.com/scanberg/viamd), and prints
the leading natural orbitals of the first-order polarisation of each monomer.

It needs the trexio package (pip install trexio). In VIAMD, open e.g.
trexio_ne_h2/delta_dm_total.h5 and show the Electron Density with spin Difference: the
alpha - beta density of a file is its density change.
"""

import numpy as np
import psi4
from prop_sapt import Dimer, calc_densities, density_natural_orbitals
from prop_sapt.utils import CalcTimer

# specify geometry in Psi4 format
GEO = """
symmetry c1
no_com
no_reorient
units bohr
0 1
Ne -2.500000000 0.000000000 0.000000000
--
0 1
H 1.775500000 0.000000000 0.000000000
H 3.224500000 0.000000000 0.000000000
"""

# specify memory and threads
MEMORY = "4 GB"
THREADS = 4

# specify basis sets
BASIS = "aug-cc-pvdz"
DF_BASIS = "aug-cc-pvdz"

# specify options
OPTIONS = {
# "option": "value",
"basis": BASIS,
"DF_BASIS_SCF": DF_BASIS + "-jkfit",
"DF_BASIS_SAPT": DF_BASIS + "-ri",
"scf_type": "df",
}

# specify output filename and the directory of the TREXIO files
OUTPUT_FILE_PATH = "output.dat"
TREXIO_DIR = "trexio_ne_h2"

if __name__ == "__main__":

with CalcTimer("Example propSAPT TREXIO export"):

### Psi4 options
psi4.set_memory(MEMORY)
psi4.set_num_threads(THREADS)
psi4.core.set_output_file(OUTPUT_FILE_PATH, False)
psi4.set_options(OPTIONS)

### Initialise prop_sapt.Dimer object
dimer = Dimer(GEO)

### Calculate the density matrices and write a TREXIO file of each
densities = calc_densities(dimer, save_trexio=True, trexio_dir=TREXIO_DIR)

### The natural orbitals of the polarisation: a +/- pair per occupied orbital
orbitals = density_natural_orbitals(
densities, dimer, keys=["pol_A", "pol_B"], cutoff=1e-8
)
for key, natural in orbitals.items():
occupations = np.array2string(natural.occupations, precision=6)
print(f"{key}: {len(natural)} natural orbitals, occupations {occupations}")

### End calculations
psi4.core.clean()
3 changes: 2 additions & 1 deletion examples/run_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,5 @@ tasks:
# property: {prop: dipole, results: property_results.csv}
# interaction-induced density matrices (one densities.npz bundle, see
# prop_sapt.load_densities) and their total-density cubes (delta_dm_*.cube)
densities: {orbital_basis: AO, save_cubes: true, save_matrices: true, matrix_format: npz}
densities: {orbital_basis: AO, save_cubes: true, save_matrices: true, matrix_format: npz,
save_trexio: false} # true: TREXIO files for VIAMD, needs `pip install trexio`
1 change: 1 addition & 0 deletions prop-sapt.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ dependencies:
- ipywidgets
- scikit-image
- pythreejs
- trexio
4 changes: 4 additions & 0 deletions prop_sapt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from .properties import calc_property
from .densities import calc_density_matrix, calc_densities, DENSITY_COMPONENTS
from .artifacts import load_densities
from .natural_orbitals import density_natural_orbitals
from .trexio_export import export_trexio
from .sapt_driver import calc_sapt_energy
from .finite_field import finite_field_sapt
from .options import set_propsapt_options, get_propsapt_options, reset_propsapt_options
Expand All @@ -16,6 +18,8 @@
"calc_densities",
"DENSITY_COMPONENTS",
"load_densities",
"density_natural_orbitals",
"export_trexio",
"calc_sapt_energy",
"finite_field_sapt",
"set_propsapt_options",
Expand Down
Loading
Loading