Skip to content

Commit fe14332

Browse files
committed
Update Optim to 2
1 parent 31a4b29 commit fe14332

5 files changed

Lines changed: 23 additions & 47 deletions

File tree

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ AtomsBuilder = "0.2.2"
2929
AtomsCalculators = "0.2.3"
3030
DocStringExtensions = "0.9"
3131
LineSearches = "7"
32-
Optim = "1.11.0"
32+
Optim = "2"
3333
Optimization = "3, 4"
3434
PrettyTables = "3"
3535
StaticArrays = "1"

src/dof_management.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ In addition set at most one of the kwargs:
2121
On call to the constructor, `DofManager` stores positions and cell
2222
`X0, C0`, dofs are understood *relative* to this initial configuration.
2323
`get_dofs(sys, dm::DofManager)` returns a vector that represents the
24-
non-dimensional displacement and a deformation matrix `(U, F)`. The new configuration extracted from a dof vector
25-
is understood as
24+
non-dimensional displacement and a deformation matrix `(U, F)`.
25+
The new configuration extracted from a dof vector is understood as
2626
* The new cell: `C = F * C0`
2727
* The new positions: `𝐫[i] = F * (X0[i] + U[i] * r0)`
2828
One aspect of this definition is that clamped atom positions still change via

src/minimize_energy.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ function eval_objective_gradient!(G, prob::GeoOptProblem, ps, x)
4545
objective = res.energy_unitless
4646
energy = res.energy
4747

48-
gradnorm = nothing
49-
forces = nothing
50-
virial = nothing
48+
grad = nothing
49+
forces = nothing
50+
virial = nothing
5151
if !isnothing(G)
5252
res = eval_gradient(prob.system, prob.calculator, prob.dofmgr, x, ps, res.state)
53-
gradnorm = maximum(abs, res.grad)
53+
grad = res.grad
5454
haskey(res, :forces) && (forces = res.forces)
5555
haskey(res, :virial) && (virial = res.virial)
5656
copy!(G, res.grad)
@@ -62,7 +62,7 @@ function eval_objective_gradient!(G, prob::GeoOptProblem, ps, x)
6262
if energy min_energy
6363
geoopt_state.calc_state = res.state
6464
end
65-
push!(geoopt_state.cache_evaluations, (; energy, forces, virial, objective, gradnorm))
65+
push!(geoopt_state.cache_evaluations, (; energy, forces, virial, objective, grad))
6666

6767
objective
6868
end

src/optim.jl

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,28 @@ function solve_problem(prob::GeoOptProblem, solver::Optim.AbstractOptimizer, cvg
4242
end
4343

4444
geoopt_state = prob.geoopt_state
45-
inner_callback = function(ts)
45+
inner_callback = function(optim_state)
4646
cache_evaluations = geoopt_state.cache_evaluations
47-
48-
geoopt_state.n_iter = ts.iteration
47+
n_iter = optim_state.pseudo_iteration
48+
geoopt_state.n_iter = n_iter
4949
if isempty(cache_evaluations)
5050
# Find out if we already added the current state (if optim cannot
5151
# make progress it keeps printing iterations, but does not run further
5252
# function evaluations ... in this case we have no new forces and virials).
5353
# Also it sometimes does an extra call to the callback even though
5454
# convergence has already been flagged.
55-
tol = 10eps(typeof(ts.value))
56-
is_match = abs(austrip(geoopt_state.history_energy[end]) - ts.value) < tol
55+
tol = 10eps(typeof(optim_state.f_x))
56+
is_match = abs(austrip(geoopt_state.history_energy[end]) - optim_state.f_x) < tol
5757
if !geoopt_state.converged && !is_match
58-
@warn "Discarding optimisation step of iteration $(ts.iteration)"
58+
@warn "Discarding optimisation step of iteration $(n_iter)"
5959
end
6060
else
6161
# Find position in the cache matching Optim's current state
6262
i_match = findlast(cache_evaluations) do eval
63-
isnothing(eval.gradnorm) && return false
64-
tol = 10eps(typeof(ts.value))
65-
( abs(eval.objective - ts.value) < tol
66-
&& abs(eval.gradnorm - ts.g_norm) < tol)
63+
isnothing(eval.grad) && return false
64+
tol = 10eps(typeof(optim_state.f_x))
65+
( abs(eval.objective - optim_state.f_x) < tol
66+
&& maximum(abs, eval.grad - optim_state.g_x) < tol)
6767
end
6868
i_match = @something i_match length(cache_evaluations)
6969

@@ -82,7 +82,7 @@ function solve_problem(prob::GeoOptProblem, solver::Optim.AbstractOptimizer, cvg
8282
end
8383

8484
# Callback and possible abortion
85-
halt = callback(ts, geoopt_state)
85+
halt = callback(optim_state, geoopt_state)
8686
halt && return true
8787

8888
geoopt_state.converged
@@ -95,8 +95,8 @@ function solve_problem(prob::GeoOptProblem, solver::Optim.AbstractOptimizer, cvg
9595
allow_f_increases=true,
9696
successive_f_tol=2,
9797
callback=inner_callback,
98-
x_abstol=-1, f_abstol=-1, g_tol=10eps(T),
99-
x_reltol=-1, f_reltol=-1,
98+
x_abstol=NaN, f_abstol=NaN, g_tol=10eps(T),
99+
x_reltol=NaN, f_reltol=NaN,
100100
iterations=maxiters,
101101
time_limit=maxtime,
102102
kwargs...
@@ -108,6 +108,7 @@ end
108108

109109
function solve_problem(prob, solver::Optim.ZerothOrderOptimizer, cvg;
110110
callback, maxiters, maxtime, kwargs...)
111-
# TODO Supporting this needs more fiddeling with the callbacks and convergence checks
111+
# TODO: Supporting this needs more fiddeling with the callbacks and convergence checks
112+
# and it's generally not very useful as forces / stresses are usually available
112113
throw(ArgumentError("Zeroth-order optimizers are currently not supported."))
113114
end

test/nlopt.jl

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)