Model of an electrical line with neutral cable. This model represents an AC three-phase unbalanced cable with
neutral connection. The model is based on

Buildings.Electrical.AC.ThreePhasesUnbalanced.Lines.TwoPortRLC
and provides functionalities to parametrize the values of R, L and C
using either commercial cables or default values.

within Buildings.Electrical.AC.ThreePhasesUnbalanced.Lines;
model Line_N "Model of an electrical line with neutral cable"
  extends Buildings.Electrical.AC.ThreePhasesUnbalanced.Interfaces.TwoPort_N(
    terminal_p(phase(v(each nominal = V_nominal))),
    terminal_n(phase(v(each nominal = V_nominal))));
  extends Buildings.Electrical.Transmission.BaseClasses.PartialBaseLine(
  V_nominal(start = 480),
  commercialCable = Buildings.Electrical.Transmission.Functions.selectCable_low(P_nominal, V_nominal));
  OnePhase.Lines.TwoPortRL phase1(
    final useHeatPort=true,
    final T_ref=T_ref,
    final M=M,
    final mode=modelMode,
    final R=R/3,
    final L=L/3) "Impedance line 1"
  OnePhase.Lines.TwoPortRL phase2(
    final useHeatPort=true,
    final T_ref=T_ref,
    final M=M,
    final mode=modelMode,
    final R=R/3,
    final L=L/3) "Impedance line 1"
  OnePhase.Lines.TwoPortRL phase3(
    final useHeatPort=true,
    final T_ref=T_ref,
    final M=M,
    final mode=modelMode,
    final R=R/3,
    final L=L/3) "Impedance line 1"
  OnePhase.Lines.TwoPortRL neutral(
    final useHeatPort=true,
    final T_ref=T_ref,
    final M=M,
    final mode=modelMode,
    final R=R/3,
    final L=L/3) "Impedance of the neutral cable"
equation

  connect(cableTemp.port, phase1.heatPort)
  connect(cableTemp.port, phase2.heatPort)
  connect(cableTemp.port, phase3.heatPort)
  connect(terminal_n.phase[1], phase1.terminal_n)
  connect(terminal_n.phase[2], phase2.terminal_n)
  connect(terminal_n.phase[3], phase3.terminal_n)
  connect(phase1.terminal_p, terminal_p.phase[1])
  connect(phase2.terminal_p, terminal_p.phase[2])
  connect(phase3.terminal_p, terminal_p.phase[3])
  connect(cableTemp.port, neutral.heatPort)

  // Neutral cable connection
  connect(terminal_n.phase[4], neutral.terminal_n)
  connect(terminal_p.phase[4], neutral.terminal_p)
end Line_N;


Three phases unbalanced AC network without neutral cable. This model represents a generalized electrical AC three-phase unbalanced network
without neutral cable.

within Buildings.Electrical.AC.ThreePhasesUnbalanced.Lines;
model Network "Three phases unbalanced AC network without neutral cable"
  extends Transmission.BaseClasses.PartialNetwork(
    redeclare
      Buildings.Electrical.AC.ThreePhasesUnbalanced.Interfaces.Terminal_p terminal,
    redeclare replaceable Transmission.Grids.TestGrid2Nodes grid,
    redeclare Buildings.Electrical.AC.ThreePhasesUnbalanced.Lines.Line lines(commercialCable=grid.cables));
  Modelica.Units.SI.Voltage VAbs[3,grid.nNodes] "RMS voltage of the grid nodes";
equation
  for i in 1:grid.nLinks loop
    connect(lines[i].terminal_p, terminal[grid.fromTo[i,1]]);
    connect(lines[i].terminal_n, terminal[grid.fromTo[i,2]]);
  end for;

  for i in 1:grid.nNodes loop
    VAbs[1,i] = Buildings.Electrical.PhaseSystems.OnePhase.systemVoltage(terminal[i].phase[1].v);
    VAbs[2,i] = Buildings.Electrical.PhaseSystems.OnePhase.systemVoltage(terminal[i].phase[2].v);
    VAbs[3,i] = Buildings.Electrical.PhaseSystems.OnePhase.systemVoltage(terminal[i].phase[3].v);
  end for;
end Network;


Three phases unbalanced AC network with neutral cable. This model represents a generalized electrical AC three-phase unbalanced network
with neutral cable.

within Buildings.Electrical.AC.ThreePhasesUnbalanced.Lines;
model Network_N "Three phases unbalanced AC network with neutral cable"
  extends Transmission.BaseClasses.PartialNetwork(
    redeclare
      Buildings.Electrical.AC.ThreePhasesUnbalanced.Interfaces.Terminal4_p terminal,
    redeclare replaceable Transmission.Grids.TestGrid2Nodes grid,
    redeclare Buildings.Electrical.AC.ThreePhasesUnbalanced.Lines.Line_N lines(
        commercialCable=grid.cables));
  Modelica.Units.SI.Voltage VAbs[3,grid.nNodes] "RMS voltage of the grid nodes";
equation
  for i in 1:grid.nLinks loop
    connect(lines[i].terminal_p, terminal[grid.fromTo[i,1]]);
    connect(lines[i].terminal_n, terminal[grid.fromTo[i,2]]);
  end for;

  for i in 1:grid.nNodes loop
    VAbs[1,i] = Buildings.Electrical.PhaseSystems.OnePhase.systemVoltage(terminal[i].phase[1].v - terminal[i].phase[4].v);
    VAbs[2,i] = Buildings.Electrical.PhaseSystems.OnePhase.systemVoltage(terminal[i].phase[2].v - terminal[i].phase[4].v);
    VAbs[3,i] = Buildings.Electrical.PhaseSystems.OnePhase.systemVoltage(terminal[i].phase[3].v - terminal[i].phase[4].v);
  end for;
end Network_N;


within Buildings.Electrical.AC.ThreePhasesUnbalanced;
package Lines "Package with transmission line models for three-phase unbalanced AC systems"
  extends Modelica.Icons.Package;
end Lines;


Model of an inductance with two electrical ports. Inductive model that connects two AC three-phase
unbalanced interfaces. This model can be used to represent a
cable in a three-phase unbalanced AC system.

within Buildings.Electrical.AC.ThreePhasesUnbalanced.Lines;
model TwoPortInductance "Model of an inductance with two electrical ports"
  extends Buildings.Electrical.AC.ThreePhasesUnbalanced.Interfaces.TwoPort;
  parameter Modelica.Units.SI.Inductance L "Inductance";
  OnePhase.Lines.TwoPortInductance  phase1(
    final L=L/3) "Inductance line 1"
  OnePhase.Lines.TwoPortInductance phase2(
    final L=L/3) "Inductance line 2"
  OnePhase.Lines.TwoPortInductance phase3(
    final L=L/3) "Inductance line 3"
equation
  connect(terminal_n.phase[1], phase1.terminal_n)
  connect(terminal_n.phase[2], phase2.terminal_n)
  connect(terminal_n.phase[3], phase3.terminal_n)
  connect(phase1.terminal_p, terminal_p.phase[1])
  connect(phase2.terminal_p, terminal_p.phase[2])
  connect(phase3.terminal_p, terminal_p.phase[3])
end TwoPortInductance;


Model of an inductance with two electrical ports and neutral line cable. Inductive model that connects two AC three-phase
unbalanced interfaces with neutral line. This model can be used to represent a
cable in a three-phase unbalanced AC system.

within Buildings.Electrical.AC.ThreePhasesUnbalanced.Lines;
model TwoPortInductance_N
  "Model of an inductance with two electrical ports and neutral line cable"
  extends Buildings.Electrical.AC.ThreePhasesUnbalanced.Interfaces.TwoPort_N;
  parameter Modelica.Units.SI.Inductance L "Inductance";
  parameter Modelica.Units.SI.Inductance Ln "Inductance of neutral cable";
  OnePhase.Lines.TwoPortInductance  phase1(
    final L=L/3) "Inductance line 1"
  OnePhase.Lines.TwoPortInductance phase2(
    final L=L/3) "Inductance line 2"
  OnePhase.Lines.TwoPortInductance phase3(
    final L=L/3) "Inductance line 3"
  OnePhase.Lines.TwoPortInductance phase4(
    final L=L/3) "Inductance line 3"
equation
  connect(terminal_n.phase[1], phase1.terminal_n)
  connect(terminal_n.phase[2], phase2.terminal_n)
  connect(terminal_n.phase[3], phase3.terminal_n)
  connect(phase1.terminal_p, terminal_p.phase[1])
  connect(phase2.terminal_p, terminal_p.phase[2])
  connect(phase3.terminal_p, terminal_p.phase[3])

  connect(phase4.terminal_p, terminal_p.phase[4])
  connect(phase4.terminal_n, terminal_n.phase[4])
end TwoPortInductance_N;


Model of an RL line parameterized with impedance matrices. Resistive-inductive model that connects two AC three-phase
unbalanced interfaces. This model can be used to represent a
cable in a three-phase unbalanced AC system.
The voltage between the ports is

within Buildings.Electrical.AC.ThreePhasesUnbalanced.Lines;
model TwoPortMatrixRL
  "Model of an RL line parameterized with impedance matrices"
  extends Buildings.Electrical.AC.ThreePhasesUnbalanced.Interfaces.TwoPort(
    terminal_p(phase(v(each nominal = V_nominal))),
    terminal_n(phase(v(each nominal = V_nominal))));
  parameter Modelica.Units.SI.Voltage V_nominal(min=0, start=480)
    "Nominal voltage (V_nominal >= 0)"
  parameter Modelica.Units.SI.Impedance Z11[2]
    "Element [1,1] of impedance matrix";
  parameter Modelica.Units.SI.Impedance Z12[2]
    "Element [1,2] of impedance matrix";
  parameter Modelica.Units.SI.Impedance Z13[2]
    "Element [1,3] of impedance matrix";
  parameter Modelica.Units.SI.Impedance Z22[2]
    "Element [2,2] of impedance matrix";
  parameter Modelica.Units.SI.Impedance Z23[2]
    "Element [2,3] of impedance matrix";
  parameter Modelica.Units.SI.Impedance Z33[2]
    "Element [3,3] of impedance matrix";
  final parameter Modelica.Units.SI.Impedance[2] Z21=Z12
    "Element [2,1] of impedance matrix";
  final parameter Modelica.Units.SI.Impedance[2] Z31=Z13
    "Element [3,1] of impedance matrix";
  final parameter Modelica.Units.SI.Impedance[2] Z32=Z23
    "Element [3,1] of impedance matrix";

  Modelica.Units.SI.Current i1[2](each stateSelect=StateSelect.prefer)=
    terminal_n.phase[1].i "Current in line 1";
  Modelica.Units.SI.Current i2[2](each stateSelect=StateSelect.prefer)=
    terminal_n.phase[2].i "Current in line 2";
  Modelica.Units.SI.Current i3[2](each stateSelect=StateSelect.prefer)=
    terminal_n.phase[3].i "Current in line 3";
  Modelica.Units.SI.Voltage v1_n[2](
    start=Buildings.Electrical.PhaseSystems.OnePhase.phaseVoltages(V_nominal/
        sqrt(3), phi=0),
    each stateSelect=StateSelect.never) = terminal_n.phase[1].v
    "Voltage in line 1 at connector N";
  Modelica.Units.SI.Voltage v2_n[2](
    start=Buildings.Electrical.PhaseSystems.OnePhase.phaseVoltages(V_nominal/
        sqrt(3), phi=-2*Modelica.Constants.pi/3),
    each stateSelect=StateSelect.never) = terminal_n.phase[2].v
    "Voltage in line 2 at connector N";
  Modelica.Units.SI.Voltage v3_n[2](
    start=Buildings.Electrical.PhaseSystems.OnePhase.phaseVoltages(V_nominal/
        sqrt(3), phi=2*Modelica.Constants.pi/3),
    each stateSelect=StateSelect.never) = terminal_n.phase[3].v
    "Voltage in line 3 at connector N";
  Modelica.Units.SI.Voltage v1_p[2](
    start=Buildings.Electrical.PhaseSystems.OnePhase.phaseVoltages(V_nominal/
        sqrt(3), phi=0),
    each stateSelect=StateSelect.never) = terminal_p.phase[1].v
    "Voltage in line 1 at connector P";
  Modelica.Units.SI.Voltage v2_p[2](
    start=Buildings.Electrical.PhaseSystems.OnePhase.phaseVoltages(V_nominal/
        sqrt(3), phi=-2*Modelica.Constants.pi/3),
    each stateSelect=StateSelect.never) = terminal_p.phase[2].v
    "Voltage in line 2 at connector P";
  Modelica.Units.SI.Voltage v3_p[2](
    start=Buildings.Electrical.PhaseSystems.OnePhase.phaseVoltages(V_nominal/
        sqrt(3), phi=2*Modelica.Constants.pi/3),
    each stateSelect=StateSelect.never) = terminal_p.phase[3].v
    "Voltage in line 3 at connector P";

protected
  function productAC1p = Buildings.Electrical.PhaseSystems.OnePhase.product
    "Product between complex quantities";
equation

  // Link the connectors to propagate the overdetermined variable
  for i in 1:3 loop
      Connections.branch(terminal_p.phase[i].theta, terminal_n.phase[i].theta);
      terminal_p.phase[i].theta = terminal_n.phase[i].theta;

      // No current losses, they are preserved in each line
      terminal_p.phase[i].i = - terminal_n.phase[i].i;
  end for;

  // Voltage drop caused by the impedance matrix
  v1_n - v1_p = productAC1p(Z11, i1) + productAC1p(Z12, i2) + productAC1p(Z13, i3);
  v2_n - v2_p = productAC1p(Z21, i1) + productAC1p(Z22, i2) + productAC1p(Z23, i3);
  v3_n - v3_p = productAC1p(Z31, i1) + productAC1p(Z32, i2) + productAC1p(Z33, i3);
end TwoPortMatrixRL;


PI model of a line parameterized with impedance and admittance matrices. RLC line model (π-model) that connects two AC three-phase
unbalanced interfaces. This model can be used to represent a
cable in a three-phase unbalanced AC system.

