Skip to content

Commit c66d73a

Browse files
authored
Use systems = @nAmed begin .. end idiom to define systems in tutorials
1 parent 76b07b3 commit c66d73a

File tree

6 files changed

+71
-74
lines changed

6 files changed

+71
-74
lines changed

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,20 @@ R = 1.0
5353
C = 1.0
5454
V = 1.0
5555
@variables t
56-
@named resistor = Resistor(R = R)
57-
@named capacitor = Capacitor(C = C)
58-
@named source = Voltage()
59-
@named constant = Constant(k = V)
60-
@named ground = Ground()
56+
systems = @named begin
57+
resistor = Resistor(R = R)
58+
capacitor = Capacitor(C = C)
59+
source = Voltage()
60+
constant = Constant(k = V)
61+
ground = Ground()
62+
end
6163

6264
rc_eqs = [connect(constant.output, source.V)
6365
connect(source.p, resistor.p)
6466
connect(resistor.n, capacitor.p)
6567
connect(capacitor.n, source.n, ground.g)]
6668

67-
@named rc_model = ODESystem(rc_eqs, t,
68-
systems = [resistor, capacitor, constant, source, ground])
69+
@named rc_model = ODESystem(rc_eqs, t; systems)
6970
sys = structural_simplify(rc_model)
7071
prob = ODEProblem(sys, Pair[], (0, 10.0))
7172
sol = solve(prob, Tsit5())

