In a certain program, I need to cater with a model M :=(omega, eos)
, where omega:=(omega_1,... omega_P)
and eos:=(eos_1,..., eos_Q)
are “lists” of P
and Q
real parameters, with P
known in advance (at compile time), but Q
only at run time. The whole model M
or its “component lists”, omega
or eos
, by themselves, appear in several functions, either as explicit or implicit arguments.
In fact, I have, at least, two physically well-motivated situations where either Q
is 2 or 3; thus I thought of defining Julia struct
’s such that:
using Parameters
@with_kw struct Omega{T<;Real}
omega_1 = 70.0
...
omega_P = 0.7
end
@with_kw Eos1{T<:Real}
w_0 = -1.0
w_a = 0.0
end
@with_kw Eos2{T<:Real}
A = 0.5
z_f = -1.9
z_t = 1.7
end
Eos = Union(Eos1, Eos2)
#and finally:
@with_kw struct ModelM
omega::Omega
eos::Eos
end
With these types, I hope to be able to easily define my functions such that there are not too many explicit called arguments and, at the same time, I am still able to use them conveniently for _integration, optimization, plotting, etc, on the parameters above (omega_1,…,omega_P, w_0, w_a, A, z_f, z_t), by choosing conveniently the fields of the different struct
’s thus defined, via @unpack
and/or Ref
.
Question 1: Does this seem to be a good strategy?
Explicitly, sometimes I will have functions such as;
function distance(z, m::ModelM)
#function body
end
Question 2: Should I explicitly indicate the type for m
? For me, it seems to help legibility a bit, but I wonder about performance…
Some other times, my functions are just explicit functions of only eos::Eos, not of omega::Omega, such as:
function piezoenergeticratio(z, omega)
# function body
end
Finally,
Question 3: I wonder whether sometimes it would be cumbersome to access the nested struct
fields and whether an altogether distinct approach would be wiser, declaring the functions with all explicit arguments (with variable number of them and somehow “flattened”). Is this the case?
Any pointers for documentation or packages, relevant corresponding comments or general coding advice are deeply appreciated!