IEEE 4 node test feeder model with balanced load and Y - D connection (step down). IEEE 4 nodes validation test case with the following characteristics

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Validation.IEEETests.Test4NodesFeeder.BalancedStepDown;
model YD
  "IEEE 4 node test feeder model with balanced load and Y - D connection (step down)"
  extends
    AixLib.Electrical.AC.ThreePhasesUnbalanced.Validation.IEEETests.Test4NodesFeeder.BaseClasses.IEEE4(
    final line1_use_Z_y=true,
    final line2_use_Z_y=false,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeWye
      node1,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeWye
      node2,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeDelta
      node3,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeDelta
      node4,
    final VLL_side1=12.47e3,
    final VLL_side2=4.16e3,
    final VARbase=6000e3,
    final V2_ref={7113,7132,7123},
    final V3_ref={3906,3915,3909},
    final V4_ref={3437,3497,3388},
    final Theta2_ref=Modelica.Constants.pi/180.0*{-0.3,-120.3,119.6},
    final Theta3_ref=Modelica.Constants.pi/180.0*{-3.5,-123.6,116.3},
    final Theta4_ref=Modelica.Constants.pi/180.0*{-7.8,-129.3,110.6},
    loadRL(use_pf_in=false, loadConn=AixLib.Electrical.Types.LoadConnection.wye_to_delta));
  Modelica.Blocks.Sources.Constant load(k=-1800e3)
  AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.ACACTransformerStepDownYD
    transformer(
    VHigh=VLL_side1,
    VLow=VLL_side2,
    XoverR=6,
    Zperc=sqrt(0.01^2 + 0.06^2),
    VABase=VARbase,
    conv1(
      terminal_p(i(start={-477, 327})),
      V1(start={7000, -400})))
equation
  connect(load.y, loadRL.Pow1)
  connect(load.y, loadRL.Pow2)
  connect(load.y, loadRL.Pow3)
  connect(line1.terminal_p, transformer.terminal_n)
  connect(transformer.terminal_p, line2.terminal_n)
  connect(node1.term, line1.terminal_n)
  connect(node2.term, transformer.terminal_n)
  connect(node3.term, line2.terminal_n)
  connect(node4.term, loadRL.terminal)
end YD;


IEEE 4 node test feeder model with balanced load and Y - Y connection (step down). IEEE 4 nodes validation test case with the following characteristics

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Validation.IEEETests.Test4NodesFeeder.BalancedStepDown;
model YY
  "IEEE 4 node test feeder model with balanced load and Y - Y connection (step down)"
  extends
    AixLib.Electrical.AC.ThreePhasesUnbalanced.Validation.IEEETests.Test4NodesFeeder.BaseClasses.IEEE4(
    final line1_use_Z_y=true,
    final line2_use_Z_y=true,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeWye
      node1,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeWye
      node2,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeWye
      node3,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeWye
      node4,
    final VLL_side1=12.47e3,
    final VLL_side2=4.16e3,
    final VARbase=6000e3,
    final V2_ref={7107,7140,7121},
    final V3_ref={2247,2269,2256},
    final V4_ref={1918,2061,1981},
    final Theta2_ref=Modelica.Constants.pi/180.0*{-0.3,-120.3,119.6},
    final Theta3_ref=Modelica.Constants.pi/180.0*{-3.7,-123.5,116.4},
    final Theta4_ref=Modelica.Constants.pi/180.0*{-9.1,-128.3,110.9},
    loadRL(use_pf_in=false));

  Modelica.Blocks.Sources.Constant load(k=-1800e3)
  AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.ACACTransformer
    transformer(
    VHigh=VLL_side1,
    VLow=VLL_side2,
    XoverR=6,
    Zperc=sqrt(0.01^2 + 0.06^2),
    VABase=VARbase)
equation
  connect(load.y, loadRL.Pow1)
  connect(load.y, loadRL.Pow2)
  connect(load.y, loadRL.Pow3)
  connect(line1.terminal_p, transformer.terminal_n)
  connect(transformer.terminal_p, line2.terminal_n)
  connect(node1.term, line1.terminal_n)
  connect(node2.term, transformer.terminal_n)
  connect(node3.term, line2.terminal_n)
  connect(node4.term, loadRL.terminal)
end YY;


IEEE 4 node test feeder model with balanced load and D - D connection (step up). IEEE 4 nodes validation test case with the following characteristics

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Validation.IEEETests.Test4NodesFeeder.BalancedStepUp;
model DD
  "IEEE 4 node test feeder model with balanced load and D - D connection (step up)"
  extends
    AixLib.Electrical.AC.ThreePhasesUnbalanced.Validation.IEEETests.Test4NodesFeeder.BaseClasses.IEEE4(
    final line1_use_Z_y=false,
    final line2_use_Z_y=false,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeDelta
      node1,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeDelta
      node2,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeDelta
      node3,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeDelta
      node4,
    final VLL_side1=12.47e3,
    final VLL_side2=24.9e3,
    final VARbase=6000e3,
    final V2_ref={12361,12372,12348},
    final V3_ref={23723,23746,23698},
    final V4_ref={23657,23688,23625},
    final Theta2_ref=Modelica.Constants.pi/180.0*{29.7,-90.4,149.6},
    final Theta3_ref=Modelica.Constants.pi/180.0*{26.7,-93.4,146.6},
    final Theta4_ref=Modelica.Constants.pi/180.0*{26.6,-93.6,146.5},
    loadRL(use_pf_in=false, loadConn=AixLib.Electrical.Types.LoadConnection.wye_to_delta));
  Modelica.Blocks.Sources.Constant load(k=-1800e3)
  AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.ACACTransformerDD
    transformer(
    VHigh=VLL_side1,
    VLow=VLL_side2,
    XoverR=6,
    Zperc=sqrt(0.01^2 + 0.06^2),
    VABase=VARbase)
equation
  connect(load.y, loadRL.Pow1)
  connect(load.y, loadRL.Pow2)
  connect(load.y, loadRL.Pow3)
  connect(line1.terminal_p, transformer.terminal_n)
  connect(transformer.terminal_p, line2.terminal_n)
  connect(node1.term, line1.terminal_n)
  connect(node2.term, transformer.terminal_n)
  connect(node3.term, line2.terminal_n)
  connect(node4.term, loadRL.terminal)
end DD;


IEEE 4 node test feeder model with balanced load and D - Y connection (step up). IEEE 4 nodes validation test case with the following characteristics

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Validation.IEEETests.Test4NodesFeeder.BalancedStepUp;
model DY
  "IEEE 4 node test feeder model with balanced load and D - Y connection (step up)"
  extends
    AixLib.Electrical.AC.ThreePhasesUnbalanced.Validation.IEEETests.Test4NodesFeeder.BaseClasses.IEEE4(
    final line1_use_Z_y=false,
    final line2_use_Z_y=true,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeDelta
      node1,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeDelta
      node2,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeWye
      node3,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeWye
      node4,
    final VLL_side1=12.47e3,
    final VLL_side2=24.9e3,
    final VARbase=6000e3,
    final V2_ref={12361,12372,12348},
    final V3_ref={13697,13710,13681},
    final V4_ref={13653,13678,13644},
    final Theta2_ref=Modelica.Constants.pi/180.0*{29.7,-90.4,149.6},
    final Theta3_ref=Modelica.Constants.pi/180.0*{26.7,-93.4,146.6},
    final Theta4_ref=Modelica.Constants.pi/180.0*{26.6,-93.5,146.5},
    loadRL(use_pf_in=false));
  Modelica.Blocks.Sources.Constant load(k=-1800e3)
  AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.ACACTransformerStepUpDY
    transformer(
    VHigh=VLL_side1,
    VLow=VLL_side2,
    XoverR=6,
    Zperc=sqrt(0.01^2 + 0.06^2),
    VABase=VARbase,
    conv1(V1(start={11E3, 6E3})),
    conv2(V1(start={-700, -11E3})))
equation
  connect(load.y, loadRL.Pow1)
  connect(load.y, loadRL.Pow2)
  connect(load.y, loadRL.Pow3)
  connect(line1.terminal_p, transformer.terminal_n)
  connect(transformer.terminal_p, line2.terminal_n)
  connect(node1.term, line1.terminal_n)
  connect(node2.term, transformer.terminal_n)
  connect(node3.term, line2.terminal_n)
  connect(node4.term, loadRL.terminal)
end DY;


within AixLib.Electrical.AC.ThreePhasesUnbalanced.Validation.IEEETests.Test4NodesFeeder;
package BalancedStepUp "Package that contains the examples for balanced loads and step up transformer"
  extends Modelica.Icons.ExamplesPackage;
end BalancedStepUp;


IEEE 4 node test feeder model with balanced load and Y - D connection (step up). IEEE 4 nodes validation test case with the following characteristics

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Validation.IEEETests.Test4NodesFeeder.BalancedStepUp;
model YD
  "IEEE 4 node test feeder model with balanced load and Y - D connection (step up)"
  extends
    AixLib.Electrical.AC.ThreePhasesUnbalanced.Validation.IEEETests.Test4NodesFeeder.BaseClasses.IEEE4(
    final line1_use_Z_y=true,
    final line2_use_Z_y=false,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeWye
      node1,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeWye
      node2,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeDelta
      node3,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeDelta
      node4,
    final VLL_side1=12.47e3,
    final VLL_side2=24.9e3,
    final VARbase=6000e3,
    final V2_ref={7128,7145,7137},
    final V3_ref={23746,23722,23698},
    final V4_ref={23680,23663,23625},
    final Theta2_ref=Modelica.Constants.pi/180.0*{-0.3,-120.3,119.6},
    final Theta3_ref=Modelica.Constants.pi/180.0*{56.7,-63.4,176.7},
    final Theta4_ref=Modelica.Constants.pi/180.0*{56.6,-63.6,176.5},
    loadRL(use_pf_in=false,
           loadConn=AixLib.Electrical.Types.LoadConnection.wye_to_delta,
           load1(v(start = {13000, 20000}))));
  Modelica.Blocks.Sources.Constant load(k=-1800e3)
  AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.ACACTransformerStepUpYD
    transformer(
    VHigh=VLL_side1,
    VLow=VLL_side2,
    XoverR=6,
    Zperc=sqrt(0.01^2 + 0.06^2),
    VABase=VARbase)
equation
  connect(load.y, loadRL.Pow1)
  connect(load.y, loadRL.Pow2)
  connect(load.y, loadRL.Pow3)
  connect(line1.terminal_p, transformer.terminal_n)
  connect(transformer.terminal_p, line2.terminal_n)
  connect(node1.term, line1.terminal_n)
  connect(node2.term, transformer.terminal_n)
  connect(node3.term, line2.terminal_n)
  connect(node4.term, loadRL.terminal)
end YD;


IEEE 4 node test feeder model with balanced load and Y - Y connection (step up). IEEE 4 nodes validation test case with the following characteristics

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Validation.IEEETests.Test4NodesFeeder.BalancedStepUp;
model YY
  "IEEE 4 node test feeder model with balanced load and Y - Y connection (step up)"
  extends
    AixLib.Electrical.AC.ThreePhasesUnbalanced.Validation.IEEETests.Test4NodesFeeder.BaseClasses.IEEE4(
    final line1_use_Z_y=true,
    final line2_use_Z_y=true,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeWye
      node1,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeWye
      node2,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeWye
      node3,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeWye
      node4,
    final VLL_side1=12.47e3,
    final VLL_side2=24.9e3,
    final VARbase=6000e3,
    final V2_ref={7126,7145,7137},
    final V3_ref={13675,13715,13698},
    final V4_ref={13631,13682,13661},
    final Theta2_ref=Modelica.Constants.pi/180.0*{-0.3,-120.4,119.6},
    final Theta3_ref=Modelica.Constants.pi/180.0*{-3.3,-123.4,116.6},
    final Theta4_ref=Modelica.Constants.pi/180.0*{-3.5,-123.5,116.5},
    loadRL(use_pf_in=false));
  Modelica.Blocks.Sources.Constant load(k=-1800e3)
  AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.ACACTransformer
    transformer(
    VHigh=VLL_side1,
    VLow=VLL_side2,
    XoverR=6,
    Zperc=sqrt(0.01^2 + 0.06^2),
    VABase=VARbase)
equation
  connect(load.y, loadRL.Pow1)
  connect(load.y, loadRL.Pow2)
  connect(load.y, loadRL.Pow3)
  connect(line1.terminal_p, transformer.terminal_n)
  connect(transformer.terminal_p, line2.terminal_n)
  connect(node1.term, source.terminal)
  connect(node2.term, transformer.terminal_n)
  connect(node3.term, line2.terminal_n)
  connect(node4.term, loadRL.terminal)
end YY;


