Skip to content

Commit 1ea9438

Browse files
authored
Merge pull request #1572 from SciML/fb/builddocs
Run examples in docs
2 parents 137e7a2 + a3a5c9a commit 1ea9438

File tree

11 files changed

+98
-235
lines changed

11 files changed

+98
-235
lines changed

docs/Project.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
[deps]
2+
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
3+
DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa"
4+
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
25
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
6+
GalacticOptim = "a75be94c-b780-496d-a8a9-0878b188d577"
7+
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
38
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
9+
NonlinearSolve = "8913a72c-1f9b-4ce2-8d82-65094dcecaec"
10+
Optim = "429524aa-4258-5aef-a3af-852621145aeb"
11+
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
12+
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
13+
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
14+
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
415

516
[compat]
617
Documenter = "0.27"

docs/make.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ makedocs(
55
authors="Chris Rackauckas",
66
modules=[ModelingToolkit],
77
clean=true,doctest=false,
8+
strict=[
9+
:doctest,
10+
:linkcheck,
11+
:parse_error,
12+
:example_block,
13+
# Other available options are
14+
# :autodocs_block, :cross_references, :docs_block, :eval_block, :example_block, :footnote, :meta_block, :missing_docs, :setup_block
15+
],
816
format=Documenter.HTML(analytics = "UA-90474609-3",
917
assets=["assets/favicon.ico"],
1018
canonical="https://mtk.sciml.ai/stable/"),

docs/src/basics/Composition.md

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ forcing later. Here, the library author defines a component named
1414
saying the forcing term of `decay1` is a constant while the forcing term
1515
of `decay2` is the value of the state variable `x`.
1616

17-
```julia
17+
```@example composition
1818
using ModelingToolkit
1919
2020
function decay(;name)
@@ -48,16 +48,11 @@ equations(connected)
4848
simplified_sys = structural_simplify(connected)
4949
5050
equations(simplified_sys)
51-
52-
#3-element Vector{Equation}:
53-
# Differential(t)(decay1₊f(t)) ~ 0
54-
# Differential(t)(decay1₊x(t)) ~ decay1₊f(t) - (decay1₊a*(decay1₊x(t)))
55-
# Differential(t)(decay2₊x(t)) ~ decay1₊x(t) - (decay2₊a*(decay2₊x(t)))
5651
```
5752

5853
Now we can solve the system:
5954

60-
```julia
55+
```@example composition
6156
x0 = [
6257
decay1.x => 1.0
6358
decay1.f => 0.0
@@ -179,7 +174,7 @@ let's assume we have three separate systems which we want to compose to a single
179174
one. This is how one could explicitly forward all states and parameters to the
180175
higher level system:
181176

182-
```julia
177+
```@example compose
183178
using ModelingToolkit, OrdinaryDiffEq, Plots
184179
185180
## Library code
@@ -195,7 +190,7 @@ N = S + I + R
195190
@named ieqn = ODESystem([D(I) ~ β*S*I/N-γ*I])
196191
@named reqn = ODESystem([D(R) ~ γ*I])
197192
198-
@named sir = compose(ODESystem([
193+
sir = compose(ODESystem([
199194
S ~ ieqn.S,
200195
I ~ seqn.I,
201196
R ~ ieqn.R,
@@ -209,24 +204,21 @@ N = S + I + R
209204
ieqn.β => β
210205
ieqn.γ => γ
211206
reqn.γ => γ
212-
]), seqn, ieqn, reqn)
207+
], name=:sir), seqn, ieqn, reqn)
213208
```
214209

215210
Note that the states are forwarded by an equality relationship, while
216211
the parameters are forwarded through a relationship in their default
217212
values. The user of this model can then solve this model simply by
218213
specifying the values at the highest level:
219214

220-
```julia
215+
```@example compose
221216
sireqn_simple = structural_simplify(sir)
222217
223218
equations(sireqn_simple)
219+
```
224220

225-
# 3-element Vector{Equation}:
226-
#Differential(t)(seqn₊S(t)) ~ -seqn₊β*ieqn₊I(t)*seqn₊S(t)*(((ieqn₊I(t)) + (reqn₊R(t)) + (seqn₊S(t)))^-1)
227-
#Differential(t)(ieqn₊I(t)) ~ ieqn₊β*ieqn₊I(t)*seqn₊S(t)*(((ieqn₊I(t)) + (reqn₊R(t)) + (seqn₊S(t)))^-1) - (ieqn₊γ*(ieqn₊I(t)))
228-
#Differential(t)(reqn₊R(t)) ~ reqn₊γ*ieqn₊I(t)
229-
221+
```@example compose
230222
## User Code
231223
232224
u0 = [seqn.S => 990.0,
@@ -270,7 +262,7 @@ single_state_variable ~ expression_involving_any_variables
270262

271263
### Example: Friction
272264
The system below illustrates how this can be used to model Coulomb friction
273-
```julia
265+
```@example events
274266
using ModelingToolkit, OrdinaryDiffEq, Plots
275267
function UnitMassWithFriction(k; name)
276268
@variables t x(t)=0 v(t)=0
@@ -290,7 +282,7 @@ plot(sol)
290282
### Example: Bouncing ball
291283
In the documentation for DifferentialEquations, we have an example where a bouncing ball is simulated using callbacks which has an `affect!` on the state. We can model the same system using ModelingToolkit like this
292284

293-
```julia
285+
```@example events
294286
@variables t x(t)=1 v(t)=0
295287
D = Differential(t)
296288
@@ -313,7 +305,7 @@ plot(sol)
313305

314306
### Test bouncing ball in 2D with walls
315307
Multiple events? No problem! This example models a bouncing ball in 2D that is enclosed by two walls at $y = \pm 1.5$.
316-
```julia
308+
```@example events
317309
@variables t x(t)=1 y(t)=0 vx(t)=0 vy(t)=2
318310
D = Differential(t)
319311

docs/src/mtkitize_tutorials/modelingtoolkitize.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ For some `DEProblem` types, automatic tracing functionality is already included
44
via the `modelingtoolkitize` function. Take, for example, the Robertson ODE
55
defined as an `ODEProblem` for DifferentialEquations.jl:
66

7-
```julia
8-
using DifferentialEquations
7+
```@example mtkize
8+
using DifferentialEquations, ModelingToolkit
99
function rober(du,u,p,t)
1010
y₁,y₂,y₃ = u
1111
k₁,k₂,k₃ = p
@@ -20,12 +20,12 @@ prob = ODEProblem(rober,[1.0,0.0,0.0],(0.0,1e5),(0.04,3e7,1e4))
2020
If we want to get a symbolic representation, we can simply call `modelingtoolkitize`
2121
on the `prob`, which will return an `ODESystem`:
2222

23-
```julia
23+
```@example mtkize
2424
sys = modelingtoolkitize(prob)
2525
```
2626

2727
Using this, we can symbolically build the Jacobian and then rebuild the ODEProblem:
2828

29-
```julia
29+
```@example mtkize
3030
prob_jac = ODEProblem(sys,[],(0.0,1e5),jac=true)
3131
```

docs/src/mtkitize_tutorials/modelingtoolkitize_index_reduction.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pendulum which accidentally generates an index-3 DAE, and show how to use the
77

88
## Copy-Pastable Example
99

10-
```julia
10+
```@example indexred
1111
using ModelingToolkit
1212
using LinearAlgebra
1313
using OrdinaryDiffEq
@@ -55,7 +55,7 @@ As a good DifferentialEquations.jl user, one would follow
5555
[the mass matrix DAE tutorial](https://diffeq.sciml.ai/stable/tutorials/advanced_ode_example/#Handling-Mass-Matrices)
5656
to arrive at code for simulating the model:
5757

58-
```julia
58+
```@example indexred
5959
using OrdinaryDiffEq, LinearAlgebra
6060
function pendulum!(du, u, p, t)
6161
x, dx, y, dy, T = u
@@ -143,7 +143,7 @@ the numerical code into symbolic code, run `dae_index_lowering` lowering,
143143
then transform back to numerical code with `ODEProblem`, and solve with a
144144
numerical solver. Let's try that out:
145145

146-
```julia
146+
```@example indexred
147147
traced_sys = modelingtoolkitize(pendulum_prob)
148148
pendulum_sys = structural_simplify(dae_index_lowering(traced_sys))
149149
prob = ODEProblem(pendulum_sys, Pair[], tspan)
@@ -153,8 +153,6 @@ using Plots
153153
plot(sol, vars=states(traced_sys))
154154
```
155155

156-
![](https://user-images.githubusercontent.com/1814174/110587364-9524b400-8141-11eb-91c7-4e56ce4fa20b.png)
157-
158156
Note that plotting using `states(traced_sys)` is done so that any
159157
variables which are symbolically eliminated, or any variable reorderings
160158
done for enhanced parallelism/performance, still show up in the resulting
@@ -166,16 +164,14 @@ constructor, we can remove the algebraic equations from the states of the
166164
system and fully transform the index-3 DAE into an index-0 ODE which can
167165
be solved via an explicit Runge-Kutta method:
168166

169-
```julia
167+
```@example indexred
170168
traced_sys = modelingtoolkitize(pendulum_prob)
171169
pendulum_sys = structural_simplify(dae_index_lowering(traced_sys))
172170
prob = ODAEProblem(pendulum_sys, Pair[], tspan)
173171
sol = solve(prob, Tsit5(),abstol=1e-8,reltol=1e-8)
174172
plot(sol, vars=states(traced_sys))
175173
```
176174

177-
![](https://user-images.githubusercontent.com/1814174/110587362-9524b400-8141-11eb-8b77-d940f108ae72.png)
178-
179175
And there you go: this has transformed the model from being too hard to
180176
solve with implicit DAE solvers, to something that is easily solved with
181177
explicit Runge-Kutta methods for non-stiff equations.

docs/src/mtkitize_tutorials/sparse_jacobians.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ process.
1010
First let's start out with an implementation of the 2-dimensional Brusselator
1111
partial differential equation discretized using finite differences:
1212

13-
```julia
13+
```@example sparsejac
1414
using DifferentialEquations, ModelingToolkit
1515
1616
const N = 32
@@ -49,14 +49,14 @@ prob = ODEProblem(brusselator_2d_loop,u0,(0.,11.5),p)
4949

5050
Now let's use `modelingtoolkitize` to generate the symbolic version:
5151

52-
```julia
53-
sys = modelingtoolkitize(prob_ode_brusselator_2d)
52+
```@example sparsejac
53+
sys = modelingtoolkitize(prob)
5454
```
5555

5656
Now we regenerate the problem using `jac=true` for the analytical Jacobian
5757
and `sparse=true` to make it sparse:
5858

59-
```julia
59+
```@example sparsejac
6060
sparseprob = ODEProblem(sys,Pair[],(0.,11.5),jac=true,sparse=true)
6161
```
6262

0 commit comments

Comments
 (0)