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.676296e+02 1.569304e+02 * time: 0.0005040168762207031 1 1.569729e+02 1.172376e+02 * time: 0.002096891403198242 2 1.154506e+02 1.278276e+02 * time: 0.004133939743041992 3 6.027551e+01 9.631214e+01 * time: 0.006357908248901367 4 5.432612e+01 9.923688e+01 * time: 0.0083160400390625 5 4.936006e+01 8.955212e+01 * time: 0.010009050369262695 6 3.116001e+01 7.705015e+01 * time: 0.011693954467773438 7 5.906720e+00 1.845137e+01 * time: 0.01340484619140625 8 3.246261e+00 5.803628e+00 * time: 0.014837980270385742 9 2.275165e+00 2.493780e+00 * time: 0.016015052795410156 10 1.880706e+00 2.604682e+00 * time: 0.017187833786010742 11 1.413685e+00 1.822961e+00 * time: 0.018396854400634766 12 1.283332e+00 1.781974e+00 * time: 0.019362926483154297 13 1.197557e+00 1.066727e+00 * time: 0.02030801773071289 14 1.179234e+00 8.486206e-01 * time: 0.02121591567993164 15 1.159120e+00 5.777676e-01 * time: 0.02213001251220703 16 1.149098e+00 2.456666e-01 * time: 0.02305293083190918 17 1.146033e+00 1.201687e-01 * time: 0.023970842361450195 18 1.144499e+00 7.030165e-02 * time: 0.024890899658203125 19 1.144197e+00 3.677684e-02 * time: 0.025542020797729492 20 1.144096e+00 3.173746e-02 * time: 0.026453018188476562 21 1.144067e+00 1.611968e-02 * time: 0.027362823486328125 22 1.144053e+00 1.210524e-02 * time: 0.028301000595092773 23 1.144049e+00 1.177349e-02 * time: 0.028949975967407227 24 1.144043e+00 7.191353e-03 * time: 0.029866933822631836 25 1.144039e+00 3.940658e-03 * time: 0.030772924423217773 26 1.144038e+00 2.952111e-03 * time: 0.031685829162597656 27 1.144037e+00 1.770095e-03 * time: 0.032614946365356445 28 1.144037e+00 9.939459e-04 * time: 0.033531904220581055 29 1.144037e+00 6.624174e-04 * time: 0.034442901611328125 30 1.144037e+00 5.483406e-04 * time: 0.035347938537597656 31 1.144037e+00 4.632020e-04 * time: 0.03628182411193848 32 1.144037e+00 3.189801e-04 * time: 0.03719282150268555 33 1.144037e+00 2.961620e-04 * time: 0.03810000419616699 34 1.144037e+00 1.536009e-04 * time: 0.0390169620513916 35 1.144037e+00 9.471999e-05 * time: 0.03993082046508789 36 1.144037e+00 5.617899e-05 * time: 0.04095101356506348 37 1.144037e+00 1.796171e-05 * time: 0.04186201095581055 38 1.144037e+00 1.343504e-05 * time: 0.04284191131591797 39 1.144037e+00 6.546055e-06 * time: 0.04375100135803223 40 1.144037e+00 4.383270e-06 * time: 0.04468488693237305 41 1.144037e+00 2.571939e-06 * time: 0.0455930233001709 42 1.144037e+00 1.601070e-06 * time: 0.04649782180786133 43 1.144037e+00 1.242006e-06 * time: 0.04749894142150879 44 1.144037e+00 7.492115e-07 * time: 0.048429012298583984 45 1.144037e+00 6.986179e-07 * time: 0.049340009689331055 46 1.144037e+00 5.973524e-07 * time: 0.05034303665161133 47 1.144037e+00 3.809266e-07 * time: 0.051242828369140625 48 1.144037e+00 2.567736e-07 * time: 0.052201032638549805
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.1225937441356955e-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)
3.322758294724426e-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.00022351464256441226