How to use units in Cosmology.jl

I think you’ll need to explicitly load Unitful.jl, which provides the @u_str macro used in your examples, as well as UnitfulAstro, which provides Gpc and other astro-relevant units.

julia> using Cosmology, Unitful, UnitfulAstro

julia> c = cosmology()
Cosmology.FlatLCDM{Float64}(0.69, 0.7099122024007928, 0.29, 8.779759920715362e-5)

julia> hubble_dist(u"km", c, 0)
1.3406708213779601e23 km

julia> hubble_dist(u"Gpc", c, 0)
4.34481823188406 Gpc

You can also import the units from the internals of Cosmology.jl, but there are no guarantees of stability when using a package’s internals.

julia> import Cosmology: km, Mpc

julia> hubble_dist(km, c, 0)
1.3406708213779598e23 km

I opened an issue to help clarify this.

2 Likes