Skip to content

Commit 88a1c32

Browse files
Merge pull request #2460 from AayushSabharwal/as/doc-mtkbuild
docs: use `@mtkbuild` instead of manually calling `complete`
2 parents 28fee62 + 02824b1 commit 88a1c32

13 files changed

+38
-55
lines changed

docs/src/basics/Events.md

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ function UnitMassWithFriction(k; name)
7272
D(v) ~ sin(t) - k * sign(v)]
7373
ODESystem(eqs, t; continuous_events = [v ~ 0], name) # when v = 0 there is a discontinuity
7474
end
75-
@named m = UnitMassWithFriction(0.7)
76-
prob = ODEProblem(complete(m), Pair[], (0, 10pi))
75+
@mtkbuild m = UnitMassWithFriction(0.7)
76+
prob = ODEProblem(m, Pair[], (0, 10pi))
7777
sol = solve(prob, Tsit5())
7878
plot(sol)
7979
```
@@ -93,11 +93,9 @@ D = Differential(t)
9393
root_eqs = [x ~ 0] # the event happens at the ground x(t) = 0
9494
affect = [v ~ -v] # the effect is that the velocity changes sign
9595
96-
@named ball = ODESystem([D(x) ~ v
96+
@mtkbuild ball = ODESystem([D(x) ~ v
9797
D(v) ~ -9.8], t; continuous_events = root_eqs => affect) # equation => affect
9898
99-
ball = structural_simplify(ball)
100-
10199
tspan = (0.0, 5.0)
102100
prob = ODEProblem(ball, Pair[], tspan)
103101
sol = solve(prob, Tsit5())
@@ -116,15 +114,13 @@ D = Differential(t)
116114
continuous_events = [[x ~ 0] => [vx ~ -vx]
117115
[y ~ -1.5, y ~ 1.5] => [vy ~ -vy]]
118116
119-
@named ball = ODESystem([
117+
@mtkbuild ball = ODESystem([
120118
D(x) ~ vx,
121119
D(y) ~ vy,
122120
D(vx) ~ -9.8 - 0.1vx, # gravity + some small air resistance
123121
D(vy) ~ -0.1vy,
124122
], t; continuous_events)
125123
126-
ball = structural_simplify(ball)
127-
128124
tspan = (0.0, 10.0)
129125
prob = ODEProblem(ball, Pair[], tspan)
130126
@@ -197,10 +193,9 @@ end
197193
198194
reflect = [x ~ 0] => (bb_affect!, [v], [], nothing)
199195
200-
@named bb_model = ODESystem(bb_eqs, t, sts, par,
196+
@mtkbuild bb_sys = ODESystem(bb_eqs, t, sts, par,
201197
continuous_events = reflect)
202198
203-
bb_sys = structural_simplify(bb_model)
204199
u0 = [v => 0.0, x => 1.0]
205200
206201
bb_prob = ODEProblem(bb_sys, u0, (0, 5.0))
@@ -243,8 +238,8 @@ injection = (t == tinject) => [N ~ N + M]
243238
u0 = [N => 0.0]
244239
tspan = (0.0, 20.0)
245240
p = [α => 100.0, tinject => 10.0, M => 50]
246-
@named osys = ODESystem(eqs, t, [N], [α, M, tinject]; discrete_events = injection)
247-
oprob = ODEProblem(complete(osys), u0, tspan, p)
241+
@mtkbuild osys = ODESystem(eqs, t, [N], [α, M, tinject]; discrete_events = injection)
242+
oprob = ODEProblem(osys, u0, tspan, p)
248243
sol = solve(oprob, Tsit5(); tstops = 10.0)
249244
plot(sol)
250245
```
@@ -262,7 +257,7 @@ to
262257
```@example events
263258
injection = ((t == tinject) & (N < 50)) => [N ~ N + M]
264259
265-
@named osys = ODESystem(eqs, t, [N], [M, tinject, α]; discrete_events = injection)
260+
@mtkbuild osys = ODESystem(eqs, t, [N], [M, tinject, α]; discrete_events = injection)
266261
oprob = ODEProblem(osys, u0, tspan, p)
267262
sol = solve(oprob, Tsit5(); tstops = 10.0)
268263
plot(sol)
@@ -287,7 +282,7 @@ killing = (t == tkill) => [α ~ 0.0]
287282
288283
tspan = (0.0, 30.0)
289284
p = [α => 100.0, tinject => 10.0, M => 50, tkill => 20.0]
290-
@named osys = ODESystem(eqs, t, [N], [α, M, tinject, tkill];
285+
@mtkbuild osys = ODESystem(eqs, t, [N], [α, M, tinject, tkill];
291286
discrete_events = [injection, killing])
292287
oprob = ODEProblem(osys, u0, tspan, p)
293288
sol = solve(oprob, Tsit5(); tstops = [10.0, 20.0])
@@ -315,7 +310,7 @@ injection = [10.0] => [N ~ N + M]
315310
killing = [20.0] => [α ~ 0.0]
316311
317312
p = [α => 100.0, M => 50]
318-
@named osys = ODESystem(eqs, t, [N], [α, M];
313+
@mtkbuild osys = ODESystem(eqs, t, [N], [α, M];
319314
discrete_events = [injection, killing])
320315
oprob = ODEProblem(osys, u0, tspan, p)
321316
sol = solve(oprob, Tsit5())

docs/src/basics/FAQ.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ using ModelingToolkit, StaticArrays
141141
sts = @variables x1(t)=0.0
142142
D = Differential(t)
143143
eqs = [D(x1) ~ 1.1 * x1]
144-
@named sys = ODESystem(eqs, t)
145-
sys = structural_simplify(sys)
144+
@mtkbuild sys = ODESystem(eqs, t)
146145
prob = ODEProblem{false}(sys, [], (0,1); u0_constructor = x->SVector(x...))
147146
```