within Buildings.Electrical.AC.ThreePhasesUnbalanced.Lines;
model TwoPortMatrixRLC
  "PI model of a line parameterized with impedance and admittance matrices"
  extends Buildings.Electrical.AC.ThreePhasesUnbalanced.Interfaces.TwoPort(
    terminal_p(phase(v(each nominal = V_nominal))),
    terminal_n(phase(v(each nominal = V_nominal))));
  parameter Modelica.Units.SI.Voltage V_nominal(min=0, start=480)
    "Nominal voltage (V_nominal >= 0)"
  parameter Modelica.Units.SI.Impedance Z11[2]
    "Element [1,1] of impedance matrix";
  parameter Modelica.Units.SI.Impedance Z12[2]
    "Element [1,2] of impedance matrix";
  parameter Modelica.Units.SI.Impedance Z13[2]
    "Element [1,3] of impedance matrix";
  parameter Modelica.Units.SI.Impedance Z22[2]
    "Element [2,2] of impedance matrix";
  parameter Modelica.Units.SI.Impedance Z23[2]
    "Element [2,3] of impedance matrix";
  parameter Modelica.Units.SI.Impedance Z33[2]
    "Element [3,3] of impedance matrix";
  final parameter Modelica.Units.SI.Impedance[2] Z21=Z12
    "Element [2,1] of impedance matrix";
  final parameter Modelica.Units.SI.Impedance[2] Z31=Z13
    "Element [3,1] of impedance matrix";
  final parameter Modelica.Units.SI.Impedance[2] Z32=Z23
    "Element [3,1] of impedance matrix";

  parameter Modelica.Units.SI.Admittance B11
    "Element [1,1] of admittance matrix";
  parameter Modelica.Units.SI.Admittance B12
    "Element [1,2] of admittance matrix";
  parameter Modelica.Units.SI.Admittance B13
    "Element [1,3] of admittance matrix";
  parameter Modelica.Units.SI.Admittance B22
    "Element [2,2] of admittance matrix";
  parameter Modelica.Units.SI.Admittance B23
    "Element [2,3] of admittance matrix";
  parameter Modelica.Units.SI.Admittance B33
    "Element [3,3] of admittance matrix";
  final parameter Modelica.Units.SI.Admittance B21=B12
    "Element [2,1] of admittance matrix";
  final parameter Modelica.Units.SI.Admittance B31=B13
    "Element [3,1] of admittance matrix";
  final parameter Modelica.Units.SI.Admittance B32=B23
    "Element [3,2] of admittance matrix";

  Modelica.Units.SI.Voltage v1_n[2](
    start=Buildings.Electrical.PhaseSystems.OnePhase.phaseVoltages(V_nominal/
        sqrt(3), phi=0),
    each stateSelect=StateSelect.never) = terminal_n.phase[1].v
    "Voltage in line 1 at connector N";
  Modelica.Units.SI.Voltage v2_n[2](
    start=Buildings.Electrical.PhaseSystems.OnePhase.phaseVoltages(V_nominal/
        sqrt(3), phi=-2*Modelica.Constants.pi/3),
    each stateSelect=StateSelect.never) = terminal_n.phase[2].v
    "Voltage in line 2 at connector N";
  Modelica.Units.SI.Voltage v3_n[2](
    start=Buildings.Electrical.PhaseSystems.OnePhase.phaseVoltages(V_nominal/
        sqrt(3), phi=2*Modelica.Constants.pi/3),
    each stateSelect=StateSelect.never) = terminal_n.phase[3].v
    "Voltage in line 3 at connector N";
  Modelica.Units.SI.Voltage v1_p[2](
    start=Buildings.Electrical.PhaseSystems.OnePhase.phaseVoltages(V_nominal/
        sqrt(3), phi=0),
    each stateSelect=StateSelect.never) = terminal_p.phase[1].v
    "Voltage in line 1 at connector P";
  Modelica.Units.SI.Voltage v2_p[2](
    start=Buildings.Electrical.PhaseSystems.OnePhase.phaseVoltages(V_nominal/
        sqrt(3), phi=-2*Modelica.Constants.pi/3),
    each stateSelect=StateSelect.never) = terminal_p.phase[2].v
    "Voltage in line 2 at connector P";
  Modelica.Units.SI.Voltage v3_p[2](
    start=Buildings.Electrical.PhaseSystems.OnePhase.phaseVoltages(V_nominal/
        sqrt(3), phi=2*Modelica.Constants.pi/3),
    each stateSelect=StateSelect.never) = terminal_p.phase[3].v
    "Voltage in line 3 at connector P";

protected
  function productAC1p = Buildings.Electrical.PhaseSystems.OnePhase.product
    "Product between complex quantities";
  Modelica.Units.SI.Current Isr[3,2](start=zeros(3, Buildings.Electrical.PhaseSystems.OnePhase.n),
      each stateSelect=StateSelect.prefer)
    "Currents that pass through the lines";
  Modelica.Units.SI.Current Ish_p[3,2](start=zeros(3, Buildings.Electrical.PhaseSystems.OnePhase.n),
      each stateSelect=StateSelect.prefer) "Shunt current on side p";
  Modelica.Units.SI.Current Ish_n[3,2](start=zeros(3, Buildings.Electrical.PhaseSystems.OnePhase.n),
      each stateSelect=StateSelect.prefer) "Shunt current on side n";

equation

  // Link the connectors to propagate the overdetermined variable
  for i in 1:3 loop
      Connections.branch(terminal_p.phase[i].theta, terminal_n.phase[i].theta);
      terminal_p.phase[i].theta = terminal_n.phase[i].theta;
  end for;

  // Kirkoff current law for the terminal n (left side)
  Isr[1,:] = terminal_n.phase[1].i - Ish_n[1,:];
  Isr[2,:] = terminal_n.phase[2].i - Ish_n[2,:];
  Isr[3,:] = terminal_n.phase[3].i - Ish_n[3,:];

  // Kirkoff current law for the terminal p (right side)
  Isr[1,:] + terminal_p.phase[1].i = Ish_p[1,:];
  Isr[2,:] + terminal_p.phase[2].i = Ish_p[2,:];
  Isr[3,:] + terminal_p.phase[3].i = Ish_p[3,:];

  // Voltage drop caused by the impedance matrix
  terminal_n.phase[1].v - terminal_p.phase[1].v = productAC1p(Z11, terminal_n.phase[1].i)
                                                + productAC1p(Z12, terminal_n.phase[2].i)
                                                + productAC1p(Z13, terminal_n.phase[3].i);
  terminal_n.phase[2].v - terminal_p.phase[2].v = productAC1p(Z21, terminal_n.phase[1].i)
                                                + productAC1p(Z22, terminal_n.phase[2].i)
                                                + productAC1p(Z23, terminal_n.phase[3].i);
  terminal_n.phase[3].v - terminal_p.phase[3].v = productAC1p(Z31, terminal_n.phase[1].i)
                                                + productAC1p(Z32, terminal_n.phase[2].i)
                                                + productAC1p(Z33, terminal_n.phase[3].i);

  // Current loss at the terminal n
  Ish_n[1,:] = productAC1p({0, B11/2}, v1_n)
             + productAC1p({0, B12/2}, v2_n)
             + productAC1p({0, B13/2}, v3_n);
  Ish_n[2,:] = productAC1p({0, B21/2}, v1_n)
             + productAC1p({0, B22/2}, v2_n)
             + productAC1p({0, B23/2}, v3_n);
  Ish_n[3,:] = productAC1p({0, B31/2}, v1_n)
             + productAC1p({0, B32/2}, v2_n)
             + productAC1p({0, B33/2}, v3_n);

  // Current loss at the terminal n
  Ish_p[1,:] = productAC1p({0, B11/2}, v1_p)
             + productAC1p({0, B12/2}, v2_p)
             + productAC1p({0, B13/2}, v3_p);
  Ish_p[2,:] = productAC1p({0, B21/2}, v1_p)
             + productAC1p({0, B22/2}, v2_p)
             + productAC1p({0, B23/2}, v3_p);
  Ish_p[3,:] = productAC1p({0, B31/2}, v1_p)
             + productAC1p({0, B32/2}, v2_p)
             + productAC1p({0, B33/2}, v3_p);
end TwoPortMatrixRLC;


PI model of a line parameterized with impedance and admittance matrices and neutral line. RLC line model (π-model) that connects two AC three-phase
unbalanced interfaces and neutral line. This model can be used to represent a
cable in a three-phase unbalanced AC system.

within Buildings.Electrical.AC.ThreePhasesUnbalanced.Lines;
model TwoPortMatrixRLC_N
  "PI model of a line parameterized with impedance and admittance matrices and neutral line"
  extends Buildings.Electrical.AC.ThreePhasesUnbalanced.Interfaces.TwoPort_N(
    terminal_p(phase(v(each nominal = V_nominal))),
    terminal_n(phase(v(each nominal = V_nominal))));

  parameter Modelica.Units.SI.Voltage V_nominal(min=0, start=480)
    "Nominal voltage (V_nominal >= 0)"
  parameter Modelica.Units.SI.Impedance Z11[2]
    "Element [1,1] of impedance matrix";
  parameter Modelica.Units.SI.Impedance Z12[2]
    "Element [1,2] of impedance matrix";
  parameter Modelica.Units.SI.Impedance Z13[2]
    "Element [1,3] of impedance matrix";
  parameter Modelica.Units.SI.Impedance Z14[2]
    "Element [1,4] of impedance matrix";
  parameter Modelica.Units.SI.Impedance Z22[2]
    "Element [2,2] of impedance matrix";
  parameter Modelica.Units.SI.Impedance Z23[2]
    "Element [2,3] of impedance matrix";
  parameter Modelica.Units.SI.Impedance Z24[2]
    "Element [2,4] of impedance matrix";
  parameter Modelica.Units.SI.Impedance Z33[2]
    "Element [3,3] of impedance matrix";
  parameter Modelica.Units.SI.Impedance Z34[2]
    "Element [3,4] of impedance matrix";
  parameter Modelica.Units.SI.Impedance Z44[2]
    "Element [4,4] of impedance matrix";
  final parameter Modelica.Units.SI.Impedance[2] Z21=Z12
    "Element [2,1] of impedance matrix";
  final parameter Modelica.Units.SI.Impedance[2] Z31=Z13
    "Element [3,1] of impedance matrix";
  final parameter Modelica.Units.SI.Impedance[2] Z32=Z23
    "Element [3,1] of impedance matrix";
  final parameter Modelica.Units.SI.Impedance[2] Z41=Z14
    "Element [4,1] of impedance matrix";
  final parameter Modelica.Units.SI.Impedance[2] Z42=Z24
    "Element [4,2] of impedance matrix";
  final parameter Modelica.Units.SI.Impedance[2] Z43=Z34
    "Element [4,3] of impedance matrix";

  parameter Modelica.Units.SI.Admittance B11
    "Element [1,1] of admittance matrix";
  parameter Modelica.Units.SI.Admittance B12
    "Element [1,2] of admittance matrix";
  parameter Modelica.Units.SI.Admittance B13
    "Element [1,3] of admittance matrix";
  parameter Modelica.Units.SI.Admittance B14
    "Element [1,4] of admittance matrix";
  parameter Modelica.Units.SI.Admittance B22
    "Element [2,2] of admittance matrix";
  parameter Modelica.Units.SI.Admittance B23
    "Element [2,3] of admittance matrix";
  parameter Modelica.Units.SI.Admittance B24
    "Element [2,4] of admittance matrix";
  parameter Modelica.Units.SI.Admittance B33
    "Element [3,3] of admittance matrix";
  parameter Modelica.Units.SI.Admittance B34
    "Element [3,4] of admittance matrix";
  parameter Modelica.Units.SI.Admittance B44
    "Element [4,4] of admittance matrix";
  final parameter Modelica.Units.SI.Admittance B21=B12
    "Element [2,1] of admittance matrix";
  final parameter Modelica.Units.SI.Admittance B31=B13
    "Element [3,1] of admittance matrix";
  final parameter Modelica.Units.SI.Admittance B32=B23
    "Element [3,2] of admittance matrix";
  final parameter Modelica.Units.SI.Admittance B41=B14
    "Element [4,1] of admittance matrix";
  final parameter Modelica.Units.SI.Admittance B42=B24
    "Element [4,2] of admittance matrix";
  final parameter Modelica.Units.SI.Admittance B43=B34
    "Element [4,3] of admittance matrix";

  Modelica.Units.SI.Voltage v1_n[2](
    start=Buildings.Electrical.PhaseSystems.OnePhase.phaseVoltages(V_nominal/
        sqrt(3), phi=0),
    each stateSelect=StateSelect.never) = terminal_n.phase[1].v
    "Voltage in line 1 at connector N";
  Modelica.Units.SI.Voltage v2_n[2](
    start=Buildings.Electrical.PhaseSystems.OnePhase.phaseVoltages(V_nominal/
        sqrt(3), phi=-2*Modelica.Constants.pi/3),
    each stateSelect=StateSelect.never) = terminal_n.phase[2].v
    "Voltage in line 2 at connector N";
  Modelica.Units.SI.Voltage v3_n[2](
    start=Buildings.Electrical.PhaseSystems.OnePhase.phaseVoltages(V_nominal/
        sqrt(3), phi=2*Modelica.Constants.pi/3),
    each stateSelect=StateSelect.never) = terminal_n.phase[3].v
    "Voltage in line 3 at connector N";
  Modelica.Units.SI.Voltage v4_n[2](
    start=Buildings.Electrical.PhaseSystems.OnePhase.phaseVoltages(0),
    each stateSelect=StateSelect.never) = terminal_n.phase[4].v
    "Voltage in line 4 (neutral) at connector N";
  Modelica.Units.SI.Voltage v1_p[2](
    start=Buildings.Electrical.PhaseSystems.OnePhase.phaseVoltages(V_nominal/
        sqrt(3), phi=0),
    each stateSelect=StateSelect.never) = terminal_p.phase[1].v
    "Voltage in line 1 at connector P";
  Modelica.Units.SI.Voltage v2_p[2](
    start=Buildings.Electrical.PhaseSystems.OnePhase.phaseVoltages(V_nominal/
        sqrt(3), phi=-2*Modelica.Constants.pi/3),
    each stateSelect=StateSelect.never) = terminal_p.phase[2].v
    "Voltage in line 2 at connector P";
  Modelica.Units.SI.Voltage v3_p[2](
    start=Buildings.Electrical.PhaseSystems.OnePhase.phaseVoltages(V_nominal/
        sqrt(3), phi=2*Modelica.Constants.pi/3),
    each stateSelect=StateSelect.never) = terminal_p.phase[3].v
    "Voltage in line 3 at connector P";
  Modelica.Units.SI.Voltage v4_p[2](
    start=Buildings.Electrical.PhaseSystems.OnePhase.phaseVoltages(0),
    each stateSelect=StateSelect.never) = terminal_p.phase[4].v
    "Voltage in line 4 (neutral) at connector P";

