Reading input parameters from a text file

I don’t have any advice on how to store or parse parameters from text files, but if the number of parameters you need to read aren’t too large, it may be better to store them in either a custom struct (if only the values of the parameters change, and not the set of parameters itself) or a NamedTuple. This would entail writing either:

# Struct
@kwdef struct Params
    num1::Int = 10
    num2::Float64 = 14.6
    num3::Float64 =  1e20
    bool1::Bool = true
    bool2::Bool =  false
    option1::String = "string1"
end

params = Params() # Default values
params = Params(num1 = 3) # Change one of the values 

# Named Tuple 
params = (;
    num1 = 10,
    num2 = 14.6,
    num3 = 1e20,
    bool1 = true,
    bool2 = false,
    option1 = "string1"
)

If you do this, you can use destructuring by name inside functions, which can be handy for carrying out computations with the parameters:

function my_function(x, params)
    (; num1, num2, num3) = params 
    x + num1*num2*num3
end

This may also yield some performance benefits as the Dict you have above carries a bunch of different types, which means Julia will not typically be able to infer types when using it.

5 Likes