# Total energy convergence

In this tutorial we will show how to converge a quantity of interest (the total energy) with respect to the parameters that define the real-space grid. For this we will use two examples, the Nitrogen atom from the Basic input options tutorial, and a methane molecule.

## Nitrogen atom: finding a good spacing

The key parameter of a real-space calculation is the spacing between the points of the mesh. The default option in Octopus is to use a Cartesian grid. This is a regular grid where the spacing along each Cartesian direction is the same. The first step in any calculation should then be making sure that this spacing is good enough for our purposes. This should be done through a convergence study, very similar to the ones performed in plane-wave calculations.

The needed spacing essentially depends on the pseudopotentials that are being used. The idea is to repeat a series of ground-state calculations, with identical input files except for the grid spacing. There are many different ways of doing it, the simplest one being to change the input file by hand and run Octopus each time. Another option is to use a little bash script like the one bellow.

As a first example, we will use the Nitrogen atom from the previous tutorial, so make sure you have the corresponding inp and N.xyz files in your working directory. Next, create a file called spacing.sh with the following content:


#!/bin/bash
echo "#Sp    Energy        s_eigen   p_eigen" > spacing.dat
list="0.26 0.24 0.22 0.20 0.18 0.16 0.14"
export OCT_PARSE_ENV=1
for Spacing in $list do export OCT_Spacing=$(echo $Spacing*1.8897261328856432 | bc) echo$Spacing
octopus >& out-$Spacing energy=grep Total static/info | head -1 | cut -d "=" -f 2 seigen=grep "1 --" static/info | head -1 | awk '{print$3}'
peigen=grep  "2   --" static/info | head -1 |  awk '{print $3}' echo$Spacing $energy$seigen $peigen >> spacing.dat rm -rf restart done unset OCT_Spacing  What this script does is quite simple: it runs Octopus for a list of spacings (0.26, 0.24, …, 0.14) and after each calculation it collects the values of the total energy and of the eigenvalues, and prints them to a file called spacing.dat . In order to change the value of the spacing between each calculation, it uses the feature that one can override variables in the inp file by defining them as environment variables in the shell. Note that we unset the variable OCT_Spacing at the end of the script, to avoid it to remaining defined in case you run the script directly in the shell. Now, to run the script type source spacing.sh  (NOT sh spacing.sh or bash spacing.sh !). Note that for this to work, the octopus executable must be found in the shell path. If that is not the case, you can replace  octopus >& out-$Spacing