protected
  function productAC1p = Buildings.Electrical.PhaseSystems.OnePhase.product
    "Product between complex quantities";
  Modelica.Units.SI.Current Isr[4,2](each stateSelect=StateSelect.prefer)
    "Currents that pass through the lines";
  Modelica.Units.SI.Current Ish_p[4,2](each stateSelect=StateSelect.prefer)
    "Shunt current on side p";
  Modelica.Units.SI.Current Ish_n[4,2](each stateSelect=StateSelect.prefer)
    "Shunt current on side n";

equation

  // Link the connectors to propagate the overdetermined variable
  for i in 1:4 loop
      Connections.branch(terminal_p.phase[i].theta, terminal_n.phase[i].theta);
      terminal_p.phase[i].theta = terminal_n.phase[i].theta;
  end for;

  // Kirkoff current law for the terminal n (left side)
  Isr[1,:] = terminal_n.phase[1].i - Ish_n[1,:];
  Isr[2,:] = terminal_n.phase[2].i - Ish_n[2,:];
  Isr[3,:] = terminal_n.phase[3].i - Ish_n[3,:];
  Isr[4,:] = terminal_n.phase[4].i - Ish_n[4,:];

  // Kirkoff current law for the terminal p (right side)
  Isr[1,:] + terminal_p.phase[1].i = Ish_p[1,:];
  Isr[2,:] + terminal_p.phase[2].i = Ish_p[2,:];
  Isr[3,:] + terminal_p.phase[3].i = Ish_p[3,:];
  Isr[4,:] + terminal_p.phase[4].i = Ish_p[4,:];

  // Voltage drop caused by the impedance matrix
  terminal_n.phase[1].v - terminal_p.phase[1].v = productAC1p(Z11, terminal_n.phase[1].i)
                                                + productAC1p(Z12, terminal_n.phase[2].i)
                                                + productAC1p(Z13, terminal_n.phase[3].i)
                                                + productAC1p(Z14, terminal_n.phase[4].i);
  terminal_n.phase[2].v - terminal_p.phase[2].v = productAC1p(Z21, terminal_n.phase[1].i)
                                                + productAC1p(Z22, terminal_n.phase[2].i)
                                                + productAC1p(Z23, terminal_n.phase[3].i)
                                                + productAC1p(Z24, terminal_n.phase[4].i);
  terminal_n.phase[3].v - terminal_p.phase[3].v = productAC1p(Z31, terminal_n.phase[1].i)
                                                + productAC1p(Z32, terminal_n.phase[2].i)
                                                + productAC1p(Z33, terminal_n.phase[3].i)
                                                + productAC1p(Z34, terminal_n.phase[4].i);
  terminal_n.phase[4].v - terminal_p.phase[4].v = productAC1p(Z41, terminal_n.phase[1].i)
                                                + productAC1p(Z42, terminal_n.phase[2].i)
                                                + productAC1p(Z43, terminal_n.phase[3].i)
                                                + productAC1p(Z44, terminal_n.phase[4].i);

  // Current loss at the terminal n
  Ish_n[1,:] = productAC1p({0, B11/2}, v1_n)
             + productAC1p({0, B12/2}, v2_n)
             + productAC1p({0, B13/2}, v3_n)
             + productAC1p({0, B14/2}, v4_n);
  Ish_n[2,:] = productAC1p({0, B21/2}, v1_n)
             + productAC1p({0, B22/2}, v2_n)
             + productAC1p({0, B23/2}, v3_n)
             + productAC1p({0, B24/2}, v4_n);
  Ish_n[3,:] = productAC1p({0, B31/2}, v1_n)
             + productAC1p({0, B32/2}, v2_n)
             + productAC1p({0, B33/2}, v3_n)
             + productAC1p({0, B34/2}, v4_n);
  Ish_n[4,:] = productAC1p({0, B41/2}, v1_n)
             + productAC1p({0, B42/2}, v2_n)
             + productAC1p({0, B43/2}, v3_n)
             + productAC1p({0, B44/2}, v4_n);

  // Current loss at the terminal n
  Ish_p[1,:] = productAC1p({0, B11/2}, v1_p)
             + productAC1p({0, B12/2}, v2_p)
             + productAC1p({0, B13/2}, v3_p)
             + productAC1p({0, B14/2}, v4_p);
  Ish_p[2,:] = productAC1p({0, B21/2}, v1_p)
             + productAC1p({0, B22/2}, v2_p)
             + productAC1p({0, B23/2}, v3_p)
             + productAC1p({0, B24/2}, v4_p);
  Ish_p[3,:] = productAC1p({0, B31/2}, v1_p)
             + productAC1p({0, B32/2}, v2_p)
             + productAC1p({0, B33/2}, v3_p)
             + productAC1p({0, B34/2}, v4_p);
  Ish_p[4,:] = productAC1p({0, B41/2}, v1_p)
             + productAC1p({0, B42/2}, v2_p)
             + productAC1p({0, B43/2}, v3_p)
             + productAC1p({0, B44/2}, v4_p);
end TwoPortMatrixRLC_N;


Model of an RL line parameterized with impedance matrices and neutral line. Resistive-inductive model that connects two AC three-phase
unbalanced interfaces with neutral line. This model can be used to represent a
cable in a three-phase unbalanced AC system.
The voltage between the ports is

within Buildings.Electrical.AC.ThreePhasesUnbalanced.Lines;
model TwoPortMatrixRL_N
  "Model of an RL line parameterized with impedance matrices and neutral line"
  extends Buildings.Electrical.AC.ThreePhasesUnbalanced.Interfaces.TwoPort_N(
    terminal_p(phase(v(each nominal = V_nominal))),
    terminal_n(phase(v(each nominal = V_nominal))));

  parameter Modelica.Units.SI.Voltage V_nominal(min=0, start=480)
    "Nominal voltage (V_nominal >= 0)"
  parameter Modelica.Units.SI.Impedance Z11[2]
    "Element [1,1] of impedance matrix";
  parameter Modelica.Units.SI.Impedance Z12[2]
    "Element [1,2] of impedance matrix";
  parameter Modelica.Units.SI.Impedance Z13[2]
    "Element [1,3] of impedance matrix";
  parameter Modelica.Units.SI.Impedance Z14[2]
    "Element [1,4] of impedance matrix";
  parameter Modelica.Units.SI.Impedance Z22[2]
    "Element [2,2] of impedance matrix";
  parameter Modelica.Units.SI.Impedance Z23[2]
    "Element [2,3] of impedance matrix";
  parameter Modelica.Units.SI.Impedance Z24[2]
    "Element [2,4] of impedance matrix";
  parameter Modelica.Units.SI.Impedance Z33[2]
    "Element [3,3] of impedance matrix";
  parameter Modelica.Units.SI.Impedance Z34[2]
    "Element [3,4] of impedance matrix";
  parameter Modelica.Units.SI.Impedance Z44[2]
    "Element [4,4] of impedance matrix";
  final parameter Modelica.Units.SI.Impedance[2] Z21=Z12
    "Element [2,1] of impedance matrix";
  final parameter Modelica.Units.SI.Impedance[2] Z31=Z13
    "Element [3,1] of impedance matrix";
  final parameter Modelica.Units.SI.Impedance[2] Z32=Z23
    "Element [3,1] of impedance matrix";
  final parameter Modelica.Units.SI.Impedance[2] Z41=Z14
    "Element [4,1] of impedance matrix";
  final parameter Modelica.Units.SI.Impedance[2] Z42=Z24
    "Element [4,2] of impedance matrix";
  final parameter Modelica.Units.SI.Impedance[2] Z43=Z34
    "Element [4,3] of impedance matrix";

  Modelica.Units.SI.Current i1[2](each stateSelect=StateSelect.prefer)=
    terminal_n.phase[1].i "Current in line 1";
  Modelica.Units.SI.Current i2[2](each stateSelect=StateSelect.prefer)=
    terminal_n.phase[2].i "Current in line 2";
  Modelica.Units.SI.Current i3[2](each stateSelect=StateSelect.prefer)=
    terminal_n.phase[3].i "Current in line 3";
  Modelica.Units.SI.Current i4[2](each stateSelect=StateSelect.prefer)=
    terminal_n.phase[4].i "Current in line 4 (neutral)";
  Modelica.Units.SI.Voltage v1_n[2](
    start=Buildings.Electrical.PhaseSystems.OnePhase.phaseVoltages(V_nominal/
        sqrt(3), phi=0),
    each stateSelect=StateSelect.never) = terminal_n.phase[1].v
    "Voltage in line 1 at connector N";
  Modelica.Units.SI.Voltage v2_n[2](
    start=Buildings.Electrical.PhaseSystems.OnePhase.phaseVoltages(V_nominal/
        sqrt(3), phi=-2*Modelica.Constants.pi/3),
    each stateSelect=StateSelect.never) = terminal_n.phase[2].v
    "Voltage in line 2 at connector N";
  Modelica.Units.SI.Voltage v3_n[2](
    start=Buildings.Electrical.PhaseSystems.OnePhase.phaseVoltages(V_nominal/
        sqrt(3), phi=2*Modelica.Constants.pi/3),
    each stateSelect=StateSelect.never) = terminal_n.phase[3].v
    "Voltage in line 3 at connector N";
  Modelica.Units.SI.Voltage v4_n[2](
    start=Buildings.Electrical.PhaseSystems.OnePhase.phaseVoltages(0),
    each stateSelect=StateSelect.never) = terminal_n.phase[4].v
    "Voltage in line 4 (neutral) at connector N";
  Modelica.Units.SI.Voltage v1_p[2](
    start=Buildings.Electrical.PhaseSystems.OnePhase.phaseVoltages(V_nominal/
        sqrt(3), phi=0),
    each stateSelect=StateSelect.never) = terminal_p.phase[1].v
    "Voltage in line 1 at connector P";
  Modelica.Units.SI.Voltage v2_p[2](
    start=Buildings.Electrical.PhaseSystems.OnePhase.phaseVoltages(V_nominal/
        sqrt(3), phi=-2*Modelica.Constants.pi/3),
    each stateSelect=StateSelect.never) = terminal_p.phase[2].v
    "Voltage in line 2 at connector P";
  Modelica.Units.SI.Voltage v3_p[2](
    start=Buildings.Electrical.PhaseSystems.OnePhase.phaseVoltages(V_nominal/
        sqrt(3), phi=2*Modelica.Constants.pi/3),
    each stateSelect=StateSelect.never) = terminal_p.phase[3].v
    "Voltage in line 3 at connector P";
  Modelica.Units.SI.Voltage v4_p[2](
    start=Buildings.Electrical.PhaseSystems.OnePhase.phaseVoltages(0),
    each stateSelect=StateSelect.never) = terminal_p.phase[4].v
    "Voltage in line 4 (neutral) at connector P";

protected
  function productAC1p = Buildings.Electrical.PhaseSystems.OnePhase.product
    "Product between complex quantities";
equation

  // Link the connectors to propagate the overdetermined variable
  for i in 1:4 loop
      Connections.branch(terminal_p.phase[i].theta, terminal_n.phase[i].theta);
      terminal_p.phase[i].theta = terminal_n.phase[i].theta;

      // No current losses, they are preserved in each line
      terminal_p.phase[i].i = - terminal_n.phase[i].i;
  end for;

  // Voltage drop caused by the impedance matrix
  v1_n - v1_p = productAC1p(Z11, i1) + productAC1p(Z12, i2) + productAC1p(Z13, i3)+ productAC1p(Z14, i4);
  v2_n - v2_p = productAC1p(Z21, i1) + productAC1p(Z22, i2) + productAC1p(Z23, i3)+ productAC1p(Z24, i4);
  v3_n - v3_p = productAC1p(Z31, i1) + productAC1p(Z32, i2) + productAC1p(Z33, i3)+ productAC1p(Z34, i4);
  v4_n - v4_p = productAC1p(Z41, i1) + productAC1p(Z42, i2) + productAC1p(Z43, i3)+ productAC1p(Z44, i4);
end TwoPortMatrixRL_N;


Model of a resistance with two electrical ports. Resistive model that connects two AC three-phase
unbalanced interfaces. This model can be used to represent a
cable in a three-phase unbalanced AC system.

within Buildings.Electrical.AC.ThreePhasesUnbalanced.Lines;
model TwoPortResistance "Model of a resistance with two electrical ports"
  extends Modelica.Electrical.Analog.Interfaces.ConditionalHeatPort;
  extends Buildings.Electrical.AC.ThreePhasesUnbalanced.Interfaces.TwoPort;
  parameter Modelica.Units.SI.Temperature T_ref=298.15 "Reference temperature"
  parameter Modelica.Units.SI.Temperature M=507.65
    "Temperature constant (R_actual = R*(M + T_heatPort)/(M + T_ref))"
  parameter Modelica.Units.SI.Resistance R "Resistance at temperature T_ref";
  OnePhase.Lines.TwoPortResistance  phase1(
    final T_ref=T_ref,
    final M=M,
    final R=R/3,
    final useHeatPort=useHeatPort) "Resistance line 1"
  OnePhase.Lines.TwoPortResistance phase2(
    final T_ref=T_ref,
    final M=M,
    final R=R/3,
    final useHeatPort=useHeatPort) "Resistance line 2"
  OnePhase.Lines.TwoPortResistance phase3(
    final T_ref=T_ref,
    final M=M,
    final R=R/3,
    final useHeatPort=useHeatPort) "Resistance line 3"
equation
  // Joule Losses
  LossPower = phase1.LossPower + phase2.LossPower + phase3.LossPower;
  connect(terminal_n.phase[1], phase1.terminal_n)
  connect(terminal_n.phase[2], phase2.terminal_n)
  connect(terminal_n.phase[3], phase3.terminal_n)
  connect(phase1.terminal_p, terminal_p.phase[1])
  connect(phase2.terminal_p, terminal_p.phase[2])
  connect(phase3.terminal_p, terminal_p.phase[3])

  connect(phase1.heatPort, heatPort)
  connect(phase3.heatPort, heatPort)
  connect(phase2.heatPort, heatPort)
end TwoPortResistance;


