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

Add CODATA2022 module #39

Merged
merged 8 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ Introduction

`PhysicalConstants.jl` provides common physical constants. They are defined as
instances of the new `Constant` type, which is subtype of `AbstractQuantity`
(from [`Unitful.jl`](https://github.com/ajkeller34/Unitful.jl) package) and can
(from [`Unitful.jl`](https://github.com/PainterQubits/Unitful.jl) package) and can
giordano marked this conversation as resolved.
Show resolved Hide resolved
also be turned into `Measurement` objects (from
[`Measurements.jl`](https://github.com/JuliaPhysics/Measurements.jl) package) at
request.

Constants are grouped into different submodules, so that the user can choose
different datasets as needed. Currently, 2014 and 2018 editions of
different datasets as needed. Currently, 2014, 2018, and 2022 editions of
[CODATA](https://physics.nist.gov/cuu/Constants/) recommended values of the
fundamental physical constants are provided.

Expand All @@ -38,41 +38,41 @@ Usage
You can load the package as usual with `using PhysicalConstants` but this module
does not provide anything useful for the end-users. You most probably want to
directly load the submodule with the dataset you are interested in. For
example, for CODATA 2018 load `PhysicalConstants.CODATA2018`:
example, for CODATA 2022 load `PhysicalConstants.CODATA2022`:

```julia
julia> using PhysicalConstants.CODATA2018
julia> using PhysicalConstants.CODATA2022

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

julia> NewtonianConstantOfGravitation
Newtonian constant of gravitation (G)
Value = 6.6743e-11 m^3 kg^-1 s^-2
Standard uncertainty = 1.5e-15 m^3 kg^-1 s^-2
Relative standard uncertainty = 2.2e-5
Reference = CODATA 2018
Reference = CODATA 2022
```

`SpeedOfLightInVacuum` and `NewtonianConstantOfGravitation` are two of the
`PhysicalConstant`s defined in the `PhysicalConstants.CODATA2018` module, the
`PhysicalConstant`s defined in the `PhysicalConstants.CODATA2022` module, the
full list of available constants is given below.

`PhysicalConstant`s can be readily used in mathematical operations, using by
default their `Float64` value:

```julia
julia> import PhysicalConstants.CODATA2018: c_0, ε_0, μ_0
julia> import PhysicalConstants.CODATA2022: c_0, ε_0, μ_0

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

julia> ε_0 - 1 / (μ_0 * c_0 ^ 2)
-3.8450973786644646e-25 A^2 s^4 kg^-1 m^-3
1.0567555442791707e-23 A^2 s^4 kg^-1 m^-3
```

If you want to use a different precision for the value of the constant, use the
Expand All @@ -83,13 +83,13 @@ julia> float(Float32, ε_0)
8.854188f-12 F m^-1

julia> float(BigFloat, ε_0)
8.854187812799999999999999999999999999999999999999999999999999999999999999999973e-12 F m^-1
8.854187818800000000000000000000000000000000000000000000000000000000000000000059e-12 F m^-1

julia> big(ε_0)
8.854187812799999999999999999999999999999999999999999999999999999999999999999973e-12 F m^-1
8.854187818800000000000000000000000000000000000000000000000000000000000000000059e-12 F m^-1

julia> big(ε_0) - inv(big(μ_0) * big(c_0)^2)
-3.849883307464075736533920296598236938395867709081184624499315166190408485179288e-25 A^2 s^4 kg^-1 m^-3
1.056704162590924117341831987227432956066714823419574007586677144869010778731235e-23 A^2 s^4 kg^-1 m^-3
```

Note that `big(constant)` is an alias for `float(BigFloat, constant)`.
Expand All @@ -100,7 +100,7 @@ the constant, use `measurement(x)`:
```julia
julia> using Measurements

julia> import PhysicalConstants.CODATA2018: h, ħ
julia> import PhysicalConstants.CODATA2022: h, ħ

julia> measurement(ħ)
1.0545718176461565e-34 ± 0.0 J s
Expand Down
3 changes: 2 additions & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Documenter, PhysicalConstants, Unitful
using PhysicalConstants: CODATA2014, CODATA2018, CODATA2022
giordano marked this conversation as resolved.
Show resolved Hide resolved

## Generate list of constants
open(joinpath(@__DIR__, "src", "constants.md"), "w") do io
Expand All @@ -15,7 +16,7 @@ open(joinpath(@__DIR__, "src", "constants.md"), "w") do io
use most frequently, as shown in the examples above.
"""
)
for set in (PhysicalConstants.CODATA2014, PhysicalConstants.CODATA2018)
for set in (CODATA2014, CODATA2018, CODATA2022)
println(io)
println(io, "### ", nameof(set))
println(io)
Expand Down
4 changes: 2 additions & 2 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
[`PhysicalConstants.jl`](https://github.com/JuliaPhysics/PhysicalConstants.jl)
provides common physical constants. They are defined as instances of the new
`Constant` type, which is subtype of `AbstractQuantity` (from
[`Unitful.jl`](https://github.com/ajkeller34/Unitful.jl) package) and can also
[`Unitful.jl`](https://github.com/PainterQubits/Unitful.jl) package) and can also
be turned into `Measurement` objects (from
[`Measurements.jl`](https://github.com/JuliaPhysics/Measurements.jl) package) at
request.

Constants are grouped into different submodules, so that the user can choose
different datasets as needed. Currently, 2014 and 2018 editions of
different datasets as needed. Currently, 2014, 2018, and 2022 editions of
[CODATA](https://physics.nist.gov/cuu/Constants/) recommended values of the
fundamental physical constants are provided.

Expand Down
24 changes: 12 additions & 12 deletions docs/src/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,41 @@
You can load the package as usual with `using PhysicalConstants` but this module
does not provide anything useful for the end-users. You most probably want to
directly load the submodule with the dataset you are interested in. For
example, for CODATA 2018 load `PhysicalConstants.CODATA2018`:
example, for CODATA 2022 load `PhysicalConstants.CODATA2022`:

```julia
julia> using PhysicalConstants.CODATA2018
julia> using PhysicalConstants.CODATA2022

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

julia> NewtonianConstantOfGravitation
Newtonian constant of gravitation (G)
Value = 6.6743e-11 m^3 kg^-1 s^-2
Standard uncertainty = 1.5e-15 m^3 kg^-1 s^-2
Relative standard uncertainty = 2.2e-5
Reference = CODATA 2018
Reference = CODATA 2022
```

`SpeedOfLightInVacuum` and `NewtonianConstantOfGravitation` are two of the
`PhysicalConstant`s defined in the `PhysicalConstants.CODATA2018` module, the
`PhysicalConstant`s defined in the `PhysicalConstants.CODATA2022` module, the
full list of available constants is given below.

`PhysicalConstant`s can be readily used in mathematical operations, using by
default their `Float64` value:

```julia
julia> import PhysicalConstants.CODATA2018: c_0, ε_0, μ_0
julia> import PhysicalConstants.CODATA2022: c_0, ε_0, μ_0

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

julia> ε_0 - 1 / (μ_0 * c_0 ^ 2)
-3.8450973786644646e-25 A^2 s^4 kg^-1 m^-3
1.0567555442791707e-23 A^2 s^4 kg^-1 m^-3
```

If you want to use a different precision for the value of the constant, use the
Expand All @@ -48,13 +48,13 @@ julia> float(Float32, ε_0)
8.854188f-12 F m^-1

julia> float(BigFloat, ε_0)
8.854187812799999999999999999999999999999999999999999999999999999999999999999973e-12 F m^-1
8.854187818800000000000000000000000000000000000000000000000000000000000000000059e-12 F m^-1

julia> big(ε_0)
8.854187812799999999999999999999999999999999999999999999999999999999999999999973e-12 F m^-1
8.854187818800000000000000000000000000000000000000000000000000000000000000000059e-12 F m^-1

julia> big(ε_0) - inv(big(μ_0) * big(c_0)^2)
-3.849883307464075736533920296598236938395867709081184624499315166190408485179288e-25 A^2 s^4 kg^-1 m^-3
1.056704162590924117341831987227432956066714823419574007586677144869010778731235e-23 A^2 s^4 kg^-1 m^-3
```

Note that `big(constant)` is an alias for `float(BigFloat, constant)`.
Expand All @@ -65,7 +65,7 @@ the constant, use `measurement(x)`:
```julia
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All these codeblocks should probably be jldoctests

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, and it caught an error where the second code block didn't have its own explicit import.

julia> using Measurements

julia> import PhysicalConstants.CODATA2018: h, ħ
julia> import PhysicalConstants.CODATA2022: h, ħ

julia> measurement(ħ)
1.0545718176461565e-34 ± 0.0 J s
Expand Down
27 changes: 14 additions & 13 deletions src/PhysicalConstants.jl
Original file line number Diff line number Diff line change
Expand Up @@ -252,14 +252,14 @@ Return the physical constant as a `Quantity` with the floating type optionally s
`FloatType`, `Float64` by default.

```jldoctest
julia> using PhysicalConstants.CODATA2018: G
julia> using PhysicalConstants.CODATA2022: G

julia> G
Newtonian constant of gravitation (G)
Value = 6.6743e-11 m^3 kg^-1 s^-2
Standard uncertainty = 1.5e-15 m^3 kg^-1 s^-2
Relative standard uncertainty = 2.2e-5
Reference = CODATA 2018
Reference = CODATA 2022

julia> float(G)
6.6743e-11 m^3 kg^-1 s^-2
Expand All @@ -278,22 +278,22 @@ Return the physical constant as a `Quantity` with standard uncertainty. The flo
precision can be optionally specified with the `FloatType`, `Float64` by default.

```jldoctest
julia> using PhysicalConstants.CODATA2018, Measurements
julia> using PhysicalConstants.CODATA2022, Measurements

julia> import PhysicalConstants.CODATA2018: μ_0
julia> import PhysicalConstants.CODATA2022: μ_0

julia> μ_0
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
Value = 1.25663706127e-6 N A^-2
Standard uncertainty = 2.0e-16 N A^-2
Relative standard uncertainty = 1.6e-10
Reference = CODATA 2022

julia> measurement(μ_0)
1.25663706212e-6 ± 1.9e-16 N A^-2
1.25663706127e-6 ± 2.0e-16 N A^-2

julia> measurement(Float32, μ_0)
1.256637e-6 ± 1.9e-16 N A^-2
1.256637e-6 ± 2.0e-16 N A^-2
```
"""
measurement(::PhysicalConstant)
Expand All @@ -306,23 +306,24 @@ Return the reference defined for the physical constant.
```jldoctest
julia> using PhysicalConstants

julia> using PhysicalConstants.CODATA2018: h
julia> using PhysicalConstants.CODATA2022: h

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

julia> PhysicalConstants.reference(h)
"CODATA 2018"
"CODATA 2022"
```
"""
function reference end

include("promotion.jl")
include("codata2014.jl")
include("codata2018.jl")
include("codata2022.jl")

end # module
115 changes: 115 additions & 0 deletions src/codata2022.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
module CODATA2022
giordano marked this conversation as resolved.
Show resolved Hide resolved

using PhysicalConstants, Measurements, Unitful, Roots

import Unitful: Ω, A, C, F, Hz, J, kg, K, m, mol, N, Pa, s, T, W
import PhysicalConstants: @constant, @derived_constant

## Helper functions for the Wien displacement law constants
_λf(x) = (x - 5) * exp(x) + 5
_Dλf(x) = (x - 4) * exp(x)

Check warning on line 10 in src/codata2022.jl

View check run for this annotation

Codecov / codecov/patch

src/codata2022.jl#L9-L10

Added lines #L9 - L10 were not covered by tests
const _λx0 = 4.965114231744276
_λx(T) = find_zero((_λf, _Dλf), T(_λx0), Roots.Newton())

Check warning on line 12 in src/codata2022.jl

View check run for this annotation

Codecov / codecov/patch

src/codata2022.jl#L12

Added line #L12 was not covered by tests

_νf(x) = (x - 3) * exp(x) + 3
_Dνf(x) = (x - 2) * exp(x)

Check warning on line 15 in src/codata2022.jl

View check run for this annotation

Codecov / codecov/patch

src/codata2022.jl#L14-L15

Added lines #L14 - L15 were not covered by tests
const _νx0 = 2.8214393721220787
_νx(T) = find_zero((_νf, _Dνf), T(_νx0), Roots.Newton())

Check warning on line 17 in src/codata2022.jl

View check run for this annotation

Codecov / codecov/patch

src/codata2022.jl#L17

Added line #L17 was not covered by tests
## end

@constant(FineStructureConstant, α, "Fine-structure constant", 7.297_352_564_3e-3,
BigFloat(7_297_352_564_3)/BigFloat(10_000_000_000_000), Unitful.NoUnits,
1.1e-12, BigFloat(11)/BigFloat(10_000_000_000_000), "CODATA 2022")
@constant(BohrRadius, a_0, "Bohr radius", 5.291_772_105_44e-11,
BigFloat(5_291_772_105_44)/BigFloat(10_000_000_000_000_000_000_000), m,
8.2e-21, BigFloat(82)/BigFloat(10_000_000_000_000_000_000_000), "CODATA 2022")
@constant(StandardAtmosphere, atm, "Standard atmosphere", 101_325.0, BigFloat(101_325), Pa,
0.0, BigFloat(0), "CODATA 2022")
@constant(SpeedOfLightInVacuum, c_0, "Speed of light in vacuum", 299_792_458.0,
BigFloat(299_792_458.0), m / s, 0.0, BigFloat(0), "CODATA 2022")
@constant(VacuumMagneticPermeability, µ_0, "Vacuum magnetic permeability", 1.256_637_061_27e-6,
BigFloat(1_256_637_061_27)/BigFloat(100_000_000_000_000_000), N * A^-2, 2.0e-16,
BigFloat(20)/BigFloat(100_000_000_000_000_000), "CODATA 2022")
@constant(VacuumElectricPermittivity, ε_0, "Vacuum electric permittivity", 8.854_187_818_8e-12,
BigFloat(8_854_187_818_8)/BigFloat(10_000_000_000_000_000_000_000), F * m^-1,
1.4e-21, BigFloat(14)/BigFloat(10_000_000_000_000_000_000_000), "CODATA 2022")
@constant(ElementaryCharge, e, "Elementary charge", 1.602_176_634e-19,
BigFloat(1_602_176_634)/BigFloat(10_000_000_000_000_000_000_000_000_000),
C, 0.0, BigFloat(0.0), "CODATA 2022")
@constant(NewtonianConstantOfGravitation, G, "Newtonian constant of gravitation",
6.674_30e-11, BigFloat(6_674_30)/BigFloat(10_000_000_000_000_000), m^3 * kg^-1 * s^-2,
1.5e-15, BigFloat(15)/BigFloat(10_000_000_000_000_000), "CODATA 2022")
@constant(StandardAccelerationOfGravitation, g_n, "Standard acceleration of gravitation",
9.806_65, BigFloat(9_806_65)/BigFloat(100_000), m * s^-2, 0, 0, "CODATA 2022")
@constant(PlanckConstant, h, "Planck constant", 6.626_070_15e-34,
BigFloat(6_626_070_15)/BigFloat(1_000_000_000_000_000_000_000_000_000_000_000_000_000_000),
J * s, 0.0, BigFloat(0.0), "CODATA 2022")
@derived_constant(ReducedPlanckConstant, ħ, "Reduced Planck constant",
convert(Float64, ustrip(big(h))/(2 * big(pi))),
ustrip(big(h))/(2 * big(pi)), J * s,
measurement(h)/2pi, measurement(BigFloat, h)/(2 * big(pi)), "CODATA 2022")
@constant(BoltzmannConstant, k_B, "Boltzmann constant", 1.380_649e-23,
BigFloat(1_380_649)/BigFloat(100_000_000_000_000_000_000_000_000_000), J * K^-1,
0.0, BigFloat(0.0), "CODATA 2022")
@constant(BohrMagneton, µ_B, "Bohr magneton", 9.274_010_0657e-24,
BigFloat(9_274_010_065_7)/BigFloat(10_000_000_000_000_000_000_000_000_000_000_000),
J * T^-1, 2.9e-33,
BigFloat(29)/BigFloat(100_00_000_000_000_000_000_000_000_000_000_000), "CODATA 2022")
@constant(ElectronMass, m_e, "Electron mass", 9.109_383_713_9e-31,
BigFloat(9_109_383_713_9)/BigFloat(100_000_000_000_000_000_000_000_000_000_000_000_000_000),
kg, 2.8e-40,
BigFloat(28)/BigFloat(100_000_000_000_000_000_000_000_000_000_000_000_000_000),
"CODATA 2022")
@constant(NeutronMass, m_n, "Neutron mass", 1.674_927_500_56e-27,
BigFloat(1_674_927_500_56)/BigFloat(100_000_000_000_000_000_000_000_000_000_000_000_000),
kg, 8.5e-37,
BigFloat(85)/BigFloat(100_000_000_000_000_000_000_000_000_000_000_000_000),
"CODATA 2022")
@constant(ProtonMass, m_p, "Proton mass", 1.672_621_925_95e-27,
BigFloat(1_672_621_925_95)/BigFloat(100_000_000_000_000_000_000_000_000_000_000_000_000),
kg, 5.2e-37,
BigFloat(52)/BigFloat(100_000_000_000_000_000_000_000_000_000_000_000_000),
"CODATA 2022")
@constant(AtomicMassConstant, m_u, "Atomic mass constant", 1.660_539_068_92e-27,
BigFloat(1_660_539_068_92)/BigFloat(100_000_000_000_000_000_000_000_000_000_000_000_000),
kg, 5.2e-37,
BigFloat(52)/BigFloat(100_000_000_000_000_000_000_000_000_000_000_000_000),
"CODATA 2022")
@constant(AvogadroConstant, N_A, "Avogadro constant", 6.022_140_76e23,
BigFloat(6_022_140_760_000_000_000_000_00), mol^-1,
0.0, BigFloat(0.0), "CODATA 2022")
@derived_constant(MolarGasConstant, R, "Molar gas constant",
convert(Float64, ustrip(big(N_A) * big(k_B))),
ustrip(big(N_A) * big(k_B)), J * mol^-1 * K^-1,
measurement(N_A) * measurement(k_B),
measurement(BigFloat, N_A) * measurement(BigFloat, k_B), "CODATA 2022")
@constant(RydbergConstant, R_∞, "Rydberg constant", 10_973_731.568_157,
BigFloat(10_973_731_568_157)/BigFloat(1_000_000), m^-1,
1.2e-5, BigFloat(12)/BigFloat(1_000_000), "CODATA 2022")
@derived_constant(StefanBoltzmannConstant, σ, "Stefan-Boltzmann constant",
convert(Float64, ustrip(2 * big(pi)^5 * big(k_B)^4 / (15 * big(h)^3 * big(c_0)^2))),
ustrip(2 * big(pi) ^ 5 * big(k_B) ^ 4 / (15 * big(h) ^ 3 * big(c_0) ^ 2)), W * m^-2 * K^-4,
(2 * pi ^ 5 * measurement(k_B) ^ 4) / (15 * measurement(h) ^ 3 * measurement(c_0) ^ 2),
(2 * big(pi) ^ 5 * measurement(BigFloat, k_B) ^ 4) / (15 * measurement(BigFloat, h) ^ 3 * measurement(BigFloat, c_0) ^ 2),
"CODATA 2022")
@constant(ThomsonCrossSection, σ_e, "Thomson cross section", 6.652_458_705_1e-29,
BigFloat(6_652_458_705_1)/BigFloat(1000_000_000_000_000_000_000_000_000_000_000_000_000),
m^2, 6.2e-38,
BigFloat(62)/BigFloat(1000_000_000_000_000_000_000_000_000_000_000_000_000),
"CODATA 2022")
@derived_constant(WienWavelengthDisplacementLawConstant, b, "Wien wavelength displacement law constant",
convert(Float64, ustrip(big(h) * big(c_0) / (_λx(BigFloat) * big(k_B)))),
ustrip(big(h) * big(c_0) / (_λx(BigFloat) * big(k_B))), m * K,
measurement(h) * measurement(c_0) / (_λx0 * measurement(k_B)),
measurement(BigFloat, h) * measurement(BigFloat, c_0) / (_λx(BigFloat) * measurement(BigFloat, k_B)),
"CODATA 2022")
@derived_constant(WienFrequencyDisplacementLawConstant, b′, "Wien frequency displacement law constant",
convert(Float64, ustrip(_νx(BigFloat) * big(k_B) / big(h))),
ustrip(_νx(BigFloat) * big(k_B) / big(h)), Hz / K,
_νx0 * measurement(k_B) / measurement(h),
_νx(BigFloat) * measurement(BigFloat, k_B) / measurement(BigFloat, h), "CODATA 2022")
@constant(CharacteristicImpedanceOfVacuum, Z_0, "Characteristic impedance of vacuum",
376.730_313_412, BigFloat(376_730_313_412)/BigFloat(1_000_000_000), Ω, 5.9e-8,
BigFloat(59) / BigFloat(1_000_000_000), "CODATA 2022")

end # module CODATA2022
Loading
Loading