A (hopefully) gentle guide to the computer implementation of molecular integrals over Gaussian basis functions.28 Apr 2017
Note: You can find the following integral routines implemented in a working Hartree-Fock code here.
In quantum chemistry, we are often interested in evaluating integrals over Gaussian basis functions. Here I am going to take much of the theory for granted and talk about how one might actually implement the integrals for quantum chemistry by using recursion relationships with Hermite Gaussians. Our goal is to have clean, readable code. I’m writing this for the people are comfortable with the mathematics behind the Gaussian integrals, but want to see a readable computer implementation. I’ll talk a bit about some computational considerations at the end, but my goal is to convert equations to code. In fact, I’ve tried to structure the equations and the code in such a way that the two look very similar.
For a very good overview of integral evaluation, please see:
Helgaker, Trygve, and Peter R. Taylor. “Gaussian basis sets and molecular integrals.” Modern Electronic Structure (1995).
I will try and follow the notation used in the above reference.
Let’s start with some of the basics. First, we have our 3D Gaussian functions
with orbital exponent , electronic coordinates , origin , and
also, are the angular quantum numbers (e.g. is -type, is type, etc.) Cartesian Gaussians are separable in 3D along so that
with the 1D Gaussian
So far, so good. Let’s consider the overlap integral of two 1D Gaussians, and
where we used the Gaussian product theorem so that
When using Hermite Gaussians, we can express as
where are expansion coefficients (to be determined recursively) and is the Hermite Gaussian overlap of two Gaussians and . It has a simple expression that kills the sum via the Kronecker delta . It can be shown that the expansion coefficients can be defined using the following recursive definitions
The first equation gives us a way to reduce the index and the second gives us a way to reduce index so that we can get to the third equation, which is our base case. The last equation tells us what to do if we go out of index bounds.
The first thing we need to do is implement a function
E which returns
our expansion coefficients . Aside from angular momentum
and from the Gaussian functions, we also need the distance between
Gaussians and the orbital exponent coefficients and
This is simple enough! So for a 1D overlap between two Gaussians we
would just need to evaluate and multiply it by
. Overlap integrals in 3D are just a product of
the 1D overlaps. We could imagine a 3D
overlap function like
Note that we are using the
NumPy package in order to take advantage of
the definitions of and the fractional power to the . The
above two functions
E are enough to get us the overlap
between two Gaussian functions (primitives), but most basis functions
are contracted, meaning they are the sum of multiple Gaussian
primitives. It is not too difficult to account for this, and we can
finally wrap up our evaluation of overlap integrals with a function
S(a,b) which returns the overlap integral between two contracted
Basically, this is just a sum over primitive overlaps, weighted by
normalization and coefficient. A word is in order for the arguments,
however. In order to keep the number of arguments we have to pass into
our functions, we have created
BasisFunction objects that contain all
the relevant data for the basis function, including exponents,
normalization, etc. A
BasisFunction class looks like
So, for example if we had a STO-3G Hydrogen 1s at origin (1.0, 2.0, 3.0), we could create a basis function object for it like so
Where we used the EMSL STO-3G definition
S(a,a) = 1.0, since the overlap of a basis function with
itself (appropriately normalized) is one.
Kinetic energy integrals
Having finished the overlap integrals, we move on to the kinetic integrals. The kinetic energy integrals can be written in terms of overlap integrals
For a 3D primitive, we can form a
kinetic function analogous to
and for contracted Gaussians we make our final function
Nuclear attraction integrals
The last one-body integral I want to consider here is the nuclear attraction integrals. These differ from the overlap and kinetic energy integrals in that the nuclear attraction operator is Coulombic, meaning we cannot easily factor the integral into Cartesian components .
To evaluate these integrals, we need to set up an auxiliary Hermite Coulomb integral that handles the Coulomb interaction between a Gaussian charge distribution centered at and a nuclei centered at . The Hermite Coulomb integral, like its counterpart , is defined recursively:
where is the Boys function
which is a special case of the Kummer confluent hypergeometric function,
which is convenient for us, since
SciPy has an implementation of as
a part of
scipy.special. So for
R we can code up the recursion like
and we can define our
boys(n,T) function as
There are other definitions of the Boys function of course, in case you
do not want to use the
SciPy built-in. Note that
knowledge of the composite center from two Gaussians
centered at and . We can determine
using the Gaussian product center rule
which is very simply coded up as
Now that we have a the Coulomb auxiliary Hermite integrals , we can form the nuclear attraction integrals with respect to a given nucleus centered at , , via the expression
And, just like all the other routines, we can wrap it up to treat contracted Gaussians like so:
Important: Note that this is the nuclear repulsion integral contribution from an atom centered at . To get the full nuclear attraction contribution, you must sum over all the nuclei, as well as scale each term by the appropriate nuclear charge!
Two electron repulsion integrals
We are done with the necessary one-body integrals (for a basic Hartree-Fock energy code, at least) and are ready to move on to the two-body terms: the electron-electron repulsion integrals. Thankfully, much of the work has been done for us on account of the nuclear attraction one-body integrals.
In terms of Hermite integrals, to evaluate the two electron repulsion terms, we must evaluate the summation
which looks terrible and it is. However, recalling that letting (that is, the Gaussian exponents on and , and and ), we can write the equation in a similar form to the nuclear attraction integrals
And, for completeness’ sake, we wrap the above to handle contracted Gaussians
And there you have it! All the integrals necessary for a Hartree-Fock SCF code.
Computational efficiency considerations
Our goal here has been to eliminate some of the confusion when it comes
to connecting mathematics to actual computer code. So the code that we
have shown is hopefully clear and looks nearly identical to the
mathematical equations they are supposed to represent. This is one
reason we chose
Python as the code of choice to implement the
integrals. It emphasizes readability.
If you use the code as is, you’ll find that you can only really handle small systems. To that end, I’ll give a few ideas on how to improve the integral code to actually be usable.
First, I would recommend not using pure
Python. The problem is that we
have some pretty deep loops in the code, and nested
for loops will
kill your speed if you insist on sticking with
Python. Now, you can
code in another language, but I would suggest rewriting some of the
lower level routines with
statically compiles your code to eliminate many of the
that slow your loops down. In my experience, you will get several orders
of magnitude speed up. This brings me to my second point. One of the
Python is that you have the global interpreter lock
(GIL) which basically means you cannot run things in parallel. Many of
the integral evaluation routines would do well if you could split the
work up over multiple CPUs. If you rewrite some of the low level
routines such that they do not make
Python calls anymore, you can turn
off the GIL and wrap the code in
OpenMP directives, or even use
cython.parallel module. This will take some thought, but
can definitely be done. Furthermore, removing the explicit recursion in the
R functions and making them iterative would go a long way to speed
up the code.
A couple other thoughts: be sure to exploit the permutational symmetry of the integrals. The two electron repulsion integrals, for example, can be done in of the time just by exploiting these symmetries, which are unrelated to point group. Also, you can exploit many of the integral screening routines, since many of the two electron integrals are effectively zero. There are a lot of tricks out there in the literature, go check them out!