{ "cells": [ { "cell_type": "markdown", "source": [ "# Arbitrary floating-point types\n", "\n", "Since DFTK is completely generic in the floating-point type\n", "in its routines, there is no reason to perform the computation\n", "using double-precision arithmetic (i.e.`Float64`).\n", "Other floating-point types such as `Float32` (single precision)\n", "are readily supported as well.\n", "On top of that we already reported[^HLC2020] calculations\n", "in DFTK using elevated precision\n", "from [DoubleFloats.jl](https://github.com/JuliaMath/DoubleFloats.jl)\n", "or interval arithmetic\n", "using [IntervalArithmetic.jl](https://github.com/JuliaIntervals/IntervalArithmetic.jl).\n", "In this example, however, we will concentrate on single-precision\n", "computations with `Float32`.\n", "\n", "The setup of such a reduced-precision calculation is basically identical\n", "to the regular case, since Julia automatically compiles all routines\n", "of DFTK at the precision, which is used for the lattice vectors.\n", "Apart from setting up the model with an explicit cast of the lattice\n", "vectors to `Float32`, there is thus no change in user code required:\n", "\n", "[^HLC2020]:\n", " M. F. Herbst, A. Levitt, E. Cancès.\n", " *A posteriori error estimation for the non-self-consistent Kohn-Sham equations*\n", " [ArXiv 2004.13549](https://arxiv.org/abs/2004.13549)" ], "metadata": {} }, { "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "n Energy Eₙ-Eₙ₋₁ ρout-ρin α Diag\n", "--- --------------- --------- -------- ---- ----\n", " 1 -7.907574176788 NaN 1.95e-01 0.80 4.0\n", " 2 -7.912183761597 -4.61e-03 2.99e-02 0.80 1.0\n", " 3 -7.912420272827 -2.37e-04 3.05e-03 0.80 3.1\n", " 4 -7.912419795990 4.77e-07 4.74e-04 0.80 2.4\n" ] } ], "cell_type": "code", "source": [ "using DFTK\n", "\n", "# Setup silicon lattice\n", "a = 10.263141334305942 # lattice constant in Bohr\n", "lattice = a / 2 .* [[0 1 1.]; [1 0 1.]; [1 1 0.]]\n", "Si = ElementPsp(:Si, psp=load_psp(:Si, functional=\"lda\"))\n", "atoms = [Si => [ones(3)/8, -ones(3)/8]]\n", "\n", "# Cast to Float32, setup model and basis\n", "model = model_DFT(Array{Float32}(lattice), atoms, [:lda_x, :lda_c_vwn])\n", "basis = PlaneWaveBasis(model, Ecut=7, kgrid=[4, 4, 4])\n", "\n", "# Run the SCF\n", "scfres = self_consistent_field(basis, tol=1e-4);" ], "metadata": {}, "execution_count": 1 }, { "cell_type": "markdown", "source": [ "To check the calculation has really run in Float32,\n", "we check the energies and density are expressed in this floating-point type:" ], "metadata": {} }, { "outputs": [ { "output_type": "execute_result", "data": { "text/plain": "Energy breakdown (in Ha):\n Kinetic 3.0800269 \n AtomicLocal -2.1816800\n AtomicNonlocal 1.7341745 \n Ewald -8.3978930\n PspCorrection -0.2946220\n Hartree 0.5421727 \n Xc -2.3945994\n\n total -7.912419795990" }, "metadata": {}, "execution_count": 2 } ], "cell_type": "code", "source": [ "scfres.energies" ], "metadata": {}, "execution_count": 2 }, { "outputs": [ { "output_type": "execute_result", "data": { "text/plain": "Float32" }, "metadata": {}, "execution_count": 3 } ], "cell_type": "code", "source": [ "eltype(scfres.energies.total)" ], "metadata": {}, "execution_count": 3 }, { "outputs": [ { "output_type": "execute_result", "data": { "text/plain": "Float32" }, "metadata": {}, "execution_count": 4 } ], "cell_type": "code", "source": [ "eltype(scfres.ρ)" ], "metadata": {}, "execution_count": 4 }, { "cell_type": "markdown", "source": [ "!!! note \"Generic linear algebra routines\"\n", " For more unusual floating-point types (like IntervalArithmetic or DoubleFloats),\n", " which are not directly supported in the standard `LinearAlgebra` library of Julia\n", " one additional step is required: One needs to explicitly enable the generic versions\n", " of standard linear-algebra operations like `cholesky` or `qr`, which are needed\n", " inside DFTK by loading the `GenericLinearAlgebra` package in the user script\n", " (i.e. just add ad `using GenericLinearAlgebra` next to your `using DFTK` call)." ], "metadata": {} } ], "nbformat_minor": 3, "metadata": { "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.7.0" }, "kernelspec": { "name": "julia-1.7", "display_name": "Julia 1.7.0", "language": "julia" } }, "nbformat": 4 }