[ANN & RFC] New package: PhysicalConstants.jl

During the last days I’ve been working on a small new package:
https://github.com/JuliaPhysics/PhysicalConstants.jl

The physical constants are defined as Constant objects, which can be turned into Quantity objects (from
Unitful.jl package) or Measurement objects (from Measurements.jl package) at request.

An example is worth a thousand words:

julia> using PhysicalConstants.CODATA2014

julia> c
Speed of light in vacuum (c)
Value                         = 2.99792458e8 m s^-1
Standard uncertainty          = (exact)
Relative standard uncertainty = (exact)
Reference                     = CODATA 2014

julia> G
Newtonian constant of gravitation (G)
Value                         = 6.67408e-11 m^3 kg^-1 s^-2
Standard uncertainty          = 3.1e-15 m^3 kg^-1 s^-2
Relative standard uncertainty = 4.6e-5
Reference                     = CODATA 2014

julia> float(G)
6.67408e-11 m^3 kg^-1 s^-2

julia> 4pi * float(G) / float(c)^2
9.331680614932136e-27 m kg^-1

julia> using Measurements

julia> measurement(ħ)
1.0545718001391127e-34 ± 1.2891550390443523e-42 J s

julia> measurement(BigFloat, ħ)
1.054571800139112651153941068725066773746246506229852090971714108355028066256094e-34 ± 1.289155039044352219727958483317366332479123130497697234856105486877064060837251e-42 J s

julia> big(ħ) / (big(h) / (2 * big(pi)))
1.0

Constants.jl is not yet registered because I’d like first to address a couple of design issues. If you’re interested in this package, please chime in: https://github.com/JuliaPhysics/PhysicalConstants.jl/issues

16 Likes

There are many types of constants. Perhaps a prefix to the package name (Physical) would be suitable?

4 Likes

Good idea, I was thinking about this, too. BTW, is there an easy way to rename a package locally besides manually editing the toml files in ~/.julia/environments/vX.Y?

A few days ago, the first official version of PhysicalConstants.jl has been released :tada:

Features

  • physical constants with physical units (from Unitful.jl) and optionally with uncertainty (from Measurements.jl)
  • support for any AbstractFloat type that can be converted from Float64, and for BigFloat. This could be particularly useful for exact constants (like speed of light, and more coming in next CODATA 2018 dataset!), they’ll be always exact!
  • the package is modular in the sense that you can import the dataset that you want to use. The idea is that the package will provide different dataset (e.g., different versions of CODATA constants), but the user will load only the one they are interested in
  • informative printing of constants in the REPL
  • constants can be readily used in any mathematical operations supported by AbstractQuantity (the type in terms of which are defined the operations in Unitful.jl)

Example

julia> using PhysicalConstants.CODATA2014

julia> SpeedOfLightInVacuum
Speed of light in vacuum (c_0)
Value                         = 2.99792458e8 m s^-1
Standard uncertainty          = (exact)
Relative standard uncertainty = (exact)
Reference                     = CODATA 2014

julia> NewtonianConstantOfGravitation
Newtonian constant of gravitation (G)
Value                         = 6.67408e-11 m^3 kg^-1 s^-2
Standard uncertainty          = 3.1e-15 m^3 kg^-1 s^-2
Relative standard uncertainty = 4.6e-5
Reference                     = CODATA 2014

julia> import PhysicalConstants.CODATA2014: c_0, ε_0, μ_0, ħ, h

julia> 2 * ε_0
1.7708375635240778e-11 F m^-1

julia> ε_0 - 1 / (μ_0 * c_0 ^ 2)
0.0 A^2 s^4 kg^-1 m^-3

julia> big(ε_0) - inv(big(μ_0) * big(c_0)^2)
0.0 A^2 s^4 kg^-1 m^-3

julia> using Measurements

julia> ħ
Planck constant over 2pi (ħ)
Value                         = 1.0545718001391127e-34 J s
Standard uncertainty          = 1.2891550390443523e-42 J s
Relative standard uncertainty = 1.2e-8
Reference                     = CODATA 2014

julia> measurement(ħ)
1.0545718001391127e-34 ± 1.2891550390443523e-42 J s

julia> measurement(Float32, ħ)
1.0545718e-34 ± 1.289e-42 J s

julia> measurement(BigFloat, ħ)
1.054571800139112651153941068725066773746246506229852090971714108355028066256094e-34 ± 1.289155039044352219727958483317366332479123130497697234856105486877064060837251e-42 J s

julia> measurement(BigFloat, ħ) / (measurement(BigFloat, h) / (2 * big(pi)))
1.0 ± 0.0
10 Likes

Excellent package!, a little question, how do i use the value without the units?

1 Like

See here: http://ajkeller34.github.io/Unitful.jl/latest/manipulations/#Unitful.ustrip

2 Likes

thanks!

Right after the release of the new CODATA2018 set of recommended values, a new version of PhysicalConstants.jl is about to come (just waiting for the PR to be merged in the General register), featuring the new dataset:

