ElectroMagneticConverter

model ElectroMagneticConverter "Electromagnetic energy conversion"
    constant Complex j = Complex(0, 1);
    FluxTubes.Interfaces.PositiveMagneticPort port_p "Positive magnetic port"
        annotation (Placement(
            transformation(extent = {
                {90, 90}, 
                {110, 110}}),
            iconTransformation(extent = {
                {90, 90}, 
                {110, 110}})));
    FluxTubes.Interfaces.NegativeMagneticPort port_n "Negative magnetic port"
        annotation (Placement(
            transformation(extent = {
                {110, -110}, 
                {90, -90}}),
            iconTransformation(extent = {
                {110, -110}, 
                {90, -90}})));
    Modelica.Electrical.QuasiStatic.SinglePhase.Interfaces.PositivePin pin_p "Positive electric pin"
        annotation (Placement(
            transformation(extent = {
                {-90, 90}, 
                {-110, 110}}),
            iconTransformation(extent = {
                {-90, 90}, 
                {-110, 110}})));
    Modelica.Electrical.QuasiStatic.SinglePhase.Interfaces.NegativePin pin_n "Negative electric pin"
        annotation (Placement(
            transformation(extent = {
                {-110, -110}, 
                {-90, -90}}),
            iconTransformation(extent = {
                {-110, -110}, 
                {-90, -90}})));
    SI.ComplexVoltage v "Voltage";
    SI.ComplexCurrent i(re(start = 0, stateSelect = StateSelect.prefer), im(start = 0, stateSelect = StateSelect.prefer)) "Current";
    SI.ComplexMagneticPotentialDifference V_m "Magnetic potential difference";
    SI.ComplexMagneticFlux Phi "Magnetic flux coupled into magnetic circuit";
    SI.AngularVelocity omega;
    parameter Real N = 1 "Number of turns";
    SI.ComplexMagneticFlux Psi "Flux linkage for information only";
    SI.Inductance L_stat "Static inductance abs(Psi/i) for information only";
protected
    Real eps = 100 * Modelica.Constants.eps;
