Skip to content

Commit

Permalink
No conversion of collections without a '.'. Export promote_to_derived
Browse files Browse the repository at this point in the history
modified:   Project.toml                   0.5.0
modified:   README.md                      Update example
renamed:    examples/readme_example.jl -> example/readme_example.jl
renamed:    examples/torpedo_hangglider.jl -> example/torpedo_hangglider.jl
modified:   src/MechanicalUnits.jl        export promote_to_derived
modified:   src/internal_functions.jl     Improve docstring
modified:   test/conversion_promotion.jl  Update test of array conversion
  • Loading branch information
hustf committed May 7, 2021
1 parent ec1c4a4 commit b001e40
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 135 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "MechanicalUnits"
uuid = "e6be9192-89dc-11e9-36e6-5dbcb28f419e"
authors = ["hustf <[email protected]>"]
version = "0.4.4"
version = "0.5.0"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Expand Down
143 changes: 17 additions & 126 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
Using units should be quick, nice and easy. That's the aim of this package, built on a slight [modification](https://github.com/hustf/Unitfu.jl).of [Unitful.jl](https://github.com/PainterQubits/Unitful.jl).

We enhance readability with colors, and we don't throw errors at meaningful input:
```

```julia
julia> 1kgms⁻¹ |> N
1Ns
```
Expand Down Expand Up @@ -64,137 +65,25 @@ We avoid defining common and ambigious derived dimensions. For example, the deri
### Installation
We appreciate 'Unitful.jl', but do need some specific syntax. To avoid conflict, Unitfu.jl and this package is registered in a separate registry, which holds related packages for solving, plotting, sketching and latex with units.

´´´
```julia
pkg> registry add https://github.com/hustf/M8
pkg> registry add MechanicalUnits

´´´
```


### Example REPL workflow

Let us do some quick side calculations (code in `/examples`):
```julia
julia> using MechanicalUnits

julia> m_air = 1000kg; c_p = 1.00kJ/(kg*K)
1.0kJkg⁻¹K⁻¹

julia> @import_expand ~W # Watt = Joule / Second is not exported by default. Several: (u1, u2,..)

julia> Q_cp(T1, T2) = m_air*c_p*(T2-T1) |> (kW*h)
Q_cp (generic function with 1 method)

julia> Q_cp(20°C, 985°C)
268.05555555555554kWh

julia> dm |> upreferred
mm

julia> preferunits(m)

julia> m_s = [30kg/m 28.8lb/ft]
1×2 Matrix{Quantity{Float64, ᴹ ᴸ⁻¹, FreeUnits{(kg, m⁻¹), ᴹ ᴸ⁻¹, nothing}}}:
30.0 42.8591

julia> l_s = 93ft*[3 4]m/s
1×2 Matrix{Quantity{Int64, ᴸ² ᵀ⁻¹, FreeUnits{(ft, m, s⁻¹), ᴸ² ᵀ⁻¹, nothing}}}:
279 372

julia> m_s.*l_s |> (kg*m)
1×2 Matrix{Quantity{Float64, ᴸ ᵀ⁻¹, FreeUnits{(kg, m, s⁻¹), ᴸ ᵀ⁻¹, nothing}}}:
2551.18 4859.61

julia> E=206GPa; h_y = 100mm; b = 30mm; I = 1/12 * b * h_y^3
2.5e6mm⁴

julia> L = 2m; F=100kg*g |> N
980.665N

julia> F*L^3/(3E*I) |> mm
5.0778770226537215mm

julia> l_wire = 20m
20m

julia> k(d) = E * 0.691 * π/4 * d^2 / l_wire |> N/mm
k (generic function with 1 method)

julia> k.([5 6 8]mm)
1×3 Matrix{Quantity{Float64, ᴹ ᵀ⁻², FreeUnits{(mm⁻¹, N), ᴹ ᵀ⁻², nothing}}}:
139.748 201.237 357.755
Let us do some quick side calculations (code in `/example`):

julia> δ(d)= F / k(d) |> mm
δ (generic function with 1 method)

julia> δ.([5, 6, 8]mm)
3-element Vector{Quantity{Float64, ᴸ, FreeUnits{(mm,), ᴸ, nothing}}}:
7.017388381199098
4.873186375832707
2.7411673364058977

julia> d = 6mm
6mm

julia> dimension(d)

