Skip to content

Commit

Permalink
Merge pull request #119 from MineralsCloud:distance
Browse files Browse the repository at this point in the history
Bump `compat` for CrystallographyCore.jl
  • Loading branch information
singularitti authored Oct 8, 2023
2 parents e7c0114 + 187499d commit 7bafac6
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ PeriodicTableExt = "PeriodicTable"

[compat]
AnonymousEnums = "0.1"
CrystallographyCore = "0.1, 0.2, 0.3, 0.4"
CrystallographyCore = "0.4.2"
Mendeleev = "0.2, 0.3, 1"
Reexport = "1"
StaticArrays = "0.8.3, 0.9, 0.10, 0.11, 0.12, 1"
Expand Down
1 change: 1 addition & 0 deletions docs/src/lib/public.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ reciprocal

```@docs
MetricTensor
lengthof
distance
```

Expand Down
14 changes: 9 additions & 5 deletions src/metric.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using StaticArrays: SHermitianCompact, SDiagonal

export MetricTensor, distance
export MetricTensor, lengthof, distance

struct MetricTensor{T} <: AbstractMatrix{T}
data::SHermitianCompact{3,T,6}
Expand Down Expand Up @@ -34,15 +34,19 @@ function MetricTensor(a, b, c, α, β, γ)
return MetricTensor(SHermitianCompact(SVector(a^2, g₁₂, g₁₃, b^2, g₂₃, c^2)))
end

(g::MetricTensor)(𝐚::AbstractVector) = sqrt(dot(𝐚, g, 𝐚))
(g::MetricTensor)(𝐚::AbstractVector, 𝐛::AbstractVector) = g(𝐚 - 𝐛)
"""
lengthof(𝐚::ReducedCoordinates, g::MetricTensor)
Get the length of coordinates `𝐚`.
"""
lengthof(𝐚::ReducedCoordinates, g::MetricTensor) = sqrt(dot(𝐚, g, 𝐚))

"""
distance(𝐚::AbstractVector, g::MetricTensor, 𝐛::AbstractVector)
distance(𝐚::ReducedCoordinates, g::MetricTensor, 𝐛::ReducedCoordinates)
Get the distance between two coordinates using a `MetricTensor`.
"""
distance(𝐚::AbstractVector, g::MetricTensor, 𝐛::AbstractVector) = g(𝐚, 𝐛)
distance(𝐚::ReducedCoordinates, g::MetricTensor, 𝐛::ReducedCoordinates) = lengthof(𝐚 - 𝐛, g)

"""
Lattice(g::MetricTensor)
Expand Down
9 changes: 4 additions & 5 deletions test/metric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@ end
0.0 sqrt(3)/2 0.0
0.0 0.0 2.0
]))
a = [1, 2, 1]
@test dot(a, g, a) 7
@test g([1, 2, 1])^2 7
𝐚 = ReducedCoordinates(1, 2, 1)
@test lengthof(𝐚, g)^2 7
end

@testset "Test distance between atoms in a hexagonal lattice" begin
g = MetricTensor(1, 1, 2, 90, 90, 120) # Primitive hexagonal
a = [1, 1, 1]
b = [1//3, 1//3, 1//2]
a = ReducedCoordinates(1, 1, 1)
b = ReducedCoordinates(1//3, 1//3, 1//2)
@test distance(a, g, b)^2 == 13 / 9
end

Expand Down

0 comments on commit 7bafac6

Please sign in to comment.