Skip to content

Commit

Permalink
More unitful compatability. Closes #81
Browse files Browse the repository at this point in the history
  • Loading branch information
baggepinnen committed May 28, 2020
1 parent d7b302c commit f18b456
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
35 changes: 28 additions & 7 deletions src/unitful.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,35 @@ for PT in ParticleSymbols
$PT{Quantity{S,D,U},N}(fill(y, N))

end
end

function Base.:*(p::$PT{T,N}, y::Quantity{S,D,U}) where {S, D, U, T, N}
NT = promote_type(S,T)
$PT{Quantity{NT,D,U},N}(p.particles .* y)
end

function Base.:*(p::$PT, y::FreeUnits)
$PT(p.particles .* y)
for op in (*, /)
f = nameof(op)
@eval begin
function Base.$f(p::$PT{T,N}, y::Quantity{S,D,U}) where {S, D, U, T, N}
NT = promote_type(T, S)
$PT{Quantity{NT,D,U},N}($(op).(p.particles , y))
end

function Base.$f(p::$PT{T,N}, y::Quantity{S,D,U}) where {S, D, U, T <: Quantity, N}
QT = Base.promote_op($op, T, typeof(y))
$PT{QT,N}($(op).(p.particles, y))
end

# Below is just the reverse signature of above
function Base.$f(y::Quantity{S,D,U}, p::$PT{T,N}) where {S, D, U, T, N}
NT = promote_type(T, S)
$PT{Quantity{NT,D,U},N}($(op).(y, p.particles))
end

function Base.$f(y::Quantity{S,D,U}, p::$PT{T,N}) where {S, D, U, T <: Quantity, N}
QT = Base.promote_op($op, typeof(y), T)
$PT{QT,N}($(op).(y, p.particles))
end

function Base.$f(p::$PT, y::FreeUnits)
$PT($(op).(p.particles, y))
end
end

end
Expand Down
15 changes: 15 additions & 0 deletions test/test_unitful.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ register_primitive(unitful_testfunction) # must be outside testset
@testset "Unitful" begin
@info "Testing Unitful"

PT = Particles
for PT in (Particles, StaticParticles)
p1 = PT(100, Uniform(-0.5,1.5)) * 1u"V"
p2 = PT(100, Uniform(-0.5,1.5)) * u"V"
Expand All @@ -24,6 +25,20 @@ register_primitive(unitful_testfunction) # must be outside testset

p3 = unitful_testfunction(p1)
@test extrema(p3) == (0.0u"V", 1.0u"V")

@test (1 ± 0.5)u"m" * (1 ± 0)u"kg" (1 ± 0.5)u"kg*m"
@test (1 ± 0.5)u"m" * 1u"kg" (1 ± 0.5)u"kg*m"

@test (1 ± 0.5)u"m" / (1 ± 0)u"kg" (1 ± 0.5)u"m/kg"
@test (1 ± 0.5)u"m" / 1u"kg" (1 ± 0.5)u"m/kg"

@test (1 ± 0.5)u"m" + (1 ± 0)u"m" (2 ± 0.5)u"m"
@test (1 ± 0.5)u"m" + 1u"m" (2 ± 0.5)u"m"


@test 1u"m" * (1 ± 0.5)u"kg" (1 ± 0.5)u"kg*m"
@test 1u"m" / (1 ± 0.5)u"kg" (1 ± 0.5)u"m/kg"
@test 1u"m" + (1 ± 0.5)u"m" (2 ± 0.5)u"m"
end

end

0 comments on commit f18b456

Please sign in to comment.