Arbitrary floating-point types

Since DFTK is completely generic in the floating-point type in its routines, there is no reason to perform the computation using double-precision arithmetic (i.e.Float64). Other floating-point types such as Float32 (single precision) are readily supported as well. On top of that we already reported[HLC2020] calculations in DFTK using elevated precision from DoubleFloats.jl or interval arithmetic using IntervalArithmetic.jl. In this example, however, we will concentrate on single-precision computations with Float32.

The setup of such a reduced-precision calculation is basically identical to the regular case, since Julia automatically compiles all routines of DFTK at the precision, which is used for the lattice vectors. Apart from setting up the model with an explicit cast of the lattice vectors to Float32, there is thus no change in user code required:

using DFTK

# Setup silicon lattice
a = 10.263141334305942  # lattice constant in Bohr
lattice = a / 2 .* [[0 1 1.]; [1 0 1.]; [1 1 0.]]
Si = ElementPsp(:Si, load_psp("hgh/lda/Si-q4"))
atoms = [Si, Si]
positions = [ones(3)/8, -ones(3)/8]

# Cast to Float32, setup model and basis
model = model_DFT(lattice, atoms, positions; functionals=LDA())
basis = PlaneWaveBasis(convert(Model{Float32}, model), Ecut=7, kgrid=[4, 4, 4])

# Run the SCF
scfres = self_consistent_field(basis, tol=1e-3);
n     Energy            log10(ΔE)   log10(Δρ)   Diag   Δtime
---   ---------------   ---------   ---------   ----   ------
  1   -7.900490760803                   -0.70    4.6   56.6ms
  2   -7.905011653900       -2.34       -1.52    1.0   37.2ms
  3   -7.905179977417       -3.77       -2.52    1.0   37.8ms
  4   -7.905210494995       -4.52       -2.83    2.5   48.0ms
  5   -7.905211448669       -6.02       -2.96    1.1   37.5ms
  6   -7.905212402344       -6.02       -4.58    1.0   37.0ms

To check the calculation has really run in Float32, we check the energies and density are expressed in this floating-point type:

scfres.energies
Energy breakdown (in Ha):
    Kinetic             3.1021383 
    AtomicLocal         -2.1988482
    AtomicNonlocal      1.7295769 
    Ewald               -8.3978958
    PspCorrection       -0.2946220
    Hartree             0.5530612 
    Xc                  -2.3986228

    total               -7.905212402344
eltype(scfres.energies.total)
Float32
eltype(scfres.ρ)
Float32
Generic linear algebra routines

For more unusual floating-point types (like IntervalArithmetic or DoubleFloats), which are not directly supported in the standard LinearAlgebra and FFTW libraries one additional step is required: One needs to explicitly enable the generic versions of standard linear-algebra operations like cholesky or qr or standard fft operations, which DFTK requires. THis is done by loading the GenericLinearAlgebra package in the user script (i.e. just add ad using GenericLinearAlgebra next to your using DFTK call).

  • HLC2020M. F. Herbst, A. Levitt, E. Cancès. A posteriori error estimation for the non-self-consistent Kohn-Sham equations ArXiv 2004.13549