Model of a resistance with two electrical ports and neutral cable. Resistive model that connects two AC three-phase
unbalanced interfaces with neutral line. This model can be used to represent a
cable in a three-phase unbalanced AC system.

within Buildings.Electrical.AC.ThreePhasesUnbalanced.Lines;
model TwoPortResistance_N
  "Model of a resistance with two electrical ports and neutral cable"
  extends Modelica.Electrical.Analog.Interfaces.ConditionalHeatPort;
  extends Buildings.Electrical.AC.ThreePhasesUnbalanced.Interfaces.TwoPort_N;
  parameter Modelica.Units.SI.Temperature T_ref=298.15 "Reference temperature"
  parameter Modelica.Units.SI.Temperature M=507.65
    "Temperature constant (R_actual = R*(M + T_heatPort)/(M + T_ref))"
  parameter Modelica.Units.SI.Resistance R "Resistance at temperature T_ref";
  parameter Modelica.Units.SI.Resistance Rn
    "Resistance of neutral cable at temperature T_ref";
  OnePhase.Lines.TwoPortResistance  phase1(
    final T_ref=T_ref,
    final M=M,
    final R=R/3,
    final useHeatPort=useHeatPort) "Resistance line 1"
  OnePhase.Lines.TwoPortResistance phase2(
    final T_ref=T_ref,
    final M=M,
    final R=R/3,
    final useHeatPort=useHeatPort) "Resistance line 2"
  OnePhase.Lines.TwoPortResistance phase3(
    final T_ref=T_ref,
    final M=M,
    final R=R/3,
    final useHeatPort=useHeatPort) "Resistance line 3"
  OnePhase.Lines.TwoPortResistance neutral(
    final T_ref=T_ref,
    final M=M,
    final useHeatPort=useHeatPort,
    final R=Rn) "Resistance neutral cable"
equation
  // Joule Losses
  LossPower = phase1.LossPower + phase2.LossPower + phase3.LossPower + neutral.LossPower;
  connect(terminal_n.phase[1], phase1.terminal_n)
  connect(terminal_n.phase[2], phase2.terminal_n)
  connect(terminal_n.phase[3], phase3.terminal_n)
  connect(phase1.terminal_p, terminal_p.phase[1])
  connect(phase2.terminal_p, terminal_p.phase[2])
  connect(phase3.terminal_p, terminal_p.phase[3])

  connect(phase1.heatPort, heatPort)
  connect(phase3.heatPort, heatPort)
  connect(phase2.heatPort, heatPort)
  connect(neutral.heatPort, heatPort)
  connect(neutral.terminal_p, terminal_p.phase[4])
  connect(neutral.terminal_n, terminal_n.phase[4])
end TwoPortResistance_N;


Model of a resistive-inductive element with two electrical ports. Resistive-inductive model that connects two AC three-phase
unbalanced interfaces. This model can be used to represent a
cable in a three-phase unbalanced AC system.

within Buildings.Electrical.AC.ThreePhasesUnbalanced.Lines;
model TwoPortRL
  "Model of a resistive-inductive element with two electrical ports"
  extends Modelica.Electrical.Analog.Interfaces.ConditionalHeatPort;
  extends Buildings.Electrical.AC.ThreePhasesUnbalanced.Interfaces.TwoPort;
  parameter Modelica.Units.SI.Resistance R "Resistance at temperature T_ref"
  parameter Modelica.Units.SI.Temperature T_ref=298.15 "Reference temperature"
  parameter Modelica.Units.SI.Temperature M=507.65
    "Temperature constant (R_actual = R*(M + T_heatPort)/(M + T_ref))"
  parameter Modelica.Units.SI.Inductance L "Inductance";
  parameter Modelica.Units.SI.Current i1_start[2]={0,0}
    "Initial current phasor of phase 1 (positive if entering from terminal p)"
  parameter Modelica.Units.SI.Current i2_start[2]={0,0}
    "Initial current phasor of phase 2 (positive if entering from terminal p)"
  parameter Modelica.Units.SI.Current i3_start[2]={0,0}
    "Initial current phasor of phase 3 (positive if entering from terminal p)"
  parameter Buildings.Electrical.Types.Load mode(
    min=Buildings.Electrical.Types.Load.FixedZ_steady_state,
    max=Buildings.Electrical.Types.Load.FixedZ_dynamic) = Buildings.Electrical.Types.Load.FixedZ_steady_state
    "Type of model (e.g., steady state, dynamic, prescribed power consumption, etc.)"
  OnePhase.Lines.TwoPortRL phase1(
    final T_ref=T_ref,
    final M=M,
    final R=R/3,
    final L=L/3,
    final mode=mode,
    final useHeatPort=useHeatPort,
    i_start=i1_start) "Impedance line 1"
  OnePhase.Lines.TwoPortRL phase2(
    final T_ref=T_ref,
    final M=M,
    final R=R/3,
    final L=L/3,
    final mode=mode,
    final useHeatPort=useHeatPort,
    i_start=i2_start) "Impedance line 2"
  OnePhase.Lines.TwoPortRL phase3(
    final T_ref=T_ref,
    final M=M,
    final R=R/3,
    final L=L/3,
    final mode=mode,
    final useHeatPort=useHeatPort,
    i_start=i3_start) "Impedance line 3"
equation
  // Joule Losses
  LossPower = phase1.LossPower + phase2.LossPower + phase3.LossPower;

  connect(terminal_n.phase[1], phase1.terminal_n)
  connect(terminal_n.phase[2], phase2.terminal_n)
  connect(terminal_n.phase[3], phase3.terminal_n)
  connect(phase1.terminal_p, terminal_p.phase[1])
  connect(phase2.terminal_p, terminal_p.phase[2])
  connect(phase3.terminal_p, terminal_p.phase[3])

  connect(phase1.heatPort, heatPort)
  connect(phase3.heatPort, heatPort)
  connect(phase2.heatPort, heatPort)
end TwoPortRL;


Model of an RLC element with two electrical ports. RLC line model (T-model) that connects two AC three-phase
unbalanced interfaces. This model can be used to represent a
cable in a three-phase unbalanced AC system.

within Buildings.Electrical.AC.ThreePhasesUnbalanced.Lines;
model TwoPortRLC "Model of an RLC element with two electrical ports"
  extends Modelica.Electrical.Analog.Interfaces.ConditionalHeatPort;
  extends Buildings.Electrical.AC.ThreePhasesUnbalanced.Interfaces.TwoPort(
    terminal_p(phase(v(each nominal = V_nominal))),
    terminal_n(phase(v(each nominal = V_nominal))));

  parameter Modelica.Units.SI.Resistance R "Resistance at temperature T_ref";
  parameter Modelica.Units.SI.Capacitance C "Capacity";
  parameter Modelica.Units.SI.Inductance L "Inductance";
  parameter Modelica.Units.SI.Temperature T_ref=298.15 "Reference temperature"
  parameter Modelica.Units.SI.Temperature M=507.65
    "Temperature constant (R_actual = R*(M + T_heatPort)/(M + T_ref))"
  parameter Modelica.Units.SI.Voltage Vc1_start[2]=V_nominal/sqrt(3)*{1,0}
    "Initial voltage phasor of the capacitance located in the middle of phase 1"
  parameter Modelica.Units.SI.Voltage Vc2_start[2]=V_nominal/sqrt(3)*{-1/2,-
      sqrt(3)/2}
    "Initial voltage phasor of the capacitance located in the middle of phase 1"
  parameter Modelica.Units.SI.Voltage Vc3_start[2]=V_nominal/sqrt(3)*{-1/2,+
      sqrt(3)/2}
    "Initial voltage phasor of the capacitance located in the middle of phase 1"
  parameter Buildings.Electrical.Types.Load mode(
    min=Buildings.Electrical.Types.Load.FixedZ_steady_state,
    max=Buildings.Electrical.Types.Load.FixedZ_dynamic)=
    Buildings.Electrical.Types.Load.FixedZ_steady_state
    "Type of model (e.g., steady state, dynamic, prescribed power consumption, etc.)"
  parameter Modelica.Units.SI.Voltage V_nominal(min=0, start=480)
    "Nominal voltage (V_nominal >= 0)"
  OnePhase.Lines.TwoPortRLC phase1(
    final T_ref=T_ref,
    final M=M,
    final R=R/3,
    final L=L/3,
    final C=C/3,
    final mode=mode,
    final V_nominal = V_nominal/sqrt(3),
    final useHeatPort=useHeatPort,
    Vc_start=Vc1_start) "Impedance line 1"
  OnePhase.Lines.TwoPortRLC phase2(
    final T_ref=T_ref,
    final M=M,
    final R=R/3,
    final L=L/3,
    final C=C/3,
    final mode=mode,
    final V_nominal = V_nominal/sqrt(3),
    final useHeatPort=useHeatPort,
    Vc_start=Vc2_start) "Impedance line 2"
  OnePhase.Lines.TwoPortRLC phase3(
    final T_ref=T_ref,
    final M=M,
    final R=R/3,
    final L=L/3,
    final C=C/3,
    final mode=mode,
    final V_nominal = V_nominal/sqrt(3),
    final useHeatPort=useHeatPort,
    Vc_start=Vc3_start) "Impedance line 3"
equation
  // Joule Losses
  LossPower = phase1.LossPower + phase2.LossPower + phase3.LossPower;

  connect(terminal_n.phase[1], phase1.terminal_n)
  connect(terminal_n.phase[2], phase2.terminal_n)
  connect(terminal_n.phase[3], phase3.terminal_n)
  connect(phase1.terminal_p, terminal_p.phase[1])
  connect(phase2.terminal_p, terminal_p.phase[2])
  connect(phase3.terminal_p, terminal_p.phase[3])

  connect(phase1.heatPort, heatPort)
  connect(phase3.heatPort, heatPort)
  connect(phase2.heatPort, heatPort)
end TwoPortRLC;


Model of an RLC element with two electrical ports and neutral line cable. RLC line model (T-model) that connects two AC three-phase
unbalanced interfaces with neutral line. This model can be used to represent a
cable in a three-phase unbalanced AC system.

within Buildings.Electrical.AC.ThreePhasesUnbalanced.Lines;
model TwoPortRLC_N
  "Model of an RLC element with two electrical ports and neutral line cable"
  extends Modelica.Electrical.Analog.Interfaces.ConditionalHeatPort;
  extends Buildings.Electrical.AC.ThreePhasesUnbalanced.Interfaces.TwoPort_N(
    terminal_p(phase(v(each nominal = V_nominal))),
    terminal_n(phase(v(each nominal = V_nominal))));

  parameter Modelica.Units.SI.Resistance R "Resistance at temperature T_ref";
  parameter Modelica.Units.SI.Resistance Rn
    "Resistance of neutral cable at temperature T_ref";
  parameter Modelica.Units.SI.Capacitance C "Capacity";
  parameter Modelica.Units.SI.Capacitance Cn "Capacityof neutral cable";
  parameter Modelica.Units.SI.Inductance L "Inductance";
  parameter Modelica.Units.SI.Inductance Ln "Inductance of neutral cable";
  parameter Modelica.Units.SI.Temperature T_ref=298.15 "Reference temperature"
  parameter Modelica.Units.SI.Temperature M=507.65
    "Temperature constant (R_actual = R*(M + T_heatPort)/(M + T_ref))"
  parameter Modelica.Units.SI.Voltage Vc1_start[2]=V_nominal/sqrt(3)*{1,0}
    "Initial voltage phasor of the capacitance located in the middle of phase 1"
  parameter Modelica.Units.SI.Voltage Vc2_start[2]=V_nominal/sqrt(3)*{-1/2,-
      sqrt(3)/2}
    "Initial voltage phasor of the capacitance located in the middle of phase 1"
  parameter Modelica.Units.SI.Voltage Vc3_start[2]=V_nominal/sqrt(3)*{-1/2,+
      sqrt(3)/2}
    "Initial voltage phasor of the capacitance located in the middle of phase 1"
  parameter Buildings.Electrical.Types.Load mode(
    min=Buildings.Electrical.Types.Load.FixedZ_steady_state,
    max=Buildings.Electrical.Types.Load.FixedZ_dynamic)=
    Buildings.Electrical.Types.Load.FixedZ_steady_state
    "Type of model (e.g., steady state, dynamic, prescribed power consumption, etc.)"
  parameter Modelica.Units.SI.Voltage V_nominal(min=0, start=480)
    "Nominal voltage (V_nominal >= 0)"
  OnePhase.Lines.TwoPortRLC phase1(
    final T_ref=T_ref,
    final M=M,
    final R=R/3,
    final L=L/3,
    final C=C/3,
    final mode=mode,
    final V_nominal = V_nominal/sqrt(3),
    final useHeatPort=useHeatPort,
    Vc_start=Vc1_start) "Impedance line 1"
  OnePhase.Lines.TwoPortRLC phase2(
    final T_ref=T_ref,
    final M=M,
    final R=R/3,
    final L=L/3,
    final C=C/3,
    final mode=mode,
    final V_nominal = V_nominal/sqrt(3),
    final useHeatPort=useHeatPort,
    Vc_start=Vc2_start) "Impedance line 2"
  OnePhase.Lines.TwoPortRLC phase3(
    final T_ref=T_ref,
    final M=M,
    final R=R/3,
    final L=L/3,
    final C=C/3,
    final mode=mode,
    final V_nominal = V_nominal/sqrt(3),
    final useHeatPort=useHeatPort,
    Vc_start=Vc3_start) "Impedance line 3"
  OnePhase.Lines.TwoPortRLC neutral(
    final T_ref=T_ref,
    final M=M,
    final mode=mode,
    final V_nominal=V_nominal/sqrt(3),
    final useHeatPort=useHeatPort,
    final R=Rn,
    final C=Cn,
    final L=Ln,
    Vc_start=-Vc1_start - Vc2_start - Vc3_start) "Neutral line RLC model"
equation
  // Joule Losses
  LossPower = phase1.LossPower + phase2.LossPower + phase3.LossPower + neutral.LossPower;

  connect(terminal_n.phase[1], phase1.terminal_n)
  connect(terminal_n.phase[2], phase2.terminal_n)
  connect(terminal_n.phase[3], phase3.terminal_n)
  connect(phase1.terminal_p, terminal_p.phase[1])
  connect(phase2.terminal_p, terminal_p.phase[2])
  connect(phase3.terminal_p, terminal_p.phase[3])

  connect(phase1.heatPort, heatPort)
  connect(phase3.heatPort, heatPort)
  connect(phase2.heatPort, heatPort)
  connect(neutral.terminal_p, terminal_p.phase[4])
  connect(neutral.terminal_n, terminal_n.phase[4])
  connect(neutral.heatPort, heatPort)
