Skip to content

Commit 8995773

Browse files
Add a bunch more documentation stuff
Finishes #806
1 parent d04acc3 commit 8995773

13 files changed

+154
-4
lines changed

docs/make.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@ makedocs(
2222
],
2323
"ModelingToolkitize Tutorials" => Any[
2424
"mtkitize_tutorials/modelingtoolkitize.md",
25-
"mtkitize_tutorials/modelingtoolkitize_index_reduction.md"
25+
"mtkitize_tutorials/modelingtoolkitize_index_reduction.md",
26+
#"mtkitize_tutorials/sparse_jacobians",
2627
],
2728
"Basics" => Any[
2829
"basics/AbstractSystem.md",
2930
"basics/ContextualVariables.md",
3031
"basics/Composition.md",
3132
"basics/Validation.md",
32-
"basics/DependencyGraphs.md"
33+
"basics/DependencyGraphs.md",
34+
"basics/FAQ.md"
3335
],
3436
"System Types" => Any[
3537
"systems/ODESystem.md",

docs/src/basics/AbstractSystem.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ another `AbstractSystem`. These are passes, like optimizations (e.g., Block-Lowe
7171
Triangle transformations), or changes to the representation, which allow for
7272
alternative numerical methods to be utilized on the model (e.g., DAE index reduction).
7373

74+
## Analyses
75+
76+
Analyses are functions on a system which return information about the corresponding
77+
properties, like whether its parameters are structurally identifiable, or whether
78+
it's linear.
79+
7480
## Function Calculation and Generation
7581

7682
The calculation and generation functions allow for calculating additional
@@ -112,3 +118,21 @@ array would normally be provided, such as `u0` the initial condition of an
112118
`ODEProblem`, it is instead replaced with a variable map, i.e., an array of
113119
pairs `var=>value`, which allows the user to designate the values without having
114120
to know the order that ModelingToolkit is internally using.
121+
122+
For the value maps, the parameters are allowed to be functions of each other,
123+
and value maps of states can be functions of the parameters, i.e. you can do:
124+
125+
```
126+
u0 = [
127+
lorenz1.x => 2.0
128+
lorenz2.x => lorenz1.x * lorenz1.p
129+
]
130+
```
131+
132+
## Default Value Handling
133+
134+
The `AbstractSystem` types allow for specifying default values, for example
135+
`default_p` inside of them. At problem construction time, these values are merged
136+
into the value maps, where for any repeats the value maps override the default.
137+
In addition, defaults of a higher level in the system override the defaults of
138+
a lower level in the system.

docs/src/basics/FAQ.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Frequently Asked Questions
2+
3+
## Getting the index for a symbol
4+
5+
Since **ordering of symbols is not guaranteed after symbolic transformations**,
6+
one should normally refer to values by their name. For example, `sol[lorenz.x]`
7+
from the solution. But what if you need to get the index? The following helper
8+
function will do the trick:
9+
10+
```julia
11+
indexof(sym,syms) = findfirst(isequal(sym),syms)
12+
indexof(σ,parameters(sys))
13+
```
14+
15+
## Transforming value maps to arrays
16+
17+
ModelingToolkit.jl allows (and recommends) input maps like `[x => 2.0, y => 3.0]`
18+
because symbol ordering is not guaranteed. However, what if you want to get the
19+
lowered array? You can use the internal function `varmap_to_vars`. For example:
20+
21+
```julia
22+
pnew = varmap_to_vars([β=>3.0, c=>10.0, γ=>2.0],parameters(sys))
23+
```

docs/src/mtkitize_tutorials/modelingtoolkitize.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Symbolic Extensions to ODEProblem via Modelingtoolkitize
1+
# Automatically Accelerating ODEProblem Code
22

33
For some `DEProblem` types, automatic tracing functionality is already included
44
via the `modelingtoolkitize` function. Take, for example, the Robertson ODE

docs/src/mtkitize_tutorials/modelingtoolkitize_index_reduction.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Automated Index Reduction of DAEs via ModelingToolkitize
1+
# Automated Index Reduction of DAEs
22

33
In many cases one may accidentally write down a DAE that is not easily solvable
44
by numerical methods. In this tutorial we will walk through an example of a
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Automated Sparse Analytical Jacobians
2+
3+
In many cases where you have large stiff differential equations, getting a
4+
sparse Jacobian can be essential for performance. In this tutorial we will show
5+
how to use `modelingtoolkitize` to regenerate an `ODEProblem` code with
6+
the analytical solution to the sparse Jacobian, along with the sparsity
7+
pattern required by DifferentialEquations.jl's solvers to specialize the solving
8+
process.
9+
10+
First let's start out with an implementation of the 2-dimensional Brusselator
11+
partial differential equation discretized using finite differences:
12+
13+
```julia
14+
using DifferentialEquations, ModelingToolkit
15+
16+
const N = 32
17+
const xyd_brusselator = range(0,stop=1,length=N)
18+
brusselator_f(x, y, t) = (((x-0.3)^2 + (y-0.6)^2) <= 0.1^2) * (t >= 1.1) * 5.
19+
limit(a, N) = a == N+1 ? 1 : a == 0 ? N : a
20+
function brusselator_2d_loop(du, u, p, t)
21+
A, B, alpha, dx = p
22+
alpha = alpha/dx^2
23+
@inbounds for I in CartesianIndices((N, N))
24+
i, j = Tuple(I)
25+
x, y = xyd_brusselator[I[1]], xyd_brusselator[I[2]]
26+
ip1, im1, jp1, jm1 = limit(i+1, N), limit(i-1, N), limit(j+1, N), limit(j-1, N)
27+
du[i,j,1] = alpha*(u[im1,j,1] + u[ip1,j,1] + u[i,jp1,1] + u[i,jm1,1] - 4u[i,j,1]) +
28+
B + u[i,j,1]^2*u[i,j,2] - (A + 1)*u[i,j,1] + brusselator_f(x, y, t)
29+
du[i,j,2] = alpha*(u[im1,j,2] + u[ip1,j,2] + u[i,jp1,2] + u[i,jm1,2] - 4u[i,j,2]) +
30+
A*u[i,j,1] - u[i,j,1]^2*u[i,j,2]
31+
end
32+
end
33+
p = (3.4, 1., 10., step(xyd_brusselator))
34+
35+
function init_brusselator_2d(xyd)
36+
N = length(xyd)
37+
u = zeros(N, N, 2)
38+
for I in CartesianIndices((N, N))
39+
x = xyd[I[1]]
40+
y = xyd[I[2]]
41+
u[I,1] = 22*(y*(1-y))^(3/2)
42+
u[I,2] = 27*(x*(1-x))^(3/2)
43+
end
44+
u
45+
end
46+
u0 = init_brusselator_2d(xyd_brusselator)
47+
prob = ODEProblem(brusselator_2d_loop,u0,(0.,11.5),p)
48+
```
49+
50+
Now let's use `modelingtoolkitize` to generate the symbolic version:
51+
52+
```julia
53+
sys = modelingtoolkitize(prob_ode_brusselator_2d)
54+
```
55+
56+
Now we regenerate the problem using `jac=true` for the analytical Jacobian
57+
and `sparse=true` to make it sparse:
58+
59+
```julia
60+
sparseprob = ODEProblem(sys,Pair[],(0.,11.5),jac=true,sparse=true)
61+
```
62+
63+
Hard? No! How much did that help?
64+
65+
```julia
66+
using BenchmarkTools
67+
@btime solve(prob,save_everystep=false) # 51.714 s (7317 allocations: 70.12 MiB)
68+
@btime solve(sparseprob,save_everystep=false) # 2.880 s (55533 allocations: 885.09 MiB)
69+
```
70+
71+
Notice though that the analytical solution to the Jacobian can be quite expensive.
72+
Thus in some cases we may only want to get the sparsity pattern. In this case,
73+
we can simply do:
74+
75+
```julia
76+
sparsepatternprob = ODEProblem(sys,Pair[],(0.,11.5),sparse=true)
77+
@btime solve(sparsepatternprob,save_everystep=false) # 2.880 s (55533 allocations: 885.09 MiB)
78+
```

docs/src/systems/ControlSystem.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ ControlSystem
1919
ModelingToolkit.runge_kutta_discretize
2020
structural_simplify
2121
```
22+
23+
## Analyses

docs/src/systems/JumpSystem.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ JumpSystem
1919
structural_simplify
2020
```
2121

22+
## Analyses
23+
2224
## Problem Constructors
2325

2426
```@docs

docs/src/systems/NonlinearSystem.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ alias_elimination
2020
tearing
2121
```
2222

23+
## Analyses
24+
25+
```@docs
26+
ModelingToolkit.islinear
27+
```
28+
2329
## Applicable Calculation and Generation Functions
2430

2531
```julia

docs/src/systems/ODESystem.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ alias_elimination
2424
tearing
2525
```
2626

27+
## Analyses
28+
29+
```@docs
30+
ModelingToolkit.islinear
31+
ModelingToolkit.isautonomous
32+
```
33+
2734
## Applicable Calculation and Generation Functions
2835

2936
```julia

0 commit comments

Comments
 (0)