@@ -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]
2929end
3030
3131@mtkmodel Ground begin
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
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
101101end
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)
106104u0 = [
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))
110108sol = solve(prob, Tsit5())
111109plot(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]
136134end
137135```
138136
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
276274the 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
282280This 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:
300298parameters(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
323310with two state variables. One of the equations is a differential equation,
324311while the other is an algebraic equation. We can then give the values for the
325312initial conditions of our states, and solve the system by converting it to
@@ -331,20 +318,20 @@ This is done as follows:
331318u0 = [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))
335322sol = solve(prob, Rodas4())
336323plot(sol)
337324```
338325
339- Since we have run ` structural_simplify ` , MTK can numerically solve all the
326+ MTK can numerically solve all the
340327unreduced algebraic equations using the ` ODAEProblem ` (note the
341328letter ` A ` ):
342329
343330``` @example acausal
344331u0 = [
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))
348335sol = solve(prob, Rodas4())
349336plot(sol)
350337```
@@ -357,7 +344,7 @@ like `structural_simplify` simply change state variables into observables which
357344defined by ` observed ` equations.
358345
359346``` @example acausal
360- observed(sys )
347+ observed(rc_model )
361348```
362349
363350These are explicit algebraic equations which can then be used to reconstruct
0 commit comments