Skip to content

Commit a740c6f

Browse files
committed
Merge tag 'v0.6.0' into weak-floats
[Diff since v0.5.0](v0.5.0...v0.6.0) **Closed issues:** - Physical constants (#26) **Merged pull requests:** - Create (1) physical constants, and (2) symbolic dimensions object (#32) (@MilesCranmer)
2 parents 943c748 + 89bc4e9 commit a740c6f

File tree

15 files changed

+768
-84
lines changed

15 files changed

+768
-84
lines changed

.github/workflows/CI.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@ jobs:
3333
- uses: julia-actions/cache@v1
3434
- uses: julia-actions/julia-buildpkg@v1
3535
- name: "Run tests"
36+
shell: bash
3637
run: |
3738
julia --color=yes --project=. -e 'import Pkg; Pkg.add("Coverage")'
3839
julia --color=yes --threads=auto --check-bounds=yes --depwarn=yes --code-coverage=user --project=. -e 'import Pkg; Pkg.test(coverage=true)'
3940
DQ_TEST_UPREFERRED=true julia --color=yes --threads=auto --check-bounds=yes --depwarn=yes --code-coverage=user --project=. -e 'import Pkg; Pkg.test(coverage=true)'
4041
julia --color=yes --project=. coverage.jl
4142
- name: "Coveralls"
4243
uses: coverallsapp/github-action@v2
44+
if: matrix.version == '1'
4345
with:
4446
github-token: ${{ secrets.GITHUB_TOKEN }}
4547
file: lcov.info

Project.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
name = "DynamicQuantities"
22
uuid = "06fc5a27-2a28-4c7c-a15d-362465fb6821"
33
authors = ["MilesCranmer <[email protected]> and contributors"]
4-
version = "0.5.0"
4+
version = "0.6.0"
55

66
[deps]
77
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
8+
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
89
Tricks = "410a4b4d-49e4-4fbc-ab6d-cb71b17b3775"
910

1011
[weakdeps]

docs/make.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ makedocs(;
4141
"Home" => "index.md",
4242
"Utilities" => "api.md",
4343
"Units" => "units.md",
44+
"Constants" => "constants.md",
45+
"Symbolic Units" => "symbolic_units.md",
4446
"Types" => "types.md",
4547
]
4648
)

docs/src/constants.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
# Units
3+
4+
Many common physical constants are available as well:
5+
6+
```@docs
7+
Constants.c
8+
Constants.h
9+
Constants.hbar
10+
Constants.e
11+
Constants.k_B
12+
Constants.N_A
13+
Constants.eV
14+
Constants.R
15+
Constants.F
16+
Constants.sigma_sb
17+
Constants.alpha
18+
Constants.u
19+
Constants.G
20+
Constants.mu_0
21+
Constants.eps_0
22+
Constants.m_e
23+
Constants.m_p
24+
Constants.m_n
25+
Constants.a_0
26+
Constants.k_e
27+
Constants.Ryd
28+
```
29+
30+
## Astronomical constants
31+
32+
```@docs
33+
Constants.M_earth
34+
Constants.M_sun
35+
Constants.M_jup
36+
Constants.R_earth
37+
Constants.R_jup
38+
Constants.R_sun
39+
Constants.L_sun
40+
Constants.L_bol0
41+
Constants.sigma_T
42+
Constants.au
43+
Constants.pc
44+
Constants.ly
45+
Constants.atm
46+
```

docs/src/symbolic_units.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Symbolic Dimensions
2+
3+
Whereas `u"..."` will automatically convert all units to the same
4+
base SI units, `us"..."` will not. This uses the `SymbolicDimensions`
5+
type, which is a subtype of `AbstractDimensions` that stores the
6+
dimensions symbolically. This is useful for keeping track of the
7+
original units and constants in a user-entered expression.
8+
9+
The two main functions for working with symbolic
10+
units are `sym_uparse` and `us_str`:
11+
12+
```@docs
13+
@us_str
14+
sym_uparse
15+
```
16+
17+
To convert a quantity to its regular base SI units, use `expand_units`:
18+
19+
```@docs
20+
expand_units
21+
```

docs/src/types.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,10 @@ AbstractQuantity
1515
```
1616

1717
Note also that the `Quantity` object can take a custom `AbstractDimensions`
18-
as input, so there is often no need to subtype `AbstractQuantity` separately.
18+
as input, so there is often no need to subtype `AbstractQuantity` separately.
19+
20+
Another type which subtypes `AbstractDimensions` is `SymbolicDimensions`:
21+
22+
```@docs
23+
SymbolicDimensions
24+
```

docs/src/units.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ in a namespace with all the units available.
1616

1717
```@docs
1818
Units.m
19-
Units.g
19+
Units.kg
2020
Units.s
2121
Units.A
2222
Units.K
2323
Units.cd
2424
Units.mol
2525
```
2626

27+
### Derived units
28+
2729
Several derived SI units are available as well:
2830

2931
```@docs
@@ -39,4 +41,4 @@ Units.Ω
3941
Units.T
4042
Units.L
4143
Units.bar
42-
```
44+
```

