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 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 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
Iter Function value Gradient norm 0 1.610628e+02 1.283988e+02 * time: 0.0007500648498535156 1 1.564910e+02 1.079730e+02 * time: 0.0029091835021972656 2 1.191841e+02 1.236910e+02 * time: 0.005340099334716797 3 6.651424e+01 9.191941e+01 * time: 0.008243083953857422 4 1.277787e+01 1.778462e+01 * time: 0.011087179183959961 5 1.030328e+01 1.123248e+01 * time: 0.013128995895385742 6 9.966489e+00 1.071154e+01 * time: 0.014400005340576172 7 9.588082e+00 1.066484e+01 * time: 0.015676021575927734 8 7.625424e+00 9.384852e+00 * time: 0.01773214340209961 9 2.869076e+00 5.399263e+00 * time: 0.02004408836364746 10 1.574974e+00 5.077365e+00 * time: 0.021879196166992188 11 1.436411e+00 3.995086e+00 * time: 0.023360013961791992 12 1.277620e+00 2.239685e+00 * time: 0.024525165557861328 13 1.190843e+00 1.128935e+00 * time: 0.025687217712402344 14 1.176160e+00 7.121330e-01 * time: 0.026876211166381836 15 1.157089e+00 3.059998e-01 * time: 0.028209209442138672 16 1.149470e+00 3.148633e-01 * time: 0.029401063919067383 17 1.145894e+00 2.682887e-01 * time: 0.030561208724975586 18 1.144489e+00 8.606633e-02 * time: 0.03171515464782715 19 1.144187e+00 4.086573e-02 * time: 0.03302311897277832 20 1.144081e+00 2.473614e-02 * time: 0.03420901298522949 21 1.144054e+00 1.188806e-02 * time: 0.03542613983154297 22 1.144045e+00 1.183009e-02 * time: 0.036704063415527344 23 1.144039e+00 9.316052e-03 * time: 0.038082122802734375 24 1.144038e+00 3.451088e-03 * time: 0.039376020431518555 25 1.144037e+00 2.259737e-03 * time: 0.04059314727783203 26 1.144037e+00 1.396795e-03 * time: 0.04181718826293945 27 1.144037e+00 8.329755e-04 * time: 0.04306221008300781 28 1.144037e+00 5.701011e-04 * time: 0.044257164001464844 29 1.144037e+00 3.634530e-04 * time: 0.045185089111328125 30 1.144037e+00 2.509176e-04 * time: 0.0460200309753418 31 1.144037e+00 1.437895e-04 * time: 0.04726600646972656 32 1.144037e+00 6.753942e-05 * time: 0.04853200912475586 33 1.144037e+00 3.673952e-05 * time: 0.04977011680603027 34 1.144037e+00 2.787790e-05 * time: 0.05097818374633789 35 1.144037e+00 2.488622e-05 * time: 0.052201032638549805 36 1.144037e+00 2.480094e-05 * time: 0.053481101989746094 37 1.144037e+00 2.333793e-05 * time: 0.054766178131103516 38 1.144037e+00 1.681236e-05 * time: 0.05607104301452637 39 1.144037e+00 8.333946e-06 * time: 0.05733799934387207 40 1.144037e+00 5.292323e-06 * time: 0.05857110023498535 41 1.144037e+00 2.795723e-06 * time: 0.05971503257751465 42 1.144037e+00 1.892417e-06 * time: 0.06055903434753418 43 1.144037e+00 8.393962e-07 * time: 0.06138801574707031 44 1.144037e+00 6.093650e-07 * time: 0.06253910064697266 45 1.144037e+00 3.222789e-07 * time: 0.06371903419494629 46 1.144037e+00 2.874799e-07 * time: 0.06490015983581543 47 1.144037e+00 1.771701e-07 * time: 0.06606507301330566 48 1.144037e+00 1.177326e-07 * time: 0.06727409362792969
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:
ψ = 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.ρ - abs2.(ψ))
8.616097585654255e-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)
2.5160174348773616e-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.00022349915270499098