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.696984e+02     1.609230e+02
 * time: 0.0004928112030029297
     1     1.685692e+02     1.241667e+02
 * time: 0.0017278194427490234
     2     1.205450e+02     1.336305e+02
 * time: 0.0038568973541259766
     3     8.836168e+01     1.181966e+02
 * time: 0.005646944046020508
     4     2.358290e+01     6.401658e+01
 * time: 0.007350921630859375
     5     1.849204e+01     5.369975e+01
 * time: 0.008742809295654297
     6     4.844388e+00     2.381481e+01
 * time: 0.00999903678894043
     7     3.232381e+00     7.725110e+00
 * time: 0.010939836502075195
     8     1.929858e+00     1.037768e+01
 * time: 0.011874914169311523
     9     1.489733e+00     6.579945e+00
 * time: 0.012948036193847656
    10     1.240645e+00     2.854978e+00
 * time: 0.014071941375732422
    11     1.181890e+00     7.487593e-01
 * time: 0.014979839324951172
    12     1.164747e+00     4.232490e-01
 * time: 0.015902996063232422
    13     1.154497e+00     2.919645e-01
 * time: 0.01692485809326172
    14     1.150000e+00     3.531799e-01
 * time: 0.017912864685058594
    15     1.147268e+00     1.875993e-01
 * time: 0.018877029418945312
    16     1.144921e+00     1.009468e-01
 * time: 0.01997089385986328
    17     1.144742e+00     1.068119e-01
 * time: 0.02079486846923828
    18     1.144390e+00     6.054911e-02
 * time: 0.021623849868774414
    19     1.144129e+00     6.319764e-02
 * time: 0.022655963897705078
    20     1.144057e+00     1.269546e-02
 * time: 0.0234220027923584
    21     1.144053e+00     9.805785e-03
 * time: 0.02440786361694336
    22     1.144040e+00     6.332917e-03
 * time: 0.02537703514099121
    23     1.144039e+00     5.551739e-03
 * time: 0.02636098861694336
    24     1.144038e+00     4.880190e-03
 * time: 0.027254819869995117
    25     1.144037e+00     2.694711e-03
 * time: 0.028239965438842773
    26     1.144037e+00     1.783312e-03
 * time: 0.02936387062072754
    27     1.144037e+00     1.534160e-03
 * time: 0.030261993408203125
    28     1.144037e+00     1.421684e-03
 * time: 0.031137943267822266
    29     1.144037e+00     6.666474e-04
 * time: 0.031848907470703125
    30     1.144037e+00     2.176300e-04
 * time: 0.03268003463745117
    31     1.144037e+00     1.642421e-04
 * time: 0.034372806549072266
    32     1.144037e+00     1.307222e-04
 * time: 0.035455942153930664
    33     1.144037e+00     1.194693e-04
 * time: 0.0364689826965332
    34     1.144037e+00     4.667283e-05
 * time: 0.03755593299865723
    35     1.144037e+00     4.223087e-05
 * time: 0.03855299949645996
    36     1.144037e+00     2.881812e-05
 * time: 0.039528846740722656
    37     1.144037e+00     1.428124e-05
 * time: 0.040448904037475586
    38     1.144037e+00     7.671627e-06
 * time: 0.041380882263183594
    39     1.144037e+00     5.224334e-06
 * time: 0.04231882095336914
    40     1.144037e+00     4.781252e-06
 * time: 0.0430450439453125
    41     1.144037e+00     5.081461e-06
 * time: 0.043782949447631836
    42     1.144037e+00     2.191185e-06
 * time: 0.044551849365234375
    43     1.144037e+00     1.239762e-06
 * time: 0.04538583755493164
    44     1.144037e+00     9.169111e-07
 * time: 0.04632401466369629
    45     1.144037e+00     7.352664e-07
 * time: 0.04721999168395996
    46     1.144037e+00     4.971911e-07
 * time: 0.04829287528991699
    47     1.144037e+00     3.084620e-07
 * time: 0.049362897872924805
    48     1.144037e+00     2.350292e-07
 * time: 0.05026698112487793
    49     1.144037e+00     1.129135e-07
 * time: 0.051175832748413086
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.3250535760055254e-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]:
1.5515748768512322e-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.00022341923778219596