Skip to content

Commit

Permalink
Removed type restriction to AbstractSystem in functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
CedricTravelletti committed Jan 9, 2024
1 parent 0cfe514 commit a71dfc4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/atomsbase_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ function update_positions(system, positions::AbstractVector{<:AbstractVector{<:U
AbstractSystem(system; particles)
end

@doc raw"""
Creates a new system based on ``system`` but with atoms positions and lattice vectors
updated to the ones provided.
New atomic positions and lattice vectors should be provided in a ComponentArray
with components `positions` and `lattice` respectively, the lattice
vectors being given as a vector of vectors.
"""
function update_positions(system,
generalized_positions::ComponentVector{<:Unitful.Length})
particles = [Atom(atom; position) for (atom, position)
in zip(system, generalized_positions.positions)]
AbstractSystem(system; particles, generalized_positions.bounding_box)
end

@doc raw"""
Creates a new system based on ``system`` where the non clamped positions are
updated to the ones provided (in the order in which they appear in the system).
Expand Down
29 changes: 27 additions & 2 deletions src/optimization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export minimize_energy
are used.
"""
function Optimization.OptimizationFunction(system::AbstractSystem, calculator; kwargs...)
function Optimization.OptimizationFunction(system, calculator; kwargs...)
mask = not_clamped_mask(system) # mask is assumed not to change during optim.

f = function(x::AbstractVector{<:Real}, p)
Expand All @@ -35,10 +35,35 @@ function Optimization.OptimizationFunction(system::AbstractSystem, calculator; k
OptimizationFunction(f; grad=g!)
end

function minimize_energy(system::AbstractSystem, calculator; solver=Optim.LBFGS(), kwargs...)
function minimize_energy(system, calculator; solver=Optim.LBFGS(), kwargs...)
# Use current system parameters as starting positions.
x0 = austrip.(not_clamped_positions(system)) # Optim modifies x0 in-place, so need a mutable type.
f_opt = OptimizationFunction(system, calculator)
problem = OptimizationProblem(f_opt, x0, nothing) # Last argument needed in Optimization.jl.
solve(problem, solver; kwargs...)
end

# TODO: temporary name. This performs structural relaxation allowing atoms as well as unit cell
# vectors to move.
function OptimizationFunctionVariableCell(system, calculator; kwargs...)
mask = not_clamped_mask(system) # mask is assumed not to change during optim.

f = function(x::AbstractVector{<:Real}, p)
new_system = update_not_clamped_positions(system, x * u"bohr")
energy = AtomsCalculators.potential_energy(new_system, calculator; kwargs...)
austrip(energy)
end

g! = function(G::AbstractVector{<:Real}, x::AbstractVector{<:Real}, p)
new_system = update_not_clamped_positions(system, x * u"bohr")
energy = AtomsCalculators.potential_energy(new_system, calculator; kwargs...)

forces = AtomsCalculators.forces(new_system, calculator; kwargs...)
# Translate the forces vectors on each particle to a single gradient for the optimization parameter.
forces_concat = collect(Iterators.flatten(forces[mask]))

# NOTE: minus sign since forces are opposite to gradient.
G .= - austrip.(forces_concat)
end
OptimizationFunction(f; grad=g!)
end

0 comments on commit a71dfc4

Please # to comment.