In this example, we show how to define custom solvers. Our system will again be silicon, because we are not very imaginative
using DFTK, LinearAlgebra
a = 10.26
lattice = a / 2 * [[0 1 1.];
[1 0 1.];
[1 1 0.]]
Si = ElementPsp(:Si, psp=load_psp("hgh/lda/Si-q4"))
atoms = [Si, Si]
positions = [ones(3)/8, -ones(3)/8]
# We take very (very) crude parameters
model = model_LDA(lattice, atoms, positions)
basis = PlaneWaveBasis(model; Ecut=5, kgrid=[1, 1, 1]);
We define our custom fix-point solver: simply a damped fixed-point
function my_fp_solver(f, x0, max_iter; tol)
mixing_factor = .7
x = x0
fx = f(x)
for n = 1:max_iter
inc = fx - x
if norm(inc) < tol
break
end
x = x + mixing_factor * inc
fx = f(x)
end
(fixpoint=x, converged=norm(fx-x) < tol)
end;
Our eigenvalue solver just forms the dense matrix and diagonalizes it explicitly (this only works for very small systems)
function my_eig_solver(A, X0; maxiter, tol, kwargs...)
n = size(X0, 2)
A = Array(A)
E = eigen(A)
λ = E.values[1:n]
X = E.vectors[:, 1:n]
(; λ, X, residual_norms=[], iterations=0, converged=true, n_matvec=0)
end;
Finally we also define our custom mixing scheme. It will be a mixture
of simple mixing (for the first 2 steps) and than default to Kerker mixing.
In the mixing interface δF
is $(ρ_\text{out} - ρ_\text{in})$, i.e.
the difference in density between two subsequent SCF steps and the mix
function returns $δρ$, which is added to $ρ_\text{in}$ to yield $ρ_\text{next}$,
the density for the next SCF step.
struct MyMixing
n_simple # Number of iterations for simple mixing
end
MyMixing() = MyMixing(2)
function DFTK.mix_density(mixing::MyMixing, basis, δF; n_iter, kwargs...)
if n_iter <= mixing.n_simple
return δF # Simple mixing -> Do not modify update at all
else
# Use the default KerkerMixing from DFTK
DFTK.mix_density(KerkerMixing(), basis, δF; kwargs...)
end
end
That's it! Now we just run the SCF with these solvers
scfres = self_consistent_field(basis;
tol=1e-8,
solver=my_fp_solver,
eigensolver=my_eig_solver,
mixing=MyMixing());
n Energy log10(ΔE) log10(Δρ) Diag --- --------------- --------- --------- ---- 1 -7.224299812278 -0.48 0.0 2 -7.247842676288 -1.63 -0.87 0.0 3 -7.251068302542 -2.49 -1.31 0.0 4 -7.251272329754 -3.69 -1.62 0.0 5 -7.251322045645 -4.30 -1.92 0.0 6 -7.251334385000 -4.91 -2.22 0.0 7 -7.251337572211 -5.50 -2.51 0.0 8 -7.251338438752 -6.06 -2.79 0.0 9 -7.251338687652 -6.60 -3.06 0.0 10 -7.251338762977 -7.12 -3.32 0.0 11 -7.251338786827 -7.62 -3.58 0.0 12 -7.251338794658 -8.11 -3.83 0.0 13 -7.251338797301 -8.58 -4.08 0.0 14 -7.251338798212 -9.04 -4.31 0.0 15 -7.251338798530 -9.50 -4.55 0.0 16 -7.251338798642 -9.95 -4.78 0.0 17 -7.251338798682 -10.40 -5.01 0.0 18 -7.251338798697 -10.85 -5.24 0.0 19 -7.251338798702 -11.29 -5.47 0.0 20 -7.251338798704 -11.74 -5.69 0.0 21 -7.251338798704 -12.18 -5.92 0.0 22 -7.251338798704 -12.62 -6.14 0.0 23 -7.251338798704 -13.07 -6.37 0.0 24 -7.251338798705 -13.56 -6.59 0.0 25 -7.251338798705 -13.82 -6.81 0.0 26 -7.251338798705 -14.75 -7.02 0.0 27 -7.251338798705 -14.75 -7.24 0.0 28 -7.251338798705 + -Inf -7.37 0.0 29 -7.251338798705 + -15.05 -7.42 0.0 30 -7.251338798705 -15.05 -7.61 0.0 31 -7.251338798705 + -13.88 -7.05 0.0 32 -7.251338798705 -13.88 -7.28 0.0 33 -7.251338798705 + -Inf -7.61 0.0 34 -7.251338798705 -14.45 -7.91 0.0 35 -7.251338798704 + -13.13 -6.76 0.0 36 -7.251338798705 -13.17 -7.00 0.0 37 -7.251338798705 -14.10 -7.36 0.0 38 -7.251338798705 + -14.57 -7.68 0.0 39 -7.251338798705 + -14.27 -7.63 0.0 40 -7.251338798705 -14.27 -7.68 0.0 41 -7.251338798705 -14.75 -7.76 0.0 42 -7.251338798705 + -15.05 -7.85 0.0 43 -7.251338798705 + -14.75 -7.84 0.0 44 -7.251338798705 -15.05 -7.71 0.0 45 -7.251338798705 + -15.05 -7.56 0.0 46 -7.251338798705 + -14.75 -7.84 0.0 47 -7.251338798705 -14.57 -7.67 0.0 48 -7.251338798705 + -Inf -7.58 0.0 49 -7.251338798705 + -Inf -7.51 0.0 50 -7.251338798705 + -15.05 -7.50 0.0 51 -7.251338798705 + -14.57 -7.44 0.0 52 -7.251338798705 -14.35 -7.68 0.0 53 -7.251338798705 + -14.57 -7.89 0.0 54 -7.251338798705 + -13.91 -7.07 0.0 55 -7.251338798705 -13.91 -7.33 0.0 56 -7.251338798705 -14.75 -7.56 0.0 57 -7.251338798705 + -Inf -7.77 0.0 58 -7.251338798705 -15.05 -7.69 0.0 59 -7.251338798705 + -Inf -7.53 0.0 60 -7.251338798705 + -15.05 -7.65 0.0 61 -7.251338798705 + -Inf -7.77 0.0 62 -7.251338798705 + -14.45 -7.53 0.0 63 -7.251338798705 -14.45 -7.71 0.0 64 -7.251338798705 + -Inf -7.56 0.0 65 -7.251338798705 + -15.05 -7.69 0.0 66 -7.251338798705 -15.05 -7.58 0.0 67 -7.251338798705 + -15.05 -7.79 0.0 68 -7.251338798705 + -Inf -7.67 0.0 69 -7.251338798705 + -Inf -7.53 0.0 70 -7.251338798705 + -15.05 -7.42 0.0 71 -7.251338798705 + -Inf -7.61 0.0 72 -7.251338798705 -15.05 -7.89 0.0 73 -7.251338798705 + -14.75 -7.73 0.0 74 -7.251338798705 -14.57 -7.47 0.0 75 -7.251338798705 + -Inf -7.56 0.0 76 -7.251338798705 + -14.57 -7.46 0.0 77 -7.251338798705 -15.05 -7.61 0.0 78 -7.251338798705 -14.45 -7.66 0.0 79 -7.251338798705 + -14.75 -7.49 0.0 80 -7.251338798705 -14.75 -7.74 0.0 81 -7.251338798705 + -13.85 -7.17 0.0 82 -7.251338798705 -13.91 -7.41 0.0 83 -7.251338798705 + -15.05 -7.75 0.0 84 -7.251338798705 + -14.75 -7.71 0.0 85 -7.251338798705 -14.35 -7.85 0.0 86 -7.251338798705 + -13.75 -7.05 0.0 87 -7.251338798705 -13.80 -7.30 0.0 88 -7.251338798705 -14.75 -7.61 0.0 89 -7.251338798705 + -14.27 -7.39 0.0 90 -7.251338798705 -14.75 -7.62 0.0 91 -7.251338798705 + -Inf -7.85 0.0 92 -7.251338798705 -14.57 -7.55 0.0 93 -7.251338798705 + -14.57 -7.64 0.0 94 -7.251338798705 -14.75 -7.80 0.0 95 -7.251338798704 + -13.19 -6.75 0.0 96 -7.251338798705 -13.22 -7.00 0.0 97 -7.251338798705 -14.75 -7.35 0.0 98 -7.251338798705 -14.75 -7.57 0.0 99 -7.251338798705 + -15.05 -7.66 0.0 100 -7.251338798705 + -15.05 -7.88 0.0 101 -7.251338798705 + -13.59 -6.93 0.0 ┌ Warning: SCF not converged. └ @ DFTK ~/work/DFTK.jl/DFTK.jl/src/scf/scf_callbacks.jl:37
Note that the default convergence criterion is the difference in
density. When this gets below tol
, the
"driver" self_consistent_field
artificially makes the fixed-point
solver think it's converged by forcing f(x) = x
. You can customize
this with the is_converged
keyword argument to
self_consistent_field
.