Geometry optimization

We use DFTK and the GeometryOptimization package to find the minimal-energy bond length of the $H_2$ molecule. First we set up an appropriate DFTKCalculator (see AtomsCalculators integration), for which we use the LDA model just like in the Tutorial for silicon.

using DFTK

calc = DFTKCalculator(;
    model_kwargs = (; functionals=LDA()),        # model_DFT keyword arguments
    basis_kwargs = (; kgrid=[1, 1, 1], Ecut=10)  # PlaneWaveBasis keyword arguments
)
DFTKCalculator{Nothing}(DFTK.DFTKParameters((functionals = Xc(lda_x, lda_c_pw),), (kgrid = [1, 1, 1], Ecut = 10), (callback = identity,)), nothing, Base.RefValue{Int64}(0), Base.RefValue{Int64}(0), true)

Next we set up an initial hydrogen molecule within a box of vacuum. We use the parameters of the equivalent tutorial from ABINIT, that is a simulation box of 10 bohr times 10 bohr times 10 bohr and a pseudodojo pseudopotential.

using LinearAlgebra
using LazyArtifacts

r0 = 1.4   # Initial bond length in Bohr
a  = 10.0  # Box size in Bohr

lattice = a * I(3)
H = ElementPsp(:H; psp=load_psp(artifact"pd_nc_sr_pbe_standard_0.4.1_upf/H.upf"));
atoms = [H, H]
positions = [zeros(3), lattice \ [r0, 0., 0.]]

h2_crude = periodic_system(lattice, atoms, positions)
FlexibleSystem(H₂, periodic = TTT):
    bounding_box      : [      10        0        0;
                                0       10        0;
                                0        0       10]u"a₀"

    Atom(H,  [       0,        0,        0]u"a₀")
    Atom(H,  [     1.4,        0,        0]u"a₀")

       .------------.  
      /|            |  
     / |            |  
    *  |            |  
    |  |            |  
    |  |            |  
    |  .------------.  
    | /            /   
    |/            /    
    H-H----------*     

Finally we call minimize_energy! to start the geometry optimisation. We use verbosity=2 to get some insight into the minimisation. With verbosity=1 only a summarising table would be printed and with verbosity=0 (default) the minimisation would be quiet.

using GeometryOptimization
results = minimize_energy!(h2_crude, calc; tol_forces=2e-6, verbosity=2)
n     Energy            log10(ΔE)   log10(Δρ)   α      Diag   Δtime
---   ---------------   ---------   ---------   ----   ----   ------
  1   -1.110569704579                   -0.82   0.80    8.0   28.4ms
  2   -1.117203520901       -2.18       -1.86   0.80    1.0   17.8ms
  3   -1.117251890661       -4.32       -2.71   0.80    1.0   69.2ms
  4   -1.117252481029       -6.23       -3.56   0.80    1.0   18.4ms
  5   -1.117252499308       -7.74       -4.04   0.80    1.0   18.5ms
  6   -1.117252500790       -8.83       -5.08   0.80    1.0   19.1ms
  7   -1.117252500846      -10.25       -5.81   0.80    2.0   22.0ms
  8   -1.117252500846      -12.60       -6.58   0.80    1.0   20.1ms
  9   -1.117252500846      -14.15       -7.16   0.80    1.0   49.8ms
 10   -1.117252500846      -14.65       -7.26   0.80    1.0   20.4ms
 11   -1.117252500846   +  -14.88       -8.52   0.80    1.0   21.1ms
n     Energy            log10(ΔE)   log10(Δρ)   α      Diag   Δtime
---   ---------------   ---------   ---------   ----   ----   ------
  1   -1.117252500846                   -8.91   0.80    1.0   11.5ms
  2   -1.117252500846   +  -15.05       -9.59   0.80    1.0   18.4ms

Geometry optimisation convergence (in atomic units)
┌─────┬─────────────────┬───────────┬────────────┬────────┐
│   n           Energy  log10(ΔE)  max(Force)   Δtime │
├─────┼─────────────────┼───────────┼────────────┼────────┤
│   0 │ -1.117252500846 │           │  0.0269316 │  2.19s │
└─────┴─────────────────┴───────────┴────────────┴────────┘

