Skip to content

Commit 17a3b6a

Browse files
Merge pull request #2305 from SciML/mtkbuild
Add `@mtkbuild` greatly simplify the tutorials
2 parents 3034c9f + c679dd9 commit 17a3b6a

File tree

15 files changed

+257
-138
lines changed

15 files changed

+257
-138
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@
44
Manifest.toml
55
.vscode
66
.vscode/*
7+
docs/src/assets/Project.toml
8+
docs/src/assets/Manifest.toml

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ModelingToolkit"
22
uuid = "961ee093-0014-501f-94e3-6117800e7a78"
33
authors = ["Yingbo Ma <[email protected]>", "Chris Rackauckas <[email protected]> and contributors"]
4-
version = "8.71.2"
4+
version = "8.72.0"
55

66
[deps]
77
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"

docs/pages.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ pages = [
55
"tutorials/nonlinear.md",
66
"tutorials/optimization.md",
77
"tutorials/modelingtoolkitize.md",
8+
"tutorials/programmatically_generating.md",
89
"tutorials/stochastic_diffeq.md",
910
"tutorials/parameter_identifiability.md",
1011
"tutorials/domain_connections.md"],

docs/src/tutorials/acausal_components.md

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ using ModelingToolkit, Plots, DifferentialEquations
2424
2525
@variables t
2626
@connector Pin begin
27-
v(t) = 1.0
28-
i(t) = 1.0, [connect = Flow]
27+
v(t)
28+
i(t), [connect = Flow]
2929
end
3030
3131
@mtkmodel Ground begin
@@ -43,8 +43,8 @@ end
4343
n = Pin()
4444
end
4545
@variables begin
46-
v(t) = 1.0
47-
i(t) = 1.0
46+
v(t)
47+
i(t)
4848
end
4949
@equations begin
5050
v ~ p.v - n.v
@@ -56,7 +56,7 @@ end
5656
@mtkmodel Resistor begin
5757
@extend v, i = oneport = OnePort()
5858
@parameters begin
59-
R = 1.0
59+
R = 1.0 # Sets the default resistance
6060
end
6161
@equations begin
6262
v ~ i * R
@@ -100,13 +100,11 @@ end
100100
end
101101
end
102102
103-
@named rc_model = RCModel(resistor.R = 2.0)
104-
rc_model = complete(rc_model)
105-
sys = structural_simplify(rc_model)
103+
@mtkbuild rc_model = RCModel(resistor.R = 2.0)
106104
u0 = [
107105
rc_model.capacitor.v => 0.0,
108106
]
109-
prob = ODAEProblem(sys, u0, (0, 10.0))
107+
prob = ODAEProblem(rc_model, u0, (0, 10.0))
110108
sol = solve(prob, Tsit5())
111109
plot(sol)
112110
```
@@ -131,8 +129,8 @@ default, variables are equal in a connection.
131129

132130
```@example acausal
133131
@connector Pin begin
134-
v(t) = 1.0
135-
i(t) = 1.0, [connect = Flow]
132+
v(t)
133+
i(t), [connect = Flow]
136134
end
137135
```
138136

@@ -175,8 +173,8 @@ pin.
175173
n = Pin()
176174
end
177175
@variables begin
178-
v(t) = 1.0
179-
i(t) = 1.0
176+
v(t)
177+
i(t)
180178
end
181179
@equations begin
182180
v ~ p.v - n.v
@@ -276,7 +274,7 @@ We can create a RCModel component with `@named`. And using `subcomponent_name.pa
276274
the parameters or defaults values of variables of subcomponents.
277275

278276
```@example acausal
279-
@named rc_model = RCModel(resistor.R = 2.0)
277+
@mtkbuild rc_model = RCModel(resistor.R = 2.0)
280278
```
281279

282280
This model is acausal because we have not specified anything about the causality of the model. We have
@@ -300,26 +298,15 @@ and the parameters are:
300298
parameters(rc_model)
301299
```
302300

303-
## Simplifying and Solving this System
304-
305-
This system could be solved directly as a DAE using [one of the DAE solvers
306-
from DifferentialEquations.jl](https://docs.sciml.ai/DiffEqDocs/stable/solvers/dae_solve/).
307-
However, let's take a second to symbolically simplify the system before doing the
308-
solve. Although we can use ODE solvers that handle mass matrices to solve the
309-
above system directly, we want to run the `structural_simplify` function first,
310-
as it eliminates many unnecessary variables to build the leanest numerical
311-
representation of the system. Let's see what it does here:
301+
The observed equations are:
312302

313303
```@example acausal
314-
sys = structural_simplify(rc_model)
315-
equations(sys)
304+
observed(rc_model)
316305
```
317306

318-
```@example acausal
319-
states(sys)
320-
```
307+
## Solving this System
321308

322-
After structural simplification, we are left with a system of only two equations
309+
We are left with a system of only two equations
323310
with two state variables. One of the equations is a differential equation,
324311
while the other is an algebraic equation. We can then give the values for the
325312
initial conditions of our states, and solve the system by converting it to
@@ -331,20 +318,20 @@ This is done as follows:
331318
u0 = [rc_model.capacitor.v => 0.0
332319
rc_model.capacitor.p.i => 0.0]
333320
334-
prob = ODEProblem(sys, u0, (0, 10.0))
321+
prob = ODEProblem(rc_model, u0, (0, 10.0))
335322
sol = solve(prob, Rodas4())
336323
plot(sol)
337324
```
338325

339-
Since we have run `structural_simplify`, MTK can numerically solve all the
326+
MTK can numerically solve all the
340327
unreduced algebraic equations using the `ODAEProblem` (note the
341328
letter `A`):
342329

343330
```@example acausal
344331
u0 = [
345332
rc_model.capacitor.v => 0.0,
346333
]
347-
prob = ODAEProblem(sys, u0, (0, 10.0))
334+
prob = ODAEProblem(rc_model, u0, (0, 10.0))
348335
sol = solve(prob, Rodas4())
349336
plot(sol)
350337
```
@@ -357,7 +344,7 @@ like `structural_simplify` simply change state variables into observables which
357344
defined by `observed` equations.
358345

359346
```@example acausal
360-
observed(sys)
347+
observed(rc_model)
361348
```
362349

363350
These are explicit algebraic equations which can then be used to reconstruct

0 commit comments

Comments
 (0)