equation
    Connections.branch(pin_p.reference, pin_n.reference);
    Connections.branch(port_p.reference, pin_p.reference);
    Connections.branch(port_p.reference, port_n.reference);
    i = pin_p.i;
    v = -j * omega * N * Phi;
    v = pin_p.v - pin_n.v;
    L_stat = noEvent(if eps < Modelica.ComplexMath.abs(i) then Modelica.ComplexMath.abs(Psi / i) else Modelica.ComplexMath.abs(Psi / eps));
    Phi = port_p.Phi;
    Psi = N * Phi;
    V_m = N * i;
    V_m = port_p.V_m - port_n.V_m;
    omega = der(port_p.reference.gamma);
    pin_p.reference.gamma = pin_n.reference.gamma;
    port_p.reference.gamma = pin_p.reference.gamma;
    port_p.reference.gamma = port_n.reference.gamma;
    Complex(0) = pin_p.i + pin_n.i;
    Complex(0) = port_p.Phi + port_n.Phi;

    annotation (
        defaultComponentName = "converter",
        Icon(
            coordinateSystem(
                preserveAspectRatio = false,
                extent = {
                    {-100, -100}, 
                    {100, 100}}),
            graphics = {
                Line(
                    points = {
                        {-30, 100}, 
                        {-30, 60}},
                    color = {85, 170, 255}), 
                Line(
                    points = {
                        {-30, -60}, 
                        {-30, -100}},
                    color = {85, 170, 255}), 
                Line(
                    points = {
                        {-30, 100}, 
                        {-90, 100}},
                    color = {85, 170, 255}), 
                Line(
                    points = {
                        {-30, -100}, 
                        {-88, -100}},
                    color = {85, 170, 255}), 
                Ellipse(
                    extent = {
                        {-4, -34}, 
                        {64, 34}},
                    lineColor = {255, 170, 85}), 
                Line(
                    points = {
                        {30, -100}, 
                        {30, 0}},
                    color = {255, 170, 85}), 
                Line(
                    points = {
                        {30, 0}, 
                        {30, 100}},
                    color = {255, 170, 85}), 
                Line(
                    points = {
                        {30, 100}, 
                        {90, 100}},
                    color = {255, 170, 85}), 
                Line(
                    points = {
                        {30, -100}, 
                        {90, -100}},
                    color = {255, 170, 85}), 
                Text(
                    extent = {
                        {-150, 150}, 
                        {150, 110}},
                    textString = "%name",
                    pattern = LinePattern.None,
                    textColor = {0, 0, 255}), 
                Line(
                    points = {
                        {-15, -7}, 
                        {-14, -1}, 
                        {-7, 7}, 
                        {7, 7}, 
                        {14, -1}, 
                        {15, -7}},
                    color = {85, 170, 255},
                    smooth = Smooth.Bezier,
                    origin = {-23, 45},
                    rotation = 270), 
                Line(
                    points = {
                        {-15, -7}, 
                        {-14, -1}, 
                        {-7, 7}, 
                        {7, 7}, 
                        {14, -1}, 
                        {15, -7}},
                    color = {85, 170, 255},
                    smooth = Smooth.Bezier,
                    origin = {-23, 15},
                    rotation = 270), 
                Line(
                    points = {
                        {-15, -7}, 
                        {-14, -1}, 
                        {-7, 7}, 
                        {7, 7}, 
                        {14, -1}, 
                        {15, -7}},
                    color = {85, 170, 255},
                    smooth = Smooth.Bezier,
                    origin = {-23, -15},
                    rotation = 270), 
                Line(
                    points = {
                        {-15, -7}, 
                        {-14, -1}, 
                        {-7, 7}, 
                        {7, 7}, 
                        {14, -1}, 
                        {15, -7}},
                    color = {85, 170, 255},
                    smooth = Smooth.Bezier,
                    origin = {-23, -45},
                    rotation = 270)}),
        Documentation(info = "<html>\n<p>\nThe electromagnetic energy conversion is given by <em>Ampere</em>'s law and <em>Faraday</em>'s law respectively:\n</p>\n\n<pre>\n    V<sub>m</sub> = N * i\n    N * d&Phi;/dt = -v\n</pre>\n\n<p>\n<img src=\"modelica://Modelica/Resources/Images/Magnetic/FluxTubes/Basic/converter_signs.png\" alt=\"converter signs\">\n</p>\n\n<p>\nV<sub>m</sub> is the magnetic potential difference applied to the magnetic circuit due to the current i through the coil (Ampere's law).\nThere exists a left-hand assignment between the current i (fingers) and the magnetic potential difference V<sub>m</sub> (thumb).<br>\n<strong>Note:</strong> There exists a right-hand assignment between the current through the coil i (fingers) and the magnetomotive force mmf.\nThe mmf has the opposite direction compared with V<sub>m</sub>. It is not used in Modelica.\n</p>\n\n<p>\nFor the complete magnetic circuit the sum of all magnetic potential differences counted with the correct sign in a reference direction is equal to zero: sum(V<sub>m</sub>) = 0.<br>\nThe magnetic flux &Phi; in each passive component is related to the magnetic potential difference V<sub>m</sub> by the equivalent of Ohms' law: V<sub>m</sub> = R<sub>m</sub> * &Phi;<br>\n<strong>Note:</strong> The magnetic resistance R<sub>m</sub> depends on geometry and material properties. For ferromagnetic materials R<sub>m</sub> is not constant due to saturation.\n</p>\n\n<p>\nTherefore the sign (actual direction) of &Phi; (magnetic flux through the converter) depends on the associated branch of the magnetic circuit.<br>\nv is the induced voltage in the coil due to the derivative of magnetic flux &Phi; (Faraday's law).<br>\n<strong>Note:</strong> The negative sign of the induced voltage v is due to <em>Lenz</em>'s law.\n</p>\n\n<p>\n<strong>Note:</strong> The image shows a right-handed coil.\nIf a left-handed coil has to be modeled instead of a right-handed coil, the parameter N (Number of turns) can be set to a negative value.\n</p>\n\n<p>\nThe flux linkage &Psi; and the static inductance L_stat = |&Psi;/i| are calculated for information only. Note that L_stat is set to |&Psi;/eps| if |i| &lt; eps\n(= 100*Modelica.Constants.eps).\n</p>\n</html>"));
end ElectroMagneticConverter;