end TwoPortRLC_N;


Model of a resistive-inductive element with two electrical ports and neutral line cable. Resistive-inductive model that connects two AC three-phase
unbalanced interfaces with neutral line. This model can be used to represent a
cable in a three-phase unbalanced AC system.

within Buildings.Electrical.AC.ThreePhasesUnbalanced.Lines;
model TwoPortRL_N
  "Model of a resistive-inductive element with two electrical ports and neutral line cable"
  extends Modelica.Electrical.Analog.Interfaces.ConditionalHeatPort;
  extends Buildings.Electrical.AC.ThreePhasesUnbalanced.Interfaces.TwoPort_N;
  parameter Modelica.Units.SI.Resistance R "Resistance at temperature T_ref";
  parameter Modelica.Units.SI.Resistance Rn
    "Resistance of neutral cable at temperature T_ref";
  parameter Modelica.Units.SI.Temperature T_ref=298.15 "Reference temperature"
  parameter Modelica.Units.SI.Temperature M=507.65
    "Temperature constant (R_actual = R*(M + T_heatPort)/(M + T_ref))"
  parameter Modelica.Units.SI.Inductance L "Inductance";
  parameter Modelica.Units.SI.Inductance Ln "Inductance of neutral cable";
  parameter Modelica.Units.SI.Current i1_start[2]={0,0}
    "Initial current phasor of phase 1 (positive if entering from terminal p)"
  parameter Modelica.Units.SI.Current i2_start[2]={0,0}
    "Initial current phasor of phase 2 (positive if entering from terminal p)"
  parameter Modelica.Units.SI.Current i3_start[2]={0,0}
    "Initial current phasor of phase 3 (positive if entering from terminal p)"
  parameter Buildings.Electrical.Types.Load mode(
    min=Buildings.Electrical.Types.Load.FixedZ_steady_state,
    max=Buildings.Electrical.Types.Load.FixedZ_dynamic) = Buildings.Electrical.Types.Load.FixedZ_steady_state
    "Type of model (e.g., steady state, dynamic, prescribed power consumption, etc.)"
  OnePhase.Lines.TwoPortRL  phase1(
    final T_ref=T_ref,
    final M=M,
    final R=R/3,
    final L=L/3,
    final mode=mode,
    final useHeatPort=useHeatPort,
    i_start=i1_start) "Impedance line 1"
  OnePhase.Lines.TwoPortRL phase2(
    final T_ref=T_ref,
    final M=M,
    final R=R/3,
    final L=L/3,
    final mode=mode,
    final useHeatPort=useHeatPort,
    i_start=i2_start) "Impedance line 2"
  OnePhase.Lines.TwoPortRL phase3(
    final T_ref=T_ref,
    final M=M,
    final R=R/3,
    final L=L/3,
    final mode=mode,
    final useHeatPort=useHeatPort,
    i_start=i3_start) "Impedance line 3"
  OnePhase.Lines.TwoPortRL neutral(
    final T_ref=T_ref,
    final M=M,
    final mode=mode,
    final useHeatPort=useHeatPort,
    final R=Rn,
    final L=Ln,
    i_start=-i1_start - i2_start - i3_start) "neutral cable RL model"
equation
  // Joule Losses
  LossPower = phase1.LossPower + phase2.LossPower + phase3.LossPower + neutral.LossPower;

  connect(terminal_n.phase[1], phase1.terminal_n)
  connect(terminal_n.phase[2], phase2.terminal_n)
  connect(terminal_n.phase[3], phase3.terminal_n)
  connect(phase1.terminal_p, terminal_p.phase[1])
  connect(phase2.terminal_p, terminal_p.phase[2])
  connect(phase3.terminal_p, terminal_p.phase[3])

  connect(phase1.heatPort, heatPort)
  connect(phase3.heatPort, heatPort)
  connect(phase2.heatPort, heatPort)
  connect(neutral.heatPort, heatPort)
  connect(neutral.terminal_p, terminal_p.phase[4])
  connect(neutral.terminal_n, terminal_n.phase[4])
end TwoPortRL_N;


Test model for a three-phase unbalanced commercial cable without neutral. This example demonstrates how to use a cable model without neutral line
to connect a source to a load.

within Buildings.Electrical.AC.ThreePhasesUnbalanced.Lines.Examples;
model ACLine
  "Test model for a three-phase unbalanced commercial cable without neutral"
  extends Modelica.Icons.Example;
  Sources.FixedVoltage E(
    definiteReference=true,
    f=60,
    V=480) "Voltage source"
  Loads.Impedance R1(R=10) "Resistive load 1"
  Loads.Impedance R2(R=10) "Resistive load 2"
  Loads.Impedance R3(R=10) "Resistive load 3"
  Line line_1(
    l=1000,
    P_nominal=2000,
    V_nominal=480,
    mode=Buildings.Electrical.Types.CableMode.commercial,
    redeclare Buildings.Electrical.Transmission.LowVoltageCables.Cu35
      commercialCable) "Cable that connects to load 1"
  Line line_2a(
    l=500,
    P_nominal=2000,
    V_nominal=480,
    mode=Buildings.Electrical.Types.CableMode.commercial,
    redeclare Buildings.Electrical.Transmission.LowVoltageCables.Cu35
      commercialCable) "Cable that connects to load 2"
  Line line_2b(
    l=500,
    P_nominal=2000,
    V_nominal=480,
    mode=Buildings.Electrical.Types.CableMode.commercial,
    redeclare Buildings.Electrical.Transmission.LowVoltageCables.Cu35
      commercialCable) "Cable that connects to load 2"
  Line line_3a(
    l=2000,
    P_nominal=1000,
    V_nominal=480,
    mode=Buildings.Electrical.Types.CableMode.commercial,
    redeclare Buildings.Electrical.Transmission.LowVoltageCables.Cu35
      commercialCable) "Cable that connects to load 3"
  Line line_3b(
    l=2000,
    P_nominal=1000,
    V_nominal=480,
    mode=Buildings.Electrical.Types.CableMode.commercial,
    redeclare Buildings.Electrical.Transmission.LowVoltageCables.Cu35
      commercialCable) "Cable that connects to load 3"
equation
  connect(E.terminal, line_1.terminal_n)
  connect(line_1.terminal_p, R1.terminal)
  connect(E.terminal, line_2a.terminal_n)
  connect(line_2a.terminal_p, line_2b.terminal_n)
  connect(line_2b.terminal_p, R2.terminal)
  connect(E.terminal, line_3a.terminal_n)
  connect(E.terminal, line_3b.terminal_n)
  connect(line_3a.terminal_p, R3.terminal)
  connect(line_3b.terminal_p, R3.terminal)
end ACLine;


Test model for a three-phase unbalanced inductive-resistive line specified by a Z matrix. This example demonstrates how to use an inductive resistive line model to connect
a source to a load. The model is parameterized using the impedance matrix <i>Z</i>.

within Buildings.Electrical.AC.ThreePhasesUnbalanced.Lines.Examples;
model ACLineMatrix_RL
  "Test model for a three-phase unbalanced inductive-resistive line specified by a Z matrix"
  extends Modelica.Icons.Example;
  Sources.FixedVoltage E(
    definiteReference=true,
    f=60,
    V=100*sqrt(3)) "Voltage source"
  Loads.Impedance sc_load1(R=0, L=0) "Short circuit load"
  Loads.Impedance sc_load2(R=0, L=0) "Short circuit load"
  Loads.Impedance sc_load3(R=0, L=0) "Short circuit load"
  Lines.TwoPortMatrixRL Rline_1(
    Z11={10,10},
    Z12={0,0},
    Z13={0,0},
    Z22={10,10},
    Z23={0,0},
    Z33={10,10},
    V_nominal=100*sqrt(3)) "RL line that connects to load 1"
  Lines.TwoPortMatrixRL Rline_2a(
    Z12={0,0},
    Z13={0,0},
    Z23={0,0},
    Z11=0.5*{10,10},
    Z22=0.5*{10,10},
    Z33=0.5*{10,10},
    V_nominal=100*sqrt(3)) "RL line that connects to load 2"
  Lines.TwoPortMatrixRL Rline_2b(
    Z12={0,0},
    Z13={0,0},
    Z23={0,0},
    Z11=0.5*{10,10},
    Z22=0.5*{10,10},
    Z33=0.5*{10,10},
    V_nominal=100*sqrt(3)) "RL line that connects to load 2"
  Lines.TwoPortMatrixRL Rline_3a(
    Z12={0,0},
    Z13={0,0},
    Z23={0,0},
    Z11=2*{10,10},
    Z22=2*{10,10},
    Z33=2*{10,10},
    V_nominal=100*sqrt(3)) "RL line that connects to load 3"
  Lines.TwoPortMatrixRL Rline_3b(
    Z12={0,0},
    Z13={0,0},
    Z23={0,0},
    Z11=2*{10,10},
    Z22=2*{10,10},
    Z33=2*{10,10},
    V_nominal=100*sqrt(3)) "RL line that connects to load 3"
equation
  connect(E.terminal, Rline_1.terminal_n)
  connect(Rline_1.terminal_p, sc_load1.terminal)
  connect(E.terminal, Rline_2a.terminal_n)
  connect(Rline_2a.terminal_p, Rline_2b.terminal_n)
  connect(Rline_2b.terminal_p, sc_load2.terminal)
  connect(E.terminal, Rline_3a.terminal_n)
  connect(E.terminal, Rline_3b.terminal_n)
  connect(Rline_3a.terminal_p, sc_load3.terminal)
  connect(Rline_3b.terminal_p, sc_load3.terminal)
end ACLineMatrix_RL;


Test model for a three-phase unbalanced RLC line specified by Z and B matrices. This example demonstrates how to use a RLC line model to connect
a source to a load. The model is parameterized using the impedance matrix Z
and the admittance matrix B.

within Buildings.Electrical.AC.ThreePhasesUnbalanced.Lines.Examples;
model ACLineMatrix_RLC
  "Test model for a three-phase unbalanced RLC line specified by Z and B matrices"
  extends Modelica.Icons.Example;
  Sources.FixedVoltage E(
    definiteReference=true,
    f=60,
    V=100*sqrt(3)) "Voltage source"
  Loads.Impedance sc_load1(R=0, L=0) "Short circuit load"
  Lines.TwoPortMatrixRLC Rline_1(
    Z11={10,10},
    Z12={0,0},
    Z13={0,0},
    Z22={10,10},
    Z23={0,0},
    Z33={10,10},
    V_nominal=100*sqrt(3),
    B12=0,
    B13=0,
    B23=0,
    B11=10,
    B22=10,
    B33=10) "RL line that connects to load 1"
  Lines.TwoPortMatrixRLC Rline_2(
    Z12={0,0},
    Z13={0,0},
    Z23={0,0},
    V_nominal=100*sqrt(3),
    B12=0,
    B13=0,
    B23=0,
    Z11={0,0},
    Z22={0,0},
    Z33={0,0},
    B11=0.1,
    B22=0.1,
    B33=0.1) "RL line that connects to load 2"
equation
  connect(E.terminal, Rline_1.terminal_n)
  connect(Rline_1.terminal_p, sc_load1.terminal)
  connect(E.terminal, Rline_2.terminal_n)
end ACLineMatrix_RLC;


Test model for a three-phase unbalanced RLC line with neutral cable specified by Z and B matrices. This example demonstrates how to use a RLC line model with neutral line to connect
a source to a load. The model is parameterized using the impedance matrix Z
and the admittance matrix B.

within Buildings.Electrical.AC.ThreePhasesUnbalanced.Lines.Examples;
model ACLineMatrix_RLC_N
  "Test model for a three-phase unbalanced RLC line with neutral cable specified by Z and B matrices"
  extends Modelica.Icons.Example;
  Sources.FixedVoltage_N E(
    definiteReference=true,
    f=60,
    V=100*sqrt(3)) "Voltage source"
  Loads.Impedance_N sc_load1(R=0, L=0) "Short circuit load"
  Lines.TwoPortMatrixRLC_N Rline_1(
    Z11={10,10},
    Z12={0,0},
    Z13={0,0},
    Z22={10,10},
    Z23={0,0},
    Z33={10,10},
    V_nominal=100*sqrt(3),
    B12=0,
    B13=0,
    B23=0,
    B11=10,
    B22=10,
    B33=10,
    Z14={0,0},
    Z24={0,0},
    Z34={0,0},
    Z44={10,10},
    B14=0,
    B24=0,
    B34=0,
    B44=10) "RL line that connects to load 1"
  Lines.TwoPortMatrixRLC_N Rline_2(
    Z12={0,0},
    Z13={0,0},
    Z23={0,0},
    V_nominal=100*sqrt(3),
    B12=0,
    B13=0,
    B23=0,
    Z11={0,0},
    Z22={0,0},
    Z33={0,0},
    B11=0.1,
    B22=0.1,
    B33=0.1,
    Z14={0,0},
    Z24={0,0},
    B14=0,
    B24=0,
    B34=0,
    B44=0.1,
    Z34={0,0},
    Z44={0,0}) "RL line that connects to load 2"
equation
  connect(E.terminal, Rline_1.terminal_n)
  connect(E.terminal, Rline_2.terminal_n)
  connect(Rline_1.terminal_p, sc_load1.terminal)
end ACLineMatrix_RLC_N;


Test model for a three-phase unbalanced inductive-resistive line with neutral cable specified by a Z matrix. This example demonstrates how to use an inductive resistive line model with neutral line to connect
a source to a load. The model is parameterized using the impedance matrix <i>Z</i>.

