Skip to content

Commit

Permalink
get units, release 1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
islent committed Feb 6, 2020
1 parent 2e1df83 commit dc7de1b
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ license = "GPLv3"
authors = ["islent <[email protected]>"]
description = "Support for physical vectors and particles"
repository = "https://github.com/JuliaAstroSim/PhysicalParticles.jl"
version = "1.0.1"
version = "1.0.2"

[deps]
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
Expand Down
34 changes: 33 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,39 @@ preferunits(uSI)
```
or simply call `si()`. `astro()` and `cgs()` are implemented in the same way.

This would affect unit promotions in `Unitful` package and default outputs in related packages.
This would affect unit promotions in `Unitful` package and default outputs in related packages, by setting `Unitful.promotion` and `PhysicalParticles.uDefaults` respectively.

Interfaces to get basic units:
```
julia> getunits()
(m, s, A, K, cd, kg, mol)
julia> getunits(uAstro)
(kpc, Gyr, A, K, cd, M⊙, mol)
julia> getunits(nothing)
(nothing, nothing, nothing, nothing, nothing, nothing, nothing)
julia> getuLength()
m
julia> getuTime(uSI)
s
julia> getuCurrent(uCGS)
A
julia> getuTemperature(nothing)
julia> getuLuminosity()
cd
julia> getuMass()
kg
julia> getuAmount()
mol
```

### Constants

Expand Down
34 changes: 33 additions & 1 deletion docs/src/manual/Units.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,39 @@ preferunits(uSI)
```
or simply call `si()`. `astro()` and `cgs()` are implemented in the same way.

This would affect both unit promotions in `Unitful` package and default outputs in related packages, by setting `Unitful.promotion` and `PhysicalParticles.uDefaults` respectively.
This would affect unit promotions in `Unitful` package and default outputs in related packages, by setting `Unitful.promotion` and `PhysicalParticles.uDefaults` respectively.

Interfaces to get basic units:
```
julia> getunits()
(m, s, A, K, cd, kg, mol)
julia> getunits(uAstro)
(kpc, Gyr, A, K, cd, M⊙, mol)
julia> getunits(nothing)
(nothing, nothing, nothing, nothing, nothing, nothing, nothing)
julia> getuLength()
m
julia> getuTime(uSI)
s
julia> getuCurrent(uCGS)
A
julia> getuTemperature(nothing)
julia> getuLuminosity()
cd
julia> getuMass()
kg
julia> getuAmount()
mol
```

## Constants

Expand Down
9 changes: 8 additions & 1 deletion src/PhysicalParticles.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ using Unitful, UnitfulAstro, PhysicalConstants
## Explicitly overload functions and import types
import Unitful: Units, AbstractQuantity, uconvert, ustrip

import Base: +,-,*,/,zero,length,iterate,real,to_index, rand, show, ==, getproperty, one, zero
import Base: +, -, *, /, zero, length, iterate, real, to_index, rand, show, ==, getproperty, one, zero

import LinearAlgebra: norm, normalize, dot, cross

Expand Down Expand Up @@ -80,6 +80,13 @@ export
astro, si, cgs,
preferunits,
getunits,
getuLength,
getuTime,
getuCurrent,
getuTemperature,
getuLuminosity,
getuMass,
getuAmount,
uconvert, ustrip,

# Constant
Expand Down
30 changes: 23 additions & 7 deletions src/Unit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,27 @@ astro() = preferunits(uAstro)
si() = preferunits(uSI)
cgs() = preferunits(uCGS)

function getunits(units = nothing)
uLength, uTime, uCurrent, uTemperature, uLuminosity, uMass, uAmount = uDefaults

if !isnothing(units)
uLength, uTime, uCurrent, uTemperature, uLuminosity, uMass, uAmount = units
end
function getunits(units = uDefaults)
uLength, uTime, uCurrent, uTemperature, uLuminosity, uMass, uAmount = units
return uLength, uTime, uCurrent, uTemperature, uLuminosity, uMass, uAmount
end
end

function getunits(::Nothing)
return (nothing, nothing, nothing, nothing, nothing, nothing, nothing)
end

getuLength(::Nothing) = nothing
getuTime(::Nothing) = nothing
getuCurrent(::Nothing) = nothing
getuTemperature(::Nothing) = nothing
getuLuminosity(::Nothing) = nothing
getuMass(::Nothing) = nothing
getuAmount(::Nothing) = nothing

getuLength(units = uDefaults) = units[1]
getuTime(units = uDefaults) = units[2]
getuCurrent(units = uDefaults) = units[3]
getuTemperature(units = uDefaults) = units[4]
getuLuminosity(units = uDefaults) = units[5]
getuMass(units = uDefaults) = units[6]
getuAmount(units = uDefaults) = units[7]
19 changes: 19 additions & 0 deletions test/testUnitConstants.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,25 @@
u = getunits(uAstro)
@test [u...] == uAstro

u = getunits(nothing)
@test u == (nothing, nothing, nothing, nothing, nothing, nothing, nothing)

@test getuLength(nothing) == nothing
@test getuTime(nothing) == nothing
@test getuCurrent(nothing) == nothing
@test getuTemperature(nothing) == nothing
@test getuLuminosity(nothing) == nothing
@test getuMass(nothing) == nothing
@test getuAmount(nothing) == nothing

@test getuLength(uAstro) == u"kpc"
@test getuTime(uAstro) == u"Gyr"
@test getuCurrent(uAstro) == u"A"
@test getuTemperature(uAstro) == u"K"
@test getuLuminosity(uAstro) == u"cd"
@test getuMass(uAstro) == u"Msun"
@test getuAmount(uAstro) == u"mol"


@test uconvert(u"m", PVector2D(u"km")) == PVector2D(u"m")
@test uconvert(u"m", PVector(u"km")) == PVector(u"m")
Expand Down

0 comments on commit dc7de1b

Please sign in to comment.