Skip to content

Commit a2ecca0

Browse files
authored
Refactors Electrical module with @mtkmodel (#192)
* refactor: refactor Electrical/Analog components with `@mtkmodel` * docs: bump MTK to 8 (from 8.33) and fix custom_component example
1 parent 403829e commit a2ecca0

File tree

12 files changed

+256
-252
lines changed

12 files changed

+256
-252
lines changed

docs/Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ IfElse = "0.1"
1616
ModelingToolkit = "8"
1717
ModelingToolkitStandardLibrary = "1.8"
1818
OrdinaryDiffEq = "6.31"
19-
Plots = "1.36"
19+
Plots = "1.36"

docs/src/tutorials/custom_component.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ model NonlinearResistor "Chua's resistor"
2727
parameter SI.Conductance Ga "conductance in inner voltage range";
2828
parameter SI.Conductance Gb "conductance in outer voltage range";
2929
parameter SI.Voltage Ve "inner voltage range limit";
30-
equation
30+
equation
3131
i = if (v < -Ve) then Gb*(v + Ve) - Ga*Ve else if (v > Ve) then Gb*(v - Ve) + Ga*Ve else Ga*v;
3232
end NonlinearResistor;
3333
```
@@ -94,7 +94,7 @@ The final model can now be created with the components from the library and the
9494
@named L = Inductor(L = 18)
9595
@named Ro = Resistor(R = 12.5e-3)
9696
@named G = Conductor(G = 0.565)
97-
@named C1 = Capacitor(C = 10, v_start = 4)
97+
@named C1 = Capacitor(C = 10, v = 4)
9898
@named C2 = Capacitor(C = 100)
9999
@named Nr = NonlinearResistor(Ga = -0.757576,
100100
Gb = -0.409091,
@@ -119,7 +119,7 @@ nothing # hide
119119

120120
Now the model can be simulated.
121121
First, `structural_simplify` is called on the model and an `ODEProblem` is built from the result.
122-
Since the initial voltage of the first capacitor was already specified via `v_start`, no initial condition is given and an empty pair is supplied.
122+
Since the initial voltage of the first capacitor was already specified via `v`, no initial condition is given and an empty pair is supplied.
123123

124124
```@example components
125125
sys = structural_simplify(model)

docs/src/tutorials/rc_circuit.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ C = 1.0
1616
V = 1.0
1717
@variables t
1818
@named resistor = Resistor(R = R)
19-
@named capacitor = Capacitor(C = C)
19+
@named capacitor = Capacitor(C = C, v = 0.0)
2020
@named source = Voltage()
2121
@named constant = Constant(k = V)
2222
@named ground = Ground()

src/Electrical/Analog/ideal_components.jl

+104-82
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+
@mtkmodel Ground begin
12+
@components begin
13+
g = Pin()
14+
end
15+
@equations begin
16+
g.v ~ 0
17+
end
1518
end
1619

1720
"""
@@ -32,18 +35,18 @@ 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+
@mtkmodel Resistor begin
39+
@extend v, i = oneport = OnePort()
40+
@parameters begin
41+
R, [description = "Resistance"]
42+
end
43+
@equations begin
44+
v ~ i * R
45+
end
4346
end
4447

4548
"""
46-
Conductor(;name, G)
49+
Conductor(; name, G)
4750
4851
Creates an ideal conductor.
4952
@@ -60,24 +63,25 @@ 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+
@mtkmodel Conductor begin
67+
@extend v, i = oneport = OnePort()
68+
@parameters begin
69+
G, [description = "Conductance"]
70+
end
71+
@equations begin
72+
i ~ v * G
73+
end
7174
end
7275

7376
"""
74-
Capacitor(; name, C)
77+
Capacitor(; name, C, v)
7578
7679
Creates an ideal capacitor.
80+
Initial voltage of capacitor can be set with `v` ([`V`])
7781
7882
# States:
7983
80-
- `v(t)`: [`V`] The voltage across the capacitor, given by `D(v) ~ p.i / C`
84+
See [OnePort](@ref)
8185
8286
# Connectors:
8387
@@ -87,22 +91,25 @@ Creates an ideal capacitor.
8791
# Parameters:
8892
8993
- `C`: [`F`] Capacitance
90-
- `v_start`: [`V`] Initial voltage of capacitor
91-
"""
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)
94+
"""
95+
@mtkmodel Capacitor begin
96+
@parameters begin
97+
C, [description = "Capacitance"]
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)
104110
105111
Creates an ideal Inductor.
112+
Initial current through inductor can be set with `i` ([`A`]).
106113
107114
# States:
108115
@@ -116,16 +123,18 @@ See [OnePort](@ref)
116123
# Parameters:
117124
118125
- `L`: [`H`] Inductance
119-
- `i_start`: [`A`] Initial current through inductor
120-
"""
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)
126+
"""
127+
@mtkmodel Inductor begin
128+
@parameters begin
129+
L, [description = "Inductance"]
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
"""
@@ -146,13 +155,12 @@ See [TwoPort](@ref)
146155
- `n1` Negative pin (left port)
147156
- `n2` Negative pin (right port)
148157
"""
149-
@component function IdealOpAmp(; name)
150-
@named twoport = TwoPort()
151-
@unpack v1, v2, i1, i2 = twoport
152-
153-
eqs = [v1 ~ 0
154-
i1 ~ 0]
155-
extend(ODESystem(eqs, t, [], [], name = name), twoport)
158+
@mtkmodel IdealOpAmp begin
159+
@extend v1, v2, i1, i2 = twoport = TwoPort()
160+
@equations begin
161+
v1 ~ 0
162+
i1 ~ 0
163+
end
156164
end
157165

158166
"""
@@ -169,15 +177,15 @@ See [OnePort](@ref)
169177
- `p` Positive pin
170178
- `n` Negative pin
171179
"""
172-
@component function Short(; name)
173-
@named oneport = OnePort()
174-
@unpack v, i = oneport
175-
eqs = [v ~ 0]
176-
extend(ODESystem(eqs, t, [], []; name = name), oneport)
180+
@mtkmodel Short begin
181+
@extend v, i = oneport = OnePort()
182+
@equations begin
183+
v ~ 0
184+
end
177185
end
178186

179187
"""
180-
HeatingResistor(;name, R_ref=1.0, T_ref=300.15, alpha=0)
188+
HeatingResistor(; name, R_ref = 1.0, T_ref = 300.15, alpha = 0)
181189
182190
Temperature dependent electrical resistor
183191
@@ -195,25 +203,29 @@ Temperature dependent electrical resistor
195203
196204
- `R_ref`: [`Ω`] Reference resistance
197205
- `T_ref`: [K] Reference temperature
206+
- `alpha`: [K⁻¹] Temperature coefficient of resistance
198207
"""
199-
@component function HeatingResistor(; name, R_ref = 1.0, T_ref = 300.15, alpha = 0)
200-
@named oneport = OnePort()
201-
@unpack v, i = oneport
202-
@named heat_port = HeatPort()
203-
pars = @parameters begin
204-
R_ref = R_ref
205-
T_ref = T_ref
206-
alpha = alpha
207-
end
208-
@variables R(t) = R_ref
209-
eqs = [R ~ R_ref * (1 + alpha * (heat_port.T - T_ref))
208+
@mtkmodel HeatingResistor begin
209+
@extend v, i = oneport = OnePort()
210+
@components begin
211+
heat_port = HeatPort()
212+
end
213+
@parameters begin
214+
R_ref = 1.0, [description = "Reference resistance"]
215+
T_ref = 300.15, [description = "Reference temperature"]
216+
alpha = 0, [description = "Temperature coefficient of resistance"]
217+
end
218+
@variables begin
219+
R(t) = R_ref
220+
end
221+
@equations begin
222+
R ~ R_ref * (1 + alpha * (heat_port.T - T_ref))
210223
heat_port.Q_flow ~ -v * i # -LossPower
211-
v ~ i * R]
212-
extend(ODESystem(eqs, t, [R], pars; name = name, systems = [heat_port]), oneport)
224+
v ~ i * R
225+
end
213226
end
214-
215227
"""
216-
EMF(;name, k)
228+
EMF(; name, k)
217229
218230
Electromotoric force (electric/mechanic transformer)
219231
@@ -235,19 +247,29 @@ Electromotoric force (electric/mechanic transformer)
235247
236248
- `k`: [`N⋅m/A`] Transformation coefficient
237249
"""
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
250+
@mtkmodel EMF begin
251+
@components begin
252+
p = Pin()
253+
n = Pin()
254+
flange = Flange()
255+
support = Support()
256+
end
257+
@parameters begin
258+
k, [description = "Transformation coefficient"]
259+
end
260+
@variables begin
261+
v(t) = 0.0
262+
i(t) = 0.0
263+
phi(t) = 0.0
264+
w(t) = 0.0
265+
end
266+
@equations begin
267+
v ~ p.v - n.v
246268
0 ~ p.i + n.i
247269
i ~ p.i
248270
phi ~ flange.phi - support.phi
249271
D(phi) ~ w
250272
k * w ~ v
251-
flange.tau ~ -k * i]
252-
ODESystem(eqs, t, [v, i, phi, w], [k]; name = name, systems = [p, n, flange, support])
273+
flange.tau ~ -k * i
274+
end
253275
end

0 commit comments

Comments
 (0)