within Buildings.Electrical.AC.ThreePhasesUnbalanced.Lines.Examples;
model ACLineMatrix_RL_N
  "Test model for a three-phase unbalanced inductive-resistive line with neutral cable specified by a Z matrix"
  extends Modelica.Icons.Example;
  Sources.FixedVoltage_N E(
    definiteReference=true,
    f=60,
    V=100*sqrt(3)) "Voltage source"
  Loads.Impedance_N sc_load1(R=0, L=0) "Short circuit load"
  Loads.Impedance_N sc_load2(R=0, L=0) "Short circuit load"
  Loads.Impedance_N sc_load3(R=0, L=0) "Short circuit load"
  Lines.TwoPortMatrixRL_N Rline_1(
    Z11={10,10},
    Z12={0,0},
    Z13={0,0},
    Z22={10,10},
    Z23={0,0},
    Z33={10,10},
    V_nominal=100*sqrt(3),
    Z14={0,0},
    Z24={0,0},
    Z34={0,0},
    Z44={10,10}) "RL line that connects to load 1"
  Lines.TwoPortMatrixRL_N Rline_2a(
    Z12={0,0},
    Z13={0,0},
    Z23={0,0},
    Z11=0.5*{10,10},
    Z22=0.5*{10,10},
    Z33=0.5*{10,10},
    V_nominal=100*sqrt(3),
    Z14={0,0},
    Z24={0,0},
    Z34={0,0},
    Z44=0.5*{10,10}) "RL line that connects to load 2"
  Lines.TwoPortMatrixRL_N Rline_2b(
    Z12={0,0},
    Z13={0,0},
    Z23={0,0},
    Z11=0.5*{10,10},
    Z22=0.5*{10,10},
    Z33=0.5*{10,10},
    V_nominal=100*sqrt(3),
    Z14={0,0},
    Z24={0,0},
    Z34={0,0},
    Z44=0.5*{10,10}) "RL line that connects to load 2"
  Lines.TwoPortMatrixRL_N Rline_3a(
    Z12={0,0},
    Z13={0,0},
    Z23={0,0},
    Z11=2*{10,10},
    Z22=2*{10,10},
    Z33=2*{10,10},
    V_nominal=100*sqrt(3),
    Z14={0,0},
    Z24={0,0},
    Z34={0,0},
    Z44=2*{10,10}) "RL line that connects to load 3"
  Lines.TwoPortMatrixRL_N Rline_3b(
    Z12={0,0},
    Z13={0,0},
    Z23={0,0},
    Z11=2*{10,10},
    Z22=2*{10,10},
    Z33=2*{10,10},
    V_nominal=100*sqrt(3),
    Z14={0,0},
    Z24={0,0},
    Z34={0,0},
    Z44=2*{10,10}) "RL line that connects to load 3"
equation
  connect(E.terminal, Rline_1.terminal_n)
  connect(E.terminal, Rline_2a.terminal_n)
  connect(E.terminal, Rline_3a.terminal_n)
  connect(E.terminal, Rline_3b.terminal_n)
  connect(Rline_3b.terminal_p, sc_load3.terminal)
  connect(Rline_3a.terminal_p, sc_load3.terminal)
  connect(Rline_2b.terminal_p, sc_load2.terminal)
  connect(Rline_1.terminal_p, sc_load1.terminal)
  connect(Rline_2a.terminal_p, Rline_2b.terminal_n)
end ACLineMatrix_RL_N;


Test model for a three-phase unbalanced inductive line. This example demonstrates how to use a purely inductive line model to connect
a source to a load.

within Buildings.Electrical.AC.ThreePhasesUnbalanced.Lines.Examples;
model ACLine_L "Test model for a three-phase unbalanced inductive line"
  extends Modelica.Icons.Example;
  parameter Modelica.Units.SI.Inductance Lbase=10/2/Modelica.Constants.pi/60
    "Base value for the line inductances";
  Sources.FixedVoltage E(
    definiteReference=true,
    f=60,
    V=480) "Voltage source"
  Loads.Impedance L1(R=0, L=Lbase) "Inductive load 1"
  Loads.Impedance L2(R=0, L=Lbase) "Inductive load 2"
  Loads.Impedance L3(R=0, L=Lbase) "Inductive load 3"
  Loads.Impedance sc_load(R=0) "Short circuit load"
  Lines.TwoPortInductance Lline_sc(L=6*Lbase)
    "Inductive line that connects to the short circuit"
  Lines.TwoPortInductance Rline_1(L=3*Lbase)
    "Inductive line that connects to load 1"
  Lines.TwoPortInductance Rline_2a(L=3*Lbase/2)
    "Inductive line that connects to load 2"
  Lines.TwoPortInductance Rline_2b(L=3*Lbase/2)
    "Inductive line that connects to load 2"
  Lines.TwoPortInductance Rline_3a(L=6*Lbase)
    "Inductive line that connects to load 3"
  Lines.TwoPortInductance Rline_3b(L=6*Lbase)
    "Inductive line that connects to load 3"
equation
  connect(E.terminal,Lline_sc. terminal_n)
  connect(Lline_sc.terminal_p, sc_load.terminal)
  connect(E.terminal, Rline_1.terminal_n)
  connect(Rline_1.terminal_p,L1. terminal)
  connect(E.terminal, Rline_2a.terminal_n)
  connect(Rline_2a.terminal_p, Rline_2b.terminal_n)
  connect(Rline_2b.terminal_p,L2. terminal)
  connect(E.terminal, Rline_3a.terminal_n)
  connect(E.terminal, Rline_3b.terminal_n)
  connect(Rline_3a.terminal_p,L3. terminal)
  connect(Rline_3b.terminal_p,L3. terminal)
end ACLine_L;


Test model for a three-phase unbalanced inductive line with neutral cable. This example demonstrates how to use a purely inductive line model with neutral cable to connect
a source to a load.

within Buildings.Electrical.AC.ThreePhasesUnbalanced.Lines.Examples;
model ACLine_L_N
  "Test model for a three-phase unbalanced inductive line with neutral cable"
  extends Modelica.Icons.Example;
  parameter Modelica.Units.SI.Inductance Lbase=10/2/Modelica.Constants.pi/60
    "Base value for the line inductances";
  Sources.FixedVoltage_N E(
    definiteReference=true,
    f=60,
    V=480) "Voltage source"
  Loads.Impedance_N L1(R=0, L=Lbase) "Inductive load 1"
  Loads.Impedance_N L2(R=0, L=Lbase) "Inductive load 2"
  Loads.Impedance_N L3(R=0, L=Lbase) "Inductive load 3"
  Loads.Impedance_N sc_load(R=0) "Short circuit load"
  Lines.TwoPortInductance_N Lline_sc(L=6*Lbase, Ln=Lbase)
    "Inductive line that connects to the short circuit"
  Lines.TwoPortInductance_N Rline_1(L=3*Lbase, Ln=0.5*Lbase)
    "Inductive line that connects to load 1"
  Lines.TwoPortInductance_N Rline_2a(L=3*Lbase/2, Ln=0.5*Lbase/2)
    "Inductive line that connects to load 2"
  Lines.TwoPortInductance_N Rline_2b(L=3*Lbase/2, Ln=0.5*Lbase/2)
    "Inductive line that connects to load 2"
  Lines.TwoPortInductance_N Rline_3a(L=6*Lbase, Ln=Lbase)
    "Inductive line that connects to load 3"
  Lines.TwoPortInductance_N Rline_3b(L=6*Lbase, Ln=Lbase)
    "Inductive line that connects to load 3"
equation
  connect(E.terminal, Lline_sc.terminal_n)
  connect(E.terminal, Rline_1.terminal_n)
  connect(E.terminal, Rline_2a.terminal_n)
  connect(E.terminal, Rline_3a.terminal_n)
  connect(E.terminal, Rline_3b.terminal_n)
  connect(Rline_3b.terminal_p, L3.terminal)
  connect(Rline_3a.terminal_p, L3.terminal)
  connect(Rline_2b.terminal_p, L2.terminal)
  connect(Rline_2a.terminal_p, Rline_2b.terminal_n)
  connect(Rline_1.terminal_p, L1.terminal)
  connect(Lline_sc.terminal_p, sc_load.terminal)
end ACLine_L_N;


Test model for a three-phase unbalanced commercial cable with neutral. This example demonstrates how to use a cable line model with
neutral to connect a source to a load.

within Buildings.Electrical.AC.ThreePhasesUnbalanced.Lines.Examples;
model ACLine_N
  "Test model for a three-phase unbalanced commercial cable with neutral"
  extends Modelica.Icons.Example;
  Sources.FixedVoltage_N E(
    definiteReference=true,
    f=60,
    V=480) "Voltage source"
  Loads.Impedance_N R1(R=10) "Resistive load 1"
  Loads.Impedance_N R2(R=10) "Resistive load 2"
  Loads.Impedance_N R3(R=10) "Resistive load 3"
  Line_N line_1(
    l=1000,
    P_nominal=2000,
    V_nominal=480,
    mode=Buildings.Electrical.Types.CableMode.commercial,
    redeclare Buildings.Electrical.Transmission.LowVoltageCables.Cu35
      commercialCable) "Cable that connects to load 1"
  Line_N line_2a(
    l=500,
    P_nominal=2000,
    V_nominal=480,
    mode=Buildings.Electrical.Types.CableMode.commercial,
    redeclare Buildings.Electrical.Transmission.LowVoltageCables.Cu35
      commercialCable) "Cable that connects to load 2"
  Line_N line_2b(
    l=500,
    P_nominal=2000,
    V_nominal=480,
    mode=Buildings.Electrical.Types.CableMode.commercial,
    redeclare Buildings.Electrical.Transmission.LowVoltageCables.Cu35
      commercialCable) "Cable that connects to load 2"
  Line_N line_3a(
    l=2000,
    P_nominal=1000,
    V_nominal=480,
    mode=Buildings.Electrical.Types.CableMode.commercial,
    redeclare Buildings.Electrical.Transmission.LowVoltageCables.Cu35
      commercialCable) "Cable that connects to load 3"
  Line_N line_3b(
    l=2000,
    P_nominal=1000,
    V_nominal=480,
    mode=Buildings.Electrical.Types.CableMode.commercial,
    redeclare Buildings.Electrical.Transmission.LowVoltageCables.Cu35
      commercialCable) "Cable that connects to load 3"
equation
  connect(E.terminal, line_2a.terminal_n)
  connect(E.terminal, line_1.terminal_n)
  connect(line_1.terminal_p, R1.terminal)
  connect(E.terminal, line_3a.terminal_n)
  connect(E.terminal, line_3b.terminal_n)
  connect(line_2a.terminal_p, line_2b.terminal_n)
  connect(line_3a.terminal_p, R3.terminal)
  connect(line_3b.terminal_p, R3.terminal)
  connect(line_2b.terminal_p, R2.terminal)
end ACLine_N;


Test model for a three-phase unbalanced resistive line. This example demonstrates how to use a resistive line model to connect
a source to a load.

within Buildings.Electrical.AC.ThreePhasesUnbalanced.Lines.Examples;
model ACLine_R "Test model for a three-phase unbalanced resistive line"
  extends Modelica.Icons.Example;
  Sources.FixedVoltage E(
    definiteReference=true,
    f=60,
    V=480) "Voltage source"
  Loads.Impedance R1(R=10) "Resistive load 1"
  Loads.Impedance R2(R=10) "Resistive load 2"
  Loads.Impedance R3(R=10) "Resistive load 3"
  Loads.Impedance sc_load(R=0) "Short circuit load"
  Lines.TwoPortResistance Rline_sc(      useHeatPort=false, R=60)
    "Resistive line that connects to the short circuit"
  Lines.TwoPortResistance Rline_1(R=30)
    "Resistive line that connects to load 1"
  Lines.TwoPortResistance Rline_2a(R=15)
    "Resistive line that connects to load 2"
  Lines.TwoPortResistance Rline_2b(R=15)
    "Resistive line that connects to load 2"
  Lines.TwoPortResistance Rline_3a(R=60)
    "Resistive line that connects to load 3"
  Lines.TwoPortResistance Rline_3b(R=60)
    "Resistive line that connects to load 3"
equation
  connect(E.terminal, Rline_sc.terminal_n)
  connect(Rline_sc.terminal_p, sc_load.terminal)
  connect(E.terminal, Rline_1.terminal_n)
  connect(Rline_1.terminal_p, R1.terminal)
  connect(E.terminal, Rline_2a.terminal_n)
  connect(Rline_2a.terminal_p, Rline_2b.terminal_n)
  connect(Rline_2b.terminal_p, R2.terminal)
  connect(E.terminal, Rline_3a.terminal_n)
  connect(E.terminal, Rline_3b.terminal_n)
  connect(Rline_3a.terminal_p, R3.terminal)
  connect(Rline_3b.terminal_p, R3.terminal)
end ACLine_R;


Test model for a three-phase unbalanced inductive-resistive line. This example demonstrates how to use a resistive-inductive line model to connect
a source to a load.

within Buildings.Electrical.AC.ThreePhasesUnbalanced.Lines.Examples;
model ACLine_RL
  "Test model for a three-phase unbalanced inductive-resistive line"
  extends Modelica.Icons.Example;
  parameter Modelica.Units.SI.Resistance Rbase=3*10
    "Base value for the line resistance";
  parameter Modelica.Units.SI.Inductance Lbase=Rbase/2/Modelica.Constants.pi/60
    "Base value for the line inductance";
  Sources.FixedVoltage E(
    definiteReference=true,
    f=60,
    V=100*sqrt(3)) "Voltage source"
  Loads.Impedance load_sc_1(R=0) "Short circuit 1"
  Loads.Impedance load_sc_2(R=0) "Short circuit 2"
  Lines.TwoPortRL RL_2(
    R=Rbase,
    L=Lbase) "Resistive-Inductive line connected to short circuit 2"
  Lines.TwoPortResistance R_1(R=Rbase)
    "Resistance line connected to short circuit 1"
  Lines.TwoPortInductance L_1(L=Lbase)
    "Inductance line connected to short circuit 1"
  Lines.TwoPortRL RL_3(
    R=Rbase,
    L=Lbase,
    mode=Buildings.Electrical.Types.Load.FixedZ_dynamic)
    "Dynamic resistive-inductive line connected to short circuit 3"
  Loads.Impedance load_sc_3(R=0) "Short circuit 3"
equation
  connect(E.terminal, R_1.terminal_n)
  connect(R_1.terminal_p, L_1.terminal_n)
  connect(L_1.terminal_p, load_sc_1.terminal)
  connect(E.terminal, RL_2.terminal_n)
  connect(RL_2.terminal_p, load_sc_2.terminal)
  connect(E.terminal, RL_3.terminal_n)
  connect(RL_3.terminal_p, load_sc_3.terminal)
end ACLine_RL;


Test model for a three-phase unbalanced RLC line. This example demonstrates how to use an RLC line model to connect
a source to a load.

