Skip to content

Commit

Permalink
easy access to objective
Browse files Browse the repository at this point in the history
  • Loading branch information
ACEsuit committed May 26, 2024
1 parent aee53a8 commit de4a5ef
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/dof_management.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
3 changes: 1 addition & 2 deletions src/optim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit de4a5ef

Please sign in to comment.