# 1. Isolated water molecule

Let's take a look at our input file: open `in` from the left panel.
We have divided it into sections marked by comments (line starting with #).
Note that the commands on each line can appear in any order,
and we have grouped them in the way that makes the most sense for us here.
See the [JDFTx input file documentation](http://jdftx.org/Commands.html) for details on any command used here.

The first section specifies a cubic unit cell of side 20 bohrs,
and specifies the atoms of H2O centered at 10, 10, 10.
Note that as a plane-wave code, all calculations are periodic by default.
We could have centered the molecule anywhere, but this choice will be convenient for visualization.
Finally, note that we include the commands:

    coulomb-interaction Isolated
    coulomb-truncation-embed 10 10 10

which change the long-range Coulomb interaction to non-periodic in order to calculate
isolated molecules accurately (even though the wavefunctions are still expanded in a periodic basis).
The second line above specifies the origin for breaking periodicity, and should be
centered on the molecule (or later slab) we are trying to isolate from its periodic images.

The next block selects the family of pseudopotentials,
specifically here, the high-throughput GBRV ultrasoft pseudopotentials.
The kinetic energy cutoff for wavefunctions and charge densities are
set to 20 and 100 Hartrees respectively, as recommended for these pseudopotentials.
(The exchange-correlation functional is PBE GGA by default, and not specified explicitly here.)

We then request that a geometry optimization be carried out for up to 10 iterations.
The electronic optimization parameters have been left at their defaults here,
and we will tweak them in later calculations.

The final block specifies how to name the output files, and what to output.
We are specifically requesting the electron density to be output at the end,
and to name it as `jdftx.*` (we will end up with a `jdftx.n` correspondingly).

## Run the calculation

Switch to a terminal in this directory, and start an interactive session first:

    beast-interactive

This should produce an output like:

    salloc: Granted job allocation XXXXXXXX
    salloc: Waiting for resource configuration
    salloc: Nodes nidXXXXX are ready for job

and you are now on a session connected to a compute node on Perlmutter.
We can run the calculation now as:

    srun -n 1 --gpus 1 jdftx_gpu -i in | tee out

(Once the calculation completes, hit `Ctrl+D` to exit the allocation.
You can choose to stay in the interactive session through multiple tutorials,
but note that it will time out at 30 minutes, and you would need to start it again then.)

This specifies the code to be run with a single process on one gpu using input file `in` and mirroring the output to both the terminal and the file `out`.
We will use more gpus effectively in later and larger calculations.

When you look through the calculation output, you'll see the initialization
corresponding to all the commands in your input,
followed by a sequence of `ElecMinimize` blocks,
one for each step of `IonicMinimize` (geometry optimization).

Within each ionic step, the code is optimizing the electronic wavefunctions variationally (ElecMinimize).
It then outputs the final energy components, current atomic positions and forces,
and a Lowdin analysis of charge states on each atom.
The `|grad|_K` in the `IonicMinimize` lines specifies the RMS force component,
which reduces with every subsequent step till the geometry is converged to
a force threshold of 1E-4 Eh/a0 by default.
You can see this easily with `grep IonicMinimize out`, which should lead to something like:

    IonicMinimize: Iter:   0  Etot: -17.267918387453513  |grad|_K:  6.538e-03  t[s]:      4.41
    IonicMinimize: Iter:   1  Etot: -17.268154061676690  |grad|_K:  2.339e-03  alpha:  1.000e+00  linmin: -6.146e-01  t[s]:      5.01
    IonicMinimize: Iter:   2  Etot: -17.268217425469125  |grad|_K:  1.904e-03  alpha:  1.000e+00  linmin: -6.898e-01  t[s]:      5.48
    IonicMinimize: Iter:   3  Etot: -17.268297591958245  |grad|_K:  1.129e-03  alpha:  1.000e+00  linmin: -3.557e-01  t[s]:      5.96
    IonicMinimize: Iter:   4  Etot: -17.268308291339274  |grad|_K:  8.355e-05  alpha:  1.000e+00  linmin:  4.964e-01  t[s]:      6.38
    IonicMinimize: Converged (|grad|_K<1.000000e-04).

Exercise: search for `ElecMinimize` similarly and interpret the output. How did these converge? 

## Visualization

Finally, let's look at the optimized structure and electron density.
Usually, working offline, we could use VESTA as suggested in the [website tutorials](http://jdftx.org/FirstCalc.html).
Here, we have extended the visualization tools in the Atomistic Simulation Environment to do this within Jupyter.
Open Visualize.ipynb to continue.

## Further exercises

1. Let's examine the output: open the file named `out` from the left panel.  

Near the beginning locate:
+ The resource configuration (processes, threads, gpus)
+ Your inputs and the defaults

Near the end locate:
+ The final cartesian coordinates of the H2O molecule
+ The converged total energy (Etot) and the different contributions of the energy components. See tutorial on [output variables](https://jdftx.org/Outputs.html) if you need help with interpreting these.  

2. Let's look at convergence of this calculation with respect to box size. We are using a plane-wave basis with periodic boundary conditions so, it is important to ensure interactions between the molecule and its periodic images are minimized.

    Set up the same calculation with several box sizes ranging from 15 to 30 bohrs (say) and repeat the above calculation with and without Coulomb truncation. You can do this by saving the following to `water.xyz`:

        3
        O    0  0        0.127118
        H    0  0.75799 -0.50847
        H    0 -0.75799 -0.50847 
        
    
    and using the jdftx `xyzToIonposOpt` utility to create inputs with different box sizes, e.g. 20 bohr:
    
        xyzToIonposOpt water.xyz 20
    \
    And running as above:
    
        srun -n 1 --gpus 1 jdftx_gpu -i in | tee water-20.out
    \
    Hint: Write this in a loop.

    How effective is Coulomb truncation at reducing the required box sizes along non-periodic directions?