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 reported1 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:


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

In [1]:
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, psp=load_psp(:Si, functional="lda"))
atoms = [Si => [ones(3)/8, -ones(3)/8]]

# Cast to Float32, setup model and basis
model = model_DFT(Array{Float32}(lattice), atoms, [:lda_x, :lda_c_vwn])
Ecut = 7
basis = PlaneWaveBasis(model, Ecut, kgrid=[4, 4, 4])

# Run the SCF
scfres = self_consistent_field(basis, tol=1e-4);
n     Energy            Eₙ-Eₙ₋₁     ρout-ρin   Diag
---   ---------------   ---------   --------   ----
  1   -7.907642364502         NaN   1.95e-01    4.1 
  2   -7.912328720093   -4.69e-03   2.98e-02    2.0 
  3   -7.912409305573   -8.06e-05   3.17e-03    1.0 

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

In [2]:
scfres.energies
Out[2]:
Energy breakdown:
    Kinetic             3.0811591 
    AtomicLocal         -2.1865373
    AtomicNonlocal      1.7369288 
    Ewald               -8.3978930
    PspCorrection       -0.2946220
    Hartree             0.5437310 
    Xc                  -2.3951764

    total               -7.912409305573
In [3]:
eltype(scfres.energies.total)
Out[3]:
Float32
In [4]:
eltype(scfres.ρ.real)
Out[4]:
Float32