Base model of the IEEE 4 nodes test feeder. This is a partial model that is extended by all the other validation test cases.
This model defined replaceable probes and transformer so they can be
easily changed when implementing the different tests.

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Validation.IEEETests.Test4NodesFeeder.BaseClasses;
partial model IEEE4 "Base model of the IEEE 4 nodes test feeder"
  extends Modelica.Icons.Example;
  parameter Modelica.Units.SI.Voltage VLL_side1=12.47e3
    "Voltage line to line side 1";
  parameter Modelica.Units.SI.Voltage VLL_side2=4.16e3
    "Voltage line to line side 2";

  parameter Modelica.Units.SI.ApparentPower VARbase=6000e3
    "Base VA power of the transformer";

  parameter Boolean line1_use_Z_y = true
    "Choose between Zy or Zd impedance matrix for line 1";
  parameter Boolean line2_use_Z_y = true
    "Choose between Zy or Zd impedance matrix for line 2";

  parameter Modelica.Units.SI.Voltage V2_ref[3]={7107,7140,7121}
    "Reference RMS voltage node 2 - IEEE results";
  parameter Modelica.Units.SI.Voltage V3_ref[3]={2247,2269,2256}
    "Reference RMS voltage node 3 - IEEE results";
  parameter Modelica.Units.SI.Voltage V4_ref[3]={1918,2061,1981}
    "Reference RMS voltage node 4 - IEEE results";
  parameter Modelica.Units.SI.Angle Theta2_ref[3](each displayUnit="deg") = {-0.3,
    -120.3,119.6} "Reference voltage phase angle node 2 - IEEE results";
  parameter Modelica.Units.SI.Angle Theta3_ref[3](each displayUnit="deg") = {-3.7,
    -123.5,116.4} "Reference voltage phase angle node 3 - IEEE results";
  parameter Modelica.Units.SI.Angle Theta4_ref[3](each displayUnit="deg") = {-9.1,
    -128.3,110.9} "Reference voltage phase angle node 4 - IEEE results";
  Modelica.Units.SI.Voltage err_V2[3]=node2.V - V2_ref
    "Error on voltage at node 2";
  Modelica.Units.SI.Voltage err_V3[3]=node3.V - V3_ref
    "Error on voltage at node 3";
  Modelica.Units.SI.Voltage err_V4[3]=node4.V - V4_ref
    "Error on voltage at node 4";
  Modelica.Units.SI.Angle err_Theta2[3](each displayUnit="deg") = node2.theta
     - Theta2_ref "Error on voltage at node 2";
  Modelica.Units.SI.Angle err_Theta3[3](each displayUnit="deg") = node3.theta
     - Theta3_ref "Error on voltage at node 3";
  Modelica.Units.SI.Angle err_Theta4[3](each displayUnit="deg") = node4.theta
     - Theta4_ref "Error on voltage at node 4";
  Real err_V2_percent[3] = 100*{err_V2[i]/V2_ref[i] for i in 1:3}
    "Error in RMS voltage at node 2 -- percent";
  Real err_V3_percent[3] = 100*{err_V3[i]/V3_ref[i] for i in 1:3}
    "Error in RMS voltage at node 3 -- percent";
  Real err_V4_percent[3] = 100*{err_V4[i]/V4_ref[i] for i in 1:3}
    "Error in RMS voltage at node 4 -- percent";
  Real err_Theta2_percent[3] = 100*{err_Theta2[i]/Theta2_ref[i] for i in 1:3}
    "Error in voltage phase angle at node 2 -- percent";
  Real err_Theta3_percent[3] = 100*{err_Theta3[i]/Theta3_ref[i] for i in 1:3}
    "Error in voltage phase angle at node 3 -- percent";
  Real err_Theta4_percent[3] = 100*{err_Theta4[i]/Theta4_ref[i] for i in 1:3}
    "Error in voltage phase angle at node 4 -- percent";
  AixLib.Electrical.AC.ThreePhasesUnbalanced.Sources.FixedVoltage source(
    f=60,
    V=VLL_side1) "Voltage source"
  AixLib.Electrical.AC.ThreePhasesUnbalanced.Lines.TwoPortMatrixRL line1(
    Z11=L1*(if line1_use_Z_y then Z11_y else Z11_d),
    Z12=L1*(if line1_use_Z_y then Z12_y else Z12_d),
    Z13=L1*(if line1_use_Z_y then Z13_y else Z13_d),
    Z22=L1*(if line1_use_Z_y then Z22_y else Z22_d),
    Z23=L1*(if line1_use_Z_y then Z23_y else Z23_d),
    Z33=L1*(if line1_use_Z_y then Z33_y else Z33_d),
    V_nominal=VLL_side1) "Line at primary side"
  AixLib.Electrical.AC.ThreePhasesUnbalanced.Lines.TwoPortMatrixRL line2(
    Z11=L2*(if line2_use_Z_y then Z11_y else Z11_d),
    Z12=L2*(if line2_use_Z_y then Z12_y else Z12_d),
    Z13=L2*(if line2_use_Z_y then Z13_y else Z13_d),
    Z22=L2*(if line2_use_Z_y then Z22_y else Z22_d),
    Z23=L2*(if line2_use_Z_y then Z23_y else Z23_d),
    Z33=L2*(if line2_use_Z_y then Z33_y else Z33_d),
    V_nominal=VLL_side2) "Line at secondary side"
  AixLib.Electrical.AC.ThreePhasesUnbalanced.Loads.Inductive loadRL(
    pf=0.9,
    V_nominal=VLL_side2,
    mode=AixLib.Electrical.Types.Load.VariableZ_P_input,
    use_pf_in=true) "Load"
  replaceable
    AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.BaseClasses.GeneralizedProbe
    node1(perUnit=false, V_nominal=VLL_side1) "Probe at source"
  replaceable
    AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.BaseClasses.GeneralizedProbe
    node2(perUnit=false, V_nominal=VLL_side1)
    "Probe at the primary side of the transformer"
  replaceable
    AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.BaseClasses.GeneralizedProbe
    node3(perUnit=false, V_nominal=VLL_side2)
    "Probe at the secondary side of the transformer"
  replaceable
    AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.BaseClasses.GeneralizedProbe
    node4(perUnit=false, V_nominal=VLL_side2) "Probe at the load"

protected
  parameter Real L1 = 2000*(1.0/5280.0) "Length line 1 in miles";
  parameter Real L2 = 2500*(1.0/5280.0) "Length line 2 in miles";

  parameter Modelica.Units.SI.Impedance Z11_d[2]={0.4013,1.4133}
    "Element [1,1] of impedance matrix";
  parameter Modelica.Units.SI.Impedance Z12_d[2]={0.0953,0.8515}
    "Element [1,2] of impedance matrix";
  parameter Modelica.Units.SI.Impedance Z13_d[2]={0.0953,0.7266}
    "Element [1,3] of impedance matrix";
  parameter Modelica.Units.SI.Impedance Z22_d[2]={0.4013,1.4133}
    "Element [2,2] of impedance matrix";
  parameter Modelica.Units.SI.Impedance Z23_d[2]={0.0953,0.7802}
    "Element [2,3] of impedance matrix";
  parameter Modelica.Units.SI.Impedance Z33_d[2]={0.4013,1.4133}
    "Element [3,3] of impedance matrix";

  parameter Modelica.Units.SI.Impedance Z11_y[2]={0.4576,1.078}
    "Element [1,1] of impedance matrix";
  parameter Modelica.Units.SI.Impedance Z12_y[2]={0.1559,0.5017}
    "Element [1,2] of impedance matrix";
  parameter Modelica.Units.SI.Impedance Z13_y[2]={0.1535,0.3849}
    "Element [1,3] of impedance matrix";
  parameter Modelica.Units.SI.Impedance Z22_y[2]={0.4666,1.0482}
    "Element [2,2] of impedance matrix";
  parameter Modelica.Units.SI.Impedance Z23_y[2]={0.158,0.4236}
    "Element [2,3] of impedance matrix";
  parameter Modelica.Units.SI.Impedance Z33_y[2]={0.4615,1.0651}
    "Element [3,3] of impedance matrix";
equation
  connect(source.terminal, line1.terminal_n)
  connect(line2.terminal_p, loadRL.terminal)
end IEEE4;


within AixLib.Electrical.AC.ThreePhasesUnbalanced.Validation.IEEETests.Test4NodesFeeder;
package BaseClasses "This package contains the base classes used by the IEEE 4 nodes test feeder"
extends Modelica.Icons.BasesPackage;
end BaseClasses;


IEEE 4 node test feeder model with unbalanced load and D - D connection (step down). IEEE 4 nodes validation test case with the following characteristics

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Validation.IEEETests.Test4NodesFeeder.UnbalancedStepDown;
model DD
  "IEEE 4 node test feeder model with unbalanced load and D - D connection (step down)"
  extends
    AixLib.Electrical.AC.ThreePhasesUnbalanced.Validation.IEEETests.Test4NodesFeeder.BaseClasses.IEEE4(
    final line1_use_Z_y=false,
    final line2_use_Z_y=false,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeDelta
      node1,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeDelta
      node2,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeDelta
      node3,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeDelta
      node4,
    final VLL_side1=12.47e3,
    final VLL_side2=4.16e3,
    final VARbase=6000e3,
    final V2_ref={12341,12370,12302},
    final V3_ref={3902,3972,3871},
    final V4_ref={3431,3647,3294},
    final Theta2_ref=Modelica.Constants.pi/180.0*{29.8,-90.5,149.5},
    final Theta3_ref=Modelica.Constants.pi/180.0*{27.2,-93.9,145.7},
    final Theta4_ref=Modelica.Constants.pi/180.0*{24.3,-100.4,138.6},
    loadRL(loadConn=AixLib.Electrical.Types.LoadConnection.wye_to_delta,
        use_pf_in=true));
  AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.ACACTransformerDD
    transformer(
    VHigh=VLL_side1,
    VLow=VLL_side2,
    XoverR=6,
    Zperc=sqrt(0.01^2 + 0.06^2),
    VABase=VARbase)
  Modelica.Blocks.Sources.Constant load3(k=-2375e3)
  Modelica.Blocks.Sources.Constant load2(k=-1800e3)
  Modelica.Blocks.Sources.Constant load1(k=-1275e3)
  Modelica.Blocks.Sources.Constant pf1(k=0.85)
  Modelica.Blocks.Sources.Constant pf2(k=0.9)
  Modelica.Blocks.Sources.Constant pf3(k=0.95)
equation
  connect(line1.terminal_p, transformer.terminal_n)
  connect(transformer.terminal_p, line2.terminal_n)
  connect(load1.y, loadRL.Pow1)
  connect(load2.y, loadRL.Pow2)
  connect(load3.y, loadRL.Pow3)
  connect(pf1.y, loadRL.pf_in_1)
  connect(pf2.y, loadRL.pf_in_2)
  connect(pf3.y, loadRL.pf_in_3)
  connect(node1.term, line1.terminal_n)
  connect(node2.term, transformer.terminal_n)
  connect(node3.term, line2.terminal_n)
  connect(node4.term, loadRL.terminal)
end DD;


IEEE 4 node test feeder model with unbalanced load and D - Y connection (step down). IEEE 4 nodes validation test case with the following characteristics

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Validation.IEEETests.Test4NodesFeeder.UnbalancedStepDown;
model DY
  "IEEE 4 node test feeder model with unbalanced load and D - Y connection (step down)"
  extends
    AixLib.Electrical.AC.ThreePhasesUnbalanced.Validation.IEEETests.Test4NodesFeeder.BaseClasses.IEEE4(
    final line1_use_Z_y=false,
    final line2_use_Z_y=true,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeDelta
      node1,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeDelta
      node2,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeWye
      node3,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeWye
      node4,
    final VLL_side1=12.47e3,
    final VLL_side2=4.16e3,
    final VARbase=6000e3,
    final V2_ref={12350,12314,12333},
    final V3_ref={2290,2261,2214},
    final V4_ref={2157,1936,1849},
    final Theta2_ref=Modelica.Constants.pi/180.0*{29.6,-90.4,149.8},
    final Theta3_ref=Modelica.Constants.pi/180.0*{-32.4,-153.8,85.2},
    final Theta4_ref=Modelica.Constants.pi/180.0*{-34.2,-157.0,73.4},
    loadRL(use_pf_in=true),
    line2(terminal_p(phase(v(each start=-1)))));
  AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.ACACTransformerStepDownDY
    transformer(
    VHigh=VLL_side1,
    VLow=VLL_side2,
    XoverR=6,
    Zperc=sqrt(0.01^2 + 0.06^2),
    VABase=VARbase)
  Modelica.Blocks.Sources.Constant load3(k=-2375e3)
  Modelica.Blocks.Sources.Constant load2(k=-1800e3)
  Modelica.Blocks.Sources.Constant pf1(k=0.85)
  Modelica.Blocks.Sources.Constant pf2(k=0.9)
  Modelica.Blocks.Sources.Constant pf3(k=0.95)
  Modelica.Blocks.Sources.Constant load1(k=-1275e3)
equation
  connect(line1.terminal_p, transformer.terminal_n)
  connect(transformer.terminal_p, line2.terminal_n)
  connect(pf1.y, loadRL.pf_in_1)
  connect(pf2.y, loadRL.pf_in_2)
  connect(pf3.y, loadRL.pf_in_3)
  connect(load2.y, loadRL.Pow2)
  connect(load3.y, loadRL.Pow3)
  connect(load1.y, loadRL.Pow1)
  connect(node1.term, line1.terminal_n)
  connect(node2.term, transformer.terminal_n)
  connect(node3.term, line2.terminal_n)
  connect(node4.term, loadRL.terminal)
end DY;


within AixLib.Electrical.AC.ThreePhasesUnbalanced.Validation.IEEETests.Test4NodesFeeder;
package UnbalancedStepDown "Package that contains the examples for unbalanced loads and step down transformer"
  extends Modelica.Icons.ExamplesPackage;
end UnbalancedStepDown;


IEEE 4 node test feeder model with unbalanced load and Y - D connection (step down). IEEE 4 nodes validation test case with the following characteristics

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Validation.IEEETests.Test4NodesFeeder.UnbalancedStepDown;
model YD
  "IEEE 4 node test feeder model with unbalanced load and Y - D connection (step down)"
  extends
    AixLib.Electrical.AC.ThreePhasesUnbalanced.Validation.IEEETests.Test4NodesFeeder.BaseClasses.IEEE4(
    final line1_use_Z_y=true,
    final line2_use_Z_y=false,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeWye
      node1,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeWye
      node2,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeDelta
      node3,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeDelta
      node4,
    final VLL_side1=12.47e3,
    final VLL_side2=4.16e3,
    final VARbase=6000e3,
    final V2_ref={7113,7144,7111},
    final V3_ref={3896,3972,3875},
    final V4_ref={3425,3646,3298},
    final Theta2_ref=Modelica.Constants.pi/180.0*{-0.2,-120.4,119.5},
    final Theta3_ref=Modelica.Constants.pi/180.0*{-2.8,-123.8,115.7},
    final Theta4_ref=Modelica.Constants.pi/180.0*{-5.8,-130.3,108.6},
    loadRL(loadConn=AixLib.Electrical.Types.LoadConnection.wye_to_delta,
           use_pf_in=true,
           load1(v(each start = 3000))),
    line1(i1(each start=200)));
  AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.ACACTransformerStepDownYD
    transformer(
    VHigh=VLL_side1,
    VLow=VLL_side2,
    XoverR=6,
    Zperc=sqrt(0.01^2 + 0.06^2),
    VABase=VARbase)
  Modelica.Blocks.Sources.Constant load3(k=-2375e3)
  Modelica.Blocks.Sources.Constant load2(k=-1800e3)
  Modelica.Blocks.Sources.Constant load1(k=-1275e3)
  Modelica.Blocks.Sources.Constant pf1(k=0.85)
  Modelica.Blocks.Sources.Constant pf2(k=0.9)
  Modelica.Blocks.Sources.Constant pf3(k=0.95)