within Buildings.Electrical.AC.ThreePhasesUnbalanced.Lines.Examples;
model ACLine_RLC "Test model for a three-phase unbalanced RLC line"
  extends Modelica.Icons.Example;
  parameter Modelica.Units.SI.Resistance RBase=3*10
    "Base value for the line resistance";
  parameter Modelica.Units.SI.Inductance LBase=RBase/(2*Modelica.Constants.pi*
      60) "Base value for the line inductances";
  parameter Modelica.Units.SI.Capacitance CBase=3*0.1/(2*Modelica.Constants.pi*
      60) "Base value for the line inductances";
  Sources.FixedVoltage E(
    definiteReference=true,
    f=60,
    V=100*sqrt(3)) "Voltage source"
  Loads.Impedance sc_load1(R=0, L=0) "Short circuit load"
  Loads.Impedance sc_load2(R=0, L=0) "Short circuit load"
  Loads.Impedance sc_load3(R=0, L=0) "Short circuit load"
  Loads.Impedance sc_load(R=0, L=0) "Short circuit load"
  Lines.TwoPortRLC RLCLine_sc(
    R=RBase,
    C=CBase,
    L=LBase,
    mode=Buildings.Electrical.Types.Load.FixedZ_dynamic,
    V_nominal=480) "RLC line that connects to the short circuit"
  Lines.TwoPortRLC RLCLine_1(
    R=RBase,
    C=CBase,
    L=LBase,
    V_nominal=480) "RLC line that connects to load 1"
  Lines.TwoPortRLC RLCLine_2a(
    V_nominal=480,
    R=RBase/2,
    L=LBase/2,
    C=CBase/2) "RLC line that connects to load 2"
  Lines.TwoPortRLC RLCLine_2b(
    V_nominal=480,
    R=RBase/2,
    L=LBase/2,
    C=CBase/2) "RLC line that connects to load 2"
  Lines.TwoPortRLC RLCLine_3a(
    R=2*RBase,
    L=2*LBase,
    V_nominal=480,
    C=CBase/2) "RLC line that connects to load 3"
  Lines.TwoPortRLC RLCLine_3b(
    R=2*RBase,
    L=2*LBase,
    V_nominal=480,
    C=CBase/2) "RLC line that connects to load 3"
equation
  connect(E.terminal, RLCLine_sc.terminal_n)
  connect(RLCLine_sc.terminal_p, sc_load.terminal)
  connect(E.terminal, RLCLine_1.terminal_n)
  connect(RLCLine_1.terminal_p, sc_load1.terminal)
  connect(E.terminal, RLCLine_2a.terminal_n)
  connect(RLCLine_2a.terminal_p, RLCLine_2b.terminal_n)
  connect(RLCLine_2b.terminal_p, sc_load2.terminal)
  connect(E.terminal, RLCLine_3a.terminal_n)
  connect(E.terminal, RLCLine_3b.terminal_n)
  connect(RLCLine_3a.terminal_p, sc_load3.terminal)
  connect(RLCLine_3b.terminal_p, sc_load3.terminal)
end ACLine_RLC;


Test model for a three-phase unbalanced RLC line with neutral cable. This example demonstrates how to use an RLC line model with neutral cable to connect
a source to a load.

within Buildings.Electrical.AC.ThreePhasesUnbalanced.Lines.Examples;
model ACLine_RLC_N
  "Test model for a three-phase unbalanced RLC line with neutral cable"
  extends Modelica.Icons.Example;
  parameter Modelica.Units.SI.Resistance RBase=3*10
    "Base value for the line resistance";
  parameter Modelica.Units.SI.Inductance LBase=RBase/(2*Modelica.Constants.pi*
      60) "Base value for the line inductances";
  parameter Modelica.Units.SI.Capacitance CBase=3*0.1/(2*Modelica.Constants.pi*
      60) "Base value for the line inductances";
  Sources.FixedVoltage_N E(
    definiteReference=true,
    f=60,
    V=100*sqrt(3)) "Voltage source"
  Loads.Impedance_N sc_load1(R=0, L=0) "Short circuit load"
  Loads.Impedance_N sc_load2(R=0, L=0) "Short circuit load"
  Loads.Impedance_N sc_load3(R=0, L=0) "Short circuit load"
  Loads.Impedance_N sc_load(R=0, L=0) "Short circuit load"
  Lines.TwoPortRLC_N RLCLine_sc(
    R=RBase,
    C=CBase,
    L=LBase,
    mode=Buildings.Electrical.Types.Load.FixedZ_dynamic,
    V_nominal=480,
    Rn=RBase,
    Cn=CBase,
    Ln=LBase) "RLC line that connects to the short circuit"
  Lines.TwoPortRLC_N RLCLine_1(
    R=RBase,
    C=CBase,
    L=LBase,
    V_nominal=480,
    Rn=RBase,
    Cn=CBase,
    Ln=LBase) "RLC line that connects to load 1"
  Lines.TwoPortRLC_N RLCLine_2a(
    V_nominal=480,
    R=RBase/2,
    L=LBase/2,
    C=CBase/2,
    Rn=RBase/2,
    Cn=CBase/2,
    Ln=LBase/2) "RLC line that connects to load 2"
  Lines.TwoPortRLC_N RLCLine_2b(
    V_nominal=480,
    R=RBase/2,
    L=LBase/2,
    C=CBase/2,
    Rn=RBase/2,
    Cn=CBase/2,
    Ln=LBase/2) "RLC line that connects to load 2"
  Lines.TwoPortRLC_N RLCLine_3a(
    R=2*RBase,
    L=2*LBase,
    V_nominal=480,
    C=CBase/2,
    Rn=2*RBase,
    Cn=CBase/2,
    Ln=2*LBase) "RLC line that connects to load 3"
  Lines.TwoPortRLC_N RLCLine_3b(
    R=2*RBase,
    L=2*LBase,
    V_nominal=480,
    C=CBase/2,
    Rn=2*RBase,
    Cn=CBase/2,
    Ln=2*LBase) "RLC line that connects to load 3"
equation
  connect(RLCLine_2a.terminal_p, RLCLine_2b.terminal_n)
  connect(RLCLine_2b.terminal_p, sc_load2.terminal)
  connect(RLCLine_3a.terminal_p, sc_load3.terminal)
  connect(RLCLine_3b.terminal_p, sc_load3.terminal)
  connect(RLCLine_1.terminal_p, sc_load1.terminal)
  connect(RLCLine_sc.terminal_p, sc_load.terminal)
  connect(E.terminal, RLCLine_sc.terminal_n)
  connect(E.terminal, RLCLine_1.terminal_n)
  connect(E.terminal, RLCLine_2a.terminal_n)
  connect(E.terminal, RLCLine_3a.terminal_n)
  connect(E.terminal, RLCLine_3b.terminal_n)
end ACLine_RLC_N;


Test model for a three-phase unbalanced inductive-resistive line with neutral cable. This example demonstrates how to use a resistive-inductive line model with neutral cable to connect
a source to a load.

within Buildings.Electrical.AC.ThreePhasesUnbalanced.Lines.Examples;
model ACLine_RL_N
  "Test model for a three-phase unbalanced inductive-resistive line with neutral cable"
  extends Modelica.Icons.Example;
  parameter Modelica.Units.SI.Resistance Rbase=3*10
    "Base value for the line resistance";
  parameter Modelica.Units.SI.Inductance Lbase=Rbase/2/Modelica.Constants.pi/60
    "Base value for the line inductance";
  Sources.FixedVoltage_N E(
    definiteReference=true,
    f=60,
    V=100*sqrt(3)) "Voltage source"
  Loads.Impedance_N load_sc_1(R=0) "Short circuit 1"
  Loads.Impedance_N load_sc_2(R=0) "Short circuit 2"
  Lines.TwoPortRL_N RL_2(
    R=Rbase,
    L=Lbase,
    Rn=0.5*Rbase,
    Ln=0.5*Lbase) "Resistive-Inductive line connected to short circuit 2"
  Lines.TwoPortResistance_N R_1(R=Rbase, Rn=0.5*Rbase)
    "Resistance line connected to short circuit 1"
  Lines.TwoPortInductance_N L_1(L=Lbase, Ln=0.5*Lbase)
    "Inductance line connected to short circuit 1"
equation
  connect(RL_2.terminal_p, load_sc_2.terminal)
  connect(L_1.terminal_p, load_sc_1.terminal)
  connect(L_1.terminal_n, R_1.terminal_p)
  connect(R_1.terminal_n, E.terminal)
  connect(E.terminal, RL_2.terminal_n)
end ACLine_RL_N;


Test model for a three-phase unbalanced resistive line with neutral cable. This example demonstrates how to use a resistive line model with neutral cable to connect
a source to a load.

within Buildings.Electrical.AC.ThreePhasesUnbalanced.Lines.Examples;
model ACLine_R_N
  "Test model for a three-phase unbalanced resistive line with neutral cable"
  extends Modelica.Icons.Example;
  Sources.FixedVoltage_N E(
    definiteReference=true,
    f=60,
    V=480) "Voltage source"
  Loads.Impedance_N R1(R=10) "Resistive load 1"
  Loads.Impedance_N R2(R=10) "Resistive load 2"
  Loads.Impedance_N R3(R=10) "Resistive load 3"
  Loads.Impedance_N sc_load(R=0) "Short circuit load"
  Lines.TwoPortResistance_N Rline_sc(      useHeatPort=false, R=60,
    Rn=20) "Resistive line that connects to the short circuit"
  Lines.TwoPortResistance_N Rline_1(R=30, Rn=10)
    "Resistive line that connects to load 1"
  Lines.TwoPortResistance_N Rline_2a(R=15, Rn=5)
    "Resistive line that connects to load 2"
  Lines.TwoPortResistance_N Rline_2b(R=15, Rn=5)
    "Resistive line that connects to load 2"
  Lines.TwoPortResistance_N Rline_3a(R=60, Rn=20)
    "Resistive line that connects to load 3"
  Lines.TwoPortResistance_N Rline_3b(R=60, Rn=20)
    "Resistive line that connects to load 3"
equation
  connect(Rline_sc.terminal_p, sc_load.terminal)
  connect(Rline_1.terminal_p, R1.terminal)
  connect(Rline_2b.terminal_p, R2.terminal)
  connect(Rline_2a.terminal_p, Rline_2b.terminal_n)
  connect(Rline_3b.terminal_p, R3.terminal)
  connect(Rline_3a.terminal_p, R3.terminal)
  connect(E.terminal, Rline_sc.terminal_n)
  connect(E.terminal, Rline_1.terminal_n)
  connect(E.terminal, Rline_2a.terminal_n)
  connect(E.terminal, Rline_3a.terminal_n)
  connect(E.terminal, Rline_3b.terminal_n)
end ACLine_R_N;


Test model for a network model for three-phase unbalanced systems without neutral cable. This example demonstrates how to use a network model to connect
a source to a load. In this simple case the network has two nodes
that are connected by a commercial cable without a neutral line.

within Buildings.Electrical.AC.ThreePhasesUnbalanced.Lines.Examples;
model ACSimpleGrid
  "Test model for a network model for three-phase unbalanced systems without neutral cable"
  extends Modelica.Icons.Example;
  Network network(
    redeclare Buildings.Electrical.Transmission.Grids.TestGrid2Nodes grid,
    V_nominal=480)
    "Network model that represents the connection between the source and the load"
  Loads.Inductive load(
    V_nominal=480,
    mode=Buildings.Electrical.Types.Load.VariableZ_P_input,
    plugPhase3=false) "Load connected to the network"
  Sources.FixedVoltage E(f=60, V=480,
    definiteReference=true) "Voltage source"
  Modelica.Blocks.Sources.Ramp load_inputs(
    height=5000,
    offset=-2000,
    duration=0.5,
    startTime=0.25) "Input signal for the power consumption of the loads"
equation
  connect(E.terminal, network.terminal[1])
  connect(load.terminal, network.terminal[2])
  connect(load_inputs.y, load.Pow1)
  connect(load_inputs.y, load.Pow2)
end ACSimpleGrid;


Test model for a network model for three-phase unbalanced systems with neutral cable. This example demonstrates how to use a network model to connect
a source to a load. In this simple case the network has two nodes
that are connected by a commercial cable with neutral line.

within Buildings.Electrical.AC.ThreePhasesUnbalanced.Lines.Examples;
model ACSimpleGrid_N
  "Test model for a network model for three-phase unbalanced systems with neutral cable"
  extends Modelica.Icons.Example;
  Network_N network(
    redeclare Buildings.Electrical.Transmission.Grids.TestGrid2Nodes grid,
    V_nominal=480)
    "Network model that represents the connection between the source and the load"
  Loads.Inductive_N load(
    V_nominal=480,
    mode=Buildings.Electrical.Types.Load.VariableZ_P_input,
    plugPhase3=false) "Load connected to the network"
  Sources.FixedVoltage_N E(
    f=60,
    V=480,
    definiteReference=true) "Voltage source"
  Modelica.Blocks.Sources.Ramp load_inputs(
    height=5000,
    offset=-2000,
    duration=0.5,
    startTime=0.25) "Input signal for the power consumption of the loads"
equation
  connect(E.terminal, network.terminal[1])
  connect(load.terminal, network.terminal[2])
  connect(load_inputs.y, load.Pow1)
  connect(load_inputs.y, load.Pow2)
end ACSimpleGrid_N;


Package with example models

within Buildings.Electrical.AC.ThreePhasesUnbalanced.Lines;
package Examples "Package with example models"
  extends Modelica.Icons.ExamplesPackage;
end Examples;


Model of a three-phase unbalanced capacitive load without neutral cable. This model represents a three-phase unbalanced capacitive load.
The model extends from

Buildings.Electrical.AC.ThreePhasesUnbalanced.Loads.BaseClasses.LoadCtrl
and uses the load model from the package

Buildings.Electrical.AC.OnePhase.Loads.
The model computes the voltages, currents and powers on each phase.

