Hi!
With one of the latest Julia updates I got the following warning while running my code:
Warning: Using arrays or dicts to store parameters of different types can hurt performance.
│ Consider using tuples instead.
I figured out quickly that the warning has its origin in the parameters that I have associated with my ODE.
I created the following MWE to demonstrate the behaviour of my code:
using DifferentialEquations
function pendulum!(u̇, u, p, t)
m = 1.0
g = 9.81
l = p[1]
M = p[3]
u̇[1] = u[2]
u̇[2] = -3g / (2l) * sin(u[1]) + 3 / (m * l^2) * M(t)
end
function ctrl_f!( integrator)
t = integrator.t
if t ≥ 5.0
A = integrator.p[2]
M_new(t) = 2A * sin(t)
integrator.p[3] = M_new
#integrator.p = (integrator.p[1:2]..., M_new) # this doen't work
end
u_modified!( integrator, false)
end
rhs = ODEFunction( pendulum!)
u₀ = [0.01, 0.0]
t_0, t_f = 0.0, 10.0
t = t_0:0.1:t_f
l = 1.0
A = 0.1
M(t) = A * sin(t)
ode_paras = [l, A, M]
#ode_paras = (l, A, M) # this doen't work
cb = PresetTimeCallback( t[2:end], ctrl_f!)
prob = ODEProblem( rhs, u₀, (t_0, t_f), ode_paras, callback=cb)
sol = solve( prob)
As you can see the problem here is not simply solved by using a tuple instead of an array. The main reason for this is that i need to change the parameters during the integration process of my ODE solver which can be done using arrays with integrator.p[3] = M_new
. However, switching from an array to a tuple in order to avoid the warning above changing the parameters by integrator.p = (integrator.p[1:2]..., M_new)
yields the following error:
ERROR: MethodError: Cannot
convert
an object of type var"#M_new#7"{Float64} to an object of type typeof(M)
So my question is: What is the recommened way when you want to get rid of this performance warning? So how can I use tuples here effectively?
Please note: I am aware of the fact that for this MWE there would be absolutely no need to use a PresetTimeCallback
and change the parameters. The easiest way would be to construct a suitable piecewise function M
and work without a callback. But I did that for the illustration of my acutal, of course, way more complex problem.