# 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]:
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.845817e+02     1.875738e+02
* time: 0.0006749629974365234
1     1.784198e+02     1.561448e+02
* time: 0.001744985580444336
2     1.339381e+02     1.759117e+02
* time: 0.0036199092864990234
3     1.151392e+02     1.578388e+02
* time: 0.006090879440307617
4     6.085448e+01     1.369946e+02
* time: 0.008424043655395508
5     1.161184e+01     9.576371e+00
* time: 0.010793924331665039
6     8.740923e+00     4.084099e+01
* time: 0.012814044952392578
7     7.988010e+00     1.021672e+01
* time: 0.014317989349365234
8     5.157600e+00     1.227008e+01
* time: 0.015616893768310547
9     3.481186e+00     6.859708e+00
* time: 0.016833066940307617
10     2.148391e+00     3.704436e+00
* time: 0.018130064010620117
11     1.692132e+00     2.165911e+00
* time: 0.019144058227539062
12     1.627265e+00     2.216602e+00
* time: 0.02012801170349121
13     1.511015e+00     2.102521e+00
* time: 0.021085023880004883
14     1.346542e+00     2.208534e+00
* time: 0.022098064422607422
15     1.205986e+00     8.990820e-01
* time: 0.023022890090942383
16     1.165216e+00     4.959862e-01
* time: 0.024045944213867188
17     1.151168e+00     1.942536e-01
* time: 0.02516007423400879
18     1.146023e+00     1.329458e-01
* time: 0.02629685401916504
19     1.145388e+00     1.223203e-01
* time: 0.027029037475585938
20     1.144983e+00     8.775142e-02
* time: 0.02786707878112793
21     1.144660e+00     8.372149e-02
* time: 0.029080867767333984
22     1.144270e+00     4.680063e-02
* time: 0.03008294105529785
23     1.144163e+00     3.694710e-02
* time: 0.03103494644165039
24     1.144077e+00     2.590327e-02
* time: 0.032167911529541016
25     1.144049e+00     1.920314e-02
* time: 0.033232927322387695
26     1.144042e+00     1.327942e-02
* time: 0.03415799140930176
27     1.144040e+00     5.242690e-03
* time: 0.03489494323730469
28     1.144039e+00     5.098688e-03
* time: 0.03561592102050781
29     1.144038e+00     3.868386e-03
* time: 0.03658485412597656
30     1.144037e+00     1.368721e-03
* time: 0.03750801086425781
31     1.144037e+00     6.935030e-04
* time: 0.03842902183532715
32     1.144037e+00     4.615488e-04
* time: 0.039407968521118164
33     1.144037e+00     3.203513e-04
* time: 0.040389060974121094
34     1.144037e+00     2.261281e-04
* time: 0.04135894775390625
35     1.144037e+00     2.827412e-04
* time: 0.04246091842651367
36     1.144037e+00     2.766044e-04
* time: 0.04344892501831055
37     1.144037e+00     2.351059e-04
* time: 0.044439077377319336
38     1.144037e+00     1.372076e-04
* time: 0.04544496536254883
39     1.144037e+00     9.924393e-05
* time: 0.04646587371826172
40     1.144037e+00     5.777429e-05
* time: 0.047419071197509766
41     1.144037e+00     3.415872e-05
* time: 0.04836606979370117
42     1.144037e+00     1.457655e-05
* time: 0.04937291145324707
43     1.144037e+00     9.480982e-06
* time: 0.05030107498168945
44     1.144037e+00     5.758112e-06
* time: 0.051245927810668945
45     1.144037e+00     2.428635e-06
* time: 0.05238986015319824
46     1.144037e+00     1.451797e-06
* time: 0.05349397659301758
47     1.144037e+00     1.179369e-06
* time: 0.05450892448425293
48     1.144037e+00     7.596201e-07
* time: 0.0555570125579834
49     1.144037e+00     7.236365e-07
* time: 0.05622291564941406
50     1.144037e+00     4.627091e-07
* time: 0.05716300010681152
51     1.144037e+00     3.224470e-07
* time: 0.05818295478820801
52     1.144037e+00     1.920055e-07
* time: 0.05916285514831543
53     1.144037e+00     1.386505e-07
* time: 0.06018185615539551

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]:
8.4323112932426105e-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.6795297654504086e-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.00022342571230789122