How to use units in Cosmology.jl

I have installed and now am trying to use the Cosmology.jl package.

When I try, from a recently launched Julia REPL:

using Cosmology
c = cosmology()

I get the expected output: 434.818231884059 Mpc. However, right after, when I try:

hubble_dist(km, c, 0)
hubble_dist(u"km", c, 0)
hubble_dist(u"Gpc", c, 0)

there is always an error with UndefVarError, either with km or Gpc not defined or rather @u_str not defined. I simply could not manage to use distinct units from the default ones. What am I doing wrong?

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.


@stillyslalom Thank you; it worked like a charm, now.