Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: write SymbolCache tutorial #28

Merged
merged 1 commit into from
Jan 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions docs/src/simple_sii_sys.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,78 @@ In this tutorial we will show how to implement a system structure type for defin
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:

```math
\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:

```@example symbolcache
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
```

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

```@example symbolcache
using OrdinaryDiffEq, SymbolicIndexingInterface

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

This is then used to create and solve the `ODEProblem`

```@example symbolcache
prob = ODEProblem(odefun, [1.0, 0.0, 0.0], (0.0, 1e5), [0.04, 3e7, 1e4])
sol = solve(prob, Rosenbrock23())
```

The solution can now be indexed symbolically:

```@example symbolcache
sol[:y₁]
```

```@example symbolcache
sol(1e3, idxs=:y₁)
```

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

```@example symbolcache
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)
```

```@example symbolcache
getk1(sol)
```

```@example symbolcache
sol[:t]
```