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 ASEconvert
using DFTK
using LazyArtifacts

ase_Al    = ase.build.bulk("Al"; cubic=true) * pytuple((4, 1, 1))
system_Al = attach_psp(pyconvert(AbstractSystem, ase_Al);
                       Al=artifact"pd_nc_sr_pbe_standard_0.4.1_upf/Al.upf")
FlexibleSystem(Al₁₆, periodic = TTT):
    bounding_box      : [    16.2        0        0;
                                0     4.05        0;
                                0        0     4.05]u"Å"

      .---------------------------------------.  
     /|                                       |  
    * |                                       |  
    |Al        Al        Al        Al         |  
    | |                                       |  
    | .--Al--------Al--------Al--------Al-----.  
    |/    Al        Al        Al        Al   /   
    Al--------Al--------Al--------Al--------*    

and we discretise:

model_Al = model_LDA(system_Al; temperature=1e-3, symmetries=false)
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   -35.98111978732                   -0.86   11.0    349ms
  2   -35.97466599050   +   -2.19       -1.56    1.0   86.8ms
  3   -18.88250354421   +    1.23       -0.45    6.0    206ms
  4   -35.43700535480        1.22       -1.04    8.0    200ms
  5   -35.88270863551       -0.35       -1.39    4.0    142ms
  6   -35.83250179931   +   -1.30       -1.46    3.0    124ms
  7   -35.86609669551       -1.47       -1.51    4.0    146ms
  8   -35.98898969440       -0.91       -2.23    2.0    109ms
  9   -35.98813983041   +   -3.07       -2.12    4.0    159ms
 10   -35.98760744648   +   -3.27       -2.08    3.0    128ms
 11   -35.98938708140       -2.75       -2.56    2.0    122ms
 12   -35.98952696663       -3.85       -2.62    2.0    107ms
 13   -35.98962263055       -4.02       -3.34    1.0    112ms
 14   -35.98962120010   +   -5.84       -3.29    4.0    140ms
 15   -35.98953946748   +   -4.09       -3.05    3.0    141ms
 16   -35.98962544654       -4.07       -3.62    4.0    141ms
 17   -35.98963153045       -5.22       -4.13    2.0    112ms
 18   -35.98963084184   +   -6.16       -3.93    3.0    139ms
 19   -35.98963128650       -6.35       -4.12    3.0    137ms
 20   -35.98963193096       -6.19       -4.59    2.0    103ms
 21   -35.98963190783   +   -7.64       -4.48    4.0    204ms
 22   -35.98963197605       -7.17       -4.96    2.0    123ms
 23   -35.98963197849       -8.61       -4.89    3.0    168ms
 24   -35.98963198582       -8.14       -5.65    2.0    108ms
 25   -35.98963198534   +   -9.32       -5.47    3.0    145ms
 26   -35.98963198598       -9.19       -5.64    2.0    111ms
 27   -35.98963198573   +   -9.60       -5.66    3.0    116ms
 28   -35.98963198608       -9.45       -6.11    1.0   93.8ms
 29   -35.98963198599   +  -10.03       -5.97    3.0    169ms
 30   -35.98963198609       -9.99       -6.24    3.0    130ms
 31   -35.98963198613      -10.45       -6.75    3.0    119ms
 32   -35.98963198613      -11.58       -6.98    3.0    138ms
 33   -35.98963198613      -12.27       -7.23    2.0    107ms
 34   -35.98963198613      -12.83       -7.62    2.0    110ms
 35   -35.98963198613   +  -12.85       -7.43    3.0    141ms
 36   -35.98963198613      -12.97       -7.74    3.0    140ms
 37   -35.98963198613      -13.55       -7.98    2.0    147ms
 38   -35.98963198613   +    -Inf       -8.29    2.0    101ms
 39   -35.98963198613      -13.85       -8.42    2.0    129ms
 40   -35.98963198613   +    -Inf       -8.75    2.0    100ms
 41   -35.98963198613   +    -Inf       -9.33    2.0    102ms
 42   -35.98963198613   +  -14.15       -8.94    4.0    166ms
 43   -35.98963198613      -14.15       -9.21    4.0    149ms
 44   -35.98963198613   +    -Inf       -9.53    3.0    118ms
 45   -35.98963198613   +    -Inf       -9.88    1.0    120ms
 46   -35.98963198613   +    -Inf       -9.98    2.0    129ms
 47   -35.98963198613   +  -14.15       -9.63    3.0    130ms
 48   -35.98963198613      -14.15       -9.99    4.0    149ms
 49   -35.98963198613   +  -14.15      -10.33    3.0    120ms
 50   -35.98963198613   +  -14.15      -10.75    1.0   95.1ms
 51   -35.98963198613      -13.85      -10.45    3.0    138ms
 52   -35.98963198613   +    -Inf      -11.25    3.0    118ms
 53   -35.98963198613   +    -Inf      -11.21    2.0    175ms
 54   -35.98963198613   +  -14.15      -11.19    5.0    143ms
 55   -35.98963198613   +    -Inf      -11.50    2.0    108ms
 56   -35.98963198613      -14.15      -11.12    4.0    149ms
 57   -35.98963198613   +  -13.85      -11.94    3.0    155ms
 58   -35.98963198613      -13.85      -11.77    4.0    155ms
 59   -35.98963198613   +  -13.85      -11.54    3.0    131ms
 60   -35.98963198613   +    -Inf      -12.16    4.0    194ms

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   -35.98113248012                   -0.86   12.0    366ms
  2   -35.98740271198       -2.20       -1.34    1.0   90.5ms
  3   -35.98680602319   +   -3.22       -1.64    7.0    143ms
  4   -35.98937728770       -2.59       -2.26    1.0   91.0ms
  5   -35.98953268071       -3.81       -2.39    4.0    146ms
  6   -35.98960474743       -4.14       -2.62    2.0    133ms
  7   -35.98961595114       -4.95       -2.91    2.0    104ms
  8   -35.98962702554       -4.96       -3.04    5.0    132ms
  9   -35.98963039368       -5.47       -3.54    1.0   94.0ms
 10   -35.98963185497       -5.84       -4.15    4.0    134ms
 11   -35.98963191213       -7.24       -4.32    6.0    159ms
 12   -35.98963186936   +   -7.37       -4.35    5.0    136ms
 13   -35.98963196608       -7.01       -4.83    2.0    107ms
 14   -35.98963198553       -7.71       -5.34    3.0    174ms
 15   -35.98963198609       -9.25       -5.76    8.0    148ms
 16   -35.98963198608   +  -10.98       -5.97    2.0    131ms
 17   -35.98963198613      -10.29       -6.59    2.0    106ms
 18   -35.98963198613      -12.18       -6.63    3.0    139ms
 19   -35.98963198613      -12.72       -7.01    1.0    104ms
 20   -35.98963198613      -12.81       -7.19    3.0    124ms
 21   -35.98963198613      -13.45       -7.60    2.0    106ms
 22   -35.98963198613      -13.67       -7.82    5.0    175ms
 23   -35.98963198613   +    -Inf       -8.33    7.0    142ms
 24   -35.98963198613   +  -13.85       -8.69    3.0    142ms
 25   -35.98963198613      -13.85       -9.10    2.0    113ms
 26   -35.98963198613   +    -Inf       -9.30    4.0    150ms
 27   -35.98963198613   +  -14.15       -9.71    2.0    105ms
 28   -35.98963198613      -14.15      -10.20   10.0    165ms
 29   -35.98963198613   +    -Inf      -10.28    3.0    139ms
 30   -35.98963198613   +  -14.15      -10.64    2.0    137ms
 31   -35.98963198613      -14.15      -10.83    3.0    135ms
 32   -35.98963198613   +  -13.85      -11.10    2.0    105ms
 33   -35.98963198613   +    -Inf      -11.64    3.0    141ms
 34   -35.98963198613      -14.15      -11.93    6.0    138ms
┌ Warning: Eigensolver not converged
  n_iter =
   1-element Vector{Int64}:
    2
@ DFTK ~/work/DFTK.jl/DFTK.jl/src/scf/self_consistent_field.jl:76
 35   -35.98963198613      -13.67      -12.07    2.0    167ms

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

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

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

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