Skip to content

Commit a4256d1

Browse files
committed
refactor: redefine the components in tutorials with @model
1 parent 1a22c77 commit a4256d1

File tree

20 files changed

+303
-154
lines changed

20 files changed

+303
-154
lines changed

docs/src/API/linear_analysis.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ The following example builds a simple closed-loop system with a plant $P$ and a
5757
```@example LINEAR_ANALYSIS
5858
using ModelingToolkitStandardLibrary.Blocks, ModelingToolkit
5959
@named P = FirstOrder(k = 1, T = 1) # A first-order system with pole in -1
60-
@named C = Gain(-1) # A P controller
60+
@named C = Gain(; k = -1) # A P controller
6161
t = ModelingToolkit.get_iv(P)
6262
eqs = [connect(P.output, :plant_output, C.input) # Connect with an automatically created analysis point called :plant_output
6363
connect(C.output, :plant_input, P.input)]

docs/src/tutorials/rc_circuit.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ rc_eqs = [connect(constant.output, source.V)
2929
@named rc_model = ODESystem(rc_eqs, t,
3030
systems = [resistor, capacitor, constant, source, ground])
3131
sys = structural_simplify(rc_model)
32-
prob = ODAEProblem(sys, Pair[], (0, 10.0))
32+
prob = ODAEProblem(sys, Pair[capacitor.v_start => 1], (0, 10.0))
3333
sol = solve(prob, Tsit5())
3434
plot(sol, vars = [capacitor.v, resistor.i],
3535
title = "RC Circuit Demonstration",

fmt/Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[deps]
2+
JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899"

format/Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[deps]
2+
JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899"

src/Blocks/analysis_points.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ See also [`get_sensitivity`](@ref), [`get_comp_sensitivity`](@ref), [`get_looptr
4646
```julia
4747
using ModelingToolkitStandardLibrary.Blocks
4848
@named P = FirstOrder(k = 1, T = 1)
49-
@named C = Gain(-1)
49+
@named C = Gain(; k = -1)
5050
t = ModelingToolkit.get_iv(P)
5151
eqs = [connect(P.output, C.input)
5252
connect(C.output, :plant_input, P.input)]

src/Blocks/math.jl

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Gain(k; name)
2+
Gain(; name, k)
33
44
Output the product of a gain value with the input signal.
55
@@ -12,15 +12,16 @@ Output the product of a gain value with the input signal.
1212
- `input`
1313
- `output`
1414
"""
15-
@component function Gain(k; name)
16-
@named siso = SISO()
17-
@unpack u, y = siso
18-
pars = @parameters k=k [description = "Gain of Gain $name"]
19-
eqs = [
20-
y ~ k * u,
21-
]
22-
extend(ODESystem(eqs, t, [], pars; name = name), siso)
15+
@model Gain begin
16+
@extend u, y = siso = SISO()
17+
@parameters begin
18+
k, [description = "Gain function"]
19+
end
20+
@equations begin
21+
y ~ k * u
22+
end
2323
end
24+
Gain.f(k; name) = Gain.f(; k, name)
2425

2526
"""
2627
MatrixGain(K::AbstractArray; name)
@@ -78,14 +79,15 @@ Output difference between reference input (input1) and feedback input (input2).
7879
- `input2`
7980
- `output`
8081
"""
81-
@component function Feedback(; name)
82-
@named input1 = RealInput()
83-
@named input2 = RealInput()
84-
@named output = RealOutput()
85-
eqs = [
86-
output.u ~ input1.u - input2.u,
87-
]
88-
return compose(ODESystem(eqs, t, [], []; name = name), input1, input2, output)
82+
@model Feedback begin
83+
@components begin
84+
input1 = RealInput()
85+
input2 = RealInput()
86+
output = RealOutput()
87+
end
88+
@equations begin
89+
output.u ~ input1.u - input2.u
90+
end
8991
end
9092

9193
"""

src/Blocks/sources.jl

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,16 @@ Generate constant signal.
6969
7070
- `output`
7171
"""
72-
@component function Constant(; name, k = 1)
73-
@named output = RealOutput()
74-
pars = @parameters k=k [description = "Constant output value of block $name"]
75-
eqs = [
76-
output.u ~ k,
77-
]
78-
compose(ODESystem(eqs, t, [], pars; name = name), [output])
72+
@model Constant begin
73+
@components begin
74+
output = RealOutput()
75+
end
76+
@parameters begin
77+
k, [description = "Constant output value of block $name"]
78+
end
79+
@equations begin
80+
output.u ~ k
81+
end
7982
end
8083

8184
"""
@@ -548,7 +551,7 @@ end
548551
"""
549552
SampledData(; name, buffer)
550553
551-
data input component.
554+
data input component.
552555
553556
# Parameters:
554557
- `buffer`: a `Parameter` type which holds the data and sample time

src/Blocks/utils.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
end
1414
ODESystem(Equation[], t, [u...], []; name = name)
1515
end
16+
17+
#=
18+
@connector RealInput begin
19+
u(t), [input = true]
20+
end
21+
=#
1622
@doc """
1723
RealInput(;name, nin, u_start)
1824
@@ -25,7 +31,6 @@ Connector with one input signal of type Real.
2531
# States:
2632
- `u`: Value of the connector; if nin=1 this is a scalar
2733
""" RealInput
28-
2934
@connector function RealOutput(; name, nout = 1, u_start = nout > 1 ? zeros(nout) : 0.0)
3035
if nout == 1
3136
@variables u(t)=u_start [
@@ -41,6 +46,10 @@ Connector with one input signal of type Real.
4146
end
4247
ODESystem(Equation[], t, [u...], []; name = name)
4348
end
49+
50+
#=@connector RealOutput begin
51+
u(t), [output = true]
52+
end=#
4453
@doc """
4554
RealOutput(;name, nout, u_start)
4655

src/Electrical/Analog/ideal_components.jl

Lines changed: 70 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ node.
88
99
- `g`
1010
"""
11-
@component function Ground(; name)
12-
@named g = Pin()
13-
eqs = [g.v ~ 0]
14-
ODESystem(eqs, t, [], []; systems = [g], name = name)
11+
@model Ground begin
12+
@components begin
13+
g = Pin()
14+
end
15+
@equations begin
16+
g.v ~ 0
17+
end
1518
end
1619

1720
"""
@@ -32,14 +35,14 @@ See [OnePort](@ref)
3235
3336
- `R`: [`Ohm`] Resistance
3437
"""
35-
@component function Resistor(; name, R)
36-
@named oneport = OnePort()
37-
@unpack v, i = oneport
38-
pars = @parameters R = R
39-
eqs = [
40-
v ~ i * R,
41-
]
42-
extend(ODESystem(eqs, t, [], pars; name = name), oneport)
38+
@model Resistor begin
39+
@extend v, i = oneport = OnePort()
40+
@parameters begin
41+
R
42+
end
43+
@equations begin
44+
v ~ i * R
45+
end
4346
end
4447

4548
"""
@@ -60,18 +63,18 @@ See [OnePort](@ref)
6063
6164
- `G`: [`S`] Conductance
6265
"""
63-
@component function Conductor(; name, G)
64-
@named oneport = OnePort()
65-
@unpack v, i = oneport
66-
pars = @parameters G = G
67-
eqs = [
68-
i ~ v * G,
69-
]
70-
extend(ODESystem(eqs, t, [], pars; name = name), oneport)
66+
@model Conductor begin
67+
@extend v, i = oneport = OnePort()
68+
@parameters begin
69+
G
70+
end
71+
@equations begin
72+
i ~ v * G
73+
end
7174
end
7275

7376
"""
74-
Capacitor(; name, C)
77+
Capacitor(; name, C, v_start)
7578
7679
Creates an ideal capacitor.
7780
@@ -87,20 +90,23 @@ Creates an ideal capacitor.
8790
# Parameters:
8891
8992
- `C`: [`F`] Capacitance
90-
- `v_start`: [`V`] Initial voltage of capacitor
93+
- `v`: [`V`] Initial voltage of capacitor
9194
"""
92-
@component function Capacitor(; name, C, v_start = 0.0)
93-
@named oneport = OnePort(; v_start = v_start)
94-
@unpack v, i = oneport
95-
pars = @parameters C = C
96-
eqs = [
97-
D(v) ~ i / C,
98-
]
99-
extend(ODESystem(eqs, t, [], pars; name = name), oneport)
95+
@model Capacitor begin
96+
@parameters begin
97+
C
98+
end
99+
@variables begin
100+
v
101+
end
102+
@extend v, i = oneport = OnePort(; v = v)
103+
@equations begin
104+
D(v) ~ i / C
105+
end
100106
end
101107

102108
"""
103-
Inductor(; name, L)
109+
Inductor(; name, L, i_start)
104110
105111
Creates an ideal Inductor.
106112
@@ -116,16 +122,19 @@ See [OnePort](@ref)
116122
# Parameters:
117123
118124
- `L`: [`H`] Inductance
119-
- `i_start`: [`A`] Initial current through inductor
125+
- `i`: [`A`] Initial current through inductor
120126
"""
121-
@component function Inductor(; name, L, i_start = 0.0)
122-
@named oneport = OnePort(; i_start = i_start)
123-
@unpack v, i = oneport
124-
pars = @parameters L = L
125-
eqs = [
126-
D(i) ~ 1 / L * v,
127-
]
128-
extend(ODESystem(eqs, t, [], pars; name = name), oneport)
127+
@model Inductor begin # name, L, i_start = 0.0)
128+
@parameters begin
129+
L
130+
end
131+
@variables begin
132+
i
133+
end
134+
@extend v, i = oneport = OnePort(; i = i)
135+
@equations begin
136+
D(i) ~ 1 / L * v
137+
end
129138
end
130139

131140
"""
@@ -213,7 +222,7 @@ Temperature dependent electrical resistor
213222
end
214223

215224
"""
216-
EMF(;name, k)
225+
EMF(; name, k)
217226
218227
Electromotoric force (electric/mechanic transformer)
219228
@@ -235,19 +244,29 @@ Electromotoric force (electric/mechanic transformer)
235244
236245
- `k`: [`N⋅m/A`] Transformation coefficient
237246
"""
238-
@component function EMF(; name, k)
239-
@named p = Pin()
240-
@named n = Pin()
241-
@named flange = Flange()
242-
@named support = Support()
243-
@parameters k = k
244-
@variables v(t)=0.0 i(t)=0.0 phi(t)=0.0 w(t)=0.0
245-
eqs = [v ~ p.v - n.v
247+
@model EMF begin
248+
@components begin
249+
p = Pin()
250+
n = Pin()
251+
flange = Flange()
252+
support = Support()
253+
end
254+
@parameters begin
255+
k
256+
end
257+
@variables begin
258+
v(t) = 0.0
259+
i(t) = 0.0
260+
phi(t) = 0.0
261+
w(t) = 0.0
262+
end
263+
@equations begin
264+
v ~ p.v - n.v
246265
0 ~ p.i + n.i
247266
i ~ p.i
248267
phi ~ flange.phi - support.phi
249268
D(phi) ~ w
250269
k * w ~ v
251-
flange.tau ~ -k * i]
252-
ODESystem(eqs, t, [v, i, phi, w], [k]; name = name, systems = [p, n, flange, support])
270+
flange.tau ~ -k * i
271+
end
253272
end

src/Electrical/Analog/sources.jl

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@ See [OnePort](@ref)
1313
- `n` Negative pin
1414
- `V` [RealInput](@ref) Input for the voltage control signal, i.e. `V ~ p.v - n.v`
1515
"""
16-
@component function Voltage(; name)
17-
@named oneport = OnePort()
18-
@unpack v, i = oneport
19-
@named V = RealInput()
20-
eqs = [
21-
v ~ V.u,
22-
]
23-
24-
extend(ODESystem(eqs, t, [], []; name = name, systems = [V]), oneport)
16+
@model Voltage begin
17+
@extend v, i = oneport = OnePort()
18+
@components begin
19+
V = RealInput()
20+
end
21+
@equations begin
22+
v ~ V.u
23+
end
2524
end
2625

2726
"""
@@ -39,13 +38,12 @@ See [OnePort](@ref)
3938
- `n` Negative pin
4039
- `I` [RealInput](@ref) Input for the current control signal, i.e. `I ~ p.i
4140
"""
42-
@component function Current(; name)
43-
@named oneport = OnePort()
44-
@unpack v, i = oneport
45-
@named I = RealInput()
46-
eqs = [
47-
i ~ I.u,
48-
]
49-
50-
extend(ODESystem(eqs, t, [], []; name = name, systems = [I]), oneport)
41+
@model Current begin
42+
@extend v, i = oneport = OnePort()
43+
@components begin
44+
I = RealInput()
45+
end
46+
@equations begin
47+
i ~ I.u
48+
end
5149
end

0 commit comments

Comments
 (0)