Giter Club home page Giter Club logo

naturallyunitful.jl's Introduction

CI

NaturallyUnitful.jl

This package reexports Unitful.jl alongside two extra functions:

  1. natural, a function for converting a given quantity to the Physicist's so-called "natural units", in which

    ħ = c = ϵ₀ = kb = 1

    julia> using NaturallyUnitful
    
    julia> natural(1u"m")
    5.067730759202785e6 eV^-1
    
    julia> natural(3e8u"m/s")
    1.000692285594456

    natural also accepts a keyword argument base (defaults to electron volts) which determines what unit your natural quantity is constructed from. Currently, the base unit must have dimensions of energy.

    julia> natural(1u"m", base=u"GeV")
    5.067730759202785e15 GeV^-1
  2. unnatural, a function for converting from natural units to a given unnatural unit such as meters

    julia> unnatural(u"m", 5.067730759202785e6u"eV^-1")
    1.0 m
    
    julia> unnatural(u"m/s", 1)
    2.99792458e8 m s^-1

Installation Instructions

To install, simply open the pkg prompt from the julia REPL by pressing ], and type:

pkg> add NaturallyUnitful

naturallyunitful.jl's People

Contributors

masonprotter avatar mo8it avatar mschauer avatar waldyrious avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

naturallyunitful.jl's Issues

Fractional powers of units

I think NaturallyUnitful.jl does not handle fractional powers of units correctly. At least that's what I guess from trying to get the Planck mass in eV by taking 1/sqrt(8 pi G) with G from PhysicalConstants.jl and then trying to convert this to natural units using NaturallyUnitful.jl as follows:

julia> using PhysicalConstants.CODATA2018: G

julia> Mpl = 1/sqrt(8pi* G)
24416.151347483574 kg^1/2 s m^-3/2

julia> using NaturallyUnitful

