We solve the 1D Gross-Pitaevskii equation with a custom potential.
This is similar to Gross-Pitaevskii equation in 1D example
and we show how to define local potentials attached to atoms, which allows for
instance to compute forces.
The custom potential is actually already defined as ElementGaussian
in DFTK, and could
be used as is.
using DFTK
using LinearAlgebra
First, we define a new element which represents a nucleus generating a Gaussian potential.
struct CustomPotential <: DFTK.Element
α # Prefactor
L # Width of the Gaussian nucleus
end
Some default values
CustomPotential() = CustomPotential(1.0, 0.5);
We extend the two methods providing access to the real and Fourier representation of the potential to DFTK.
function DFTK.local_potential_real(el::CustomPotential, r::Real)
-el.α / (√(2π) * el.L) * exp(- (r / el.L)^2 / 2)
end
function DFTK.local_potential_fourier(el::CustomPotential, p::Real)
# = ∫ V(r) exp(-ix⋅p) dx
-el.α * exp(- (p * el.L)^2 / 2)
end
Gaussian potentials and DFTK
DFTK already implements
CustomPotential
in form of theDFTK.ElementGaussian
, so this explicit re-implementation is only provided for demonstration purposes.
We set up the lattice. For a 1D case we supply two zero lattice vectors
a = 10
lattice = a .* [[1 0 0.]; [0 0 0]; [0 0 0]];
In this example, we want to generate two Gaussian potentials generated by two "nuclei" localized at positions x1 and x2, that are expressed in [0,1) in fractional coordinates. |x1−x2| should be different from 0.5 to break symmetry and get nonzero forces.
x1 = 0.2
x2 = 0.8
positions = [[x1, 0, 0], [x2, 0, 0]]
gauss = CustomPotential()
atoms = [gauss, gauss];
We setup a Gross-Pitaevskii model
C = 1.0
α = 2;
n_electrons = 1 # Increase this for fun
terms = [Kinetic(),
AtomicLocal(),
LocalNonlinearity(ρ -> C * ρ^α)]
model = Model(lattice, atoms, positions; n_electrons, terms,
spin_polarization=:spinless); # use "spinless electrons"
We discretize using a moderate Ecut and run a SCF algorithm to compute forces
afterwards. As there is no ionic charge associated to gauss
we have to specify
a starting density and we choose to start from a zero density.
basis = PlaneWaveBasis(model; Ecut=500, kgrid=(1, 1, 1))
ρ = zeros(eltype(basis), basis.fft_size..., 1)
scfres = self_consistent_field(basis; tol=1e-5, ρ)
scfres.energies
n Energy log10(ΔE) log10(Δρ) Diag Δtime --- --------------- --------- --------- ---- ------ 1 -0.143553788980 -0.42 8.0 233ms 2 -0.156032441512 -1.90 -1.10 1.0 58.2ms 3 -0.156769355985 -3.13 -1.56 1.0 842μs 4 -0.157045773146 -3.56 -2.32 1.0 776μs 5 -0.157052891724 -5.15 -2.68 1.0 742μs 6 -0.157056384471 -5.46 -3.65 1.0 748μs 7 -0.157056405066 -7.69 -4.12 1.0 721μs 8 -0.157056406883 -8.74 -4.95 1.0 758μs 9 -0.157056406912 -10.53 -5.31 1.0 744μs
Energy breakdown (in Ha): Kinetic 0.0380294 AtomicLocal -0.3163465 LocalNonlinearity 0.1212608 total -0.157056406912
Computing the forces can then be done as usual:
compute_forces(scfres)
2-element Vector{StaticArraysCore.SVector{3, Float64}}: [-0.05568565687478944, 0.0, 0.0] [0.05568660289350602, 0.0, 0.0]
Extract the converged total local potential
tot_local_pot = DFTK.total_local_potential(scfres.ham)[:, 1, 1]; # use only dimension 1
Extract other quantities before plotting them
ρ = scfres.ρ[:, 1, 1, 1] # converged density, first spin component
ψ_fourier = scfres.ψ[1][:, 1] # first k-point, all G components, first eigenvector
101-element Vector{ComplexF64}: 0.23604326478731336 - 0.9436054290094268im 0.02425907548108349 - 0.09697734399682581im -0.02874262221672827 + 0.11490266682744071im -0.012486303726337864 + 0.04991458308125314im 0.0021788988420742656 - 0.008710671163639695im 0.003315696243315579 - 0.013254649020993737im 0.0004783799167270374 - 0.0019121913768802164im -0.0005321860005208545 + 0.002127501741453898im -0.00022922468934527792 + 0.0009163154999455728im 3.488976040818906e-5 - 0.00013948585671455274im ⋮ 3.4889623097778416e-5 - 0.00013948495774455865im -0.00022920280839420075 + 0.0009163183842559104im -0.0005321832601997748 + 0.002127504834803301im 0.00047831378297903676 - 0.0019122050686719114im 0.003315636840205554 - 0.013254660810430115im 0.002179032782628568 - 0.008710625673328575im -0.012486082389081983 + 0.04991466317856004im -0.028743219992033532 + 0.11490247437051933im 0.024258779264420986 - 0.0969774095552969im
Transform the wave function to real space and fix the phase:
ψ = ifft(basis, basis.kpoints[1], ψ_fourier)[:, 1, 1]
ψ /= (ψ[div(end, 2)] / abs(ψ[div(end, 2)]));
using Plots
x = a * vec(first.(DFTK.r_vectors(basis)))
p = plot(x, real.(ψ), label="real(ψ)")
plot!(p, x, imag.(ψ), label="imag(ψ)")
plot!(p, x, ρ, label="ρ")
plot!(p, x, tot_local_pot, label="tot local pot")