# Temperature and metallic systems

In this example we consider the modeling of a magnesium lattice as a simple example for a metallic system. For our treatment we will use the PBE exchange-correlation functional. First we import required packages and setup the lattice.

```
using DFTK
using Plots
a = 3.01794 # bohr
b = 5.22722 # bohr
c = 9.77362 # bohr
lattice = [[-a -a 0]; [-b b 0]; [0 0 -c]]
Mg = ElementPsp(:Mg, psp=load_psp("hgh/pbe/Mg-q2"))
atoms = [Mg => [[2/3, 1/3, 1/4], [1/3, 2/3, 3/4]]];
```

Next we build the PBE model and discretize it. Since magnesium is a metal we apply a small smearing temperature to ease convergence using the Fermi-Dirac smearing scheme. Note that both the `Ecut`

is too small as well as the minimal $k$-point spacing `kspacing`

far too large to give a converged result. These have been selected to obtain a fast execution time. By default `PlaneWaveBasis`

chooses a `kspacing`

of `2π * 0.022`

inverse Bohrs, which is much more reasonable.

```
kspacing = 0.5 # Minimal spacing of k-points,
# in units of wavevectors (inverse Bohrs)
Ecut = 5 # kinetic energy cutoff in Hartree
temperature = 0.01 # Smearing temperature in Hartree
model = model_DFT(lattice, atoms, [:gga_x_pbe, :gga_c_pbe];
temperature=temperature,
smearing=DFTK.Smearing.FermiDirac())
kgrid = kgrid_size_from_minimal_spacing(lattice, kspacing)
basis = PlaneWaveBasis(model, Ecut, kgrid=kgrid);
```

Finally we run the SCF. Two magnesium atoms in our pseudopotential model result in four valence electrons being explicitly treated. Nevertheless this SCF will solve for eight bands by default in order to capture partial occupations beyond the Fermi level due to the employed smearing scheme.

`scfres = self_consistent_field(basis);`

```
n Free energy Eₙ-Eₙ₋₁ ρout-ρin Diag
--- --------------- --------- -------- ----
1 -1.761837028999 NaN 5.02e-02 5.2
2 -1.762198626672 -3.62e-04 1.02e-02 2.6
3 -1.762240099485 -4.15e-05 5.72e-04 2.4
4 -1.762241503386 -1.40e-06 8.68e-05 3.4
5 -1.762241506037 -2.65e-09 6.37e-06 2.4
```

`scfres.occupation[1]`

```
9-element Array{Float64,1}:
1.9999999999077946
1.9999975862876218
0.00401689097322075
3.0005064737893e-15
1.1115698387152064e-18
1.1114765750503069e-18
7.977850288774056e-19
7.977272051128696e-19
3.2246073534882983e-22
```

`scfres.energies`

```
Energy breakdown:
Kinetic 0.7180642
AtomicLocal 0.3145385
AtomicNonlocal 0.3265767
Ewald -2.1544222
PspCorrection -0.1026056
Hartree 0.0055003
Xc -0.8610489
Entropy -0.0088446
total -1.762241506037
```

The fact that magnesium is a metal is confirmed by plotting the density of states around the Fermi level.

```
εs = range(minimum(minimum(scfres.eigenvalues)) - .5,
maximum(maximum(scfres.eigenvalues)) + .5, length=1000)
Ds = DOS.(εs, Ref(basis), Ref(scfres.eigenvalues))
q = plot(εs, Ds, label="DOS")
vline!(q, [scfres.εF], label="εF")
```