In this example we will use DFTK to solve the Gross-Pitaevskii equation, and use this opportunity to explore a few internals.
The Gross-Pitaevskii equation (GPE) is a simple non-linear equation used to model bosonic systems in a mean-field approach. Denoting by $ψ$ the effective one-particle bosonic wave function, the time-independent GPE reads in atomic units: $$ H ψ = \left(-\frac12 Δ + V + 2 C |ψ|^2\right) ψ = μ ψ \qquad \|ψ\|_{L^2} = 1 $$ where $C$ provides the strength of the boson-boson coupling. It's in particular a favorite model of applied mathematicians because it has a structure simpler than but similar to that of DFT, and displays interesting behavior (especially in higher dimensions with magnetic fields, see Gross-Pitaevskii equation with external magnetic field).
We wish to model this equation in 1D using DFTK. First we set up the lattice. For a 1D case we supply two zero lattice vectors,
a = 10
lattice = a .* [[1 0 0.]; [0 0 0]; [0 0 0]];
which is special cased in DFTK to support 1D models.
For the potential term V
we pick a harmonic
potential. We use the function ExternalFromReal
which uses
Cartesian coordinates ( see Lattices and lattice vectors).
pot(x) = (x - a/2)^2;
We setup each energy term in sequence: kinetic, potential and nonlinear term.
For the non-linearity we use the LocalNonlinearity(f)
term of DFTK, with f(ρ) = C ρ^α.
This object introduces an energy term $C ∫ ρ(r)^α dr$
to the total energy functional, thus a potential term $α C ρ^{α-1}$.
In our case we thus need the parameters
C = 1.0
α = 2;
… and with this build the model
using DFTK
using LinearAlgebra
n_electrons = 1 # Increase this for fun
terms = [Kinetic(),
ExternalFromReal(r -> pot(r[1])),
LocalNonlinearity(ρ -> C * ρ^α),
]
model = Model(lattice; n_electrons, terms, spin_polarization=:spinless); # spinless electrons
We discretize using a moderate Ecut (For 1D values up to 5000
are completely fine)
and run a direct minimization algorithm:
basis = PlaneWaveBasis(model, Ecut=500, kgrid=(1, 1, 1))
scfres = direct_minimization(basis, tol=1e-8) # This is a constrained preconditioned LBFGS
scfres.energies
n Energy log10(ΔE) log10(Δρ) Δtime --- --------------- --------- --------- ------ 1 +159.8469026645 -1.57 320ms 2 +122.5658803513 1.57 -0.90 1.58ms 3 +68.42271237955 1.73 -0.65 1.56ms 4 +12.04805716751 1.75 -0.45 1.53ms 5 +9.191318452175 0.46 -0.31 1.09ms 6 +7.832911040811 0.13 -0.60 835μs 7 +4.887964593339 0.47 -0.33 850μs 8 +2.748013272908 0.33 -0.14 822μs 9 +2.671390247657 -1.12 -0.18 701μs 10 +2.370654782410 -0.52 -0.30 664μs 11 +1.451754614743 -0.04 -0.15 828μs 12 +1.297595520654 -0.81 -0.66 657μs 13 +1.165546209566 -0.88 -0.96 660μs 14 +1.153364585952 -1.91 -1.49 722μs 15 +1.148183746230 -2.29 -1.36 910μs 16 +1.145903970534 -2.64 -1.75 657μs 17 +1.144903477262 -3.00 -2.32 660μs 18 +1.144441760142 -3.34 -2.53 647μs 19 +1.144122953493 -3.50 -2.55 475μs 20 +1.144077686419 -4.34 -2.93 671μs 21 +1.144049380286 -4.55 -2.79 697μs 22 +1.144045255958 -5.38 -2.48 672μs 23 +1.144041266372 -5.40 -2.87 839μs 24 +1.144038883350 -5.62 -2.77 655μs 25 +1.144038062934 -6.09 -3.16 656μs 26 +1.144037435939 -6.20 -4.01 646μs 27 +1.144037101964 -6.48 -3.90 673μs 28 +1.144036920624 -6.74 -4.11 654μs 29 +1.144036880220 -7.39 -3.85 644μs 30 +1.144036861308 -7.72 -4.06 646μs 31 +1.144036856415 -8.31 -4.68 814μs 32 +1.144036854693 -8.76 -4.65 649μs 33 +1.144036854153 -9.27 -4.67 674μs 34 +1.144036853308 -9.07 -4.93 650μs 35 +1.144036853094 -9.67 -5.32 644μs 36 +1.144036852928 -9.78 -4.93 651μs 37 +1.144036852849 -10.11 -5.31 650μs 38 +1.144036852793 -10.25 -5.97 652μs 39 +1.144036852766 -10.57 -5.99 868μs 40 +1.144036852761 -11.26 -6.51 663μs 41 +1.144036852758 -11.50 -6.35 649μs 42 +1.144036852756 -11.92 -6.52 656μs 43 +1.144036852756 -12.25 -6.36 654μs 44 +1.144036852756 -12.90 -6.84 650μs 45 +1.144036852756 -12.68 -7.13 680μs 46 +1.144036852755 -13.14 -7.20 835μs 47 +1.144036852755 -13.57 -6.87 650μs 48 +1.144036852755 -13.46 -7.36 652μs 49 +1.144036852755 -13.69 -7.42 648μs 50 +1.144036852755 -14.45 -7.79 678μs 51 +1.144036852755 -14.51 -8.11 658μs
Energy breakdown (in Ha): Kinetic 0.2682057 ExternalFromReal 0.4707475 LocalNonlinearity 0.4050836 total 1.144036852755
We use the opportunity to explore some of DFTK internals.
Extract the converged density and the obtained wave function:
ρ = real(scfres.ρ)[:, 1, 1, 1] # converged density, first spin component
ψ_fourier = scfres.ψ[1][:, 1]; # first k-point, all G components, first eigenvector
Transform the wave function to real space and fix the phase:
ψ = ifft(basis, basis.kpoints[1], ψ_fourier)[:, 1, 1]
ψ /= (ψ[div(end, 2)] / abs(ψ[div(end, 2)]));
Check whether $ψ$ is normalised:
x = a * vec(first.(DFTK.r_vectors(basis)))
N = length(x)
dx = a / N # real-space grid spacing
@assert sum(abs2.(ψ)) * dx ≈ 1.0
The density is simply built from ψ:
norm(scfres.ρ - abs2.(ψ))
8.639717975914011e-16
We summarize the ground state in a nice plot:
using Plots
p = plot(x, real.(ψ), label="real(ψ)")
plot!(p, x, imag.(ψ), label="imag(ψ)")
plot!(p, x, ρ, label="ρ")
The energy_hamiltonian
function can be used to get the energy and
effective Hamiltonian (derivative of the energy with respect to the density matrix)
of a particular state (ψ, occupation).
The density ρ associated to this state is precomputed
and passed to the routine as an optimization.
E, ham = energy_hamiltonian(basis, scfres.ψ, scfres.occupation; ρ=scfres.ρ)
@assert E.total == scfres.energies.total
Now the Hamiltonian contains all the blocks corresponding to $k$-points. Here, we just have one $k$-point:
H = ham.blocks[1];
H
can be used as a linear operator (efficiently using FFTs), or converted to a dense matrix:
ψ11 = scfres.ψ[1][:, 1] # first k-point, first eigenvector
Hmat = Array(H) # This is now just a plain Julia matrix,
# which we can compute and store in this simple 1D example
@assert norm(Hmat * ψ11 - H * ψ11) < 1e-10
Let's check that ψ11 is indeed an eigenstate:
norm(H * ψ11 - dot(ψ11, H * ψ11) * ψ11)
1.9793316300831036e-7
Build a finite-differences version of the GPE operator $H$, as a sanity check:
A = Array(Tridiagonal(-ones(N - 1), 2ones(N), -ones(N - 1)))
A[1, end] = A[end, 1] = -1
K = A / dx^2 / 2
V = Diagonal(pot.(x) + C .* α .* (ρ.^(α-1)))
H_findiff = K + V;
maximum(abs.(H_findiff*ψ - (dot(ψ, H_findiff*ψ) / dot(ψ, ψ)) * ψ))
0.00022344388025419073