Skip to content

Commit b01cfdb

Browse files
committed
Add documentation page for debugging
1 parent ca78852 commit b01cfdb

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

docs/pages.jl

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pages = [
3131
"basics/InputOutput.md",
3232
"basics/MTKLanguage.md",
3333
"basics/Validation.md",
34+
"basics/Debugging.md",
3435
"basics/DependencyGraphs.md",
3536
"basics/Precompilation.md",
3637
"basics/FAQ.md"],

docs/src/basics/Debugging.md

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Debugging
2+
3+
Every (mortal) modeler writes models that contain mistakes or are susceptible to numerical errors in their hunt for the perfect model.
4+
Debugging such errors is part of the modeling process, and ModelingToolkit includes some functionality that helps with this.
5+
6+
For example, consider an ODE model with "dangerous" functions (here ``):
7+
```julia
8+
using ModelingToolkit, OrdinaryDiffEq
9+
using ModelingToolkit: t_nounits as t, D_nounits as D
10+
11+
@variables u1(t) u2(t) u3(t)
12+
@named sys = ODESystem([
13+
D(u1) ~ -√(u1)
14+
D(u2) ~ -√(u2)
15+
D(u3) ~ -√(u3)
16+
], t; defaults = [
17+
u1 => 1.0
18+
u2 => 2.0
19+
u3 => 3.0
20+
])
21+
sys = structural_simplify(sys)
22+
```
23+
24+
This problem causes the ODE solver to crash:
25+
```julia
26+
prob = ODEProblem(sys, [], (0.0, 10.0), [])
27+
sol = solve(prob, Tsit5())
28+
```
29+
30+
This error message suggests *that* something went wrong, but not exactly *what* went wrong and *where* it did.
31+
In such situations, the `debug_system` function is helpful:
32+
```julia
33+
dsys = debug_system(sys; functions = [sqrt])
34+
dprob = ODEProblem(dsys, [], (0.0, 10.0), [])
35+
dsol = solve(dprob, Tsit5())
36+
```
37+
38+
Now we see that it crashed because `u1` decreased so much that it became negative and outside the domain of the `` function.
39+
We could have figured that out ourselves, but it is not always so obvious for more complex models.
40+
41+
```@docs
42+
debug_system
43+
```

0 commit comments

Comments
 (0)