Analysing SCF convergence

The goal of this example is to explain the differing convergence behaviour of SCF algorithms depending on the choice of the mixing. For this we look at the eigenpairs of the Jacobian governing the SCF convergence, that is

\[1 - α P^{-1} \varepsilon^\dagger \qquad \text{with} \qquad \varepsilon^\dagger = (1-\chi_0 K).\]

where $α$ is the damping $P^{-1}$ is the mixing preconditioner (e.g. KerkerMixing, LdosMixing) and $\varepsilon^\dagger$ is the dielectric operator.

We thus investigate the largest and smallest eigenvalues of $(P^{-1} \varepsilon^\dagger)$ and $\varepsilon^\dagger$. The ratio of largest to smallest eigenvalue of this operator is the condition number

\[\kappa = \frac{\lambda_\text{max}}{\lambda_\text{min}},\]

which can be related to the rate of convergence of the SCF. The smaller the condition number, the faster the convergence. For more details on SCF methods, see Self-consistent field methods.

For our investigation we consider a crude aluminium setup:

using AtomsBuilder
using DFTK

system_Al = bulk(:Al; cubic=true) * (4, 1, 1)
FlexibleSystem(Al₁₆, periodicity = TTT):
    cell_vectors      : [    16.2        0        0;
                                0     4.05        0;
                                0        0     4.05]u"Å"

and we discretise:

using PseudoPotentialData

pseudopotentials = PseudoFamily("dojo.nc.sr.lda.v0_4_1.standard.upf")
model_Al = model_DFT(system_Al; functionals=LDA(), temperature=1e-3,
                     symmetries=false, pseudopotentials)
basis_Al = PlaneWaveBasis(model_Al; Ecut=7, kgrid=[1, 1, 1]);

On aluminium (a metal) already for moderate system sizes (like the 8 layers we consider here) the convergence without mixing / preconditioner is slow:

# Note: DFTK uses the self-adapting LdosMixing() by default, so to truly disable
#       any preconditioning, we need to supply `mixing=SimpleMixing()` explicitly.
scfres_Al = self_consistent_field(basis_Al; tol=1e-12, mixing=SimpleMixing());
n     Energy            log10(ΔE)   log10(Δρ)   Diag   Δtime 
---   ---------------   ---------   ---------   ----   ------
  1   -36.73347947989                   -0.88   10.0    1.29s
  2   -36.64293513790   +   -1.04       -1.43    1.0    231ms
  3   +33.18892092141   +    1.84       -0.14    8.0    244ms
  4   -36.67264026992        1.84       -1.13    7.0    198ms
  5   -36.44338824298   +   -0.64       -1.28    3.0    118ms
  6   -35.57762065118   +   -0.06       -1.03    3.0    146ms
  7   -36.69797708740        0.05       -1.61    3.0    118ms
  8   -36.73833079822       -1.39       -2.06    2.0   96.4ms
  9   -36.74090670537       -2.59       -2.18    2.0    117ms
 10   -36.74189222094       -3.01       -2.24    1.0   83.1ms
 11   -36.74152486506   +   -3.43       -2.29    2.0   98.6ms
 12   -36.74235158549       -3.08       -2.71    1.0   85.0ms
 13   -36.74219348074   +   -3.80       -2.73    3.0    122ms
 14   -36.74244190006       -3.60       -3.02    2.0   93.2ms
 15   -36.74231652901   +   -3.90       -2.85    2.0    120ms
 16   -36.74244299896       -3.90       -3.14    2.0    101ms
 17   -36.74246708008       -4.62       -3.37    2.0    102ms
 18   -36.74247162769       -5.34       -3.44    2.0   96.8ms
 19   -36.74247738880       -5.24       -3.48    2.0    108ms
 20   -36.74239556849   +   -4.09       -3.10    3.0    123ms
 21   -36.74247828903       -4.08       -3.81    3.0    126ms
 22   -36.74247941044       -5.95       -3.81    2.0    119ms
 23   -36.74248031221       -6.04       -4.12    1.0   91.0ms
 24   -36.74248066055       -6.46       -4.69    1.0   85.5ms
 25   -36.74248063785   +   -7.64       -4.52    3.0    136ms
 26   -36.74248048446   +   -6.81       -4.41    3.0    127ms
 27   -36.74248066014       -6.76       -4.97    3.0    124ms
 28   -36.74248066886       -8.06       -5.19    3.0    110ms
 29   -36.74248067217       -8.48       -5.51    2.0    115ms
 30   -36.74248067237       -9.69       -5.58    2.0   95.8ms
 31   -36.74248067263       -9.59       -5.89    1.0   85.3ms
 32   -36.74248066417   +   -8.07       -5.05    3.0    142ms
 33   -36.74248067263       -8.07       -6.06    3.0    132ms
 34   -36.74248067236   +   -9.58       -5.71    3.0    128ms
 35   -36.74248067264       -9.56       -6.22    2.0    107ms
 36   -36.74248067268      -10.42       -6.83    2.0    102ms
 37   -36.74248067267   +  -10.83       -6.37    3.0    138ms
 38   -36.74248067268      -10.97       -6.69    3.0    128ms
 39   -36.74248067265   +  -10.56       -6.29    3.0    121ms
 40   -36.74248067268      -10.49       -7.30    3.0    126ms
 41   -36.74248067268      -13.45       -7.41    2.0    115ms
 42   -36.74248067268      -13.37       -7.59    2.0    184ms
 43   -36.74248067268      -13.25       -7.95    2.0   90.3ms
 44   -36.74248067268   +  -14.15       -8.15    3.0    872ms
 45   -36.74248067268   +    -Inf       -8.40    2.0   98.5ms
 46   -36.74248067268   +    -Inf       -8.30    3.0    117ms
 47   -36.74248067268   +    -Inf       -8.67    2.0   98.2ms
 48   -36.74248067268   +    -Inf       -8.51    3.0    125ms
 49   -36.74248067268      -13.67       -8.95    2.0    111ms
 50   -36.74248067268   +  -13.85       -9.17    3.0    111ms
 51   -36.74248067268   +    -Inf       -9.26    2.0    118ms
 52   -36.74248067268   +    -Inf       -9.38    3.0    113ms
 53   -36.74248067268      -13.85       -9.42    3.0    109ms
 54   -36.74248067268   +  -13.85      -10.02    2.0   99.0ms
 55   -36.74248067268      -14.15       -9.94    2.0    122ms
 56   -36.74248067268   +  -13.85      -10.48    2.0    118ms
 57   -36.74248067268      -13.85      -10.72    3.0    140ms
 58   -36.74248067268   +  -14.15      -10.81    2.0    104ms
 59   -36.74248067268   +  -13.85      -10.92    2.0   98.6ms
 60   -36.74248067268      -13.55      -10.85    3.0    117ms
 61   -36.74248067268   +  -13.85      -11.16    1.0   84.9ms
 62   -36.74248067268   +    -Inf      -11.33    3.0    124ms
 63   -36.74248067268   +    -Inf      -11.74    2.0   92.2ms
 64   -36.74248067268   +    -Inf      -11.68    3.0    126ms
 65   -36.74248067268   +    -Inf      -11.86    2.0   98.5ms
 66   -36.74248067268   +    -Inf      -11.94    3.0    107ms
 67   -36.74248067268   +    -Inf      -11.77    2.0    107ms
 68   -36.74248067268   +    -Inf      -12.84    2.0    109ms