equation
  connect(line1.terminal_p, transformer.terminal_n)
  connect(transformer.terminal_p, line2.terminal_n)
  connect(load2.y, loadRL.Pow2)
  connect(load3.y, loadRL.Pow3)
  connect(load1.y, loadRL.Pow1)
  connect(pf1.y, loadRL.pf_in_1)
  connect(pf2.y, loadRL.pf_in_2)
  connect(pf3.y, loadRL.pf_in_3)
  connect(node1.term, line1.terminal_n)
  connect(node2.term, transformer.terminal_n)
  connect(node3.term, line2.terminal_n)
  connect(node4.term, loadRL.terminal)
end YD;


IEEE 4 node test feeder model with unbalanced load and Y - Y connection (step down). IEEE 4 nodes validation test case with the following characteristics

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Validation.IEEETests.Test4NodesFeeder.UnbalancedStepDown;
model YY
  "IEEE 4 node test feeder model with unbalanced load and Y - Y connection (step down)"
  extends
    AixLib.Electrical.AC.ThreePhasesUnbalanced.Validation.IEEETests.Test4NodesFeeder.BaseClasses.IEEE4(
    final line1_use_Z_y=true,
    final line2_use_Z_y=true,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeWye
      node1,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeWye
      node2,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeWye
      node3,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeWye
      node4,
    final VLL_side1=12.47e3,
    final VLL_side2=4.16e3,
    final VARbase=6000e3,
    final V2_ref={7164,7110,7082},
    final V3_ref={2305,2255,2203},
    final V4_ref={2175,1930,1833},
    final Theta2_ref=Modelica.Constants.pi/180.0*{-0.1,-120.2,119.3},
    final Theta3_ref=Modelica.Constants.pi/180.0*{-2.3,-123.6,114.8},
    final Theta4_ref=Modelica.Constants.pi/180.0*{-4.1,-126.8,102.8},
    loadRL(use_pf_in=true),
    line1(i1(start={180, -130}),
          i2(start={-300, -160}),
          i3(start={40, 450})),
    line2(i2(start={-900, -480}),
          i3(start={130, 1400})));

  Modelica.Blocks.Sources.Constant load1(k=-1275e3)
  Modelica.Blocks.Sources.Constant load2(k=-1800e3)
  Modelica.Blocks.Sources.Constant load3(k=-2375e3)
  Modelica.Blocks.Sources.Constant pf1(k=0.85)
  Modelica.Blocks.Sources.Constant pf2(k=0.9)
  Modelica.Blocks.Sources.Constant pf3(k=0.95)
  AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.ACACTransformer
    transformer(
    VHigh=VLL_side1,
    VLow=VLL_side2,
    XoverR=6,
    Zperc=sqrt(0.01^2 + 0.06^2),
    VABase=VARbase)
equation
  connect(load1.y, loadRL.Pow1)
  connect(load2.y, loadRL.Pow2)
  connect(load3.y, loadRL.Pow3)
  connect(pf1.y, loadRL.pf_in_1)
  connect(pf2.y, loadRL.pf_in_2)
  connect(pf3.y, loadRL.pf_in_3)
  connect(line1.terminal_p, transformer.terminal_n)
  connect(transformer.terminal_p, line2.terminal_n)
  connect(node1.term, line1.terminal_n)
  connect(node2.term, transformer.terminal_n)
  connect(node3.term, line2.terminal_n)
  connect(node4.term, loadRL.terminal)
end YY;


IEEE 4 node test feeder model with unbalanced load and D - D connection (step up). IEEE 4 nodes validation test case with the following characteristics

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Validation.IEEETests.Test4NodesFeeder.UnbalancedStepUp;
model DD
  "IEEE 4 node test feeder model with unbalanced load and D - D connection (step up)"
  extends
    AixLib.Electrical.AC.ThreePhasesUnbalanced.Validation.IEEETests.Test4NodesFeeder.BaseClasses.IEEE4(
    final line1_use_Z_y=false,
    final line2_use_Z_y=false,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeDelta
      node1,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeDelta
      node2,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeDelta
      node3,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeDelta
      node4,
    final VLL_side1=12.47e3,
    final VLL_side2=24.9e3,
    final VARbase=6000e3,
    final V2_ref={12362,12392,12334},
    final V3_ref={23675,24060,23573},
    final V4_ref={23610,24015,23492},
    final Theta2_ref=Modelica.Constants.pi/180.0*{29.8,-90.4,149.5},
    final Theta3_ref=Modelica.Constants.pi/180.0*{27.2,-93.6,146.0},
    final Theta4_ref=Modelica.Constants.pi/180.0*{27.2,-93.7,145.9},
    loadRL(loadConn=AixLib.Electrical.Types.LoadConnection.wye_to_delta,
        use_pf_in=true));
  AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.ACACTransformerDD
    transformer(
    VHigh=VLL_side1,
    VLow=VLL_side2,
    XoverR=6,
    Zperc=sqrt(0.01^2 + 0.06^2),
    VABase=VARbase)
  Modelica.Blocks.Sources.Constant load3(k=-2375e3)
  Modelica.Blocks.Sources.Constant load2(k=-1800e3)
  Modelica.Blocks.Sources.Constant load1(k=-1275e3)
  Modelica.Blocks.Sources.Constant pf1(k=0.85)
  Modelica.Blocks.Sources.Constant pf2(k=0.9)
  Modelica.Blocks.Sources.Constant pf3(k=0.95)
equation
  connect(line1.terminal_p, transformer.terminal_n)
  connect(transformer.terminal_p, line2.terminal_n)
  connect(load1.y, loadRL.Pow1)
  connect(load2.y, loadRL.Pow2)
  connect(load3.y, loadRL.Pow3)
  connect(pf1.y, loadRL.pf_in_1)
  connect(pf2.y, loadRL.pf_in_2)
  connect(pf3.y, loadRL.pf_in_3)
  connect(node1.term, source.terminal)
  connect(node2.term, transformer.terminal_n)
  connect(node3.term, line2.terminal_n)
  connect(node4.term, loadRL.terminal)
end DD;


IEEE 4 node test feeder model with unbalanced load and D - Y connection (step up). IEEE 4 nodes validation test case with the following characteristics

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Validation.IEEETests.Test4NodesFeeder.UnbalancedStepUp;
model DY
  "IEEE 4 node test feeder model with unbalanced load and D - Y connection (step up)"
  extends
    AixLib.Electrical.AC.ThreePhasesUnbalanced.Validation.IEEETests.Test4NodesFeeder.BaseClasses.IEEE4(
    final line1_use_Z_y=false,
    final line2_use_Z_y=true,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeDelta
      node1,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeDelta
      node2,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeWye
      node3,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeWye
      node4,
    final VLL_side1=12.47e3,
    final VLL_side2=24.9e3,
    final VARbase=6000e3,
    final V2_ref={12364,12391,12333},
    final V3_ref={13792,13733,13641},
    final V4_ref={13768,13684,13600},
    final Theta2_ref=Modelica.Constants.pi/180.0*{29.8,-90.5,149.6},
    final Theta3_ref=Modelica.Constants.pi/180.0*{27.7,-93.5,145.4},
    final Theta4_ref=Modelica.Constants.pi/180.0*{27.7,-93.6,145.2},
    loadRL(use_pf_in=true));
  AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.ACACTransformerStepUpDY
    transformer(
    VHigh=VLL_side1,
    VLow=VLL_side2,
    XoverR=6,
    Zperc=sqrt(0.01^2 + 0.06^2),
    VABase=VARbase,
    conv1(V1(start={10E3, 5.5E3})))
  Modelica.Blocks.Sources.Constant load3(k=-2375e3)
  Modelica.Blocks.Sources.Constant load2(k=-1800e3)
  Modelica.Blocks.Sources.Constant pf1(k=0.85)
  Modelica.Blocks.Sources.Constant pf2(k=0.9)
  Modelica.Blocks.Sources.Constant pf3(k=0.95)
  Modelica.Blocks.Sources.Constant load1(k=-1275e3)
equation
  connect(line1.terminal_p, transformer.terminal_n)
  connect(transformer.terminal_p, line2.terminal_n)
  connect(pf1.y, loadRL.pf_in_1)
  connect(pf2.y, loadRL.pf_in_2)
  connect(pf3.y, loadRL.pf_in_3)
  connect(load2.y, loadRL.Pow2)
  connect(load3.y, loadRL.Pow3)
  connect(load1.y, loadRL.Pow1)
  connect(node1.term, line1.terminal_n)
  connect(node2.term, transformer.terminal_n)
  connect(node3.term, line2.terminal_n)
  connect(node4.term, loadRL.terminal)
end DY;


within AixLib.Electrical.AC.ThreePhasesUnbalanced.Validation.IEEETests.Test4NodesFeeder;
package UnbalancedStepUp "Package that contains the examples for unbalanced loads and step up transformer"
  extends Modelica.Icons.ExamplesPackage;
end UnbalancedStepUp;


IEEE 4 node test feeder model with unbalanced load and Y - D connection (step up). IEEE 4 nodes validation test case with the following characteristics

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Validation.IEEETests.Test4NodesFeeder.UnbalancedStepUp;
model YD
  "IEEE 4 node test feeder model with unbalanced load and Y - D connection (step up)"
  extends
    AixLib.Electrical.AC.ThreePhasesUnbalanced.Validation.IEEETests.Test4NodesFeeder.BaseClasses.IEEE4(
    final line1_use_Z_y=true,
    final line2_use_Z_y=false,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeWye
      node1,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeWye
      node2,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeDelta
      node3,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeDelta
      node4,
    final VLL_side1=12.47e3,
    final VLL_side2=24.9e3,
    final VARbase=6000e3,
    final V2_ref={7121,7147,7150},
    final V3_ref={23703,24040,23576},
    final V4_ref={23637,23995,23496},
    final Theta2_ref=Modelica.Constants.pi/180.0*{-0.4,-120.3,119.5},
    final Theta3_ref=Modelica.Constants.pi/180.0*{57.2,-63.6,176.1},
    final Theta4_ref=Modelica.Constants.pi/180.0*{57.1,-63.8,175.9},
    loadRL(loadConn=AixLib.Electrical.Types.LoadConnection.wye_to_delta,
        use_pf_in=true));
  AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.ACACTransformerStepUpYD
    transformer(
    VHigh=VLL_side1,
    VLow=VLL_side2,
    XoverR=6,
    Zperc=sqrt(0.01^2 + 0.06^2),
    VABase=VARbase)
  Modelica.Blocks.Sources.Constant load3(k=-2375e3)
  Modelica.Blocks.Sources.Constant load2(k=-1800e3)
  Modelica.Blocks.Sources.Constant load1(k=-1275e3)
  Modelica.Blocks.Sources.Constant pf1(k=0.85)
  Modelica.Blocks.Sources.Constant pf2(k=0.9)
  Modelica.Blocks.Sources.Constant pf3(k=0.95)
equation
  connect(line1.terminal_p, transformer.terminal_n)
  connect(transformer.terminal_p, line2.terminal_n)
  connect(load2.y, loadRL.Pow2)
  connect(load3.y, loadRL.Pow3)
  connect(load1.y, loadRL.Pow1)
  connect(pf1.y, loadRL.pf_in_1)
  connect(pf2.y, loadRL.pf_in_2)
  connect(pf3.y, loadRL.pf_in_3)
  connect(node1.term, line1.terminal_n)
  connect(node2.term, transformer.terminal_n)
  connect(node3.term, line2.terminal_n)
  connect(node4.term, loadRL.terminal)
end YD;


within AixLib.Electrical.AC.ThreePhasesUnbalanced.Validation.IEEETests.Test4NodesFeeder.UnbalancedStepUp;
model YY
  "IEEE 4 node test feeder model with unbalanced load and Y - Y connection (step up)"
  extends
    AixLib.Electrical.AC.ThreePhasesUnbalanced.Validation.IEEETests.Test4NodesFeeder.BaseClasses.IEEE4(
    final line1_use_Z_y=true,
    final line2_use_Z_y=true,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeWye
      node1,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeWye
      node2,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeWye
      node3,
    redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeWye
      node4,
    final VLL_side1=12.47e3,
    final VLL_side2=24.9e3,
    final VARbase=6000e3,
    final V2_ref={7161,7120,7128},
    final V3_ref={13839,13663,13655},
    final V4_ref={13815,13614,13615},
    final Theta2_ref=Modelica.Constants.pi/180.0*{-0.1,-120.3,119.3},
    final Theta3_ref=Modelica.Constants.pi/180.0*{-2.1,-123.3,115.1},
    final Theta4_ref=Modelica.Constants.pi/180.0*{-2.2,-123.4,114.9},
    loadRL(use_pf_in=true));
  Modelica.Blocks.Sources.Constant load2(k=-1800e3)
  Modelica.Blocks.Sources.Constant load3(k=-2375e3)
  Modelica.Blocks.Sources.Constant load1(k=-1275e3)
  Modelica.Blocks.Sources.Constant pf1(k=0.85)
  Modelica.Blocks.Sources.Constant pf2(k=0.9)
  Modelica.Blocks.Sources.Constant pf3(k=0.95)
  AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.ACACTransformer
    transformer(
    VHigh=VLL_side1,
    VLow=VLL_side2,
    XoverR=6,
    Zperc=sqrt(0.01^2 + 0.06^2),
    VABase=VARbase,
    conv1(V1(start={6.9E3, -250})),
    conv2(V1(start={-3.8E3, -5.7E3})))
