From a452029e7df549bea8d02cc34ad33b4331c87267 Mon Sep 17 00:00:00 2001 From: singularitti Date: Sun, 8 Oct 2023 02:47:01 -0400 Subject: [PATCH 1/3] Add `convert` between `Lattice`s --- src/lattice.jl | 4 ++++ src/reciprocal.jl | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/src/lattice.jl b/src/lattice.jl index 857c40c..5211737 100644 --- a/src/lattice.jl +++ b/src/lattice.jl @@ -163,3 +163,7 @@ Base.:+(x::Number, lattice::Lattice) = lattice + x Base.:-(lattice::Lattice) = -one(eltype(lattice)) * lattice Base.:-(lattice::Lattice, x::Number) = Lattice(parent(lattice) .- x) Base.:-(x::Number, lattice::Lattice) = -lattice + x + +Base.convert(::Type{Lattice{T}}, lattice::Lattice{T}) where {T} = lattice +Base.convert(::Type{Lattice{T}}, lattice::Lattice{S}) where {S,T} = + Lattice(convert(Matrix{T}, parent(lattice))) diff --git a/src/reciprocal.jl b/src/reciprocal.jl index ba3b875..278ce13 100644 --- a/src/reciprocal.jl +++ b/src/reciprocal.jl @@ -85,3 +85,8 @@ Base.:/(lattice::ReciprocalLattice, x::Number) = ReciprocalLattice(parent(lattic Base.:+(lattice::ReciprocalLattice) = lattice Base.:-(lattice::ReciprocalLattice) = -one(eltype(lattice)) * lattice + +Base.convert(::Type{ReciprocalLattice{T}}, lattice::ReciprocalLattice{T}) where {T} = + lattice +Base.convert(::Type{ReciprocalLattice{T}}, lattice::ReciprocalLattice{S}) where {S,T} = + ReciprocalLattice(convert(Matrix{T}, parent(lattice))) From dc1a34fe93558b97f05843275bc2dece355f1113 Mon Sep 17 00:00:00 2001 From: singularitti Date: Sun, 8 Oct 2023 03:12:07 -0400 Subject: [PATCH 2/3] Add Unitful.jl to `weakdeps`, `UnitfulExt` to `extensions` --- Project.toml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Project.toml b/Project.toml index 3cc8ad7..0cbf1fc 100644 --- a/Project.toml +++ b/Project.toml @@ -8,6 +8,12 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" StructEquality = "6ec83bb0-ed9f-11e9-3b4c-2b04cb4e219c" +[weakdeps] +Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" + +[extensions] +UnitfulExt = "Unitful" + [compat] StaticArrays = "1" StructEquality = "1, 2" From e650e976ba33fa6ac1343007be36befb9a30ae4c Mon Sep 17 00:00:00 2001 From: singularitti Date: Sun, 8 Oct 2023 03:14:31 -0400 Subject: [PATCH 3/3] Extend `Base.convert`, `uconvert`, `ustrip` for `ReciprocalLattice` & `Lattice` in ext/UnitfulExt.jl --- ext/UnitfulExt.jl | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 ext/UnitfulExt.jl diff --git a/ext/UnitfulExt.jl b/ext/UnitfulExt.jl new file mode 100644 index 0000000..8200e1e --- /dev/null +++ b/ext/UnitfulExt.jl @@ -0,0 +1,40 @@ +module UnitfulExt + +using CrystallographyCore: Lattice, ReciprocalLattice +using Unitful: Quantity, DimensionError, Units, dimension + +import Unitful: uconvert, ustrip + +# See https://github.com/PainterQubits/Unitful.jl/blob/v1.17.0/src/conversion.jl#L102-L111 +Base.convert( + ::Type{Lattice{Quantity{T,D,U}}}, lattice::Lattice{Quantity{T,D,U}} +) where {T,D,U} = lattice +Base.convert( + ::Type{ReciprocalLattice{Quantity{T,D,U}}}, lattice::ReciprocalLattice{Quantity{T,D,U}} +) where {T,D,U} = lattice +function Base.convert(::Type{Lattice{Quantity{T,D,U}}}, lattice::Lattice) where {T,D,U} + if dimension(eltype(lattice)) == D + return Lattice(map(Base.Fix1(convert, Quantity{T,D,U}), parent(lattice))) + else + throw(DimensionError(U(), first(lattice))) + end +end +function Base.convert( + ::Type{ReciprocalLattice{Quantity{T,D,U}}}, lattice::ReciprocalLattice +) where {T,D,U} + if dimension(eltype(lattice)) == D + return ReciprocalLattice(map(Base.Fix1(convert, Quantity{T,D,U}), parent(lattice))) + else + throw(DimensionError(U(), first(lattice))) + end +end + +# See https://github.com/PainterQubits/Unitful.jl/blob/v1.17.0/src/conversion.jl#L66-L74 +uconvert(u::Units, lattice::Lattice) = Lattice(map(Base.Fix1(uconvert, u), parent(lattice))) +uconvert(u::Units, lattice::ReciprocalLattice) = + ReciprocalLattice(map(Base.Fix1(uconvert, u), parent(lattice))) + +ustrip(lattice::Lattice) = Lattice(map(ustrip, parent(lattice))) +ustrip(lattice::ReciprocalLattice) = ReciprocalLattice(map(ustrip, parent(lattice))) + +end