Using a Optimization.jl compatible solver

In this example we optimize a bulk silicon structure using a trust region quasi-Newton method from NLopt.

We create a Stillinger-Weber calculator

using EmpiricalPotentials
sw = StillingerWeber()
nothing

and we build a slightly rattled silicon structure

using AtomsBuilder
using Unitful
system = rattle!(bulk(:Si, cubic=true) * (2, 2, 2), 0.1u"Å")
FlexibleSystem(Si₆₄, periodicity = TTT):
    cell_vectors      : [   10.86        0        0;
                                0    10.86        0;
                                0        0    10.86]u"Å"

We now run GeometryOptimization.minimize_energy!, but notably pass the NLopt.LD_TNEWTON solver from NLopt as the third argument to employ this solver. Extra keyword argument to NLopt can be added, e.g. here the maxevel=100, which limits the solver to 100 function evaluations:

using GeometryOptimization
using OptimizationNLopt
solver = NLopt.LD_TNEWTON()

results = minimize_energy!(system, sw, solver;
                           tol_forces=1e-4u"eV/Å", verbosity=1,
                           maxeval=100)
nothing
┌ Warning: The selected optimization algorithm requires second order derivatives, but `SecondOrder` ADtype was not provided.
        So a `SecondOrder` with SciMLBase.NoAD() for both inner and outer will be created, this can be suboptimal and not work in some cases so
        an explicit `SecondOrder` ADtype is recommended.
