# Bad performance of DifferentialEquations.jl

**URL:** https://discourse.julialang.org/t/bad-performance-of-differentialequations-jl/60923
**Category:** New to Julia
**Tags:** question
**Created:** [May 11, 2021, 1:57am UTC](https://discourse.julialang.org/t/bad-performance-of-differentialequations-jl/60923 "2021-05-11T01:57:05Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![leonandonayre](https://avatars.discourse-cdn.com/v4/letter/l/da6949/32.png) [@leonandonayre](https://discourse.julialang.org/u/leonandonayre)
#### Post date: [May 11, 2021, 1:57am UTC](https://discourse.julialang.org/t/bad-performance-of-differentialequations-jl/60923/1 "2021-05-11T01:57:05Z")

</div>

I am trying to solve a ODE with ModelingToolkit.jl, unfortunately I have been waiting for the result for more than 11 hours.  
I would to use Multithread, or some CUDA package in order to increase this awful performance, but I am pretty new to this (not only to Julia).

> using ModelingToolkit, DifferentialEquations;  
> using Plots;  
> using DataFrames, CSV;
> 
> @parameters t ξ;  
> @variables φ(t) ψ(t);
> 
> N = 2;  
> n = 4;  
> V₀ = 0.002;  
> B = φ^N;  
> V = V₀ \* φ^n;
> 
> D = Differential(t);
> 
> dB = expand\_derivatives(D(B)/D(φ));  
> dV = expand\_derivatives(D(V)/D(φ));  
> ddB = expand\_derivatives(D(dB)/D(φ));  
> ddV = expand\_derivatives(D(dV)/D(φ));
> 
> H1 = (3 \* ξ \* ψ \* dB - sqrt(9 \* ξ^2 \* φ^2 \* dB^2 + (1 - 6 \* ξ \* B)\*(ψ^2 + 2 \* V)) )/(1 - 6 \* ξ \* B)
> 
> dψ = -3 \* H1 \* ψ +((3 \* ξ \* dB \*(1 - 3 \* ξ \* ddB))/(1 - 6 \* ξ \* B + 9 \*ξ ^2 \* dB^2)) \* ψ^2 - (dV + 6 \* ξ \* (2 \* V \* dB - dV \* B))/(1 - 6 \* ξ \* B + 9 \* ξ^2 \* dB^2)
> 
> eqs1 = [D(φ) ~ ψ,  
> D(ψ) ~ dψ]  
> sys1 = ODESystem(eqs1)
> 
> #green  
> u01 = [φ =\> 0.000,  
> ψ =\> 0.010]  
> p1 = [ξ =\> -1.0]  
> tspan1 = (0.0,150.0)  
> prob1 = ODEProblem(sys1,u01,tspan1,p1)  
> sol1 = solve(prob1,AutoTsit5(Rosenbrock23()))  
> df1 = DataFrame(sol1)  
> CSV.write(“greenxi-1fit0,010.csv”,df1)
> 
> plot(sol1,  
> vars = (t,φ),  
> title = “φ vs t”,  
> xaxis = “Time (t)”,  
> yaxis = “φ(t)”,  
> color = green,  
> legend = false)

---

<div class="post-metadata">

### Author: ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)
#### Post date: [May 11, 2021, 3:35am UTC](https://discourse.julialang.org/t/bad-performance-of-differentialequations-jl/60923/2 "2021-05-11T03:35:21Z")

</div>

There is a few reasons why your code is running slow. Mainly that all your variables are in the global scope and so the compiler can not generate efficient code for it. The first thing I’d do is wrap your entire code into a function. That should help a lot. Then consider reading the [Performance Tips](https://docs.julialang.org/en/v1/manual/performance-tips/).

That being said, I am not sure why that piece of code is taking more than 11 hours to run. What part of the code is getting hung?

Lastly, please provide julia version `versioninfo()` and package versions by `julia> ] st`

---

<div class="post-metadata">

### Author: ![leonandonayre](https://avatars.discourse-cdn.com/v4/letter/l/da6949/32.png) [@leonandonayre](https://discourse.julialang.org/u/leonandonayre)
#### Post date: [May 11, 2021, 3:51am UTC](https://discourse.julialang.org/t/bad-performance-of-differentialequations-jl/60923/3 "2021-05-11T03:51:54Z")

</div>

> versioninfo():

> Commit 6aaedec (2021-04-23 05:59 UTC)  
> Platform Info:  
> OS: Linux (x86\_64-redhat-linux)  
> CPU: Intel(R) Core™ i7-10750H CPU @ 2.60GHz  
> WORD\_SIZE: 64  
> LIBM: libopenlibm  
> LLVM: libLLVM-11.0.1 (ORCJIT, skylake)

> ] status:

> ```
> Status `~/.julia/environments/v1.6/Project.toml`
> 
> ```
> 
> [336ed68f] CSV v0.8.4  
> [a93c6f00] DataFrames v1.1.1  
> [0c46a032] DifferentialEquations v6.16.0  
> [28b8d3ca] GR v0.57.4  
> [7073ff75] IJulia v1.23.2  
> [4138dd39] JLD v0.12.3  
> [961ee093] ModelingToolkit v5.16.0  
> [e7bfaba1] NumericalIntegration v0.3.3  
> [47be7bcc] ORCA v0.5.0  
> [8314cec4] PGFPlotsX v1.2.10  
> [f0f68f2c] PlotlyJS v0.14.1  
> [91a5bcdd] Plots v1.13.2  
> [d330b81b] PyPlot v2.9.0  
> [24249f21] SymPy v1.0.44

This is the part which is getting hung:

> u01 = [φ =\> 0.000,  
> ψ =\> 0.010]  
> p1 = [ξ =\> -1.0]  
> tspan1 = (0.0,150.0)  
> prob1 = ODEProblem(sys1,u01,tspan1,p1)  
> sol1 = solve(prob1,AutoTsit5(Rosenbrock23()))  
> df1 = DataFrame(sol1)  
> CSV.write(“greenxi-1fit0,010.csv”,df1)

Thank you so much @affans, I will check the performance tips

---

<div class="post-metadata">

### Author: ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)
#### Post date: [May 11, 2021, 4:14am UTC](https://discourse.julialang.org/t/bad-performance-of-differentialequations-jl/60923/4 "2021-05-11T04:14:06Z")

</div>

I do not think it is because of that. The ModelingToolkit converts system of equations into a function by itself and calls with given initial state `u01` and `p1`. @leonandonayre you can try a small time step(if it is oscillating fast then use a fraction of the period of oscillation) for tspan to see if it really shows something or if it stuck in somewhere in compilation. If it still takes much time then interrupt and show us some stacktrace from the interruption.

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [May 11, 2021, 4:31am UTC](https://discourse.julialang.org/t/bad-performance-of-differentialequations-jl/60923/5 "2021-05-11T04:31:49Z")

</div>

This example is broken running `H1 = (3ξψdB - sqrt(9ξ^2 * φ^2 * dB^2 + (1 - 6ξB)(ψ^2 + 2V)) )/(1 - 6ξB)` gives  
`ERROR: UndefVarError: ξψdB not defined`.

---

<div class="post-metadata">

### Author: ![leonandonayre](https://avatars.discourse-cdn.com/v4/letter/l/da6949/32.png) [@leonandonayre](https://discourse.julialang.org/u/leonandonayre)
#### Post date: [May 11, 2021, 4:42am UTC](https://discourse.julialang.org/t/bad-performance-of-differentialequations-jl/60923/6 "2021-05-11T04:42:22Z")

</div>

There are some \* missing when I copied the code from the notebook

 ![Screenshot_2021-05-10 Toporensky 1404 6226 - Jupyter Notebook](https://global.discourse-cdn.com/julialang/original/3X/e/1/e1f8dca62d7c2174f517318c1f9edb957264b83f.png)

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [May 11, 2021, 4:45am UTC](https://discourse.julialang.org/t/bad-performance-of-differentialequations-jl/60923/7 "2021-05-11T04:45:10Z")

</div>

Can you please update the original post to be actually runable? Otherwise it is very difficult to debug this.

---

<div class="post-metadata">

### Author: ![leonandonayre](https://avatars.discourse-cdn.com/v4/letter/l/da6949/32.png) [@leonandonayre](https://discourse.julialang.org/u/leonandonayre)
#### Post date: [May 11, 2021, 6:32am UTC](https://discourse.julialang.org/t/bad-performance-of-differentialequations-jl/60923/8 "2021-05-11T06:32:30Z")

</div>

Sorry. I think now it will work

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [May 11, 2021, 1:31pm UTC](https://discourse.julialang.org/t/bad-performance-of-differentialequations-jl/60923/9 "2021-05-11T13:31:32Z")

</div>

The problem appears to be that the model completely blows up between x=14 and x=15. If you use `Rodas5()` as your solver, you will see that it finishes in about 5 seconds, but stops after around x=14 because the solution blows up to infinity.

---

<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: [May 11, 2021, 2:26pm UTC](https://discourse.julialang.org/t/bad-performance-of-differentialequations-jl/60923/10 "2021-05-11T14:26:54Z")

</div>

Just run the model with solvers from 3 languages: Julia, C++, and Fortran.

```julia
using OrdinaryDiffEq, Sundials, ODEInterfaceDiffEq
sol1 = solve(prob1,Rodas5(),abstol=1e-12,reltol=1e-12)
sol2 = solve(prob1,CVODE_BDF(),abstol=1e-12,reltol=1e-12)
sol3 = solve(prob1,radau(),abstol=1e-12,reltol=1e-12)

@show sol1.t[end], sol2.t[end], sol3.t[end]
@show sol1[end], sol2[end], sol3[end]

(sol1.t[end], sol2.t[end], sol3.t[end]) = (14.57381167838683, 14.57381163596623, 14.573811679098052)
(sol1[end], sol2[end], sol3[end]) = ([213178.70363115682, 4.8383636378845696e17], [1.868111482362633e36, 2.930079193719137e117], [349223.9235893567, 2.3765960233233695e18])

```

You see that every robust solver from each language all blows up to infinity at almost precisely the same point in time. That is very strong evidence that the true solution of the model you had written down goes to infinity at that time.

It seems like the automatic stiffness detection falls into a loop (which we should fix), but at least your issue is that the differential equation you wrote down is divergent and you should probably double check for sign issues.

---

<div class="post-metadata">

### Author: ![leonandonayre](https://avatars.discourse-cdn.com/v4/letter/l/da6949/32.png) [@leonandonayre](https://discourse.julialang.org/u/leonandonayre)
#### Post date: [May 11, 2021, 3:55pm UTC](https://discourse.julialang.org/t/bad-performance-of-differentialequations-jl/60923/11 "2021-05-11T15:55:24Z")

</div>

Should I report the issue in github?

---

<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: [May 11, 2021, 4:01pm UTC](https://discourse.julialang.org/t/bad-performance-of-differentialequations-jl/60923/12 "2021-05-11T16:01:21Z")

</div>

Yes
