# 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 :
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 :
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 :
C = 1.0
α = 2;


... and with this build the model

In :
using DFTK
using LinearAlgebra

n_electrons = 1  # Increase this for fun
terms = [Kinetic(),
ExternalFromReal(r -> pot(r)),
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 :
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.597855e+02     1.595717e+02
* time: 0.0005300045013427734
1     1.469986e+02     1.261816e+02
* time: 0.0022919178009033203
2     1.115577e+02     1.294368e+02
* time: 0.0041179656982421875
3     3.741183e+01     7.778237e+01
* time: 0.006308078765869141
4     1.132672e+01     2.650572e+01
* time: 0.008222103118896484
5     5.237033e+00     2.651336e+01
* time: 0.009653091430664062
6     3.525862e+00     5.232413e+00
* time: 0.010807037353515625
7     2.579229e+00     4.300211e+00
* time: 0.011939048767089844
8     1.857861e+00     5.166602e+00
* time: 0.013085126876831055
9     1.348603e+00     1.152814e+00
* time: 0.013997077941894531
10     1.266251e+00     8.369854e-01
* time: 0.014881134033203125
11     1.236860e+00     1.189976e+00
* time: 0.0157620906829834
12     1.180866e+00     1.116620e+00
* time: 0.016663074493408203
13     1.155944e+00     6.315887e-01
* time: 0.017580032348632812
14     1.146175e+00     1.323158e-01
* time: 0.01848292350769043
15     1.145246e+00     9.650417e-02
* time: 0.019381999969482422
16     1.144528e+00     5.546970e-02
* time: 0.020288944244384766
17     1.144217e+00     3.787587e-02
* time: 0.02118396759033203
18     1.144109e+00     4.298021e-02
* time: 0.02210402488708496
19     1.144089e+00     2.082608e-02
* time: 0.02300405502319336
20     1.144053e+00     1.406235e-02
* time: 0.023906946182250977
21     1.144046e+00     1.115889e-02
* time: 0.024810075759887695
22     1.144042e+00     7.881216e-03
* time: 0.02573394775390625
23     1.144039e+00     4.643694e-03
* time: 0.026634931564331055
24     1.144038e+00     2.360815e-03
* time: 0.027534008026123047
25     1.144037e+00     1.434339e-03
* time: 0.028425931930541992
26     1.144037e+00     1.088896e-03
* time: 0.029330015182495117
27     1.144037e+00     4.398326e-04
* time: 0.030247926712036133
28     1.144037e+00     3.776739e-04
* time: 0.03115105628967285
29     1.144037e+00     2.171108e-04
* time: 0.032058000564575195
30     1.144037e+00     1.694329e-04
* time: 0.03296613693237305
31     1.144037e+00     1.174734e-04
* time: 0.03389406204223633
32     1.144037e+00     6.650623e-05
* time: 0.03479599952697754
33     1.144037e+00     5.347064e-05
* time: 0.035427093505859375
34     1.144037e+00     4.466947e-05
* time: 0.036309003829956055
35     1.144037e+00     2.760755e-05
* time: 0.03720593452453613
36     1.144037e+00     2.058599e-05
* time: 0.03815007209777832
37     1.144037e+00     1.853899e-05
* time: 0.03906607627868652
38     1.144037e+00     7.690783e-06
* time: 0.0399630069732666
39     1.144037e+00     5.385035e-06
* time: 0.04087996482849121
40     1.144037e+00     7.139302e-06
* time: 0.041548967361450195
41     1.144037e+00     3.974504e-06
* time: 0.042472124099731445
42     1.144037e+00     2.237889e-06
* time: 0.04339313507080078
43     1.144037e+00     1.608997e-06
* time: 0.04430413246154785
44     1.144037e+00     6.633127e-07
* time: 0.045213937759399414
45     1.144037e+00     4.057094e-07
* time: 0.04614710807800293
46     1.144037e+00     2.924376e-07
* time: 0.04706096649169922
47     1.144037e+00     2.454457e-07
* time: 0.04796314239501953
48     1.144037e+00     1.680865e-07
* time: 0.04887104034423828

Out:
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 :
ρ = real(scfres.ρ)[:, 1, 1, 1]  # converged density, first spin component
ψ_fourier = scfres.ψ[:, 1];    # first k-point, all G components, first eigenvector


Transform the wave function to real space and fix the phase:

In :
ψ = G_to_r(basis, basis.kpoints, ψ_fourier)[:, 1, 1]
ψ /= (ψ[div(end, 2)] / abs(ψ[div(end, 2)]));


Check whether ψ is normalised:

In :
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 :
norm(scfres.ρ - abs2.(ψ))

Out:
8.096429383436293e-16

We summarize the ground state in a nice plot:

In :
using Plots

p = plot(x, real.(ψ), label="real(ψ)")
plot!(p, x, imag.(ψ), label="imag(ψ)")
plot!(p, x, ρ, label="ρ")

Out:

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 :
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 :
H = ham.blocks;


H can be used as a linear operator (efficiently using FFTs), or converted to a dense matrix:

In :
ψ11 = scfres.ψ[:, 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 :
norm(H * ψ11 - dot(ψ11, H * ψ11) * ψ11)

Out:
3.048896039669727e-7

Build a finite-differences version of the GPE operator H, as a sanity check:

In :
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:
0.00022347205815728655