Skip to content

Commit

Permalink
modified: Project.toml 1.3.1
Browse files Browse the repository at this point in the history
modified:   README.md          Add NoUnits example
modified:   example/readme_example.jl
  • Loading branch information
hustf committed Jun 13, 2021
1 parent b001e40 commit a07a161
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Unitfu = "5ee08b94-2369-4f4a-b8c7-99333ba35fb0"

[compat]
julia = "1.3.0"
julia = "1.3.1"
35 changes: 25 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,13 @@ pkg> registry add MechanicalUnits
Let us do some quick side calculations (code in `/example`):

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

julia> # First code section

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)
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)
Expand All @@ -95,7 +91,7 @@ julia> Q_cp(20°C, 985°C)
julia> dm |> upreferred
mm

julia> preferunits(m)
julia> preferunits(m) # No effect, since upreferred was called once this session

julia> m_s = [30kg/m 28.8lb/ft]
1×2 Matrix{Quantity{Float64, ᴹ ᴸ⁻¹, FreeUnits{(kg, m⁻¹), ᴹ ᴸ⁻¹, nothing}}}:
Expand All @@ -115,7 +111,7 @@ julia> E=206GPa; h_y = 100mm; b = 30mm; I = 1/12 * b * h_y^3
julia> L = 2m; F=100kg*g |> N
980.665N

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

julia> l_wire = 20m
Expand Down Expand Up @@ -174,7 +170,6 @@ julia> u*1.5A .|> J
-101.97560814373261 + 313.8486503774007im
-266.97560814373264 - 193.9691332565161im
266.97560814373264 - 193.9691332565162im

```

### Importing fewer units, or other units
Expand All @@ -183,18 +178,19 @@ 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> @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)
Unitfu.FreeUnits{(dyn,), ᴸ ᵀ⁻², nothing}
FreeUnits{(dyn,), ᴸ ᵀ⁻², nothing}

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

### Parsing text
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:

```julia
Expand All @@ -214,6 +210,25 @@ julia> time, Fx, Fy, Fz, Mx, My, Mz, px, py, pz = parse.(Quantity{Float64}, spli
2825.15907287598mm
```


### Special case: Units without dimension
Unit conversion works slightly different with such units, because the dimension is undefined. Here are some workarounds (using `ustrip` is discouraged since calculation errors may be masked by such operations):

```julia
julia> strain = 10.6μm/m
10.6μmm⁻¹

julia> strain |> upreferred
1.0599999999999998e-5

julia> strain *m/μm
10.6

julia> strain |> NoUnits
1.0599999999999998e-5
```


## Goals
This dependency of a [fork](https://github.com/hustf/Unitfu.jl) of [Unitful.jl](https://github.com/PainterQubits/Unitful.jl) aims to be a tool for quick side calculations in an office computer.

Expand Down
20 changes: 11 additions & 9 deletions example/readme_example.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
using MechanicalUnits
m_air = 1000kg; c_p = 1.00kJ/(kg*K)
@import_expand ~W # Watt = Joule / Second is not exported by default. Several: (u1, u2,..)
Q_cp(T1, T2) = m_air*c_p*(T2-T1) |> (kW*h)
Q_cp(T1, T2) = m_air * c_p * (T2 - T1) |> (kW*h)
Q_cp(20°C, 985°C)
dm |> upreferred
preferunits(m)
preferunits(m) # No effect, since upreferred was called once this session
m_s = [30kg/m 28.8lb/ft]
l_s = 93ft*[3 4]m/s
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
F*L^3/(3E*I) |> upreferred
l_wire = 20m
k(d) = E * 0.691 * π/4 * d^2 / l_wire |> N/mm
k.([5 6 8]mm)
Expand All @@ -31,18 +31,20 @@ u*1.5A .|> J

# Second code section
import MechanicalUnits: @import_expand,
@import_expand(~m, dyn) # ~ : also import SI prefixes for metre
@import_expand ~m dyn # ~ : also import SI prefixes for metre
(1.0cm², 2.0mmm, 3.0dm⁴/m² ) .|> mm²
typeof(dyn)
1dyn |> μm



# Third code section
strinp = "2 [s]\t11364.56982421875 [N]\t-44553.50244140625 [N]\t-26.586366176605225 [N]\t0.0[N mm]\t mm]\t1561.00350610.0[N mm]\t0.0[N mm]\t1561.00350618362 [mm]\t-6072.3729133606 [mm]\t2825.15907287598 [mm]"



strinp = "2 [s]\t11364.56982421875 [N]\t-44553.50244140625 [N]\t-26.586366176605225 [N]\t0.0[N mm]\t0.0[N mm]\t0.0[N mm]\t1561.00350618362 [mm]\t-6072.3729133606 [mm]\t2825.15907287598 [mm]";

time, Fx, Fy, Fz, Mx, My, Mz, px, py, pz = parse.(Quantity{Float64}, split(strinp, '\t'))
time, Fx, Fy, Fz, Mx, My, Mz, px, py, pz = parse.(Quantity{Float64}, split(strinp, '\t'))

# Fourth code section
strain = 10.6μm/m
strain |> upreferred
strain *m/μm
strain |> NoUnits

0 comments on commit a07a161

Please sign in to comment.