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 zero, one, oneunit to MetricTensor #118

Merged
merged 7 commits into from
Oct 8, 2023
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
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ julia = "1.6"

[extras]
Mendeleev = "c116f080-063d-490a-9873-2b5b2cce4c34"
SymPy = "24249f21-da20-56a4-8eb1-6a02cf4ae2e6"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
UnitfulAtomic = "a7773ee8-282e-5fa2-be4e-bd808c38a91a"

[targets]
test = ["Mendeleev", "Test", "Unitful", "UnitfulAtomic"]
test = ["Mendeleev", "SymPy", "Test", "Unitful", "UnitfulAtomic"]
24 changes: 18 additions & 6 deletions src/metric.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using StaticArrays: SHermitianCompact
using StaticArrays: SHermitianCompact, SDiagonal

export MetricTensor, distance

Expand All @@ -11,10 +11,8 @@ MetricTensor(data::AbstractMatrix) = MetricTensor(SHermitianCompact{3}(data))

Generate a `MetricTensor` from the three basis vectors.
"""
function MetricTensor(𝐚::AbstractVector, 𝐛::AbstractVector, 𝐜::AbstractVector)
𝐚𝐛𝐜 = (𝐚, 𝐛, 𝐜)
return MetricTensor([dot(𝐱, 𝐲) for 𝐱 in 𝐚𝐛𝐜, 𝐲 in 𝐚𝐛𝐜])
end
MetricTensor(𝐚::AbstractVector, 𝐛::AbstractVector, 𝐜::AbstractVector) =
MetricTensor([dot(𝐱, 𝐲) for 𝐱 in (𝐚, 𝐛, 𝐜), 𝐲 in (𝐚, 𝐛, 𝐜)])
"""
MetricTensor(lattice::Lattice)

Expand Down Expand Up @@ -51,7 +49,7 @@ distance(𝐚::AbstractVector, g::MetricTensor, 𝐛::AbstractVector) = g(𝐚,

Construct a `Lattice` from a `MetricTensor`.
"""
Lattice(g::MetricTensor) = Lattice(latticeconstants(g))
Lattice(g::MetricTensor) = Lattice(latticeconstants(g)...)

"""
latticeconstants(g::MetricTensor)
Expand All @@ -65,6 +63,20 @@ function latticeconstants(g::MetricTensor)
return a, b, c, α, β, γ
end

# See https://github.com/JuliaLang/julia/blob/v1.10.0-beta1/stdlib/LinearAlgebra/src/uniformscaling.jl#L130-L131
Base.one(::Type{MetricTensor{T}}) where {T} =
MetricTensor(SDiagonal(one(T), one(T), one(T)))
Base.one(g::MetricTensor) = one(typeof(g))

# See https://github.com/JuliaLang/julia/blob/v1.10.0-beta1/stdlib/LinearAlgebra/src/uniformscaling.jl#L132-L133
Base.oneunit(::Type{MetricTensor{T}}) where {T} =
MetricTensor(SDiagonal(oneunit(T), oneunit(T), oneunit(T)))
Base.oneunit(g::MetricTensor) = oneunit(typeof(g))

# See https://github.com/JuliaLang/julia/blob/v1.10.0-beta1/stdlib/LinearAlgebra/src/uniformscaling.jl#L134-L135
Base.zero(::Type{MetricTensor{T}}) where {T} = MetricTensor(zeros(T, 3, 3))
Base.zero(lattice::MetricTensor) = zero(typeof(lattice))

Base.parent(g::MetricTensor) = g.data

Base.size(::MetricTensor) = (3, 3)
Expand Down
2 changes: 1 addition & 1 deletion src/volume.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ cellvolume(cell::Cell) = cellvolume(Lattice(cell))

Calculate the cell volume from a `MetricTensor`.
"""
cellvolume(g::MetricTensor) = sqrt(_det(g.data)) # `sqrt` is always positive!
cellvolume(g::MetricTensor) = sqrt(_det(parent(g))) # `sqrt` is always positive!

"""
crystaldensity(lattice::Lattice, atoms)
Expand Down
30 changes: 24 additions & 6 deletions test/metric.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
using LinearAlgebra: dot, norm, diagm
# using SymPy: symbols
using SymPy: symbols

@testset "Test consistency of constructors of `MetricTensor`" begin
lattice = Lattice(
4.59983732u"angstrom", 4.59983732u"angstrom", 2.95921356u"angstrom", 90, 90, 90
)
𝐚, 𝐛, 𝐜 = basisvectors(lattice)
@test MetricTensor(lattice) ==
MetricTensor(𝐚, 𝐛, 𝐜) ==
MetricTensor(
4.59983732u"angstrom", 4.59983732u"angstrom", 2.95921356u"angstrom", 90, 90, 90
)
@test Lattice(MetricTensor(lattice)) == lattice
@testset "Test `zero`, `one`, and `oneunit`" begin
@test one(MetricTensor(lattice)) == MetricTensor(one(lattice))
@test oneunit(MetricTensor(lattice)) == MetricTensor(oneunit(lattice))
@test zero(MetricTensor(lattice)) == MetricTensor(zero(lattice))
end
end

@testset "Test lengths in a hexagonal lattice" begin
g = MetricTensor(1, 1, 2, 90, 90, 120) # Primitive hexagonal
Expand All @@ -21,8 +39,8 @@ end
@test distance(a, g, b)^2 == 13 / 9
end

# @testset "Symbolic calculation" begin
# a, c = symbols("a, c", positive = true)
# @test MetricTensor(a, a, c, 90, 90, 120) ==
# MetricTensor([a^2 -0.5*a^2 0; -0.5*a^2 a^2 0; 0 0 c^2]) # Primitive hexagonal
# end
@testset "Test symbolic calculation" begin
a, c = symbols("a, c"; positive=true)
@test MetricTensor(a, a, c, 90, 90, 120) ==
MetricTensor([a^2 -0.5*a^2 0; -0.5*a^2 a^2 0; 0 0 c^2]) # Primitive hexagonal
end
Loading