Skip to content

Commit 5a700a2

Browse files
committed
refactor: Magnetic/FluxTubes
1 parent 7a5c20a commit 5a700a2

File tree

3 files changed

+106
-99
lines changed

3 files changed

+106
-99
lines changed

src/Magnetic/FluxTubes/basic.jl

Lines changed: 81 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,37 @@
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,16 +43,17 @@ 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
"""
@@ -65,14 +65,14 @@ Constant permeance.
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
72+
end
73+
@equations begin
74+
Phi ~ G_m * V_m
75+
end
7676
end
7777

7878
"""
@@ -84,18 +84,18 @@ Constant reluctance.
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
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 = 0.0)
9999
100100
Ideal electromagnetic energy conversion.
101101
@@ -108,28 +108,38 @@ N * dΦ/dt = -v
108108
- `N`: Number of turns
109109
- `Phi_start`: [Wb] Initial magnetic flux flowing into the port_p
110110
"""
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
111+
@mtkmodel ElectroMagneticConverter begin
112+
@components begin
113+
port_p = PositiveMagneticPort()
114+
port_n = NegativeMagneticPort()
115+
p = Pin()
116+
n = Pin()
117+
end
118+
119+
@variables begin
120+
v(t)
121+
i(t)
122+
V_m(t)
123+
Phi(t) = 0.0
124+
end
125+
@parameters begin
126+
N
127+
end
128+
@equations begin
129+
v ~ p.v - n.v
120130
0 ~ p.i + n.i
121131
i ~ p.i
122132
V_m ~ port_p.V_m - port_n.V_m
123133
0 ~ port_p.Phi + port_n.Phi
124134
Phi ~ port_p.Phi
125135
#converter equations:
126136
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)
137+
D(Phi) ~ -v / N
138+
end
129139
end
130140

131141
"""
132-
EddyCurrent(;name, rho=0.098e-6, l=1, A=1, Phi_start=0.0)
142+
EddyCurrent(;name, rho = 0.098e-6, l = 1, A = 1, Phi = 0.0)
133143
134144
For modelling of eddy current in a conductive magnetic flux tube.
135145
@@ -138,14 +148,18 @@ For modelling of eddy current in a conductive magnetic flux tube.
138148
- `rho`: [ohm * m] Resistivity of flux tube material (default: Iron at 20degC)
139149
- `l`: [m] Average length of eddy current path
140150
- `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)
151+
- `Phi`: [Wb] Initial magnetic flux flowing into the port_p
152+
"""
153+
@mtkmodel EddyCurrent begin
154+
@extend (V_m, Phi) = two_port = TwoPort(; Phi = 0.0)
155+
@parameters begin
156+
rho = 0.098e-6
157+
l = 1
158+
A = 1
159+
R = rho * l / A # Electrical resistance of eddy current path
160+
end
161+
@equations begin
162+
D(Phi) ~ V_m * R
163+
end
151164
end
165+
EddyCurrent.f(; Phi, name, kwargs...) = EddyCurrent.f(; two_port__Phi = Phi, name, kwargs...)

src/Magnetic/FluxTubes/sources.jl

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,10 @@ Parameters:
55
66
- `V_m`: [A] Magnetic potential difference
77
"""
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
14-
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)
8+
@mtkmodel ConstantMagneticPotentialDifference begin
9+
@extend Phi, V_m = twoport = TwoPort(; V_m = 0.0)
1710
end
11+
ConstantMagneticPotentialDifference.f(; V_m, name) = ConstantMagneticPotentialDifference.f(; twoport__V_m = V_m, name)
1812

1913
"""
2014
Source of constant magnetic flux.
@@ -23,13 +17,7 @@ Parameters:
2317
2418
- `Phi`: [Wb] Magnetic flux
2519
"""
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
32-
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)
20+
@mtkmodel ConstantMagneticFlux begin
21+
@extend Phi, V_m = twoport = TwoPort(; Phi = 1.0)
3522
end
23+
ConstantMagneticFlux.f(; Phi, name) = ConstantMagneticFlux.f(; twoport__Phi = Phi, name)

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)
34+
Phi(t)
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

0 commit comments

Comments
 (0)