Gross-Pitaevskii equation in one dimension

In this example we will use DFTK to solve the Gross-Pitaevskii equation, and use this opportunity to explore a few internals.

The model

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 magnetism).

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,

In [1]:
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:

In [2]:
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

In [3]:
C = 1.0
α = 2;

... and with this build the model

In [4]:
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:

In [5]:
Ecut = 500
basis = PlaneWaveBasis(model, Ecut, 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.614387e+02     1.806263e+02
 * time: 0.0006468296051025391
     1     1.436126e+02     1.657295e+02
 * time: 0.0025129318237304688
     2     1.131730e+02     1.847482e+02
 * time: 0.0043888092041015625
     3     3.371126e+01     9.999710e+01
 * time: 0.006456851959228516
     4     9.345840e+00     2.243569e+01
 * time: 0.008205890655517578
     5     8.232266e+00     3.425224e+01
 * time: 0.009320974349975586
     6     7.782163e+00     9.977994e+00
 * time: 0.01027679443359375
     7     5.516550e+00     1.344878e+01
 * time: 0.011412858963012695
     8     3.689370e+00     1.737232e+01
 * time: 0.01254582405090332
     9     2.215969e+00     2.796277e+00
 * time: 0.014145851135253906
    10     1.648608e+00     2.213569e+00
 * time: 0.015372991561889648
    11     1.486869e+00     1.613429e+00
 * time: 0.016527891159057617
    12     1.305844e+00     3.558300e+00
 * time: 0.017470836639404297
    13     1.255289e+00     3.427566e+00
 * time: 0.01844000816345215
    14     1.219686e+00     1.080702e+00
 * time: 0.019402027130126953
    15     1.183469e+00     7.747285e-01
 * time: 0.020337820053100586
    16     1.159813e+00     5.149841e-01
 * time: 0.02131485939025879
    17     1.152193e+00     2.151827e-01
 * time: 0.02229785919189453
    18     1.149209e+00     1.914533e-01
 * time: 0.023207902908325195
    19     1.145469e+00     9.750422e-02
 * time: 0.024147987365722656
    20     1.144848e+00     9.403890e-02
 * time: 0.02506089210510254
    21     1.144358e+00     5.305962e-02
 * time: 0.026002883911132812
    22     1.144260e+00     4.956954e-02
 * time: 0.026878833770751953
    23     1.144135e+00     4.103386e-02
 * time: 0.027789831161499023
    24     1.144076e+00     1.937664e-02
 * time: 0.028650999069213867
    25     1.144052e+00     1.035006e-02
 * time: 0.029529809951782227
    26     1.144042e+00     9.597718e-03
 * time: 0.030444860458374023
    27     1.144039e+00     5.935892e-03
 * time: 0.031320810317993164
    28     1.144038e+00     7.411316e-03
 * time: 0.032212018966674805
    29     1.144037e+00     6.077827e-03
 * time: 0.033101797103881836
    30     1.144037e+00     3.494883e-03
 * time: 0.03406786918640137
    31     1.144037e+00     2.298563e-03
 * time: 0.035002946853637695
    32     1.144037e+00     1.398335e-03
 * time: 0.035893917083740234
    33     1.144037e+00     8.037125e-04
 * time: 0.03655695915222168
    34     1.144037e+00     5.204882e-04
 * time: 0.037425994873046875
    35     1.144037e+00     3.453041e-04
 * time: 0.03835582733154297
    36     1.144037e+00     1.800365e-04
 * time: 0.03937983512878418
    37     1.144037e+00     7.956492e-05
 * time: 0.04031491279602051
    38     1.144037e+00     4.568732e-05
 * time: 0.041235923767089844
    39     1.144037e+00     1.829647e-05
 * time: 0.04217791557312012
    40     1.144037e+00     1.876433e-05
 * time: 0.0430757999420166
    41     1.144037e+00     1.499032e-05
 * time: 0.043968915939331055
    42     1.144037e+00     1.058132e-05
 * time: 0.04488492012023926
    43     1.144037e+00     1.013058e-05
 * time: 0.045764923095703125
    44     1.144037e+00     8.721327e-06
 * time: 0.04668593406677246
    45     1.144037e+00     4.979075e-06
 * time: 0.04758596420288086
    46     1.144037e+00     4.008791e-06
 * time: 0.048503875732421875
    47     1.144037e+00     3.794795e-06
 * time: 0.04938483238220215
    48     1.144037e+00     2.777426e-06
 * time: 0.05030989646911621
    49     1.144037e+00     1.671054e-06
 * time: 0.05119490623474121
    50     1.144037e+00     1.370743e-06
 * time: 0.052063941955566406
    51     1.144037e+00     1.042339e-06
 * time: 0.052937984466552734
    52     1.144037e+00     6.456199e-07
 * time: 0.05388784408569336
    53     1.144037e+00     3.389542e-07
 * time: 0.054811954498291016
    54     1.144037e+00     1.069706e-07
 * time: 0.055673837661743164
Out[5]:
Energy breakdown:
    Kinetic             0.2682057 
    ExternalFromReal    0.4707475 
    PowerNonlinearity   0.4050836 

    total               1.144036852755 

Internals

We use the opportunity to explore some of DFTK internals.

Extract the converged density and the obtained wave function:

In [6]:
ρ = real(scfres.ρ.real)[:, 1, 1]  # converged density
ψ_fourier = scfres.ψ[1][:, 1];    # first kpoint, all G components, first eigenvector

Transform the wave function to real space and fix the phase:

In [7]:
ψ = G_to_r(basis, basis.kpoints[1], ψ_fourier)[:, 1, 1]
ψ /= (ψ[div(end, 2)] / abs(ψ[div(end, 2)]));

Check whether ψ is normalised:

In [8]:
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 ψ:

In [9]:
norm(scfres.ρ.real - abs2.(ψ))
Out[9]:
7.932758161526176e-16

We summarize the ground state in a nice plot:

In [10]:
using Plots

p = plot(x, real.(ψ), label="real(ψ)")
plot!(p, x, imag.(ψ), label="imag(ψ)")
plot!(p, x, ρ, label="ρ")
Out[10]:

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.

In [11]:
E, ham = energy_hamiltonian(basis, scfres.ψ, scfres.occupation; ρ=scfres.ρ)
@assert E.total == scfres.energies.total

Now the Hamiltonian contains all the blocks corresponding to kpoints. Here, we just have one kpoint:

In [12]:
H = ham.blocks[1];

H can be used as a linear operator (efficiently using FFTs), or converted to a dense matrix:

In [13]:
ψ11 = scfres.ψ[1][:, 1] # first kpoint, 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:

In [14]:
norm(H * ψ11 - dot(ψ11, H * ψ11) * ψ11)
Out[14]:
1.6414868696706246e-7

Build a finite-differences version of the GPE operator H, as a sanity check:

In [15]:
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(ψ, ψ)) * ψ))
Out[15]:
0.00022342553456520191