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 in combination with a pseudodojo pseudopotential (see Pseudopotentials).

using DFTK
using PseudoPotentialData

pseudopotentials = PseudoFamily("dojo.nc.sr.pbe.v0_4_1.standard.upf")
calc = DFTKCalculator(;
    model_kwargs = (; functionals=LDA(), pseudopotentials),  # model_DFT keyword arguments
    basis_kwargs = (; kgrid=[1, 1, 1], Ecut=10)  # PlaneWaveBasis keyword arguments
)
DFTKCalculator(functionals=Xc(lda_x, lda_c_pw), pseudopotentials=PseudoFamily("dojo.nc.sr.pbe.v0_4_1.standard.upf"), Ecut=10, kgrid=[1, 1, 1])

Next we set up an initial hydrogen molecule within a box of vacuum. We use the parameters of the equivalent tutorial from ABINIT and DFTK's AtomsBase integration to setup the hydrogen molecule. We employ a simulation box of 10 bohr times 10 bohr times 10 bohr and a pseudodojo pseudopotential.

using LinearAlgebra
using Unitful
using UnitfulAtomic

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

cell_vectors = [[a, 0, 0]u"bohr", [0, a, 0]u"bohr", [0, 0, a]u"bohr"]
h2_crude = periodic_system([:H => [0, 0, 0.]u"bohr",
                            :H => [r0, 0, 0]u"bohr"],
                           cell_vectors)
FlexibleSystem(H₂, periodicity = TTT):
    cell_vectors      : [      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₀")

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)
nothing  # hide
n     Energy            log10(ΔE)   log10(Δρ)   α      Diag   Δtime 
---   ---------------   ---------   ---------   ----   ----   ------
  1   -1.110593307538                   -0.82   0.80    7.0    2.80s
  2   -1.117194906251       -2.18       -1.87   0.80    1.0    1.18s
  3   -1.117250092931       -4.26       -2.70   0.80    1.0   23.7ms
  4   -1.117251115694       -5.99       -3.38   0.80    1.0   22.9ms
  5   -1.117251187968       -7.14       -4.22   0.80    1.0   23.2ms
  6   -1.117251190071       -8.68       -4.81   0.80    2.0   26.4ms
  7   -1.117251190141      -10.16       -5.62   0.80    1.0   53.9ms
  8   -1.117251190143      -11.61       -6.51   0.80    1.0   25.2ms
  9   -1.117251190143      -13.44       -6.97   0.80    1.0   23.9ms
 10   -1.117251190143      -15.18       -7.88   0.80    1.0   24.8ms
 11   -1.117251190143      -15.65       -8.84   0.80    2.0   27.3ms
n     Energy            log10(ΔE)   log10(Δρ)   α      Diag   Δtime 
---   ---------------   ---------   ---------   ----   ----   ------
  1   -1.117251190143                   -9.38   0.80    1.0    2.41s
  2   -1.117251190143   +  -14.70      -10.83   0.80    1.0   22.3ms

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

n     Energy            log10(ΔE)   log10(Δρ)   α      Diag   Δtime 
---   ---------------   ---------   ---------   ----   ----   ------
  1   -1.117517997646                   -2.46   0.80    1.0   15.8ms
  2   -1.117520586047       -5.59       -3.49   0.80    1.0   71.7ms
  3   -1.117520612986       -7.57       -4.23   0.80    1.0   22.6ms
  4   -1.117520613449       -9.33       -5.14   0.80    1.0   23.4ms
  5   -1.117520613465      -10.81       -5.92   0.80    1.0   23.4ms
  6   -1.117520613465      -12.14       -6.42   0.80    1.0   41.7ms
  7   -1.117520613465      -13.37       -7.40   0.80    1.0   24.3ms
  8   -1.117520613465      -14.75       -8.22   0.80    1.0   24.3ms
  9   -1.117520613465   +  -14.95       -8.94   0.80    1.0   23.7ms
n     Energy            log10(ΔE)   log10(Δρ)   α      Diag   Δtime 
---   ---------------   ---------   ---------   ----   ----   ------
  1   -1.118247264596                   -1.69   0.80    1.0   14.9ms
  2   -1.118339043931       -4.04       -2.70   0.80    1.0   22.7ms
  3   -1.118340011110       -6.01       -3.44   0.80    1.0   29.9ms
  4   -1.118340028550       -7.76       -4.36   0.80    1.0   22.4ms
  5   -1.118340029135       -9.23       -5.12   0.80    1.0   24.4ms
  6   -1.118340029162      -10.57       -5.62   0.80    1.0   31.7ms
  7   -1.118340029164      -11.73       -6.68   0.80    1.0   24.1ms
  8   -1.118340029164      -13.57       -7.58   0.80    1.0   27.5ms
  9   -1.118340029164   +  -15.65       -8.12   0.80    1.0   24.2ms
 10   -1.118340029164      -15.05       -8.55   0.80    1.0   27.2ms
n     Energy            log10(ΔE)   log10(Δρ)   α      Diag   Δtime 
---   ---------------   ---------   ---------   ----   ----   ------
  1   -1.118340029164                   -9.05   0.80    1.0   15.6ms
  2   -1.118340029164   +  -14.57      -10.36   0.80    1.0    118ms
n     Energy            log10(ΔE)   log10(Δρ)   α      Diag   Δtime 
---   ---------------   ---------   ---------   ----   ----   ------
  1   -1.118340029164                  -11.05   0.80    1.0    572ms
  2   -1.118340029164      -14.40      -11.42   0.80    1.0   23.3ms

┌─────┬─────────────────┬───────────┬─────────────┬────────┐
│   n │          Energy │ log10(ΔE) │  max(Force) │  Δtime │
├─────┼─────────────────┼───────────┼─────────────┼────────┤
│   1 │ -1.118340029164 │           │  0.00297055 │  3.66s │
└─────┴─────────────────┴───────────┴─────────────┴────────┘

n     Energy            log10(ΔE)   log10(Δρ)   α      Diag   Δtime 
---   ---------------   ---------   ---------   ----   ----   ------
  1   -1.118346721505                   -3.09   0.80    1.0   15.2ms
  2   -1.118346864757       -6.84       -4.11   0.80    1.0   22.5ms
  3   -1.118346866234       -8.83       -4.86   0.80    1.0   22.3ms
  4   -1.118346866260      -10.59       -5.78   0.80    1.0   22.6ms
  5   -1.118346866261      -12.09       -6.55   0.80    1.0   23.0ms
  6   -1.118346866261      -13.43       -7.04   0.80    1.0   22.8ms
  7   -1.118346866261      -14.37       -8.13   0.80    1.0   24.5ms
  8   -1.118346866261   +  -15.05       -8.95   0.80    1.0   24.1ms
n     Energy            log10(ΔE)   log10(Δρ)   α      Diag   Δtime 
---   ---------------   ---------   ---------   ----   ----   ------
  1   -1.118354724004                   -2.61   0.80    1.0   15.4ms
  2   -1.118356087056       -5.87       -3.62   0.80    1.0   21.9ms
  3   -1.118356101131       -7.85       -4.37   0.80    1.0   23.1ms
  4   -1.118356101377       -9.61       -5.29   0.80    1.0   23.2ms
  5   -1.118356101385      -11.10       -6.06   0.80    1.0   23.3ms
  6   -1.118356101386      -12.45       -6.55   0.80    1.0   26.4ms
  7   -1.118356101386      -13.60       -7.65   0.80    1.0   40.4ms
  8   -1.118356101386      -14.48       -8.45   0.80    1.0   24.7ms
n     Energy            log10(ΔE)   log10(Δρ)   α      Diag   Δtime 
---   ---------------   ---------   ---------   ----   ----   ------
  1   -1.118356101386                   -9.24   0.80    1.0   15.3ms
  2   -1.118356101386   +  -14.88       -9.82   0.80    1.0   22.5ms
n     Energy            log10(ΔE)   log10(Δρ)   α      Diag   Δtime 
---   ---------------   ---------   ---------   ----   ----   ------
  1   -1.118356101386                  -10.62   0.80    1.0   16.0ms
  2   -1.118356101386   +  -15.18      -11.46   0.80    1.0   21.8ms

┌─────┬─────────────────┬───────────┬─────────────┬────────┐
│   n │          Energy │ log10(ΔE) │  max(Force) │  Δtime │
├─────┼─────────────────┼───────────┼─────────────┼────────┤
│   2 │ -1.118356101386 │     -4.79 │  4.47268e-5 │  598ms │
└─────┴─────────────────┴───────────┴─────────────┴────────┘

n     Energy            log10(ΔE)   log10(Δρ)   α      Diag   Δtime 
---   ---------------   ---------   ---------   ----   ----   ------
  1   -1.118356102848                   -4.94   0.80    1.0   26.9ms
  2   -1.118356102877      -10.54       -5.96   0.80    1.0   22.4ms
  3   -1.118356102877      -12.52       -6.70   0.80    1.0   22.3ms
  4   -1.118356102877      -14.15       -7.62   0.80    1.0   22.9ms
  5   -1.118356102877   +  -14.88       -8.40   0.80    1.0   25.2ms
  6   -1.118356102877   +  -15.18       -8.89   0.80    1.0   24.1ms
n     Energy            log10(ΔE)   log10(Δρ)   α      Diag   Δtime 
---   ---------------   ---------   ---------   ----   ----   ------
  1   -1.118356104771                   -4.40   0.80    1.0   26.1ms
  2   -1.118356105116       -9.46       -5.42   0.80    1.0   22.4ms
  3   -1.118356105120      -11.45       -6.17   0.80    1.0   22.3ms
  4   -1.118356105120      -13.21       -7.09   0.80    1.0   22.9ms
  5   -1.118356105120      -14.57       -7.86   0.80    1.0   23.4ms
  6   -1.118356105120   +  -15.35       -8.35   0.80    1.0   26.7ms
  7   -1.118356105120      -14.42       -9.45   0.80    1.0   23.9ms
n     Energy            log10(ΔE)   log10(Δρ)   α      Diag   Δtime 
---   ---------------   ---------   ---------   ----   ----   ------
  1   -1.118356105120                  -10.08   0.80    1.0   15.3ms
  2   -1.118356105120      -14.51      -11.02   0.80    1.0   23.1ms
n     Energy            log10(ΔE)   log10(Δρ)   α      Diag   Δtime 
---   ---------------   ---------   ---------   ----   ----   ------
  1   -1.118356105120                  -11.67   0.80    1.0   14.9ms
  2   -1.118356105120   +  -15.18      -12.44   0.80    1.0   22.1ms

┌─────┬─────────────────┬───────────┬─────────────┬────────┐
│   n │          Energy │ log10(ΔE) │  max(Force) │  Δtime │
├─────┼─────────────────┼───────────┼─────────────┼────────┤
│   3 │ -1.118356105120 │     -8.43 │  1.13439e-8 │  546ms │
└─────┴─────────────────┴───────────┴─────────────┴────────┘

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

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

    Atom(H,  [-0.0431829, 1.08508e-18, 1.10953e-17]u"a₀")
    Atom(H,  [ 1.44318, 2.11788e-17, -6.56937e-19]u"a₀")

Compute final bond length:

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

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

Cell optimisations

Recent versions of GeometryOptimization support cell shape optimisations as well by passing variablecell=true to minimize_energy!. See the GeometryOptimization documentation for an example.