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.755505e+02     1.599729e+02
 * time: 0.0004899501800537109
     1     1.610994e+02     1.377143e+02
 * time: 0.0016510486602783203
     2     1.217723e+02     1.526572e+02
 * time: 0.003050088882446289
     3     4.913712e+01     1.010788e+02
 * time: 0.004614114761352539
     4     2.652192e+01     7.768343e+01
 * time: 0.00592494010925293
     5     7.521172e+00     4.704053e+00
 * time: 0.007122039794921875
     6     6.532983e+00     5.301687e+00
 * time: 0.007838964462280273
     7     5.552310e+00     3.404753e+01
 * time: 0.008712053298950195
     8     4.458500e+00     1.650916e+01
 * time: 0.009562969207763672
     9     3.300532e+00     1.607191e+01
 * time: 0.010463953018188477
    10     2.070245e+00     1.019517e+01
 * time: 0.011322021484375
    11     1.767372e+00     4.951547e+00
 * time: 0.012186050415039062
    12     1.550104e+00     2.946036e+00
 * time: 0.012905120849609375
    13     1.410975e+00     2.125734e+00
 * time: 0.013620138168334961
    14     1.375322e+00     2.328545e+00
 * time: 0.014403104782104492
    15     1.307995e+00     2.575039e+00
 * time: 0.015725135803222656
    16     1.211577e+00     2.053909e+00
 * time: 0.016564130783081055
    17     1.205535e+00     8.903566e-01
 * time: 0.017141103744506836
    18     1.173329e+00     7.974770e-01
 * time: 0.01771402359008789
    19     1.156801e+00     3.910967e-01
 * time: 0.018522024154663086
    20     1.146309e+00     1.858864e-01
 * time: 0.019355058670043945
    21     1.144562e+00     1.562937e-01
 * time: 0.020115137100219727
    22     1.144410e+00     1.520720e-01
 * time: 0.02083301544189453
    23     1.144228e+00     8.848874e-02
 * time: 0.02154397964477539
    24     1.144131e+00     4.414995e-02
 * time: 0.022284984588623047
    25     1.144099e+00     2.488609e-02
 * time: 0.022999048233032227
    26     1.144078e+00     1.697392e-02
 * time: 0.023713111877441406
    27     1.144052e+00     1.950068e-02
 * time: 0.024424076080322266
    28     1.144043e+00     2.142163e-02
 * time: 0.02513599395751953
    29     1.144039e+00     1.240663e-02
 * time: 0.025846004486083984
    30     1.144038e+00     6.180939e-03
 * time: 0.026591062545776367
    31     1.144037e+00     3.052281e-03
 * time: 0.027302980422973633
    32     1.144037e+00     7.743455e-04
 * time: 0.028010129928588867
    33     1.144037e+00     4.706603e-04
 * time: 0.028717994689941406
    34     1.144037e+00     4.072362e-04
 * time: 0.029428958892822266
    35     1.144037e+00     3.423622e-04
 * time: 0.030187129974365234
    36     1.144037e+00     3.063016e-04
 * time: 0.030903100967407227
    37     1.144037e+00     2.882213e-04
 * time: 0.03162407875061035
    38     1.144037e+00     1.797990e-04
 * time: 0.03228902816772461
    39     1.144037e+00     9.520380e-05
 * time: 0.0330049991607666
    40     1.144037e+00     5.032534e-05
 * time: 0.0337069034576416
    41     1.144037e+00     3.164985e-05
 * time: 0.03456306457519531
    42     1.144037e+00     2.292945e-05
 * time: 0.03520798683166504
    43     1.144037e+00     1.698935e-05
 * time: 0.035932064056396484
    44     1.144037e+00     6.683249e-06
 * time: 0.03664398193359375
    45     1.144037e+00     6.134734e-06
 * time: 0.03734993934631348
    46     1.144037e+00     3.275605e-06
 * time: 0.03808093070983887
    47     1.144037e+00     1.721814e-06
 * time: 0.03879690170288086
    48     1.144037e+00     1.706302e-06
 * time: 0.03946495056152344
    49     1.144037e+00     1.878129e-06
 * time: 0.04014396667480469
    50     1.144037e+00     1.561906e-06
 * time: 0.04085111618041992
    51     1.144037e+00     8.420301e-07
 * time: 0.041558027267456055
    52     1.144037e+00     8.571096e-07
 * time: 0.04229402542114258
    53     1.144037e+00     6.736076e-07
 * time: 0.042997121810913086
    54     1.144037e+00     2.660108e-07
 * time: 0.04355502128601074
    55     1.144037e+00     2.104323e-07
 * time: 0.04423403739929199
    56     1.144037e+00     1.231348e-07
 * time: 0.044934988021850586
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.385285437428026e-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.677075901831831e-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.00022342696110591457