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.659236e+02     1.273413e+02
 * time: 0.0005431175231933594
     1     1.629640e+02     1.076857e+02
 * time: 0.0017230510711669922
     2     1.189965e+02     1.233141e+02
 * time: 0.0031621456146240234
     3     6.382899e+01     8.841720e+01
 * time: 0.0046901702880859375
     4     5.285382e+01     8.996111e+01
 * time: 0.006206035614013672
     5     3.672449e+01     6.707361e+01
 * time: 0.007630109786987305
     6     9.657292e+00     2.334245e+01
 * time: 0.009122133255004883
     7     5.714108e+00     5.499301e+00
 * time: 0.010296106338500977
     8     5.460833e+00     6.627074e+00
 * time: 0.011226177215576172
     9     4.599566e+00     4.976298e+00
 * time: 0.012139081954956055
    10     3.679981e+00     3.796006e+00
 * time: 0.013181209564208984
    11     3.566766e+00     9.160392e+00
 * time: 0.013978004455566406
    12     2.671424e+00     4.426654e+00
 * time: 0.01498723030090332
    13     1.940267e+00     1.816700e+00
 * time: 0.015896081924438477
    14     1.709309e+00     2.622563e+00
 * time: 0.016662120819091797
    15     1.482670e+00     2.091593e+00
 * time: 0.01723504066467285
    16     1.291693e+00     1.296827e+00
 * time: 0.01830005645751953
    17     1.200778e+00     7.728255e-01
 * time: 0.019262075424194336
    18     1.156592e+00     5.778878e-01
 * time: 0.02006220817565918
    19     1.152121e+00     4.035080e-01
 * time: 0.02081918716430664
    20     1.149436e+00     2.725012e-01
 * time: 0.021610021591186523
    21     1.146264e+00     1.498671e-01
 * time: 0.022431135177612305
    22     1.144894e+00     6.952439e-02
 * time: 0.02320718765258789
    23     1.144380e+00     4.165815e-02
 * time: 0.02394700050354004
    24     1.144285e+00     5.653299e-02
 * time: 0.024468183517456055
    25     1.144139e+00     2.574190e-02
 * time: 0.025199174880981445
    26     1.144084e+00     1.957682e-02
 * time: 0.026006221771240234
    27     1.144044e+00     1.027799e-02
 * time: 0.026789188385009766
    28     1.144040e+00     4.777109e-03
 * time: 0.02761220932006836
    29     1.144038e+00     2.389800e-03
 * time: 0.028562068939208984
    30     1.144037e+00     1.594167e-03
 * time: 0.02924323081970215
    31     1.144037e+00     1.154946e-03
 * time: 0.03010106086730957
    32     1.144037e+00     9.267397e-04
 * time: 0.030907154083251953
    33     1.144037e+00     5.423600e-04
 * time: 0.03166913986206055
    34     1.144037e+00     4.033488e-04
 * time: 0.032476186752319336
    35     1.144037e+00     3.783241e-04
 * time: 0.033306121826171875
    36     1.144037e+00     2.676693e-04
 * time: 0.03416800498962402
    37     1.144037e+00     1.821987e-04
 * time: 0.03498411178588867
    38     1.144037e+00     1.222264e-04
 * time: 0.035822153091430664
    39     1.144037e+00     7.712673e-05
 * time: 0.036646127700805664
    40     1.144037e+00     5.254643e-05
 * time: 0.037484169006347656
    41     1.144037e+00     2.344311e-05
 * time: 0.038346052169799805
    42     1.144037e+00     2.212903e-05
 * time: 0.03913617134094238
    43     1.144037e+00     1.542135e-05
 * time: 0.03997921943664551
    44     1.144037e+00     9.354198e-06
 * time: 0.04082822799682617
    45     1.144037e+00     6.800788e-06
 * time: 0.041671037673950195
    46     1.144037e+00     4.920860e-06
 * time: 0.04252910614013672
    47     1.144037e+00     3.296588e-06
 * time: 0.04340004920959473
    48     1.144037e+00     1.754460e-06
 * time: 0.04426121711730957
    49     1.144037e+00     1.175943e-06
 * time: 0.04513120651245117
    50     1.144037e+00     8.524324e-07
 * time: 0.04598808288574219
    51     1.144037e+00     5.671743e-07
 * time: 0.04689311981201172
    52     1.144037e+00     3.244964e-07
 * time: 0.047750234603881836
    53     1.144037e+00     1.671850e-07
 * time: 0.048693180084228516
    54     1.144037e+00     9.836016e-08
 * time: 0.04957318305969238
    55     1.144037e+00     1.091886e-07
 * time: 0.05047917366027832
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]:
9.290622208760838e-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.8411086231969123e-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.00022342725151536436