Skip to content

Unitful compatibility #60

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

Merged
merged 3 commits into from
May 14, 2020
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 @@ -13,8 +13,9 @@ StaticArrays = "0.11,0.12"
julia = "1"

[extras]
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test", "ForwardDiff"]
test = ["Test", "ForwardDiff", "Unitful"]
49 changes: 35 additions & 14 deletions src/coordinatesystems.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@
### 2D Coordinate systems ###
#############################
"""
`Polar{T}(r::T, θ::T)` - 2D polar coordinates
`Polar{T,A}(r::T, θ::A)` - 2D polar coordinates
"""
struct Polar{T}
struct Polar{T,A}
r::T
θ::T
θ::A

Polar{T, A}(r, θ) where {T, A} = new(r, θ)
end

function Polar(r, θ)
r2, θ2 = promote(r, θ)

return Polar{typeof(r2), typeof(θ2)}(r2, θ2)
end

Base.show(io::IO, x::Polar) = print(io, "Polar(r=$(x.r), θ=$(x.θ) rad)")
Base.isapprox(p1::Polar, p2::Polar; kwargs...) = isapprox(p1.r, p2.r; kwargs...) && isapprox(p1.θ, p2.θ; kwargs...)
Base.eltype(::Polar{T}) where {T} = T
Base.eltype(::Type{Polar{T}}) where {T} = T

"`PolarFromCartesian()` - transformation from `AbstractVector` of length 2 to `Polar` type"
struct PolarFromCartesian <: Transformation; end
Expand Down Expand Up @@ -67,28 +74,42 @@ Base.convert(::Type{Polar}, v::AbstractVector) = PolarFromCartesian()(v)
"""
Spherical(r, θ, ϕ) - 3D spherical coordinates
"""
struct Spherical{T}
struct Spherical{T,A}
r::T
θ::T
ϕ::T
θ::A
ϕ::A

Spherical{T, A}(r, θ, ϕ) where {T, A} = new(r, θ, ϕ)
end

function Spherical(r, θ, ϕ)
r2, θ2, ϕ2 = promote(r, θ, ϕ)

return Spherical{typeof(r2), typeof(θ2)}(r2, θ2, ϕ2)
end

Base.show(io::IO, x::Spherical) = print(io, "Spherical(r=$(x.r), θ=$(x.θ) rad, ϕ=$(x.ϕ) rad)")
Base.isapprox(p1::Spherical, p2::Spherical; kwargs...) = isapprox(p1.r, p2.r; kwargs...) && isapprox(p1.θ, p2.θ; kwargs...) && isapprox(p1.ϕ, p2.ϕ; kwargs...)
Base.eltype(::Spherical{T}) where {T} = T
Base.eltype(::Type{Spherical{T}}) where {T} = T

"""
Cylindrical(r, θ, z) - 3D cylindrical coordinates
"""
struct Cylindrical{T}
struct Cylindrical{T,A}
r::T
θ::T
θ::A
z::T

Cylindrical{T, A}(r, θ, z) where {T, A} = new(r, θ, z)
end

function Cylindrical(r, θ, z)
r2, θ2, z2 = promote(r, θ, z)

return Cylindrical{typeof(r2), typeof(θ2)}(r2, θ2, z2)
end

Base.show(io::IO, x::Cylindrical) = print(io, "Cylindrical(r=$(x.r), θ=$(x.θ) rad, z=$(x.z))")
Base.isapprox(p1::Cylindrical, p2::Cylindrical; kwargs...) = isapprox(p1.r, p2.r; kwargs...) && isapprox(p1.θ, p2.θ; kwargs...) && isapprox(p1.z, p2.z; kwargs...)
Base.eltype(::Cylindrical{T}) where {T} = T
Base.eltype(::Type{Cylindrical{T}}) where {T} = T

"`SphericalFromCartesian()` - transformation from 3D point to `Spherical` type"
struct SphericalFromCartesian <: Transformation; end
Expand Down
89 changes: 89 additions & 0 deletions test/coordinatesystems.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,34 @@
partials(xy_gn[2], 1) partials(xy_gn[2], 2) ]
m = transform_deriv(c_from_p, rθ)
@test m ≈ m_gn

