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.765089e+02 1.182079e+02 * time: 0.0006511211395263672 1 1.708323e+02 9.753269e+01 * time: 0.0019850730895996094 2 1.256698e+02 9.437160e+01 * time: 0.004372119903564453 3 1.076115e+02 8.272244e+01 * time: 0.007039070129394531 4 4.933072e+01 6.689338e+01 * time: 0.009698152542114258 5 3.165150e+01 4.701766e+01 * time: 0.012061119079589844 6 1.017234e+01 9.863353e+00 * time: 0.01418614387512207 7 1.013702e+01 1.736950e+01 * time: 0.014991998672485352 8 6.807519e+00 7.913527e+00 * time: 0.016422033309936523 9 3.305763e+00 3.922085e+00 * time: 0.018199920654296875 10 2.527361e+00 9.239986e+00 * time: 0.019628047943115234 11 1.819008e+00 4.401821e+00 * time: 0.021079063415527344 12 1.653479e+00 2.715344e+00 * time: 0.022576093673706055 13 1.491605e+00 1.672313e+00 * time: 0.02370309829711914 14 1.436884e+00 2.405348e+00 * time: 0.024821996688842773 15 1.311629e+00 1.844782e+00 * time: 0.026051998138427734 16 1.232842e+00 1.205995e+00 * time: 0.027163982391357422 17 1.226469e+00 7.379742e-01 * time: 0.02796006202697754 18 1.185173e+00 5.690920e-01 * time: 0.02908015251159668 19 1.151745e+00 2.702299e-01 * time: 0.030213117599487305 20 1.146948e+00 2.121817e-01 * time: 0.03132009506225586 21 1.144795e+00 1.357340e-01 * time: 0.03242802619934082 22 1.144300e+00 3.863231e-02 * time: 0.03355813026428223 23 1.144113e+00 1.533640e-02 * time: 0.03465700149536133 24 1.144099e+00 1.431367e-02 * time: 0.03576207160949707 25 1.144072e+00 1.131954e-02 * time: 0.03686714172363281 26 1.144058e+00 1.158915e-02 * time: 0.03799796104431152 27 1.144042e+00 5.220822e-03 * time: 0.03909802436828613 28 1.144039e+00 3.325529e-03 * time: 0.040216922760009766 29 1.144038e+00 3.065392e-03 * time: 0.041355133056640625 30 1.144037e+00 1.240680e-03 * time: 0.04250812530517578 31 1.144037e+00 1.036939e-03 * time: 0.04366898536682129 32 1.144037e+00 9.482615e-04 * time: 0.04481196403503418 33 1.144037e+00 7.319115e-04 * time: 0.0459599494934082 34 1.144037e+00 3.303911e-04 * time: 0.047070980072021484 35 1.144037e+00 2.160884e-04 * time: 0.04818296432495117 36 1.144037e+00 1.803334e-04 * time: 0.049317121505737305 37 1.144037e+00 1.028342e-04 * time: 0.05043292045593262 38 1.144037e+00 7.265890e-05 * time: 0.051553964614868164 39 1.144037e+00 4.889936e-05 * time: 0.052670955657958984 40 1.144037e+00 2.569856e-05 * time: 0.05381011962890625 41 1.144037e+00 1.637467e-05 * time: 0.05491495132446289 42 1.144037e+00 1.474808e-05 * time: 0.05602693557739258 43 1.144037e+00 9.318347e-06 * time: 0.057138919830322266 44 1.144037e+00 5.161005e-06 * time: 0.05825495719909668 45 1.144037e+00 3.368800e-06 * time: 0.05934596061706543 46 1.144037e+00 2.132506e-06 * time: 0.06044912338256836 47 1.144037e+00 1.623603e-06 * time: 0.061231136322021484 48 1.144037e+00 1.731472e-06 * time: 0.06238102912902832 49 1.144037e+00 8.606646e-07 * time: 0.06348395347595215 50 1.144037e+00 4.209374e-07 * time: 0.0645899772644043 51 1.144037e+00 3.540226e-07 * time: 0.06574797630310059 52 1.144037e+00 1.692595e-07 * time: 0.06686806678771973 53 1.144037e+00 2.020204e-07 * time: 0.06765007972717285 54 1.144037e+00 9.579822e-08 * time: 0.06843113899230957 55 1.144037e+00 1.073623e-07 * time: 0.06955504417419434
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:
ψ = 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.(ψ))
1.161130389502071e-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)
1.321459555244377e-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.00022342332015932995