with the actual path to the executable

  /path/to/octopus >& out-$Spacing  Once the script finishes running, the spacing.dat file should look something like this:  #Sp Energy s_eigen p_eigen 0.26 -256.56920556 -19.856140 -6.753351 0.24 -260.26120259 -18.816521 -7.084948 0.22 -262.60795748 -18.190522 -7.321640 0.20 -262.93498179 -18.096181 -7.363718 0.18 -262.24162451 -18.282738 -7.302364 0.16 -261.80046468 -18.390809 -7.251448 0.14 -261.81949746 -18.386189 -7.256954  You can also plot the results from the file in your favorite plotting program, e.g. gnuplot . Convergence with spacing of N Expand to see the gnuplot script The results, for this particular example, are shown in the figure. In this figure we are actually plotting the error with respect to the most accurate results (smallest spacing). That means that we are plotting the ‘‘‘difference’’’ between the values for a given spacing and the values for a spacing of 0.14 Å. So, in reading it, note that the most accurate results are at the left (smallest spacing). A rather good spacing for this nitrogen pseudopotential seems to be 0.18 Å. However, as we are usually not interested in total energies, but in energy differences, probably a larger one may also be used without compromising the results. ## Methane molecule We will now move on to a slightly more complex system, the methane molecule CH4, and add a convergence study with respect to the box size. ### Input As usual, the first thing to do is create an input file for this system. From our basic chemistry class we know that methane has a tetrahedral structure. The only other thing required to define the geometry is the bond length between the carbon and the hydrogen atoms. If we put the carbon atom at the origin, the hydrogen atoms have the coordinates given in the following input file:  CalculationMode = gs UnitsOutput = eV_Angstrom Radius = 3.5*angstrom Spacing = 0.22*angstrom CH = 1.2*angstrom %Coordinates "C" | 0 | 0 | 0 "H" | CH/sqrt(3) | CH/sqrt(3) | CH/sqrt(3) "H" | -CH/sqrt(3) |-CH/sqrt(3) | CH/sqrt(3) "H" | CH/sqrt(3) |-CH/sqrt(3) | -CH/sqrt(3) "H" | -CH/sqrt(3) | CH/sqrt(3) | -CH/sqrt(3) %  Here we define a variable CH that represents the bond length between the carbon and the hydrogen atoms, which simplifies the writing of the coordinates. We start with a bond length of 1.2 Å but that’s not so important at the moment, since we can optimize it later (for more information see the tutorial). (You should not use 5 Å or so, but something slightly bigger than 1 Å is fine.) Some notes concerning the input file: • We do not tell Octopus explicitly which BoxShape to use. The default is a union of spheres centered around each atom. This turns out to be the most economical choice in almost all cases. • We also do not specify the %Species block. In this way, Octopus will use default pseudopotentials for both Carbon and Hydrogen. This should be OK in many cases, but, as a rule of thumb, you should do careful testing before using any pseudopotential for serious calculations. If you use the given input file you should find the following values in the resulting static/info file.  Eigenvalues [eV] #st Spin Eigenvalue Occupation 1 -- -15.990926 2.000000 2 -- -9.065616 2.000000 3 -- -9.065616 2.000000 4 -- -9.065616 2.000000 Energy [eV]: Total = -219.03751043 Free = -219.03751043 ----------- Ion-ion = 236.08498119 Eigenvalues = -86.37554993 Hartree = 393.45819611 Int[n*v_xc] = -105.38067937 Exchange = -69.66885295 Correlation = -11.00057200 vanderWaals = 0.00000000 Delta XC = 0.00000000 Entropy = 0.00000000 -TS = -0.00000000 Photon ex. = 0.00000000 Kinetic = 163.97293343 External = -931.88444364 Non-local = -49.75841882 Int[n*v_E] = 0.00000000  Now the question is whether these values are converged or not. This will depend on two things: the Spacing, as seen above, and the Radius. Just like for the Nitrogen atom, the only way to answer this question is to try other values for these variables. ### Convergence with the spacing As before, we will keep all entries in the input file fixed except for the spacing that we will make smaller by 0.02 Å all the way down to 0.1 Å. So you have to run Octopus several times. You can use a similar script as the one from the Nitrogen atom example:  #!/bin/bash echo "#Sp Energy" > spacing.dat list="0.22 0.20 0.18 0.16 0.14 0.12 0.10" export OCT_PARSE_ENV=1 for Spacing in$list
do
export OCT_Spacing=$(echo$Spacing*1.8897261328856432 | bc)
octopus >& out-$Spacing energy=grep Total static/info | head -1 | cut -d "=" -f 2 echo$Spacing $energy >> spacing.dat rm -rf restart done unset OCT_Spacing  For this example we changed the list of spacings to try and, to keep things simple, we are only writing the total energy to the spacing.dat file. Now run the script to get the following results:  #Sp Energy 0.22 -219.03751043 0.20 -218.58393646 0.18 -218.27957498 0.16 -218.20004092 0.14 -218.17917682 0.12 -218.15985341 0.10 -218.13844949  If you give these numbers to ‘‘gnuplot’’ (or other software to plot) you will get a curve like the one shown below: Convergence with spacing of methane As you can see from this picture, the total energy is converged to within 0.1 eV for a spacing of 0.18 Å. So we will use this spacing for the next calculations. ### Convergence with the radius Now we will see how the total energy changes with the Radius of the box. We will change the radius in steps of 0.5 Å. You can change the input file by hand and run Octopus each time, or again use a small script:  #!/bin/bash echo "#Rad Energy" > radius.dat list="2.5 3.0 3.5 4.0 4.5 5.0" export OCT_PARSE_ENV=1 for Radius in$list
do
export OCT_Radius=$(echo$Radius*1.8897261328856432 | bc)
octopus >& out-$Radius energy=grep Total static/info | head -1 | cut -d "=" -f 2 echo$Radius \$energy >> radius.dat
rm -rf restart
done


Before running the script, make sure that the spacing is set to 0.18 Å in the input file. You should then get a file radius.dat that looks like this: