Geometry optimization

In this tutorial

You optimize water from a deliberately distorted start, follow the optimizer step by step in the output, check the result against an independent code and against experiment, and learn when the default convergence criteria are not tight enough. The exercises compare internal and Cartesian coordinates on the water dimer and run a constrained (relaxed) angle scan. Time: 3 s for the main run, about 1.5 min for all exercises on 16 cores. Files: h2o_opt.yaml, h2o_opt.out (reference output), dimer_opt.yaml, h2o_scan.yaml, run.sh.

The input file

files/h2o_opt.yaml starts water with O–H bonds of 1.05 Å and a 120° angle. The PBE/def2-TZVP minimum is near 0.97 Å and 104°.

job:
  task: optimize

system:
  charge: 0
  unpaired_electrons: 0
  units: angstrom

functional:
  exchange: pbe
  correlation: pbe

basis:
  name: def2-TZVP
  file: basis/def2-TZVP.nw

scf:
  conv_tol: 1.0e-8

optimization:
  trajectory_file: h2o_opt_traj.xyz

atoms:
  - element: O
    position: [0.000000,  0.000000,  0.000000]
  - element: H
    position: [0.000000,  0.909327, -0.525000]
  - element: H
    position: [0.000000, -0.909327, -0.525000]

Compared with Your first calculation: SCF and cube files, there are two new sections:

job: task: optimize

Minimize the energy with respect to the nuclear positions. Other tasks are energy (the default), gradient, frequencies, opt_freq (optimize, then frequencies), ts, irc and md.

optimization

Everything has a default. trajectory_file additionally writes every accepted geometry as a multi-frame XYZ file that Molden, Jmol, VMD or ASE can play.

The settings you are most likely to change are:

Key

Default

Meaning

max_force, rms_force

4.5e-4, 3.0e-4

Largest and RMS force (Ha/bohr) at convergence.

max_step, rms_step

1.8e-3, 1.2e-3

Largest and RMS component of the next step (bohr).

max_iter

100

Maximum number of gradient evaluations.

coordinates

internal

Redundant internal coordinates, or cartesian.

trust_radius

0.3

Initial trust radius (bohr).

constraints

none

Fixed distances, angles, dihedrals or atoms (exercise 2).

The run converges when all four criteria are met (the Gaussian defaults), or when both force criteria are 100 times below threshold.

scf: conv_tol: 1.0e-8 is the default. It is written out here because analytic gradients need a converged SCF: do not loosen it for optimizations.

The Coulomb method changes with the task

For single-point DFT energies the default Coulomb method is the numerical Poisson solver. For optimize, gradient and frequencies it is exact integrals (eri). The first lines of the output say so: coulomb: method = eri (default; poisson is not the default for task=optimize (analytic gradients)).

Running

$ OMP_NUM_THREADS=8 ./run_dft tutorials/02_geomopt/files/h2o_opt.yaml > h2o_opt.out

tutorials/02_geomopt/run.sh does this and makes the figures. run.sh --exercises also runs the two exercises.

Reading the output

The full reference output is files/h2o_opt.out.

The optimizer setup

 Geometry optimization (delocalized internal coordinates, RFO/BFGS, lindh Hessian, trust  0.300)
  internal coordinates: 3 primitives (2 bonds, 1 angles, 0 dihedrals, 0 linear bends, 0 Cartesian)

The optimizer takes rational-function (RFO) steps inside a trust radius. It updates an approximate Hessian with BFGS, starting from Lindh’s model Hessian. The steps are taken in internal coordinates, here the two O–H bonds and the H–O–H angle.

The SCF at the first geometry is printed in full, exactly as in tutorial 1. For the later geometries the output prints only one line per SCF (scf: inner_output: full prints them all).

One geometry step

Each step prints the energy contributions and frontier orbitals of that geometry, then three optimizer lines:

 ---------------- geometry step 0 ----------------
  ...
  TOTAL ENERGY              =       -76.3609179475
  ...
  Cartesian forces: max |F| =   6.5445E-02 (atom 2 H ), RMS F =   3.2590E-02 Ha/bohr
  opt   energy (Ha)           max|F|     rms F      max step   rms step   trust
  opt   0      -76.3609179475  6.545E-02  3.259E-02  1.869E-01  9.107E-02  3.000E-01
          (max F, rms F, max step, rms step below threshold:) F F F F
   SCF converged in 9 iterations, E =       -76.3760113719
  • The opt line has the energy, the largest and RMS force, the largest and RMS component of the step the optimizer is about to take, and the trust radius.

  • The T/F flags show which of the four criteria are already met.

  • The SCF converged line belongs to the next geometry. It starts from the orbitals of the previous geometry, so it needs only 6–9 iterations.

Convergence

  opt   3      -76.3776068927  1.001E-03  4.207E-04  2.383E-03  1.464E-03  3.000E-01
          (max F, rms F, max step, rms step below threshold:) F F F F
  opt   4      -76.3776098579  3.739E-04  1.527E-04  1.087E-03  5.957E-04  3.000E-01  converged
          (max F, rms F, max step, rms step below threshold:) T T T T
  Geometry optimization converged in 5 gradient evaluations
../../_images/opt_convergence.png

Fig. 9 Energy (relative to the final one) and forces at each geometry step, with the default thresholds. The energy is converged to 0.002 kcal/mol at step 3, but the forces need one more step.

