Skip to content

Commit

Permalink
Fix dihedral definition, add pbc_shortest_vectors method
Browse files Browse the repository at this point in the history
- Dihedral definition was incorrect and giving negative results for pbc.
  Correct it. pbc_shortest_vectors indices need to be inverted.
- Update test correcting this. Now pbc dihedral gives correct with
  respect to ASE, i.e, ASE gives 300, here we get -60, probably ASE range
  is 0 to 360. Here it is (-180, 180).
- Add method for pbc_shortes_vectors with system and index of atoms.
  • Loading branch information
rashidrafeek committed Oct 31, 2024
1 parent a404929 commit b18954a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
10 changes: 5 additions & 5 deletions src/getters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,13 @@ function dihedral(
frpos2 = pos2' * icell
frpos3 = pos3' * icell
frpos4 = pos4' * icell
vec1 = pbc_shortest_vectors(cell, frpos3, frpos4)[1,1,:] # Axis
vec1 = pbc_shortest_vectors(cell, frpos1, frpos2)[1,1,:] # Axis
vec2 = pbc_shortest_vectors(cell, frpos2, frpos3)[1,1,:]
vec3 = pbc_shortest_vectors(cell, frpos1, frpos2)[1,1,:]
vec3 = pbc_shortest_vectors(cell, frpos3, frpos4)[1,1,:]
else
vec1 = pos3 - pos4
vec2 = pos2 - pos3
vec3 = pos1 - pos2
vec1 = pos2 - pos1
vec2 = pos3 - pos2
vec3 = pos4 - pos3
end
p23 = vec2 × vec3
p12 = vec1 × vec2
Expand Down
13 changes: 13 additions & 0 deletions src/pbc_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ function dot_2d_mod(a,b)
end

"""
pbc_shortest_vectors(
system::AbstractSystem, at1::Int, at2::Int,
return_dists::Val{RD}=Val(false), return_vects::Val{RV}=Val(true)
) where {RD, RV}
pbc_shortest_vectors(
system::AbstractSystem, pos1::T, pos2::T,
return_dists::Val{RD}=Val(false), return_vects::Val{RV}=Val(true)
Expand All @@ -32,6 +36,15 @@ end
Obtain the shortest vectors between position vectors, `pos1` and `pos2`,
taking into account PBC of the given `system`.
"""
function pbc_shortest_vectors(
system::AbstractSystem, at1::Int, at2::Int,
return_dists::Val{RD}=Val(false), return_vects::Val{RV}=Val(true)
) where {RD, RV}
pos1 = position(system, at1)
pos2 = position(system, at2)

return pbc_shortest_vectors(system, pos1, pos2, return_dists, return_vects)[1,1,:]
end
function pbc_shortest_vectors(
system::AbstractSystem, pos1::T, pos2::U,
return_dists::Val{RD}=Val(false), return_vects::Val{RV}=Val(true)
Expand Down
3 changes: 1 addition & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ using AtomsIO
@test angle(si, 3, 2, 5; pbc=false) 58.51784589u"°"
end

# This needs to be verified for pbc=true. Seems inconsistent with ASE
@testset "dihedral" begin
@test dihedral(si, 1,5,2,6; pbc=false) 0.0u"°" atol=1e-8u"°"
@test dihedral(si, 5,6,1,4) 60.0u"°" atol=1e-8u"°"
@test dihedral(si, 5,6,1,4) -60.0u"°" atol=1e-8u"°"
@test dihedral(si, 5,6,1,4; pbc=false) 30.0u"°" atol=1e-8u"°"
end

Expand Down

0 comments on commit b18954a

Please sign in to comment.