julia> 1d |> s
(3//500)m

julia> @import_expand ~V ~W ~A G

julia> sqrt(1G²)
6.6743e-11kg⁻¹s⁻²

julia> [1V*12.0A 2W 1kg*g*1m/2s]*30minute |> kJ
1×3 Matrix{Quantity{Float64, ᴸ² ᵀ⁻², FreeUnits{(kJ,), ᴸ² ᵀ⁻², nothing}}}:
21.6 3.6 8.82598

julia> ω = 50*2π*rad/s
314.1592653589793rads⁻¹

julia> t = (0:0.006:0.02)s
(0.0:0.006:0.018)s

julia> u = 220V*exp.(imt))
4-element Vector{Quantity{ComplexF64, ᴸ² ᴵ⁻¹ ᵀ⁻³, FreeUnits{(V,), ᴸ² ᴵ⁻¹ ᵀ⁻³, nothing}}}:
220.0 + 0.0im
-67.98373876248841 + 209.2324335849338im
-177.98373876248843 - 129.31275550434407im
177.98373876248843 - 129.31275550434412im

julia> typeof(dyn)
FreeUnits{(dyn,), ᴸ ᵀ⁻², nothing} ), ᴸ² ᵀ⁻³, nothing}}}:
y default
julia> 1dyn |> μm
10kgμms⁻²

julia>

