|
1 | 1 | struct MinimizationResult |
2 | 2 | converged :: Bool |
3 | | - iterations :: Int |
| 3 | + niters :: Int |
| 4 | + energy :: Float64 |
4 | 5 | data :: Optim.OptimizationResults |
| 6 | + |
| 7 | + function MinimizationResult(niters, res) |
| 8 | + return new(Optim.converged(res), niters, Optim.minimum(res), res) |
| 9 | + end |
5 | 10 | end |
6 | 11 |
|
7 | 12 | function Base.show(io::IO, ::MIME"text/plain", res::MinimizationResult) |
8 | | - (; converged, iterations, data) = res |
| 13 | + (; converged, niters, data) = res |
9 | 14 | Δx = number_to_simple_string(Optim.x_abschange(data); digits=3) |
10 | 15 | g_res = number_to_simple_string(Optim.g_residual(data); digits=3) |
11 | 16 | if converged |
12 | | - print(io, "Converged in $iterations iterations") |
| 17 | + print(io, "Converged in $niters iterations") |
13 | 18 | else |
14 | | - print(io, "Non-converged after $iterations iterations: |Δx|=$Δx, |∂E/∂x|=$g_res") |
| 19 | + print(io, "Non-converged after $niters iterations: |Δx|=$Δx, |∂E/∂x|=$g_res") |
15 | 20 | end |
16 | 21 | end |
17 | 22 |
|
@@ -52,34 +57,52 @@ function Optim.project_tangent!(sm::SpinManifold, g, x) |
52 | 57 | end |
53 | 58 |
|
54 | 59 | function optimize_with_restarts(; calc_f, calc_g!, x, method, maxiters, options_args) |
55 | | - iters = 0 |
| 60 | + niters = 0 |
56 | 61 | while true |
57 | | - options = Optim.Options(; iterations=maxiters-iters, options_args...) |
| 62 | + options = Optim.Options(; iterations=maxiters-niters, options_args...) |
58 | 63 | res = Optim.optimize(calc_f, calc_g!, x, method, options) |
59 | 64 | x = Optim.minimizer(res) |
60 | | - iters += Optim.iterations(res) |
61 | | - if Optim.converged(res) || iters >= maxiters |
62 | | - return (res, iters) |
| 65 | + niters += Optim.iterations(res) |
| 66 | + if Optim.converged(res) || niters >= maxiters |
| 67 | + return (; res, niters) |
63 | 68 | end |
64 | 69 | end |
65 | 70 | end |
66 | 71 |
|
67 | 72 |
|
68 | 73 | """ |
69 | | - minimize_energy!(sys::System; maxiters=1000, kwargs...) |
70 | | -
|
71 | | -Optimizes the spin configuration in `sys` to minimize energy. A total of |
72 | | -`maxiters` iterations will be attempted. Any remaining `kwargs` will be included |
73 | | -in the `Options` constructor of the [Optim.jl |
74 | | -package](https://github.com/JuliaNLSolvers/Optim.jl) |
75 | | -
|
76 | | -Convergence status is stored in the field `ret.converged` of the return value |
77 | | -`ret`. Additional optimization statistics are stored in the field `ret.data`. |
| 74 | + minimize_energy!(sys::System; maxiters=1000, jitter=1e-8, kwargs...) |
| 75 | +
|
| 76 | +Optimizes the spin configuration in `sys` to find a local minimum of the energy. |
| 77 | +Large magnetic cells will be slower to converge; increase `maxiters` as needed. |
| 78 | +Prior to optimization, each spin will be randomly perturbed with the |
| 79 | +dimensionless `jitter` magnitude, which can be useful to break accidental |
| 80 | +symmetries. Any remaining `kwargs` will be included in the `Options` constructor |
| 81 | +of the [Optim.jl package](https://github.com/JuliaNLSolvers/Optim.jl). |
| 82 | +
|
| 83 | +Returns an object that can be inspected for optimization statistics. |
| 84 | +
|
| 85 | +!!! tip "Escaping local minima" |
| 86 | + To search for the global energy minimum, a simple strategy is to repeatedly |
| 87 | + call [`randomize_spins!`](@ref) and then `minimize_energy!`. The example |
| 88 | + below keeps the minimum energy spin state found within 100 runs. |
| 89 | +
|
| 90 | + ```julia |
| 91 | + tmp_sys = clone_system(sys) |
| 92 | + for i in 1:100 |
| 93 | + randomize_spins!(sys) |
| 94 | + minimize_energy!(sys) |
| 95 | + if energy(sys) < energy(tmp_sys) |
| 96 | + copy_spins!(tmp_sys, sys) |
| 97 | + end |
| 98 | + end |
| 99 | + copy_spins!(sys, tmp_sys) |
| 100 | + ``` |
78 | 101 | """ |
79 | | -function minimize_energy!(sys::System{N}; maxiters=1000, δ=1e-8, kwargs...) where N |
| 102 | +function minimize_energy!(sys::System{N}; maxiters=1000, jitter=1e-8, kwargs...) where N |
80 | 103 | # Small perturbation to destabilize an accidental stationary point (local |
81 | 104 | # maximum or saddle). |
82 | | - perturb_spins!(sys, δ) |
| 105 | + perturb_spins!(sys, jitter) |
83 | 106 |
|
84 | 107 | # Optimization variables are normalized spins or coherent states. In case of |
85 | 108 | # a vacancy, use an arbitrary representative on the sphere: [1, 1, …] / √N. |
@@ -132,10 +155,10 @@ function minimize_energy!(sys::System{N}; maxiters=1000, δ=1e-8, kwargs...) whe |
132 | 155 | manifold = SpinManifold(iszero(N) ? 3 : N, length(eachsite(sys))) |
133 | 156 | method = Optim.ConjugateGradient(; alphaguess=LineSearches.InitialHagerZhang(; αmax=10.0), manifold) |
134 | 157 | options_args = (; g_abstol, x_abstol, x_reltol=NaN, f_reltol=NaN, f_abstol=NaN, kwargs...) |
135 | | - (res, iters) = optimize_with_restarts(; calc_f, calc_g!, x, method, maxiters, options_args) |
| 158 | + (; res, niters) = optimize_with_restarts(; calc_f, calc_g!, x, method, maxiters, options_args) |
136 | 159 |
|
137 | 160 | load_spins!(Optim.minimizer(res)) |
138 | | - mr = MinimizationResult(Optim.converged(res), iters, res) |
| 161 | + mr = MinimizationResult(niters, res) |
139 | 162 | if !mr.converged |
140 | 163 | @warn repr("text/plain", mr) |
141 | 164 | end |
|
0 commit comments