Monitoring self-consistent field calculations
self_consistent_field function takes as the
callback keyword argument one function to be called after each iteration. This function gets passed the complete internal state of the SCF solver and can thus be used both to monitor and debug the iterations as well as to quickly patch it with additional functionality.
This example discusses a few aspects of the
callback function taking again our favourite silicon example.
We setup silicon in an LDA model using the ASE interface to build a bulk silicon lattice, see Input and output formats for more details.
using DFTK using ASEconvert system = pyconvert(AbstractSystem, ase.build.bulk("Si")) model = model_LDA(attach_psp(system; Si="hgh/pbe/si-q4.hgh")) basis = PlaneWaveBasis(model; Ecut=5, kgrid=[3, 3, 3]);
DFTK already defines a few callback functions for standard tasks. One example is the usual convergence table, which is defined in the callback
ScfDefaultCallback. Another example is
ScfPlotTrace, which records the total energy at each iteration and uses it to plot the convergence of the SCF graphically once it is converged. For details and other callbacks see
Callbacks are not exported from the DFTK namespace as of now, so you will need to use them, e.g., as
In this example we define a custom callback, which plots the change in density at each SCF iteration after the SCF has finished. For this we first define the empty plot canvas and an empty container for all the density differences:
using Plots p = plot(yaxis=:log) density_differences = Float64;
The callback function itself gets passed a named tuple similar to the one returned by
self_consistent_field, which contains the input and output density of the SCF step as
ρout. Since the callback gets called both during the SCF iterations as well as after convergence just before
self_consistent_field finishes we can both collect the data and initiate the plotting in one function.
using LinearAlgebra function plot_callback(info) if info.stage == :finalize plot!(p, density_differences, label="|ρout - ρin|", markershape=:x) else push!(density_differences, norm(info.ρout - info.ρin)) end info end callback = DFTK.ScfDefaultCallback() ∘ plot_callback;
Notice that for constructing the
callback function we chained the
plot_callback (which does the plotting) with the
ScfDefaultCallback, such that when using the
plot_callback function with
self_consistent_field we still get the usual convergence table printed. We run the SCF with this callback …
scfres = self_consistent_field(basis; tol=1e-5, callback);
n Energy log10(ΔE) log10(Δρ) α Diag Δtime --- --------------- --------- --------- ---- ---- ------ 1 -7.774226536951 -0.70 0.80 4.8 2 -7.779026471052 -2.32 -1.52 0.80 1.0 36.6ms 3 -7.779319183859 -3.53 -2.57 0.80 1.5 38.0ms 4 -7.779350241409 -4.51 -2.91 0.80 2.2 44.4ms 5 -7.779350724884 -6.32 -3.24 0.80 1.2 46.6ms 6 -7.779350848285 -6.91 -3.98 0.80 1.0 37.4ms 7 -7.779350855819 -8.12 -5.37 0.80 1.8 40.9ms
… and show the plot
info object passed to the callback contains not just the densities but also the complete Bloch wave (in
eigenvalues and so on. See
src/scf/self_consistent_field.jl for all currently available keys.
Very handy for debugging SCF algorithms is to employ callbacks with an
@infiltrate from Infiltrator.jl to interactively monitor what is happening each SCF step.