Differentiation of expressions involving arrays

Hi. I’m a ModellingToolkit newbie (and DE in general).
I wrote some code last December which now throws the error Differentiation of expressions involving arrays and array variables is not yet supported.
I saw that raising the error has been introduced in this pull request 530, and I have a couple of simple ways to avoid it… but I don’t have much understanding of what’s going on, so I would like some feedback.

Here is what I want to do.
I have some arrays of various lengths v_1, ..., v_n, and I want to define an ODE system whose equations are of the form \frac d{dt} f_i(t)= g_i(f_i(t)) such that g_i(x) = f(x, v_i) for some f.
Back in December it seemed that I could indeed register only f and achieve my goal as in this MWE, but now it raises the error mentioned above.

using ModelingToolkit, DifferentialEquations

function f(x, y)
sum(y)*x
return x
end
@register f(x, y::AbstractVector)
@variables t h(t) w(t) k(t)

a = rand(10)
b = rand(5)
D = Differential(t)
eqs = [
D(h) ~ f(w, a)
D(w) ~ f(k, b)
D(k) ~ 1
]
@named sys = ODESystem(eqs)
sys = structural_simplify(sys)
u0 = [h => 1, w => 1, k => 1]
tspan = (0.0, 1.0)
prob = ODEProblem(sys, u0, tspan, jac=true)
sol = solve(prob)


No error is issued if either

• AbstractVector is replaced with NTuple or
• We remove the arg jac=true from ODEProblem.

Are both solutions fine?
As a side question (probably worth a separate thread?), I also had an hard time finding information about what jac=true means in the documentation. The most relevant page seems to be this one, and it seems to be that jac=false is equivalent to jac=nothing but I don’t get what jac=true means.

I also just updated my packages including Symbolics and now get the same error on similar code (ODEProblem construction from a ODESystem using jac=true). I can confirm that leaving out jac=true makes the error disappear.

Regarding your question: As far as I know (which is not much, just starting with all of this), jac = true will trigger symbolic construction of the Jacobian and hence is quite essential for largeish problems.

Is the error appearing since the Symbolics.jl update a regression or a desired assertion? My code too ran fine (as far as I can tell) before that update.

@shashi knows about this and is working on it. The issue is that differentiation with arrays of variables had some correctness issues, but the error message may have been placed in a way that generates too many false positives.

There is an according issue on Symbolics.jl.

Should be fixed now.

1 Like