docs/src/connectors/connections.md

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,16 @@ using ModelingToolkitStandardLibrary.Electrical, ModelingToolkit, DifferentialEq
9393
using ModelingToolkit: t_nounits as t
9494
using Plots
9595
96-
@named resistor = Resistor(R = 1)
97-
@named capacitor = Capacitor(C = 1)
98-
@named ground = Ground()
96+
systems = @named begin
97+
resistor = Resistor(R = 1)
98+
capacitor = Capacitor(C = 1)
99+
ground = Ground()
100+
end
99101
100102
eqs = [connect(capacitor.p, resistor.p)
101103
connect(resistor.n, ground.g, capacitor.n)]
102104
103-
@named model = ODESystem(eqs, t; systems = [resistor, capacitor, ground])
105+
@named model = ODESystem(eqs, t; systems)
104106
105107
sys = structural_simplify(model)
106108
@@ -133,14 +135,16 @@ Now using the Translational library based on velocity, we can see the same relat
133135
using ModelingToolkitStandardLibrary
134136
const TV = ModelingToolkitStandardLibrary.Mechanical.Translational
135137
136-
@named damping = TV.Damper(d = 1, flange_a.v = 1)
137-
@named body = TV.Mass(m = 1, v = 1)
138-
@named ground = TV.Fixed()
138+
systems = @named begin
139+
damping = TV.Damper(d = 1, flange_a.v = 1)
140+
body = TV.Mass(m = 1, v = 1)
141+
ground = TV.Fixed()
142+
end
139143
140144
eqs = [connect(damping.flange_a, body.flange)
141145
connect(ground.flange, damping.flange_b)]
142146
143-
@named model = ODESystem(eqs, t; systems = [damping, body, ground])
147+
@named model = ODESystem(eqs, t; systems)
144148
145149
sys = structural_simplify(model)
146150
@@ -166,14 +170,16 @@ Now, let's consider the position-based approach. We can build the same model wi
166170
```@example connections
167171
const TP = ModelingToolkitStandardLibrary.Mechanical.TranslationalPosition
168172
169-
@named damping = TP.Damper(d = 1, va = 1, vb = 0.0)
170-
@named body = TP.Mass(m = 1, v = 1)
171-
@named ground = TP.Fixed(s_0 = 0)
173+
systems = @named begin
174+
damping = TP.Damper(d = 1, va = 1, vb = 0.0)
175+
body = TP.Mass(m = 1, v = 1)
176+
ground = TP.Fixed(s_0 = 0)
177+
end
172178
173179
eqs = [connect(damping.flange_a, body.flange)
174180
connect(ground.flange, damping.flange_b)]
175181
176-
@named model = ODESystem(eqs, t; systems = [damping, body, ground])
182+
@named model = ODESystem(eqs, t; systems)
177183
178184
sys = structural_simplify(model)
179185

docs/src/tutorials/custom_component.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,17 @@ extend(ODESystem(eqs, t, [], pars; name = name), oneport)
9090
The final model can now be created with the components from the library and the new custom component.
9191

9292
```@example components
93-
@named L = Inductor(L = 18)
94-
@named Ro = Resistor(R = 12.5e-3)
95-
@named G = Conductor(G = 0.565)
96-
@named C1 = Capacitor(C = 10, v = 4)
97-
@named C2 = Capacitor(C = 100)
98-
@named Nr = NonlinearResistor(Ga = -0.757576,
99-
Gb = -0.409091,
100-
Ve = 1)
101-
@named Gnd = Ground()
93+
systems = @named begin
94+
L = Inductor(L = 18)
95+
Ro = Resistor(R = 12.5e-3)
96+
G = Conductor(G = 0.565)
97+
C1 = Capacitor(C = 10, v = 4)
98+
C2 = Capacitor(C = 100)
99+
Nr = NonlinearResistor(Ga = -0.757576,
100+
Gb = -0.409091,
101+
Ve = 1)
102+
Gnd = Ground()
103+
end
102104
103105
connections = [connect(L.p, G.p)
104106
connect(G.n, Nr.p)
@@ -110,7 +112,7 @@ connections = [connect(L.p, G.p)
110112
connect(C2.n, Gnd.g)
111113
connect(Ro.n, Gnd.g)]
112114
113-
@named model = ODESystem(connections, t, systems = [L, Ro, G, C1, C2, Nr, Gnd])
115+
@named model = ODESystem(connections, t; systems)
114116
nothing # hide
115117
```
116118

docs/src/tutorials/dc_motor_pi.md

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,22 @@ nothing # hide
3333
The actual model can now be composed.
3434

3535
```@example dc_motor_pi
36-
@named ground = Ground()
37-
@named source = Voltage()
38-
@named ref = Blocks.Step(height = 1, start_time = 0)
39-
@named pi_controller = Blocks.LimPI(k = 1.1, T = 0.035, u_max = 10, Ta = 0.035)
40-
@named feedback = Blocks.Feedback()
41-
@named R1 = Resistor(R = R)
42-
@named L1 = Inductor(L = L)
43-
@named emf = EMF(k = k)
44-
@named fixed = Fixed()
45-
@named load = Torque()
46-
@named load_step = Blocks.Step(height = tau_L_step, start_time = 3)
47-
@named inertia = Inertia(J = J)
48-
@named friction = Damper(d = f)
49-
@named speed_sensor = SpeedSensor()
36+
systems = @named begin
37+
ground = Ground()
38+
source = Voltage()
39+
ref = Blocks.Step(height = 1, start_time = 0)
40+
pi_controller = Blocks.LimPI(k = 1.1, T = 0.035, u_max = 10, Ta = 0.035)
41+
feedback = Blocks.Feedback()
42+
R1 = Resistor(R = R)
43+
L1 = Inductor(L = L)
44+
emf = EMF(k = k)
45+
fixed = Fixed()
46+
load = Torque()
47+
load_step = Blocks.Step(height = tau_L_step, start_time = 3)
48+
inertia = Inertia(J = J)
49+
friction = Damper(d = f)
50+
speed_sensor = SpeedSensor()
51+
end
5052
5153
connections = [connect(fixed.flange, emf.support, friction.flange_b)
5254
connect(emf.flange, friction.flange_a, inertia.flange_a)
@@ -62,23 +64,7 @@ connections = [connect(fixed.flange, emf.support, friction.flange_b)
6264
connect(L1.n, emf.p)
6365
connect(emf.n, source.n, ground.g)]
6466
65-
@named model = ODESystem(connections, t,
66-
systems = [
67-
ground,
68-
ref,
69-
pi_controller,
70-
feedback,
71-
source,
72-
R1,
73-
L1,
74-
emf,
75-
fixed,
76-
load,
77-
load_step,
78-
inertia,
79-
friction,
80-
speed_sensor
81-
])
67+
@named model = ODESystem(connections, t; systems)
8268
nothing # hide
8369
```
8470

docs/src/tutorials/rc_circuit.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,20 @@ using ModelingToolkit: t_nounits as t
1515
R = 1.0
1616
C = 1.0
1717
V = 1.0
18-
@named resistor = Resistor(R = R)
19-
@named capacitor = Capacitor(C = C, v = 0.0)
20-
@named source = Voltage()
21-
@named constant = Constant(k = V)
22-
@named ground = Ground()
18+
systems = @named begin
19+
resistor = Resistor(R = R)
20+
capacitor = Capacitor(C = C, v = 0.0)
21+
source = Voltage()
22+
constant = Constant(k = V)
23+
ground = Ground()
24+
end
2325
2426
rc_eqs = [connect(constant.output, source.V)
2527
connect(source.p, resistor.p)
2628
connect(resistor.n, capacitor.p)
2729
connect(capacitor.n, source.n, ground.g)]
2830
29-
@named rc_model = ODESystem(rc_eqs, t,
30-
systems = [resistor, capacitor, constant, source, ground])
31+
@named rc_model = ODESystem(rc_eqs, t; systems)
3132
sys = structural_simplify(rc_model)
3233
prob = ODEProblem(sys, Pair[], (0, 10.0))
3334
sol = solve(prob, Tsit5())

docs/src/tutorials/thermal_model.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ using ModelingToolkit: t_nounits as t
1212
1313
C1 = 15
1414
C2 = 15
15-
@named mass1 = HeatCapacitor(C = C1, T = 373.15)
16-
@named mass2 = HeatCapacitor(C = C2, T = 273.15)
17-
@named conduction = ThermalConductor(G = 10)
18-
@named Tsensor1 = TemperatureSensor()
19-
@named Tsensor2 = TemperatureSensor()
15+
systems = @named begin
16+
mass1 = HeatCapacitor(C = C1, T = 373.15)
17+
mass2 = HeatCapacitor(C = C2, T = 273.15)
18+
conduction = ThermalConductor(G = 10)
19+
Tsensor1 = TemperatureSensor()
20+
Tsensor2 = TemperatureSensor()
21+
end
2022
2123
connections = [
2224
connect(mass1.port, conduction.port_a),
@@ -25,8 +27,7 @@ connections = [
2527
connect(mass2.port, Tsensor2.port)
2628
]
2729
28-
@named model = ODESystem(connections, t,
29-
systems = [mass1, mass2, conduction, Tsensor1, Tsensor2])
30+
@named model = ODESystem(connections, t; systems)
3031
sys = structural_simplify(model)
3132
prob = ODEProblem(sys, Pair[], (0, 5.0))
3233
sol = solve(prob, Tsit5())

0 commit comments

Comments
 (0)