@ OptimizationBase ~/.julia/packages/OptimizationBase/SX92W/src/cache.jl:49
Geometry optimisation convergence (in atomic units)
┌─────┬─────────────────┬───────────┬─────────────┬────────┐
│   n │          Energy │ log10(ΔE) │  max(Force) │  Δtime │
├─────┼─────────────────┼───────────┼─────────────┼────────┤
│   0 │ -10.11911754098 │           │   0.0443565 │  6.55s │
Geometry optimisation convergence (in atomic units)
┌─────┬─────────────────┬───────────┬─────────────┬────────┐
│   n │          Energy │ log10(ΔE) │  max(Force) │  Δtime │
├─────┼─────────────────┼───────────┼─────────────┼────────┤
│   0 │ -10.11911755314 │           │   0.0443565 │  6.70s │
Geometry optimisation convergence (in atomic units)
┌─────┬─────────────────┬───────────┬─────────────┬────────┐
│   n │          Energy │ log10(ΔE) │  max(Force) │  Δtime │
├─────┼─────────────────┼───────────┼─────────────┼────────┤
│   0 │ -10.18833249588 │           │  0.00904262 │  6.71s │
Geometry optimisation convergence (in atomic units)
┌─────┬─────────────────┬───────────┬─────────────┬────────┐
│   n │          Energy │ log10(ΔE) │  max(Force) │  Δtime │
├─────┼─────────────────┼───────────┼─────────────┼────────┤
│   0 │ -10.18833249827 │           │  0.00904262 │  6.71s │
Geometry optimisation convergence (in atomic units)
┌─────┬─────────────────┬───────────┬─────────────┬────────┐
│   n │          Energy │ log10(ΔE) │  max(Force) │  Δtime │
├─────┼─────────────────┼───────────┼─────────────┼────────┤
│   0 │ -10.18833249708 │           │  0.00904262 │  6.71s │
Geometry optimisation convergence (in atomic units)
┌─────┬─────────────────┬───────────┬─────────────┬────────┐
│   n │          Energy │ log10(ΔE) │  max(Force) │  Δtime │
├─────┼─────────────────┼───────────┼─────────────┼────────┤
│   0 │ -10.18833249658 │           │  0.00904262 │  6.72s │
Geometry optimisation convergence (in atomic units)
┌─────┬─────────────────┬───────────┬─────────────┬────────┐
│   n │          Energy │ log10(ΔE) │  max(Force) │  Δtime │
├─────┼─────────────────┼───────────┼─────────────┼────────┤
│   0 │ -10.19527931035 │           │  0.00196194 │  6.72s │
Geometry optimisation convergence (in atomic units)
┌─────┬─────────────────┬───────────┬─────────────┬────────┐
│   n │          Energy │ log10(ΔE) │  max(Force) │  Δtime │
├─────┼─────────────────┼───────────┼─────────────┼────────┤
│   0 │ -10.19527931082 │           │  0.00196194 │  6.72s │
Geometry optimisation convergence (in atomic units)
┌─────┬─────────────────┬───────────┬─────────────┬────────┐
│   n │          Energy │ log10(ΔE) │  max(Force) │  Δtime │
├─────┼─────────────────┼───────────┼─────────────┼────────┤
│   0 │ -10.19527931063 │           │  0.00196194 │  6.73s │
Geometry optimisation convergence (in atomic units)
┌─────┬─────────────────┬───────────┬─────────────┬────────┐
│   n │          Energy │ log10(ΔE) │  max(Force) │  Δtime │
├─────┼─────────────────┼───────────┼─────────────┼────────┤
│   0 │ -10.19527931055 │           │  0.00196194 │  6.73s │
Geometry optimisation convergence (in atomic units)
┌─────┬─────────────────┬───────────┬─────────────┬────────┐
│   n │          Energy │ log10(ΔE) │  max(Force) │  Δtime │
├─────┼─────────────────┼───────────┼─────────────┼────────┤
│   0 │ -10.19527931049 │           │  0.00196195 │  6.74s │
Geometry optimisation convergence (in atomic units)
┌─────┬─────────────────┬───────────┬─────────────┬────────┐
│   n │          Energy │ log10(ΔE) │  max(Force) │  Δtime │
├─────┼─────────────────┼───────────┼─────────────┼────────┤
│   0 │ -10.19527931043 │           │  0.00196194 │  6.78s │
Geometry optimisation convergence (in atomic units)
┌─────┬─────────────────┬───────────┬─────────────┬────────┐
│   n │          Energy │ log10(ΔE) │  max(Force) │  Δtime │
├─────┼─────────────────┼───────────┼─────────────┼────────┤
│   0 │ -10.19572612870 │           │ 0.000118062 │  6.78s │
Geometry optimisation convergence (in atomic units)
┌─────┬─────────────────┬───────────┬─────────────┬────────┐
│   n │          Energy │ log10(ΔE) │  max(Force) │  Δtime │
├─────┼─────────────────┼───────────┼─────────────┼────────┤
│   0 │ -10.19572612873 │           │ 0.000118059 │  6.78s │
Geometry optimisation convergence (in atomic units)
┌─────┬─────────────────┬───────────┬─────────────┬────────┐
│   n │          Energy │ log10(ΔE) │  max(Force) │  Δtime │
├─────┼─────────────────┼───────────┼─────────────┼────────┤
│   0 │ -10.19572612872 │           │ 0.000118063 │  6.79s │
Geometry optimisation convergence (in atomic units)
┌─────┬─────────────────┬───────────┬─────────────┬────────┐
│   n │          Energy │ log10(ΔE) │  max(Force) │  Δtime │
├─────┼─────────────────┼───────────┼─────────────┼────────┤
│   0 │ -10.19572612871 │           │ 0.000118062 │  6.79s │
Geometry optimisation convergence (in atomic units)
┌─────┬─────────────────┬───────────┬─────────────┬────────┐
│   n │          Energy │ log10(ΔE) │  max(Force) │  Δtime │
├─────┼─────────────────┼───────────┼─────────────┼────────┤
│   0 │ -10.19572612871 │           │ 0.000118063 │  6.79s │
Geometry optimisation convergence (in atomic units)
┌─────┬─────────────────┬───────────┬─────────────┬────────┐
│   n │          Energy │ log10(ΔE) │  max(Force) │  Δtime │
├─────┼─────────────────┼───────────┼─────────────┼────────┤
│   0 │ -10.19572612870 │           │ 0.000118062 │  6.80s │
Geometry optimisation convergence (in atomic units)
┌─────┬─────────────────┬───────────┬─────────────┬────────┐
│   n │          Energy │ log10(ΔE) │  max(Force) │  Δtime │
├─────┼─────────────────┼───────────┼─────────────┼────────┤
│   0 │ -10.19572612870 │           │ 0.000118062 │  6.80s │
Geometry optimisation convergence (in atomic units)
┌─────┬─────────────────┬───────────┬─────────────┬────────┐
│   n │          Energy │ log10(ΔE) │  max(Force) │  Δtime │
├─────┼─────────────────┼───────────┼─────────────┼────────┤
│   0 │ -10.19572612870 │           │ 0.000118064 │  6.80s │
Geometry optimisation convergence (in atomic units)
┌─────┬─────────────────┬───────────┬─────────────┬────────┐
│   n │          Energy │ log10(ΔE) │  max(Force) │  Δtime │
├─────┼─────────────────┼───────────┼─────────────┼────────┤
│   0 │ -10.19572784034 │           │  2.80645e-6 │  6.81s │
Geometry optimisation convergence (in atomic units)
┌─────┬─────────────────┬───────────┬─────────────┬────────┐
│   n │          Energy │ log10(ΔE) │  max(Force) │  Δtime │
├─────┼─────────────────┼───────────┼─────────────┼────────┤
│   0 │ -10.19572784034 │           │  2.80383e-6 │  6.81s │
Geometry optimisation convergence (in atomic units)
┌─────┬─────────────────┬───────────┬─────────────┬────────┐
│   n │          Energy │ log10(ΔE) │  max(Force) │  Δtime │
├─────┼─────────────────┼───────────┼─────────────┼────────┤
│   0 │ -10.19572784034 │           │  2.80626e-6 │  6.81s │
Geometry optimisation convergence (in atomic units)
┌─────┬─────────────────┬───────────┬─────────────┬────────┐
│   n │          Energy │ log10(ΔE) │  max(Force) │  Δtime │
├─────┼─────────────────┼───────────┼─────────────┼────────┤
│   0 │ -10.19572784034 │           │  2.80726e-6 │  6.82s │
Geometry optimisation convergence (in atomic units)
┌─────┬─────────────────┬───────────┬─────────────┬────────┐
│   n │          Energy │ log10(ΔE) │  max(Force) │  Δtime │
├─────┼─────────────────┼───────────┼─────────────┼────────┤
│   0 │ -10.19572784034 │           │  2.80641e-6 │  6.82s │
Geometry optimisation convergence (in atomic units)
┌─────┬─────────────────┬───────────┬─────────────┬────────┐
│   n │          Energy │ log10(ΔE) │  max(Force) │  Δtime │
├─────┼─────────────────┼───────────┼─────────────┼────────┤
│   0 │ -10.19572784034 │           │  2.80662e-6 │  6.82s │
Geometry optimisation convergence (in atomic units)
┌─────┬─────────────────┬───────────┬─────────────┬────────┐
│   n │          Energy │ log10(ΔE) │  max(Force) │  Δtime │
├─────┼─────────────────┼───────────┼─────────────┼────────┤
│   0 │ -10.19572784034 │           │  2.80687e-6 │  6.83s │
Geometry optimisation convergence (in atomic units)
┌─────┬─────────────────┬───────────┬─────────────┬────────┐
│   n │          Energy │ log10(ΔE) │  max(Force) │  Δtime │
├─────┼─────────────────┼───────────┼─────────────┼────────┤
│   0 │ -10.19572784034 │           │  2.80606e-6 │  6.83s │
Geometry optimisation convergence (in atomic units)
┌─────┬─────────────────┬───────────┬─────────────┬────────┐
│   n │          Energy │ log10(ΔE) │  max(Force) │  Δtime │
├─────┼─────────────────┼───────────┼─────────────┼────────┤
│   0 │ -10.19572784034 │           │  2.80686e-6 │  6.83s │
Geometry optimisation convergence (in atomic units)
┌─────┬─────────────────┬───────────┬─────────────┬────────┐
│   n │          Energy │ log10(ΔE) │  max(Force) │  Δtime │
├─────┼─────────────────┼───────────┼─────────────┼────────┤
│   0 │ -10.19572784034 │           │   2.8062e-6 │  6.84s │
Geometry optimisation convergence (in atomic units)
┌─────┬─────────────────┬───────────┬─────────────┬────────┐
│   n │          Energy │ log10(ΔE) │  max(Force) │  Δtime │
├─────┼─────────────────┼───────────┼─────────────┼────────┤
│   0 │ -10.19572784034 │           │   2.8063e-6 │  6.84s │
Geometry optimisation convergence (in atomic units)
┌─────┬─────────────────┬───────────┬─────────────┬────────┐
│   n │          Energy │ log10(ΔE) │  max(Force) │  Δtime │
├─────┼─────────────────┼───────────┼─────────────┼────────┤
│   0 │ -10.19572784034 │           │  2.80551e-6 │  6.84s │
Geometry optimisation convergence (in atomic units)
┌─────┬─────────────────┬───────────┬─────────────┬────────┐
│   n │          Energy │ log10(ΔE) │  max(Force) │  Δtime │
├─────┼─────────────────┼───────────┼─────────────┼────────┤
│   0 │ -10.19572784034 │           │  2.80682e-6 │  6.85s │
Geometry optimisation convergence (in atomic units)
┌─────┬─────────────────┬───────────┬─────────────┬────────┐
│   n │          Energy │ log10(ΔE) │  max(Force) │  Δtime │
├─────┼─────────────────┼───────────┼─────────────┼────────┤
│   0 │ -10.19572784135 │           │  3.32987e-9 │  6.85s │
┌ Warning: NLopt failed to converge: FORCED_STOP
@ OptimizationNLopt ~/.julia/packages/OptimizationNLopt/YE3fr/src/OptimizationNLopt.jl:299

While in principle all first-order solvers supported by Optimization.jl can be employed, only few have been tested with this package so far. Given the complexity of the Optimization.jl ecosystem we expect that minor changes of our integration may be needed to make specific solvers work well. We welcome any PRs.