Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add energy_and_force to API #20

Merged
merged 4 commits into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/InteratomicPotentials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ include("PotentialTypes/types.jl")
export NeighborList, neighborlist

# Export Potentials
export EmpiricalPotential, LennardJones, BornMayer, Coulomb, ZBL
export EmpiricalPotential, LennardJones, BornMayer, Coulomb, ZBL
export BasisPotential, BasisParameters
export SNAP, SNAPParams, get_num_snap_coeffs # Export SNAP

# Export energies, forces, virial evaluations
export potential_energy, force, virial, virial_stress
export energy_and_force, potential_energy, force, virial, virial_stress
export grad_potential_energy, grad_force, grad_virial, grad_virial_stress

# Export Basis set evaluations
Expand Down
7 changes: 7 additions & 0 deletions src/PotentialTypes/ArbitraryPotential/arbitrary_potential.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
################################################################################
# InteratomicPotentials API default generic implmentations
################################################################################

potential_energy(A::AbstractSystem, p::ArbitraryPotential) = energy_and_force(A, p).e
force(A::AbstractSystem, p::ArbitraryPotential) = energy_and_force(A, p).f
virial(A::AbstractSystem, p::ArbitraryPotential) = sum(virial_stress(A, p)[1:3])
12 changes: 4 additions & 8 deletions src/PotentialTypes/BasisPotentials/SNAP/snap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ struct SNAP <: BasisPotential
basis_params :: SNAPParams
end



function evaluate_basis(A::AbstractSystem, snap::SNAPParams)
# Produce NeighborList
nnlist = neighborlist(A, snap)
Expand All @@ -29,7 +27,7 @@ function evaluate_basis(A::AbstractSystem, snap::SNAPParams)
B += runtime_arrays.blist
end
return B
end
end

function evaluate_basis_d(A::AbstractSystem, snap::SNAPParams)
number_of_particles = length(A.particles)
Expand Down Expand Up @@ -73,7 +71,7 @@ function evaluate_basis_d(A::AbstractSystem, snap::SNAPParams)
end
end
return dB
end
end

function evaluate_basis_v(A::AbstractSystem, snap::SNAPParams)
number_of_particles = length(A.particles)
Expand Down Expand Up @@ -133,7 +131,7 @@ function evaluate_basis_v(A::AbstractSystem, snap::SNAPParams)
end
end
return sum(W)
end
end

function evaluate_full(A::AbstractSystem, snap::SNAPParams)
number_of_particles = length(A.particles)
Expand Down Expand Up @@ -195,6 +193,4 @@ function evaluate_full(A::AbstractSystem, snap::SNAPParams)
end
end
return B, dB, W
end


end
20 changes: 12 additions & 8 deletions src/PotentialTypes/BasisPotentials/basis_potentials.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
# Include types

abstract type BasisParameters end


#include SNAP
include("SNAP/snap.jl")

################################################################################
# InteratomicPotentials API implmentations for basis potentials
################################################################################

#include Energies, Forces, Stress
include("potential_energy.jl")
include("force.jl")
include("virials.jl")


function energy_and_force(A::AbstractSystem, p::BasisPotential)
B, dB, _ = evaluate_full(A, p.basis_params)
e = sum(B) ⋅ p.coefficients
f = [SVector{3}(di' * p.coefficients) for di in dB]
(; e, f)
end

function virial_stress(A::AbstractSystem, p::BasisPotential)
sum(SVector{6}(di ⋅ p.coefficients) for di in evaluate_basis_v(A, p.basis_params))
end
15 changes: 0 additions & 15 deletions src/PotentialTypes/BasisPotentials/force.jl

This file was deleted.

3 changes: 0 additions & 3 deletions src/PotentialTypes/BasisPotentials/potential_energy.jl

This file was deleted.

19 changes: 0 additions & 19 deletions src/PotentialTypes/BasisPotentials/virials.jl

This file was deleted.

44 changes: 38 additions & 6 deletions src/PotentialTypes/EmpiricalPotentials/empirical_potentials.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,44 @@

############################### Types of Potentials ################################################
################################################################################
# Types of Potentials
################################################################################

include("lj.jl")
include("bm.jl")
include("coulomb.jl")
include("zbl.jl")

############################### Methods on Potentials ################################################
include("potential_energy.jl")
include("force.jl")
include("virials.jl")
################################################################################
# InteratomicPotentials API implmentations for emperical potentials
################################################################################

function energy_and_force(A::AbstractSystem, p::EmpiricalPotential)
nnlist = neighborlist(A, p.rcutoff)

e = 0.0
f = fill(SVector{3}(zeros(3)), length(A))
for ii in 1:length(A)
for (jj, r, R) in zip(nnlist.j[ii], nnlist.r[ii], nnlist.R[ii])
e += potential_energy(R, p)
fo = force(R, r, p)
f[ii] = f[ii] + fo
f[jj] = f[jj] - fo
end
end
(; e, f)
end

force(r::SVector{3,<:AbstractFloat}, p::EmpiricalPotential) = force(norm(r), r, p)

function virial_stress(A::AbstractSystem, p::EmpiricalPotential)
nnlist = neighborlist(A, p.rcutoff)

v = SVector{6}(zeros(6))
for ii in 1:length(A)
for (r, R) in zip(nnlist.r[ii], nnlist.R[ii])
fo = force(R, r, p)
vi = r * fo'
v = v + [vi[1, 1], vi[2, 2], vi[3, 3], vi[3, 2], vi[3, 1], vi[2, 1]]
end
end
v
end
20 changes: 0 additions & 20 deletions src/PotentialTypes/EmpiricalPotentials/force.jl

This file was deleted.

11 changes: 0 additions & 11 deletions src/PotentialTypes/EmpiricalPotentials/potential_energy.jl

This file was deleted.

20 changes: 0 additions & 20 deletions src/PotentialTypes/EmpiricalPotentials/virials.jl

This file was deleted.

22 changes: 16 additions & 6 deletions src/PotentialTypes/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,26 @@
################################################################################

abstract type ArbitraryPotential end
abstract type EmpiricalPotential <:ArbitraryPotential end
abstract type EmpiricalPotential <: ArbitraryPotential end
abstract type BasisPotential <: ArbitraryPotential end
abstract type MixedPotential <:ArbitraryPotential end
abstract type MixedPotential <: ArbitraryPotential end

############################### Empirical Potentials ################################################
include("EmpiricalPotentials/empirical_potentials.jl")
################################################################################
# InteratomicPotentials API default generic implmentations
################################################################################
include("ArbitraryPotential/arbitrary_potential.jl")

################################################################################
# Empirical Potentials
################################################################################
include("EmpiricalPotentials/empirical_potentials.jl")

################################ BasisPotentials ##############################################################
################################################################################
# BasisPotentials
################################################################################
include("BasisPotentials/basis_potentials.jl")

################################ GaN ###############################################################
################################################################################
# GaN
################################################################################
# include("GaN/gan.jl")