while when using the Kerker preconditioner it is much faster:

scfres_Al = self_consistent_field(basis_Al; tol=1e-12, mixing=KerkerMixing());
n     Energy            log10(ΔE)   log10(Δρ)   Diag   Δtime 
---   ---------------   ---------   ---------   ----   ------
  1   -36.73446238208                   -0.88   11.0    934ms
  2   -36.74070585334       -2.20       -1.37    1.0    599ms
  3   -36.74193639665       -2.91       -2.17    2.0    124ms
  4   -36.74184243098   +   -4.03       -1.98    4.0    121ms
  5   -36.74244678712       -3.22       -2.72    1.0   82.3ms
  6   -36.74244942122       -5.58       -2.73    3.0    132ms
  7   -36.74247123645       -4.66       -2.91    1.0   83.0ms
  8   -36.74248006784       -5.05       -3.69    1.0   83.5ms
  9   -36.74248029365       -6.65       -3.63    3.0    129ms
 10   -36.74248061909       -6.49       -3.90    1.0   84.4ms
 11   -36.74248065726       -7.42       -4.47    1.0   84.6ms
 12   -36.74248066623       -8.05       -4.93    4.0    112ms
 13   -36.74248067203       -8.24       -5.18    2.0    119ms
 14   -36.74248067167   +   -9.44       -5.40    2.0   92.1ms
 15   -36.74248067262       -9.02       -5.74    2.0   97.2ms
 16   -36.74248067267      -10.31       -6.02    2.0    117ms
 17   -36.74248067268      -11.01       -6.26    1.0   90.3ms
 18   -36.74248067268      -12.02       -6.48    2.0    103ms
 19   -36.74248067268      -11.97       -7.06    1.0   89.7ms
 20   -36.74248067268      -13.07       -7.32    4.0    137ms
 21   -36.74248067268   +  -14.15       -7.38    2.0   90.8ms
 22   -36.74248067268      -13.67       -7.72    1.0   90.1ms
 23   -36.74248067268   +    -Inf       -8.25    3.0    105ms
 24   -36.74248067268   +    -Inf       -8.39    2.0    127ms
 25   -36.74248067268   +    -Inf       -8.68    1.0   87.8ms
 26   -36.74248067268      -14.15       -8.75    3.0    118ms
 27   -36.74248067268   +  -14.15       -9.20    1.0   89.3ms
 28   -36.74248067268   +    -Inf       -9.50    3.0    112ms
 29   -36.74248067268   +    -Inf       -9.85    2.0    120ms
 30   -36.74248067268   +    -Inf      -10.14    2.0   91.3ms
 31   -36.74248067268   +    -Inf      -10.65    2.0    121ms
 32   -36.74248067268   +    -Inf      -10.88    2.0   96.6ms
 33   -36.74248067268      -14.15      -11.27    2.0    120ms
 34   -36.74248067268   +    -Inf      -11.62    2.0    103ms
 35   -36.74248067268   +  -14.15      -11.91    2.0    115ms
 36   -36.74248067268      -13.85      -12.34    1.0   89.6ms

