# 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 LocalNonlinearity(f) term of DFTK, with f(ρ) = C ρ^α. 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])),
LocalNonlinearity(ρ -> C * ρ^α),
]
model = Model(lattice; n_electrons, terms, spin_polarization=:spinless);  # 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.661630e+02     1.220271e+02
* time: 0.0005140304565429688
1     1.560613e+02     9.945211e+01
* time: 0.0024950504302978516
2     1.138946e+02     1.118128e+02
* time: 0.004624128341674805
3     3.905047e+01     6.609204e+01
* time: 0.007133960723876953
4     1.143671e+01     2.537296e+01
* time: 0.009335041046142578
5     5.893976e+00     1.444123e+01
* time: 0.011001110076904297
6     4.399215e+00     6.582750e+00
* time: 0.012304067611694336
7     3.023164e+00     4.070280e+00
* time: 0.013629913330078125
8     2.039549e+00     5.826073e+00
* time: 0.014933109283447266
9     1.723607e+00     2.522300e+00
* time: 0.015949010848999023
10     1.587219e+00     2.180984e+00
* time: 0.016976118087768555
11     1.382871e+00     1.317110e+00
* time: 0.01799297332763672
12     1.229627e+00     6.468400e-01
* time: 0.019016027450561523
13     1.177118e+00     6.259283e-01
* time: 0.02003002166748047
14     1.157871e+00     2.923518e-01
* time: 0.021069049835205078
15     1.147153e+00     3.559411e-01
* time: 0.022092103958129883
16     1.144862e+00     2.244438e-01
* time: 0.02310490608215332
17     1.144425e+00     1.079130e-01
* time: 0.023826122283935547
18     1.144244e+00     8.780283e-02
* time: 0.024842023849487305
19     1.144170e+00     8.273636e-02
* time: 0.02586507797241211
20     1.144068e+00     5.467829e-02
* time: 0.026868104934692383
21     1.144055e+00     4.209759e-02
* time: 0.027873992919921875
22     1.144047e+00     2.903353e-02
* time: 0.02890491485595703
23     1.144042e+00     6.439027e-03
* time: 0.029634952545166016
24     1.144039e+00     6.480712e-03
* time: 0.03063201904296875
25     1.144038e+00     4.101864e-03
* time: 0.03163909912109375
26     1.144037e+00     1.309490e-03
* time: 0.03264808654785156
27     1.144037e+00     7.137552e-04
* time: 0.03368091583251953
28     1.144037e+00     8.020028e-04
* time: 0.0344080924987793
29     1.144037e+00     5.147850e-04
* time: 0.035421133041381836
30     1.144037e+00     6.693456e-04
* time: 0.03643012046813965
31     1.144037e+00     5.633759e-04
* time: 0.03746294975280762
32     1.144037e+00     4.131895e-04
* time: 0.03847908973693848
33     1.144037e+00     1.602104e-04
* time: 0.03950190544128418
34     1.144037e+00     7.009518e-05
* time: 0.04022097587585449
35     1.144037e+00     6.534810e-05
* time: 0.04125690460205078
36     1.144037e+00     4.857843e-05
* time: 0.04227089881896973
37     1.144037e+00     2.267448e-05
* time: 0.043003082275390625
38     1.144037e+00     2.563574e-05
* time: 0.04401397705078125
39     1.144037e+00     1.212221e-05
* time: 0.0450439453125
40     1.144037e+00     3.015074e-06
* time: 0.04608511924743652
41     1.144037e+00     1.838945e-06
* time: 0.047091007232666016
42     1.144037e+00     1.813401e-06
* time: 0.0480959415435791
43     1.144037e+00     6.856473e-07
* time: 0.04913210868835449
44     1.144037e+00     3.937664e-07
* time: 0.05014491081237793
45     1.144037e+00     2.594689e-07
* time: 0.05115008354187012
46     1.144037e+00     2.284454e-07
* time: 0.05215907096862793

Out[5]:
Energy breakdown (in Ha):
Kinetic             0.2682057
ExternalFromReal    0.4707475
LocalNonlinearity   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]:
ψ = ifft(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]:
9.259985883015804e-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]:
4.045693981441243e-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.00022350161312805722