Where to put a bunch of constants?

Hi everyone.
I am developing a set of scripts for geophysics in Julia and at the core of many of my codes I have a bunch of values (maybe they can be consider Julia Constants at some point) but in general they are physical constants (like the Earth’s mean Radius or the Core’s Mass).

Now, many of the users won’t bother changing this, but someone might. Is there a good way to place them in a file for easy modification? What would you recommend?

Some are available in
https://juliaphysics.github.io/PhysicalConstants.jl/stable/constants/

Scripts bad, modules good.

I recommend against such speculative generalization. If you must, because you think the users might try to operate on other celestial bodies, perhaps your functions can accept a Planet parameter, and the module would provide a function to return an earth instance of Planet. The functions may even make the planet parameter default to earth.

1 Like

One way to structure it, without relying on global constants:

julia> module MyPackage
           export myfunction, MyConstants
           Base.@kwdef struct MyConstants
               a::Int=10
               b::Float64=1e-3
           end
           function myfunction(x;constants=MyConstants())
               (;a,b) = constants
               return a*x^b
           end
       end
Main.MyPackage

julia> using .MyPackage

julia> myfunction(5.0)
10.016107337527293

julia> myfunction(5.0,constants=MyConstants(b=1e-2))
10.162245912673256


4 Likes