Skip to content

Commit 6a332ff

Browse files
authored
Merge pull request #60 from SebastianM-C/unitful
Unitful compatibility
2 parents 1c03fc9 + 2027403 commit 6a332ff

File tree

4 files changed

+127
-15
lines changed

4 files changed

+127
-15
lines changed

Project.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ StaticArrays = "0.11,0.12"
1313
julia = "1"
1414

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

1920
[targets]
20-
test = ["Test", "ForwardDiff"]
21+
test = ["Test", "ForwardDiff", "Unitful"]

src/coordinatesystems.jl

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,23 @@
22
### 2D Coordinate systems ###
33
#############################
44
"""
5-
`Polar{T}(r::T, θ::T)` - 2D polar coordinates
5+
`Polar{T,A}(r::T, θ::A)` - 2D polar coordinates
66
"""
7-
struct Polar{T}
7+
struct Polar{T,A}
88
r::T
9-
θ::T
9+
θ::A
10+
11+
Polar{T, A}(r, θ) where {T, A} = new(r, θ)
12+
end
13+
14+
function Polar(r, θ)
15+
r2, θ2 = promote(r, θ)
16+
17+
return Polar{typeof(r2), typeof(θ2)}(r2, θ2)
1018
end
19+
1120
Base.show(io::IO, x::Polar) = print(io, "Polar(r=$(x.r), θ=$(x.θ) rad)")
1221
Base.isapprox(p1::Polar, p2::Polar; kwargs...) = isapprox(p1.r, p2.r; kwargs...) && isapprox(p1.θ, p2.θ; kwargs...)
13-
Base.eltype(::Polar{T}) where {T} = T
14-
Base.eltype(::Type{Polar{T}}) where {T} = T
1522

1623
"`PolarFromCartesian()` - transformation from `AbstractVector` of length 2 to `Polar` type"
1724
struct PolarFromCartesian <: Transformation; end
@@ -67,28 +74,42 @@ Base.convert(::Type{Polar}, v::AbstractVector) = PolarFromCartesian()(v)
6774
"""
6875
Spherical(r, θ, ϕ) - 3D spherical coordinates
6976
"""
70-
struct Spherical{T}
77+
struct Spherical{T,A}
7178
r::T
72-
θ::T
73-
ϕ::T
79+
θ::A
80+
ϕ::A
81+
82+
Spherical{T, A}(r, θ, ϕ) where {T, A} = new(r, θ, ϕ)
7483
end
84+
85+
function Spherical(r, θ, ϕ)
86+
r2, θ2, ϕ2 = promote(r, θ, ϕ)
87+
88+
return Spherical{typeof(r2), typeof(θ2)}(r2, θ2, ϕ2)
89+
end
90+
7591
Base.show(io::IO, x::Spherical) = print(io, "Spherical(r=$(x.r), θ=$(x.θ) rad, ϕ=$(x.ϕ) rad)")
7692
Base.isapprox(p1::Spherical, p2::Spherical; kwargs...) = isapprox(p1.r, p2.r; kwargs...) && isapprox(p1.θ, p2.θ; kwargs...) && isapprox(p1.ϕ, p2.ϕ; kwargs...)
77-
Base.eltype(::Spherical{T}) where {T} = T
78-
Base.eltype(::Type{Spherical{T}}) where {T} = T
7993

8094
"""
8195
Cylindrical(r, θ, z) - 3D cylindrical coordinates
8296
"""
83-
struct Cylindrical{T}
97+
struct Cylindrical{T,A}
8498
r::T
85-
θ::T
99+
θ::A
86100
z::T
101+
102+
Cylindrical{T, A}(r, θ, z) where {T, A} = new(r, θ, z)
87103
end
104+
105+
function Cylindrical(r, θ, z)
106+
r2, θ2, z2 = promote(r, θ, z)
107+
108+
return Cylindrical{typeof(r2), typeof(θ2)}(r2, θ2, z2)
109+
end
110+
88111
Base.show(io::IO, x::Cylindrical) = print(io, "Cylindrical(r=$(x.r), θ=$(x.θ) rad, z=$(x.z))")
89112
Base.isapprox(p1::Cylindrical, p2::Cylindrical; kwargs...) = isapprox(p1.r, p2.r; kwargs...) && isapprox(p1.θ, p2.θ; kwargs...) && isapprox(p1.z, p2.z; kwargs...)
90-
Base.eltype(::Cylindrical{T}) where {T} = T
91-
Base.eltype(::Type{Cylindrical{T}}) where {T} = T
92113

93114
"`SphericalFromCartesian()` - transformation from 3D point to `Spherical` type"
94115
struct SphericalFromCartesian <: Transformation; end

