Skip to content

Commit

Permalink
Add PhysicalConstants evaluation to @ufac_str
Browse files Browse the repository at this point in the history
Now we have two sets of Physical constants accessible this way:
 Na,q,k etc. from Unitful (didn't see them before)
 N_A,e, k_B from PhysicalConstants.CODATA2018

The former are buggy under Julia 1.8, see
PainterQubits/Unitful.jl#545

Also, this taps now into the deeper mechanisms of Unitful. Look like a rabbit hole...
  • Loading branch information
j-fu committed Jul 20, 2022
1 parent f2563e6 commit 94335a1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "LessUnitful"
uuid = "f29f6376-6e90-4d80-80c9-fb8ec61203d5"
authors = ["Jürgen Fuhrmann <[email protected]>"]
version = "0.4.0"
version = "0.5.0"

[deps]
PhysicalConstants = "5ad8b20f-a522-5ce9-bfc9-ddf1d5bda6ab"
Expand Down
30 changes: 29 additions & 1 deletion src/LessUnitful.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ unitfactor(u) = unitfactor(Unitful,u)
unitfactor(U::Module,u::Unitful.FreeUnits) = unitfactor(U,1u)
unitfactor(U::Module,quantity) = U.float(U.ustrip(U.upreferred(quantity)))

function _ufac_str(x)
Unitful=getproperty(@__MODULE__,:Unitful)
PhysicalConstants=getproperty(@__MODULE__,:PhysicalConstants)
code = Expr(:block)
push!(code.args, :(unitfactor($(Unitful),eval($(Unitful).lookup_units(($(PhysicalConstants).CODATA2018,$(Unitful)), Meta.parse($x))))))
code
end

"""
@ufac_str
Expand All @@ -54,14 +62,35 @@ String macro for calculating the unit factor of a quantity, see also [`unitfacto
julia> ufac"1mV"
0.001
```
This macro allows as wellto obtatin unit factors from physical constants from [PhysicalConstants.CODATA2018](https://juliaphysics.github.io/PhysicalConstants.jl/stable/constants/#CODATA2018-1)
```jldoctest
julia> ufac"N_A"
6.02214076e23
```
```jldoctest
julia> ufac"N_A*e"
96485.33212331001
```
"""
macro ufac_str(x)
esc(_ufac_str(x))
end



macro xufac_str(x)
Unitful=getproperty(@__MODULE__,:Unitful)
quote
unitfactor($(Unitful),$(Unitful).@u_str($(x)))
end
end



function _unitfactors(xs...)
Unitful=getproperty(@__MODULE__,:Unitful)
code = Expr(:block)
Expand Down Expand Up @@ -295,7 +324,6 @@ macro local_phconstants(xs...)
esc(_local_phconstants(xs...))
end


export @phconstants, @local_phconstants

end # module LessUnitful
Expand Down

0 comments on commit 94335a1

Please sign in to comment.