# Tutorial¶

DFTK is a Julia package for playing with plane-wave density-functional theory algorithms. In its basic formulation it solves periodic Kohn-Sham equations.

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 density-functional theory chapter for some introductory material on the topic.

!!! note "Convergence parameters in the documentation" 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.

In :
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.]]

Out:
3×3 Matrix{Unitful.Quantity{Float64, 𝐋, Unitful.FreeUnits{(Å,), 𝐋, nothing}}}:
0.0 Å  2.7155 Å  2.7155 Å
2.7155 Å     0.0 Å  2.7155 Å
2.7155 Å  2.7155 Å     0.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.

In :
# Load HGH pseudopotential for Silicon

# Specify type and positions of atoms
atoms = [Si => [ones(3)/8, -ones(3)/8]]

# 2. Select model and basis
model = model_LDA(lattice, atoms)
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-8);

n     Energy            Eₙ-Eₙ₋₁     ρout-ρin   α      Diag
---   ---------------   ---------   --------   ----   ----
1   -7.904938386565         NaN   1.95e-01   0.80    3.9
2   -7.909781227844   -4.84e-03   2.98e-02   0.80    1.0
3   -7.910052173409   -2.71e-04   3.04e-03   0.80    3.1
4   -7.910052839513   -6.66e-07   4.43e-04   0.80    2.4
5   -7.910052852934   -1.34e-08   3.04e-05   0.80    2.0
6   -7.910052854037   -1.10e-09   5.59e-06   0.80    3.1


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

In :
scfres.energies

Out:
Energy breakdown:
Kinetic             3.0795140
AtomicLocal         -2.1806344
AtomicNonlocal      1.7339382
Ewald               -8.3979253
PspCorrection       -0.2946254
Hartree             0.5417559
Xc                  -2.3920760

total               -7.910052854037


Eigenvalues:

In :
hcat(scfres.eigenvalues...)

Out:
7×10 Matrix{Float64}:
-0.170182  -0.1318     -0.0883279  …  -0.0562604  -0.11494    -0.0700165
0.201343   0.0909039   0.0122927      0.0111148   0.0420616   0.0176498
0.249296   0.174773    0.176137       0.132964    0.220115    0.11233
0.249296   0.231428    0.20237        0.161043    0.220115    0.190456
0.350986   0.360027    0.340134       0.291807    0.320727    0.327376
0.36997    0.395898    0.389483   …   0.331814    0.38819     0.460226
0.36997    0.401676    0.412474       0.565536    0.38819     0.462717

eigenvalues is an array (indexed by kpoints) of arrays (indexed by eigenvalue number). The "splatting" operation ... calls hcat with all the inner arrays as arguments, which collects them into a matrix.

The resulting matrix is 7 (number of computed eigenvalues) by 8 (number of kpoints). There are 7 eigenvalues per kpoint 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 n_ep_extra argument to self_consistent_field).

We can check the occupations ...

In :
hcat(scfres.occupation...)

Out:
7×10 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  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  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 ρ[iσ, ix, iy, iz], i.e. first in the spin component and then in the 3-dimensional real-space grid.

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

Out:

We can also perform various postprocessing steps: for instance compute a band structure

In :
plot_bandstructure(scfres, kline_density=5)

Computing bands along kpath:
Γ -> X -> W -> K -> Γ -> L -> U -> W -> L -> K  and  U -> X
Diagonalising Hamiltonian kblocks: 100%|████████████████| Time: 0:00:01

Out:

or get the cartesian forces (in Hartree / Bohr)

In :
compute_forces_cart(scfres)  # Select silicon forces

Out:
2-element Vector{StaticArrays.SVector{3, Float64}}:
[0.00044876924247816386, 0.00044877116961621576, 0.0004487694128655534]
[-0.0004487711696164536, -0.0004487692424776881, -0.0004487694128654898]

The  extracts the forces for the first kind of atoms, i.e. Si (silicon) in the setup of the atoms list of step 1 above. As expected, they are almost zero in this highly symmetric configuration.

## Where to go from here¶

Take a look at the example index to continue exploring DFTK.