within Buildings.Electrical.AC.ThreePhasesUnbalanced.Loads;
model Capacitive
  "Model of a three-phase unbalanced capacitive load without neutral cable"
  extends BaseClasses.LoadCtrl(
    redeclare Buildings.Electrical.AC.OnePhase.Loads.Capacitive load1(pf=pf,
        use_pf_in=use_pf_in),
    redeclare Buildings.Electrical.AC.OnePhase.Loads.Capacitive load2(pf=pf,
        use_pf_in=use_pf_in),
    redeclare Buildings.Electrical.AC.OnePhase.Loads.Capacitive load3(pf=pf,
        use_pf_in=use_pf_in));
  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_1(
    min=0,
    max=1,
    unit="1") if (use_pf_in and plugPhase1) "Power factor of load on phase 1"
  Modelica.Blocks.Interfaces.RealInput pf_in_2(
    min=0,
    max=1,
    unit="1") if (use_pf_in and plugPhase2) "Power factor of load on phase 2"
  Modelica.Blocks.Interfaces.RealInput pf_in_3(
    min=0,
    max=1,
    unit="1") if (use_pf_in and plugPhase3) "Power factor of load on phase 3"
equation
  connect(pf_in_1, load1.pf_in)
  connect(pf_in_2, load2.pf_in)
  connect(pf_in_3, load3.pf_in)
end Capacitive;


Model of a three-phase unbalanced capacitive load with neutral cable. This model represents a three-phase unbalanced capacitive load.
The model extends from

Buildings.Electrical.AC.ThreePhasesUnbalanced.Loads.BaseClasses.LoadCtrl_N
and uses the load model from the package

Buildings.Electrical.AC.OnePhase.Loads.
The model computes the voltages, currents and powers on each phase.

within Buildings.Electrical.AC.ThreePhasesUnbalanced.Loads;
model Capacitive_N
  "Model of a three-phase unbalanced capacitive load with neutral cable"
  extends BaseClasses.LoadCtrl_N(
    redeclare Buildings.Electrical.AC.OnePhase.Loads.Capacitive load1(pf=pf,
        use_pf_in=use_pf_in),
    redeclare Buildings.Electrical.AC.OnePhase.Loads.Capacitive load2(pf=pf,
        use_pf_in=use_pf_in),
    redeclare Buildings.Electrical.AC.OnePhase.Loads.Capacitive load3(pf=pf,
        use_pf_in=use_pf_in));
  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_1(
    min=0,
    max=1,
    unit="1") if (use_pf_in and plugPhase1) "Power factor of load on phase 1"
  Modelica.Blocks.Interfaces.RealInput pf_in_2(
    min=0,
    max=1,
    unit="1") if (use_pf_in and plugPhase2) "Power factor of load on phase 2"
  Modelica.Blocks.Interfaces.RealInput pf_in_3(
    min=0,
    max=1,
    unit="1") if (use_pf_in and plugPhase3) "Power factor of load on phase 3"
equation
  connect(pf_in_1, load1.pf_in)
  connect(pf_in_2, load2.pf_in)
  connect(pf_in_3, load3.pf_in)
end Capacitive_N;


Model of a three-phase unbalanced impedance without neutral cable. This model represents a three-phase unbalanced impedance without neutral cable.
The model extends from

Buildings.Electrical.AC.ThreePhasesUnbalanced.Loads.BaseClasses.Impedance
and uses the load model from the package

Buildings.Electrical.AC.OnePhase.Loads.Impedance.
The model computes the voltages, currents and powers on each phase.

within Buildings.Electrical.AC.ThreePhasesUnbalanced.Loads;
model Impedance
  "Model of a three-phase unbalanced impedance without neutral cable"
  extends BaseClasses.Impedance(
    redeclare Buildings.Electrical.AC.OnePhase.Loads.Impedance load1,
    redeclare Buildings.Electrical.AC.OnePhase.Loads.Impedance load2,
    redeclare Buildings.Electrical.AC.OnePhase.Loads.Impedance load3);
equation
end Impedance;


Model of a three-phase unbalanced impedance with neutral cable. This model represents a three-phase unbalanced impedance with neutral cable.
The current in the neutral cable is computed as the algebraic sum of the currents
of the loads.
The model extends from

Buildings.Electrical.AC.ThreePhasesUnbalanced.Loads.BaseClasses.Impedance_N
and uses the load model from the package

Buildings.Electrical.AC.OnePhase.Loads.Impedance.
The model computes the voltages, currents and powers on each phase.

within Buildings.Electrical.AC.ThreePhasesUnbalanced.Loads;
model Impedance_N
  "Model of a three-phase unbalanced impedance with neutral cable"
  extends BaseClasses.Impedance_N(
    redeclare Buildings.Electrical.AC.OnePhase.Loads.Impedance load1,
    redeclare Buildings.Electrical.AC.OnePhase.Loads.Impedance load2,
    redeclare Buildings.Electrical.AC.OnePhase.Loads.Impedance load3);
equation
end Impedance_N;


Model of a three-phase unbalanced inductive load without neutral cable. This model represents a three-phase unbalanced inductive load.
The model extends from

Buildings.Electrical.AC.ThreePhasesUnbalanced.Loads.BaseClasses.LoadCtrl
and uses the load model from the package

Buildings.Electrical.AC.OnePhase.Loads.
The model computes the voltages, currents and powers on each phase.

within Buildings.Electrical.AC.ThreePhasesUnbalanced.Loads;
model Inductive
  "Model of a three-phase unbalanced inductive load without neutral cable"
  extends BaseClasses.LoadCtrl(
    redeclare Buildings.Electrical.AC.OnePhase.Loads.Inductive load1(
      pf=pf,
      use_pf_in=use_pf_in),
    redeclare Buildings.Electrical.AC.OnePhase.Loads.Inductive load2(
      pf=pf,
      use_pf_in=use_pf_in),
    redeclare Buildings.Electrical.AC.OnePhase.Loads.Inductive load3(
      pf=pf,
      use_pf_in=use_pf_in));
  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_1(
    min=0,
    max=1,
    unit="1") if (use_pf_in and plugPhase1) "Power factor of load on phase 1"
  Modelica.Blocks.Interfaces.RealInput pf_in_2(
    min=0,
    max=1,
    unit="1") if (use_pf_in and plugPhase2) "Power factor of load on phase 2"
  Modelica.Blocks.Interfaces.RealInput pf_in_3(
    min=0,
    max=1,
    unit="1") if (use_pf_in and plugPhase3) "Power factor of load on phase 3"

equation
  connect(pf_in_1, load1.pf_in)
  connect(pf_in_2, load2.pf_in)
  connect(pf_in_3, load3.pf_in)
end Inductive;


Model of a three-phase unbalanced inductive load with neutral cable. This model represents a three-phase unbalanced inductive load.
The model extends from

Buildings.Electrical.AC.ThreePhasesUnbalanced.Loads.BaseClasses.LoadCtrl_N
and uses the load model from the package

Buildings.Electrical.AC.OnePhase.Loads.
The model computes the voltages, currents and powers on each phase.

within Buildings.Electrical.AC.ThreePhasesUnbalanced.Loads;
model Inductive_N
  "Model of a three-phase unbalanced inductive load with neutral cable"
  extends BaseClasses.LoadCtrl_N(
    redeclare Buildings.Electrical.AC.OnePhase.Loads.Inductive load1(pf=pf,
        use_pf_in=use_pf_in),
    redeclare Buildings.Electrical.AC.OnePhase.Loads.Inductive load2(pf=pf,
        use_pf_in=use_pf_in),
    redeclare Buildings.Electrical.AC.OnePhase.Loads.Inductive load3(pf=pf,
        use_pf_in=use_pf_in));
  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_1(
    min=0,
    max=1,
    unit="1") if (use_pf_in and plugPhase1) "Power factor of load on phase 1"
  Modelica.Blocks.Interfaces.RealInput pf_in_2(
    min=0,
    max=1,
    unit="1") if (use_pf_in and plugPhase2) "Power factor of load on phase 2"
  Modelica.Blocks.Interfaces.RealInput pf_in_3(
    min=0,
    max=1,
    unit="1") if (use_pf_in and plugPhase3) "Power factor of load on phase 3"
equation
  connect(pf_in_1, load1.pf_in)
  connect(pf_in_2, load2.pf_in)
  connect(pf_in_3, load3.pf_in)
end Inductive_N;


within Buildings.Electrical.AC.ThreePhasesUnbalanced;
package Loads "Package with load models for three-phase unbalanced AC systems"
  extends Modelica.Icons.VariantsPackage;
end Loads;


Model of a three-phase unbalanced resistive load without neutral cable. This model represents a three-phase unbalanced resistive load.
The model extends from

Buildings.Electrical.AC.ThreePhasesUnbalanced.Loads.BaseClasses.LoadCtrl
and uses the load model from the package

Buildings.Electrical.AC.OnePhase.Loads.
The model computes the voltages, currents and powers on each phase.

within Buildings.Electrical.AC.ThreePhasesUnbalanced.Loads;
model Resistive
  "Model of a three-phase unbalanced resistive load without neutral cable"
  extends BaseClasses.LoadCtrl(
    redeclare Buildings.Electrical.AC.OnePhase.Loads.Resistive load1,
    redeclare Buildings.Electrical.AC.OnePhase.Loads.Resistive load2,
    redeclare Buildings.Electrical.AC.OnePhase.Loads.Resistive load3);
equation
end Resistive;


Model of a three-phase unbalanced resistive load with neutral cable. This model represents a three-phase unbalanced resistive load.
The model extends from

Buildings.Electrical.AC.ThreePhasesUnbalanced.Loads.BaseClasses.LoadCtrl_N
and uses the load model from the package

Buildings.Electrical.AC.OnePhase.Loads.
The model computes the voltages, currents and powers on each phase.

within Buildings.Electrical.AC.ThreePhasesUnbalanced.Loads;
model Resistive_N
  "Model of a three-phase unbalanced resistive load with neutral cable"
  extends BaseClasses.LoadCtrl_N(
    redeclare Buildings.Electrical.AC.OnePhase.Loads.Resistive load1,
    redeclare Buildings.Electrical.AC.OnePhase.Loads.Resistive load2,
    redeclare Buildings.Electrical.AC.OnePhase.Loads.Resistive load3);
equation
end Resistive_N;


Partial model of a three-phase unbalanced impedance. This model represents a partial interface for a three-phase AC
unbalanced impedance.

within Buildings.Electrical.AC.ThreePhasesUnbalanced.Loads.BaseClasses;
partial model BaseImpedance
  "Partial model of a three-phase unbalanced impedance"
  extends Buildings.Electrical.Interfaces.PartialPluggableUnbalanced;
  replaceable Buildings.Electrical.AC.OnePhase.Loads.Impedance
    load1(
    inductive=inductive,
    R=R,
    L=L,
    C=C,
    use_R_in=use_R_in,
    RMin=RMin,
    RMax=RMax,
    use_C_in=use_C_in,
    CMin=CMin,
    CMax=CMax,
    use_L_in=use_L_in,
    LMin=LMin,
    LMax=LMax)
    if plugPhase1 "Load 1"
  replaceable Buildings.Electrical.AC.OnePhase.Loads.Impedance
    load2(
    inductive=inductive,
    R=R,
    L=L,
    C=C,
    use_R_in=use_R_in,
    RMin=RMin,
    RMax=RMax,
    use_C_in=use_C_in,
    CMin=CMin,
    CMax=CMax,
    use_L_in=use_L_in,
    LMin=LMin,
    LMax=LMax)
    if plugPhase2 "Load 2"
  replaceable Buildings.Electrical.AC.OnePhase.Loads.Impedance
    load3(
    inductive=inductive,
    R=R,
    L=L,
    C=C,
    use_R_in=use_R_in,
    RMin=RMin,
    RMax=RMax,
    use_C_in=use_C_in,
    CMin=CMin,
    CMax=CMax,
    use_L_in=use_L_in,
    LMin=LMin,
    LMax=LMax)
    if plugPhase3 "Load 3"
  parameter Buildings.Electrical.Types.LoadConnection loadConn=
    Buildings.Electrical.Types.LoadConnection.wye_to_wyeg
    "Type of load connection (Yg or D)";
  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 sepecifies variable R"
  Modelica.Blocks.Interfaces.RealInput y_C(min=0, max=1) if use_C_in
    "Input that sepecifies variable C"
  Modelica.Blocks.Interfaces.RealInput y_L(min=0, max=1) if use_L_in
    "Input that sepecifies variable L"

  Buildings.Electrical.AC.ThreePhasesUnbalanced.Interfaces.WyeToDelta
    wyeToDelta if (loadConn == Buildings.Electrical.Types.LoadConnection.wye_to_delta)
    "Wye to delta load connection"
  Buildings.Electrical.AC.ThreePhasesUnbalanced.Interfaces.WyeToWyeGround
    wyeToWyeGround if (loadConn == Buildings.Electrical.Types.LoadConnection.wye_to_wyeg)
    "Wye to grounded wye connection"
protected
  Interfaces.Adapter3to3 adaDel
    if (loadConn == Buildings.Electrical.Types.LoadConnection.wye_to_delta)
    "Adapter"
  Interfaces.Adapter3to3 adaWye
    if (loadConn == Buildings.Electrical.Types.LoadConnection.wye_to_wyeg)
    "Adapter"
equation

  // Conditional connections to load 1
  if plugPhase1 then
    if use_R_in then
      connect(y_R, load1.y_R)
    end if;
    if use_C_in then
      connect(y_C, load1.y_C)
    end if;
    if use_L_in then
      connect(y_L, load1.y_L)
    end if;
  end if;
  // Conditional connections to load 2
  if plugPhase2 then
    if use_R_in then
      connect(y_R, load2.y_R)
    end if;
    if use_L_in then
      connect(y_L, load2.y_L)
    end if;
    if use_C_in then
      connect(y_C, load2.y_C)
    end if;
  end if;

  // Conditional connections to load 3
  if plugPhase3 then
    if use_R_in then
      connect(y_R, load3.y_R)
    end if;
    if use_C_in then
      connect(y_C, load3.y_C)
    end if;
    if use_L_in then
      connect(y_L, load3.y_L)
    end if;
  end if;

  // Connection of the single loads to the 3phases connector
  if plugPhase1 then
    connect(load1.terminal, adaDel.terminals[1])
    connect(load1.terminal, adaWye.terminals[1])
  end if;

  if plugPhase2 then
    connect(load2.terminal, adaDel.terminals[2])
    connect(load2.terminal, adaWye.terminals[2])
  end if;
  if plugPhase3 then
    connect(load3.terminal, adaDel.terminals[3])
    connect(load3.terminal, adaWye.terminals[3])
  end if;

  connect(adaDel.terminal, wyeToDelta.delta)
  connect(adaWye.terminal, wyeToWyeGround.wyeg)
end BaseImpedance;