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 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,

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]:
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.569950e+02     1.475165e+02
 * time: 0.0006210803985595703
     1     1.462999e+02     1.280535e+02
 * time: 0.0018641948699951172
     2     1.099572e+02     1.388987e+02
 * time: 0.003020048141479492
     3     3.461111e+01     7.750219e+01
 * time: 0.00440216064453125
     4     9.792206e+00     2.028901e+01
 * time: 0.005523204803466797
     5     4.643768e+00     1.457883e+01
 * time: 0.006398200988769531
     6     3.692633e+00     1.198520e+01
 * time: 0.007093191146850586
     7     2.907833e+00     4.781847e+00
 * time: 0.007870197296142578
     8     2.415187e+00     3.373766e+00
 * time: 0.008682012557983398
     9     2.325554e+00     7.641153e+00
 * time: 0.009289026260375977
    10     1.875915e+00     9.739840e+00
 * time: 0.010096073150634766
    11     1.503594e+00     4.792395e+00
 * time: 0.010890007019042969
    12     1.312164e+00     1.918003e+00
 * time: 0.011541128158569336
    13     1.197278e+00     5.809415e-01
 * time: 0.01223611831665039
    14     1.165462e+00     4.538327e-01
 * time: 0.012847185134887695
    15     1.152961e+00     3.375983e-01
 * time: 0.013442039489746094
    16     1.146802e+00     1.153249e-01
 * time: 0.01402902603149414
    17     1.146042e+00     2.007782e-01
 * time: 0.014696121215820312
    18     1.144605e+00     5.419894e-02
 * time: 0.015351057052612305
    19     1.144371e+00     6.250033e-02
 * time: 0.016062021255493164
    20     1.144189e+00     3.115222e-02
 * time: 0.016746997833251953
    21     1.144114e+00     3.187875e-02
 * time: 0.017208099365234375
    22     1.144084e+00     2.420941e-02
 * time: 0.01781010627746582
    23     1.144048e+00     1.928183e-02
 * time: 0.018447160720825195
    24     1.144041e+00     8.211231e-03
 * time: 0.019070148468017578
    25     1.144038e+00     2.602573e-03
 * time: 0.019697189331054688
    26     1.144037e+00     1.869669e-03
 * time: 0.020338058471679688
    27     1.144037e+00     1.072517e-03
 * time: 0.020920991897583008
    28     1.144037e+00     6.269688e-04
 * time: 0.021499156951904297
    29     1.144037e+00     3.584544e-04
 * time: 0.022119998931884766
    30     1.144037e+00     3.676293e-04
 * time: 0.022752046585083008
    31     1.144037e+00     5.429743e-04
 * time: 0.02339315414428711
    32     1.144037e+00     1.205961e-04
 * time: 0.02405405044555664
    33     1.144037e+00     6.787509e-05
 * time: 0.024689197540283203
    34     1.144037e+00     7.920861e-05
 * time: 0.025269031524658203
    35     1.144037e+00     4.074150e-05
 * time: 0.025886058807373047
    36     1.144037e+00     3.299029e-05
 * time: 0.02650308609008789
    37     1.144037e+00     1.246370e-05
 * time: 0.027100086212158203
    38     1.144037e+00     1.310536e-05
 * time: 0.027744054794311523
    39     1.144037e+00     9.293200e-06
 * time: 0.028406143188476562
    40     1.144037e+00     3.987355e-06
 * time: 0.029052019119262695
    41     1.144037e+00     3.673421e-06
 * time: 0.029731035232543945
    42     1.144037e+00     2.799374e-06
 * time: 0.030381202697753906
    43     1.144037e+00     2.361704e-06
 * time: 0.031016111373901367
    44     1.144037e+00     1.226682e-06
 * time: 0.031699180603027344
    45     1.144037e+00     6.938885e-07
 * time: 0.03235316276550293
    46     1.144037e+00     3.865066e-07
 * time: 0.032981157302856445
    47     1.144037e+00     2.257562e-07
 * time: 0.03363919258117676
    48     1.144037e+00     1.805372e-07
 * time: 0.034243106842041016
Out[5]:
Energy breakdown (in Ha):
    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.ρ)[:, 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:

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.ρ - abs2.(ψ))
Out[9]:
7.490189186705262e-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 k-points. Here, we just have one k-point:

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 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:

In [14]:
norm(H * ψ11 - dot(ψ11, H * ψ11) * ψ11)
Out[14]:
3.5111470189989075e-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.00022344684174090268