Skip to content

Commit 2f92e5b

Browse files
authored
introduce PhysicalConstants and new energy units (#257)
1 parent 221e70c commit 2f92e5b

File tree

3 files changed

+69
-20
lines changed

3 files changed

+69
-20
lines changed

docs/src/api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ QuantumToolbox.versioninfo
239239
QuantumToolbox.about
240240
gaussian
241241
n_thermal
242+
PhysicalConstants
242243
convert_unit
243244
row_major_reshape
244245
meshgrid

src/utilities.jl

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ Utilities:
33
internal (or external) functions which will be used throughout the entire package
44
=#
55

6-
export gaussian, n_thermal, convert_unit
6+
export gaussian, n_thermal
7+
export PhysicalConstants, convert_unit
78
export row_major_reshape, meshgrid
89

910
@doc raw"""
@@ -48,42 +49,77 @@ function n_thermal(ω::T1, ω_th::T2) where {T1<:Real,T2<:Real}
4849
return _FType(promote_type(T1, T2))(n)
4950
end
5051

51-
# some fundamental physical constants and common energy units
52-
const _e = 1.602176565e-19 # elementary charge (C)
53-
const _kB = 1.3806488e-23 # Boltzmann constant (J/K)
54-
const _h = 6.62607015e-34 # Planck constant (J⋅s)
52+
@doc raw"""
53+
const PhysicalConstants
54+
55+
A `NamedTuple` which stores some constant values listed in [*CODATA recommended values of the fundamental physical constants: 2022*](https://physics.nist.gov/cuu/pdf/wall_2022.pdf)
56+
57+
The current stored constants are:
58+
- `c` : (exact) speed of light in vacuum with unit ``[\textrm{m}\cdot\textrm{s}^{-1}]``
59+
- `G` : Newtonian constant of gravitation with unit ``[\textrm{m}^3\cdot\textrm{kg}^{−1}\cdot\textrm{s}^{−2}]``
60+
- `h` : (exact) Planck constant with unit ``[\textrm{J}\cdot\textrm{s}]``
61+
- `ħ` : reduced Planck constant with unit ``[\textrm{J}\cdot\textrm{s}]``
62+
- `e` : (exact) elementary charge with unit ``[\textrm{C}]``
63+
- `μ0` : vacuum magnetic permeability with unit ``[\textrm{N}\cdot\textrm{A}^{-2}]``
64+
- `ϵ0` : vacuum electric permittivity with unit ``[\textrm{F}\cdot\textrm{m}^{-1}]``
65+
- `k` : (exact) Boltzmann constant with unit ``[\textrm{J}\cdot\textrm{K}^{-1}]``
66+
- `NA` : (exact) Avogadro constant with unit ``[\textrm{mol}^{-1}]``
67+
68+
# Examples
69+
70+
```
71+
julia> PhysicalConstants.ħ
72+
1.0545718176461565e-34
73+
```
74+
"""
75+
const PhysicalConstants = (
76+
c = 299792458.0,
77+
G = 6.67430e-11,
78+
h = 6.62607015e-34,
79+
ħ = 6.62607015e-34 / (2 * π),
80+
e = 1.602176634e-19,
81+
μ0 = 1.25663706127e-6,
82+
ϵ0 = 8.8541878188e-12,
83+
k = 1.380649e-23,
84+
NA = 6.02214076e23,
85+
)
86+
87+
# common energy units (the values below are all in the unit of Joule)
5588
const _energy_units::Dict{Symbol,Float64} = Dict(
56-
# the values below are all in the unit of Joule
5789
:J => 1.0,
58-
:eV => _e,
59-
:meV => 1e-3 * _e,
60-
:GHz => 1e9 * _h,
61-
:mK => 1e-3 * _kB,
90+
:eV => PhysicalConstants.e,
91+
:meV => 1.0e-3 * PhysicalConstants.e,
92+
:MHz => 1.0e6 * PhysicalConstants.h,
93+
:GHz => 1.0e9 * PhysicalConstants.h,
94+
:K => PhysicalConstants.k,
95+
:mK => 1.0e-3 * PhysicalConstants.k,
6296
)
6397

6498
@doc raw"""
6599
convert_unit(value::Real, unit1::Symbol, unit2::Symbol)
66100
67-
Convert the energy `value` from `unit1` to `unit2`.
101+
Convert the energy `value` from `unit1` to `unit2`. The `unit1` and `unit2` can be either the following `Symbol`:
102+
- `:J` : Joule
103+
- `:eV` : electron volt
104+
- `:meV` : milli-electron volt
105+
- `:MHz` : Mega-Hertz multiplied by Planck constant ``h``
106+
- `:GHz` : Giga-Hertz multiplied by Planck constant ``h``
107+
- `:K` : Kelvin multiplied by Boltzmann constant ``k``
108+
- `:mK` : milli-Kelvin multiplied by Boltzmann constant ``k``
68109
69-
Note that `unit1` and `unit2` can be either the following `Symbol`:
70-
- `:J`: Joule
71-
- `:eV`: electron volt.
72-
- `:meV`: milli-electron volt.
73-
- `:GHz`: Giga-Hertz multiplied by Planck constant ``h``.
74-
- `:mK`: milli-Kelvin multiplied by Boltzmann constant ``k_{\textrm{B}}``.
110+
Note that we use the values stored in [`PhysicalConstants`](@ref) to do the conversion.
75111
76112
# Examples
77113
78114
```
79115
julia> convert_unit(1, :eV, :J)
80-
1.602176565e-19
116+
1.602176634e-19
81117
82118
julia> convert_unit(1, :GHz, :J)
83119
6.62607015e-25
84120
85121
julia> convert_unit(1, :meV, :mK)
86-
11604.51930280894
122+
11604.518121550082
87123
```
88124
"""
89125
function convert_unit(value::T, unit1::Symbol, unit2::Symbol) where {T<:Real}

test/core-test/utilities.jl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,21 @@
1414
@test typeof(n_thermal(Float32(2), Float64(3))) == Float64
1515
end
1616

17+
@testset "CODATA Physical Constants" begin
18+
c = PhysicalConstants.c
19+
h = PhysicalConstants.h
20+
ħ = PhysicalConstants.ħ
21+
μ0 = PhysicalConstants.μ0
22+
ϵ0 = PhysicalConstants.ϵ0
23+
24+
@test h / ħ 2 * π
25+
@test μ0 / (4e-7 * π) 1.0
26+
@test c^2 * μ0 * ϵ0 1.0
27+
end
28+
1729
@testset "convert unit" begin
1830
V = 100 * rand(Float64)
19-
_unit_list = [:J, :eV, :meV, :GHz, :mK]
31+
_unit_list = [:J, :eV, :meV, :MHz, :GHz, :K, :mK]
2032
for origin in _unit_list
2133
for middle in _unit_list
2234
for target in _unit_list

0 commit comments

Comments
 (0)