Skip to content

Commit

Permalink
Update distance_matrix interface
Browse files Browse the repository at this point in the history
  • Loading branch information
zhubonan committed Dec 13, 2022
1 parent 2e4071d commit 67c0ee0
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions src/cell.jl
Original file line number Diff line number Diff line change
Expand Up @@ -404,21 +404,52 @@ end
Base.length(cell::Cell) = natoms(cell)
Base.getindex(cell::Cell, i::Int) = Site(@view(cell.positions[:, i]), i, cell.symbols[i])


function distance_matrix(cell::Cell;mic=true)
if mic == true
_distance_matrix_mic(cell)
else
_distance_matrix_no_mic(cell)
end
end


"""
_distance_matrix_no_mic(structure::Cell)
Compute the distnace matrix without minimum image convention, e.g. the PBC is not respected.
"""
function _distance_matrix_no_mic(cell::Cell)
# Compute the naive pair-wise vectors
nn = nions(cell)
pos = sposarray(cell)
dmat = zeros(nn, nn)
for i in 1:nn
for j in i+1:nn
d = norm(pos[j] - pos[i])
dmat[i, j] = d
dmat[j, i] = d
end
end
dmat
end


"""
distance_matrix(structure::Cell; mic=true)
_distance_matrix_mic(cell::Cell)
Compute the distance matrix for the given structure, using the minimum image convention (or not).
Note the returned matrix does not expand the cell. The distance matrix cannot be safety used for obtaining the minimum separations.
For example, a structure with a single atom would be a distance matrix containing only zero.
"""
function distance_matrix(structure::Cell)
function _distance_matrix_mic(cell::Cell)

# Compute the naive pair-wise vectors
nn = nions(structure)
nn = nions(cell)
vecs = zeros(3, nn*nn)
pos = sposarray(structure)
pos = sposarray(cell)
dmat = zeros(nn, nn)
ivec = 0
for i in 1:nn
Expand All @@ -428,7 +459,7 @@ function distance_matrix(structure::Cell)
end
end
# Apply minimum image conventions
_, dmic = mic(lattice(structure), @view(vecs[:, 1:ivec]))
_, dmic = mic(lattice(cell), @view(vecs[:, 1:ivec]))
# Unpack computed distances
ivec = 0
for i in 1:nn
Expand Down

2 comments on commit 67c0ee0

@zhubonan
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/74038

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.3.1 -m "<description of version>" 67c0ee0570c65f391fd4a30f153a9445b1e885ef
git push origin v0.3.1

Please sign in to comment.