julia>
julia> lin = "2 [s]\t11364.56982421875 [N]\t-44553.50244140625 [N]\t-26.586366176605225 [N]\t0.0[N mm]\t0.0[N mm]"
"2 [s]\t11364.56982421875 [N]\t-44553.50244140625 [N]\t-26.586366176605225 [N]\t0.0[N mm]\t0.0[N mm]"
julia> strinp = "2 [s]\t11364.56982421875 [N]\t-44553.50240625 [N]\t4140625 [N]\t-26.586366176605225 [N]\t0.0[N mm]\t0.0[N mm]" .586366176
"2 [s]\t11364.56982421875 [N]\t-44553.50244140625 [N]\t-26.586366176605225 [N]\t0.0[N mm]\t0.0[N mm]" 4140625 [N

julia> .586366176
```julia
julia> # Run line by line in the REPL, or ctrl + enter in VSCode

julia>
julia> # First code section

julia> time, Fx, Fy, Fz, Mx, My, Mz, px, py, pz = parse.(Quantity{Float64}, split(lin, '\t'))
ERROR: BoundsError: attempt to access 6-element Vector{Quantity{Float64, D, U} where {D, U}} at index [7]StacktraFloat64, D, U} where {D, U}} at index [7]Stacktrace:
[1] getindex
@ .\array.jl:801 [inlined]
[2] indexed_iterate(a::Vector{Quantity{Float64, D, U} where {D, U}}, i::Int64, state::Int64) U}}, i::Int64, state::Int64)
@ Base .\tuple.jl:87
[3] top-level scope
@ REPL[38]:1
, '\t'))index [7]Stacktrajulia> using MechanicalUnits
julia> using MechanicalUnits

julia> m_air = 1000kg; c_p = 1.00kJ/(kg*K)
1.0kJkg⁻¹K⁻¹

julia> @import_expand ~W # Watt = Joule / Second is not exported by default. Several: (u1, u2,..)

julia> Q_cp(T1, T2) = m_air*c_p*(T2-T1) |> (kW*h)
Expand All @@ -216,7 +105,7 @@ julia> l_s = 93ft*[3 4]m/s
1×2 Matrix{Quantity{Int64, ᴸ² ᵀ⁻¹, FreeUnits{(ft, m, s⁻¹), ᴸ² ᵀ⁻¹, nothing}}}:
279 372

julia> m_s.*l_s |> (kg*m)
julia> m_s.*l_s .|> (kg*m)
1×2 Matrix{Quantity{Float64, ᴸ ᵀ⁻¹, FreeUnits{(kg, m, s⁻¹), ᴸ ᵀ⁻¹, nothing}}}:
2551.18 4859.61

Expand Down Expand Up @@ -262,7 +151,7 @@ julia> @import_expand ~V ~W ~A G
julia> sqrt(1G²)
6.6743e-11kg⁻¹s⁻²

julia> [1V*12.0A 2W 1kg*g*1m/2s]*30minute |> kJ
julia> [1V*12.0A 2W 1kg*g*1m/2s]*30minute .|> kJ
1×3 Matrix{Quantity{Float64, ᴸ² ᵀ⁻², FreeUnits{(kJ,), ᴸ² ᵀ⁻², nothing}}}:
21.6 3.6 8.82598

Expand All @@ -279,28 +168,31 @@ julia> u = 220V*exp.(im∙(ω∙t))
-177.98373876248843 - 129.31275550434407im
177.98373876248843 - 129.31275550434412im

julia> u*1.5A |> J
julia> u*1.5A .|> J
4-element Vector{Quantity{ComplexF64, ᴸ² ᵀ⁻³, FreeUnits{(J, s⁻¹), ᴸ² ᵀ⁻³, nothing}}}:
330.0 + 0.0im
-101.97560814373261 + 313.8486503774007im
-266.97560814373264 - 193.9691332565161im
266.97560814373264 - 193.9691332565162im

```

### Importing fewer units, or other units
If you want fewer globally defined variables, @import_expand just what you need:

```julia
julia> import MechanicalUnits: @import_expand,

julia> @import_expand(~m, dyn) # ~ : also import SI prefixes for metre

julia> (1.0cm², 2.0mmm, 3.0dm⁴/m² ) .|> mm²
(100.0, 2000.0, 300.0)mm²

julia> typeof(dyn)
FreeUnits{(dyn,), ᴸ ᵀ⁻², nothing}
Unitfu.FreeUnits{(dyn,), ᴸ ᵀ⁻², nothing}

julia> 1dyn |> μm
10kgμms⁻²

```

When parsing a text file, typically from some other software, spaces as multipliers and brackets are allowed. Tabs are also accepted. But you need to specify the numeric type of output quantities, like this:
Expand All @@ -320,7 +212,6 @@ julia> time, Fx, Fy, Fz, Mx, My, Mz, px, py, pz = parse.(Quantity{Float64}, spli
1561.00350618362mm
-6072.3729133606mm
2825.15907287598mm

```

## Goals
Expand Down
6 changes: 3 additions & 3 deletions examples/readme_example.jl → example/readme_example.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dm |> upreferred
preferunits(m)
m_s = [30kg/m 28.8lb/ft]
l_s = 93ft*[3 4]m/s
m_s.*l_s |> (kg*m)
m_s.*l_s .|> (kg*m)
E=206GPa; h_y = 100mm; b = 30mm; I = 1/12 * b * h_y^3
L = 2m; F=100kg*g |> N
F*L^3/(3E*I) |> mm
Expand All @@ -23,11 +23,11 @@ dimension(d)
1d |> s
@import_expand ~V ~W ~A G
sqrt(1G²)
[1V*12.0A 2W 1kg*g*1m/2s]*30minute |> kJ
[1V*12.0A 2W 1kg*g*1m/2s]*30minute .|> kJ
ω = 50*2π*rad/s
t = (0:0.006:0.02)s
u = 220V*exp.(imt))
u*1.5A |> J
u*1.5A .|> J

# Second code section
import MechanicalUnits: @import_expand,
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions src/MechanicalUnits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export ∙
# Import / exports for short and parseable type signatures
import Unitfu: Time, Length, Mass, Temperature, Current, Luminosity, Amount
import Unitfu: ᵀ , ᴸ , ᴹ , ᶿ, ᴶ , ᴺ
import Unitfu: lookup_units
import Unitfu: lookup_units, promote_to_derived
export Time, Length, Mass, Temperature, Current, Luminosity, Amount, Level
export ᵀ , ᴸ , ᴹ , ᶿ , ᴶ , ᴺ
export Quantity, DimensionlessQuantity, NoUnits, NoDims
Expand All @@ -22,7 +22,7 @@ export logunit, unit, absoluteunit, dimension, uconvert, ustrip, upreferred, ∙
export uconvertp, uconvertrp, reflevel, linear

# Useful functions that are not exported by Unitfu.
export preferunits, convfact
export preferunits, convfact, promote_to_derived

# A vector of all the exported units. This is printed during precompilation.
export MECH_UNITS
Expand Down
2 changes: 1 addition & 1 deletion src/internal_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ end

"""
Import non-affine units from Unitfu.
Expand to also include exponents -4 to 4.
Expand to also include 'fancy' exponents -4 to 4, i.e. m² etc.
"""
macro import_expand(args...)
expr = Expr(:block)
Expand Down
4 changes: 2 additions & 2 deletions test/conversion_promotion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ end

@testset "Conversions with arrays" begin
v = [1N 2daN]
@test v |> kg == [1000//1 20000//1]kgmms⁻²
@test (v |> kg .=== Array([(1000//1) (20000//1)]kg*mm*s^-2)) == [true true]
@test all(v .|> kg .=== (([1000 // 1 20000 // 1] * kg) mm) s⁻² )
@test all(v .|> kg .=== Array([(1000//1) (20000//1)]kg*mm*s^-2))
end

@testset "Mixed collections" begin
Expand Down

0 comments on commit b001e40

Please sign in to comment.