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.620254e+02     1.447057e+02
 * time: 0.0006029605865478516
     1     1.387679e+02     1.079574e+02
 * time: 0.001878976821899414
     2     1.005616e+02     1.217431e+02
 * time: 0.0030519962310791016
     3     2.097994e+01     4.182211e+01
 * time: 0.004261016845703125
     4     1.149958e+01     2.748740e+01
 * time: 0.005278825759887695
     5     9.125888e+00     9.082666e+00
 * time: 0.006073951721191406
     6     7.043560e+00     1.917138e+01
 * time: 0.0068929195404052734
     7     3.988754e+00     5.945317e+00
 * time: 0.007701873779296875
     8     2.726113e+00     3.024515e+00
 * time: 0.008566856384277344
     9     1.579391e+00     2.833376e+00
 * time: 0.009402036666870117
    10     1.413821e+00     2.784967e+00
 * time: 0.010223865509033203
    11     1.316640e+00     2.903979e+00
 * time: 0.010879993438720703
    12     1.225098e+00     1.191196e+00
 * time: 0.011534929275512695
    13     1.183430e+00     5.483881e-01
 * time: 0.01220703125
    14     1.157298e+00     2.956812e-01
 * time: 0.012887954711914062
    15     1.148557e+00     2.021497e-01
 * time: 0.013510942459106445
    16     1.146062e+00     1.049308e-01
 * time: 0.014078855514526367
    17     1.144636e+00     1.088491e-01
 * time: 0.014629840850830078
    18     1.144163e+00     3.123473e-02
 * time: 0.015193939208984375
    19     1.144122e+00     2.765210e-02
 * time: 0.01575303077697754
    20     1.144109e+00     4.863846e-02
 * time: 0.016216039657592773
    21     1.144064e+00     1.773289e-02
 * time: 0.01669001579284668
    22     1.144058e+00     1.421626e-02
 * time: 0.017282962799072266
    23     1.144051e+00     1.498466e-02
 * time: 0.017679929733276367
    24     1.144044e+00     9.733992e-03
 * time: 0.018204927444458008
    25     1.144038e+00     3.650658e-03
 * time: 0.018730878829956055
    26     1.144037e+00     3.983450e-03
 * time: 0.019284963607788086
    27     1.144037e+00     3.426817e-03
 * time: 0.019841909408569336
    28     1.144037e+00     1.854724e-03
 * time: 0.020424842834472656
    29     1.144037e+00     1.374631e-03
 * time: 0.020973920822143555
    30     1.144037e+00     5.714051e-04
 * time: 0.021512985229492188
    31     1.144037e+00     3.041005e-04
 * time: 0.02206897735595703
    32     1.144037e+00     3.243686e-04
 * time: 0.022650957107543945
    33     1.144037e+00     3.128633e-04
 * time: 0.023231029510498047
    34     1.144037e+00     2.327409e-04
 * time: 0.023794889450073242
    35     1.144037e+00     1.546077e-04
 * time: 0.02422499656677246
    36     1.144037e+00     5.593509e-05
 * time: 0.02480602264404297
    37     1.144037e+00     2.952055e-05
 * time: 0.025367021560668945
    38     1.144037e+00     1.658163e-05
 * time: 0.02593088150024414
    39     1.144037e+00     9.627992e-06
 * time: 0.026556968688964844
    40     1.144037e+00     7.030475e-06
 * time: 0.027151823043823242
    41     1.144037e+00     5.072021e-06
 * time: 0.02775287628173828
    42     1.144037e+00     3.266220e-06
 * time: 0.02842998504638672
    43     1.144037e+00     1.813561e-06
 * time: 0.029094934463500977
    44     1.144037e+00     1.296418e-06
 * time: 0.029744863510131836
    45     1.144037e+00     1.452421e-06
 * time: 0.030373811721801758
    46     1.144037e+00     1.276097e-06
 * time: 0.03082895278930664
    47     1.144037e+00     5.119396e-07
 * time: 0.03144383430480957
    48     1.144037e+00     3.767626e-07
 * time: 0.032013893127441406
    49     1.144037e+00     1.981879e-07
 * time: 0.032629966735839844
    50     1.144037e+00     1.486966e-07
 * time: 0.03319883346557617
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.ρ)[:, 1, 1, 1]  # converged density, first spin component
ψ_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.ρ - abs2.(ψ))
Out[9]:
1.3777528460094525e-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.5509757446571617e-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.0002234290790088227