# Tutorial

This document provides an overview of the structure of the code and how to access basic information about calculations. Basic familiarity with the concepts of plane-wave density functional theory is assumed throughout. Feel free to take a look at the Periodic problems or the Introductory resources chapters for some introductory material on the topic.

We use rough parameters in order to be able to automatically generate this documentation very quickly. Therefore results are far from converged. Tighter thresholds and larger grids should be used for more realistic results.

For our discussion we will use the classic example of computing the LDA ground state of the silicon crystal. Performing such a calculation roughly proceeds in three steps.

```
using DFTK
using Plots
using Unitful
using UnitfulAtomic
# 1. Define lattice and atomic positions
a = 5.431u"angstrom" # Silicon lattice constant
lattice = a / 2 * [[0 1 1.]; # Silicon lattice vectors
[1 0 1.]; # specified column by column
[1 1 0.]];
```

By default, all numbers passed as arguments are assumed to be in atomic units. Quantities such as temperature, energy cutoffs, lattice vectors, and the k-point grid spacing can optionally be annotated with Unitful units, which are automatically converted to the atomic units used internally. For more details, see the Unitful package documentation and the UnitfulAtomic.jl package.

```
# Load HGH pseudopotential for Silicon
Si = ElementPsp(:Si; psp=load_psp("hgh/lda/Si-q4"))
# Specify type and positions of atoms
atoms = [Si, Si]
positions = [ones(3)/8, -ones(3)/8]
# 2. Select model and basis
model = model_LDA(lattice, atoms, positions)
kgrid = [4, 4, 4] # k-point grid (Regular Monkhorst-Pack grid)
Ecut = 7 # kinetic energy cutoff
# Ecut = 190.5u"eV" # Could also use eV or other energy-compatible units
basis = PlaneWaveBasis(model; Ecut, kgrid)
# Note the implicit passing of keyword arguments here:
# this is equivalent to PlaneWaveBasis(model; Ecut=Ecut, kgrid=kgrid)
# 3. Run the SCF procedure to obtain the ground state
scfres = self_consistent_field(basis, tol=1e-5);
```

```
n Energy log10(ΔE) log10(Δρ) Diag Δtime
--- --------------- --------- --------- ---- ------
1 -7.900551748513 -0.70 4.8 43.7ms
2 -7.905012626248 -2.35 -1.52 1.0 23.4ms
3 -7.905179326231 -3.78 -2.52 1.1 23.9ms
4 -7.905210527410 -4.51 -2.84 2.8 32.8ms
5 -7.905211099822 -6.24 -2.98 1.1 24.0ms
6 -7.905211519081 -6.38 -4.67 1.0 23.5ms
7 -7.905211531050 -7.92 -4.55 3.0 36.3ms
8 -7.905211531389 -9.47 -5.31 1.0 24.4ms
```

That's it! Now you can get various quantities from the result of the SCF. For instance, the different components of the energy:

`scfres.energies`

```
Energy breakdown (in Ha):
Kinetic 3.1020972
AtomicLocal -2.1987875
AtomicNonlocal 1.7296111
Ewald -8.3979253
PspCorrection -0.2946254
Hartree 0.5530400
Xc -2.3986215
total -7.905211531389
```

Eigenvalues:

`stack(scfres.eigenvalues)`

```
7×8 Matrix{Float64}:
-0.176942 -0.147441 -0.0911695 … -0.101219 -0.0239773 -0.0184083
0.261073 0.116914 0.00482486 0.061164 -0.0239773 -0.0184083
0.261073 0.232989 0.216733 0.121636 0.155531 0.117747
0.261073 0.232989 0.216733 0.212134 0.155531 0.117747
0.354532 0.335109 0.317102 0.350436 0.285692 0.417258
0.354532 0.389828 0.384601 … 0.436925 0.285692 0.417437
0.354533 0.389828 0.384601 0.449236 0.627545 0.443806
```

`eigenvalues`

is an array (indexed by k-points) of arrays (indexed by eigenvalue number).

The resulting matrix is 7 (number of computed eigenvalues) by 8 (number of irreducible k-points). There are 7 eigenvalues per k-point because there are 4 occupied states in the system (4 valence electrons per silicon atom, two atoms per unit cell, and paired spins), and the eigensolver gives itself some breathing room by computing some extra states (see the `bands`

argument to `self_consistent_field`

as well as the `AdaptiveBands`

documentation). There are only 8 k-points (instead of 4x4x4) because symmetry has been used to reduce the amount of computations to just the irreducible k-points (see Crystal symmetries for details).

We can check the occupations ...

`stack(scfres.occupation)`

```
7×8 Matrix{Float64}:
2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0
2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0
2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0
2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
```

... and density, where we use that the density objects in DFTK are indexed as ρ[ix, iy, iz, iσ], i.e. first in the 3-dimensional real-space grid and then in the spin component.

```
rvecs = collect(r_vectors(basis))[:, 1, 1] # slice along the x axis
x = [r[1] for r in rvecs] # only keep the x coordinate
plot(x, scfres.ρ[1, :, 1, 1], label="", xlabel="x", ylabel="ρ", marker=2)
```

We can also perform various postprocessing steps: We can get the Cartesian forces (in Hartree / Bohr):

`compute_forces_cart(scfres)`

```
2-element Vector{StaticArraysCore.SVector{3, Float64}}:
[-8.057221523259338e-16, -2.638818005894151e-17, -3.0775714250832516e-16]
[6.645184264997981e-17, -3.130230336794735e-16, -1.4578050286125092e-16]
```

As expected, they are numerically zero in this highly symmetric configuration. We could also compute a band structure,

`plot_bandstructure(scfres; kline_density=10)`

or plot a density of states, for which we increase the kgrid a bit to get smoother plots:

```
bands = compute_bands(scfres, MonkhorstPack(6, 6, 6))
plot_dos(bands; temperature=1e-3, smearing=Smearing.FermiDirac())
```

Note that directly employing the `scfres`

also works, but the results are much cruder:

`plot_dos(scfres; temperature=1e-3, smearing=Smearing.FermiDirac())`