n     Energy            log10(ΔE)   log10(Δρ)   α      Diag   Δtime
---   ---------------   ---------   ---------   ----   ----   ------
  1   -1.117519303301                   -2.46   0.80    1.0   11.8ms
  2   -1.117521893093       -5.59       -3.49   0.80    1.0   18.2ms
  3   -1.117521920028       -7.57       -4.23   0.80    1.0   18.3ms
  4   -1.117521920479       -9.35       -5.15   0.80    1.0   28.0ms
  5   -1.117521920494      -10.81       -5.94   0.80    1.0   18.9ms
  6   -1.117521920495      -12.17       -6.45   0.80    1.0   19.1ms
  7   -1.117521920495      -13.38       -7.51   0.80    1.0   19.6ms
  8   -1.117521920495   +    -Inf       -8.29   0.80    1.0   24.2ms
  9   -1.117521920495      -15.18       -8.64   0.80    1.0   20.0ms
n     Energy            log10(ΔE)   log10(Δρ)   α      Diag   Δtime
---   ---------------   ---------   ---------   ----   ----   ------
  1   -1.118248561978                   -1.69   0.80    1.0   19.8ms
  2   -1.118340340333       -4.04       -2.70   0.80    1.0   17.7ms
  3   -1.118341307500       -6.01       -3.44   0.80    1.0   17.9ms
  4   -1.118341324940       -7.76       -4.36   0.80    1.0   22.4ms
  5   -1.118341325525       -9.23       -5.12   0.80    1.0   18.7ms
  6   -1.118341325552      -10.57       -5.62   0.80    1.0   19.4ms
  7   -1.118341325554      -11.73       -6.68   0.80    1.0   23.7ms
  8   -1.118341325554      -13.56       -7.58   0.80    1.0   21.5ms
  9   -1.118341325554      -14.65       -8.12   0.80    1.0   19.9ms
 10   -1.118341325554   +  -14.88       -8.55   0.80    1.0   24.7ms
n     Energy            log10(ΔE)   log10(Δρ)   α      Diag   Δtime
---   ---------------   ---------   ---------   ----   ----   ------
  1   -1.118341325554                   -9.05   0.80    1.0   11.4ms
  2   -1.118341325554   +  -15.35      -10.36   0.80    1.0   17.8ms

┌─────┬─────────────────┬───────────┬────────────┬────────┐
│   n           Energy  log10(ΔE)  max(Force)   Δtime │
├─────┼─────────────────┼───────────┼────────────┼────────┤
│   1 │ -1.118341325554 │     -2.96 │  0.0029705 │  2.02s │
└─────┴─────────────────┴───────────┴────────────┴────────┘

n     Energy            log10(ΔE)   log10(Δρ)   α      Diag   Δtime
---   ---------------   ---------   ---------   ----   ----   ------
  1   -1.118348017678                   -3.09   0.80    1.0   11.4ms
  2   -1.118348160925       -6.84       -4.11   0.80    1.0   17.5ms
  3   -1.118348162402       -8.83       -4.86   0.80    1.0   29.9ms
  4   -1.118348162428      -10.59       -5.78   0.80    1.0   18.4ms
  5   -1.118348162429      -12.08       -6.55   0.80    1.0   18.4ms
  6   -1.118348162429      -13.42       -7.04   0.80    1.0   18.7ms
  7   -1.118348162429      -14.54       -8.13   0.80    1.0   23.4ms
  8   -1.118348162429   +  -14.81       -8.95   0.80    1.0   19.1ms
n     Energy            log10(ΔE)   log10(Δρ)   α      Diag   Δtime
---   ---------------   ---------   ---------   ----   ----   ------
  1   -1.118356019890                   -2.61   0.80    1.0   22.3ms
  2   -1.118357382894       -5.87       -3.62   0.80    1.0   18.1ms
  3   -1.118357396968       -7.85       -4.37   0.80    1.0   17.7ms
  4   -1.118357397214       -9.61       -5.29   0.80    1.0   18.3ms
  5   -1.118357397222      -11.10       -6.06   0.80    1.0   23.4ms
  6   -1.118357397223      -12.45       -6.55   0.80    1.0   19.1ms
  7   -1.118357397223      -13.58       -7.65   0.80    1.0   19.6ms
  8   -1.118357397223      -14.75       -8.45   0.80    1.0   24.0ms