Given this scfres_Al we construct functions representing $\varepsilon^\dagger$ and $P^{-1}$:

# Function, which applies P^{-1} for the case of KerkerMixing
Pinv_Kerker(δρ) = DFTK.mix_density(KerkerMixing(), basis_Al, δρ)

# Function which applies ε† = 1 - χ0 K
function epsilon(δρ)
    δV   = apply_kernel(basis_Al, δρ; ρ=scfres_Al.ρ).δVρ
    χ0δV = apply_χ0(scfres_Al, δV).δρ
    δρ - χ0δV
end
epsilon (generic function with 1 method)

With these functions available we can now compute the desired eigenvalues. For simplicity we only consider the first few largest ones.

using KrylovKit
λ_Simple, X_Simple = eigsolve(epsilon, randn(size(scfres_Al.ρ)), 3, :LM;
                              tol=1e-3, eager=true, verbosity=2)
λ_Simple_max = maximum(real.(λ_Simple))
44.02448902021249

The smallest eigenvalue is a bit more tricky to obtain, so we will just assume

λ_Simple_min = 0.952
0.952

This makes the condition number around 30:

cond_Simple = λ_Simple_max / λ_Simple_min
46.24421115568539

This does not sound large compared to the condition numbers you might know from linear systems.

However, this is sufficient to cause a notable slowdown, which would be even more pronounced if we did not use Anderson, since we also would need to drastically reduce the damping (try it!).

Having computed the eigenvalues of the dielectric matrix we can now also look at the eigenmodes, which are responsible for the bad convergence behaviour. The largest eigenmode for example:

using Statistics
using Plots
mode_xy = mean(real.(X_Simple[1]), dims=3)[:, :, 1, 1]  # Average along z axis
heatmap(mode_xy', c=:RdBu_11, aspect_ratio=1, grid=false,
        legend=false, clim=(-0.006, 0.006))

This mode can be physically interpreted as the reason why this SCF converges slowly. For example in this case it displays a displacement of electron density from the centre to the extremal parts of the unit cell. This phenomenon is called charge-sloshing.

We repeat the exercise for the Kerker-preconditioned dielectric operator:

λ_Kerker, X_Kerker = eigsolve(Pinv_Kerker ∘ epsilon,
                              randn(size(scfres_Al.ρ)), 3, :LM;
                              tol=1e-3, eager=true, verbosity=2)

mode_xy = mean(real.(X_Kerker[1]), dims=3)[:, :, 1, 1]  # Average along z axis
heatmap(mode_xy', c=:RdBu_11, aspect_ratio=1, grid=false,
        legend=false, clim=(-0.006, 0.006))

Clearly the charge-sloshing mode is no longer dominating.

The largest eigenvalue is now

maximum(real.(λ_Kerker))
4.7235909684646495

Since the smallest eigenvalue in this case remains of similar size (it is now around 0.8), this implies that the conditioning improves noticeably when KerkerMixing is used.

Note: Since LdosMixing requires solving a linear system at each application of $P^{-1}$, determining the eigenvalues of $P^{-1} \varepsilon^\dagger$ is slightly more expensive and thus not shown. The results are similar to KerkerMixing, however.

We could repeat the exercise for an insulating system (e.g. a Helium chain). In this case you would notice that the condition number without mixing is actually smaller than the condition number with Kerker mixing. In other words employing Kerker mixing makes the convergence worse. A closer investigation of the eigenvalues shows that Kerker mixing reduces the smallest eigenvalue of the dielectric operator this time, while keeping the largest value unchanged. Overall the conditioning thus workens.

Takeaways:

  • For metals the conditioning of the dielectric matrix increases steeply with system size.
  • The Kerker preconditioner tames this and makes SCFs on large metallic systems feasible by keeping the condition number of order 1.
  • For insulating systems the best approach is to not use any mixing.
  • The ideal mixing strongly depends on the dielectric properties of system which is studied (metal versus insulator versus semiconductor).