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.584044e+02 1.109605e+02 * time: 0.0007719993591308594 1 1.341764e+02 9.300470e+01 * time: 0.004700183868408203 2 9.945836e+01 9.903251e+01 * time: 0.008404016494750977 3 2.273558e+01 4.374918e+01 * time: 0.012911081314086914 4 1.793901e+01 4.388375e+01 * time: 0.016122102737426758 5 5.011312e+00 6.423494e+00 * time: 0.0188140869140625 6 3.471600e+00 4.410215e+00 * time: 0.021117210388183594 7 2.862617e+00 6.733444e+00 * time: 0.02371811866760254 8 2.086857e+00 2.772509e+00 * time: 0.02615809440612793 9 1.692723e+00 5.634494e+00 * time: 0.02832198143005371 10 1.575460e+00 2.005044e+00 * time: 0.03005218505859375 11 1.506059e+00 1.866459e+00 * time: 0.03181600570678711 12 1.306430e+00 1.317504e+00 * time: 0.03383803367614746 13 1.210884e+00 1.449574e+00 * time: 0.03568720817565918 14 1.161782e+00 8.210837e-01 * time: 0.03746509552001953 15 1.161165e+00 4.557407e-01 * time: 0.03879714012145996 16 1.151784e+00 3.969017e-01 * time: 0.040606021881103516 17 1.145741e+00 1.040170e-01 * time: 0.04252505302429199 18 1.144890e+00 1.081876e-01 * time: 0.04440712928771973 19 1.144326e+00 5.425003e-02 * time: 0.04651618003845215 20 1.144134e+00 2.327570e-02 * time: 0.04840898513793945 21 1.144064e+00 1.885392e-02 * time: 0.05023908615112305 22 1.144055e+00 2.069282e-02 * time: 0.05196714401245117 23 1.144045e+00 9.641875e-03 * time: 0.054508209228515625 24 1.144040e+00 4.528390e-03 * time: 0.056466102600097656 25 1.144038e+00 2.036064e-03 * time: 0.05833005905151367 26 1.144037e+00 1.785616e-03 * time: 0.060149192810058594 27 1.144037e+00 2.498608e-03 * time: 0.06136608123779297 28 1.144037e+00 1.401401e-03 * time: 0.06318807601928711 29 1.144037e+00 9.895200e-04 * time: 0.06496810913085938 30 1.144037e+00 6.013618e-04 * time: 0.06682419776916504 31 1.144037e+00 2.851837e-04 * time: 0.0686490535736084 32 1.144037e+00 2.258539e-04 * time: 0.07042813301086426 33 1.144037e+00 1.105154e-04 * time: 0.07222509384155273 34 1.144037e+00 8.180588e-05 * time: 0.07438802719116211 35 1.144037e+00 8.528393e-05 * time: 0.07619404792785645 36 1.144037e+00 6.377480e-05 * time: 0.07800006866455078 37 1.144037e+00 2.045594e-05 * time: 0.0798490047454834 38 1.144037e+00 1.622647e-05 * time: 0.0811150074005127 39 1.144037e+00 8.560488e-06 * time: 0.08291816711425781 40 1.144037e+00 4.806425e-06 * time: 0.08472609519958496 41 1.144037e+00 4.194742e-06 * time: 0.08643603324890137 42 1.144037e+00 2.431635e-06 * time: 0.08814716339111328 43 1.144037e+00 2.122886e-06 * time: 0.08998608589172363 44 1.144037e+00 9.718637e-07 * time: 0.09196901321411133 45 1.144037e+00 8.407175e-07 * time: 0.09380698204040527 46 1.144037e+00 6.292721e-07 * time: 0.09559321403503418 47 1.144037e+00 3.881629e-07 * time: 0.09739112854003906 48 1.144037e+00 2.454246e-07 * time: 0.09911417961120605 49 1.144037e+00 1.572060e-07 * time: 0.10092806816101074 50 1.144037e+00 1.059208e-07 * time: 0.10211610794067383 51 1.144037e+00 1.880626e-07 * time: 0.1034390926361084 52 1.144037e+00 1.880626e-07 * time: 0.10893416404724121
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.(ψ))
7.922265991860201e-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.1280382760558124e-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.00022341573102804718