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.796554e+02     1.621610e+02
 * time: 0.00046706199645996094
     1     1.765557e+02     1.381380e+02
 * time: 0.0013620853424072266
     2     1.322890e+02     1.575009e+02
 * time: 0.0027070045471191406
     3     1.291856e+02     1.438058e+02
 * time: 0.004106998443603516
     4     8.519556e+01     1.400220e+02
 * time: 0.005408048629760742
     5     3.577844e+01     7.357446e+01
 * time: 0.006927967071533203
     6     1.253144e+01     1.527640e+01
 * time: 0.008049964904785156
     7     8.806660e+00     3.319643e+01
 * time: 0.00855112075805664
     8     4.613286e+00     6.924283e+00
 * time: 0.009604930877685547
     9     3.816805e+00     2.065872e+01
 * time: 0.01052093505859375
    10     2.969985e+00     1.412308e+01
 * time: 0.01155400276184082
    11     2.823231e+00     4.246204e+00
 * time: 0.01222991943359375
    12     2.804328e+00     1.095390e+01
 * time: 0.012892007827758789
    13     2.801366e+00     9.364745e+00
 * time: 0.01359415054321289
    14     2.503603e+00     6.227278e+00
 * time: 0.01429605484008789
    15     2.007359e+00     3.262992e+00
 * time: 0.01511693000793457
    16     1.699088e+00     2.141388e+00
 * time: 0.015716075897216797
    17     1.615603e+00     2.562667e+00
 * time: 0.016309022903442383
    18     1.324555e+00     1.425976e+00
 * time: 0.016967058181762695
    19     1.211161e+00     8.615679e-01
 * time: 0.01774311065673828
    20     1.156973e+00     7.266140e-01
 * time: 0.01848912239074707
    21     1.149926e+00     2.129798e-01
 * time: 0.019238948822021484
    22     1.147934e+00     4.172996e-01
 * time: 0.019976139068603516
    23     1.145721e+00     1.601387e-01
 * time: 0.02072906494140625
    24     1.144693e+00     4.780865e-02
 * time: 0.021558046340942383
    25     1.144553e+00     6.495186e-02
 * time: 0.02232813835144043
    26     1.144236e+00     5.347165e-02
 * time: 0.023051977157592773
    27     1.144113e+00     5.457617e-02
 * time: 0.023749113082885742
    28     1.144065e+00     2.518325e-02
 * time: 0.02445697784423828
    29     1.144053e+00     1.149139e-02
 * time: 0.025213003158569336
    30     1.144042e+00     5.884988e-03
 * time: 0.025954008102416992
    31     1.144039e+00     3.964863e-03
 * time: 0.026658058166503906
    32     1.144038e+00     2.576994e-03
 * time: 0.027341127395629883
    33     1.144037e+00     2.183956e-03
 * time: 0.028028011322021484
    34     1.144037e+00     1.397237e-03
 * time: 0.06602597236633301
    35     1.144037e+00     9.794050e-04
 * time: 0.06675601005554199
    36     1.144037e+00     9.784813e-04
 * time: 0.06742000579833984
    37     1.144037e+00     1.000341e-03
 * time: 0.0681149959564209
    38     1.144037e+00     6.879353e-04
 * time: 0.06882596015930176
    39     1.144037e+00     3.680227e-04
 * time: 0.06939697265625
    40     1.144037e+00     3.060667e-04
 * time: 0.0699150562286377
    41     1.144037e+00     1.928656e-04
 * time: 0.07056808471679688
    42     1.144037e+00     9.736329e-05
 * time: 0.07121396064758301
    43     1.144037e+00     3.826276e-05
 * time: 0.0718541145324707
    44     1.144037e+00     3.344021e-05
 * time: 0.0724949836730957
    45     1.144037e+00     3.469809e-05
 * time: 0.07315897941589355
    46     1.144037e+00     3.149593e-05
 * time: 0.07380795478820801
    47     1.144037e+00     2.086549e-05
 * time: 0.07444000244140625
    48     1.144037e+00     1.217044e-05
 * time: 0.07503604888916016
    49     1.144037e+00     9.643829e-06
 * time: 0.0756380558013916
    50     1.144037e+00     3.833179e-06
 * time: 0.07624197006225586
    51     1.144037e+00     2.246018e-06
 * time: 0.07684206962585449
    52     1.144037e+00     1.864299e-06
 * time: 0.07745003700256348
    53     1.144037e+00     1.484146e-06
 * time: 0.07799005508422852
    54     1.144037e+00     9.633378e-07
 * time: 0.07853198051452637
    55     1.144037e+00     3.599088e-07
 * time: 0.07897615432739258
    56     1.144037e+00     3.506398e-07
 * time: 0.0795290470123291
    57     1.144037e+00     2.020302e-07
 * time: 0.08013701438903809
    58     1.144037e+00     1.125714e-07
 * time: 0.08073902130126953
    59     1.144037e+00     1.207900e-07
 * time: 0.0814211368560791
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]:
1.024068132162509e-15

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]:
2.0013135757351903e-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.00022342068330310092