IdealIntermediateSwitch

model IdealIntermediateSwitch "Ideal intermediate switch"
    import Modelica.ComplexMath.real;
    import Modelica.ComplexMath.conj;

    parameter Modelica.SIunits.Resistance Ron(final min = 0) = 1e-5 "Closed switch resistance";
    parameter Modelica.SIunits.Conductance Goff(final min = 0) = 1e-5 "Opened switch conductance";

    extends Modelica.Electrical.Analog.Interfaces.ConditionalHeatPort(final T = 293.15);

    Modelica.Electrical.QuasiStationary.SinglePhase.Interfaces.PositivePin p1 annotation (Placement(
        transformation(extent = {
            {-110, 30}, 
            {-90, 50}}),
        iconTransformation(extent = {
            {-110, 30}, 
            {-90, 50}})));
    Modelica.Electrical.QuasiStationary.SinglePhase.Interfaces.PositivePin p2 annotation (Placement(transformation(extent = {
        {-110, -10}, 
        {-90, 10}})));
    Modelica.Electrical.QuasiStationary.SinglePhase.Interfaces.NegativePin n1 annotation (Placement(
        transformation(extent = {
            {90, 30}, 
            {110, 50}}),
        iconTransformation(extent = {
            {90, 30}, 
            {110, 50}})));
    Modelica.Electrical.QuasiStationary.SinglePhase.Interfaces.NegativePin n2 annotation (Placement(transformation(extent = {
        {90, -10}, 
        {110, 10}})));
    Modelica.Blocks.Interfaces.BooleanInput control "true => p1--n2, p2--n1 connected, otherwise p1--n1, p2--n2  connected"
        annotation (Placement(transformation(
            origin = {0, 120},
            extent = {
                {-20, -20}, 
                {20, 20}},
            rotation = 270)));
protected
    Complex s1(re(final unit = "1"), im(final unit = "1"));
    Complex s2(re(final unit = "1"), im(final unit = "1"));
    Complex s3(re(final unit = "1"), im(final unit = "1"));
    Complex s4(re(final unit = "1"), im(final unit = "1")) "Auxiliary variables";
    constant Modelica.SIunits.ComplexVoltage unitVoltage = Complex(1, 0) annotation (HideResult = true);
    constant Modelica.SIunits.ComplexCurrent unitCurrent = Complex(1, 0) annotation (HideResult = true);
equation
    Connections.branch(n1.reference, n2.reference);
    Connections.branch(p1.reference, n1.reference);
    Connections.branch(p2.reference, n2.reference);
    LossPower = real(p1.v * conj(p1.i)) + real(p2.v * conj(p2.i)) + real(n1.v * conj(n1.i)) + real(n2.v * conj(n2.i));
    n1.i = if control then -s1 * unitVoltage * Goff - s4 * unitCurrent else -s1 * unitCurrent - s4 * unitVoltage * Goff;
    n2.i = if control then -s2 * unitVoltage * Goff - s3 * unitCurrent else -s2 * unitCurrent - s3 * unitVoltage * Goff;
    p1.i = if control then s1 * unitVoltage * Goff + s3 * unitCurrent else s1 * unitCurrent + s3 * unitVoltage * Goff;
    p2.i = if control then s2 * unitVoltage * Goff + s4 * unitCurrent else s2 * unitCurrent + s4 * unitVoltage * Goff;
    n1.reference.gamma = n2.reference.gamma;
    p1.reference.gamma = n1.reference.gamma;
    p2.reference.gamma = n2.reference.gamma;
    p1.v - n1.v = s1 * unitCurrent * (if control then 1 else Ron);
    p1.v - n2.v = s3 * unitCurrent * (if control then Ron else 1);
    p2.v - n1.v = s4 * unitCurrent * (if control then Ron else 1);
    p2.v - n2.v = s2 * unitCurrent * (if control then 1 else Ron);

    annotation (
        defaultComponentName = "switch",
        Documentation(info = "<html>\n<p>\nThe intermediate switch has four switching contact pins p1, p2, n1, and n2.\nThe switching behaviour is controlled by the input signal control. If control\nis true, the pin p1 is connected to pin n2, and the pin p2 is\nconnected to the pin n2. Otherwise, the pin p1 is connected to n1, and\np2 is connected to n2.\n</p>\n\n<p>\n<img src=\"modelica://Modelica/Resources/Images/Electrical/QuasiStationary/SinglePhase/Ideal/IdealIntermediateSwitch1.png\" alt=\"IdealIntermediateSwitch1\">\n</p>\n\n<p>\nIn order to prevent singularities during switching, the opened\nswitch has a (very low) conductance Goff\nand the closed switch has a (very low) resistance Ron.\n</p>\n\n<p>\n<img src=\"modelica://Modelica/Resources/Images/Electrical/QuasiStationary/SinglePhase/Ideal/IdealIntermediateSwitch2.png\" alt=\"IdealIntermediateSwitch2\">\n</p>\n\n<p>\nThe limiting case is also allowed, i.e., the resistance Ron of the\nclosed switch could be exactly zero and the conductance Goff of the\nopen switch could be also exactly zero. Note, there are circuits,\nwhere a description with zero Ron or zero Goff is not possible.\n<br><br>\n<strong>Please note:</strong>\nIn case of useHeatPort=true the temperature dependence of the electrical\nbehavior is <strong>not</strong> modelled. The parameters are not temperature dependent.\n</p>\n<p>\n<strong>Use with care:</strong>\nThis switch is only intended to be used for structural changes, not for fast switching sequences, due to the quasistationary formulation.\n</p>\n</html>"),
        Icon(
            coordinateSystem(
                preserveAspectRatio = true,
                extent = {
                    {-100, -100}, 
                    {100, 100}}),
            graphics = {
                Ellipse(
                    extent = {
                        {-4, 24}, 
                        {4, 16}},
                    lineColor = {85, 170, 255}), 
                Line(
                    points = {
                        {-90, 0}, 
                        {-40, 0}},
                    color = {85, 170, 255}), 
                Line(
                    points = {
                        {-90, 40}, 
                        {-40, 40}},
                    color = {85, 170, 255}), 
                Line(
                    points = {
                        {-40, 0}, 
                        {40, 40}},
                    color = {85, 170, 255}), 
                Line(
                    points = {
                        {-40, 40}, 
                        {40, 2}},
                    color = {85, 170, 255}), 
                Line(
                    points = {
                        {40, 40}, 
                        {90, 40}},
                    color = {85, 170, 255}), 
                Line(
                    points = {
                        {40, 0}, 
                        {90, 0}},
                    color = {85, 170, 255}), 
                Text(
                    extent = {
                        {-150, 90}, 
                        {150, 50}},
                    textString = "%name",
                    lineColor = {0, 0, 255})}));
end IdealIntermediateSwitch;