From b57b81b81e001a091711e70d56c38e8af51a015e Mon Sep 17 00:00:00 2001 From: Venkateshprasad <32921645+ven-k@users.noreply.github.com> Date: Mon, 3 Jul 2023 17:08:10 +0530 Subject: [PATCH] refactor: refactor Magnetic/FluxTubes components with `@mtkmodel` --- src/Magnetic/FluxTubes/basic.jl | 161 ++++++++++++++++-------------- src/Magnetic/FluxTubes/sources.jl | 50 +++++++--- src/Magnetic/FluxTubes/utils.jl | 33 +++--- test/Magnetic/magnetic.jl | 2 +- 4 files changed, 140 insertions(+), 106 deletions(-) diff --git a/src/Magnetic/FluxTubes/basic.jl b/src/Magnetic/FluxTubes/basic.jl index 37545d286..e19ab198c 100644 --- a/src/Magnetic/FluxTubes/basic.jl +++ b/src/Magnetic/FluxTubes/basic.jl @@ -1,12 +1,15 @@ """ - Ground(;name) + Ground(; name) Zero magnetic potential. """ -@component function Ground(; name) - @named port = PositiveMagneticPort() - eqs = [port.V_m ~ 0] - ODESystem(eqs, t, [], [], systems = [port], name = name) +@mtkmodel Ground begin + @components begin + port = PositiveMagneticPort() + end + @equations begin + port.V_m ~ 0 + end end """ @@ -14,13 +17,11 @@ end Idle running branch. """ -@component function Idle(; name) - @named two_port = TwoPort() - @unpack Phi = two_port - eqs = [ - Phi ~ 0, - ] - extend(ODESystem(eqs, t, [], [], systems = [], name = name), two_port) +@mtkmodel Idle begin + @extend (Phi,) = two_port = TwoPort() + @equations begin + Phi ~ 0 + end end """ @@ -28,13 +29,11 @@ end Short cut branch. """ -@component function Short(; name) - @named two_port = TwoPort() - @unpack V_m = two_port - eqs = [ - V_m ~ 0, - ] - extend(ODESystem(eqs, t, [], [], systems = [], name = name), two_port) +@mtkmodel Short begin + @extend (V_m,) = two_port = TwoPort() + @equations begin + V_m ~ 0 + end end """ @@ -44,20 +43,21 @@ Crossing of two branches. 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. """ -@component function Crossing(; name) - @named port_p1 = PositiveMagneticPort() - @named port_p2 = PositiveMagneticPort() - @named port_n1 = NegativeMagneticPort() - @named port_n2 = NegativeMagneticPort() - eqs = [ - connect(port_p1, port_p2), - connect(port_n1, port_n2), - ] - ODESystem(eqs, t, [], [], systems = [port_p1, port_p2, port_n1, port_n2], name = name) +@mtkmodel Crossing begin + @components begin + port_p1 = PositiveMagneticPort() + port_p2 = PositiveMagneticPort() + port_n1 = NegativeMagneticPort() + port_n2 = NegativeMagneticPort() + end + @equations begin + connect(port_p1, port_p2) + connect(port_n1, port_n2) + end end """ - ConstantPermeance(;name, G_m=1.0) + ConstantPermeance(; name, G_m = 1.0) Constant permeance. @@ -65,18 +65,18 @@ Constant permeance. - `G_m`: [H] Magnetic permeance """ -@component function ConstantPermeance(; name, G_m = 1.0) - @named two_port = TwoPort() - @unpack V_m, Phi = two_port - @parameters G_m = G_m - eqs = [ - Phi ~ G_m * V_m, - ] - extend(ODESystem(eqs, t, [], [G_m], name = name), two_port) +@mtkmodel ConstantPermeance begin + @extend V_m, Phi = two_port = TwoPort() + @parameters begin + G_m = 1.0, [description = "Magnetic permeance"] + end + @equations begin + Phi ~ G_m * V_m + end end """ - ConstantReluctance(;name, R_m=1.0) + ConstantReluctance(; name, R_m = 1.0) Constant reluctance. @@ -84,18 +84,18 @@ Constant reluctance. - `R_m`: [H^-1] Magnetic reluctance """ -@component function ConstantReluctance(; name, R_m = 1.0) - @named two_port = TwoPort() - @unpack V_m, Phi = two_port - @parameters R_m = R_m - eqs = [ - V_m ~ Phi * R_m, - ] - extend(ODESystem(eqs, t, [], [R_m], name = name), two_port) +@mtkmodel ConstantReluctance begin + @extend V_m, Phi = two_port = TwoPort(; Phi = 0.0) + @parameters begin + R_m = 1.0, [description = "Magnetic reluctance"] + end + @equations begin + V_m ~ Phi * R_m + end end """ - ElectroMagneticConverter(;name, N, Phi_start=0.0) + ElectroMagneticConverter(; name, N, Phi) Ideal electromagnetic energy conversion. @@ -103,49 +103,60 @@ The electromagnetic energy conversion is given by Ampere's law and Faraday's law V_m = N * i N * dΦ/dt = -v +Initial magnetic flux flowing into the port_p can be set with `Phi` ([Wb]) + # Parameters: - `N`: Number of turns - - `Phi_start`: [Wb] Initial magnetic flux flowing into the port_p """ -@component function ElectroMagneticConverter(; name, N, Phi_start = 0.0) - @named port_p = PositiveMagneticPort() - @named port_n = NegativeMagneticPort() - @named p = Pin() - @named n = Pin() - - sts = @variables v(t) i(t) V_m(t) Phi(t)=Phi_start - pars = @parameters N = N - eqs = [v ~ p.v - n.v +@mtkmodel ElectroMagneticConverter begin + @parameters begin + N, [description = "Number of turns"] + end + @variables begin + v(t) + i(t) + Phi + end + @extend V_m, Phi = two_port = TwoPort(; Phi = Phi) + @components begin + p = Pin() + n = Pin() + end + @equations begin + v ~ p.v - n.v 0 ~ p.i + n.i i ~ p.i - V_m ~ port_p.V_m - port_n.V_m - 0 ~ port_p.Phi + port_n.Phi - Phi ~ port_p.Phi - #converter equations: + #converter equations: V_m ~ i * N # Ampere's law - D(Phi) ~ -v / N] - ODESystem(eqs, t, sts, pars, systems = [port_p, port_n, p, n], name = name) + D(Phi) ~ -v / N + end end """ - EddyCurrent(;name, rho=0.098e-6, l=1, A=1, Phi_start=0.0) + EddyCurrent(;name, Phi, rho = 0.098e-6, l = 1, A = 1) For modelling of eddy current in a conductive magnetic flux tube. +Initial magnetic flux flowing into the port_p can be set with `Phi` ([`Wb`]) # Parameters: - `rho`: [ohm * m] Resistivity of flux tube material (default: Iron at 20degC) - `l`: [m] Average length of eddy current path - `A`: [m^2] Cross sectional area of eddy current path - - `Phi_start`: [Wb] Initial magnetic flux flowing into the port_p -""" -@component function EddyCurrent(; name, rho = 0.098e-6, l = 1, A = 1, Phi_start = 0.0) - @named two_port = TwoPort(Phi_start = Phi_start) - @unpack V_m, Phi = two_port - @parameters R = rho * l / A # Electrical resistance of eddy current path - eqs = [ - D(Phi) ~ V_m * R, - ] - extend(ODESystem(eqs, t, [], [R], name = name), two_port) +""" +@mtkmodel EddyCurrent begin + @variables begin + Phi + end + @parameters begin + rho = 0.098e-6, [description = "Resistivity of flux tube material"] + l = 1, [description = "Average length of eddy current path"] + A = 1, [description = "Cross sectional area of eddy current path"] + R = rho * l / A # Electrical resistance of eddy current path + end + @extend (V_m, Phi) = two_port = TwoPort(; Phi = Phi) + @equations begin + D(Phi) ~ V_m * R + end end diff --git a/src/Magnetic/FluxTubes/sources.jl b/src/Magnetic/FluxTubes/sources.jl index 5be663e2d..d2c5f8dfe 100644 --- a/src/Magnetic/FluxTubes/sources.jl +++ b/src/Magnetic/FluxTubes/sources.jl @@ -1,35 +1,53 @@ """ + ConstantMagneticPotentialDifference(; name, V_m = 0.0) + Constant magnetomotive force. Parameters: - `V_m`: [A] Magnetic potential difference """ -@component function ConstantMagneticPotentialDifference(; name, V_m = 1.0) - port_p = PositiveMagneticPort() - port_n = NegativeMagneticPort() - @parameters V_m = V_m - @variables Phi(t) - eqs = [V_m ~ port_p.V_m - port_n.V_m +@mtkmodel ConstantMagneticPotentialDifference begin + @components begin + port_p = PositiveMagneticPort() + port_n = NegativeMagneticPort() + end + @parameters begin + V_m = 0.0, [description = "Magnetic potential difference"] + end + @variables begin + Phi(t) + end + @equations begin + V_m ~ port_p.V_m - port_n.V_m Phi ~ port_p.Phi - 0 ~ port_p.Phi + port_n.Phi] - ODESystem(eqs, t, [Phi], [V_m], systems = [port_p, port_n], name = name) + 0 ~ port_p.Phi + port_n.Phi + end end """ + ConstantMagneticFlux(; name, Phi = 0.0) + Source of constant magnetic flux. Parameters: - `Phi`: [Wb] Magnetic flux """ -@component function ConstantMagneticFlux(; name, Phi = 1.0) - port_p = PositiveMagneticPort() - port_n = NegativeMagneticPort() - @parameters Phi = Phi - @variables V_m(t) - eqs = [V_m ~ port_p.V_m - port_n.V_m +@mtkmodel ConstantMagneticFlux begin + @components begin + port_p = PositiveMagneticPort() + port_n = NegativeMagneticPort() + end + @parameters begin + Phi = 0.0, [description = "Magnetic flux"] + end + @variables begin + V_m(t) + end + @equations begin + V_m ~ port_p.V_m - port_n.V_m Phi ~ port_p.Phi - 0 ~ port_p.Phi + port_n.Phi] - ODESystem(eqs, t, [Phi], [V_m], systems = [port_p, port_n], name = name) + 0 ~ port_p.Phi + port_n.Phi + end end diff --git a/src/Magnetic/FluxTubes/utils.jl b/src/Magnetic/FluxTubes/utils.jl index 441ac2f93..f6154d81f 100644 --- a/src/Magnetic/FluxTubes/utils.jl +++ b/src/Magnetic/FluxTubes/utils.jl @@ -1,7 +1,6 @@ -@connector function MagneticPort(; name, V_m_start = 0.0, Phi_start = 0.0) - @variables V_m(t) = V_m_start # [Wb] Magnetic potential at the port - @variables Phi(t)=Phi_start [connect = Flow] # [A] Magnetic flux flowing into the port" - ODESystem(Equation[], t, [V_m, Phi], []; name = name) +@connector MagneticPort begin + V_m(t), [description = "Magnetic potential at the port"] + Phi(t), [connect = Flow, description = "Magnetic flux flowing into the port"] end Base.@doc "Port for a Magnetic system." MagneticPort @@ -16,21 +15,27 @@ Negative magnetic port const NegativeMagneticPort = MagneticPort """ - TwoPort(;name, V_m_start=0.0, Phi_start=0.0) + TwoPort(; name, V_m = 0.0, Phi = 0.0) Partial component with magnetic potential difference between two magnetic ports p and n and magnetic flux Phi from p to n. # Parameters: - - `V_m_start`: Initial magnetic potential difference between both ports - - `Phi_start`: Initial magnetic flux from port_p to port_n + - `V_m`: Initial magnetic potential difference between both ports + - `Phi`: Initial magnetic flux from port_p to port_n """ -@component function TwoPort(; name, V_m_start = 0.0, Phi_start = 0.0) - @named port_p = PositiveMagneticPort() - @named port_n = NegativeMagneticPort() - @variables V_m(t)=V_m_start Phi(t)=Phi_start - eqs = [V_m ~ port_p.V_m - port_n.V_m +@mtkmodel TwoPort begin + @components begin + port_p = PositiveMagneticPort() + port_n = NegativeMagneticPort() + end + @variables begin + V_m(t) = 0.0 + Phi(t) = 0.0 + end + @equations begin + V_m ~ port_p.V_m - port_n.V_m Phi ~ port_p.Phi - 0 ~ port_p.Phi + port_n.Phi] - ODESystem(eqs, t, [V_m, Phi], [], systems = [port_p, port_n], name = name) + 0 ~ port_p.Phi + port_n.Phi + end end diff --git a/test/Magnetic/magnetic.jl b/test/Magnetic/magnetic.jl index c8e1af10e..e378c32f4 100644 --- a/test/Magnetic/magnetic.jl +++ b/test/Magnetic/magnetic.jl @@ -20,7 +20,7 @@ using OrdinaryDiffEq: ReturnCode.Success @named voltage = Electrical.Voltage() @named r = Electrical.Resistor(R = 7.5) @named ground = Electrical.Ground() - @named coil = Magnetic.FluxTubes.ElectroMagneticConverter(N = 600) + @named coil = Magnetic.FluxTubes.ElectroMagneticConverter(N = 600, Phi = 0.0) @named ground_m = Magnetic.FluxTubes.Ground() @named r_mAirPar = Magnetic.FluxTubes.ConstantReluctance(R_m = a * b * l_air * mu_air) @named r_mFe = Magnetic.FluxTubes.ConstantReluctance(R_m = a * b * l_Fe * mu_Fe)