Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working in SI units #23

Closed
md-arif-shaikh opened this issue Mar 12, 2021 · 2 comments
Closed

Working in SI units #23

md-arif-shaikh opened this issue Mar 12, 2021 · 2 comments

Comments

@md-arif-shaikh
Copy link

Hi, I was wondering if there is a simple way to import all the astrophysical units only in SI units, i.e., all I want is the number. For example for the mass of the Sun, I want the value 1.989 x 1e30 and for speed of light, it would be 3 x 1e8 and similarly for other astrophysical constants.

@mileslucas
Copy link
Member

mileslucas commented Mar 15, 2021

Hi there! This kind of functionality already exists, and I'll also point you towards https://github.com/JuliaPhysics/PhysicalConstants.jl which has even more constants. By default those are defined in SI units. Here's an example of how to string these together for some calculations-

For getting preferred units, you'll leverage upreferred and Unitful.preferunits (both from Unitful.jl). By default upreferred is set for SI

julia> using Unitful, UnitfulAstro
julia> 1u"Msun" |> upreferred
1.988409870698051e30 kg

According to Unitful, in order to change preferred units you'll have to restart the runtime (i.e., you need to call preferunits before calling upreferred for the first time)

julia> using Unitful, UnitfulAstro
julia> Unitful.preferunits(u"cm,g,s"...) # cgs
julia> 1u"Msun" |> upreferred
1.988409870698051e33 g

For constants like the Stefan-Boltzmann constant, speed of light, Gravitational constant, etc.

julia> using PhysicalConstants.CODATA2018: G
julia> G
Newtonian constant of gravitation (G)
Value                         = 6.6743e-11 m³ kg⁻¹ s⁻²
Standard uncertainty          = 1.5e-15 m³ kg⁻¹ s⁻²
Relative standard uncertainty = 2.2e-5
Reference                     = CODATA 2018

julia> G |> upreferred
6.674299999999999e-8 cm³ g⁻¹ s⁻²

this example actually doesn't work currently but will as soon as PainterQubits/Unitful.jl#431 is merged. Like I said, though, PhysicalConstants.jl is SI by default, and in the meantime you can add

julia> Unitful.upreferred(q::Unitful.AbstractQuantity) = uconvert(upreferred(unit(q)), q)

locally and it should all work!

@mileslucas
Copy link
Member

I'm going to go ahead and close this issue- if you have further questions or have problems feel free to reopen!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants