Simple Demonstration of a Symbolic System Structure

In this tutorial we will show how to implement a system structure type for defining the symbolic indexing of a domain-specific language. This tutorial will show how the SymbolCache type is defined to take in arrays of symbols for its independent, dependent, and parameter variable names and uses that to define the symbolic indexing interface.

Defining the ODE

For this example, we will use the Robertson equations:

\[\begin{aligned} \frac{dy_1}{dt} &= -0.04y₁ + 10^4 y_2 y_3 \\ \frac{dy_2}{dt} &= 0.04 y_1 - 10^4 y_2 y_3 - 3*10^7 y_{2}^2 \\ \frac{dy_3}{dt} &= 3*10^7 y_{2}^2 \\ \end{aligned}\]

The in-place function for this ODE system can be defined as:

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
rober! (generic function with 1 method)

To add symbolic names for the states in this example, a SymbolCache can be created and passed as the sys keyword argument to the ODEFunction constructor, as shown below:

using OrdinaryDiffEq, SymbolicIndexingInterface

sys = SymbolCache([:y₁, :y₂, :y₃])
odefun = ODEFunction(rober!; sys = sys)

This is then used to create and solve the ODEProblem

prob = ODEProblem(odefun, [1.0, 0.0, 0.0], (0.0, 1e5), [0.04, 3e7, 1e4])
sol = solve(prob, Rosenbrock23())
retcode: Success
Interpolation: specialized 2nd order "free" stiffness-aware interpolation
t: 61-element Vector{Float64}:
      0.0
      3.196206628740808e-5
      0.00014400709669791316
      0.00025605212710841824
      0.0004859387252056149
      0.0007179482298405133
      0.0010819240431280998
      0.0014801655696439712
      0.0020679567767069714
      0.0028435846274559493
      ⋮
  25371.934242574636
  30784.119976873913
  37217.42637183452
  44850.61378119758
  53893.69155452613
  64593.73799781128
  77241.71960460563
  92180.81944906653
 100000.0
u: 61-element Vector{Vector{Float64}}:
 [1.0, 0.0, 0.0]
 [0.9999987215181657, 1.2780900152625978e-6, 3.9181897521319503e-10]
 [0.9999942397327672, 5.718510591908377e-6, 4.1756640838141946e-8]
 [0.9999897579685716, 9.992106860321924e-6, 2.499245680985421e-7]
 [0.99998056266788, 1.7833624284594363e-5, 1.6037078354141808e-6]
 [0.9999712826600025, 2.4034886076943867e-5, 4.682453920643881e-6]
 [0.9999567250106862, 3.0390689558961375e-5, 1.2884299754832166e-5]
 [0.999940798607161, 3.3884273738713e-5, 2.5317119100293923e-5]
 [0.9999172960308617, 3.583508668595172e-5, 4.686888245237191e-5]
 [0.9998862913719596, 3.6412401653671283e-5, 7.729622638673516e-5]
 ⋮
 [0.0556350773100088, 2.3546320422346616e-7, 0.944364687226786]
 [0.04792534876404702, 2.0121495043224782e-7, 0.9520744500210006]
 [0.04123342149003358, 1.7192788166891057e-7, 0.9587664065820821]
 [0.035436998370307456, 1.4688361975260143e-7, 0.9645628547460707]
 [0.030425371816164046, 1.254680931847275e-7, 0.9695745027157401]
 [0.02609913220115014, 1.0715608431718005e-7, 0.9739007606427639]
 [0.02236969169455015, 9.149844876161136e-8, 0.9776302168069988]
 [0.019158563313533234, 7.81109638013875e-8, 0.9808413585755004]
 [0.017827893850751765, 7.258919982166354e-8, 0.9821720335600459]

The solution can now be indexed symbolically:

sol[:y₁]
61-element Vector{Float64}:
 1.0
 0.9999987215181657
 0.9999942397327672
 0.9999897579685716
 0.99998056266788
 0.9999712826600025
 0.9999567250106862
 0.999940798607161
 0.9999172960308617
 0.9998862913719596
 ⋮
 0.0556350773100088
 0.04792534876404702
 0.04123342149003358
 0.035436998370307456
 0.030425371816164046
 0.02609913220115014
 0.02236969169455015
 0.019158563313533234
 0.017827893850751765
sol(1e3, idxs=:y₁)
0.33675468346974147

However, we did not give names to the parameters or the independent variables. They can be specified using SymbolCache as well:

sys = SymbolCache([:y₁, :y₂, :y₃], [:k₁, :k₂, :k₃], :t)
odefun = ODEFunction(rober!; sys = sys)
prob = ODEProblem(odefun, [1.0, 0.0, 0.0], (0.0, 1e5), [0.04, 3e7, 1e4])
sol = solve(prob, Rosenbrock23())
getk1 = getp(sys, :k₁)

getk1(prob)
0.04
getk1(sol)
0.04
sol[:t]
61-element Vector{Float64}:
      0.0
      3.196206628740808e-5
      0.00014400709669791316
      0.00025605212710841824
      0.0004859387252056149
      0.0007179482298405133
      0.0010819240431280998
      0.0014801655696439712
      0.0020679567767069714
      0.0028435846274559493
      ⋮
  25371.934242574636
  30784.119976873913
  37217.42637183452
  44850.61378119758
  53893.69155452613
  64593.73799781128
  77241.71960460563
  92180.81944906653
 100000.0