In this chapter we demonstrate how to write code for \PSIfour\ using some simple
examples.  It can be quite daunting to develop code in an unfamiliar programming
environment, so a good starting point is to write code that requires little
knowledge of the existing code structure.  In this regard, the modular nature
of \PSIthree\ made it a perfect development platform, with each ``module''
existing as a standalone code.  With the transition to a single executable
paradigm in \PSIfour, developing new code is still a simple process; in fact
the availability of library functions to perform most tasks makes it even
easier to get started with programming in \PSIfour, as we will demonstrate.

Here's a more realistic example of a \PSIfour\ module - a reasonably efficient
DF-MP2 code.  The major advantage of density fitting methods is the replacement
of four-index integrals with products of three-index integrals.  With the
\libmints\ class, \PSIfour\ can compute these integrals easily and efficiently,
as we will now demonstrate.  The Makefile for this project is

\includesource{sample-codes/df-mp2/Makefile}{make}

As with the integral code in section \ref{integrals}, we make a simple
\module{main} routine to set up the \PSI\ I/O routines and call our new module.
Notice that the hyphens are absent from the namespace definition, as they are
not allowed in C++, although underscores are valid.

\includesource{sample-codes/df-mp2/main.cc}{C++}

The code for the Module itself is a little more complex than before, and a few
extra features are used that were absent from the \module{integrals} code.  On
lines 31 and 36, instead of throwing a generic \module{PsiException} we throw a
more specific \module{InputException}.  The full list of exceptions available
can be found on in the \module{Doxygen} documentation or in the
\file{exceptions.h} file, which is in \$PSI4/include.  The default scale
factors are defined in \file{main.cc}, where the options are declared and not
where the options are read in on lines 51 and 52.

This code shows off the flexibility of the \libmints\ module, which can accept
arbitrary basis sets for each index in the four index integral.  Passing a
special {\tt zero\_basis\_set} object for one index allows us to compute
three-center integrals efficiently - see the \module{IntegralFactory} creation
on lines 174 and 175.  

\includesource{sample-codes/df-mp2/df-mp2.cc}{C++}

The formation of the metric matrix is performed in a function defined in a
seperate file.  Breaking large chunks of code into smaller, more manageable,
segments is usually a good strategy; if fact, the \file{main.cc} should
probably be further subdivided.  To do this, we need a declaration of the
external function to be defined (lines 12 and 13 of \file{df-mp2.cc}) before
defining the function itself in a seperate file, in this case we create the
following file:

\includesource{sample-codes/df-mp2/df-mp2-formJ.cc}{C++}

We also have to remember to add this new source file to the list of sources in
the \file{Makefile}, but it's as easy as that.  A sample input file that will
drive this code is shown below.

\includeinput{sample-codes/df-mp2/input.dat}

Notice how the \keyword{print} keyword is in a special {\tt DF-MP2} section of
the input file - this is to prevent the \module{input} module from parsing the
print keyword and producing excessive output when the print level is high.
This code requires \module{input}, \module{cints} and \module{cscf} to be run
before it can function.