n     Energy            log10(ΔE)   log10(Δρ)   α      Diag   Δtime
---   ---------------   ---------   ---------   ----   ----   ------
  1   -1.118357397223                   -9.24   0.80    1.0   11.3ms
  2   -1.118357397223      -14.61       -9.82   0.80    1.0   26.3ms

┌─────┬─────────────────┬───────────┬────────────┬────────┐
│   n           Energy  log10(ΔE)  max(Force)   Δtime │
├─────┼─────────────────┼───────────┼────────────┼────────┤
│   2 │ -1.118357397223 │     -4.79 │ 4.47252e-5 │  1.95s │
└─────┴─────────────────┴───────────┴────────────┴────────┘

n     Energy            log10(ΔE)   log10(Δρ)   α      Diag   Δtime
---   ---------------   ---------   ---------   ----   ----   ------
  1   -1.118357398685                   -4.94   0.80    1.0   11.7ms
  2   -1.118357398714      -10.54       -5.96   0.80    1.0   17.9ms
  3   -1.118357398714      -12.53       -6.70   0.80    1.0   31.6ms
  4   -1.118357398714      -14.06       -7.62   0.80    1.0   18.7ms
  5   -1.118357398714   +  -14.65       -8.39   0.80    1.0   19.1ms
  6   -1.118357398714      -15.05       -8.89   0.80    1.0   24.9ms
n     Energy            log10(ΔE)   log10(Δρ)   α      Diag   Δtime
---   ---------------   ---------   ---------   ----   ----   ------
  1   -1.118357400607                   -4.40   0.80    1.0   11.5ms
  2   -1.118357400953       -9.46       -5.42   0.80    1.0   18.3ms
  3   -1.118357400956      -11.45       -6.17   0.80    1.0   29.5ms
  4   -1.118357400956      -13.18       -7.09   0.80    1.0   18.9ms
  5   -1.118357400956   +    -Inf       -7.86   0.80    1.0   19.0ms
  6   -1.118357400956      -14.95       -8.35   0.80    1.0   24.3ms
  7   -1.118357400956   +  -14.95       -9.45   0.80    1.0   19.9ms
n     Energy            log10(ΔE)   log10(Δρ)   α      Diag   Δtime
---   ---------------   ---------   ---------   ----   ----   ------
  1   -1.118357400956                  -10.08   0.80    1.0   11.5ms
  2   -1.118357400956      -14.95      -11.02   0.80    1.0   29.2ms

┌─────┬─────────────────┬───────────┬────────────┬────────┐
│   n           Energy  log10(ΔE)  max(Force)   Δtime │
├─────┼─────────────────┼───────────┼────────────┼────────┤
│   3 │ -1.118357400956 │     -8.43 │ 1.10896e-8 │  1.90s │
└─────┴─────────────────┴───────────┴────────────┴────────┘

Structure after optimisation (note that the atom has wrapped around)

results.system
FlexibleSystem(H₂, periodic = TTT):
    bounding_box      : [      10        0        0;
                                0       10        0;
                                0        0       10]u"a₀"

    Atom(H,  [-0.0431826, -5.00273e-10, 2.86296e-10]u"a₀")
    Atom(H,  [ 1.44318, -4.84292e-10, 3.43505e-10]u"a₀")

       .------------.  
      /|            |  
     / |            |  
    *  |            |  
    |  |            |  
    |  |            |  
    |  .-H----------H  
    | /            /   
    |/            /    
    *------------*     

Compute final bond length:

rmin = norm(position(results.system[1]) - position(results.system[2]))
println("Optimal bond length: ", rmin)
Optimal bond length: 1.4863652630120272 a₀

Our results (1.486 Bohr) agrees with the equivalent tutorial from ABINIT.