Skip to content

Commit 41de51e

Browse files
authored
refactor: refactor Magnetic/FluxTubes components with @mtkmodel (#193)
1 parent 79e32b0 commit 41de51e

File tree

4 files changed

+140
-106
lines changed

4 files changed

+140
-106
lines changed

src/Magnetic/FluxTubes/basic.jl

Lines changed: 86 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,39 @@
11
"""
2-
Ground(;name)
2+
Ground(; name)
33
44
Zero magnetic potential.
55
"""
6-
@component function Ground(; name)
7-
@named port = PositiveMagneticPort()
8-
eqs = [port.V_m ~ 0]
9-
ODESystem(eqs, t, [], [], systems = [port], name = name)
6+
@mtkmodel Ground begin
7+
@components begin
8+
port = PositiveMagneticPort()
9+
end
10+
@equations begin
11+
port.V_m ~ 0
12+
end
1013
end
1114

1215
"""
1316
Idle(;name)
1417
1518
Idle running branch.
1619
"""
17-
@component function Idle(; name)
18-
@named two_port = TwoPort()
19-
@unpack Phi = two_port
20-
eqs = [
21-
Phi ~ 0,
22-
]
23-
extend(ODESystem(eqs, t, [], [], systems = [], name = name), two_port)
20+
@mtkmodel Idle begin
21+
@extend (Phi,) = two_port = TwoPort()
22+
@equations begin
23+
Phi ~ 0
24+
end
2425
end
2526

2627
"""
2728
Short(;name)
2829
2930
Short cut branch.
3031
"""
31-
@component function Short(; name)
32-
@named two_port = TwoPort()
33-
@unpack V_m = two_port
34-
eqs = [
35-
V_m ~ 0,
36-
]
37-
extend(ODESystem(eqs, t, [], [], systems = [], name = name), two_port)
32+
@mtkmodel Short begin
33+
@extend (V_m,) = two_port = TwoPort()
34+
@equations begin
35+
V_m ~ 0
36+
end
3837
end
3938

4039
"""
@@ -44,108 +43,120 @@ Crossing of two branches.
4443
4544
This is a simple crossing of two branches. The ports port_p1 and port_p2 are connected, as well as port_n1 and port_n2.
4645
"""
47-
@component function Crossing(; name)
48-
@named port_p1 = PositiveMagneticPort()
49-
@named port_p2 = PositiveMagneticPort()
50-
@named port_n1 = NegativeMagneticPort()
51-
@named port_n2 = NegativeMagneticPort()
52-
eqs = [
53-
connect(port_p1, port_p2),
54-
connect(port_n1, port_n2),
55-
]
56-
ODESystem(eqs, t, [], [], systems = [port_p1, port_p2, port_n1, port_n2], name = name)
46+
@mtkmodel Crossing begin
47+
@components begin
48+
port_p1 = PositiveMagneticPort()
49+
port_p2 = PositiveMagneticPort()
50+
port_n1 = NegativeMagneticPort()
51+
port_n2 = NegativeMagneticPort()
52+
end
53+
@equations begin
54+
connect(port_p1, port_p2)
55+
connect(port_n1, port_n2)
56+
end
5757
end
5858

5959
"""
60-
ConstantPermeance(;name, G_m=1.0)
60+
ConstantPermeance(; name, G_m = 1.0)
6161
6262
Constant permeance.
6363
6464
# Parameters:
6565
6666
- `G_m`: [H] Magnetic permeance
6767
"""
68-
@component function ConstantPermeance(; name, G_m = 1.0)
69-
@named two_port = TwoPort()
70-
@unpack V_m, Phi = two_port
71-
@parameters G_m = G_m
72-
eqs = [
73-
Phi ~ G_m * V_m,
74-
]
75-
extend(ODESystem(eqs, t, [], [G_m], name = name), two_port)
68+
@mtkmodel ConstantPermeance begin
69+
@extend V_m, Phi = two_port = TwoPort()
70+
@parameters begin
71+
G_m = 1.0, [description = "Magnetic permeance"]
72+
end
73+
@equations begin
74+
Phi ~ G_m * V_m
75+
end
7676
end
7777

7878
"""
79-
ConstantReluctance(;name, R_m=1.0)
79+
ConstantReluctance(; name, R_m = 1.0)
8080
8181
Constant reluctance.
8282
8383
# Parameters:
8484
8585
- `R_m`: [H^-1] Magnetic reluctance
8686
"""
87-
@component function ConstantReluctance(; name, R_m = 1.0)
88-
@named two_port = TwoPort()
89-
@unpack V_m, Phi = two_port
90-
@parameters R_m = R_m
91-
eqs = [
92-
V_m ~ Phi * R_m,
93-
]
94-
extend(ODESystem(eqs, t, [], [R_m], name = name), two_port)
87+
@mtkmodel ConstantReluctance begin
88+
@extend V_m, Phi = two_port = TwoPort(; Phi = 0.0)
89+
@parameters begin
90+
R_m = 1.0, [description = "Magnetic reluctance"]
91+
end
92+
@equations begin
93+
V_m ~ Phi * R_m
94+
end
9595
end
9696

9797
"""
98-
ElectroMagneticConverter(;name, N, Phi_start=0.0)
98+
ElectroMagneticConverter(; name, N, Phi)
9999
100100
Ideal electromagnetic energy conversion.
101101
102102
The electromagnetic energy conversion is given by Ampere's law and Faraday's law respectively
103103
V_m = N * i
104104
N * dΦ/dt = -v
105105
106+
Initial magnetic flux flowing into the port_p can be set with `Phi` ([Wb])
107+
106108
# Parameters:
107109
108110
- `N`: Number of turns
109-
- `Phi_start`: [Wb] Initial magnetic flux flowing into the port_p
110111
"""
111-
@component function ElectroMagneticConverter(; name, N, Phi_start = 0.0)
112-
@named port_p = PositiveMagneticPort()
113-
@named port_n = NegativeMagneticPort()
114-
@named p = Pin()
115-
@named n = Pin()
116-
117-
sts = @variables v(t) i(t) V_m(t) Phi(t)=Phi_start
118-
pars = @parameters N = N
119-
eqs = [v ~ p.v - n.v
112+
@mtkmodel ElectroMagneticConverter begin
113+
@parameters begin
114+
N, [description = "Number of turns"]
115+
end
116+
@variables begin
117+
v(t)
118+
i(t)
119+
Phi
120+
end
121+
@extend V_m, Phi = two_port = TwoPort(; Phi = Phi)
122+
@components begin
123+
p = Pin()
124+
n = Pin()
125+
end
126+
@equations begin
127+
v ~ p.v - n.v
120128
0 ~ p.i + n.i
121129
i ~ p.i
122-
V_m ~ port_p.V_m - port_n.V_m
123-
0 ~ port_p.Phi + port_n.Phi
124-
Phi ~ port_p.Phi
125-
#converter equations:
130+
#converter equations:
126131
V_m ~ i * N # Ampere's law
127-
D(Phi) ~ -v / N]
128-
ODESystem(eqs, t, sts, pars, systems = [port_p, port_n, p, n], name = name)
132+
D(Phi) ~ -v / N
133+
end
129134
end
130135

131136
"""
132-
EddyCurrent(;name, rho=0.098e-6, l=1, A=1, Phi_start=0.0)
137+
EddyCurrent(;name, Phi, rho = 0.098e-6, l = 1, A = 1)
133138
134139
For modelling of eddy current in a conductive magnetic flux tube.
140+
Initial magnetic flux flowing into the port_p can be set with `Phi` ([`Wb`])
135141
136142
# Parameters:
137143
138144
- `rho`: [ohm * m] Resistivity of flux tube material (default: Iron at 20degC)
139145
- `l`: [m] Average length of eddy current path
140146
- `A`: [m^2] Cross sectional area of eddy current path
141-
- `Phi_start`: [Wb] Initial magnetic flux flowing into the port_p
142-
"""
143-
@component function EddyCurrent(; name, rho = 0.098e-6, l = 1, A = 1, Phi_start = 0.0)
144-
@named two_port = TwoPort(Phi_start = Phi_start)
145-
@unpack V_m, Phi = two_port
146-
@parameters R = rho * l / A # Electrical resistance of eddy current path
147-
eqs = [
148-
D(Phi) ~ V_m * R,
149-
]
150-
extend(ODESystem(eqs, t, [], [R], name = name), two_port)
147+
"""
148+
@mtkmodel EddyCurrent begin
149+
@variables begin
150+
Phi
151+
end
152+
@parameters begin
153+
rho = 0.098e-6, [description = "Resistivity of flux tube material"]
154+
l = 1, [description = "Average length of eddy current path"]
155+
A = 1, [description = "Cross sectional area of eddy current path"]
156+
R = rho * l / A # Electrical resistance of eddy current path
157+
end
158+
@extend (V_m, Phi) = two_port = TwoPort(; Phi = Phi)
159+
@equations begin
160+
D(Phi) ~ V_m * R
161+
end
151162
end

src/Magnetic/FluxTubes/sources.jl

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,53 @@
11
"""
2+
ConstantMagneticPotentialDifference(; name, V_m = 0.0)
3+
24
Constant magnetomotive force.
35
46
Parameters:
57
68
- `V_m`: [A] Magnetic potential difference
79
"""
8-
@component function ConstantMagneticPotentialDifference(; name, V_m = 1.0)
9-
port_p = PositiveMagneticPort()
10-
port_n = NegativeMagneticPort()
11-
@parameters V_m = V_m
12-
@variables Phi(t)
13-
eqs = [V_m ~ port_p.V_m - port_n.V_m
10+
@mtkmodel ConstantMagneticPotentialDifference begin
11+
@components begin
12+
port_p = PositiveMagneticPort()
13+
port_n = NegativeMagneticPort()
14+
end
15+
@parameters begin
16+
V_m = 0.0, [description = "Magnetic potential difference"]
17+
end
18+
@variables begin
19+
Phi(t)
20+
end
21+
@equations begin
22+
V_m ~ port_p.V_m - port_n.V_m
1423
Phi ~ port_p.Phi
15-
0 ~ port_p.Phi + port_n.Phi]
16-
ODESystem(eqs, t, [Phi], [V_m], systems = [port_p, port_n], name = name)
24+
0 ~ port_p.Phi + port_n.Phi
25+
end
1726
end
1827

1928
"""
29+
ConstantMagneticFlux(; name, Phi = 0.0)
30+
2031
Source of constant magnetic flux.
2132
2233
Parameters:
2334
2435
- `Phi`: [Wb] Magnetic flux
2536
"""
26-
@component function ConstantMagneticFlux(; name, Phi = 1.0)
27-
port_p = PositiveMagneticPort()
28-
port_n = NegativeMagneticPort()
29-
@parameters Phi = Phi
30-
@variables V_m(t)
31-
eqs = [V_m ~ port_p.V_m - port_n.V_m
37+
@mtkmodel ConstantMagneticFlux begin
38+
@components begin
39+
port_p = PositiveMagneticPort()
40+
port_n = NegativeMagneticPort()
41+
end
42+
@parameters begin
43+
Phi = 0.0, [description = "Magnetic flux"]
44+
end
45+
@variables begin
46+
V_m(t)
47+
end
48+
@equations begin
49+
V_m ~ port_p.V_m - port_n.V_m
3250
Phi ~ port_p.Phi
33-
0 ~ port_p.Phi + port_n.Phi]
34-
ODESystem(eqs, t, [Phi], [V_m], systems = [port_p, port_n], name = name)
51+
0 ~ port_p.Phi + port_n.Phi
52+
end
3553
end

src/Magnetic/FluxTubes/utils.jl

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
@connector function MagneticPort(; name, V_m_start = 0.0, Phi_start = 0.0)
2-
@variables V_m(t) = V_m_start # [Wb] Magnetic potential at the port
3-
@variables Phi(t)=Phi_start [connect = Flow] # [A] Magnetic flux flowing into the port"
4-
ODESystem(Equation[], t, [V_m, Phi], []; name = name)
1+
@connector MagneticPort begin
2+
V_m(t), [description = "Magnetic potential at the port"]
3+
Phi(t), [connect = Flow, description = "Magnetic flux flowing into the port"]
54
end
65
Base.@doc "Port for a Magnetic system." MagneticPort
76

@@ -16,21 +15,27 @@ Negative magnetic port
1615
const NegativeMagneticPort = MagneticPort
1716

1817
"""
19-
TwoPort(;name, V_m_start=0.0, Phi_start=0.0)
18+
TwoPort(; name, V_m = 0.0, Phi = 0.0)
2019
2120
Partial component with magnetic potential difference between two magnetic ports p and n and magnetic flux Phi from p to n.
2221
2322
# Parameters:
2423
25-
- `V_m_start`: Initial magnetic potential difference between both ports
26-
- `Phi_start`: Initial magnetic flux from port_p to port_n
24+
- `V_m`: Initial magnetic potential difference between both ports
25+
- `Phi`: Initial magnetic flux from port_p to port_n
2726
"""
28-
@component function TwoPort(; name, V_m_start = 0.0, Phi_start = 0.0)
29-
@named port_p = PositiveMagneticPort()
30-
@named port_n = NegativeMagneticPort()
31-
@variables V_m(t)=V_m_start Phi(t)=Phi_start
32-
eqs = [V_m ~ port_p.V_m - port_n.V_m
27+
@mtkmodel TwoPort begin
28+
@components begin
29+
port_p = PositiveMagneticPort()
30+
port_n = NegativeMagneticPort()
31+
end
32+
@variables begin
33+
V_m(t) = 0.0
34+
Phi(t) = 0.0
35+
end
36+
@equations begin
37+
V_m ~ port_p.V_m - port_n.V_m
3338
Phi ~ port_p.Phi
34-
0 ~ port_p.Phi + port_n.Phi]
35-
ODESystem(eqs, t, [V_m, Phi], [], systems = [port_p, port_n], name = name)
39+
0 ~ port_p.Phi + port_n.Phi
40+
end
3641
end

test/Magnetic/magnetic.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ using OrdinaryDiffEq: ReturnCode.Success
2020
@named voltage = Electrical.Voltage()
2121
@named r = Electrical.Resistor(R = 7.5)
2222
@named ground = Electrical.Ground()
23-
@named coil = Magnetic.FluxTubes.ElectroMagneticConverter(N = 600)
23+
@named coil = Magnetic.FluxTubes.ElectroMagneticConverter(N = 600, Phi = 0.0)
2424
@named ground_m = Magnetic.FluxTubes.Ground()
2525
@named r_mAirPar = Magnetic.FluxTubes.ConstantReluctance(R_m = a * b * l_air * mu_air)
2626
@named r_mFe = Magnetic.FluxTubes.ConstantReluctance(R_m = a * b * l_Fe * mu_Fe)

0 commit comments

Comments
 (0)