ulia> using PhysicalConstants.CODATA2018

julia> PlanckConstant
Planck constant (h)
Value                         = 6.62607015e-34 J s
Standard uncertainty          = (exact)
Relative standard uncertainty = (exact)
Reference                     = CODATA 2018

julia> VacuumMagneticPermeability
Vacuum magnetic permeability (μ_0)
Value                         = 1.25663706212e-6 N A^-2
Standard uncertainty          = 1.9e-16 N A^-2
Relative standard uncertainty = 1.5e-10
Reference                     = CODATA 2018

The new dataset can be used alongside the old one, but if you load both you have to qualify the constants that have the same name (note that a couple of constants changed their name, because their name changed in the official CODATA list of values, you can find the list of all variables in the README.md):

julia> using PhysicalConstants.CODATA2014
WARNING: using CODATA2014.PlanckConstant in module Main conflicts with an existing identifier.

julia> CODATA2014.PlanckConstant
Planck constant (h)
Value                         = 6.62607004e-34 J s
Standard uncertainty          = 8.1e-42 J s
Relative standard uncertainty = 1.2e-8
Reference                     = CODATA 2014

julia> MagneticConstant
Magnetic constant (μ_0)
Value                         = 1.2566370614359173e-6 N A^-2
Standard uncertainty          = (exact)
Relative standard uncertainty = (exact)
Reference                     = CODATA 2014

An interesting variable is the Wien wavelength displacement law constant that now is defined to be exact, but whose value depends on the zero of a non algebraic function. The excellent Roots.jl is used to compute the zero when the constant is converted to arbitrary precision:

julia> big(CODATA2018.WienWavelengthDisplacementLawConstant)
0.002897771955185172661478605448092884726801628303071854440630774014015910656760728 K m

It’s interesting to see how the relative uncertainties of the constants changed from one set to the other one:

julia> using PhysicalConstants, Unitful, Measurements

julia> import Measurements: value, uncertainty

julia> for dataset in (PhysicalConstants.CODATA2014, PhysicalConstants.CODATA2018)
           constants = names(dataset)
           println("## ", nameof(dataset), "\n")
           for constant in constants
               c = getfield(dataset, constant)
               if c isa PhysicalConstants.PhysicalConstant
                   println(rpad(string(constant) * ": ", 39), uncertainty(measurement(c)) / value(measurement(c)))
               end
           end
           println("\n")
       end
## CODATA2014

AtomicMassConstant:                    1.2044281717098324e-8
AvogadroConstant:                      1.2287988899161014e-8
BohrMagneton:                          6.146208601983096e-9
BohrRadius:                            2.2676713505493936e-10
BoltzmannConstant:                     5.721948696979012e-7
CharacteristicImpedanceOfVacuum:       0.0
ElectricConstant:                      0.0
ElectronMass:                          1.2075460350908751e-8
ElementaryCharge:                      6.116678943365593e-9
FineStructureConstant:                 2.3296119853486266e-10
MagneticConstant:                      0.0
MolarGasConstant:                      5.773074998811107e-7
NeutronMass:                           1.2537856333242981e-8
NewtonianConstantOfGravitation:        4.64483494354278e-5
PlanckConstant:                        1.2224440658040493e-8
PlanckConstantOver2pi:                 1.2224440658040493e-8
ProtonMass:                            1.2555138746605121e-8
RydbergConstant:                       5.92323582859768e-12
SpeedOfLightInVacuum:                  0.0
StandardAccelerationOfGravitation:     0.0
StandardAtmosphere:                    0.0
StefanBoltzmannConstant:               2.292620565829337e-6
ThomsonCrossSection:                   1.367915290986615e-9
WienWavelengthDisplacementLawConstant: 5.866574292278045e-7


## CODATA2018

AtomicMassConstant:                    3.0110703810405613e-10
AvogadroConstant:                      0.0
BohrMagneton:                          3.019190163003642e-10
BohrRadius:                            1.511780899700616e-10
BoltzmannConstant:                     0.0
ElectronMass:                          3.0737534961217373e-10
ElementaryCharge:                      0.0
FineStructureConstant:                 1.5073959899206537e-10
MolarGasConstant:                      0.0
NeutronMass:                           5.671887297281165e-10
NewtonianConstantOfGravitation:        2.2474266964325848e-5
PlanckConstant:                        0.0
ProtonMass:                            3.0491050773439597e-10
ReducedPlanckConstant:                 0.0
RydbergConstant:                       1.9136608062230136e-12
SpeedOfLightInVacuum:                  0.0
StandardAccelerationOfGravitation:     0.0
StandardAtmosphere:                    0.0
StefanBoltzmannConstant:               0.0
ThomsonCrossSection:                   9.019221676713751e-10
VacuumElectricPermittivity:            1.468231787584925e-10
VacuumMagneticPermeability:            1.511971958549925e-10
WienFrequencyDisplacementLawConstant:  0.0
WienWavelengthDisplacementLawConstant: 0.0
5 Likes