From b001e40cdba50a8d12c0dd492fe352a2f64caeb5 Mon Sep 17 00:00:00 2001 From: hustf Date: Fri, 7 May 2021 16:38:06 +0200 Subject: [PATCH] No conversion of collections without a '.'. Export promote_to_derived 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 --- Project.toml | 2 +- README.md | 143 +++----------------- {examples => example}/readme_example.jl | 6 +- {examples => example}/torpedo_hangglider.jl | 0 src/MechanicalUnits.jl | 4 +- src/internal_functions.jl | 2 +- test/conversion_promotion.jl | 4 +- 7 files changed, 26 insertions(+), 135 deletions(-) rename {examples => example}/readme_example.jl (95%) rename {examples => example}/torpedo_hangglider.jl (100%) diff --git a/Project.toml b/Project.toml index d44bbe8..174eb96 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "MechanicalUnits" uuid = "e6be9192-89dc-11e9-36e6-5dbcb28f419e" authors = ["hustf "] -version = "0.4.4" +version = "0.5.0" [deps] Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" diff --git a/README.md b/README.md index 4fc559d..b17ceb7 100644 --- a/README.md +++ b/README.md @@ -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> 1kg∙m∙s⁻¹ |> N 1N∙s ``` @@ -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.0kJ∙kg⁻¹∙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.05555555555554kW∙h - -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-11m³∙kg⁻¹∙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.1592653589793rad∙s⁻¹ - -julia> t = (0:0.006:0.02)s -(0.0:0.006:0.018)s - -julia> u = 220V*exp.(im∙(ω∙t)) -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∙μm∙s⁻² - -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.0kJ∙kg⁻¹∙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) @@ -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 @@ -262,7 +151,7 @@ julia> @import_expand ~V ~W ~A G julia> sqrt(1G²) 6.6743e-11m³∙kg⁻¹∙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 @@ -279,7 +168,7 @@ 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 @@ -287,20 +176,23 @@ julia> u*1.5A |> J 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.0mm∙m, 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∙μm∙s⁻² - ``` 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: @@ -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 diff --git a/examples/readme_example.jl b/example/readme_example.jl similarity index 95% rename from examples/readme_example.jl rename to example/readme_example.jl index 37c59cd..f39b696 100644 --- a/examples/readme_example.jl +++ b/example/readme_example.jl @@ -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 @@ -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.(im∙(ω∙t)) -u*1.5A |> J +u*1.5A .|> J # Second code section import MechanicalUnits: @import_expand, ∙ diff --git a/examples/torpedo_hangglider.jl b/example/torpedo_hangglider.jl similarity index 100% rename from examples/torpedo_hangglider.jl rename to example/torpedo_hangglider.jl diff --git a/src/MechanicalUnits.jl b/src/MechanicalUnits.jl index 9612f28..73d4b7d 100644 --- a/src/MechanicalUnits.jl +++ b/src/MechanicalUnits.jl @@ -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 @@ -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 diff --git a/src/internal_functions.jl b/src/internal_functions.jl index dc78759..d5199e7 100644 --- a/src/internal_functions.jl +++ b/src/internal_functions.jl @@ -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) diff --git a/test/conversion_promotion.jl b/test/conversion_promotion.jl index 28858bf..558904e 100644 --- a/test/conversion_promotion.jl +++ b/test/conversion_promotion.jl @@ -38,8 +38,8 @@ end @testset "Conversions with arrays" begin v = [1N 2daN] - @test v |> kg == [1000//1 20000//1]kg∙mm∙s⁻² - @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