# Optimal approach to simulating large system of time-dependent ODEs?

**URL:** <https://discourse.julialang.org/t/optimal-approach-to-simulating-large-system-of-time-dependent-odes/63709>\
**Category:** Performance\
**Tags:** diffeq\
**Created:** [June 28, 2021, 5:23pm UTC](https://discourse.julialang.org/t/optimal-approach-to-simulating-large-system-of-time-dependent-odes/63709 "2021-06-28T17:23:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kharing](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kharing/32/26578_2.png) [@kharing](https://discourse.julialang.org/u/kharing)\
**Post date:** [June 28, 2021, 5:23pm UTC](https://discourse.julialang.org/t/optimal-approach-to-simulating-large-system-of-time-dependent-odes/63709/1 "2021-06-28T17:23:22Z")

</div>

I’m looking to solve a large (N ~ 1000) system of 1st order, linear ODEs with time-dependent coefficients,  
\frac{d u}{d t} = A(t) \cdot u(t).  
Because both the numerical constants involved in the coefficients and the number of equations N are not fixed, I have to generate them on-the-fly, using Symbolics.jl to generate a function to return the time-varying coefficient matrix A(t).  
I have a MWE to do so:

```julia
using DifferentialEquations, Symbolics, BenchmarkTools
@variables x
N = 10;

f = zeros(Num, N,N);
for i in 1:N
    for j in 1:i
        f[i, j] = cos((i-j) * x)
        f[j, i] = cos((i-j) * x)
    end
end

h = eval(build_function(f, x, parallel=Symbolics.MultithreadedForm())[2]);

A0 = zeros(ComplexF64, N,N);
function ih(A, u, p, t)
    h(A, t)
    A .*= im
end
ih(A0, nothing, nothing, 0);
A = DiffEqArrayOperator(A0, update_func=ih);
prob = ODEProblem(A, ones(ComplexF64, N), (0., 10.));
@btime solve(prob, saveat = 0.1, abstol = 1e-6, reltol = 1e-6, maxiters=1e8)

```

which apparently evaluates in times on the order of ms

```julia
# N = 10
@btime solve(prob, saveat = 0.1, abstol = 1e-6, reltol = 1e-6, maxiters=1e8)
  5.370 ms (22878 allocations: 3.24 MiB)

# N = 20
@btime solve(prob, saveat = 0.1, abstol = 1e-6, reltol = 1e-6, maxiters=1e8)
  20.012 ms (38128 allocations: 4.68 MiB)

```

The time seems to increase quadratically with N, at least for small N. Are there any optimizations I can make to speed this up?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [June 28, 2021, 10:39pm UTC](https://discourse.julialang.org/t/optimal-approach-to-simulating-large-system-of-time-dependent-odes/63709/2 "2021-06-28T22:39:00Z")

</div>

Turn off multithreading. That is way too small for multithreading to make sense and will only slow it down.
