-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* preliminary support for Unitful. Solves #68 * test printing of unitful particles * add Unitful note in the documentation * include tests
- Loading branch information
1 parent
545c8e0
commit 86461d7
Showing
7 changed files
with
79 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
name = "MonteCarloMeasurements" | ||
uuid = "0987c9cc-fe09-11e8-30f0-b96dd679fdca" | ||
authors = ["baggepinnen <[email protected]>"] | ||
version = "0.8.9" | ||
version = "0.8.10" | ||
|
||
[deps] | ||
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b" | ||
|
@@ -34,6 +34,7 @@ Measurements = "eff96d63-e80a-5855-80a2-b1b0885c5ab7" | |
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" | ||
SLEEFPirates = "476501e8-09a2-5ece-8869-fb82de89a1fa" | ||
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" | ||
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" | ||
|
||
[targets] | ||
test = ["ControlSystems", "ForwardDiff", "Measurements", "SLEEFPirates", "Test", "Plots"] | ||
test = ["ControlSystems", "ForwardDiff", "Measurements", "SLEEFPirates", "Test", "Plots", "Unitful"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import .Unitful: Quantity, FreeUnits | ||
|
||
function to_num_str(p::AbstractParticles{T}, d=3) where T <: Quantity | ||
s = std(p) | ||
if T <: AbstractFloat && s < eps(p) | ||
string(mean(p)) | ||
else | ||
string(mean(p), " ± ", s) | ||
end | ||
end | ||
|
||
for PT in ParticleSymbols | ||
|
||
@eval begin | ||
function Base.promote_rule(::Type{Quantity{S,D,U}}, ::Type{$PT{T,N}}) where {S, D, U, T, N} | ||
NT = promote_type(Quantity{S,D,U},T) | ||
$PT{NT,N} | ||
end | ||
|
||
function Base.convert(::Type{$PT{Quantity{S,D,U},N}}, y::Quantity) where {S, D, U, T, N} | ||
|
||
$PT{Quantity{S,D,U},N}(fill(y, N)) | ||
|
||
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) | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using Unitful | ||
function unitful_testfunction(Vi) | ||
if Vi ≤ 0.0u"V" | ||
return 0.0u"V" | ||
elseif Vi ≥ 1.0u"V" | ||
return 1.0u"V" | ||
else | ||
return Vi | ||
end | ||
end | ||
|
||
register_primitive(unitful_testfunction) # must be outside testset | ||
|
||
@testset "Unitful" begin | ||
@info "Testing Unitful" | ||
|
||
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" | ||
@test_nowarn println(p1) | ||
@test_nowarn display(p1) | ||
|
||
@test typeof(p1) == typeof(p2) | ||
|
||
p3 = unitful_testfunction(p1) | ||
@test extrema(p3) == (0.0u"V", 1.0u"V") | ||
end | ||
|
||
end |