@testset "Common types" begin
xy = SVector(1.0, 2.0)
xy_i = SVector(1,2)
p1 = Polar(1, 2.0f0)
p2 = Polar(1.0, 2)
p3 = Polar{Int, Float64}(1, 2.0)
rθ = Polar(2.23606797749979, 1.1071487177940904)

@test typeof(p1.r) == typeof(p1.θ)
@test typeof(p2.r) == typeof(p2.θ)
@test typeof(p3.r) == Int
@test typeof(p3.θ) == Float64

@test p_from_c(xy_i) ≈ rθ
@test p_from_c(xy) ≈ rθ
@test p_from_c(collect(xy)) ≈ rθ
@test c_from_p(rθ) ≈ xy
end

@testset "Units" begin
xy = SVector(1.0, 2.0)u"m"
rθ = Polar(2.23606797749979u"m", 1.1071487177940904)

@test_broken p_from_c(xy) ≈ rθ
@test_broken p_from_c(collect(xy)) ≈ rθ
@test c_from_p(rθ) ≈ xy
end
end

@testset "3D" begin
Expand Down Expand Up @@ -435,5 +463,66 @@
# @test isapprox(m, m_gn; atol = 1e-12)
@test m ≈ m_gn

@testset "Common types" begin
xyz = SVector(1.0, 2.0, 3.0)
xyz_i = SVector(1, 2, 3)

@testset "Spherical" begin
rθϕ = Spherical(3.7416573867739413, 1.1071487177940904, 0.9302740141154721)

@test s_from_cart(xyz) ≈ rθϕ
@test s_from_cart(xyz_i) ≈ rθϕ
@test s_from_cart(collect(xyz)) ≈ rθϕ
@test cart_from_s(rθϕ) ≈ xyz

s1 = Spherical(1, 2.0, 3.0)
s2 = Spherical(1.0, 2, 3)
s3 = Spherical{Int,Int}(1, 2, 3)

@test typeof(s1.r) == typeof(s1.θ) == typeof(s1.ϕ) == Float64
@test typeof(s2.r) == typeof(s2.θ) == typeof(s2.ϕ) == Float64
@test typeof(s3.r) == typeof(s3.θ) == typeof(s3.ϕ) == Int
end

@testset "Cylindrical" begin
rθz = Cylindrical(2.23606797749979, 1.1071487177940904, 3.0)

@test cyl_from_cart(xyz) ≈ rθz
@test cyl_from_cart(xyz_i) ≈ rθz
@test cyl_from_cart(collect(xyz)) ≈ rθz
@test cart_from_cyl(rθz) ≈ xyz

c1 = Cylindrical(1, 2.0, 3)
c2 = Cylindrical(1.0, 2, 3.0)
c3 = Cylindrical(1, 2, 3)
c4 = Cylindrical{Int,Int}(1, 2, 3)

@test typeof(c1.r) == typeof(c1.z) == typeof(c1.θ) == Float64
@test typeof(c2.r) == typeof(c2.θ) == typeof(c2.z) == Float64
@test typeof(cyl_from_cart(xyz_i).r) == typeof(cyl_from_cart(xyz_i).z) == Float64
@test c3 == c4
end
end

@testset "Units" begin
xyz = SVector(1.0, 2.0, 3.0)u"m"

@testset "Shperical" begin
rθϕ = Spherical(3.7416573867739413u"m", 1.1071487177940904, 0.9302740141154721)

@test_broken s_from_cart(xyz) ≈ rθϕ
@test typeof(s_from_cart(xyz)) == typeof(rθϕ)
@test_broken s_from_cart(collect(xyz)) ≈ rθϕ
@test cart_from_s(rθϕ) ≈ xyz
end
@testset "Cylindrical" begin
rθz = Cylindrical(2.23606797749979u"m", 1.1071487177940904, 3.0u"m")

@test_broken cyl_from_cart(xyz) ≈ rθz
@test typeof(cyl_from_cart(xyz)) == typeof(rθz)
@test_broken cyl_from_cart(collect(xyz)) ≈ rθz
@test cart_from_cyl(rθz) ≈ xyz
end
end
end
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ using LinearAlgebra
using CoordinateTransformations
using ForwardDiff: Dual, partials
using StaticArrays
using Unitful

@testset "CoordinateTransformations" begin

Expand Down