Skip to content

Commit 755b3b6

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

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-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

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
8+
```@example debug
9+
using ModelingToolkit, OrdinaryDiffEq
10+
using ModelingToolkit: t_nounits as t, D_nounits as D
11+
12+
@variables u1(t) u2(t) u3(t)
13+
eqs = [D(u1) ~ -√(u1), D(u2) ~ -√(u2), D(u3) ~ -√(u3)]
14+
defaults = [u1 => 1.0, u2 => 2.0, u3 => 3.0]
15+
@named sys = ODESystem(eqs, t; defaults)
16+
sys = structural_simplify(sys)
17+
```
18+
19+
This problem causes the ODE solver to crash:
20+
21+
```@example debug
22+
prob = ODEProblem(sys, [], (0.0, 10.0), [])
23+
sol = solve(prob, Tsit5())
24+
```
25+
26+
This suggests *that* something went wrong, but not exactly *what* went wrong and *where* it did.
27+
In such situations, the `debug_system` function is helpful:
28+
29+
```@example debug
30+
try # workaround to show Documenter.jl error (https://github.com/JuliaDocs/Documenter.jl/issues/1420#issuecomment-770539595) # hide
31+
dsys = debug_system(sys; functions = [sqrt])
32+
dprob = ODEProblem(dsys, [], (0.0, 10.0), [])
33+
dsol = solve(dprob, Tsit5())
34+
catch err # hide
35+
showerror(stderr, err) # hide
36+
end # hide
37+
```
38+
39+
Now we see that it crashed because `u1` decreased so much that it became negative and outside the domain of the `` function.
40+
We could have figured that out ourselves, but it is not always so obvious for more complex models.
41+
42+
```@docs
43+
debug_system
44+
```

0 commit comments

Comments
 (0)