@@ -3,7 +3,7 @@ Utilities:
33 internal (or external) functions which will be used throughout the entire package
44=#
55
6- export gaussian, n_thermal
6+ export gaussian, n_thermal, convert_unit
77export row_major_reshape, meshgrid
88
99@doc raw """
@@ -48,6 +48,50 @@ function n_thermal(ω::T1, ω_th::T2) where {T1<:Real,T2<:Real}
4848 return _FType (promote_type (T1, T2))(n)
4949end
5050
51+ # some fundamental physical constants and common energy units
52+ const _e = 1.602176634e-19 # elementary charge (C)
53+ const _kB = 1.3806488e-23 # Boltzmann constant (J/K)
54+ const _h = 6.62607015e-34 # Planck constant (J⋅s)
55+ const _energy_units:: Dict{Symbol,Float64} = Dict (
56+ # the values below are all in the unit of Joule
57+ :J => 1.0 ,
58+ :eV => _e,
59+ :meV => 1e-3 * _e,
60+ :GHz => 1e9 * _h,
61+ :mK => 1e-3 * _kB,
62+ )
63+
64+ @doc raw """
65+ convert_unit(value::Real, unit1::Symbol, unit2::Symbol)
66+
67+ Convert the energy `value` from `unit1` to `unit2`.
68+
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_{\t extrm{B}}``.
75+
76+ # Examples
77+
78+ ```
79+ julia> convert_unit(1, :eV, :J)
80+ 1.602176634e-19
81+
82+ julia> convert_unit(1, :GHz, :J)
83+ 6.62607015e-25
84+
85+ julia> convert_unit(1, :meV, :mK)
86+ 11604.519802573976
87+ ```
88+ """
89+ function convert_unit (value:: T , unit1:: Symbol , unit2:: Symbol ) where {T<: Real }
90+ ! haskey (_energy_units, unit1) && throw (ArgumentError (" Invalid unit :$(unit1) " ))
91+ ! haskey (_energy_units, unit2) && throw (ArgumentError (" Invalid unit :$(unit2) " ))
92+ return _FType (T)(value * (_energy_units[unit1] / _energy_units[unit2]))
93+ end
94+
5195_get_dense_similar (A:: AbstractArray , args... ) = similar (A, args... )
5296_get_dense_similar (A:: AbstractSparseMatrix , args... ) = similar (nonzeros (A), args... )
5397
0 commit comments