equation
  connect(load2.y, loadRL.Pow2)
  connect(load3.y, loadRL.Pow3)
  connect(load1.y, loadRL.Pow1)
  connect(pf1.y, loadRL.pf_in_1)
  connect(pf2.y, loadRL.pf_in_2)
  connect(pf3.y, loadRL.pf_in_3)
  connect(line1.terminal_p, transformer.terminal_n)
  connect(transformer.terminal_p, line2.terminal_n)
  connect(node1.term, line1.terminal_n)
  connect(node2.term, transformer.terminal_n)
  connect(node3.term, line2.terminal_n)
  connect(node4.term, loadRL.terminal)
end YY;


within AixLib.Electrical;
package BaseClasses "Package with base classes for AixLib.Electrical"
  extends Modelica.Icons.BasesPackage;
end BaseClasses;


info

within AixLib.Electrical.BaseClasses;
package WindTurbine
end WindTurbine;


within AixLib.Electrical.BaseClasses.WindTurbine;
model PartialWindTurbine
  "Partial model of a wind turbine with power output based on table as a function of wind speed"
  extends AixLib.Electrical.BaseClasses.WindTurbine.PartialWindTurbineBase;
  replaceable package PhaseSystem =
      AixLib.Electrical.PhaseSystems.PartialPhaseSystem
       constrainedby AixLib.Electrical.PhaseSystems.PartialPhaseSystem
    "Phase system"

  replaceable AixLib.Electrical.Interfaces.Terminal terminal(
    redeclare package PhaseSystem = PhaseSystem) "Generalized terminal"
protected
  Modelica.Blocks.Tables.CombiTable1Ds per(
    final tableOnFile=tableOnFile,
    final table=cat(1, cat(1, [0, 0], table),
                    [vOut+10*Modelica.Constants.eps, 0;
                     vOut+20*Modelica.Constants.eps, 0]),
    final tableName=tableName,
    final fileName=fileName,
    final columns=2:2,
    final smoothness=Modelica.Blocks.Types.Smoothness.LinearSegments)
    "Performance table that maps wind speed to electrical power output"
  Modelica.Blocks.Math.Gain gain(final k=scale)
    "Gain, used to allow a user to easily scale the power"
  DC.Sources.BaseClasses.WindCorrection                      cor(
    final h=h,
    final hRef=hRef,
    final n=nWin) "Correction for wind"
