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=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 1.714548e+02 1.302822e+02 * time: 0.0009160041809082031 1 1.678151e+02 1.151888e+02 * time: 0.0029289722442626953 2 1.254069e+02 1.329490e+02 * time: 0.005979061126708984 3 8.608239e+01 1.079249e+02 * time: 0.009387016296386719 4 2.200054e+01 5.151567e+01 * time: 0.013556957244873047 5 1.182185e+01 8.896212e+00 * time: 0.016191959381103516 6 1.079204e+01 9.584614e+00 * time: 0.017674922943115234 7 7.847477e+00 2.276291e+01 * time: 0.01953291893005371 8 4.692939e+00 6.359450e+00 * time: 0.021930932998657227 9 2.261538e+00 8.465775e+00 * time: 0.08053207397460938 10 1.939640e+00 1.095013e+01 * time: 0.08244204521179199 11 1.507314e+00 1.748148e+00 * time: 0.08376598358154297 12 1.390375e+00 1.405877e+00 * time: 0.0850839614868164 13 1.333261e+00 9.060257e-01 * time: 0.08640003204345703 14 1.263161e+00 1.995364e+00 * time: 0.08774995803833008 15 1.198357e+00 1.878711e+00 * time: 0.08915090560913086 16 1.167643e+00 1.091506e+00 * time: 0.09056305885314941 17 1.151599e+00 4.264317e-01 * time: 0.09211611747741699 18 1.146481e+00 1.133556e-01 * time: 0.09350204467773438 19 1.145629e+00 1.780109e-01 * time: 0.0945289134979248 20 1.144829e+00 9.725087e-02 * time: 0.0958869457244873 21 1.144521e+00 7.453592e-02 * time: 0.09728002548217773 22 1.144117e+00 2.164383e-02 * time: 0.09868502616882324 23 1.144067e+00 1.890759e-02 * time: 0.10003900527954102 24 1.144060e+00 1.543779e-02 * time: 0.10140299797058105 25 1.144052e+00 1.160299e-02 * time: 0.10307502746582031 26 1.144043e+00 5.604264e-03 * time: 0.10449099540710449 27 1.144039e+00 3.872911e-03 * time: 0.10599088668823242 28 1.144038e+00 4.125133e-03 * time: 0.10757088661193848 29 1.144037e+00 2.772683e-03 * time: 0.1090240478515625 30 1.144037e+00 2.153977e-03 * time: 0.11006808280944824 31 1.144037e+00 1.140375e-03 * time: 0.11144900321960449 32 1.144037e+00 6.859473e-04 * time: 0.11743402481079102 33 1.144037e+00 6.443614e-04 * time: 0.11901307106018066 34 1.144037e+00 3.702830e-04 * time: 0.12073707580566406 35 1.144037e+00 1.938106e-04 * time: 0.1224210262298584 36 1.144037e+00 1.696515e-04 * time: 0.12387299537658691 37 1.144037e+00 1.212610e-04 * time: 0.12544488906860352 38 1.144037e+00 6.848434e-05 * time: 0.12694001197814941 39 1.144037e+00 4.508897e-05 * time: 0.128525972366333 40 1.144037e+00 3.459866e-05 * time: 0.1300060749053955 41 1.144037e+00 1.789723e-05 * time: 0.13147211074829102 42 1.144037e+00 1.385642e-05 * time: 0.1339869499206543 43 1.144037e+00 1.095338e-05 * time: 0.13545799255371094 44 1.144037e+00 4.505258e-06 * time: 0.13694310188293457 45 1.144037e+00 3.653269e-06 * time: 0.13843989372253418 46 1.144037e+00 3.947491e-06 * time: 0.13947200775146484 47 1.144037e+00 2.428724e-06 * time: 0.1405010223388672 48 1.144037e+00 1.410912e-06 * time: 0.14203691482543945 49 1.144037e+00 1.061010e-06 * time: 0.14374399185180664 50 1.144037e+00 7.362100e-07 * time: 0.14518404006958008 51 1.144037e+00 2.061343e-07 * time: 0.14659595489501953 52 1.144037e+00 9.734874e-08 * time: 0.14805388450622559 53 1.144037e+00 1.178901e-07 * time: 0.14950108528137207
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.(ψ))
9.144412267294114e-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 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.6803186940175615e-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.00022341418017851473