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.997247e+02 1.990093e+02 * time: 0.0004818439483642578 1 1.667603e+02 1.200257e+02 * time: 0.0011508464813232422 2 1.285001e+02 1.325640e+02 * time: 0.0022268295288085938 3 1.243423e+02 1.231108e+02 * time: 0.003423929214477539 4 8.649211e+01 1.207772e+02 * time: 0.004788875579833984 5 4.160952e+01 7.471932e+01 * time: 0.006085872650146484 6 1.570836e+01 3.726758e+01 * time: 0.00716090202331543 7 1.079337e+01 3.573691e+01 * time: 0.007951021194458008 8 5.886282e+00 1.826327e+01 * time: 0.008810997009277344 9 2.334631e+00 1.534672e+00 * time: 0.009779930114746094 10 2.213391e+00 4.461903e+00 * time: 0.01041102409362793 11 1.684217e+00 2.321502e+00 * time: 0.011214017868041992 12 1.461767e+00 3.713769e+00 * time: 0.011807918548583984 13 1.317962e+00 1.902135e+00 * time: 0.012418031692504883 14 1.269853e+00 1.091392e+00 * time: 0.013106822967529297 15 1.192025e+00 6.664404e-01 * time: 0.013830900192260742 16 1.156935e+00 3.388864e-01 * time: 0.01458883285522461 17 1.148640e+00 2.612526e-01 * time: 0.015271902084350586 18 1.145471e+00 2.996803e-01 * time: 0.01587390899658203 19 1.144421e+00 1.775842e-01 * time: 0.016502857208251953 20 1.144251e+00 5.194910e-02 * time: 0.017027854919433594 21 1.144174e+00 4.598001e-02 * time: 0.017550945281982422 22 1.144093e+00 3.709757e-02 * time: 0.01812601089477539 23 1.144063e+00 2.093214e-02 * time: 0.01867389678955078 24 1.144043e+00 7.320245e-03 * time: 0.019235849380493164 25 1.144041e+00 5.549434e-03 * time: 0.019783973693847656 26 1.144038e+00 5.757830e-03 * time: 0.0203549861907959 27 1.144037e+00 3.288540e-03 * time: 0.021052837371826172 28 1.144037e+00 1.895382e-03 * time: 0.021615982055664062 29 1.144037e+00 2.266862e-03 * time: 0.022137880325317383 30 1.144037e+00 1.813715e-03 * time: 0.02275991439819336 31 1.144037e+00 8.514685e-04 * time: 0.02335500717163086 32 1.144037e+00 3.814972e-04 * time: 0.024024009704589844 33 1.144037e+00 2.450873e-04 * time: 0.024695873260498047 34 1.144037e+00 1.428968e-04 * time: 0.02536296844482422 35 1.144037e+00 9.445069e-05 * time: 0.02596592903137207 36 1.144037e+00 8.213933e-05 * time: 0.026525020599365234 37 1.144037e+00 9.655909e-05 * time: 0.027164936065673828 38 1.144037e+00 8.344206e-05 * time: 0.027835845947265625 39 1.144037e+00 5.819665e-05 * time: 0.02857184410095215 40 1.144037e+00 1.857161e-05 * time: 0.029340028762817383 41 1.144037e+00 6.306767e-06 * time: 0.03010082244873047 42 1.144037e+00 6.215129e-06 * time: 0.030841827392578125 43 1.144037e+00 4.610874e-06 * time: 0.03146982192993164 44 1.144037e+00 4.565230e-06 * time: 0.03209996223449707 45 1.144037e+00 3.655589e-06 * time: 0.032753944396972656 46 1.144037e+00 2.403522e-06 * time: 0.0334470272064209 47 1.144037e+00 1.833121e-06 * time: 0.03405880928039551 48 1.144037e+00 8.700688e-07 * time: 0.034626007080078125 49 1.144037e+00 3.931458e-07 * time: 0.03518199920654297 50 1.144037e+00 2.198528e-07 * time: 0.03572487831115723 51 1.144037e+00 1.875905e-07 * time: 0.03631091117858887 52 1.144037e+00 1.184772e-07 * time: 0.036966800689697266 53 1.144037e+00 4.549511e-08 * time: 0.03764200210571289
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.(ψ))
9.419154611360628e-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 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)
8.228242185673861e-8
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.00022342916312165066