@@ -4,3 +4,78 @@ In this tutorial we will show how to implement a system structure type for defin
44symbolic indexing of a domain-specific language. This tutorial will show how the
55` SymbolCache ` type is defined to take in arrays of symbols for its independent, dependent,
66and parameter variable names and uses that to define the symbolic indexing interface.
7+
8+ ## Defining the ODE
9+
10+ For this example, we will use the Robertson equations:
11+
12+ ``` math
13+ \begin{aligned}
14+ \frac{dy_1}{dt} &= -0.04y₁ + 10^4 y_2 y_3 \\
15+ \frac{dy_2}{dt} &= 0.04 y_1 - 10^4 y_2 y_3 - 3*10^7 y_{2}^2 \\
16+ \frac{dy_3}{dt} &= 3*10^7 y_{2}^2 \\
17+ \end{aligned}
18+ ```
19+
20+ The in-place function for this ODE system can be defined as:
21+
22+ ``` @example symbolcache
23+ function rober!(du, u, p, t)
24+ y₁, y₂, y₃ = u
25+ k₁, k₂, k₃ = p
26+ du[1] = -k₁ * y₁ + k₃ * y₂ * y₃
27+ du[2] = k₁ * y₁ - k₂ * y₂^2 - k₃ * y₂ * y₃
28+ du[3] = k₂ * y₂^2
29+ nothing
30+ end
31+ ```
32+
33+ To add symbolic names for the states in this example, a [ ` SymbolCache ` ] ( @ref ) can be
34+ created and passed as the ` sys ` keyword argument to the ` ODEFunction ` constructor,
35+ as shown below:
36+
37+ ``` @example symbolcache
38+ using OrdinaryDiffEq, SymbolicIndexingInterface
39+
40+ sys = SymbolCache([:y₁, :y₂, :y₃])
41+ odefun = ODEFunction(rober!; sys = sys)
42+ nothing # hide
43+ ```
44+
45+ This is then used to create and solve the ` ODEProblem `
46+
47+ ``` @example symbolcache
48+ prob = ODEProblem(odefun, [1.0, 0.0, 0.0], (0.0, 1e5), [0.04, 3e7, 1e4])
49+ sol = solve(prob, Rosenbrock23())
50+ ```
51+
52+ The solution can now be indexed symbolically:
53+
54+ ``` @example symbolcache
55+ sol[:y₁]
56+ ```
57+
58+ ``` @example symbolcache
59+ sol(1e3, idxs=:y₁)
60+ ```
61+
62+ However, we did not give names to the parameters or the independent variables. They can
63+ be specified using ` SymbolCache ` as well:
64+
65+ ``` @example symbolcache
66+ sys = SymbolCache([:y₁, :y₂, :y₃], [:k₁, :k₂, :k₃], :t)
67+ odefun = ODEFunction(rober!; sys = sys)
68+ prob = ODEProblem(odefun, [1.0, 0.0, 0.0], (0.0, 1e5), [0.04, 3e7, 1e4])
69+ sol = solve(prob, Rosenbrock23())
70+ getk1 = getp(sys, :k₁)
71+
72+ getk1(prob)
73+ ```
74+
75+ ``` @example symbolcache
76+ getk1(sol)
77+ ```
78+
79+ ``` @example symbolcache
80+ sol[:t]
81+ ```
0 commit comments