docs/src/examples/parsing.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ nonlinear solve:
2525
```@example parsing
2626
using ModelingToolkit, NonlinearSolve
2727
vars = union(ModelingToolkit.vars.(eqs)...)
28-
@named ns = NonlinearSystem(eqs, vars, [])
28+
@mtkbuild ns = NonlinearSystem(eqs, vars, [])
2929
30-
prob = NonlinearProblem(complete(ns), [1.0, 1.0, 1.0])
30+
prob = NonlinearProblem(ns, [1.0, 1.0, 1.0])
3131
sol = solve(prob, NewtonRaphson())
3232
```

docs/src/examples/perturbation.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ We are nearly there! From this point on, the rest is standard ODE solving proced
9292
```julia
9393
using ModelingToolkit, DifferentialEquations
9494

95-
@named sys = ODESystem(eqs, t)
96-
sys = structural_simplify(sys)
95+
@mtkbuild sys = ODESystem(eqs, t)
9796
unknowns(sys)
9897
```
9998

@@ -158,8 +157,7 @@ eqs = [substitute(first(v), subs) ~ substitute(last(v), subs) for v in vals]
158157
We continue with converting 'eqs' to an `ODEProblem`, solving it, and finally plot the results against the exact solution to the original problem, which is $x(t, \epsilon) = (1 - \epsilon)^{-1/2} e^{-\epsilon t} \sin((1- \epsilon^2)^{1/2}t)$,
159158

160159
```julia
161-
@named sys = ODESystem(eqs, t)
162-
sys = structural_simplify(sys)
160+
@mtkbuild sys = ODESystem(eqs, t)
163161
```
164162

165163
```julia

docs/src/examples/sparse_jacobians.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ prob = ODEProblem(brusselator_2d_loop, u0, (0.0, 11.5), p)
5454
Now let's use `modelingtoolkitize` to generate the symbolic version:
5555

5656
```@example sparsejac
57-
sys = modelingtoolkitize(prob);
58-
sys = complete(sys);
57+
@mtkbuild sys = modelingtoolkitize(prob);
5958
nothing # hide
6059
```
6160

docs/src/tutorials/bifurcation_diagram_computation.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ using ModelingToolkit
1212
@parameters μ α
1313
eqs = [0 ~ μ * x - x^3 + α * y,
1414
0 ~ -y]
15-
@named nsys = NonlinearSystem(eqs, [x, y], [μ, α])
16-
nsys = complete(nsys)
15+
@mtkbuild nsys = NonlinearSystem(eqs, [x, y], [μ, α])
1716
```
1817

1918
we wish to compute a bifurcation diagram for this system as we vary the parameter `μ`. For this, we need to provide the following information:
@@ -94,8 +93,7 @@ using BifurcationKit, ModelingToolkit, Plots
9493
D = Differential(t)
9594
eqs = [D(x) ~ μ * x - y - x * (x^2 + y^2),
9695
D(y) ~ x + μ * y - y * (x^2 + y^2)]
97-
@named osys = ODESystem(eqs, t)
98-
osys = complete(osys)
96+
@mtkbuild osys = ODESystem(eqs, t)
9997
10098
bif_par = μ
10199
plot_var = x

docs/src/tutorials/domain_connections.md

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ To see how the domain works, we can examine the set parameter values for each of
135135

136136
```@repl domain
137137
sys = structural_simplify(odesys)
138-
ModelingToolkit.defaults(sys)[complete(odesys).vol.port.ρ]
138+
ModelingToolkit.defaults(sys)[odesys.vol.port.ρ]
139139
```
140140

