IdealCommutingSwitch

model IdealCommutingSwitch "Ideal commuting 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 p annotation (Placement(transformation(extent = {
        {-110, -10}, 
        {-90, 10}})));
    Modelica.Electrical.QuasiStationary.SinglePhase.Interfaces.NegativePin n2 annotation (Placement(transformation(extent = {
        {90, -10}, 
        {110, 10}})));
    Modelica.Electrical.QuasiStationary.SinglePhase.Interfaces.NegativePin n1 annotation (Placement(
        transformation(extent = {
            {90, 30}, 
            {110, 50}}),
        iconTransformation(extent = {
            {90, 30}, 
            {110, 50}})));
    Modelica.Blocks.Interfaces.BooleanInput control "true => p--n2 connected, false => p--n1 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")) "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(p.reference, n1.reference);
    Connections.branch(p.reference, n2.reference);
    p.i + n2.i + n1.i = Complex(0, 0);
    LossPower = real(p.v * conj(p.i)) + real(n1.v * conj(n1.i)) + real(n2.v * conj(n2.i));
    n1.i = -s1 * unitVoltage * (if control then Goff else 1);
    n2.i = -s2 * unitVoltage * (if control then 1 else Goff);
    p.reference.gamma = n1.reference.gamma;
    p.reference.gamma = n2.reference.gamma;
    p.v - n1.v = s1 * unitCurrent * (if control then 1 else Ron);
    p.v - n2.v = s2 * unitCurrent * (if control then Ron else 1);

    annotation (
        defaultComponentName = "switch",
        Documentation(info = "<html>\n<p>\nThe commuting switch has a positive pin p and two negative pins n1 and n2.\nThe switching behaviour is controlled\nby the input signal control. If control is true, the pin p is connected\nwith the negative pin n2. Otherwise, the pin p is connected to the negative pin n1.\n</p>\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.\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 = {
                        {-44, 4}, 
                        {-36, -4}},
                    lineColor = {85, 170, 255}), 
                Line(
                    points = {
                        {-100, 0}, 
                        {-44, 0}},
                    color = {85, 170, 255}), 
                Line(
                    points = {
                        {-37, 2}, 
                        {40, 40}},
                    color = {85, 170, 255}), 
                Line(
                    points = {
                        {40, 40}, 
                        {100, 40}},
                    color = {85, 170, 255}), 
                Line(
                    points = {
                        {40, 0}, 
                        {100, 0}},
                    color = {85, 170, 255}), 
                Line(
                    visible = useHeatPort,
                    points = {
                        {0, -100}, 
                        {0, 25}},
                    color = {127, 0, 0},
                    pattern = LinePattern.Dot), 
                Text(
                    extent = {
                        {-150, 90}, 
                        {150, 50}},
                    textString = "%name",
                    lineColor = {0, 0, 255})}));
end IdealCommutingSwitch;