Psi4 Project Logo

Plugins: Adding New Functionality to Psi4

Modular Approach to Development

The redesign of Psi4 into a single-executable changed the way that code development is done. The standalone nature of modules in previous versions of Psi made their development very easy, as new functionality could be implemented almost as a standalone executable, which could easily be ported into the Psi code when completed. The new design specifies that these modules are now libraries, not separate executables, which are linked into the main Psi executable. The single-executable design is conducive to code reuse, as it allows common tasks to be implemented as a class instead of a module; the functionality can then be easily obtained throughout the code by creating objects as required. Examples of this are the LibMints class, which provides similar functionality to the old cints module, and LibTrans, which replaces the old transqt code. When codes are wrapped in a library, they should be placed into psi4/src/lib, and codes that resemble modules belong in psi4/src/bin.

The single-executable design leads to a somewhat cumbersome development cycle, since every time a change is made, one must compile the code, archive it into a library, and then re-link the code into the main executable. It’s also daunting to new developers if they’re required to learn the structure of the source tree, executable initialization code, and makefile systems in the existing code in order to add new features, which was never a problem with previous versions due to the independent nature of the modules. To overcome these problems, Psi4 now has a useful plugin feature. This allows codes to be developed as standalone entities, which are compiled independently of the Psi source, but can still link against Psi’s vast library. The plugins can be loaded at run-time from any location.

Creating a New Plugin

Psi4 can create basic plugins for you and automatically tailor them to your compilation environment. To create a basic plugin, run the following while replacing myplugin with the name of your great code. If the name you provide is not valid, Psi4 will complain.

>>> psi4 --new-plugin myplugin

Psi4 will create a new directory with the name you specify for the plugin. In this example, a directory named myplugin will be created.

All you need to do is cd into the directory and type make. Then execute psi4 in the directory on the default input file.

Psi4 comes with a few templates that provide an excellent starting point. These include code that demonstrates AO, MO, and SO integrals. Use one of the following commands that meets your needs:

>>> psi4 --new-plugin myplugin +aointegrals
>>> psi4 --new-plugin myplugin +mointegrals
>>> psi4 --new-plugin myplugin +sointegrals
>>> psi4 --new-plugin myplugin +wavefunction
>>> psi4 --new-plugin myplugin +scf
>>> psi4 --new-plugin myplugin +ambit

Several stable sample plugin directories are available to consult in the psi4/plugins directory. Other plugin directories can be used as models but are in active development. For documentation on plugin modules, see Available Plugins.

Creating a New Plugin Using a Conda Pre-compiled Binary

Psi4 plugins can also be created using Conda for both Psi4 binary and development environment. To compile a plugin with the default Makefile, it is necessary to have the gcc compiler installed in the Conda distribution or environment (below, $PSI4CONDA) used to run Psi4.

# prepare
>>> bash
>>> export PATH=$PSI4CONDA/bin:$PATH  # usually already done from psi4 installation
>>> cd "$(dirname $(which psi4))"/..  # move into distribution/environment directory, $PSI4CONDA
>>> conda install gcc                 # install compilers into expected place

# check (yes, next line gives empty result. yes, LD_LIBRARY_PATH irrelevant)

>>> which python psi4 gcc

# create and compile plugin
>>> psi4 --new-plugin testplugin      # generate new plugin
>>> cd testplugin                     # move into plugin directory
>>> make                              # compile the plugin to product
>>> psi4                              # run sample input.dat
Attention! This SCF may be density-fitted.

Please note that the conda distribution must be in $PATH or the conda enviroment must be activated before compilation and execution of plugins created using this procedure.

Files in a Plugin Directory

In addition to the main file, a fresh plugin directory contains the following files

  • Makefile — Makefile for the directory. As long as you are the only user of the plugin, this should not need editing. After any change to the plugin C++ code, make must be run in the plugin directory to recompile the executable, but recompiling the main Psi4 code is not necessary.
  • input.dat — Sample input file for the plugin. Since the file makes the plugin directory look like a Python module, the plugin can be treated as such in an input file. The location of the plugin directory must be included in PYTHONPATH, either externally in the calling shell or defined in the input file. This is usually done by manipulating PSIPATH. Then, the plugin can be loaded as import myplugin and executed as energy('myplugin'). Any other Python functions are also available from the input file, e.g. myplugin.testfunction(), note the namespace protection.
  • — Python component of the plugin. The procedure for calling plugin code shown in input.dat sounds very simple, but it can be made simpler still. By encoding the sequence of Psi4 module calls needed to run the plugin in the run_myplugin() function in this file, the plugin is hooked into the main Psi4 driver function energy() and so can be accessed through energy('myplugin') in an input file. Any other Python functions can also be placed in this file.
  • — Init script for the plugin (in the sense that the whole plugin directory is a Python module). This file generally won’t need editing unless additional Python files are added to the plugin directory (add additional lines to the # Load Python modules section) or the plugin depends on .so codes in other plugin directories (add additional plugin_load lines relative to the current plugin directory to the # Load C++ plugin section).
  • doc.rst — Documentation file. Place in this file any notes, equations, warnings to users, todo lists, etc.. Plain text is fine, though reStructuredText is the ultimate goal. Remove the .. comment text and build Sphinx documentation for samples of linking keywords, sections, and math. This file is absorbed into the Psi4 documentation, along with any docstrings to Python functions, and the C++ keywords block in the file. See Documentation for building documentation and Available Plugins for this file’s final destination.

Please note that pure virtual functions in a plugin may cause undefined symbols errors when the plugin is loaded.

To create a purely Python plugin, create a new plugin directory, then remove the Makefile and files and erase the shared object loading portion of Create as many .py files as necessary (registering each one in, use input.dat as a model for loading the plugin, no recompile ever necessary.