Custom potential

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 the DFTK.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 $x_1$ and $x_2$, that are expressed in $[0,1)$ in fractional coordinates. $|x_1 - x_2|$ 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
Energy breakdown (in Ha):
    Kinetic             0.0380294 
    AtomicLocal         -0.3163465
    LocalNonlinearity   0.1212607 

    total               -0.157056406916

Computing the forces can then be done as usual:

compute_forces(scfres)
2-element Vector{StaticArraysCore.SVector{3, Float64}}:
 [-0.055682246991620346, -0.0, -0.0]
 [0.055680470298178214, -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.11109472875698735 + 0.9663156666186258im
     0.01141719420806513 + 0.09931029205201163im
   -0.013528162476045847 - 0.1176680727485323im
   -0.005876338136779029 - 0.05111587880496305im
   0.0010255400365650038 + 0.008920599764129827im
    0.001560564997987263 + 0.013573864532144944im
  0.00022516058836358836 + 0.0019583148601463366im
 -0.00025048808397836614 - 0.002178717036934001im
 -0.00010789534335755708 - 0.0009384168640469962im
    1.642117647736182e-5 + 0.000142828429004531im
                         ⋮
   1.6419931323361807e-5 + 0.00014282902105774848im
 -0.00010787983739032206 - 0.000938418673656848im
 -0.00025047448067293504 - 0.002178719259691095im
  0.00022512486664073787 + 0.00195831886592379im
   0.0015605363317904074 + 0.013573867561800193im
   0.0010256147022275257 + 0.0089205904203304im
   -0.005876965620157728 - 0.05111580728464018im
   -0.013527809676694657 - 0.11766811349340583im
    0.011417676956999717 + 0.09931023650406888im

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")
Example block output