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 magnetism).
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 just pick a harmonic
potential. The real-space grid is in [0,1)
in fractional coordinates( see
Lattices and lattice vectors),
therefore:
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 PowerNonlinearity(C, α)
term of DFTK.
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])),
PowerNonlinearity(C, α),
]
model = Model(lattice; n_electrons=n_electrons, terms=terms,
spin_polarization=:spinless); # use "spinless electrons"
We discretize using a moderate Ecut (For 1D values up to 5000
are completely fine)
and run a direct minimization algorithm:
Ecut = 500
basis = PlaneWaveBasis(model, Ecut, kgrid=(1, 1, 1))
scfres = direct_minimization(basis, tol=1e-8) # This is a constrained preconditioned LBFGS
scfres.energies
Iter Function value Gradient norm 0 1.849886e+02 1.410134e+02 * time: 0.003821134567260742 1 1.744823e+02 1.091016e+02 * time: 0.004879951477050781 2 1.242951e+02 1.218529e+02 * time: 0.0070531368255615234 3 3.034341e+01 5.103882e+01 * time: 0.009655952453613281 4 1.811089e+01 4.397198e+01 * time: 0.011742115020751953 5 8.365599e+00 2.159938e+01 * time: 0.014126062393188477 6 7.227990e+00 1.755368e+01 * time: 0.015556097030639648 7 4.610952e+00 2.244511e+01 * time: 0.017138957977294922 8 2.952836e+00 3.263434e+00 * time: 0.0185239315032959 9 2.109745e+00 3.131655e+00 * time: 0.01968693733215332 10 1.639476e+00 2.088502e+00 * time: 0.02093505859375 11 1.624079e+00 2.353795e+00 * time: 0.02196192741394043 12 1.407694e+00 1.635939e+00 * time: 0.022875070571899414 13 1.354335e+00 1.867547e+00 * time: 0.023772001266479492 14 1.255156e+00 1.024637e+00 * time: 0.024763107299804688 15 1.185258e+00 7.481054e-01 * time: 0.025728940963745117 16 1.150411e+00 3.984686e-01 * time: 0.026953935623168945 17 1.145562e+00 1.043232e-01 * time: 0.02790999412536621 18 1.144372e+00 1.055504e-01 * time: 0.029113054275512695 19 1.144121e+00 4.309222e-02 * time: 0.030076026916503906 20 1.144092e+00 2.433981e-02 * time: 0.03102898597717285 21 1.144076e+00 1.629245e-02 * time: 0.032009124755859375 22 1.144054e+00 1.882243e-02 * time: 0.032946109771728516 23 1.144044e+00 1.424314e-02 * time: 0.03395700454711914 24 1.144041e+00 1.049984e-02 * time: 0.03493094444274902 25 1.144039e+00 5.661700e-03 * time: 0.03595709800720215 26 1.144037e+00 2.457432e-03 * time: 0.036900997161865234 27 1.144037e+00 1.168992e-03 * time: 0.037873029708862305 28 1.144037e+00 6.676670e-04 * time: 0.038828134536743164 29 1.144037e+00 3.503743e-04 * time: 0.03978705406188965 30 1.144037e+00 2.160583e-04 * time: 0.04072213172912598 31 1.144037e+00 2.585808e-04 * time: 0.041734933853149414 32 1.144037e+00 2.590131e-04 * time: 0.042680978775024414 33 1.144037e+00 1.692412e-04 * time: 0.043736934661865234 34 1.144037e+00 6.090277e-05 * time: 0.04468798637390137 35 1.144037e+00 2.812988e-05 * time: 0.04594898223876953 36 1.144037e+00 2.170094e-05 * time: 0.047055959701538086 37 1.144037e+00 1.997096e-05 * time: 0.048107147216796875 38 1.144037e+00 1.245220e-05 * time: 0.0489039421081543 39 1.144037e+00 1.067030e-05 * time: 0.04991412162780762 40 1.144037e+00 2.622121e-06 * time: 0.05091214179992676 41 1.144037e+00 1.652775e-06 * time: 0.05188393592834473 42 1.144037e+00 1.224448e-06 * time: 0.05285906791687012 43 1.144037e+00 8.550414e-07 * time: 0.05384492874145508 44 1.144037e+00 6.997785e-07 * time: 0.05475902557373047 45 1.144037e+00 5.598040e-07 * time: 0.055628061294555664 46 1.144037e+00 3.361092e-07 * time: 0.05654191970825195 47 1.144037e+00 2.947417e-07 * time: 0.057554006576538086 48 1.144037e+00 1.890853e-07 * time: 0.05847597122192383 49 1.144037e+00 1.628563e-07 * time: 0.05954694747924805 50 1.144037e+00 1.216759e-07 * time: 0.06067395210266113
Energy breakdown: Kinetic 0.2682057 ExternalFromReal 0.4707475 PowerNonlinearity 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.ρ.real)[:, 1, 1] # converged density
ψ_fourier = scfres.ψ[1][:, 1]; # first kpoint, all G components, first eigenvector
Transform the wave function to real space and fix the phase:
ψ = G_to_r(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.ρ.real - abs2.(ψ))
1.2815385018242939e-15
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 kpoints. Here, we just have one kpoint:
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 kpoint, 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.9883911151235988e-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.00022348748925293425