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.778722e+02 1.896014e+02 * time: 0.0008480548858642578 1 1.697476e+02 1.356059e+02 * time: 0.004106998443603516 2 1.309580e+02 1.410881e+02 * time: 0.007585048675537109 3 5.074785e+01 9.512703e+01 * time: 0.01159811019897461 4 2.256826e+01 5.890020e+01 * time: 0.015218019485473633 5 2.005575e+01 6.553625e+01 * time: 0.017641067504882812 6 4.395756e+00 7.551468e+00 * time: 0.020124197006225586 7 3.555850e+00 4.648838e+00 * time: 0.022117137908935547 8 2.701007e+00 1.240495e+01 * time: 0.02421116828918457 9 1.979938e+00 4.201658e+00 * time: 0.026283979415893555 10 1.679678e+00 3.302526e+00 * time: 0.027875185012817383 11 1.505921e+00 2.009918e+00 * time: 0.029410123825073242 12 1.375611e+00 1.633936e+00 * time: 0.03139901161193848 13 1.195248e+00 6.281966e-01 * time: 0.03289508819580078 14 1.163141e+00 7.036873e-01 * time: 0.03447699546813965 15 1.151819e+00 4.701986e-01 * time: 0.03608107566833496 16 1.146140e+00 2.700090e-01 * time: 0.03762006759643555 17 1.144899e+00 2.216117e-01 * time: 0.039214134216308594 18 1.144454e+00 9.266961e-02 * time: 0.04081916809082031 19 1.144372e+00 9.631160e-02 * time: 0.04247403144836426 20 1.144130e+00 2.901727e-02 * time: 0.04359912872314453 21 1.144075e+00 2.163725e-02 * time: 0.045310020446777344 22 1.144058e+00 2.442621e-02 * time: 0.04686713218688965 23 1.144049e+00 1.874257e-02 * time: 0.04839301109313965 24 1.144041e+00 6.993482e-03 * time: 0.0499269962310791 25 1.144040e+00 3.926619e-03 * time: 0.0510561466217041 26 1.144038e+00 3.672024e-03 * time: 0.052146196365356445 27 1.144037e+00 2.248227e-03 * time: 0.0536952018737793 28 1.144037e+00 1.058749e-03 * time: 0.05547904968261719 29 1.144037e+00 8.329317e-04 * time: 0.05704617500305176 30 1.144037e+00 8.115895e-04 * time: 0.058730125427246094 31 1.144037e+00 7.980699e-04 * time: 0.0602719783782959 32 1.144037e+00 3.373970e-04 * time: 0.06185102462768555 33 1.144037e+00 1.595301e-04 * time: 0.06347298622131348 34 1.144037e+00 1.599723e-04 * time: 0.0650780200958252 35 1.144037e+00 1.024221e-04 * time: 0.06667208671569824 36 1.144037e+00 1.069512e-04 * time: 0.06821513175964355 37 1.144037e+00 3.880261e-05 * time: 0.06931400299072266 38 1.144037e+00 1.725712e-05 * time: 0.07048702239990234 39 1.144037e+00 7.350636e-06 * time: 0.07209014892578125 40 1.144037e+00 8.843218e-06 * time: 0.07326698303222656 41 1.144037e+00 5.881860e-06 * time: 0.07446813583374023 42 1.144037e+00 3.302919e-06 * time: 0.07616400718688965 43 1.144037e+00 2.048117e-06 * time: 0.07770919799804688 44 1.144037e+00 1.012953e-06 * time: 0.07932615280151367 45 1.144037e+00 6.288406e-07 * time: 0.08087801933288574 46 1.144037e+00 5.185970e-07 * time: 0.08242106437683105 47 1.144037e+00 2.330390e-07 * time: 0.08398604393005371 48 1.144037e+00 2.145736e-07 * time: 0.08563518524169922 49 1.144037e+00 1.615454e-07 * time: 0.08724117279052734 50 1.144037e+00 1.333685e-07 * time: 0.08934617042541504
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.0922720651564026e-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.7955189615047208e-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.00022344388438416256