IdealClosingSwitch

model IdealClosingSwitch "Ideal electrical closer"
    import Modelica.ComplexMath.real;
    import Modelica.ComplexMath.conj;

    extends Modelica.Electrical.QuasiStationary.SinglePhase.Interfaces.OnePort;

    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.Blocks.Interfaces.BooleanInput control "true => p--n connected, false => switch open"
        annotation (Placement(transformation(
            origin = {0, 120},
            extent = {
                {-20, -20}, 
                {20, 20}},
            rotation = 270)));
protected
    Complex s(re(final unit = "1"), im(final unit = "1")) "Auxiliary variable";
    constant Modelica.SIunits.ComplexVoltage unitVoltage = Complex(1, 0) annotation (HideResult = true);
    constant Modelica.SIunits.ComplexCurrent unitCurrent = Complex(1, 0) annotation (HideResult = true);
equation
    i = s * unitVoltage * (if control then 1 else Goff);
    v = s * unitCurrent * (if control then Ron else 1);
    LossPower = real(v * conj(i));

    annotation (
        defaultComponentName = "switch",
        Documentation(info = "<html>\n<p>\nThe ideal closing switch has a positive pin p and a negative pin n.\nThe switching behaviour is controlled by input signal control.\nIf control is true, pin p is connected\nwith negative pin n. Otherwise, pin p is not connected\nwith negative pin n.\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 = {
                        {-90, 0}, 
                        {-44, 0}},
                    color = {85, 170, 255}), 
                Line(
                    points = {
                        {-37, 2}, 
                        {40, 40}},
                    color = {85, 170, 255}), 
                Line(
                    points = {
                        {40, 0}, 
                        {90, 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})}),
        Diagram(
            coordinateSystem(
                preserveAspectRatio = true,
                extent = {
                    {-100, -100}, 
                    {100, 100}}),
            graphics = {
                Text(
                    extent = {
                        {-100, -40}, 
                        {100, -79}},
                    textString = "%name",
                    lineColor = {0, 0, 255})}));
end IdealClosingSwitch;