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.815793e+02 2.028168e+02 * time: 0.0004918575286865234 1 1.659533e+02 1.710243e+02 * time: 0.00220489501953125 2 1.311300e+02 1.894558e+02 * time: 0.0040400028228759766 3 5.804803e+01 1.340117e+02 * time: 0.0060520172119140625 4 4.786448e+01 1.329135e+02 * time: 0.007799863815307617 5 1.757271e+01 6.822252e+01 * time: 0.00933694839477539 6 1.018225e+01 5.534423e+01 * time: 0.01067495346069336 7 9.423788e+00 4.742010e+01 * time: 0.011787891387939453 8 5.814031e+00 7.028769e+00 * time: 0.012953996658325195 9 4.507836e+00 2.064308e+01 * time: 0.014067888259887695 10 2.276725e+00 4.985080e+00 * time: 0.015170812606811523 11 1.708085e+00 6.144632e+00 * time: 0.016301870346069336 12 1.447499e+00 4.335181e+00 * time: 0.017409801483154297 13 1.314021e+00 9.694636e-01 * time: 0.01828289031982422 14 1.206519e+00 1.462645e+00 * time: 0.019155025482177734 15 1.158975e+00 9.547540e-01 * time: 0.020068883895874023 16 1.147452e+00 1.734094e-01 * time: 0.020941972732543945 17 1.145915e+00 1.163441e-01 * time: 0.021814823150634766 18 1.144681e+00 1.446684e-01 * time: 0.02268195152282715 19 1.144440e+00 1.209189e-01 * time: 0.023546934127807617 20 1.144284e+00 1.064357e-01 * time: 0.02443981170654297 21 1.144160e+00 1.082582e-01 * time: 0.025307893753051758 22 1.144103e+00 4.867993e-02 * time: 0.02617192268371582 23 1.144071e+00 1.328741e-02 * time: 0.027042865753173828 24 1.144048e+00 6.949246e-03 * time: 0.027930021286010742 25 1.144042e+00 5.130308e-03 * time: 0.028812885284423828 26 1.144039e+00 4.196498e-03 * time: 0.029690980911254883 27 1.144037e+00 7.425566e-03 * time: 0.030560970306396484 28 1.144037e+00 4.279970e-03 * time: 0.03142499923706055 29 1.144037e+00 1.462452e-03 * time: 0.03231501579284668 30 1.144037e+00 3.349227e-04 * time: 0.03317999839782715 31 1.144037e+00 8.935747e-04 * time: 0.033802032470703125 32 1.144037e+00 3.460495e-04 * time: 0.03466486930847168 33 1.144037e+00 1.797003e-04 * time: 0.03553199768066406 34 1.144037e+00 1.325891e-04 * time: 0.03641986846923828 35 1.144037e+00 9.450281e-05 * time: 0.03729391098022461 36 1.144037e+00 8.256522e-05 * time: 0.03816080093383789 37 1.144037e+00 3.141557e-05 * time: 0.039031982421875 38 1.144037e+00 2.437291e-05 * time: 0.0399169921875 39 1.144037e+00 1.689472e-05 * time: 0.04055500030517578 40 1.144037e+00 1.654384e-05 * time: 0.041417837142944336 41 1.144037e+00 7.015449e-06 * time: 0.04227781295776367 42 1.144037e+00 2.410464e-06 * time: 0.043141841888427734 43 1.144037e+00 1.376083e-06 * time: 0.04402279853820801 44 1.144037e+00 1.235398e-06 * time: 0.04488992691040039 45 1.144037e+00 9.742599e-07 * time: 0.0457608699798584 46 1.144037e+00 6.480868e-07 * time: 0.046623945236206055 47 1.144037e+00 4.329653e-07 * time: 0.04748892784118652 48 1.144037e+00 3.646276e-07 * time: 0.04837298393249512 49 1.144037e+00 2.603788e-07 * time: 0.049240827560424805 50 1.144037e+00 1.202274e-07 * time: 0.05011487007141113 51 1.144037e+00 1.202168e-07 * time: 0.05194401741027832
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.(ψ))
1.1033993238356295e-15
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.3703963254364263e-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.00022344795322572894