# FOD optimization :::{admonition} Video :class: tip [Watch the narrated video of this tutorial on YouTube](https://www.youtube.com/watch?v=msGTCzfqwUs). ::: :::{admonition} In this tutorial :class: tip The FLOSIC energy depends on the Fermi-orbital descriptors (FODs), and the right FODs are the ones that minimize it. You let DFTSW27 guess FODs for water automatically, relax them against the energy, and follow the relaxation in the output. You compare guesses, speed the relaxation up with a frozen-density stage, and relax the FODs of an atom. **Time:** 27 s for the main run, about 10 s for the exercises (32 cores). **Files:** {download}`h2o_fodopt.yaml `, {download}`h2o_fodopt.out ` (reference output), exercise inputs `files/h2o_fodopt_assembly.yaml`, `files/h2o_fodopt_frozen.yaml`, `files/ne_thomson.yaml`, {download}`run.sh `. ::: ## Why FODs need optimizing In {doc}`../07_flosic/index` the FODs were given. They define the localized orbitals in which the self-interaction is subtracted, so the FLOSIC energy is a function of their positions, $E(\mathbf a_1,\dots,\mathbf a_N)$. The physically meaningful FLOSIC solution is the minimum of this function. DFTSW27 computes the forces $-\partial E/\partial\mathbf a_i$ on the FODs. With the Jacobi solver they are exact. It moves the FODs with a quasi-Newton (BFGS) optimizer until the largest force is small, solving the SIC SCF at every step. ## The input file ```yaml system: charge: 0 unpaired_electrons: 0 units: angstrom functional: exchange: slater correlation: pw92 basis: name: UTEP-NRLMOL file: basis/FLOSIC-LDA_atoms_normalized.nw scf: conv_tol: 1.0e-8 sic: functional: pz_sic solver: jacobi fod_guess_method: centroid fod_optimization: energy fix_fod_at_nucleus: true relax_checkpoint_file: h2o_fods_relaxed.dat atoms: # water, as in tutorial 7 ... ``` `fod_guess_method` : Without a `fod_file`, DFTSW27 builds starting FODs itself: - **`thomson`** (default): shell-by-shell points on spheres around each atom. Ideal for atoms. For molecules the per-atom electron counts must add up (exercise 1). - **`centroid`**: the centres of Foster–Boys localized orbitals from a preliminary plain-DFT SCF. Needs no chemical input. - **`assembly`**: FODs placed from a Lewis structure and a database of optimized FOD positions (main-group elements H–Kr). Usually the best start for molecules. - **`quantum_p`, `quantum_d`, `quantum_lp`, `quantum_md`**: guesses from the orbitals of a preliminary SCF (User Manual). Every guess is a starting point, to be followed by an optimization. `fod_optimization: energy` : Relax the FODs against the SIC energy. The default criterion is `relax_force_tol: 1e-4` Ha/bohr on the largest FOD force component, with at most `relax_max_iter: 30` outer iterations, each a full SIC SCF. `solver: jacobi` : The Jacobi solver gives exact FOD forces. With KLI the forces are approximate (User Manual, `frozen_cycle`). `fix_fod_at_nucleus: true` : Hold each atom's 1s FOD at its nucleus, where it belongs anyway. This is standard practice and helps convergence. Hydrogen has no core and is skipped. `relax_checkpoint_file` : The current FODs, rewritten after every step, in `fod_file` format: the input for later runs (as in tutorial 7), and a restart point. ## Running ```bash $ OMP_NUM_THREADS=16 ./run_dft tutorials/08_fodopt/files/h2o_fodopt.yaml > h2o_fodopt.out ``` ## Reading the output **The guess.** The centroid guess first runs a plain LDA SCF, localizes its orbitals and puts the FODs at their centres: ```text sic_functional = pz_sic, fod_file not set -- fod_guess_method=centroid: deferring the FOD guess to a preliminary plain-DFT SCF boys_centroid_fods: alpha, 5 orbitals, 9 Jacobi sweeps run_dft: wrote the Boys-centroid FOD guess to fod_centroid.dat relax_fods_sc: pinning alpha FOD 1 to nucleus of atom 1 ``` **The relaxation.** One line per outer iteration (between them, the SIC SCF of that step): ```text FOD relaxation (mod_flosic_sc_relax): SC-FLOSIC, analytic force, strong-Wolfe (bfgs direction) iter e_total e_sic max|force| step(Bohr) iter 1 -76.5913850426 0.6985516602 2.2381E-01 2.0000E-01 iter 2 -76.6728587936 0.7765540201 1.2284E-02 2.0000E-01 iter 3 -76.6779269545 0.7810506048 1.7887E-03 2.0000E-01 ... iter 17 -76.6788996893 0.7820244091 9.4630E-05 1.2500E-02 relax_fods_sc: CONVERGED after 17 outer iterations final fixed-FOD Jacobi SCF at the relaxed FODs (semicanonical SIC orbital energies) ``` The energy drops by 87 mHa: the centroid guess places the lone-pair FODs much too close to the oxygen nucleus. Most of the gain comes in the first two steps; the rest is a slow approach to the force threshold. `step` is the largest FOD displacement allowed in that step. The final Jacobi SCF at the relaxed FODs gives the results printed after it. **Files.** | File | Contents | |---|---| | `fod_centroid.dat` | the starting guess | | `h2o_fods_relaxed.dat` | the relaxed FODs (`relax_checkpoint_file`) | | `FOD_RELAX_PROGRESS.log` | every outer iteration: energy, $e_{\rm sic}$, max force, every FOD's position and force | ```{figure} figures/fods_centroid_relaxed.png :width: 55% Starting FODs (faded) and relaxed FODs; spin up gold, spin down violet. The lone-pair FODs move outward by about 0.8 bohr, the bond FODs hardly move. `dftsw27plot.py fods h2o_fodopt.out fod_centroid.dat h2o_fods_relaxed.dat` ``` ## Things to try ### 1. An atom from the Thomson guess `files/ne_thomson.yaml` relaxes neon from the default Thomson guess: one FOD at the nucleus and four on a sphere. :::{dropdown} What you should find Converged after 3 outer iterations at $E=-129.2805996$ Ha, 0.1 mHa above the value of a direct minimization of the FLOSIC energy (`scf_algorithm: direct`). The Jacobi fixed point and the direct minimum can differ by this much. The Thomson guess builds FODs atom by atom, from each atom's neutral electron count and moment. For water, oxygen gives 4 FODs per spin and a hydrogen without a moment none, so 4 instead of 5 per spin, and the guess stops with a message. You can give the hydrogens per-atom moments of $+1$ and $-1$, but the Jacobi solver accepts moments only with `scf_algorithm: direct`. For molecules use `assembly` or `centroid`. ::: ### 2. A better guess `files/h2o_fodopt_assembly.yaml` starts from the `assembly` guess. :::{dropdown} What you should find ```{figure} figures/fod_relaxation.png :width: 95% Energy (relative to the lowest final energy) and largest FOD force per outer iteration. ``` | guess | outer iterations | time | final $E$ (Ha) | |---|---|---|---| | centroid | 17 | 27 s | $-76.6788997$ | | assembly | 2 | 5 s | $-76.6789032$ | | centroid + frozen density (exercise 3) | 2 (+ 25 cheap steps) | 5 s | $-76.6789040$ | The assembly guess is already within 0.01 mHa of the minimum: its database holds optimized FOD positions for each kind of bond and lone pair. The final energies agree to 4 µHa, which is what a force threshold of $10^{-4}$ Ha/bohr allows. These relaxed FODs are the file of tutorial 7. ::: ### 3. Freezing the density first `frozen_density: true` (`files/h2o_fodopt_frozen.yaml`) first optimizes the FODs at fixed orbitals: no SCF per step, so each step is cheap. Then it releases the constraint for the normal relaxation. :::{dropdown} What you should find ```text Frozen-density FOD optimization CONVERGED after 25 steps (25 force evaluations) Releasing the frozen-density constraint: continuing with SCF-coupled FOD relaxation relax_fods_sc: CONVERGED after 2 outer iterations ``` Two full SCF steps instead of 17, 5 s instead of 27 s, and the same minimum. Use it whenever the guess is poor. ::: ## Practical advice - **Relax once, reuse.** The relaxed FODs are the input (`fod_file`) for further runs at the same geometry with the same functional. - **Converge tighter for nuclear gradients.** The SIC nuclear gradient is exact only at optimized FODs, so consider a tighter `relax_force_tol` (e.g. $10^{-5}$) before a geometry optimization. - **Open shells can have several minima.** Compare different starts by their energy; the User Manual lists `relax_restarts` and multi-start options for `scf_algorithm: direct`. - **Not converging within `relax_max_iter`.** Restart from the checkpoint file (`fod_file: h2o_fods_relaxed.dat`), or use `frozen_density: true`. **Next:** {doc}`../09_lsic/index`.