src/DynamicQuantities.jl

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
module DynamicQuantities
22

3+
export Units, Constants
34
export AbstractQuantity, AbstractDimensions
4-
export Quantity, Dimensions, DimensionError, ustrip, dimension, valid
5+
export Quantity, Dimensions, SymbolicDimensions, DimensionError
6+
export ustrip, dimension, valid
57
export ulength, umass, utime, ucurrent, utemperature, uluminosity, uamount
6-
export uparse, @u_str
8+
export uparse, @u_str, sym_uparse, @us_str, expand_units
79

810
include("fixed_rational.jl")
911
include("lazy_float.jl")
@@ -12,9 +14,14 @@ include("types.jl")
1214
include("utils.jl")
1315
include("math.jl")
1416
include("units.jl")
17+
include("constants.jl")
18+
include("uparse.jl")
19+
include("symbolic_dimensions.jl")
1520

1621
import Requires: @init, @require
17-
import .Units: uparse, @u_str, DEFAULT_UNIT_TYPE
22+
import .Units: DEFAULT_UNIT_TYPE
23+
import .Constants
24+
import .UnitsParse: uparse, @u_str
1825

1926
if !isdefined(Base, :get_extension)
2027
@init @require Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" include("../ext/DynamicQuantitiesUnitfulExt.jl")

