@@ -24,8 +24,8 @@ using ModelingToolkit, Plots, DifferentialEquations
24
24
25
25
@variables t
26
26
@connector Pin begin
27
- v(t) = 1.0
28
- i(t) = 1.0 , [connect = Flow]
27
+ v(t)
28
+ i(t), [connect = Flow]
29
29
end
30
30
31
31
@mtkmodel Ground begin
43
43
n = Pin()
44
44
end
45
45
@variables begin
46
- v(t) = 1.0
47
- i(t) = 1.0
46
+ v(t)
47
+ i(t)
48
48
end
49
49
@equations begin
50
50
v ~ p.v - n.v
56
56
@mtkmodel Resistor begin
57
57
@extend v, i = oneport = OnePort()
58
58
@parameters begin
59
- R = 1.0
59
+ R = 1.0 # Sets the default resistance
60
60
end
61
61
@equations begin
62
62
v ~ i * R
@@ -100,13 +100,11 @@ end
100
100
end
101
101
end
102
102
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)
106
104
u0 = [
107
105
rc_model.capacitor.v => 0.0,
108
106
]
109
- prob = ODAEProblem(sys , u0, (0, 10.0))
107
+ prob = ODAEProblem(rc_model , u0, (0, 10.0))
110
108
sol = solve(prob, Tsit5())
111
109
plot(sol)
112
110
```
@@ -131,8 +129,8 @@ default, variables are equal in a connection.
131
129
132
130
``` @example acausal
133
131
@connector Pin begin
134
- v(t) = 1.0
135
- i(t) = 1.0 , [connect = Flow]
132
+ v(t)
133
+ i(t), [connect = Flow]
136
134
end
137
135
```
138
136
175
173
n = Pin()
176
174
end
177
175
@variables begin
178
- v(t) = 1.0
179
- i(t) = 1.0
176
+ v(t)
177
+ i(t)
180
178
end
181
179
@equations begin
182
180
v ~ p.v - n.v
@@ -276,7 +274,7 @@ We can create a RCModel component with `@named`. And using `subcomponent_name.pa
276
274
the parameters or defaults values of variables of subcomponents.
277
275
278
276
``` @example acausal
279
- @named rc_model = RCModel(resistor.R = 2.0)
277
+ @mtkbuild rc_model = RCModel(resistor.R = 2.0)
280
278
```
281
279
282
280
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:
300
298
parameters(rc_model)
301
299
```
302
300
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:
312
302
313
303
``` @example acausal
314
- sys = structural_simplify(rc_model)
315
- equations(sys)
304
+ observed(rc_model)
316
305
```
317
306
318
- ``` @example acausal
319
- states(sys)
320
- ```
307
+ ## Solving this System
321
308
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
323
310
with two state variables. One of the equations is a differential equation,
324
311
while the other is an algebraic equation. We can then give the values for the
325
312
initial conditions of our states, and solve the system by converting it to
@@ -331,20 +318,20 @@ This is done as follows:
331
318
u0 = [rc_model.capacitor.v => 0.0
332
319
rc_model.capacitor.p.i => 0.0]
333
320
334
- prob = ODEProblem(sys , u0, (0, 10.0))
321
+ prob = ODEProblem(rc_model , u0, (0, 10.0))
335
322
sol = solve(prob, Rodas4())
336
323
plot(sol)
337
324
```
338
325
339
- Since we have run ` structural_simplify ` , MTK can numerically solve all the
326
+ MTK can numerically solve all the
340
327
unreduced algebraic equations using the ` ODAEProblem ` (note the
341
328
letter ` A ` ):
342
329
343
330
``` @example acausal
344
331
u0 = [
345
332
rc_model.capacitor.v => 0.0,
346
333
]
347
- prob = ODAEProblem(sys , u0, (0, 10.0))
334
+ prob = ODAEProblem(rc_model , u0, (0, 10.0))
348
335
sol = solve(prob, Rodas4())
349
336
plot(sol)
350
337
```
@@ -357,7 +344,7 @@ like `structural_simplify` simply change state variables into observables which
357
344
defined by ` observed ` equations.
358
345
359
346
``` @example acausal
360
- observed(sys )
347
+ observed(rc_model )
361
348
```
362
349
363
350
These are explicit algebraic equations which can then be used to reconstruct
0 commit comments