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:
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.664563e+02 1.245384e+02 * time: 0.0007579326629638672 1 1.580281e+02 1.135388e+02 * time: 0.0023131370544433594 2 1.198486e+02 1.287458e+02 * time: 0.004064083099365234 3 6.042523e+01 9.207999e+01 * time: 0.005918025970458984 4 5.016078e+01 9.286239e+01 * time: 0.00757908821105957 5 2.972430e+01 6.374134e+01 * time: 0.009035110473632812 6 6.081857e+00 1.610987e+01 * time: 0.010477066040039062 7 3.007466e+00 7.342660e+00 * time: 0.011784076690673828 8 1.938174e+00 7.306919e+00 * time: 0.01295614242553711 9 1.568287e+00 5.298099e+00 * time: 0.014133930206298828 10 1.263996e+00 2.558128e+00 * time: 0.015151023864746094 11 1.199731e+00 4.901372e-01 * time: 0.0159299373626709 12 1.179497e+00 6.225754e-01 * time: 0.016742944717407227 13 1.161225e+00 5.140684e-01 * time: 0.017606019973754883 14 1.148154e+00 2.737013e-01 * time: 0.01850295066833496 15 1.145688e+00 1.038930e-01 * time: 0.019303083419799805 16 1.144843e+00 1.288194e-01 * time: 0.020127058029174805 17 1.144426e+00 8.611239e-02 * time: 0.020956039428710938 18 1.144175e+00 2.451755e-02 * time: 0.02182602882385254 19 1.144169e+00 5.498763e-02 * time: 0.022593021392822266 20 1.144123e+00 3.892616e-02 * time: 0.02345108985900879 21 1.144085e+00 2.242200e-02 * time: 0.024337053298950195 22 1.144050e+00 1.772133e-02 * time: 0.02522897720336914 23 1.144044e+00 1.230024e-02 * time: 0.026159048080444336 24 1.144040e+00 4.246210e-03 * time: 0.027143001556396484 25 1.144038e+00 2.730571e-03 * time: 0.027976036071777344 26 1.144037e+00 2.240842e-03 * time: 0.028825044631958008 27 1.144037e+00 1.018842e-03 * time: 0.029766082763671875 28 1.144037e+00 9.278684e-04 * time: 0.03064894676208496 29 1.144037e+00 8.922247e-04 * time: 0.031510114669799805 30 1.144037e+00 8.167601e-04 * time: 0.03245210647583008 31 1.144037e+00 1.826841e-04 * time: 0.03313612937927246 32 1.144037e+00 1.456541e-04 * time: 0.03401303291320801 33 1.144037e+00 7.334269e-05 * time: 0.03489208221435547 34 1.144037e+00 4.135225e-05 * time: 0.035787105560302734 35 1.144037e+00 3.542041e-05 * time: 0.0366971492767334 36 1.144037e+00 2.301022e-05 * time: 0.03755807876586914 37 1.144037e+00 1.692301e-05 * time: 0.03841996192932129 38 1.144037e+00 1.301568e-05 * time: 0.03928208351135254 39 1.144037e+00 7.260509e-06 * time: 0.03987598419189453 40 1.144037e+00 5.555602e-06 * time: 0.04073500633239746 41 1.144037e+00 2.896837e-06 * time: 0.04157304763793945 42 1.144037e+00 2.316762e-06 * time: 0.042504072189331055 43 1.144037e+00 1.993169e-06 * time: 0.04339408874511719 44 1.144037e+00 1.722576e-06 * time: 0.04422712326049805 45 1.144037e+00 1.178663e-06 * time: 0.04504704475402832 46 1.144037e+00 1.008301e-06 * time: 0.04584097862243652
Energy breakdown (in Ha): 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.ρ)[:, 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.(ψ))
7.770877854052157e-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.166509247683139e-6
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.000223573427600602