VariableImpedance

model VariableImpedance "Single phase variable impedance"
    import Modelica.ComplexMath.real;
    import Modelica.ComplexMath.imag;
    import Modelica.ComplexMath.conj;

    extends Interfaces.OnePort;

    parameter Modelica.SIunits.Temperature T_ref = 293.15 "Reference temperature";
    parameter Modelica.SIunits.LinearTemperatureCoefficient alpha_ref = 0 "Temperature coefficient of resistance (R_actual = R_ref*(1 + alpha_ref*(heatPort.T - T_ref))";

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

    Modelica.ComplexBlocks.Interfaces.ComplexInput Z_ref "Variable complex impedance"
        annotation (Placement(
            transformation(
                origin = {0, 120},
                extent = {
                    {-20, -20}, 
                    {20, 20}},
                rotation = 270),
            iconTransformation(
                extent = {
                    {-20, -20}, 
                    {20, 20}},
                rotation = 270,
                origin = {0, 120})));
    parameter Boolean frequencyDependent = false "Consider frequency dependency, if true"
        annotation (
            Evaluate = true,
            HideResult = true,
            choices(checkBox = true));
    parameter Modelica.SIunits.Frequency f_ref = 1 "Reference frequency, if frequency dependency is considered"
        annotation (Dialog(enable = frequencyDependent));
    Modelica.SIunits.Resistance R_actual "Resistance = R_ref*(1 + alpha_ref*(heatPort.T - T_ref))";
    Modelica.SIunits.Reactance X_actual "Reactance considering possible frequency dependency";
    Modelica.SIunits.Resistance R_ref = real(Z_ref) "Resistive component of impedance, resistance";
    Modelica.SIunits.Reactance X_ref = imag(Z_ref) "Reactive component of impedance, reactance";
equation
    assert(Modelica.Constants.eps <= 1 + alpha_ref * (T_heatPort - T_ref), "Temperature outside scope of model!");
    v = Complex(R_actual, X_actual) * i;
    LossPower = real(v * conj(i));
    R_actual = R_ref * (1 + alpha_ref * (T_heatPort - T_ref));
    X_actual = X_ref * (if not frequencyDependent then 1 else if 0 <= X_ref then omega / (2 * Modelica.Constants.pi * f_ref) else 2 * Modelica.Constants.pi * f_ref / omega);

    annotation (
        defaultComponentName = "impedance",
        Icon(graphics = {
            Line(
                points = {
                    {60, 0}, 
                    {90, 0}},
                color = {85, 170, 255}), 
            Line(
                points = {
                    {-90, 0}, 
                    {-60, 0}},
                color = {85, 170, 255}), 
            Rectangle(
                extent = {
                    {-70, 30}, 
                    {70, -30}},
                lineColor = {85, 170, 255},
                fillColor = {255, 255, 255},
                fillPattern = FillPattern.Solid), 
            Polygon(
                points = {
                    {-70, -30}, 
                    {70, 30}, 
                    {70, -30}, 
                    {-70, -30}},
                lineColor = {85, 170, 255},
                fillColor = {85, 170, 255},
                fillPattern = FillPattern.Solid), 
            Text(
                extent = {
                    {-150, 90}, 
                    {150, 50}},
                textString = "%name",
                lineColor = {0, 0, 255})}),
        Documentation(info = "<html>\n\n<p>The impedance model represents a <strong>series</strong> connection of a resistor and either an inductor or capacitor.<br>\n<img src=\"modelica://Modelica/Resources/Images/Electrical/QuasiStationary/SinglePhase/Basic/RX_impedance.png\"></p>\n\n<p>\nThe linear impedance connects the complex voltage <code><u>v</u></code> with the\ncomplex current <code><u>i</u></code> by <code><u>i</u>*<u>Z</u> = <u>v</u></code>.\nThe impedance <code>Z_ref = R_ref + j*X_ref</code> is given as complex input signal, representing the\nresistive and reactive component of the input impedance. The resistive\ncomponent is modeled temperature dependent, so the real part <code>R_actual = real(<u>Z</u>)</code> is determined from\nthe actual operating temperature and the reference input resistance <code>real(Z_ref)</code>.\nThe reactive component <code>X_actual = imag(<u>Z</u>)</code>\nis equal to <code>imag(Z_ref)</code> if <code>frequencyDependent = false</code>.\nFrequency dependency is considered by <code>frequencyDependent = true</code>, distinguishing two cases:\n</p>\n\n<dl>\n<dt>(a) <code>imag(Z_ref) &gt; 0</code>: inductive case</dt>\n<dd>The actual reactance <code>X_actual</code> is proportional to <code>f/f_ref</code></dd>\n<dt>(b) <code>imag(Z_ref) &lt; 0</code>: capacitive case</dt>\n<dd>The actual reactance <code>X_actual</code> is proportional to <code>f_ref/f</code></dd>\n</dl>\n\n<h4>Note</h4>\n<p>\nA zero crossing of the real or imaginary part of the impedance signal <code>Z_ref</code> could cause\nsingularities due to the actual structure of the connected network.\n</p>\n\n<h4>See also</h4>\n<p>\n<a href=\"modelica://Modelica.Electrical.QuasiStationary.SinglePhase.Basic.Resistor\">Resistor</a>,\n<a href=\"modelica://Modelica.Electrical.QuasiStationary.SinglePhase.Basic.Conductor\">Conductor</a>,\n<a href=\"modelica://Modelica.Electrical.QuasiStationary.SinglePhase.Basic.Capacitor\">Capacitor</a>,\n<a href=\"modelica://Modelica.Electrical.QuasiStationary.SinglePhase.Basic.Inductor\">Inductor</a>,\n<a href=\"modelica://Modelica.Electrical.QuasiStationary.SinglePhase.Basic.Impedance\">Impedance</a>,\n<a href=\"modelica://Modelica.Electrical.QuasiStationary.SinglePhase.Basic.Admittance\">Admittance</a>,\n<a href=\"modelica://Modelica.Electrical.QuasiStationary.SinglePhase.Basic.VariableResistor\">Variable resistor</a>,\n<a href=\"modelica://Modelica.Electrical.QuasiStationary.SinglePhase.Basic.VariableConductor\">Variable conductor</a>,\n<a href=\"modelica://Modelica.Electrical.QuasiStationary.SinglePhase.Basic.VariableCapacitor\">Variable capacitor</a>,\n<a href=\"modelica://Modelica.Electrical.QuasiStationary.SinglePhase.Basic.VariableInductor\">Variable inductor</a>,\n<a href=\"modelica://Modelica.Electrical.QuasiStationary.SinglePhase.Basic.VariableAdmittance\">Variable admittance</a>\n</p>\n</html>"));
end VariableImpedance;