From de4a5ef19b7d129a60439213d18ead56c5ab17ba Mon Sep 17 00:00:00 2001 From: ACEsuit Date: Sun, 26 May 2024 14:48:24 -0700 Subject: [PATCH] easy access to objective --- src/dof_management.jl | 14 ++++++++++++++ src/optim.jl | 3 +-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/dof_management.jl b/src/dof_management.jl index 576c242..13917fc 100644 --- a/src/dof_management.jl +++ b/src/dof_management.jl @@ -205,6 +205,20 @@ end using AtomsCalculators: potential_energy, energy_forces_virial, forces, virial +# untested +function get_obj_fg(sys, calc, dofmgr) + f = x -> energy_dofs(sys, calc, dofmgr, x) + g = x -> gradient_dofs(sys, calc, dofmgr, x) + return f, g +end + +# used in the optim interface +function get_obj_fg!(sys, calc, dofmgr) + f = x -> energy_dofs(sys, calc, dofmgr, x) + g = (g, x) -> copyto!(g, gradient_dofs(sys, calc, dofmgr, x)) + return f, g +end + function energy_dofs(sys, calc, dofmgr, x) set_dofs!(sys, dofmgr, x) return ustrip(potential_energy(sys, calc)) diff --git a/src/optim.jl b/src/optim.jl index 6ddab14..001810b 100644 --- a/src/optim.jl +++ b/src/optim.jl @@ -68,8 +68,7 @@ function minimise(sys, calc; # create an objective function dofmgr = DofManager(sys1; variablecell = variablecell) x0 = get_dofs(sys, dofmgr) - obj_f = x -> energy_dofs(sys, calc, dofmgr, x) - obj_g! = (g, x) -> copyto!(g, gradient_dofs(sys, calc, dofmgr, x)) + obj_f, obj_g! = get_obj_fg!(sys, calc, dofmgr) @assert precond ∈ [nothing, I] precond = I