$ python3 tutorials/tools/dftsw27plot.py opt h2o_opt.out -o opt_convergence.png

The final geometry

 Optimized geometry                   (bohr columns omitted here)
   atom    ...           x             y             z   (Angstrom)
     1 O   ...    0.00000000   -0.00000000    0.04635053
     2 H   ...    0.00000000    0.76665675   -0.54817523
     3 H   ...    0.00000000   -0.76665675   -0.54817523

 Nuclear gradient at the optimized geometry
   max |g| =   3.7392E-04   rms =   1.5269E-04   |sum_A g_A| =   1.8651E-14

The geometry is printed in bohr and Ångström (the bohr columns are left out above). The gradient follows; \(|\sum_A g_A|\) is the net force on the molecule and must be zero to round-off. After the optimization, the usual Results, population analysis and SUMMARY are for the optimized geometry, and so are any cube, Molden or restart files you request.

Files written next to the output:

File

Contents

h2o_opt_traj.xyz

Every accepted geometry with its energy (trajectory_file).

opt_traj_forces.xyz

Extended XYZ of every step with the forces, for ASE or OVITO (force_trajectory_file).

geom_final.xyz

The optimized geometry (final_geometry_file).

../../_images/h2o_start_final.png

Fig. 10 Start (faded) and optimized geometry: dftsw27plot.py mol h2o_opt_traj.xyz --frames 0,-1 --view yz.

How good is the result?

O–H (Å)

H–O–H (°)

\(E\) (Ha)

default criteria (this run, 5 steps)

0.97017

104.41

\(-76.3776098579\)

max_force: 1.0e-5, rms_force: 1.0e-5 (6 steps)

0.97034

104.30

\(-76.3776101688\)

experiment (\(r_e\), \(\theta_e\))

0.9572

104.52

Two things to note:

  • The default criteria are loose for the angle. It moves by 0.11° when the forces are converged 45 times tighter, while the energy changes by only 0.3 µHa. This is fine for energies, but use tight criteria before a frequency calculation (tutorial 3).

  • PBE gives O–H bonds about 0.013 Å too long. This is typical of GGAs. The angle is close to experiment.

Independent check

The forces at the distorted starting geometry agree with PySCF (same Cartesian basis, PBE) to \(7\times10^{-6}\) Ha/bohr. At the tightly optimized geometry the PySCF gradient is \(1.3\times10^{-5}\) Ha/bohr, which is the grid difference between the two codes.

Things to try

1. Internal versus Cartesian coordinates

files/dimer_opt.yaml starts the water dimer from a poor geometry: the hydrogen-bond acceptor is rotated by 40° and pulled 0.4 Å away from the S22 reference geometry. Optimize it once with the default coordinates: internal and once with coordinates: cartesian.

What you should find

Coordinates

Gradient evaluations

Time (16 cores)

\(E\) (Ha)

internal

12

15 s

\(-152.7650982\)

Cartesian

39

43 s

\(-152.7650978\)

Both reach the same minimum: O···O = 2.89 Å and H···O = 1.92 Å. Internal coordinates need three times fewer steps, because they describe the soft hydrogen bond separately from the stiff O–H bonds. The binding energy \(E_{\rm dimer}-2E_{\rm monomer}=-6.2\) kcal/mol is larger than the accurate value of about \(-5.0\) kcal/mol. Much of the difference is basis-set superposition error with a triple-\(\zeta\) basis without diffuse functions.

../../_images/dimer_start_final.png

Fig. 11 Water dimer: start (faded) and optimized, with the hydrogen bond dotted.

2. A relaxed scan

files/h2o_scan.yaml fixes the H–O–H angle and lets the O–H bonds relax:

optimization:
  constraints: angle 2 1 3 100.0     # atoms H-O-H, degrees

Atom numbers start at 1, in the order of the atoms list. Constraints are written on one line, separated by ;. The forms are distance i j [value], angle i j k [value], dihedral i j k l [value] and freeze i [j ...]. Without a value the starting value is held. Run the input for angles from 90° to 120° (run.sh --exercises does this) and plot the energies:

$ python3 tutorials/tools/dftsw27plot.py scan scan_*.out --angle 2 1 3 -o angle_scan.png
What you should find
../../_images/angle_scan.png

Fig. 12 Relaxed scan. The fitted minimum, 104.3°, agrees with the tightly converged optimization. Bending by 15° costs about 3 kcal/mol.

Each constrained optimization takes 4–8 steps and ends exactly at the requested angle. The printed gradient is the full one, so it is not zero along the constrained angle.

3. Tight convergence

Add max_force: 1.0e-5 and rms_force: 1.0e-5 to the optimization section and reproduce the second row of the table above.

Troubleshooting

  • The optimizer goes back and forth. Lower trust_radius (for example to 0.1). A step that raises the energy is retaken automatically with a smaller trust radius.

  • A symmetric molecule stays symmetric. The gradient keeps the symmetry of the start. If a lower-symmetry minimum is possible, distort the start slightly.

  • The SCF fails at some geometry. Check that the start is reasonable (no atoms too close) and see the SCF troubleshooting of tutorial 1.

  • “not available” messages. Optimizations are not available in noncollinear mode or with moment_mode: mom. Optimizations with self-interaction correction need extra settings, covered in the FLOSIC tutorials.

Next: Vibrational frequencies and IR spectrum of the optimized water.