julia> Mpl |> natural
ERROR: InexactError: Int64(-3//2)
Stacktrace:
  [1] Integer
    @ ./rational.jl:109 [inlined]
  [2] convert
    @ ./number.jl:7 [inlined]
  [3] setindex!(h::Dict{Symbol, Int64}, v0::Rational{Int64}, key::Symbol)
    @ Base ./dict.jl:382
  [4] setDimPow(D::Dict{Symbol, Int64}, x::Unitful.Dimension{:Length})
    @ NaturallyUnitful ~/.julia/packages/NaturallyUnitful/LJau5/src/NaturallyUnitful.jl:12
  [5] dimDict(x::Unitful.Dimensions{(Unitful.Dimension{:Length}(-3//2), Unitful.Dimension{:Mass}(1//2), Unitful.Dimension{:Time}(1//1))})
    @ NaturallyUnitful ~/.julia/packages/NaturallyUnitful/LJau5/src/NaturallyUnitful.jl:21
  [6] dimDict
    @ ~/.julia/packages/NaturallyUnitful/LJau5/src/NaturallyUnitful.jl:26 [inlined]
  [7] _natural(base::Unitful.FreeUnits{(eV,), 𝐋^2 𝐌 𝐓^-2, nothing}, q::Quantity{Float64, 𝐌^1/2 𝐓 𝐋^-3/2, Unitful.FreeUnits{(kg^1/2, m^-3/2, s), 𝐌^1/2 𝐓 𝐋^-3/2, nothing}})
    @ NaturallyUnitful ~/.julia/packages/NaturallyUnitful/LJau5/src/NaturallyUnitful.jl:59
  [8] #natural#1
    @ ~/.julia/packages/NaturallyUnitful/LJau5/src/NaturallyUnitful.jl:56 [inlined]
  [9] natural
    @ ~/.julia/packages/NaturallyUnitful/LJau5/src/NaturallyUnitful.jl:56 [inlined]
 [10] |>(x::Quantity{Float64, 𝐌^1/2 𝐓 𝐋^-3/2, Unitful.FreeUnits{(kg^1/2, m^-3/2, s), 𝐌^1/2 𝐓 𝐋^-3/2, nothing}}, f::typeof(natural))
    @ Base ./operators.jl:858
 [11] top-level scope
    @ REPL[4]:1

A more artificial example using fractional powers is this:

julia> using NaturallyUnitful

julia> unnatural(u"(km/s)^(3/2)", 1)
ERROR: InexactError: Int64(3//2)
Stacktrace:
  [1] Integer
    @ ./rational.jl:109 [inlined]
  [2] convert
    @ ./number.jl:7 [inlined]
  [3] setindex!(h::Dict{Symbol, Int64}, v0::Rational{Int64}, key::Symbol)
    @ Base ./dict.jl:382
  [4] setDimPow
    @ ~/.julia/packages/NaturallyUnitful/LJau5/src/NaturallyUnitful.jl:12 [inlined]
  [5] dimDict(x::Unitful.Dimensions{(Unitful.Dimension{:Length}(3//2), Unitful.Dimension{:Time}(-3//2))})
    @ NaturallyUnitful ~/.julia/packages/NaturallyUnitful/LJau5/src/NaturallyUnitful.jl:21
  [6] dimDict
    @ ~/.julia/packages/NaturallyUnitful/LJau5/src/NaturallyUnitful.jl:26 [inlined]
  [7] _natural(base::Unitful.FreeUnits{(eV,), 𝐋^2 𝐌 𝐓^-2, nothing}, q::Quantity{Int64, 𝐋^3/2 𝐓^-3/2, Unitful.FreeUnits{(km^3/2, s^-3/2), 𝐋^3/2 𝐓^-3/2, nothing}})
    @ NaturallyUnitful ~/.julia/packages/NaturallyUnitful/LJau5/src/NaturallyUnitful.jl:59
  [8] #natural#1
    @ ~/.julia/packages/NaturallyUnitful/LJau5/src/NaturallyUnitful.jl:56 [inlined]
  [9] natural
    @ ~/.julia/packages/NaturallyUnitful/LJau5/src/NaturallyUnitful.jl:56 [inlined]
 [10] unnatural(targetUnit::Unitful.FreeUnits{(km^3/2, s^-3/2), 𝐋^3/2 𝐓^-3/2, nothing}, q::Int64)
    @ NaturallyUnitful ~/.julia/packages/NaturallyUnitful/LJau5/src/NaturallyUnitful.jl:87
 [11] top-level scope
    @ REPL[2]:1

julia> unnatural(u"(km/s)^(4/2)", 1)
8.987551787368178e10 km^2 s^-2

It would be great if such cases worked as well. Otherwise, I find this packge very useful!

Merge to Unitful?

People from Unitful seem to be open for a merge: PainterQubits/Unitful.jl#480
Are you OK with that? This would join forces and introduce your awesome functions for natural units to people not knowing about NaturallyUnitful.
I would then try to prepare a merge request which you can review.

Differing constants between Unitful.jl and NaturallyUnitful.jl

This package defines the following constants:

const c = 1u"c"
const ħ = 1.0545718e-34u"kg*m^2/s"
const ħc = ħ*c
const kB = 1.38064852e-23u"kg*m^2/s^2/K"
const ϵ0 = 8.85418782e-12u"m^-3*kg^-1*s^4*A^2"

Some of these are already defined in Unitful.jl, although with slightly different values:

julia> Unitful.ħ
1.0545718176461565e-34 J s

julia> NaturallyUnitful.ħ
1.0545718e-34 kg m^2 s^-1

julia> (Unitful.ħ - NaturallyUnitful.ħ)/Unitful.ħ |> NoUnits # relative difference between the two values
1.6733005915006143e-8

Is there a reason for this? Does the community that uses natural units prefer these values over the CODATA recommended values used by Unitful.jl?

If there is no reason for the different values, I would suggest to just import the values from Unitful.jl where possible, to avoid possible confusion. Note that this is not possible in the case of c, since Unitful.c is a unit and NaturallyUnitful.c is a quantity (equal to 1 * Unitful.c).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.