Skip to content

Commit

Permalink
modified: README.md Add 'Units' section
Browse files Browse the repository at this point in the history
modified:   test/pretty-print.jl Allow for 32bit types
  • Loading branch information
hustf committed Jun 25, 2019
1 parent 9974c01 commit 1e9baf7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 21 deletions.
56 changes: 47 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![Build Status](https://ci.appveyor.com/api/projects/status/github/hustf/MechanicalUnits.jl?svg=true)](https://ci.appveyor.com/project/hustf/MechanicalUnits-jl)
[![Coveralls](https://coveralls.io/repos/github/hustf/MechanicalUnits.jl/badge.svg?branch=master)](https://coveralls.io/github/hustf/MechanicalUnits.jl?branch=master)


- [Units](#units)
- [Usage](#usage)
- [Goals](#goals)
- [Alternatives](#alternatives)
Expand All @@ -24,49 +24,86 @@ The benefits?
* You could pick plot recipes based on units
* You could pick table formats based on units


## Units
| Dimension | Unit |
| ------------- | ------------- |
| Length | nm μm μm mm cm dm m km Mm Gm Tm Pm inch ft |
| Time | ns μs μs ms s minute d h yr |
| Mass | mg cg kg lb shton |
| Temperature | K Ra °C °F |
| Angles | rad ° |
| Force | N daN kN MN lbf kip |
| Pressure | Pa kPa MPa GPa atm bar |
| Energy | J kJ MJ GJ |
| Volume | l dl cl ml |
| Acceleration | g |



## Usage
Let us do some side calculations. It's a pity we can't show the colors here:
```
julia > ]add MechanicalUnits
```
Colors won't show here. But let us do some side calculations:
```julia
julia> using MechanicalUnits

julia> c_p = 1.00kJ/(kg*K) ; T1 = 0°C ; T2 = 1000°C ; m_air = 1kg;

julia> m_air*c_p*(T2-T1)
1000.0kJ

julia> begin
"Work, heating air at constant pressure"
Q_cp(T1, T2) = m_air*c_p*(T2-T1)
end
Q_cp

julia> Q_cp(20°C, 25°C)
5.0kJ

julia> year_and_a_day = 1yr + 6*7d
(35186400//1)s

julia> 2year_and_a_day |> yr
(1086//487)yr

julia> 1dm|>upreferred
(100//1)mm

julia> exit()

PS C:\Users\F> julia --banner=no

julia> using MechanicalUnits

julia> preferunits(m)

julia> 1dm|>upreferred
(1//10)m

julia> exit()

PS C:\Users\F> julia --banner=no

julia> using MechanicalUnits

julia> # Estimate deflection

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

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

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

julia> # Pick a corresponding wire rope

julia> l_wire = 20m

julia> k(d) = E * 0.691 * π/4 * d^2 / l_wire |> N/mm

julia> k(30mm)
Expand All @@ -89,15 +126,16 @@ Stacktrace:
julia> dimension(d)
Time

julia> print(mech_units)
Symbol[:nm, :μm, :μm, :mm, :cm, :dm, :m, :km, :Mm, :Gm, :Tm, :Pm,
:ns, :μs, :μs, :ms, :s, :mg, :cg, :kg, :rad, , :K, :Ra, :minute,
:d, :atm, :bar, :N, :daN, :kN, :MN, :Pa, :kPa, :MPa, :GPa, :J, :kJ,
:MJ, :GJ, :°C, :°F, :h, :yr, :l, :dl, :cl, :ml, :g]
julia> @import_from_unitful G

julia> sqrt(1G²)
6.67408e-11kg^-1s^-2
```

You may get warning messages like the above when also loading other packages. If that happens, switch to importing just what you need:
```import MechanicalUnits: N, kg, m, s, MPa```
You may get warning messages when also loading other packages. If that happens, switch from `using MechanicalUnits`to just what you need:
```julia
import MechanicalUnits: N, kg, m, s, MPa
```


## Goals (11/19 reached)
Expand Down
22 changes: 10 additions & 12 deletions test/pretty-print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ using MechanicalUnits
using Test
shortp(x) = repr(x, context = :color=>true)
longp(x) = repr(:"text/plain", x, context = :color=>true)

global const sInt = typeof(Int(1)) == Int64 ? "Int64" : "Int32"
@testset "Most basic" begin
testunits = [m , s , kg ]
teststrings = ["m" , "s" , "kg" ]
Expand Down Expand Up @@ -68,17 +68,17 @@ end
@test String(take!(buf)) == "1Unit{:Meter,Length}(0, 1//1)"
print(buf, 1m)
@test String(take!(buf)) == "1m"
@test shortp(typeof(1kgKm/s)) == "Quantity{Int64,Length*Mass*Temperature*Time^-1,FreeUnits{(Unit{:Gram,Mass}" *
@test shortp(typeof(1kgKm/s)) == "Quantity{$(sInt),Length*Mass*Temperature*Time^-1,FreeUnits{(Unit{:Gram,Mass}" *
"(3, 1//1), Unit{:Kelvin,Temperature}(0, 1//1), " *
"Unit{:Meter,Length}(0, 1//1), Unit{:Second,Time}(0, -1//1))," *
"Length*Mass*Temperature*Time^-1,nothing}}"
q1 = Quantity{Int64, Length, FreeUnits{(Unit{:Meter, Length}(0,1),), Length, nothing}}(2)
q1 = Quantity{Int, Length, FreeUnits{(Unit{:Meter, Length}(0,1),), Length, nothing}}(2)
q2 = 2m
@test q1 == q2
@test q1 === q2
strcon = repr(:"text/plain", typeof(q2), context = :color=>false)
tq1 = typeof(q1)
@test strcon == "Quantity{Int64,Length,FreeUnits{(Unit{:Meter,Length}(0, 1//1),),Length,nothing}}"
@test strcon == "Quantity{$(sInt),Length,FreeUnits{(Unit{:Meter,Length}(0, 1//1),),Length,nothing}}"
sy = Meta.parse(strcon)
ex = :($sy(2))
q3 = eval(ex)
Expand All @@ -93,17 +93,17 @@ end
@test String(take!(buf)) == "1FreeUnits{(Unit{:Kelvin,Temperature}(0, 1//1),),Temperature,Affine{-5463//20}}"
print(buf, 1°C)
@test String(take!(buf)) == "1°C"
@test shortp(typeof(1kgKm/s)) == "Quantity{Int64,Length*Mass*Temperature*Time^-1,FreeUnits{(Unit{:Gram,Mass}" *
@test shortp(typeof(1kgKm/s)) == "Quantity{$(sInt),Length*Mass*Temperature*Time^-1,FreeUnits{(Unit{:Gram,Mass}" *
"(3, 1//1), Unit{:Kelvin,Temperature}(0, 1//1), " *
"Unit{:Meter,Length}(0, 1//1), Unit{:Second,Time}(0, -1//1))," *
"Length*Mass*Temperature*Time^-1,nothing}}"
q1 = Quantity{Int64, Temperature, FreeUnits{(Unit{:Kelvin, Temperature}(0,1),), Temperature, Affine{-5463//20}}}(2)
q1 = Quantity{Int, Temperature, FreeUnits{(Unit{:Kelvin, Temperature}(0,1),), Temperature, Affine{-5463//20}}}(2)
q2 = 2°C
@test q1 == q2
@test q1 === q2
strcon = repr(:"text/plain", typeof(q2), context = :color=>false)
tq1 = typeof(q1)
@test strcon == "Quantity{Int64,Temperature,FreeUnits{(Unit{:Kelvin,Temperature}(0, 1//1),),Temperature,Affine{-5463//20}}}"
@test strcon == "Quantity{$(sInt),Temperature,FreeUnits{(Unit{:Kelvin,Temperature}(0, 1//1),),Temperature,Affine{-5463//20}}}"
sy = Meta.parse(strcon)
ex = :($sy(2))
q3 = eval(ex)
Expand All @@ -130,20 +130,18 @@ end
a1 = [1 2]m
st ="[2 4]\e[36mm\e[39m"
@test shortp(2a1) == st
st = "1×2 Array{Int64{\e[36mm\e[39m},2}:\n 2 4"
st = "1×2 Array{$(sInt){\e[36mm\e[39m},2}:\n 2 4"
@test longp(2a1) == st
a2 = [1 2]m*s^-1
st = "[2 4]\e[36mm\e[39m∙\e[36ms^-1\e[39m"
@test shortp(2a2) == st
st = "1×2 Array{Int64{\e[36mm\e[39m∙\e[36ms^-1\e[39m},2}:\n 2 4"
st = "1×2 Array{$(sInt){\e[36mm\e[39m∙\e[36ms^-1\e[39m},2}:\n 2 4"
@test longp(2a2) == st
end

# TODO test chosen color for units
#@testset "Pick color for units" begin


#1m∙5N
#repr(1m, context = :color=>true, :unitsymbolcolor => :blue)
#end
# TODO test chosen color for units
# Add division by units for matrices, based on *(A::AbstractArray, B::Unitful.Units) in Unitful at C:\Users\F\.julia\packages\Unitful\W0mMi\src\quantities.jl:36

0 comments on commit 1e9baf7

Please sign in to comment.