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.73538931630                   -0.88   11.0    347ms
  2   -36.73822349993       -2.55       -1.65    1.0   94.1ms
  3   -33.76418002761   +    0.47       -0.81    4.0    160ms
  4   -34.98147220594        0.09       -0.91    4.0    156ms
  5   -36.70112594611        0.24       -1.52    3.0    144ms
  6   -36.71955464655       -1.73       -1.86    2.0    103ms
  7   -36.40747295958   +   -0.51       -1.31    4.0    152ms
  8   -36.74111058664       -0.48       -2.23    3.0    134ms
  9   -36.74117087401       -4.22       -2.09    2.0    123ms
 10   -36.74157013744       -3.40       -2.22    2.0    124ms
 11   -36.74236945370       -3.10       -2.73    2.0    113ms
 12   -36.74241749457       -4.32       -2.71    2.0    113ms
 13   -36.74250986915       -4.03       -3.33    1.0   95.6ms
 14   -36.74250785609   +   -5.70       -3.39    3.0    136ms
 15   -36.74237872580   +   -3.89       -2.98    3.0    136ms
 16   -36.74247226870       -4.03       -3.21    3.0    136ms
 17   -36.74251429726       -4.38       -3.80    2.0    106ms
 18   -36.74251227494   +   -5.69       -3.70    3.0    129ms
 19   -36.74251341709       -5.94       -3.94    2.0    140ms
 20   -36.74250219795   +   -4.95       -3.52    3.0    141ms
 21   -36.74251446570       -4.91       -4.13    3.0    136ms
 22   -36.74251465799       -6.72       -4.43    2.0    130ms
 23   -36.74251475149       -7.03       -4.71    1.0   92.0ms
 24   -36.74251477033       -7.72       -5.17    3.0    120ms
 25   -36.74251477024   +  -10.09       -5.17    3.0    139ms
 26   -36.74251477195       -8.77       -5.48    1.0   91.8ms
 27   -36.74251477057   +   -8.86       -5.30    3.0    151ms
 28   -36.74251477302       -8.61       -6.01    2.0    111ms
 29   -36.74251477298   +  -10.40       -6.04    3.0    141ms
 30   -36.74251477300      -10.73       -6.17    2.0    111ms
 31   -36.74251477303      -10.60       -6.25    2.0    107ms
 32   -36.74251477283   +   -9.72       -5.90    3.0    130ms
 33   -36.74251477302       -9.72       -6.44    3.0    135ms
 34   -36.74251477304      -10.91       -7.12    2.0    101ms
 35   -36.74251477304   +  -11.89       -6.74    3.0    145ms
 36   -36.74251477304      -11.96       -7.06    2.0    146ms
 37   -36.74251477303   +  -11.60       -6.78    3.0    131ms
 38   -36.74251477304      -11.56       -7.54    3.0    127ms
 39   -36.74251477304   +  -12.66       -7.29    3.0    144ms
 40   -36.74251477304      -13.11       -7.41    3.0    131ms
 41   -36.74251477304      -13.45       -7.45    2.0    111ms
 42   -36.74251477304      -13.11       -7.51    2.0    120ms
 43   -36.74251477304      -13.45       -7.82    2.0    106ms
 44   -36.74251477304   +  -13.55       -7.72    3.0    150ms
 45   -36.74251477304      -13.37       -8.01    2.0    112ms
 46   -36.74251477304   +  -13.85       -8.23    2.0    117ms
 47   -36.74251477304      -13.85       -8.85    2.0    129ms
 48   -36.74251477304   +  -13.85       -8.90    3.0    139ms
 49   -36.74251477304      -13.67       -9.49    1.0   91.3ms
 50   -36.74251477304   +  -14.15       -9.15    2.0    129ms
 51   -36.74251477304   +  -14.15       -9.32    3.0    132ms
 52   -36.74251477304      -14.15       -9.44    1.0   96.5ms
 53   -36.74251477304   +  -13.85       -9.33    3.0    139ms
 54   -36.74251477304   +    -Inf      -10.12    2.0    107ms
 55   -36.74251477304      -13.85      -10.18    3.0    145ms
 56   -36.74251477304   +    -Inf       -9.92    3.0    135ms
 57   -36.74251477304   +    -Inf      -10.64    2.0    106ms
 58   -36.74251477304   +  -14.15      -10.51    2.0    130ms
 59   -36.74251477304      -14.15      -10.64    2.0    121ms
 60   -36.74251477304   +  -14.15      -11.08    2.0    100ms
 61   -36.74251477304      -14.15      -11.14    2.0    115ms
 62   -36.74251477304   +    -Inf      -11.25    2.0    128ms
 63   -36.74251477304   +  -14.15      -11.49    1.0   96.3ms
 64   -36.74251477304      -14.15      -11.60    2.0    129ms
 65   -36.74251477304   +  -14.15      -11.18    3.0    144ms
 66   -36.74251477304   +  -14.15      -11.19    3.0    146ms
 67   -36.74251477304   +    -Inf      -12.02    3.0    137ms

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.73417006400                   -0.88   10.0    359ms
  2   -36.73994903878       -2.24       -1.36    1.0   93.1ms
  3   -36.74001698069       -4.17       -1.66    3.0    118ms
  4   -36.74232880203       -2.64       -2.25    1.0    165ms
  5   -36.74244148624       -3.95       -2.46    3.0    128ms
  6   -36.74246812135       -4.57       -2.48    5.0    718ms
  7   -36.74250202371       -4.47       -3.03    1.0   90.8ms
  8   -36.74251273961       -4.97       -3.24    2.0   99.0ms
  9   -36.74251412272       -5.86       -3.57    2.0   98.1ms
 10   -36.74251470185       -6.24       -4.16    2.0    105ms
 11   -36.74251474537       -7.36       -4.46    6.0    145ms
 12   -36.74251476270       -7.76       -4.88    2.0    140ms
 13   -36.74251476253   +   -9.77       -4.98    3.0    197ms
 14   -36.74251477276       -7.99       -5.63    2.0    132ms
 15   -36.74251477262   +   -9.85       -5.63    3.0    143ms
 16   -36.74251477301       -9.41       -6.09    1.0   93.2ms
 17   -36.74251477302      -10.79       -6.31    3.0    136ms
 18   -36.74251477304      -10.85       -6.93    1.0   93.0ms
 19   -36.74251477304      -12.02       -7.22    3.0    149ms
 20   -36.74251477304      -12.92       -7.37    2.0    128ms
 21   -36.74251477304      -13.85       -7.78    1.0   92.7ms
 22   -36.74251477304   +    -Inf       -7.90    2.0    137ms
 23   -36.74251477304   +    -Inf       -8.34    1.0   94.0ms
 24   -36.74251477304   +    -Inf       -8.85    3.0    122ms
 25   -36.74251477304      -13.85       -9.09    3.0    142ms
 26   -36.74251477304   +    -Inf       -9.31    1.0   93.9ms
 27   -36.74251477304   +  -13.85       -9.47    2.0    109ms
 28   -36.74251477304   +    -Inf       -9.87    1.0   98.2ms
 29   -36.74251477304      -14.15      -10.25    2.0    132ms
 30   -36.74251477304      -14.15      -10.47    2.0    104ms
 31   -36.74251477304   +  -14.15      -10.61    2.0    106ms
 32   -36.74251477304      -14.15      -10.98    1.0   98.1ms
 33   -36.74251477304   +  -14.15      -11.19    4.0    148ms
 34   -36.74251477304   +    -Inf      -11.73    2.0    103ms
 35   -36.74251477304   +  -14.15      -11.52    3.0    147ms
 36   -36.74251477304   +    -Inf      -12.18    2.0    114ms

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.ρ)
    χ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.024488232797744

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.24421032856906

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))
Example block output

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))
Example block output

Clearly the charge-sloshing mode is no longer dominating.

The largest eigenvalue is now

maximum(real.(λ_Kerker))
4.723588044107179

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).