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.512873e+02 1.375222e+02 * time: 0.0005550384521484375 1 1.435477e+02 1.325821e+02 * time: 0.002213001251220703 2 1.041580e+02 1.483330e+02 * time: 0.004019021987915039 3 3.009455e+01 7.316066e+01 * time: 0.0060939788818359375 4 9.832331e+00 1.068648e+01 * time: 0.008001089096069336 5 6.485721e+00 9.172395e+00 * time: 0.009205102920532227 6 5.485390e+00 2.470008e+01 * time: 0.010338068008422852 7 3.515126e+00 5.155697e+00 * time: 0.011465072631835938 8 2.019436e+00 2.716812e+00 * time: 0.012649059295654297 9 1.587086e+00 3.413942e+00 * time: 0.013792991638183594 10 1.527533e+00 3.713483e+00 * time: 0.014712095260620117 11 1.434190e+00 1.368665e+00 * time: 0.015640974044799805 12 1.297052e+00 3.363398e+00 * time: 0.016596078872680664 13 1.289323e+00 1.420985e+00 * time: 0.01731705665588379 14 1.226052e+00 2.516724e+00 * time: 0.018079042434692383 15 1.203786e+00 1.144940e+00 * time: 0.018832921981811523 16 1.168152e+00 9.702134e-01 * time: 0.019828081130981445 17 1.152504e+00 6.790808e-01 * time: 0.020860910415649414 18 1.147824e+00 1.730229e-01 * time: 0.021867990493774414 19 1.145413e+00 1.103970e-01 * time: 0.02283787727355957 20 1.144585e+00 7.813989e-02 * time: 0.024638891220092773 21 1.144282e+00 5.032958e-02 * time: 0.0256350040435791 22 1.144228e+00 3.584493e-02 * time: 0.026599884033203125 23 1.144139e+00 3.200982e-02 * time: 0.027524948120117188 24 1.144083e+00 1.953526e-02 * time: 0.02848196029663086 25 1.144063e+00 1.667393e-02 * time: 0.029253005981445312 26 1.144061e+00 1.907542e-02 * time: 0.030039072036743164 27 1.144047e+00 1.115443e-02 * time: 0.03078293800354004 28 1.144041e+00 6.494545e-03 * time: 0.031697988510131836 29 1.144039e+00 8.805683e-03 * time: 0.032633066177368164 30 1.144038e+00 3.159632e-03 * time: 0.03362703323364258 31 1.144037e+00 1.601036e-03 * time: 0.034584999084472656 32 1.144037e+00 1.045798e-03 * time: 0.035572052001953125 33 1.144037e+00 6.224392e-04 * time: 0.036602020263671875 34 1.144037e+00 6.443154e-04 * time: 0.037600040435791016 35 1.144037e+00 3.128451e-04 * time: 0.038580894470214844 36 1.144037e+00 2.523591e-04 * time: 0.03959488868713379 37 1.144037e+00 2.434505e-04 * time: 0.04046297073364258 38 1.144037e+00 1.032864e-04 * time: 0.04125690460205078 39 1.144037e+00 1.069945e-04 * time: 0.04197096824645996 40 1.144037e+00 6.393069e-05 * time: 0.042675018310546875 41 1.144037e+00 5.772990e-05 * time: 0.04359793663024902 42 1.144037e+00 4.321297e-05 * time: 0.044693946838378906 43 1.144037e+00 2.389234e-05 * time: 0.04565596580505371 44 1.144037e+00 1.932888e-05 * time: 0.046586036682128906 45 1.144037e+00 7.550192e-06 * time: 0.047515869140625 46 1.144037e+00 5.040746e-06 * time: 0.04846000671386719 47 1.144037e+00 4.330509e-06 * time: 0.04948306083679199 48 1.144037e+00 2.806654e-06 * time: 0.050488948822021484 49 1.144037e+00 1.544158e-06 * time: 0.05128002166748047 50 1.144037e+00 9.185410e-07 * time: 0.05201888084411621 51 1.144037e+00 6.317455e-07 * time: 0.05276298522949219 52 1.144037e+00 2.995184e-07 * time: 0.05356192588806152 53 1.144037e+00 2.205630e-07 * time: 0.05452990531921387
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.(ψ))
8.994397710078003e-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)
2.353833572647533e-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.00022345682490699286