Automatically Accelerating ODEProblem Code
For some DEProblem
types, automatic tracing functionality is already included via the modelingtoolkitize
function. Take, for example, the Robertson ODE defined as an ODEProblem
for DifferentialEquations.jl:
using DifferentialEquations, ModelingToolkit
function rober(du,u,p,t)
y₁,y₂,y₃ = u
k₁,k₂,k₃ = p
du[1] = -k₁*y₁+k₃*y₂*y₃
du[2] = k₁*y₁-k₂*y₂^2-k₃*y₂*y₃
du[3] = k₂*y₂^2
nothing
end
prob = ODEProblem(rober,[1.0,0.0,0.0],(0.0,1e5),(0.04,3e7,1e4))
ODEProblem with uType Vector{Float64} and tType Float64. In-place: true
timespan: (0.0, 100000.0)
u0: 3-element Vector{Float64}:
1.0
0.0
0.0
If we want to get a symbolic representation, we can simply call modelingtoolkitize
on the prob
, which will return an ODESystem
:
sys = modelingtoolkitize(prob)
\[ \begin{align} \frac{dx_1(t)}{dt} =& - \alpha_1 \mathrm{x_1}\left( t \right) + \alpha_3 \mathrm{x_2}\left( t \right) \mathrm{x_3}\left( t \right) \\ \frac{dx_2(t)}{dt} =& \alpha_1 \mathrm{x_1}\left( t \right) - \left( \mathrm{x_2}\left( t \right) \right)^{2} \alpha_2 - \alpha_3 \mathrm{x_2}\left( t \right) \mathrm{x_3}\left( t \right) \\ \frac{dx_3(t)}{dt} =& \left( \mathrm{x_2}\left( t \right) \right)^{2} \alpha_2 \end{align} \]
Using this, we can symbolically build the Jacobian and then rebuild the ODEProblem:
prob_jac = ODEProblem(sys,[],(0.0,1e5),jac=true)
ODEProblem with uType Vector{Float64} and tType Float64. In-place: true
timespan: (0.0, 100000.0)
u0: 3-element Vector{Float64}:
1.0
0.0
0.0