src/constants.jl

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
module Constants
2+
3+
import ..DEFAULT_QUANTITY_TYPE
4+
import ..Quantity
5+
import ..Units as U
6+
import ..Units: _add_prefixes
7+
8+
const _CONSTANT_SYMBOLS = Symbol[]
9+
const _CONSTANT_VALUES = DEFAULT_QUANTITY_TYPE[]
10+
11+
macro register_constant(name, value)
12+
return esc(_register_constant(name, value))
13+
end
14+
15+
macro add_prefixes(base_unit, prefixes)
16+
@assert prefixes.head == :tuple
17+
return esc(_add_prefixes(base_unit, prefixes.args, _register_constant))
18+
end
19+
20+
function _register_constant(name::Symbol, value)
21+
s = string(name)
22+
return quote
23+
const $name = $value
24+
push!(_CONSTANT_SYMBOLS, Symbol($s))
25+
push!(_CONSTANT_VALUES, $name)
26+
end
27+
end
28+
29+
# Source: http://physics.nist.gov/constants (2018)
30+
31+
# Exact, base:
32+
@register_constant c 299792458 * U.m/U.s
33+
@register_constant h 6.62607015e-34 * U.J/U.Hz
34+
@register_constant hbar h / (2π)
35+
@register_constant e 1.602176634e-19 * U.C
36+
@register_constant k_B 1.380649e-23 * U.J/U.K
37+
@register_constant N_A 6.02214076e+23 / U.mol
38+
39+
@doc(
40+
"Speed of light in a vacuum. Standard.",
41+
c,
42+
)
43+
@doc(
44+
"Planck constant. Standard.",
45+
h,
46+
)
47+
@doc(
48+
"Reduced Planck constant (h/2π). Standard.",
49+
hbar,
50+
)
51+
@doc(
52+
"Elementary charge. Standard.",
53+
e,
54+
)
55+
@doc(
56+
"Boltzmann constant. Standard.",
57+
k_B,
58+
)
59+
@doc(
60+
"Avogadro constant. Standard.",
61+
N_A,
62+
)
63+
64+
# Exact, derived:
65+
@register_constant eV e * U.J/U.C
66+
@register_constant R N_A * k_B
67+
@register_constant F N_A * e
68+
@register_constant sigma_sb (π^2/60) * k_B^4/(hbar^3 * c^2)
69+
70+
@add_prefixes eV (m, k, M, G, T)
71+
72+
@doc(
73+
"Electron volt. Standard.",
74+
eV,
75+
)
76+
@doc(
77+
"Molar gas constant. Standard.",
78+
R,
79+
)
80+
@doc(
81+
"Faraday constant. Standard.",
82+
F,
83+
)
84+
@doc(
85+
"Stefan-Boltzmann constant. Standard.",
86+
sigma_sb,
87+
)
88+
89+
# Measured
90+
@register_constant alpha DEFAULT_QUANTITY_TYPE(7.2973525693e-3)
91+
@register_constant u 1.66053906660e-27 * U.kg
92+
@register_constant G 6.67430e-11 * U.m^3 / (U.kg * U.s^2)
93+
@register_constant mu_0 4π * alpha * hbar / (e^2 * c)
94+
@register_constant eps_0 8.8541878128e-12 * U.F/U.m
95+
@register_constant m_e 9.1093837015e-31 * U.kg
96+
@register_constant m_p 1.67262192369e-27 * U.kg
97+
@register_constant m_n 1.67492749804e-27 * U.kg
98+
@register_constant a_0 hbar/(m_e * c * alpha)
99+
@register_constant k_e 1/(4π * eps_0)
100+
@register_constant Ryd alpha^2 * m_e * c^2 / (2 * h)
101+
102+
@doc(
103+
"Fine-structure constant. Measured.",
104+
alpha,
105+
)
106+
@doc(
107+
"Atomic mass unit (1/12th the mass of Carbon-12). Measured.",
108+
u,
109+
)
110+
@doc(
111+
"Newtonian constant of gravitation. Measured.",
112+
G,
113+
)
114+
@doc(
115+
"Vacuum magnetic permeability. Measured.",
116+
mu_0,
117+
)
118+
@doc(
119+
"Vacuum electric permittivity. Measured.",
120+
eps_0,
121+
)
122+
@doc(
123+
"Electron mass. Measured.",
124+
m_e,
125+
)
126+
@doc(
127+
"Proton mass. Measured.",
128+
m_p,
129+
)
130+
@doc(
131+
"Neutron mass. Measured.",
132+
m_n,
133+
)
134+
@doc(
135+
"Bohr radius. Measured.",
136+
a_0,
137+
)
138+
@doc(
139+
"Coulomb constant (Note: SI units only!). Measured.",
140+
k_e,
141+
)
142+
@doc(
143+
"Rydberg frequency. Measured.",
144+
Ryd,
145+
)
146+
147+
# Astro constants.
148+
# Source: https://arxiv.org/abs/1510.07674
149+
150+
@register_constant M_earth 5.97216787e+24 * U.kg
151+
@register_constant M_sun 1.98840987e+30 * U.kg
152+
@register_constant M_jup 1.8981246e+27 * U.kg
153+
@register_constant R_earth 6.3781e+6 * U.m
154+
@register_constant R_jup 7.1492e+7 * U.m
155+
@register_constant R_sun 6.957e+8 * U.m
156+
@register_constant L_sun 3.828e+26 * U.W
157+
@register_constant L_bol0 3.0128e+28 * U.W
158+
@register_constant sigma_T 6.6524587321e-29 * U.m^2
159+
@register_constant au 149597870700 * U.m
160+
@register_constant pc (648000/π) * au
161+
@register_constant ly c * U.yr
162+
@register_constant atm 101325 * U.Pa
163+
164+
@add_prefixes pc (k, M, G)
165+
166+
@doc(
167+
"Earth mass. Measured.",
168+
M_earth,
169+
)
170+
@doc(
171+
"Solar mass. Measured.",
172+
M_sun,
173+
)
174+
@doc(
175+
"Jupiter mass. Measured.",
176+
M_jup,
177+
)
178+
@doc(
179+
"Nominal Earth equatorial radius. Standard.",
180+
R_earth,
181+
)
182+
@doc(
183+
"Nominal Jupiter equatorial radius. Standard.",
184+
R_jup,
185+
)
186+
@doc(
187+
"Nominal solar radius. Standard.",
188+
R_sun,
189+
)
190+
@doc(
191+
"Nominal solar luminosity. Standard.",
192+
L_sun,
193+
)
194+
@doc(
195+
"Standard luminosity at absolute bolometric magnitude 0. Standard.",
196+
L_bol0,
197+
)
198+
@doc(
199+
"Thomson scattering cross-section. Measured.",
200+
sigma_T,
201+
)
202+
@doc(
203+
"Astronomical unit. Standard.",
204+
au,
205+
)
206+
@doc(
207+
"Parsec. Standard.",
208+
pc,
209+
)
210+
@doc(
211+
"Light year. Standard.",
212+
ly,
213+
)
214+
@doc(
215+
"Standard atmosphere. Standard.",
216+
atm,
217+
)
218+
219+
"""A tuple of all possible constants."""
220+
const CONSTANT_SYMBOLS = Tuple(_CONSTANT_SYMBOLS)
221+
const CONSTANT_VALUES = Tuple(_CONSTANT_VALUES)
222+
const CONSTANT_MAPPING = NamedTuple([s => i for (i, s) in enumerate(CONSTANT_SYMBOLS)])
223+
224+
end

0 commit comments

Comments
 (0)