141141
## Multiple Domain Networks
@@ -280,8 +280,7 @@ Adding the `Restrictor` to the original system example will cause a break in the
280280
ODESystem(eqs, t, [], []; systems, name)
281281
end
282282
283-
@named ressys = RestrictorSystem()
284-
sys = structural_simplify(ressys)
283+
@mtkbuild ressys = RestrictorSystem()
285284
nothing #hide
286285
```
287286

@@ -296,9 +295,9 @@ nothing #hide
296295
When `structural_simplify()` is applied to this system it can be seen that the defaults are missing for `res.port_b` and `vol.port`.
297296

298297
```@repl domain
299-
ModelingToolkit.defaults(sys)[complete(ressys).res.port_a.ρ]
300-
ModelingToolkit.defaults(sys)[complete(ressys).res.port_b.ρ]
301-
ModelingToolkit.defaults(sys)[complete(ressys).vol.port.ρ]
298+
ModelingToolkit.defaults(ressys)[ressys.res.port_a.ρ]
299+
ModelingToolkit.defaults(ressys)[ressys.res.port_b.ρ]
300+
ModelingToolkit.defaults(ressys)[ressys.vol.port.ρ]
302301
```
303302

304303
To ensure that the `Restrictor` component does not disrupt the domain network, the [`domain_connect()`](@ref) function can be used, which explicitly only connects the domain network and not the unknown variables.
@@ -322,15 +321,14 @@ To ensure that the `Restrictor` component does not disrupt the domain network, t
322321
ODESystem(eqs, t, [], pars; systems, name)
323322
end
324323
325-
@named ressys = RestrictorSystem()
326-
sys = structural_simplify(ressys)
324+
@mtkbuild ressys = RestrictorSystem()
327325
nothing #hide
328326
```
329327

330328
Now that the `Restrictor` component is properly defined using `domain_connect()`, the defaults for `res.port_b` and `vol.port` are properly defined.
331329

332330
```@repl domain
333-
ModelingToolkit.defaults(sys)[complete(ressys).res.port_a.ρ]
334-
ModelingToolkit.defaults(sys)[complete(ressys).res.port_b.ρ]
335-
ModelingToolkit.defaults(sys)[complete(ressys).vol.port.ρ]
331+
ModelingToolkit.defaults(ressys)[ressys.res.port_a.ρ]
332+
ModelingToolkit.defaults(ressys)[ressys.res.port_b.ρ]
333+
ModelingToolkit.defaults(ressys)[ressys.vol.port.ρ]
336334
```

docs/src/tutorials/modelingtoolkitize.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ If we want to get a symbolic representation, we can simply call `modelingtoolkit
5252
on the `prob`, which will return an `ODESystem`:
5353

5454
```@example mtkize
55-
sys = modelingtoolkitize(prob)
55+
@mtkbuild sys = modelingtoolkitize(prob)
5656
```
5757

5858
Using this, we can symbolically build the Jacobian and then rebuild the ODEProblem:
5959

6060
```@example mtkize
61-
prob_jac = ODEProblem(complete(sys), [], (0.0, 1e5), jac = true)
61+
prob_jac = ODEProblem(sys, [], (0.0, 1e5), jac = true)
6262
```

docs/src/tutorials/nonlinear.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ using ModelingToolkit, NonlinearSolve
1616
eqs = [0 ~ σ * (y - x),
1717
0 ~ x * (ρ - z) - y,
1818
0 ~ x * y - β * z]
19-
@named ns = NonlinearSystem(eqs, [x, y, z], [σ, ρ, β])
20-
ns = complete(ns)
19+
@mtkbuild ns = NonlinearSystem(eqs, [x, y, z], [σ, ρ, β])
2120
2221
guess = [x => 1.0,
2322
y => 0.0,

docs/src/tutorials/optimization.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Now we can define our optimization problem.
1919
end
2020
@parameters a=1 b=1
2121
loss = (a - x)^2 + b * (y - x^2)^2
22-
@named sys = OptimizationSystem(loss, [x, y], [a, b])
22+
@mtkbuild sys = OptimizationSystem(loss, [x, y], [a, b])
2323
```
2424

2525
A visualization of the objective function is depicted below.
@@ -50,7 +50,7 @@ u0 = [x => 1.0
5050
p = [a => 1.0
5151
b => 100.0]
5252
53-
prob = OptimizationProblem(complete(sys), u0, p, grad = true, hess = true)
53+
prob = OptimizationProblem(sys, u0, p, grad = true, hess = true)
5454
solve(prob, GradientDescent())
5555
```
5656

@@ -68,10 +68,10 @@ loss = (a - x)^2 + b * (y - x^2)^2
6868
cons = [
6969
x^2 + y^2 ≲ 1,
7070
]
71-
@named sys = OptimizationSystem(loss, [x, y], [a, b], constraints = cons)
71+
@mtkbuild sys = OptimizationSystem(loss, [x, y], [a, b], constraints = cons)
7272
u0 = [x => 0.14
7373
y => 0.14]
74-
prob = OptimizationProblem(complete(sys),
74+
prob = OptimizationProblem(sys,
7575
u0,
7676
grad = true,
7777
hess = true,

0 commit comments

Comments
 (0)