initial equation
  assert(abs(table[1,2]) < Modelica.Constants.eps,
  "First data point of performance table must be at cut-in wind speed,
   and be equal to 0 Watts.
   Received + " + String(table[1,1]) + " m/s with " + String(table[1,2]) + " Watts");
equation
  assert(gain.y>=0, "Wind power must be positive");
  connect(per.y[1],gain. u)
  connect(vWin,cor. vRef)
  connect(cor.vLoc,per. u)
  connect(gain.y, P)
end PartialWindTurbine;


Base class for turbine model that contains basic parameters. This partial model contains the minimum set of parameters necessary to describe
a wind turbine.
The model defines also an output P for the power generated by the wind turbine.

within AixLib.Electrical.BaseClasses.WindTurbine;
partial model PartialWindTurbineBase
  "Base class for turbine model that contains basic parameters"
  final parameter Modelica.Units.SI.Velocity vIn=table[1, 1]
    "Cut-in steady wind speed";
  final parameter Modelica.Units.SI.Velocity vOut=table[size(table, 1), 1]
    "Cut-out steady wind speed";
  parameter Real scale(min=0)=1
    "Scaling factor, used to allow adjusting the power output without changing the table";

  parameter Real h "Height over ground"
  parameter Modelica.Units.SI.Height hRef=10
    "Reference height for wind measurement"
 parameter Real nWin(min=0) = 0.4
    "Height exponent for wind profile calculation"

  parameter Boolean tableOnFile=false
    "true, if table is defined on file or in function usertab";
  parameter Real table[:,2]=
          [3.5, 0;
           5.5, 0.1;
           12, 0.9;
           14, 1;
           25, 1]
    "Table of generated power (first column is wind speed, second column is power)";
  parameter String tableName="NoName"
    "Table name on file or in function usertab (see documentation)";
  parameter String fileName="NoName" "File where matrix is stored";
  parameter Modelica.Units.SI.Voltage V_nominal(min=0, start=110)
    "Nominal voltage (V_nominal >= 0)"
  Modelica.Blocks.Interfaces.RealInput vWin(unit="m/s") "Steady wind speed"
  Modelica.Blocks.Interfaces.RealOutput P(unit="W") "Generated power"
end PartialWindTurbineBase;


within AixLib.Electrical;
package DC "Package for analog direct current (DC) electrical circuits"
  extends Modelica.Icons.Package;
end DC;


DC DC converter

within AixLib.Electrical.DC.Conversion;
model DCDCConverter "DC DC converter"
  extends AixLib.Electrical.Interfaces.PartialConversion(
    redeclare package PhaseSystem_p = PhaseSystems.TwoConductor,
    redeclare package PhaseSystem_n = PhaseSystems.TwoConductor,
    redeclare Interfaces.Terminal_n terminal_n,
    redeclare Interfaces.Terminal_p terminal_p);
  parameter Modelica.Units.SI.Voltage VHigh
    "DC voltage on side 1 of the transformer (primary side)";
  parameter Modelica.Units.SI.Voltage VLow
    "DC voltage on side 2 of the transformer (secondary side)";
  parameter Modelica.Units.SI.Efficiency eta(max=1) "Converter efficiency";
  parameter Boolean ground_1 = true "Connect side 1 of converter to ground"
  parameter Boolean ground_2 = true "Connect side 2 of converter to ground"
  Modelica.Units.SI.Power LossPower "Loss power";
protected
  parameter Real conversionFactor = VLow/VHigh
    "Ratio of high versus low voltage";
  Modelica.Units.SI.Current i1;
  Modelica.Units.SI.Current i2;
  Modelica.Units.SI.Voltage v1;
  Modelica.Units.SI.Voltage v2;
  Modelica.Units.SI.Power P_p "Power at terminal p";
  Modelica.Units.SI.Power P_n "Power at terminal n";
equation
  Connections.potentialRoot(terminal_n.theta);
  Connections.potentialRoot(terminal_p.theta);

  if not ground_1 then
    i1 = 0;
  else
    v1 = 0;
  end if;
  if not ground_2 then
    i2 = 0;
  else
    v2 = 0;
  end if;

  P_p = PhaseSystem_p.activePower(terminal_p.v, terminal_p.i);
  P_n = PhaseSystem_n.activePower(terminal_n.v, terminal_n.i);

  v1 = terminal_n.v[2];
  v2 = terminal_p.v[2];
  sum(terminal_n.i) + i1 = 0;
  sum(terminal_p.i) + i2 = 0;

  // Voltage relation
  v_p = v_n*conversionFactor;

  // OLD equations that take into account the power at the secondary
  // power balance
  // LossPower = (1-eta) * abs(P_p);
  // P_n + P_p - LossPower = 0;

  // Symmetric and linear version
  LossPower = P_p + P_n;
  if i_n >=0 then
    i_p = i_n/conversionFactor/(eta - 2);
  else
    i_n = conversionFactor*i_p/(eta - 2);
  end if;
end DCDCConverter;


within AixLib.Electrical.DC;
package Conversion "Package with models for DC/DC conversion"
  extends Modelica.Icons.Package;
end Conversion;


Test model DC to DC converter. This model illustrates the use of a model that converts between DC voltages.

within AixLib.Electrical.DC.Conversion.Examples;
model DCDCConverter "Test model DC to DC converter"
  extends Modelica.Icons.Example;
  AixLib.Electrical.DC.Loads.Conductor resistor(
    mode=AixLib.Electrical.Types.Load.FixedZ_steady_state,
    P_nominal=-2000,
    V_nominal=60) "Resistive load"
  AixLib.Electrical.DC.Sources.ConstantVoltage sou(V=120)
    "Voltage source"
  AixLib.Electrical.DC.Conversion.DCDCConverter conDCDC(
    VHigh=120,
    VLow=60,
    eta=0.9,
    i_n(start=0)) "DC/DC transformer"
  AixLib.Electrical.DC.Loads.Conductor conductor(mode=AixLib.Electrical.Types.Load.VariableZ_P_input,
      V_nominal=60,
    P_nominal=10e3) "Variable resistive load"
  Modelica.Blocks.Sources.Ramp varLoad_P(
    duration=0.5,
    startTime=0.3,
    offset=-1000,
    height=10000)
  AixLib.Electrical.DC.Sensors.GeneralizedSensor sen "Power sensor"
equation
  connect(varLoad_P.y, conductor.Pow)
  connect(conDCDC.terminal_p, resistor.terminal)
  connect(conDCDC.terminal_p, conductor.terminal)
  connect(sou.terminal, sen.terminal_n)
  connect(sen.terminal_p, conDCDC.terminal_n)
end DCDCConverter;


Package with example models

within AixLib.Electrical.DC.Conversion;
package Examples "Package with example models"
  extends Modelica.Icons.ExamplesPackage;
end Examples;


within AixLib.Electrical.DC;
package Interfaces "Package with interfaces for DC electrical systems"
  extends Modelica.Icons.InterfacesPackage;
end Interfaces;


Terminal n for DC electrical systems

within AixLib.Electrical.DC.Interfaces;
connector Terminal_n "Terminal n for DC electrical systems"
  extends AixLib.Electrical.Interfaces.Terminal(
    redeclare package PhaseSystem =
        AixLib.Electrical.PhaseSystems.TwoConductor);
end Terminal_n;


Terminal p for DC electrical systems

within AixLib.Electrical.DC.Interfaces;
connector Terminal_p "Terminal p for DC electrical systems"
  extends AixLib.Electrical.Interfaces.Terminal(
    redeclare package PhaseSystem =
        AixLib.Electrical.PhaseSystems.TwoConductor);
end Terminal_p;


Model of a DC electrical line

within AixLib.Electrical.DC.Lines;
model Line "Model of a DC electrical line"
  extends AixLib.Electrical.Transmission.BaseClasses.PartialLine(
    redeclare package PhaseSystem_p = PhaseSystems.TwoConductor,
    redeclare package PhaseSystem_n = PhaseSystems.TwoConductor,
    redeclare Interfaces.Terminal_n terminal_n,
    redeclare Interfaces.Terminal_p terminal_p,
    final modelMode=Types.Load.FixedZ_steady_state,
    commercialCable = AixLib.Electrical.Transmission.Functions.selectCable_low(P_nominal, V_nominal));

  TwoPortRCLine lineRC(
    final useHeatPort=true,
    final R=R,
    final V_nominal=V_nominal,
    final T_ref=T_ref,
    final M=M,
    final C=C,
    final use_C=use_C)
equation
  connect(terminal_n, lineRC.terminal_n)
  connect(lineRC.terminal_p, terminal_p)
  connect(cableTemp.port, lineRC.heatPort)
end Line;


within AixLib.Electrical.DC;
package Lines "Package with models for DC electrical lines"
  extends Modelica.Icons.Package;
end Lines;


within AixLib.Electrical.DC.Lines;
model TwoPortRCLine "Model of a two port DC resistance and capacity (T-model)"
  extends AixLib.Electrical.Transmission.BaseClasses.PartialTwoPortRLC(
    redeclare package PhaseSystem_p = PhaseSystems.TwoConductor,
    redeclare package PhaseSystem_n = PhaseSystems.TwoConductor,
    redeclare Interfaces.Terminal_n terminal_n,
    redeclare Interfaces.Terminal_p terminal_p,
    final L=0);
  parameter Boolean use_C = false
    "Set to true to add a capacitance in the center of the line"
  parameter Modelica.Units.SI.Voltage Vc_start=V_nominal
    "Initial value of the voltage of the capacitance in the middle of the line";
  Modelica.Units.SI.Voltage Vc(start=Vc_start, stateSelect=StateSelect.prefer)
    "Voltage of the capacitor";
initial equation
  if C>0 and use_C then
    Vc = Vc_start;
  end if;
equation
  terminal_p.v[1] - (Vc+terminal_p.v[2]) = terminal_p.i[1]*R_actual/2;
  terminal_n.v[1] - (Vc+terminal_p.v[2]) = terminal_n.i[1]*R_actual/2;

  if C>0 and use_C then
    C*der(Vc) = terminal_p.i[1] + terminal_n.i[1];
  else
    Vc = 0.5*(terminal_p.v[1] - terminal_p.v[2] + terminal_n.v[1] - terminal_n.v[2]);
  end if;

  terminal_p.v[2] = terminal_n.v[2];
  terminal_p.i[2] + terminal_n.i[2] = 0;

  // Joule losses
  LossPower = R_actual/2*terminal_p.i[1]^2 + R_actual/2*terminal_n.i[1]^2;
end TwoPortRCLine;


Model of a two port DC resistance

within AixLib.Electrical.DC.Lines;
model TwoPortResistance "Model of a two port DC resistance"
  extends
    AixLib.Electrical.Transmission.BaseClasses.PartialTwoPortResistance(
    redeclare package PhaseSystem_p = PhaseSystems.TwoConductor,
    redeclare package PhaseSystem_n = PhaseSystems.TwoConductor,
    redeclare Interfaces.Terminal_n terminal_n,
    redeclare Interfaces.Terminal_p terminal_p);
equation
  // Voltage drop on the resistance lumped on connection between terminals
  // p.v[1] and n.v[1]
  terminal_p.v[1] - terminal_n.v[1] = terminal_p.i[1]*R_actual;
  terminal_p.v[2] = terminal_n.v[2];

  // Joule losses
  LossPower = R_actual*terminal_p.i[1]^2;
end TwoPortResistance;


Example model to test the DC lines. This model is a simple test case that show how to use a line model
and parametrize it using a commercial cable.

within AixLib.Electrical.DC.Lines.Examples;
model DCLine "Example model to test the DC lines"
  extends Modelica.Icons.Example;
  Line line(
    P_nominal=500,
    V_nominal=50,
    mode=Types.CableMode.commercial,
    commercialCable=Transmission.LowVoltageCables.PvcAl16(),
    l=100) "Transmission line"
  Sources.ConstantVoltage E(V=50) "Voltage source"
  Line line1(
    P_nominal=500,
    V_nominal=50,
    mode=Types.CableMode.commercial,
    commercialCable=Transmission.LowVoltageCables.PvcAl16(),
    l=100) "Transmission line"
  Line line2(
    P_nominal=500,
    V_nominal=50,
    mode=Types.CableMode.commercial,
    commercialCable=Transmission.LowVoltageCables.PvcAl16(),
    l=100) "Transmission line"
  Modelica.Electrical.Analog.Basic.Ground ground
  Loads.Conductor load1(mode=Types.Load.VariableZ_y_input,
    V_nominal=50,
    linearized=false,
    P_nominal=-50) "Variable load"
  Loads.Conductor load2(
    V_nominal=50,
    linearized=false,
    P_nominal=-150) "Load"
  Loads.Conductor load3(
    V_nominal=50,
    linearized=false,
    P_nominal=-200) "Load"
  Modelica.Blocks.Sources.Ramp varLoad(
    height=0.8,
    duration=0.5,
    offset=0.2,
    startTime=0.3) "Load consumption profile"
equation
  connect(E.terminal, line.terminal_n)
  connect(E.terminal, line1.terminal_n)
  connect(E.terminal, line2.terminal_n)
  connect(load1.terminal, line.terminal_p)
  connect(line1.terminal_p, load2.terminal)
  connect(line2.terminal_p, load3.terminal)
  connect(varLoad.y, load1.y)
  connect(E.n, ground.p)
end DCLine;


Example model to test the possible combinations between line and load models. This model shows a DC grid with 10 loads and 16 cables.
Each cable is of length l = 10 meters, a parameter that can be modified.
Each load can be either be a full nonlinear model, or be replaced by the
linearized version. The parameter linearLoads = false
can be used to switch between linear and nonlinear implementation.

within AixLib.Electrical.DC.Lines.Examples;
model DCLines
  "Example model to test the possible combinations between line and load models"
  extends Modelica.Icons.Example;
  parameter Boolean linearLoads = false
    "Flag that selects between linearized or nonlinear load models";
  parameter Real L = 10 "Length of each cable";
  Modelica.Units.SI.Power Sloads=load1.S[1] + load2.S[1] + load3.S[1] + load4.S[
      1] + load5.S[1] + load6.S[1] + load7.S[1] + load8.S[1] + load9.S[1] +
      load10.S[1] "Sum of the power consumed by the loads";
  Line line(
    P_nominal=500,
    V_nominal=50,
    mode=Types.CableMode.commercial,
    commercialCable=Transmission.LowVoltageCables.PvcAl16(),
    l=L,
    terminal_n(v(each start=0))) "Transmission line"
  Sources.ConstantVoltage E(V=50) "Voltage source"
  Line line1(
    P_nominal=500,
    V_nominal=50,
    mode=Types.CableMode.commercial,
    commercialCable=Transmission.LowVoltageCables.PvcAl16(),
    l=L) "Transmission line"

  Line line2(
    P_nominal=500,
    V_nominal=50,
    mode=Types.CableMode.commercial,
    commercialCable=Transmission.LowVoltageCables.PvcAl16(),
    l=L) "Transmission line"

  Modelica.Electrical.Analog.Basic.Ground ground
  Loads.Conductor load1(mode=Types.Load.VariableZ_y_input,
    V_nominal=50,
    linearized=linearLoads,
    P_nominal=-150) "Load"
  Loads.Conductor load2(
    V_nominal=50,
    mode=Types.Load.VariableZ_y_input,
    linearized=linearLoads,
    P_nominal=-120) "Load"
  Loads.Conductor load3(
    V_nominal=50,
    mode=Types.Load.VariableZ_y_input,
    linearized=linearLoads,
    P_nominal=-200) "Load"
  Modelica.Blocks.Sources.Trapezoid
                               varLoad1(
    offset=0.4,
    amplitude=0.6,
    rising=600,
    width=1000,
    falling=800,
    period=3600,
    startTime=1800) "Power consumption profile"
  Loads.Conductor load4(
    V_nominal=50,
    mode=Types.Load.VariableZ_y_input,
    linearized=linearLoads,
    P_nominal=-120) "Load"
  Line line3(
    P_nominal=500,
    V_nominal=50,
    mode=Types.CableMode.commercial,
    commercialCable=Transmission.LowVoltageCables.PvcAl16(),
    l=L) "Transmission line"

  Line line4(
    P_nominal=500,
    V_nominal=50,
    mode=Types.CableMode.commercial,
    commercialCable=Transmission.LowVoltageCables.PvcAl16(),
    l=L) "Transmission line"

  Line line5(
    P_nominal=500,
    V_nominal=50,
    mode=Types.CableMode.commercial,
    commercialCable=Transmission.LowVoltageCables.PvcAl16(),
    l=L) "Transmission line"

  Line line0(
    P_nominal=500,
    V_nominal=50,
    mode=Types.CableMode.commercial,
    commercialCable=Transmission.LowVoltageCables.PvcAl16(),
    l=L) "Transmission line"

  Modelica.Blocks.Sources.Trapezoid
                               varLoad2(
    startTime=1800,
    amplitude=0.8,
    rising=400,
    width=1300,
    falling=900,
    period=4000,
    offset=0.1) "Power consumption profile"
  Line line6(
    P_nominal=500,
    V_nominal=50,
    mode=Types.CableMode.commercial,
    commercialCable=Transmission.LowVoltageCables.PvcAl16(),
    l=L) "Transmission line"

  Loads.Conductor load5(
    V_nominal=50,
    mode=Types.Load.VariableZ_y_input,
    linearized=linearLoads,
    P_nominal=-200) "Load"
  Modelica.Blocks.Sources.Trapezoid
                               varLoad3(
    amplitude=0.7,
    rising=660,
    width=900,
    falling=300,
    period=3700,
    offset=0.3,
    startTime=200) "Power consumption profile"
  Loads.Conductor load6(
    V_nominal=50,
    mode=Types.Load.VariableZ_y_input,
    linearized=linearLoads,
    P_nominal=-120) "Load"
  Line line7(
    P_nominal=500,
    V_nominal=50,
    mode=Types.CableMode.commercial,
    commercialCable=Transmission.LowVoltageCables.PvcAl16(),
    l=L) "Transmission line"

  Line line8(
    P_nominal=500,
    V_nominal=50,
    mode=Types.CableMode.commercial,
    commercialCable=Transmission.LowVoltageCables.PvcAl16(),
    l=L) "Transmission line"

  Line line9(
    P_nominal=500,
    V_nominal=50,
    mode=Types.CableMode.commercial,
    commercialCable=Transmission.LowVoltageCables.PvcAl16(),
    l=L) "Transmission line"

  Loads.Conductor load7(
    V_nominal=50,
    mode=Types.Load.VariableZ_y_input,
    linearized=linearLoads,
    P_nominal=-200) "Load"
  Modelica.Blocks.Sources.Trapezoid
                               varLoad4(
    rising=600,
    width=1000,
    falling=800,
    period=3600,
    amplitude=0.1,
    offset=0.8,
    startTime=3300) "Power consumption profile"
  Loads.Conductor load8(
    V_nominal=50,
    mode=Types.Load.VariableZ_y_input,
    linearized=linearLoads,
    P_nominal=-120) "Load"
  Line line10(
    P_nominal=500,
    V_nominal=50,
    mode=Types.CableMode.commercial,
    commercialCable=Transmission.LowVoltageCables.PvcAl16(),
    l=L) "Transmission line"

  Line line11(
    P_nominal=500,
    V_nominal=50,
    mode=Types.CableMode.commercial,
    commercialCable=Transmission.LowVoltageCables.PvcAl16(),
    l=L) "Transmission line"

  Line line12(
    P_nominal=500,
    V_nominal=50,
    mode=Types.CableMode.commercial,
    commercialCable=Transmission.LowVoltageCables.PvcAl16(),
    l=L) "Transmission line"

  Loads.Conductor load9(
    V_nominal=50,
    mode=Types.Load.VariableZ_y_input,
    linearized=linearLoads,
    P_nominal=-200) "Load"
  Modelica.Blocks.Sources.Trapezoid
                               varLoad5(
    falling=800,
    amplitude=0.5,
    rising=800,
    width=800,
    period=3000,
    offset=0.5,
    startTime=0) "Power consumption profile"
  Loads.Conductor load10(
    V_nominal=50,
    mode=Types.Load.VariableZ_y_input,
    linearized=linearLoads,
    P_nominal=-120) "Load"
  Line line13(
    P_nominal=500,
    V_nominal=50,
    mode=Types.CableMode.commercial,
    commercialCable=Transmission.LowVoltageCables.PvcAl16(),
    l=L) "Transmission line"

  Line line14(
    P_nominal=500,
    V_nominal=50,
    mode=Types.CableMode.commercial,
    commercialCable=Transmission.LowVoltageCables.PvcAl16(),
    l=100) "Transmission line"

equation
  connect(load1.terminal, line.terminal_p)
  connect(line1.terminal_p, line.terminal_n)
  connect(line2.terminal_p, line3.terminal_n)
  connect(line3.terminal_p, load3.terminal)
  connect(line1.terminal_p, line4.terminal_n)
  connect(line4.terminal_p, load2.terminal)
  connect(load4.terminal, line5.terminal_p)
  connect(line5.terminal_n, line3.terminal_n)
  connect(E.terminal, line0.terminal_n)
  connect(line0.terminal_p, line1.terminal_n)
  connect(varLoad1.y, load3.y)
  connect(varLoad1.y, load4.y)
  connect(varLoad2.y, load1.y)
  connect(varLoad2.y, load2.y)
  connect(line6.terminal_p,line7. terminal_n)
  connect(line7.terminal_p,load5. terminal)
  connect(load6.terminal,line8. terminal_p)
  connect(line8.terminal_n,line7. terminal_n)
  connect(varLoad3.y,load5. y)
  connect(varLoad3.y,load6. y)
  connect(line9.terminal_p, line10.terminal_n)
  connect(line10.terminal_p, load7.terminal)
  connect(load8.terminal, line11.terminal_p)
  connect(line11.terminal_n, line10.terminal_n)
  connect(varLoad4.y,load7. y)
  connect(varLoad4.y,load8. y)
  connect(line12.terminal_p, line13.terminal_n)
  connect(line13.terminal_p, load9.terminal)
  connect(load10.terminal, line14.terminal_p)
  connect(line14.terminal_n, line13.terminal_n)
  connect(varLoad5.y,load9. y)
  connect(varLoad5.y, load10.y)
  connect(line2.terminal_n, line4.terminal_n)
  connect(line6.terminal_n, line3.terminal_n)
  connect(line9.terminal_n, line7.terminal_n)
  connect(line12.terminal_n, line10.terminal_n)
  connect(E.n, ground.p)
end DCLines;


Example model to test the possible combinations between line and load models. This model is the linearized version of the model

AixLib.Electrical.DC.Lines.Examples.DCLines and
can be used to test how the linearized loads are affected by the voltage drop
caused by the lines. The longer the distance between the load and the source,
the bigger is the voltage drop and thus the error introduced by the linearization.

within AixLib.Electrical.DC.Lines.Examples;
model DCLinesLinearized
  "Example model to test the possible combinations between line and load models"
  extends AixLib.Electrical.DC.Lines.Examples.DCLines(linearLoads = true);
end DCLinesLinearized;


Package with example models

within AixLib.Electrical.DC.Lines;
package Examples "Package with example models"
extends Modelica.Icons.ExamplesPackage;
end Examples;


Example model to test for the DC RC two port model. This model shows how to use a two port resistance-capacitance model.
The example also shows a comparison between the dynamic and steady state version model
that can be selected by changing the boolean flag use_C.

within AixLib.Electrical.DC.Lines.Examples;
model RCModel "Example model to test for the DC RC two port model"
  extends Modelica.Icons.Example;

  TwoPortRCLine RC_ss(
    C=1,
    V_nominal=50,
    R=8) "Line resistance"
  Sources.ConstantVoltage constantVoltage(V=50) "Voltage source"
  Modelica.Electrical.Analog.Basic.Ground ground
  Loads.Resistor sc_ss(V_nominal=50, R=0) "Short circuit load"
  Sensors.GeneralizedSensor sen_ss "Sensor"
  TwoPortRCLine RC_dyn(
    C=1,
    V_nominal=50,
    use_C=true,
    R=8) "Line resistance"
  Loads.Resistor sc_dyn(V_nominal=50, R=0) "Load"
  Sensors.GeneralizedSensor sen_dyn "Sensor"
equation
  connect(ground.p, constantVoltage.n)
  connect(constantVoltage.terminal, RC_ss.terminal_n)
  connect(RC_ss.terminal_p, sen_ss.terminal_n)
  connect(sen_ss.terminal_p, sc_ss.terminal)
  connect(RC_dyn.terminal_p, sen_dyn.terminal_n)
  connect(sen_dyn.terminal_p, sc_dyn.terminal)
  connect(constantVoltage.terminal, RC_dyn.terminal_n)
end RCModel;


Example model to test for the DC resistance two port model. This model shows how to use a two port resistance.
In this example the resistance connects an ideal constant voltage source with
a short circuit. The current flowing through the circuit depends just
on the value of the two port resistance.

within AixLib.Electrical.DC.Lines.Examples;
model Resistance "Example model to test for the DC resistance two port model"
  extends Modelica.Icons.Example;

  TwoPortResistance lineR(R=10) "Line resistance"
  Sources.ConstantVoltage constantVoltage(V=50) "Voltage source"
  Modelica.Electrical.Analog.Basic.Ground ground
  Loads.Resistor short_circuit(V_nominal=50, R=0) "Short circuit load"
  Sensors.GeneralizedSensor sen "Power sensor"
equation
  connect(ground.p, constantVoltage.n)
  connect(constantVoltage.terminal, lineR.terminal_n)
  connect(lineR.terminal_p, sen.terminal_n)
  connect(sen.terminal_p, short_circuit.terminal)
end Resistance;


Model of a generic DC load

within AixLib.Electrical.DC.Loads;
model Conductor "Model of a generic DC load"
    extends AixLib.Electrical.Interfaces.ResistiveLoad(
     redeclare package PhaseSystem = PhaseSystems.TwoConductor,
     redeclare Interfaces.Terminal_n terminal);
protected
  Modelica.Units.SI.Voltage absDV
    "Absolute value of the voltage difference between the two conductors (used by the linearized model)";
equation

  absDV = abs(terminal.v[1]-terminal.v[2]);

  if linearized then

    // Linearized version of the model
    if absDV <= (8/9)*V_nominal then
      terminal.i[1] + P*(2/(0.8*V_nominal) - (terminal.v[1]-terminal.v[2])/(0.8*V_nominal)^2) = 0;
    elseif absDV >= (12/11)*V_nominal then
      terminal.i[1] + P*(2/(1.2*V_nominal) - (terminal.v[1]-terminal.v[2])/(1.2*V_nominal)^2) = 0;
    else
      terminal.i[1] + P*(2/V_nominal - (terminal.v[1]-terminal.v[2])/V_nominal^2) = 0;
    end if;

  else
    // Full nonlinear version of the model
    // PhaseSystem.activePower(terminal.v, terminal.i) + P = 0;
    if initMode == AixLib.Electrical.Types.InitMode.zero_current then
      i[1] = - homotopy(actual= P/(v[1] - v[2]),  simplified= 0);
    else
      i[1] = - homotopy(actual= P/(v[1] - v[2]),  simplified= P*(2/V_nominal - (v[1]-v[2])/V_nominal^2));
    end if;

  end if;

  // Since the connector is a two conductor, the sum of the currents at the terminal
  // is null
  sum(i) = 0;
end Conductor;


within AixLib.Electrical.DC;
package Loads "Package with models for DC electrical loads"
  extends Modelica.Icons.VariantsPackage;
end Loads;


Ideal linear electrical resistor

within AixLib.Electrical.DC.Loads;
model Resistor "Ideal linear electrical resistor"
  extends AixLib.Electrical.Interfaces.ResistiveLoad(
    redeclare package PhaseSystem = PhaseSystems.TwoConductor,
    redeclare Interfaces.Terminal_n terminal,
    final mode=AixLib.Electrical.Types.Load.FixedZ_steady_state,
    final P_nominal=V_nominal^2/max(R, Modelica.Constants.small));
  extends Modelica.Electrical.Analog.Interfaces.ConditionalHeatPort(T = T_ref);
  parameter Modelica.Units.SI.Resistance R(start=1)
    "Resistance at temperature T_ref";
  parameter Modelica.Units.SI.Temperature T_ref=300.15 "Reference temperature";
  parameter Modelica.Units.SI.LinearTemperatureCoefficient alpha=0
    "Temperature coefficient of resistance (R_actual = R*(1 + alpha*(T_heatPort - T_ref))";
  Modelica.Units.SI.Resistance R_actual
    "Actual resistance = R*(1 + alpha*(T_heatPort - T_ref))";
equation
  assert((1 + alpha*(T_heatPort - T_ref)) >= Modelica.Constants.eps, "Temperature outside of scope of model");
  R_actual = R*(1 + alpha*(T_heatPort - T_ref));
  PhaseSystem.systemVoltage(v) = R_actual*PhaseSystem.systemCurrent(i);
  LossPower = PhaseSystem.activePower(v,i);
  sum(i) = 0;
end Resistor;


Example model to check the linearized load model. This example demonstrates the use of a linealized load model AixLib.Electrical.DC.Loads.Conductor.

within AixLib.Electrical.DC.Loads.Examples;
model LinearizedLoad "Example model to check the linearized load model"
  extends Modelica.Icons.Example;
  Real error = (sen_nlin.P - sen_lin.P)*100/sen_nlin.P
    "Percentage of error between the linearized and actual power consumption";
  Real deltaV = LinearLoad.V_nominal - sen_lin.V
    "Voltage distance between nominal condition and actual voltage";
  AixLib.Electrical.DC.Loads.Conductor NonlinearLoad(
    linearized=false,
    mode=AixLib.Electrical.Types.Load.VariableZ_P_input,
    V_nominal=100,
    P_nominal=0) "Nonlinear load model"
  Sources.ConstantVoltage sou(V=100) "Voltage source"
  Modelica.Electrical.Analog.Basic.Ground gro "Ground"
  Lines.TwoPortResistance Rline1(R=2) "Line resistance"
  Sensors.GeneralizedSensor sen_nlin "Sensor"
  AixLib.Electrical.DC.Loads.Conductor LinearLoad(
    mode=AixLib.Electrical.Types.Load.VariableZ_P_input,
    V_nominal=100,
    linearized=true,
    P_nominal=0) "Linearized load model"
  Sensors.GeneralizedSensor sen_lin "Sensor"
  Lines.TwoPortResistance Rline2(R=2) "Line resistance"
  Modelica.Blocks.Sources.Ramp ramp(
    duration=0.5,
    startTime=0.2,
    offset=-50,
    height=-950) "Power consumption"
equation
  connect(sou.terminal, Rline1.terminal_n)
  connect(Rline1.terminal_p, sen_nlin.terminal_n)
  connect(sen_nlin.terminal_p, NonlinearLoad.terminal)
  connect(LinearLoad.terminal, sen_lin.terminal_p)
  connect(sou.n, gro.p)
  connect(sou.terminal, Rline2.terminal_n)
  connect(Rline2.terminal_p, sen_lin.terminal_n)
  connect(LinearLoad.Pow, ramp.y)
  connect(NonlinearLoad.Pow, ramp.y)
end LinearizedLoad;


Package with example models

within AixLib.Electrical.DC.Loads;
package Examples "Package with example models"
extends Modelica.Icons.ExamplesPackage;
end Examples;


Example model for resistor. This example demonstrates the use of the resistor model.

within AixLib.Electrical.DC.Loads.Examples;
model Resistor "Example model for resistor"
  extends Modelica.Icons.Example;
  AixLib.Electrical.DC.Loads.Resistor res2(R=2, V_nominal=12) "Resistor"
  Sources.ConstantVoltage sou(V=12) "Voltage source"
  Modelica.Electrical.Analog.Basic.Ground gro "Ground"
  Lines.TwoPortResistance res(R=2) "Line resistance"
  Sensors.GeneralizedSensor sen "Sensor"
  AixLib.Electrical.DC.Loads.Resistor res1(R=2, V_nominal=12) "Resistor"
  Sensors.GeneralizedSensor sen1 "Sensor"
equation
  connect(sou.terminal, res.terminal_n)
  connect(res.terminal_p, sen.terminal_n)
  connect(sen.terminal_p,res2. terminal)
  connect(res1.terminal, sen1.terminal_p)
  connect(sen1.terminal_n, res.terminal_n)
  connect(sou.n, gro.p)
end Resistor;


Example using variable loads models. This example shows how to use three different types of load models.
Each load is of type
AixLib.Electrical.DC.Loads.Conductor.

within AixLib.Electrical.DC.Loads.Examples;
model VariableLoad "Example using variable loads models"
  extends Modelica.Icons.Example;
  Conductor loa1(
    V_nominal=12,
    linearized=false,
    P_nominal=-50) "Load"
  Sources.ConstantVoltage sou(V=12) "Voltage source"
  Modelica.Electrical.Analog.Basic.Ground gro "Ground"
  Conductor loa2(              mode=Types.Load.VariableZ_y_input,
    V_nominal=12,
    P_nominal=-50) "Load"
  Modelica.Blocks.Sources.Ramp varLoad_y(
    height=0.8,
    duration=0.5,
    startTime=0.3,
    offset=0) "Power signal"
  Conductor loa3(
    V_nominal=12,
    P_nominal=0,
    mode=AixLib.Electrical.Types.Load.VariableZ_P_input) "Load"
  Modelica.Blocks.Sources.Ramp varLoad_P(
    duration=0.5,
    startTime=0.3,
    height=120,
    offset=-20) "Power signal"
  Lines.TwoPortResistance res(R=0.1) "Line resistance"
  Sensors.GeneralizedSensor sen "Sensor"
equation
  connect(sou.terminal, loa2.terminal)
  connect(varLoad_y.y, loa2.y)
  connect(sou.terminal, loa3.terminal)
  connect(varLoad_P.y, loa3.Pow)
  connect(sou.terminal, res.terminal_n)
  connect(res.terminal_p, sen.terminal_n)
  connect(sen.terminal_p, loa1.terminal)
  connect(sou.n, gro.p)
end VariableLoad;


Sensor for power, voltage and current

within AixLib.Electrical.DC.Sensors;
model GeneralizedSensor "Sensor for power, voltage and current"
  extends AixLib.Electrical.Icons.GeneralizedSensor;
  extends AixLib.Electrical.Interfaces.PartialTwoPort(
    redeclare package PhaseSystem_p = PhaseSystems.TwoConductor,
    redeclare package PhaseSystem_n = PhaseSystems.TwoConductor,
    redeclare Interfaces.Terminal_n terminal_n,
    redeclare Interfaces.Terminal_p terminal_p);
  Modelica.Blocks.Interfaces.RealOutput V(final quantity="ElectricPotential",
                                          final unit="V") "Voltage"
  Modelica.Blocks.Interfaces.RealOutput I(final quantity="ElectricCurrent",
                                          final unit="A") "Current"
  Modelica.Blocks.Interfaces.RealOutput P(
    final quantity="Power",
    final unit="W") "Power"
equation
  Connections.branch(terminal_p.theta, terminal_n.theta);
  terminal_p.theta = terminal_n.theta;

  V = AixLib.Electrical.PhaseSystems.TwoConductor.systemVoltage(terminal_n.v);
  I = AixLib.Electrical.PhaseSystems.TwoConductor.systemCurrent(terminal_n.i);
  P = AixLib.Electrical.PhaseSystems.TwoConductor.activePower(v=terminal_n.v, i=terminal_n.i);

  connect(terminal_n, terminal_p)
end GeneralizedSensor;


within AixLib.Electrical.DC;
package Sensors "Package with sensors for DC electrical systems"
extends Modelica.Icons.SensorsPackage;
end Sensors;


Example model for generalized sensor. This example illustrates the use of the generalized sensor.

within AixLib.Electrical.DC.Sensors.Examples;
model GeneralizedSensor "Example model for generalized sensor"
  extends Modelica.Icons.Example;
  AixLib.Electrical.DC.Sensors.GeneralizedSensor sen "Power sensor"
  AixLib.Electrical.DC.Loads.Conductor loa(V_nominal=120, P_nominal=120)
    "Constant load"
  AixLib.Electrical.DC.Sources.ConstantVoltage sou(V=120) "Voltage source"
  Modelica.Electrical.Analog.Basic.Ground ground
equation
  connect(sen.terminal_p, loa.terminal)
  connect(sen.terminal_n, sou.terminal)
  connect(sou.n, ground.p)
end GeneralizedSensor;


within AixLib.Electrical.DC.Sensors;
package Examples "Collection of models that illustrate model use and test models"
  extends Modelica.Icons.ExamplesPackage;
end Examples;


Model of a constant DC voltage source

within AixLib.Electrical.DC.Sources;
model ConstantVoltage "Model of a constant DC voltage source"
  extends AixLib.Electrical.Interfaces.Source(
    redeclare package PhaseSystem = PhaseSystems.TwoConductor,
    redeclare Interfaces.Terminal_p terminal,
    final potentialReference=true,
    final definiteReference=false);
  parameter Modelica.Units.SI.Voltage V(start=1) "Value of constant voltage";
  Modelica.Electrical.Analog.Interfaces.NegativePin n "Negative pin"
equation
  terminal.v[1] = V;
  terminal.v[2] = n.v;
  sum(terminal.i) + n.i = 0;
end ConstantVoltage;


Package with models for DC sources

within AixLib.Electrical.DC;
package Sources "Package with models for DC sources"
  extends Modelica.Icons.SourcesPackage;
end Sources;


Model of a generoic DC voltage source

within AixLib.Electrical.DC.Sources;
model VoltageSource "Model of a generoic DC voltage source"
  extends AixLib.Electrical.Interfaces.VariableVoltageSource(
    V(start = 1),
    redeclare package PhaseSystem = PhaseSystems.TwoConductor,
    redeclare Interfaces.Terminal_p terminal,
    final potentialReference=true,
    final definiteReference=false);
  Modelica.Electrical.Analog.Interfaces.NegativePin n "Negative pin"
equation
  terminal.v[1] = V_in_internal;
  terminal.v[2] = n.v;
  sum(terminal.i) + n.i = 0;
end VoltageSource;


within AixLib.Electrical.DC.Sources;
package BaseClasses "Package with base classes for DC sources"
  extends Modelica.Icons.BasesPackage;
end BaseClasses;


Block for wind correction. This model calculates the wind velocity at the location as a function of the height over ground.
The equation is based on Gash (1991).

The model computes the wind velocity <i>vLoc</i> as

within AixLib.Electrical.DC.Sources.BaseClasses;
block WindCorrection "Block for wind correction"
  extends Modelica.Blocks.Icons.Block;
  parameter Modelica.Units.SI.Height h "Height over ground";
  parameter Modelica.Units.SI.Height hRef
    "Reference height for wind measurement";
  parameter Real n(min=0) = 0.4 "Height exponent for wind profile calculation";
  Modelica.Blocks.Interfaces.RealOutput vLoc( unit="m/s")
    "Wind velocity at the location"
  Modelica.Blocks.Interfaces.RealInput vRef(unit="m/s")
    "Wind velocity at the reference height"
equation
  vLoc=vRef * (h / hRef)^n;
end WindCorrection;


Package with example models

within AixLib.Electrical.DC.Sources;
package Examples "Package with example models"
  extends Modelica.Icons.ExamplesPackage;
end Examples;


Example for the variable voltage source model. This model illustrates the use of the variable voltage source model.

within AixLib.Electrical.DC.Sources.Examples;
model VoltageSource "Example for the variable voltage source model"
  extends Modelica.Icons.Example;
  Modelica.Electrical.Analog.Basic.Ground ground
  AixLib.Electrical.DC.Loads.Resistor res(R=0.5, V_nominal=12)
    "Resistance"
  AixLib.Electrical.DC.Sources.VoltageSource sou "Voltage source"
  AixLib.Electrical.DC.Lines.TwoPortResistance lin(R=0.5)
    "Transmission line"
  AixLib.Electrical.DC.Sensors.GeneralizedSensor sen "Sensor"
  Modelica.Blocks.Sources.Sine cosine(
    phase=0,
    f=1,
    offset=12,
    amplitude=3) "Variable voltage signal"
equation
  connect(lin.terminal_p, sen.terminal_n)
  connect(sou.n, ground.p)
  connect(sen.terminal_p, res.terminal)
  connect(sou.terminal, lin.terminal_n)
  connect(cosine.y, sou.V_in)
end VoltageSource;


within AixLib.Electrical;
package Icons "This package contains icons used by the electric models"
  extends Modelica.Icons.IconsPackage;
end Icons;


Icon that represents if the angle symble should be displayed or not. This is the icon that conditionally draws the angle symbol for a
conversion model (e.g., a transformer).

within AixLib.Electrical.Icons;
partial model RefAngleConversion
  "Icon that represents if the angle symble should be displayed or not"
end RefAngleConversion;


Partial model of a capacitive load. This is a model of a generic capacitive load. This model is an extension of the base load model
AixLib.Electrical.Interfaces.Load.

within AixLib.Electrical.Interfaces;
partial model CapacitiveLoad "Partial model of a capacitive load"
  extends Load;
  parameter Boolean use_pf_in = false "If true, the power factor is defined by an input"
  parameter Real pf(min=0, max=1) = 0.8 "Power factor"
  Modelica.Blocks.Interfaces.RealInput pf_in(
    min=0,
    max=1,
    unit="1") if (use_pf_in) "Power factor"
protected
  function j = PhaseSystem.j "J operator that rotates of 90 degrees";
  Modelica.Blocks.Interfaces.RealInput pf_internal
    "Hidden value of the input load for the conditional connector";
  Modelica.Units.SI.ElectricCharge q[2](each stateSelect=StateSelect.prefer)
    "Electric charge";
  Modelica.Units.SI.Admittance[2] Y "Admittance";
  Modelica.Units.SI.AngularVelocity omega "Angular velocity";
  Modelica.Units.SI.Power Q=P*tan(-acos(pf_internal))
    "Reactive power (negative because capacitive load)";
equation
  connect(pf_in, pf_internal);

  if not use_pf_in then
    pf_internal = pf;
  end if;
end CapacitiveLoad;


within AixLib.Electrical.Interfaces;
model Ground "Generalized model of a ground connection."
  replaceable package PhaseSystem =
      AixLib.Electrical.PhaseSystems.PartialPhaseSystem constrainedby
    AixLib.Electrical.PhaseSystems.PartialPhaseSystem "Phase system"

  replaceable AixLib.Electrical.Interfaces.Terminal terminal(
    redeclare package PhaseSystem = PhaseSystem) "Generalized terminal"
equation
  terminal.v = zeros(PhaseSystem.n);
end Ground;


Partial model representing a generalized impedance. This model represents a generalized interface for an impedance.

within AixLib.Electrical.Interfaces;
model Impedance "Partial model representing a generalized impedance"
  extends AixLib.Electrical.Interfaces.Load(
    final linearized = false,
    final mode=AixLib.Electrical.Types.Load.FixedZ_steady_state,
    final P_nominal(fixed = true)=0,
    final V_nominal(fixed = true)=1);
  parameter Boolean inductive=true
    "If true, the load is inductive, otherwise it is capacitive"
  parameter Modelica.Units.SI.Resistance R(
    start=1,
    min=0) = 1 "Resistance"
  parameter Modelica.Units.SI.Inductance L(
    start=0,
    min=0) = 0 "Inductance"
  parameter Modelica.Units.SI.Capacitance C(
    start=0,
    min=0) = 0 "Capacitance"
  parameter Boolean use_R_in = false "If true, R is specified by an input"
  parameter Modelica.Units.SI.Resistance RMin(
    start=R,
    min=Modelica.Constants.eps) = 1e-4 "Minimum value of the resistance"
  parameter Modelica.Units.SI.Resistance RMax(
    start=R,
    min=Modelica.Constants.eps) = 1e2 "Maximum value of the resistance"
  parameter Boolean use_C_in = false "If true, C is specified by an input"
  parameter Modelica.Units.SI.Capacitance CMin(
    start=C,
    min=Modelica.Constants.eps) = 1e-4 "Minimum value of the capacitance"
  parameter Modelica.Units.SI.Capacitance CMax(
    start=C,
    min=Modelica.Constants.eps) = 1e2 "Maximum value of the capacitance"
  parameter Boolean use_L_in = false "If true, L is specified by an input"
  parameter Modelica.Units.SI.Inductance LMin(
    start=L,
    min=Modelica.Constants.eps) = 1e-4 "Minimum value of the inductance"
  parameter Modelica.Units.SI.Inductance LMax(
    start=L,
    min=Modelica.Constants.eps) = 1e2 "Maximum value of the inductance"
  Modelica.Blocks.Interfaces.RealInput y_R(min=0, max=1) if use_R_in
    "Input that sepecify variable R"
  Modelica.Blocks.Interfaces.RealInput y_C(min=0, max=1) if use_C_in
    "Input that sepecify variable C"
  Modelica.Blocks.Interfaces.RealInput y_L(min=0, max=1) if use_L_in
    "Input that sepecify variable L"
protected
  Modelica.Blocks.Interfaces.RealOutput y_R_internal
    "Internal signal used to compute the variable R_internal";
  Modelica.Blocks.Interfaces.RealOutput y_C_internal
    "Internal signal used to compute the variable C_internal";
  Modelica.Blocks.Interfaces.RealOutput y_L_internal
    "Internal signal used to compute the variable L_internal";
  Modelica.Units.SI.Resistance R_internal
    "Actual resistance used to compute the impedance";
  Modelica.Units.SI.Inductance L_internal
    "Actual inductance used to compute the impedance";
  Modelica.Units.SI.Capacitance C_internal
    "Actual capacitance used to compute the impedance";
equation
  // These assertions ensures that if the variable R, L or C is computed using the inputs
  // the parameters min and max are sorted
  assert((not use_R_in) or RMin < RMax, "The value of RMin has to be lower than RMax");
  assert((not use_L_in) or LMin < LMax, "The value of Lmin has to be lower than Lmax");
  assert((not use_C_in) or CMin < CMax, "The value of Cmin has to be lower than Cmax");

  // Connections to internal connectors
  connect(y_R, y_R_internal);
  connect(y_C, y_C_internal);
  connect(y_L, y_L_internal);

  // Default assignment when connectors are conditionally removed
  if not use_R_in then
    y_R_internal = 0;
  end if;

  if not use_C_in then
    y_C_internal = 0;
  end if;

  if not use_L_in then
    y_L_internal = 0;
  end if;

  // Retrieve the value of the R,L,C either if fixed or
  // varying
  if not use_R_in then
    R_internal = R;
  else
    R_internal = RMin + y_R_internal*(RMax - RMin);
  end if;

  if not use_C_in then
    C_internal = C;
  else
    C_internal = CMin + y_C_internal*(CMax - CMin);
  end if;

  if not use_L_in then
    L_internal = L;
  else
    L_internal = LMin + y_L_internal*(LMax - LMin);
  end if;
end Impedance;


Partial model of an inductive load. This is a model of a generic inductive load. This model is an extension of the base load model

AixLib.Electrical.Interfaces.Load.

within AixLib.Electrical.Interfaces;
partial model InductiveLoad "Partial model of an inductive load"
  extends Load;
  parameter Boolean use_pf_in = false "If true, the power factor is defined by an input"
  parameter Real pf(min=0, max=1) = 0.8 "Power factor"
  Modelica.Blocks.Interfaces.RealInput pf_in(
    min=0,
    max=1,
    unit="1") if (use_pf_in) "Power factor"
protected
  function j = PhaseSystem.j "J operator that rotates of 90 degrees";
  Modelica.Blocks.Interfaces.RealInput pf_internal
    "Hidden value of the input load for the conditional connector";
  Modelica.Units.SI.MagneticFlux psi[2](each stateSelect=StateSelect.prefer)
    "Magnetic flux";
  Modelica.Units.SI.Impedance Z[2] "Impedance of the load";
  Modelica.Units.SI.AngularVelocity omega "Angular frequency";
  Modelica.Units.SI.Power Q=P*tan(acos(
    AixLib.Utilities.Math.Functions.smoothMin(
      x1=pf_internal,
      x2=0.99999,
      deltaX=0.0000025)))
    "Reactive power (positive because inductive load)";
equation
  connect(pf_in, pf_internal);

  if not use_pf_in then
    pf_internal = pf;
  end if;
end InductiveLoad;


Partial model for a generic load

within AixLib.Electrical.Interfaces;
model Load "Partial model for a generic load"
  replaceable package PhaseSystem =
      AixLib.Electrical.PhaseSystems.PartialPhaseSystem constrainedby
    AixLib.Electrical.PhaseSystems.PartialPhaseSystem "Phase system"
  parameter Boolean linearized = false "If true, the load model is linearized"
  parameter AixLib.Electrical.Types.Load mode(
    min=AixLib.Electrical.Types.Load.FixedZ_steady_state,
    max=AixLib.Electrical.Types.Load.VariableZ_y_input) = AixLib.Electrical.Types.Load.FixedZ_steady_state
    "Type of load model (e.g., steady state, dynamic, prescribed power consumption, etc.)"

  parameter Modelica.Units.SI.Power P_nominal=0
    "Nominal power (negative if consumed, positive if generated). Used if mode <> AixLib.Electrical.Types.Load.VariableZ_P_input"

  parameter Modelica.Units.SI.Voltage V_nominal(min=0, start=110)
    "Nominal voltage (V_nominal >= 0)"
  parameter AixLib.Electrical.Types.InitMode initMode(
  min=AixLib.Electrical.Types.InitMode.zero_current,
  max=AixLib.Electrical.Types.InitMode.linearized) = AixLib.Electrical.Types.InitMode.zero_current
    "Initialization mode for homotopy operator"

  Modelica.Units.SI.Voltage v[:](start=PhaseSystem.phaseVoltages(V_nominal)) =
    terminal.v "Voltage vector";
  Modelica.Units.SI.Current i[:](each start=0) = terminal.i "Current vector";
  Modelica.Units.SI.Power S[PhaseSystem.n]=PhaseSystem.phasePowers_vi(v, -i)
    "Phase powers";
  Modelica.Units.SI.Power P(start=0)
    "Power of the load (negative if consumed, positive if fed into the electrical grid)";

  Modelica.Blocks.Interfaces.RealInput y(min=0, max=1, unit="1")
    if (mode == AixLib.Electrical.Types.Load.VariableZ_y_input)
    "Fraction of the nominal power consumed"
  Modelica.Blocks.Interfaces.RealInput Pow(unit="W")
    if (mode == AixLib.Electrical.Types.Load.VariableZ_P_input)
    "Power consumed"
  replaceable AixLib.Electrical.Interfaces.Terminal terminal(
    redeclare replaceable package PhaseSystem = PhaseSystem)
    "Generalized electric terminal"

protected
  Modelica.Blocks.Interfaces.RealInput y_internal
    "Hidden value of the input load for the conditional connector";
  Modelica.Blocks.Interfaces.RealInput P_internal
    "Hidden value of the input power for the conditional connector";
  Real load(min=eps, max=1)
    "Internal representation of control signal, used to avoid singularity";
  constant Real eps = 1E-10
    "Small number used to avoid a singularity if the power is zero";
  constant Real oneEps = 1-eps
    "Small number used to avoid a singularity if the power is zero";

initial equation
  if mode == AixLib.Electrical.Types.Load.VariableZ_P_input then
    assert(abs(P_nominal) < 1E-10, "*** Warning: P_nominal = " + String(P_nominal) + ", but this value will be ignored.",
           AssertionLevel.warning);
  end if;

equation
  assert(y_internal>=0 and y_internal<=1+eps, "The power load fraction P (input of the model) must be within [0,1]");

  // Connection between the conditional and inner connector
  connect(y,y_internal);
  connect(Pow,P_internal);

  // If the power is fixed, inner connector value is equal to 1
  if mode==AixLib.Electrical.Types.Load.FixedZ_steady_state or
     mode==AixLib.Electrical.Types.Load.FixedZ_dynamic then
    y_internal   = 1;
    P_internal = 0;
  elseif mode==AixLib.Electrical.Types.Load.VariableZ_y_input then
    P_internal = 0;
  elseif mode==AixLib.Electrical.Types.Load.VariableZ_P_input then
    y_internal = 1;
  end if;

  // Value of the load, depending on the type: fixed or variable
  if mode==AixLib.Electrical.Types.Load.VariableZ_y_input then
    load = eps + oneEps*y_internal;
  else
    load = 1;
  end if;

  // Power consumption
  if mode==AixLib.Electrical.Types.Load.FixedZ_steady_state or
     mode==AixLib.Electrical.Types.Load.FixedZ_dynamic then
    P = P_nominal;
  elseif mode==AixLib.Electrical.Types.Load.VariableZ_P_input then
    P = P_internal;
  else
    P = P_nominal*load;
  end if;
end Load;


within AixLib.Electrical;
package Interfaces "This package contains interfaces and partial models that are inherited by other components"
  extends Modelica.Icons.InterfacesPackage;
end Interfaces;


Partial model that contains basic parameters for a DC/AC conversion system. This model contains the minimum set of parameters necessary to describe
an AC/DC converter.

within AixLib.Electrical.Interfaces;
record PartialAcDcParameters
  "Partial model that contains basic parameters for a DC/AC conversion system"
  parameter Real pf(min=0, max=1) = 0.9 "Power factor"
  parameter Real eta_DCAC(min=0, max=1) = 0.9 "Efficiency of DC/AC conversion"
end PartialAcDcParameters;


Model of a generic two port component. This model declares connectors for electrical components with two terminals.

within AixLib.Electrical.Interfaces;
model PartialBaseTwoPort "Model of a generic two port component"

  replaceable AixLib.Electrical.Interfaces.BaseTerminal terminal_n
    "Electric terminal side p"
  replaceable AixLib.Electrical.Interfaces.BaseTerminal terminal_p
    "Electric terminal side n"
end PartialBaseTwoPort;


Model representing a generic two port system for conversion. This model extends the base class

AixLib.Electrical.Interfaces.PartialTwoPort
model and declares the variables
v_p and i_p that represents the voltage and the
current at the terminal_p, and the variables
v_n and i_n that represents the voltage and the
current at the terminal_n.
These variables are used in conversion models such as transformers and AC/DC converters.

within AixLib.Electrical.Interfaces;
model PartialConversion
  "Model representing a generic two port system for conversion"
  extends AixLib.Electrical.Interfaces.PartialTwoPort;
  Modelica.Units.SI.Voltage v_p "Voltage drop between the two positive pins";
  Modelica.Units.SI.Voltage v_n "Voltage drop between the two negative pins";
  Modelica.Units.SI.Current i_p "Current flowing through the positive pins";
  Modelica.Units.SI.Current i_n "Current flowing through the negative pins";
equation
  i_p = PhaseSystem_p.systemCurrent(terminal_p.i);
  i_n = PhaseSystem_n.systemCurrent(terminal_n.i);

  v_p = PhaseSystem_p.systemVoltage(terminal_p.v);
  v_n = PhaseSystem_n.systemVoltage(terminal_n.v);
end PartialConversion;


within AixLib.Electrical.Interfaces;
model PartialTwoPort "Model of a generic two port component with phase systems"

  replaceable package PhaseSystem_p =
      AixLib.Electrical.PhaseSystems.PartialPhaseSystem constrainedby
    AixLib.Electrical.PhaseSystems.PartialPhaseSystem
    "Phase system of terminal p"

  replaceable package PhaseSystem_n =
      AixLib.Electrical.PhaseSystems.PartialPhaseSystem constrainedby
    AixLib.Electrical.PhaseSystems.PartialPhaseSystem
    "Phase system of terminal n"

  extends AixLib.Electrical.Interfaces.PartialBaseTwoPort(
    redeclare replaceable AixLib.Electrical.Interfaces.Terminal terminal_n
      constrainedby AixLib.Electrical.Interfaces.Terminal(
        redeclare replaceable package PhaseSystem = PhaseSystem_n),
    redeclare replaceable AixLib.Electrical.Interfaces.Terminal terminal_p
      constrainedby AixLib.Electrical.Interfaces.Terminal(
        redeclare replaceable package PhaseSystem=PhaseSystem_p));
end PartialTwoPort;


Partial model of a resistive load. This is a model of a generic resistive load. This model is an extension of the base load model

AixLib.Electrical.Interfaces.Load.

within AixLib.Electrical.Interfaces;
partial model ResistiveLoad "Partial model of a resistive load"
  extends Load;
end ResistiveLoad;


Partial model of a generic source.

within AixLib.Electrical.Interfaces;
model Source "Partial model of a generic source."
  replaceable package PhaseSystem =
      AixLib.Electrical.PhaseSystems.OnePhase constrainedby
    AixLib.Electrical.PhaseSystems.PartialPhaseSystem "Phase system"
  parameter Boolean potentialReference = true
    "Serve as potential root for the reference angle theta"
  parameter Boolean definiteReference = false
    "Serve as definite root for the reference angle theta"
  Modelica.Units.SI.Power S[PhaseSystem.n]=PhaseSystem.phasePowers_vi(terminal.v,
      terminal.i) "Complex power S[1] = P, S[2]= Q";
  Modelica.Units.SI.Angle phi=PhaseSystem.phase(terminal.v) - PhaseSystem.phase(
      -terminal.i) "Phase shift with respect to reference angle";
  replaceable AixLib.Electrical.Interfaces.Terminal terminal(
    redeclare final replaceable package PhaseSystem = PhaseSystem)
    "Generalized terminal"
protected
  function j = PhaseSystem.j;
equation
  if potentialReference then
      if definiteReference then
        Connections.root(terminal.theta);
      else
        Connections.potentialRoot(terminal.theta);
      end if;
  end if;
end Source;


Generalized electric terminal

within AixLib.Electrical.Interfaces;
connector Terminal "Generalized electric terminal"
  extends AixLib.Electrical.Interfaces.BaseTerminal;
replaceable package PhaseSystem = PhaseSystems.PartialPhaseSystem
    "Phase system"
PhaseSystem.Voltage v[PhaseSystem.n] "Voltage vector";
flow PhaseSystem.Current i[PhaseSystem.n](each start=0) "Current vector";
PhaseSystem.ReferenceAngle theta[PhaseSystem.m]
    "Optional vector of phase angles";
end Terminal;


Partial model of a generic variable voltage source.. This model represents a generic variable voltage source. The model has a boolean
flag use_V_in, when this flag is equal to true
the voltage of the source is imposed by the input variable V_in.
When the flag is equal to false the voltage source is equal to the parameter V.

within AixLib.Electrical.Interfaces;
model VariableVoltageSource
  "Partial model of a generic variable voltage source."
  extends AixLib.Electrical.Interfaces.Source;
  parameter Boolean use_V_in = true "If true, the voltage is an input";
  parameter Modelica.Units.SI.Voltage V=1 "Value of constant voltage"
  Modelica.Blocks.Interfaces.RealInput V_in(unit="V", min=0, start = 1)
    if use_V_in "Input voltage"
protected
  Modelica.Blocks.Interfaces.RealInput  V_in_internal(unit="V")
    "Hidden value of the input voltage for the conditional connector";
equation

  // Connection between the conditional and inner connector
  connect(V_in,V_in_internal);

  // If the voltage is fixed, inner connector value is equal to parameter V
  if use_V_in == false then
    V_in_internal = V;
  end if;
end VariableVoltageSource;


Model of a general induction machine working as generator or electric motor. Model of an electric induction machine that includes the calculation
  of:

within AixLib.Electrical.Machines;
model InductionMachine
  "Model of a general induction machine working as generator or electric motor"
  import AixLib;
  extends Modelica.Electrical.Machines.Icons.TransientMachine;

  parameter Modelica.Units.SI.Frequency n0=f_1/p
    "Idling speed of the electric machine"
  parameter Modelica.Units.SI.Frequency n_nominal=1530/60 "Rated rotor speed"
  parameter Modelica.Units.SI.Frequency f_1=50 "Frequency"
  parameter Modelica.Units.SI.Voltage U_1=400 "Rated voltage"
  parameter Modelica.Units.SI.Current I_elNominal=P_elNominal/(sqrt(3)*U_1*
      cosPhi) "Rated current"
  parameter Modelica.Units.SI.Current I_1_start=if P_Mec_nominal <= 15000 then
      7.2*I_elNominal else 8*I_elNominal
    "Motor start current (realistic factors used from DIN VDE 2650/2651)"
  parameter Modelica.Units.SI.Power P_elNominal=15000
    "Nominal electrical power of electric machine"
  parameter Modelica.Units.SI.Power P_Mec_nominal=P_elNominal*(1 + s_nominal/
      0.22) "Nominal mechanical power of electric machine"
  parameter Modelica.Units.SI.Torque M_nominal=P_Mec_nominal/(2*Modelica.Constants.pi
      *n_nominal) "Nominal torque of electric machine"
  parameter Modelica.Units.SI.Torque M_til=2*M_nominal
    "Tilting torque of electric machine (realistic factor used from DIN VDE 2650/2651)"
  parameter Modelica.Units.SI.Torque M_start=if P_Mec_nominal <= 4000 then 1.6*
      M_nominal elseif P_Mec_nominal >= 22000 then 1*M_nominal else 1.25*
      M_nominal
    "Starting torque of electric machine (realistic factor used from DIN VDE 2650/2651)"
  parameter Modelica.Units.SI.Inertia J_Gen=1
    "Moment of inertia of the electric machine (default=0.5kg.m2)"
  parameter Real s_nominal=abs(1-n_nominal*p/f_1) "Nominal slip of electric machine"
  parameter Real s_til=abs((s_nominal*(M_til/M_nominal)+s_nominal*sqrt(abs(((M_til/M_nominal)^2)-1+2*s_nominal*((M_til/M_nominal)-1))))/(1-2*s_nominal*((M_til/M_nominal)-1)))
   "Tilting slip of electric machine"
  parameter Real p=2 "Number of pole pairs"
  parameter Real cosPhi=0.8 "Power factor of electric machine (default=0.8)"
  parameter Real calFac=1
    "Calibration factor for electric power outuput (default=1)"
  parameter Real gearRatio=1 "Transmission ratio (engine speed / generator speed)"

  Modelica.Units.SI.Frequency n=inertia.w/(2*Modelica.Constants.pi)
    "Speed of machine rotor [1/s]";
  Modelica.Units.SI.Current I_1 "Electric current of machine stator";
  Modelica.Units.SI.Power P_E "Electrical power at the electric machine";
  Modelica.Units.SI.Power P_Mec "Mechanical power at the electric machine";
  Modelica.Units.SI.Power CalQ_Loss
    "Calculated heat flow from electric machine";
  Modelica.Units.SI.Torque M "Torque at electric machine";
  Real s=1-n*p/f_1 "Current slip of electric machine";
  Real eta "Total efficiency of the electric machine (as motor)";
  Real calI_1 = 1/(1+((k-1)/((s_nominal^2)-k))*((s^2)+rho1*abs(s)+rho0));
  Boolean OpMode = (n<=n0) "Operation mode (true=motor, false=generator)";
  Boolean SwitchOnOff = isOn "Operation of electric machine (true=On, false=Off)";
  Modelica.Mechanics.Rotational.Components.Inertia inertia(       w(fixed=false), J=J_Gen)
  Modelica.Blocks.Sources.RealExpression electricTorque(y=M)
  Modelica.Mechanics.Rotational.Sources.Torque torque
  Modelica.Mechanics.Rotational.Interfaces.Flange_a flange_Machine

  Modelica.Mechanics.Rotational.Components.IdealGear transmission(ratio=
        gearRatio)
  Modelica.Blocks.Interfaces.BooleanInput isOn
  Modelica.Blocks.Interfaces.RealOutput electricCurrent
  Modelica.Blocks.Interfaces.RealOutput electricPower
  Modelica.Blocks.Interfaces.RealOutput rotationalSpeed
  Modelica.Blocks.Sources.RealExpression current(y=I_1)
  Modelica.Blocks.Sources.RealExpression power(y=P_E)
  Modelica.Blocks.Sources.RealExpression speed(y=n)

protected
  parameter Real rho0=s_til^2 "Calculation variable for analytical approach (Aree, 2017)"
  parameter Real rho1=(M_start*(1+s_til^2)-2*s_til*M_til)/(M_til-M_start) "Calculation variable for analytical approach (Aree, 2017)"
  parameter Real rho3=(M_til*M_start*(1-s_til)^2)/(M_til-M_start) "Calculation variable for analytical approach (Aree, 2017)"
  parameter Real k=((I_elNominal/I_1_start)^2)*(((s_nominal^2)+rho1*s_nominal+rho0)/(1+rho1+rho0)) "Calculation variable for analytical approach (Aree, 2017)"

equation

if noEvent(SwitchOnOff) then

  I_1=sign(s)*I_1_start*sqrt(abs((1+((k-1)/((s_nominal^2)-k))*(s^2)*(1+rho1+rho0))*calI_1));
  P_E=if noEvent(s>0) then sqrt(3)*I_1*U_1*cosPhi elseif noEvent(s<0) then calFac*(P_Mec+CalQ_Loss) else 1;
  P_Mec=2*Modelica.Constants.pi*M*n;
  CalQ_Loss= (calFac-1)*P_E + 2*Modelica.Constants.pi*M*(s*n0)/0.22;
  M=sign(s)*(rho3*abs(s))/((s^2)+rho1*abs(s)+rho0);
  eta=if noEvent(s>0) then abs(P_Mec/(P_E+1))
  elseif noEvent(s<0) then abs(P_E/(P_Mec-1)) else 0;

else

  I_1=0;
  P_E=0;
  P_Mec=0;
  CalQ_Loss=0;
  M=if noEvent(s<0) then sign(s)*(rho3*abs(s))/((s^2)+rho1*abs(s)+rho0) else 0;
  eta=0;

  end if;

  connect(electricTorque.y, torque.tau)
  connect(torque.flange, inertia.flange_a)
  connect(inertia.flange_b, transmission.flange_b)
  connect(transmission.flange_a, flange_Machine)
  connect(current.y, electricCurrent)
  connect(power.y, electricPower)
  connect(speed.y, rotationalSpeed)
end InductionMachine;