PowerSensor

model PowerSensor "Power sensor"
    import Modelica.ComplexMath.conj;

    extends Modelica.Icons.RotationalSensor;

    Interfaces.PositivePin currentP annotation (Placement(transformation(extent = {
        {-110, -10}, 
        {-90, 10}})));
    Interfaces.NegativePin currentN annotation (Placement(transformation(extent = {
        {90, -10}, 
        {110, 10}})));
    Interfaces.PositivePin voltageP annotation (Placement(transformation(extent = {
        {-10, 90}, 
        {10, 110}})));
    Interfaces.NegativePin voltageN annotation (Placement(transformation(extent = {
        {-10, -110}, 
        {10, -90}})));
    output Modelica.SIunits.ComplexCurrent i;
    output Modelica.SIunits.ComplexVoltage v;
    Modelica.ComplexBlocks.Interfaces.ComplexOutput y annotation (Placement(
        transformation(
            origin = {-100, -110},
            extent = {
                {-10, -10}, 
                {10, 10}},
            rotation = 270),
        iconTransformation(
            extent = {
                {-10, -10}, 
                {10, 10}},
            rotation = 270,
            origin = {-100, -110})));
    Modelica.SIunits.ApparentPower abs_y = Modelica.ComplexMath.'abs'(y) "Magnitude of complex apparent power";
    Modelica.SIunits.Angle arg_y = Modelica.ComplexMath.arg(y) "Argument of complex apparent power";
equation
    Connections.branch(currentP.reference, currentN.reference);
    Connections.branch(currentP.reference, voltageP.reference);
    Connections.branch(voltageP.reference, voltageN.reference);
    currentP.i + currentN.i = Complex(0);
    voltageP.i + voltageN.i = Complex(0);
    i = currentP.i;
    v = voltageP.v - voltageN.v;
    y = v * conj(i);
    voltageP.i = Complex(0);
    currentP.reference.gamma = currentN.reference.gamma;
    currentP.reference.gamma = voltageP.reference.gamma;
    voltageP.reference.gamma = voltageN.reference.gamma;
    currentP.v - currentN.v = Complex(0);

    annotation (
        Icon(graphics = {
            Line(
                points = {
                    {0, 100}, 
                    {0, 70}},
                color = {85, 170, 255}), 
            Line(
                points = {
                    {0, -70}, 
                    {0, -100}},
                color = {85, 170, 255}), 
            Text(
                extent = {
                    {-29, -70}, 
                    {30, -11}},
                textString = "P"), 
            Line(
                points = {
                    {-100, 0}, 
                    {100, 0}},
                color = {85, 170, 255}), 
            Text(
                extent = {
                    {-150, 110}, 
                    {150, 150}},
                textString = "%name",
                lineColor = {0, 0, 255}), 
            Line(
                points = {
                    {-100, -100}, 
                    {-100, -80}, 
                    {-58, -38}},
                color = {0, 0, 127})}),
        Documentation(info = "<html>\n\n<p>\nThis sensor can be used to measure the complex apparent power.\n</p>\n\n<h4>See also</h4>\n\n<p>\n<a href=\"modelica://Modelica.Electrical.QuasiStationary.SinglePhase.Sensors.ReferenceSensor\">ReferenceSensor</a>,\n<a href=\"modelica://Modelica.Electrical.QuasiStationary.SinglePhase.Sensors.FrequencySensor\">FrequencySensor</a>,\n<a href=\"modelica://Modelica.Electrical.QuasiStationary.SinglePhase.Sensors.PotentialSensor\">PotentialSensor</a>,\n<a href=\"modelica://Modelica.Electrical.QuasiStationary.SinglePhase.Sensors.VoltageSensor\">VoltageSensor</a>,\n<a href=\"modelica://Modelica.Electrical.QuasiStationary.SinglePhase.Sensors.CurrentSensor\">CurrentSensor</a>,\n<a href=\"modelica://Modelica.Electrical.QuasiStationary.SinglePhase.Sensors.MultiSensor\">MultiSensor</a>\n</p>\n\n</html>"));
end PowerSensor;