test/coordinatesystems.jl

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,34 @@
9999
partials(xy_gn[2], 1) partials(xy_gn[2], 2) ]
100100
m = transform_deriv(c_from_p, rθ)
101101
@test m m_gn
102+
103+
@testset "Common types" begin
104+
xy = SVector(1.0, 2.0)
105+
xy_i = SVector(1,2)
106+
p1 = Polar(1, 2.0f0)
107+
p2 = Polar(1.0, 2)
108+
p3 = Polar{Int, Float64}(1, 2.0)
109+
= Polar(2.23606797749979, 1.1071487177940904)
110+
111+
@test typeof(p1.r) == typeof(p1.θ)
112+
@test typeof(p2.r) == typeof(p2.θ)
113+
@test typeof(p3.r) == Int
114+
@test typeof(p3.θ) == Float64
115+
116+
@test p_from_c(xy_i)
117+
@test p_from_c(xy)
118+
@test p_from_c(collect(xy))
119+
@test c_from_p(rθ) xy
120+
end
121+
122+
@testset "Units" begin
123+
xy = SVector(1.0, 2.0)u"m"
124+
= Polar(2.23606797749979u"m", 1.1071487177940904)
125+
126+
@test_broken p_from_c(xy)
127+
@test_broken p_from_c(collect(xy))
128+
@test c_from_p(rθ) xy
129+
end
102130
end
103131

104132
@testset "3D" begin
@@ -435,5 +463,66 @@
435463
# @test isapprox(m, m_gn; atol = 1e-12)
436464
@test m m_gn
437465

466+
@testset "Common types" begin
467+
xyz = SVector(1.0, 2.0, 3.0)
468+
xyz_i = SVector(1, 2, 3)
469+
470+
@testset "Spherical" begin
471+
rθϕ = Spherical(3.7416573867739413, 1.1071487177940904, 0.9302740141154721)
472+
473+
@test s_from_cart(xyz) rθϕ
474+
@test s_from_cart(xyz_i) rθϕ
475+
@test s_from_cart(collect(xyz)) rθϕ
476+
@test cart_from_s(rθϕ) xyz
477+
478+
s1 = Spherical(1, 2.0, 3.0)
479+
s2 = Spherical(1.0, 2, 3)
480+
s3 = Spherical{Int,Int}(1, 2, 3)
481+
482+
@test typeof(s1.r) == typeof(s1.θ) == typeof(s1.ϕ) == Float64
483+
@test typeof(s2.r) == typeof(s2.θ) == typeof(s2.ϕ) == Float64
484+
@test typeof(s3.r) == typeof(s3.θ) == typeof(s3.ϕ) == Int
485+
end
486+
487+
@testset "Cylindrical" begin
488+
rθz = Cylindrical(2.23606797749979, 1.1071487177940904, 3.0)
489+
490+
@test cyl_from_cart(xyz) rθz
491+
@test cyl_from_cart(xyz_i) rθz
492+
@test cyl_from_cart(collect(xyz)) rθz
493+
@test cart_from_cyl(rθz) xyz
494+
495+
c1 = Cylindrical(1, 2.0, 3)
496+
c2 = Cylindrical(1.0, 2, 3.0)
497+
c3 = Cylindrical(1, 2, 3)
498+
c4 = Cylindrical{Int,Int}(1, 2, 3)
499+
500+
@test typeof(c1.r) == typeof(c1.z) == typeof(c1.θ) == Float64
501+
@test typeof(c2.r) == typeof(c2.θ) == typeof(c2.z) == Float64
502+
@test typeof(cyl_from_cart(xyz_i).r) == typeof(cyl_from_cart(xyz_i).z) == Float64
503+
@test c3 == c4
504+
end
505+
end
506+
507+
@testset "Units" begin
508+
xyz = SVector(1.0, 2.0, 3.0)u"m"
509+
510+
@testset "Shperical" begin
511+
rθϕ = Spherical(3.7416573867739413u"m", 1.1071487177940904, 0.9302740141154721)
512+
513+
@test_broken s_from_cart(xyz) rθϕ
514+
@test typeof(s_from_cart(xyz)) == typeof(rθϕ)
515+
@test_broken s_from_cart(collect(xyz)) rθϕ
516+
@test cart_from_s(rθϕ) xyz
517+
end
518+
@testset "Cylindrical" begin
519+
rθz = Cylindrical(2.23606797749979u"m", 1.1071487177940904, 3.0u"m")
520+
521+
@test_broken cyl_from_cart(xyz) rθz
522+
@test typeof(cyl_from_cart(xyz)) == typeof(rθz)
523+
@test_broken cyl_from_cart(collect(xyz)) rθz
524+
@test cart_from_cyl(rθz) xyz
525+
end
526+
end
438527
end
439528
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ using LinearAlgebra
33
using CoordinateTransformations
44
using ForwardDiff: Dual, partials
55
using StaticArrays
6+
using Unitful
67

78
@testset "CoordinateTransformations" begin
89

0 commit comments

Comments
 (0)