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
import Main: @artifact_str # hide
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.98102108231 -0.86 11.0 481ms 2 -35.93740344051 + -1.36 -1.53 1.0 90.9ms 3 +13.81813078165 + 1.70 -0.22 9.0 279ms 4 -35.97388977190 1.70 -1.41 9.0 271ms 5 -35.85878994001 + -0.94 -1.48 3.0 132ms 6 -35.54506961181 + -0.50 -1.24 4.0 201ms 7 -35.98339178078 -0.36 -1.91 3.0 153ms 8 -35.98858884652 -2.28 -2.16 3.0 128ms 9 -35.98811883740 + -3.33 -2.18 2.0 138ms 10 -35.98928890613 -2.93 -2.40 1.0 113ms 11 -35.98963477873 -3.46 -2.71 2.0 98.2ms 12 -35.98961944123 + -4.81 -2.68 3.0 127ms 13 -35.98975753889 -3.86 -3.32 2.0 115ms 14 -35.98976234207 -5.32 -3.20 2.0 163ms 15 -35.98969186826 + -4.15 -3.10 3.0 128ms 16 -35.98964409191 + -4.32 -3.00 4.0 152ms 17 -35.98972976939 -4.07 -3.21 3.0 126ms 18 -35.98976431449 -4.46 -3.82 5.0 132ms 19 -35.98976268419 + -5.79 -3.73 3.0 148ms 20 -35.98976626245 -5.45 -4.07 3.0 133ms 21 -35.98976628195 -7.71 -4.17 2.0 112ms 22 -35.98976673201 -6.35 -4.44 2.0 116ms 23 -35.98976677884 -7.33 -4.91 2.0 104ms 24 -35.98976673937 + -7.40 -4.58 3.0 142ms 25 -35.98976676542 -7.58 -4.70 3.0 136ms 26 -35.98976678170 -7.79 -5.17 2.0 133ms 27 -35.98976678412 -8.62 -5.66 2.0 119ms 28 -35.98976678165 + -8.61 -5.32 4.0 169ms 29 -35.98976678451 -8.54 -6.17 3.0 150ms 30 -35.98976678450 + -11.02 -6.21 3.0 156ms 31 -35.98976678451 -12.39 -6.37 2.0 118ms 32 -35.98976678452 -10.77 -6.82 1.0 102ms 33 -35.98976678445 + -10.16 -6.12 4.0 172ms 34 -35.98976678452 -10.19 -6.60 5.0 181ms 35 -35.98976678452 -11.23 -7.10 3.0 139ms 36 -35.98976678452 + -12.97 -7.08 5.0 149ms 37 -35.98976678452 -12.25 -7.42 2.0 136ms 38 -35.98976678452 -13.25 -7.66 2.0 140ms 39 -35.98976678452 -13.67 -8.07 2.0 117ms 40 -35.98976678452 + -13.85 -8.19 3.0 147ms 41 -35.98976678452 -14.15 -8.26 2.0 114ms 42 -35.98976678452 -14.15 -8.58 2.0 119ms 43 -35.98976678452 + -Inf -8.25 4.0 155ms 44 -35.98976678452 + -14.15 -9.08 3.0 159ms 45 -35.98976678452 + -14.15 -9.53 2.0 153ms 46 -35.98976678452 -13.85 -8.76 5.0 190ms 47 -35.98976678452 + -Inf -9.67 4.0 171ms 48 -35.98976678452 + -Inf -9.61 3.0 140ms 49 -35.98976678452 + -Inf -9.79 2.0 107ms 50 -35.98976678452 + -Inf -10.43 1.0 100ms 51 -35.98976678452 + -Inf -10.32 4.0 172ms 52 -35.98976678452 + -Inf -10.74 2.0 150ms 53 -35.98976678452 + -13.85 -10.56 2.0 149ms 54 -35.98976678452 -13.85 -11.06 2.0 119ms 55 -35.98976678452 + -Inf -11.09 2.0 137ms 56 -35.98976678452 + -Inf -11.41 1.0 105ms 57 -35.98976678452 + -Inf -11.81 1.0 97.6ms 58 -35.98976678452 + -14.15 -11.72 4.0 159ms 59 -35.98976678452 + -14.15 -11.86 3.0 143ms 60 -35.98976678452 -13.85 -12.02 5.0 169ms
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.98070125314 -0.86 12.0 465ms 2 -35.98705826618 -2.20 -1.34 1.0 91.9ms 3 -35.98698320197 + -4.12 -1.67 8.0 231ms 4 -35.98960803814 -2.58 -2.28 1.0 92.9ms 5 -35.98927919880 + -3.48 -2.47 5.0 151ms 6 -35.98965475375 -3.43 -2.49 2.0 115ms 7 -35.98970338365 -4.31 -2.97 1.0 96.0ms 8 -35.98976307463 -4.22 -3.55 7.0 167ms 9 -35.98976481166 -5.76 -3.53 3.0 152ms 10 -35.98976608186 -5.90 -3.79 1.0 97.6ms 11 -35.98976668309 -6.22 -4.31 2.0 110ms 12 -35.98976674865 -7.18 -4.63 3.0 184ms 13 -35.98976678038 -7.50 -5.06 6.0 154ms 14 -35.98976678433 -8.40 -5.53 3.0 152ms 15 -35.98976678441 -10.12 -5.79 7.0 168ms 16 -35.98976678449 -10.08 -6.05 3.0 152ms 17 -35.98976678452 -10.54 -6.38 2.0 111ms 18 -35.98976678452 -11.40 -6.80 4.0 178ms 19 -35.98976678452 -12.10 -7.04 3.0 152ms 20 -35.98976678452 -12.10 -7.56 3.0 131ms 21 -35.98976678452 -13.67 -7.76 8.0 203ms 22 -35.98976678452 + -13.85 -7.93 2.0 112ms 23 -35.98976678452 -13.55 -8.56 1.0 98.7ms 24 -35.98976678452 + -14.15 -9.01 4.0 179ms 25 -35.98976678452 + -Inf -9.16 6.0 189ms 26 -35.98976678452 + -Inf -9.56 5.0 143ms 27 -35.98976678452 + -Inf -10.06 3.0 153ms 28 -35.98976678452 + -Inf -10.28 3.0 150ms 29 -35.98976678452 + -13.85 -10.43 2.0 115ms 30 -35.98976678452 -14.15 -10.94 3.0 132ms 31 -35.98976678452 -14.15 -11.05 4.0 161ms 32 -35.98976678452 + -Inf -11.63 1.0 100ms 33 -35.98976678452 + -Inf -11.61 10.0 258ms 34 -35.98976678452 + -13.85 -12.07 1.0 100ms
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))
[ Info: Arnoldi schursolve in iter 1, krylovdim = 3: 0 values converged, normres = (2.97e+00, 3.06e+00, 2.72e-02) [ Info: Arnoldi schursolve in iter 1, krylovdim = 4: 0 values converged, normres = (2.67e-01, 1.66e+00, 3.05e+00) [ Info: Arnoldi schursolve in iter 1, krylovdim = 5: 0 values converged, normres = (1.09e-02, 7.11e-01, 1.09e+00) [ Info: Arnoldi schursolve in iter 1, krylovdim = 6: 1 values converged, normres = (5.89e-04, 2.18e-01, 5.26e-01) [ Info: Arnoldi schursolve in iter 1, krylovdim = 7: 1 values converged, normres = (1.66e-05, 3.19e-02, 1.32e-01) [ Info: Arnoldi schursolve in iter 1, krylovdim = 8: 1 values converged, normres = (7.99e-07, 9.06e-03, 7.21e-02) [ Info: Arnoldi schursolve in iter 1, krylovdim = 9: 2 values converged, normres = (1.00e-08, 5.39e-04, 7.20e-03) [ Info: Arnoldi schursolve in iter 1, krylovdim = 10: 2 values converged, normres = (1.23e-09, 3.04e-04, 6.17e-03) [ Info: Arnoldi schursolve in iter 1, krylovdim = 11: 1 values converged, normres = (3.06e-09, 1.24e+01, 7.04e-04) [ Info: Arnoldi schursolve in iter 1, krylovdim = 12: 1 values converged, normres = (4.79e-08, 5.35e-01, 4.92e-05) [ Info: Arnoldi schursolve in iter 1, krylovdim = 13: 0 values converged, normres = (5.53e-03, 1.75e-03, 2.41e-06) ┌ Info: Arnoldi eigsolve finished after 1 iterations: │ * 3 eigenvalues converged │ * norm of residuals = (2.9211441553350943e-6, 2.9211441553350943e-6, 8.19984905525365e-8) └ * number of operations = 14
44.387452136281354
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.62547493306865
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))
[ Info: Arnoldi schursolve in iter 1, krylovdim = 3: 0 values converged, normres = (6.81e-01, 4.13e-02, 1.20e-01) [ Info: Arnoldi schursolve in iter 1, krylovdim = 4: 0 values converged, normres = (2.83e-01, 8.37e-01, 2.28e-02) [ Info: Arnoldi schursolve in iter 1, krylovdim = 5: 0 values converged, normres = (1.22e-01, 6.12e-01, 7.81e-01) [ Info: Arnoldi schursolve in iter 1, krylovdim = 6: 0 values converged, normres = (8.55e-02, 5.80e-01, 3.68e-01) [ Info: Arnoldi schursolve in iter 1, krylovdim = 7: 0 values converged, normres = (9.00e-02, 5.85e-01, 8.16e-01) [ Info: Arnoldi schursolve in iter 1, krylovdim = 8: 0 values converged, normres = (1.61e-01, 3.13e-01, 3.06e-01) [ Info: Arnoldi schursolve in iter 1, krylovdim = 9: 0 values converged, normres = (5.80e-02, 7.00e-02, 1.07e-01) [ Info: Arnoldi schursolve in iter 1, krylovdim = 10: 0 values converged, normres = (6.94e-03, 8.88e-03, 2.07e-02) [ Info: Arnoldi schursolve in iter 1, krylovdim = 11: 2 values converged, normres = (3.20e-04, 4.19e-04, 1.31e-03) ┌ Info: Arnoldi eigsolve finished after 1 iterations: │ * 3 eigenvalues converged │ * norm of residuals = (8.267288063608711e-5, 0.00011303307831726232, 0.0005174940054455095) └ * number of operations = 12
Clearly the charge-sloshing mode is no longer dominating.
The largest eigenvalue is now
maximum(real.(λ_Kerker))
4.685462613233086
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: