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ψ=(−12Δ+V+2C|ψ|2)ψ=μψ‖ψ‖L2=1
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 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:
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 2.015684e+02 1.451541e+02 * time: 0.0006830692291259766 1 1.842670e+02 1.117706e+02 * time: 0.0014410018920898438 2 1.381893e+02 1.274119e+02 * time: 0.003370046615600586 3 3.906053e+01 5.829388e+01 * time: 0.005837202072143555 4 1.500294e+01 1.056158e+01 * time: 0.00799107551574707 5 1.395365e+01 1.053121e+01 * time: 0.008944034576416016 6 8.274507e+00 2.477097e+01 * time: 0.010503053665161133 7 8.073489e+00 1.705653e+01 * time: 0.011342048645019531 8 5.723847e+00 9.970609e+00 * time: 0.012673139572143555 9 3.609341e+00 4.035644e+00 * time: 0.013773202896118164 10 2.120041e+00 2.933524e+00 * time: 0.015034198760986328 11 1.694492e+00 2.385546e+00 * time: 0.01613020896911621 12 1.354819e+00 9.856240e-01 * time: 0.01720118522644043 13 1.307600e+00 1.092034e+00 * time: 0.01797008514404297 14 1.258360e+00 8.472756e-01 * time: 0.019118070602416992 15 1.204909e+00 6.893778e-01 * time: 0.02001500129699707 16 1.174138e+00 5.458289e-01 * time: 0.020847082138061523 17 1.156438e+00 3.387400e-01 * time: 0.021685123443603516 18 1.148475e+00 2.537470e-01 * time: 0.022644996643066406 19 1.145530e+00 7.210905e-02 * time: 0.023485183715820312 20 1.144664e+00 7.525648e-02 * time: 0.024079084396362305 21 1.144361e+00 3.798331e-02 * time: 0.02500009536743164 22 1.144250e+00 4.237083e-02 * time: 0.025930166244506836 23 1.144154e+00 3.860363e-02 * time: 0.02681708335876465 24 1.144097e+00 2.468123e-02 * time: 0.027675151824951172 25 1.144055e+00 1.086695e-02 * time: 0.02866816520690918 26 1.144046e+00 6.443505e-03 * time: 0.02957010269165039 27 1.144040e+00 4.035874e-03 * time: 0.030518054962158203 28 1.144038e+00 2.810783e-03 * time: 0.03145003318786621 29 1.144037e+00 1.629475e-03 * time: 0.03235316276550293 30 1.144037e+00 1.123801e-03 * time: 0.03327512741088867 31 1.144037e+00 1.267942e-03 * time: 0.03391313552856445 32 1.144037e+00 3.290930e-04 * time: 0.0345911979675293 33 1.144037e+00 4.760476e-04 * time: 0.0352480411529541 34 1.144037e+00 4.453289e-04 * time: 0.0362551212310791 35 1.144037e+00 2.168422e-04 * time: 0.037188053131103516 36 1.144037e+00 1.035559e-04 * time: 0.03815603256225586 37 1.144037e+00 1.121138e-04 * time: 0.04375410079956055 38 1.144037e+00 6.797317e-05 * time: 0.045060157775878906 39 1.144037e+00 4.443128e-05 * time: 0.04605722427368164 40 1.144037e+00 2.640349e-05 * time: 0.047057151794433594 41 1.144037e+00 1.657018e-05 * time: 0.04814505577087402 42 1.144037e+00 1.428427e-05 * time: 0.049221038818359375 43 1.144037e+00 6.915570e-06 * time: 0.05090022087097168 44 1.144037e+00 2.914267e-06 * time: 0.052781105041503906 45 1.144037e+00 2.814019e-06 * time: 0.057882070541381836 46 1.144037e+00 1.664617e-06 * time: 0.05901813507080078 47 1.144037e+00 7.862169e-07 * time: 0.06042814254760742 48 1.144037e+00 6.914599e-07 * time: 0.06145811080932617 49 1.144037e+00 6.295958e-07 * time: 0.062177181243896484 50 1.144037e+00 3.682016e-07 * time: 0.06310915946960449 51 1.144037e+00 1.755326e-07 * time: 0.06403803825378418 52 1.144037e+00 8.182556e-08 * time: 0.1143181324005127 53 1.144037e+00 7.314198e-08 * time: 0.11549711227416992
Energy breakdown (in Ha): 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.ρ)[:, 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.1177931243345485e-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.5505053735266486e-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.0002234216204111667