This example illustrates how using a variable power source. This example shows how to use a variable generator model. The generator model has to be used
together with a voltage source generator, which is in this example the
grid model.

within AixLib.Electrical.AC.ThreePhasesBalanced.Sources.Examples;
model VariablePowerSource
  "This example illustrates how using a variable power source"
  extends Modelica.Icons.Example;
  AixLib.Electrical.AC.ThreePhasesBalanced.Sources.Generator generator(f=60,
      phiGen=0.26179938779915) "AC generator model"
  Modelica.Blocks.Sources.Sine generation(
    offset=200,
    startTime=1,
    amplitude=100,
    f=0.05) "Generated power"
  AixLib.Electrical.AC.ThreePhasesBalanced.Loads.Inductive RL(mode=Types.Load.VariableZ_y_input,
      P_nominal=-300,
    V_nominal=480) "Load model"
  AixLib.Electrical.AC.ThreePhasesBalanced.Sources.Grid grid(f=60, V=480)
    "AC one phase electrical grid"
  Modelica.Blocks.Sources.Trapezoid load(
    rising=2,
    width=3,
    falling=3,
    period=10,
    startTime=1,
    amplitude=0.8,
    offset=0.2) "Power consumption profile"
equation
  connect(generation.y, generator.P)
  connect(generator.terminal, RL.terminal)
  connect(grid.terminal, RL.terminal)
  connect(load.y, RL.y)
end VariablePowerSource;


Three phases unbalanced AC systems

within AixLib.Electrical.AC;
package ThreePhasesUnbalanced "Three phases unbalanced AC systems"
  extends Modelica.Icons.VariantsPackage;
end ThreePhasesUnbalanced;


AC AC converter single phase systems (YY). This is an AC AC converter, based on a power balance between both circuit sides.
The parameter <i>conversionFactor</i> defines the ratio between the RMS voltages

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion;
model ACACConverter "AC AC converter single phase systems (YY)"
  extends
    AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.BaseClasses.PartialConverterYY(
    redeclare AixLib.Electrical.AC.OnePhase.Conversion.ACACConverter conv1(
      conversionFactor=conversionFactor,
      eta=eta,
      ground_1=ground_1,
      ground_2=ground_2),
    redeclare AixLib.Electrical.AC.OnePhase.Conversion.ACACConverter conv2(
      conversionFactor=conversionFactor,
      eta=eta,
      ground_1=ground_1,
      ground_2=ground_2),
    redeclare AixLib.Electrical.AC.OnePhase.Conversion.ACACConverter conv3(
      conversionFactor=conversionFactor,
      eta=eta,
      ground_1=ground_1,
      ground_2=ground_2));
  parameter Real conversionFactor
    "Ratio of QS rms voltage on side 2 / QS rms voltage on side 1";
  parameter Modelica.Units.SI.Efficiency eta(max=1)
    "Converter efficiency, pLoss = (1-eta) * Ptr";
  parameter Boolean ground_1 = false "Connect side 1 of converter to ground"
  parameter Boolean ground_2 = true "Connect side 2 of converter to ground"
end ACACConverter;


AC AC transformer simplified equivalent circuit (YY). This is a simplified equivalent transformer model.
The model accounts for winding Joule losses and leakage reactances
that are represented by a series of a resistance R and an
inductance L. The resistance and the inductance represent both the
effects of the secondary and primary side of the transformer.

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion;
model ACACTransformer "AC AC transformer simplified equivalent circuit (YY)"
  extends
    AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.BaseClasses.PartialConverterYY(
    redeclare AixLib.Electrical.AC.OnePhase.Conversion.ACACTransformer conv1(
      VHigh=VHigh/sqrt(3),
      VLow=VLow/sqrt(3),
      XoverR=XoverR,
      Zperc=Zperc,
      ground_1=ground_1,
      ground_2=ground_2,
      VABase=VABase/3,
      phi_1=0),
    redeclare AixLib.Electrical.AC.OnePhase.Conversion.ACACTransformer conv2(
      VHigh=VHigh/sqrt(3),
      VLow=VLow/sqrt(3),
      XoverR=XoverR,
      Zperc=Zperc,
      ground_1=ground_1,
      ground_2=ground_2,
      VABase=VABase/3,
      phi_1=-2.0943951023932),
    redeclare AixLib.Electrical.AC.OnePhase.Conversion.ACACTransformer conv3(
      VHigh=VHigh/sqrt(3),
      VLow=VLow/sqrt(3),
      XoverR=XoverR,
      Zperc=Zperc,
      ground_1=ground_1,
      ground_2=ground_2,
      VABase=VABase/3,
      phi_1=2.0943951023932));
  parameter Modelica.Units.SI.Voltage VHigh
    "Rms voltage on side 1 of the transformer (primary side)";
  parameter Modelica.Units.SI.Voltage VLow
    "Rms voltage on side 2 of the transformer (secondary side)";
  parameter Modelica.Units.SI.ApparentPower VABase
    "Nominal power of the transformer";
  parameter Real XoverR
    "Ratio between the complex and real components of the impedance (XL/R)";
  parameter Real Zperc "Short circuit impedance";
  parameter Boolean ground_1 = false "Connect side 1 of transformer to ground"
  parameter Boolean ground_2 = true "Connect side 2 of transformer to ground"
equation
end ACACTransformer;


AC AC transformer simplified equivalent circuit (DD). This is a simplified equivalent transformer model with Delta-Delta connection.
The model accounts for winding Joule losses and leakage reactances
that are represented by a series of a resistance R and an
inductance L. The resistance and the inductance represent the
effects of the secondary and primary side of the transformer.

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion;
model ACACTransformerDD "AC AC transformer simplified equivalent circuit (DD)"
  extends
    AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.BaseClasses.PartialConverterDD(
    redeclare AixLib.Electrical.AC.OnePhase.Conversion.ACACTransformer conv1(
      XoverR=XoverR,
      Zperc=Zperc,
      ground_1=ground_1,
      ground_2=ground_2,
      VABase=VABase/3,
      VHigh=VHigh,
      VLow=VLow,
      phi_1=0.5235987755983),
    redeclare AixLib.Electrical.AC.OnePhase.Conversion.ACACTransformer conv2(
      XoverR=XoverR,
      Zperc=Zperc,
      ground_1=ground_1,
      ground_2=ground_2,
      VABase=VABase/3,
      VHigh=VHigh,
      VLow=VLow,
      phi_1=-1.5707963267949),
    redeclare AixLib.Electrical.AC.OnePhase.Conversion.ACACTransformer conv3(
      XoverR=XoverR,
      Zperc=Zperc,
      ground_1=ground_1,
      ground_2=ground_2,
      VABase=VABase/3,
      VHigh=VHigh,
      VLow=VLow,
      phi_1=2.6179938779915));
  parameter Modelica.Units.SI.Voltage VHigh
    "Rms voltage on side 1 of the transformer (primary side)";
  parameter Modelica.Units.SI.Voltage VLow
    "Rms voltage on side 2 of the transformer (secondary side)";
  parameter Modelica.Units.SI.ApparentPower VABase
    "Nominal power of the transformer";
  parameter Real XoverR
    "Ratio between the complex and real components of the impedance (XL/R)";
  parameter Real Zperc "Short circuit impedance";
  parameter Boolean ground_1 = false "Connect side 1 of transformer to ground"
  parameter Boolean ground_2 = true "Connect side 2 of transformer to ground"
equation
end ACACTransformerDD;


AC AC transformer detailed equivalent circuit (YY). This is a detailed transformer model that takes into accounts the winding Joule losses,
and the leakage reactances on the primary and secondary side. The model also takes into account
the core or iron losses and the losses due to magnetization effects.

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion;
model ACACTransformerFull "AC AC transformer detailed equivalent circuit (YY)"
  extends
    AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.BaseClasses.PartialConverterYY(
    redeclare AixLib.Electrical.AC.OnePhase.Conversion.ACACTransformerFull conv1(
      VHigh=VHigh/sqrt(3),
      VLow=VLow/sqrt(3),
      f = f,
      VABase=VABase,
      R1=R1,L1=L1,R2=R2,L2=L2,
      magEffects=magEffects,
      Rm=Rm,Lm=Lm,
      ground_1=ground_1,
      ground_2=ground_2),
    redeclare AixLib.Electrical.AC.OnePhase.Conversion.ACACTransformerFull conv2(
      VHigh=VHigh/sqrt(3),
      VLow=VLow/sqrt(3),
      f = f,
      VABase=VABase,
      R1=R1,L1=L1,R2=R2,L2=L2,
      magEffects=magEffects,
      Rm=Rm,Lm=Lm,
      ground_1=ground_1,
      ground_2=ground_2),
    redeclare AixLib.Electrical.AC.OnePhase.Conversion.ACACTransformerFull conv3(
      VHigh=VHigh/sqrt(3),
      VLow=VLow/sqrt(3),
      f = f,
      VABase=VABase,
      R1=R1,L1=L1,R2=R2,L2=L2,
      magEffects=magEffects,
      Rm=Rm,Lm=Lm,
      ground_1=ground_1,
      ground_2=ground_2));

  parameter Modelica.Units.SI.Voltage VHigh
    "Rms voltage on side 1 of the transformer (primary side)";
  parameter Modelica.Units.SI.Voltage VLow
    "Rms voltage on side 2 of the transformer (secondary side)";
  parameter Modelica.Units.SI.ApparentPower VABase
    "Nominal power of the transformer";
  parameter Modelica.Units.SI.Frequency f(start=60) "Nominal frequency";
  parameter AixLib.Electrical.Types.PerUnit R1(min=0)
    "Resistance on side 1 of the transformer (pu)";
  parameter AixLib.Electrical.Types.PerUnit L1(min=0)
    "Inductance on side 1 of the transformer (pu)";
  parameter AixLib.Electrical.Types.PerUnit R2(min=0)
    "Resistance on side 2 of the transformer (pu)";
  parameter AixLib.Electrical.Types.PerUnit L2(min=0)
    "Inductance on side 2 of the transformer (pu)";
  parameter Boolean magEffects = false
    "If =true introduce magnetization effects"
  parameter AixLib.Electrical.Types.PerUnit Rm(min=0,start=0)
    "Magnetization resistance (pu)"
  parameter AixLib.Electrical.Types.PerUnit Lm(min=0,start=0)
    "Magnetization inductance (pu)"
  parameter Boolean ground_1 = false "Connect side 1 of converter to ground"
  parameter Boolean ground_2 = true "Connect side 2 of converter to ground"
end ACACTransformerFull;


AC AC transformer simplified equivalent circuit (DY step down). This is a simplified equivalent transformer model with Y-Delta connection
(voltage step down).
The model accounts for winding Joule losses and leakage reactances
that are represented by a series of a resistance R and an
inductance L. The resistance and the inductance represent the
effects of the secondary and primary side of the transformer.

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion;
model ACACTransformerStepDownDY
  "AC AC transformer simplified equivalent circuit (DY step down)"
  extends
    AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.BaseClasses.PartialConverterStepDownDY(
    redeclare AixLib.Electrical.AC.OnePhase.Conversion.ACACTransformer conv1(
      VHigh=VHigh,
      XoverR=XoverR,
      Zperc=Zperc,
      ground_1=ground_1,
      ground_2=ground_2,
      VABase=VABase/3,
      VLow=-VLow/sqrt(3),
      phi_1=2.6179938779915),
    redeclare AixLib.Electrical.AC.OnePhase.Conversion.ACACTransformer conv2(
      VHigh=VHigh,
      XoverR=XoverR,
      Zperc=Zperc,
      ground_1=ground_1,
      ground_2=ground_2,
      VABase=VABase/3,
      VLow=-VLow/sqrt(3),
      phi_1=0.5235987755983),
    redeclare AixLib.Electrical.AC.OnePhase.Conversion.ACACTransformer conv3(
      VHigh=VHigh,
      XoverR=XoverR,
      Zperc=Zperc,
      ground_1=ground_1,
      ground_2=ground_2,
      VABase=VABase/3,
      VLow=-VLow/sqrt(3),
      phi_1=-1.5707963267949));
  parameter Modelica.Units.SI.Voltage VHigh
    "Rms voltage on side 1 of the transformer (primary side)";
  parameter Modelica.Units.SI.Voltage VLow
    "Rms voltage on side 2 of the transformer (secondary side)";
  parameter Modelica.Units.SI.ApparentPower VABase
    "Nominal power of the transformer";
  parameter Real XoverR
    "Ratio between the complex and real components of the impedance (XL/R)";
  parameter Real Zperc "Short circuit impedance";
  parameter Boolean ground_1 = false "Connect side 1 of transformer to ground"
  parameter Boolean ground_2 = true "Connect side 2 of transformer to ground"
equation
end ACACTransformerStepDownDY;


AC AC transformer simplified equivalent circuit (YD step down). This is a simplified equivalent transformer model with Y-Delta connection
(voltage step down).
The model accounts for winding Joule losses and leakage reactances
that are represented by a series of a resistance R and an
inductance L. The resistance and the inductance represent the
effects of the secondary and primary side of the transformer.

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion;
model ACACTransformerStepDownYD
  "AC AC transformer simplified equivalent circuit (YD step down)"
  extends
    AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.BaseClasses.PartialConverterStepDownYD(
    redeclare AixLib.Electrical.AC.OnePhase.Conversion.ACACTransformer conv1(
      VHigh=VHigh/sqrt(3),
      XoverR=XoverR,
      Zperc=Zperc,
      ground_1=ground_1,
      ground_2=ground_2,
      VABase=VABase/3,
      VLow=VLow,
      phi_1=0),
    redeclare AixLib.Electrical.AC.OnePhase.Conversion.ACACTransformer conv2(
      VHigh=VHigh/sqrt(3),
      XoverR=XoverR,
      Zperc=Zperc,
      ground_1=ground_1,
      ground_2=ground_2,
      VABase=VABase/3,
      VLow=VLow,
      phi_1=-2.0943951023932),
    redeclare AixLib.Electrical.AC.OnePhase.Conversion.ACACTransformer conv3(
      VHigh=VHigh/sqrt(3),
      XoverR=XoverR,
      Zperc=Zperc,
      ground_1=ground_1,
      ground_2=ground_2,
      VABase=VABase/3,
      VLow=VLow,
      phi_1=2.0943951023932));
  parameter Modelica.Units.SI.Voltage VHigh
    "Rms voltage on side 1 of the transformer (primary side)";
  parameter Modelica.Units.SI.Voltage VLow
    "Rms voltage on side 2 of the transformer (secondary side)";
  parameter Modelica.Units.SI.ApparentPower VABase
    "Nominal power of the transformer";
  parameter Real XoverR
    "Ratio between the complex and real components of the impedance (XL/R)";
  parameter Real Zperc "Short circuit impedance";
  parameter Boolean ground_1 = false "Connect side 1 of transformer to ground"
  parameter Boolean ground_2 = true "Connect side 2 of transformer to ground"
equation
end ACACTransformerStepDownYD;


AC AC transformer simplified equivalent circuit (DY step up). This is a simplified equivalent transformer model with Delta-Y connection
(voltage step up).
The model accounts for winding Joule losses and leakage reactances
that are represented by a series of a resistance R and an
inductance L. The resistance and the inductance represent the
effects of the secondary and primary side of the transformer.

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion;
model ACACTransformerStepUpDY
  "AC AC transformer simplified equivalent circuit (DY step up)"
  extends
    AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.BaseClasses.PartialConverterStepUpDY(
    redeclare AixLib.Electrical.AC.OnePhase.Conversion.ACACTransformer conv1(
      VHigh=VHigh,
      XoverR=XoverR,
      Zperc=Zperc,
      ground_1=ground_1,
      ground_2=ground_2,
      VABase=VABase/3,
      VLow=VLow/sqrt(3),
      phi_1=0.5235987755983),
    redeclare AixLib.Electrical.AC.OnePhase.Conversion.ACACTransformer conv2(
      VHigh=VHigh,
      XoverR=XoverR,
      Zperc=Zperc,
      ground_1=ground_1,
      ground_2=ground_2,
      VABase=VABase/3,
      VLow=VLow/sqrt(3),
      phi_1=-1.5707963267949),
    redeclare AixLib.Electrical.AC.OnePhase.Conversion.ACACTransformer conv3(
      VHigh=VHigh,
      XoverR=XoverR,
      Zperc=Zperc,
      ground_1=ground_1,
      ground_2=ground_2,
      VABase=VABase/3,
      VLow=VLow/sqrt(3),
      phi_1=2.6179938779915));
  parameter Modelica.Units.SI.Voltage VHigh
    "Rms voltage on side 1 of the transformer (primary side)";
  parameter Modelica.Units.SI.Voltage VLow
    "Rms voltage on side 2 of the transformer (secondary side)";
  parameter Modelica.Units.SI.ApparentPower VABase
    "Nominal power of the transformer";
  parameter Real XoverR
    "Ratio between the complex and real components of the impedance (XL/R)";
  parameter Real Zperc "Short circuit impedance";
  parameter Boolean ground_1 = false "Connect side 1 of transformer to ground"
  parameter Boolean ground_2 = true "Connect side 2 of transformer to ground"
equation
end ACACTransformerStepUpDY;


AC AC transformer simplified equivalent circuit (YD step up). This is a simplified equivalent transformer model with Y-Delta connection
(voltage step up).
The model accounts for winding Joule losses and leakage reactances
that are represented by a series of a resistance R and an
inductance L. The resistance and the inductance represent the
effects of the secondary and primary side of the transformer.

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion;
model ACACTransformerStepUpYD
  "AC AC transformer simplified equivalent circuit (YD step up)"
  extends
    AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.BaseClasses.PartialConverterStepUpYD(
    redeclare AixLib.Electrical.AC.OnePhase.Conversion.ACACTransformer conv1(
      VHigh=VHigh/sqrt(3),
      XoverR=XoverR,
      Zperc=Zperc,
      ground_1=ground_1,
      ground_2=ground_2,
      VABase=VABase/3,
      VLow=-VLow,
      phi_1=0),
    redeclare AixLib.Electrical.AC.OnePhase.Conversion.ACACTransformer conv2(
      VHigh=VHigh/sqrt(3),
      XoverR=XoverR,
      Zperc=Zperc,
      ground_1=ground_1,
      ground_2=ground_2,
      VABase=VABase/3,
      VLow=-VLow,
      phi_1=-2.0943951023932),
    redeclare AixLib.Electrical.AC.OnePhase.Conversion.ACACTransformer conv3(
      VHigh=VHigh/sqrt(3),
      XoverR=XoverR,
      Zperc=Zperc,
      ground_1=ground_1,
      ground_2=ground_2,
      VABase=VABase/3,
      VLow=-VLow,
      phi_1=2.0943951023932));
  parameter Modelica.Units.SI.Voltage VHigh
    "Rms voltage on side 1 of the transformer (primary side)";
  parameter Modelica.Units.SI.Voltage VLow
    "Rms voltage on side 2 of the transformer (secondary side)";
  parameter Modelica.Units.SI.ApparentPower VABase
    "Nominal power of the transformer";
  parameter Real XoverR
    "Ratio between the complex and real components of the impedance (XL/R)";
  parameter Real Zperc "Short circuit impedance";
  parameter Boolean ground_1 = false "Connect side 1 of transformer to ground"
  parameter Boolean ground_2 = true "Connect side 2 of transformer to ground"
equation
end ACACTransformerStepUpYD;


within AixLib.Electrical.AC.ThreePhasesUnbalanced;
package Conversion "Package with converter and transformer models for three-phase
  unbalanced AC systems"
  extends Modelica.Icons.Package;
end Conversion;


Package with base class models

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion;
package BaseClasses "Package with base class models"
extends Modelica.Icons.BasesPackage;
end BaseClasses;


Partial model of a transformer

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.BaseClasses;
partial model PartialConverter "Partial model of a transformer"
  extends AixLib.Electrical.Icons.RefAngleConversion;
  replaceable AixLib.Electrical.Interfaces.PartialConversion
    conv1 constrainedby AixLib.Electrical.Interfaces.PartialConversion(
    redeclare package PhaseSystem_p =
        AixLib.Electrical.PhaseSystems.OnePhase,
    redeclare package PhaseSystem_n =
        AixLib.Electrical.PhaseSystems.OnePhase,
    redeclare Electrical.AC.OnePhase.Interfaces.Terminal_n terminal_n,
    redeclare Electrical.AC.OnePhase.Interfaces.Terminal_p terminal_p)
    "Transformer phase 1"
  replaceable AixLib.Electrical.Interfaces.PartialConversion
    conv2 constrainedby AixLib.Electrical.Interfaces.PartialConversion(
    redeclare package PhaseSystem_p =
        AixLib.Electrical.PhaseSystems.OnePhase,
    redeclare package PhaseSystem_n =
        AixLib.Electrical.PhaseSystems.OnePhase,
    redeclare Electrical.AC.OnePhase.Interfaces.Terminal_n terminal_n,
    redeclare Electrical.AC.OnePhase.Interfaces.Terminal_p terminal_p)
    "Transformer phase 2"
  replaceable AixLib.Electrical.Interfaces.PartialConversion
    conv3 constrainedby AixLib.Electrical.Interfaces.PartialConversion(
    redeclare package PhaseSystem_p =
        AixLib.Electrical.PhaseSystems.OnePhase,
    redeclare package PhaseSystem_n =
        AixLib.Electrical.PhaseSystems.OnePhase,
    redeclare Electrical.AC.OnePhase.Interfaces.Terminal_n terminal_n,
    redeclare Electrical.AC.OnePhase.Interfaces.Terminal_p terminal_p)
    "Transformer phase 3"
  Interfaces.Terminal_n terminal_n "Electrical connector side N"
  Interfaces.Terminal_p terminal_p "Electrical connector side P"
end PartialConverter;


Model of a transformer with D connection primary side and D
  connection secondary side. Partial model that represents a three-phase unbalanced
transformer with delta connection on both primary and secondary side.

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.BaseClasses;
partial model PartialConverterDD "Model of a transformer with D connection primary side and D
  connection secondary side"
  extends
    AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.BaseClasses.PartialConverter;
  AixLib.Electrical.AC.ThreePhasesUnbalanced.Interfaces.WyeToDelta wye_to_delta
    "Delta to wye connection"
  AixLib.Electrical.AC.ThreePhasesUnbalanced.Interfaces.WyeToDelta delta_to_wye
    "Delta to wye connection"
protected
  Interfaces.Adapter3to3 ada_n "Adapter"
  Interfaces.Adapter3to3 ada_p "Adapter"
equation

  connect(delta_to_wye.wye, terminal_n)
  connect(wye_to_delta.wye, terminal_p)

  connect(delta_to_wye.delta, ada_n.terminal)
  connect(wye_to_delta.delta, ada_p.terminal)
  connect(conv1.terminal_n, ada_n.terminals[1])
  connect(conv2.terminal_n, ada_n.terminals[2])
  connect(conv3.terminal_n, ada_n.terminals[3])
  connect(conv1.terminal_p, ada_p.terminals[1])
  connect(conv2.terminal_p, ada_p.terminals[2])
  connect(conv3.terminal_p, ada_p.terminals[3])
end PartialConverterDD;


Model of a transformer with D connection primary side and
  Y connection secondary side (Voltage step down). Partial model that represents a three-phase unbalanced
transformer with delta connection on the primary and Y connection on
the secondary side. The configuration is for voltage step down.

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.BaseClasses;
partial model PartialConverterStepDownDY "Model of a transformer with D connection primary side and
  Y connection secondary side (Voltage step down)"
  extends
    AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.BaseClasses.PartialConverter;
  AixLib.Electrical.AC.ThreePhasesUnbalanced.Interfaces.WyeToWyeGround wye_to_wyeg
    "Wye to wye grounded connection"
  AixLib.Electrical.AC.ThreePhasesUnbalanced.Interfaces.WyeToDelta delta_to_wye
    "Delta to wye connection"
protected
  Interfaces.Adapter3to3 ada3to3_n "Adapter for connections"
  Interfaces.Adapter3to3 ada3to3_p "Adapter for connections"
equation

  connect(delta_to_wye.wye, terminal_n)
  connect(wye_to_wyeg.wye, terminal_p)

  connect(conv1.terminal_n, ada3to3_n.terminals[3])
  connect(conv2.terminal_n, ada3to3_n.terminals[1])
  connect(conv3.terminal_n, ada3to3_n.terminals[2])
  connect(delta_to_wye.delta, ada3to3_n.terminal)
  connect(conv1.terminal_p, ada3to3_p.terminals[1])
  connect(conv2.terminal_p, ada3to3_p.terminals[2])
  connect(conv3.terminal_p, ada3to3_p.terminals[3])
  connect(ada3to3_p.terminal, wye_to_wyeg.wyeg)
end PartialConverterStepDownDY;


Model of a transformer with Y connection primary side and D
connection secondary side (Voltage step down). Partial model that represents a three-phase unbalanced
transformer with Y connection on primary and delta connection on
the secondary one. The configuration is for voltage step down.

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.BaseClasses;
partial model PartialConverterStepDownYD "Model of a transformer with Y connection primary side and D
connection secondary side (Voltage step down)"
  extends
    AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.BaseClasses.PartialConverter;
  AixLib.Electrical.AC.ThreePhasesUnbalanced.Interfaces.WyeToWyeGround wye_to_wyeg
    "Wye to wye grounded connection"
  AixLib.Electrical.AC.ThreePhasesUnbalanced.Interfaces.WyeToDelta delta_to_wye
    "Delta to wye connection "
protected
  Interfaces.Adapter3to3 ada3to3_n "Adapter for connections"
  Interfaces.Adapter3to3 ada3to3_p "Adapter for connections"
equation

  connect(delta_to_wye.wye, terminal_p)
  connect(wye_to_wyeg.wye, terminal_n)

  connect(conv1.terminal_n, ada3to3_n.terminals[1])
  connect(conv2.terminal_n, ada3to3_n.terminals[2])
  connect(conv3.terminal_n, ada3to3_n.terminals[3])
  connect(conv1.terminal_p, ada3to3_p.terminals[1])
  connect(conv2.terminal_p, ada3to3_p.terminals[2])
  connect(conv3.terminal_p, ada3to3_p.terminals[3])
  connect(ada3to3_p.terminal, delta_to_wye.delta)
  connect(wye_to_wyeg.wyeg, ada3to3_n.terminal)
end PartialConverterStepDownYD;


Model of a transformer with D connection primary side and Y connection
  secondary side (Voltage step up). Partial model that represents a three-phase unbalanced
transformer with delta connection on the primary and Y connection on
the secondary side. The configuration is for voltage step up.

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.BaseClasses;
partial model PartialConverterStepUpDY "Model of a transformer with D connection primary side and Y connection
  secondary side (Voltage step up)"
  extends
    AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.BaseClasses.PartialConverter;
  AixLib.Electrical.AC.ThreePhasesUnbalanced.Interfaces.WyeToWyeGround wye_to_wyeg
    "Wye to wye grounded connection"
  AixLib.Electrical.AC.ThreePhasesUnbalanced.Interfaces.WyeToDelta delta_to_wye
    "Delta to wye connection"
protected
  Interfaces.Adapter3to3 ada3to3_n "Adapter for connections"
  Interfaces.Adapter3to3 ada3to3_p "Adapter for connections"
equation

  connect(delta_to_wye.wye, terminal_n)
  connect(wye_to_wyeg.wye, terminal_p)

  connect(conv1.terminal_n,ada3to3_n. terminals[1])
  connect(conv2.terminal_n,ada3to3_n. terminals[2])
  connect(conv3.terminal_n,ada3to3_n. terminals[3])
  connect(conv1.terminal_p,ada3to3_p. terminals[1])
  connect(conv2.terminal_p,ada3to3_p. terminals[2])
  connect(conv3.terminal_p,ada3to3_p. terminals[3])
  connect(ada3to3_p.terminal, wye_to_wyeg.wyeg)
  connect(delta_to_wye.delta, ada3to3_n.terminal)
end PartialConverterStepUpDY;


Model of a transformer with Y connection primary side and D connection
  secondary side (Voltage step up). Partial model that represents a three-phase unbalanced
transformer with Y connection on the primary and delta connection on
the secondary side. The configuration is for voltage step up.

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.BaseClasses;
partial model PartialConverterStepUpYD "Model of a transformer with Y connection primary side and D connection
  secondary side (Voltage step up)"
  extends
    AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.BaseClasses.PartialConverter;
  AixLib.Electrical.AC.ThreePhasesUnbalanced.Interfaces.WyeToWyeGround wye_to_wyeg
    "Wye to wye grounded connection"
  AixLib.Electrical.AC.ThreePhasesUnbalanced.Interfaces.WyeToDelta delta_to_wye
    "Delta to wye connection"
protected
  Interfaces.Adapter3to3 ada3to3_n "Adapter for connections"
  Interfaces.Adapter3to3 ada3to3_p "Adapter for connections"
equation

  connect(delta_to_wye.wye, terminal_p)
  connect(wye_to_wyeg.wye, terminal_n)

  connect(conv1.terminal_n,ada3to3_n. terminals[1])
  connect(conv2.terminal_n,ada3to3_n. terminals[2])
  connect(conv3.terminal_n,ada3to3_n. terminals[3])
  connect(wye_to_wyeg.wyeg, ada3to3_n.terminal)
  connect(conv1.terminal_p, ada3to3_p.terminals[3])
  connect(conv2.terminal_p, ada3to3_p.terminals[1])
  connect(conv3.terminal_p, ada3to3_p.terminals[2])
  connect(ada3to3_p.terminal, delta_to_wye.delta)
end PartialConverterStepUpYD;


Model of a transformer with Y connection primary side and Y
  connection secondary side. Partial model that represents a three-phase unbalanced
transformer with Y connection on both primary and secondary side.

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.BaseClasses;
partial model PartialConverterYY "Model of a transformer with Y connection primary side and Y
  connection secondary side"
  extends
    AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.BaseClasses.PartialConverter;
  Interfaces.Connection3to3Ground_n connection3to4_n
    "Conversion between 3 to 4 connectors"
  Interfaces.Connection3to3Ground_p connection3to4_p
    "Conversion between 3 to 4 connectors"
  OnePhase.Basics.Ground ground_n "Ground reference"
  OnePhase.Basics.Ground ground_p "Ground reference"
protected
  Interfaces.Adapter3to3 ada3to3_n "Adapter for connections"
  Interfaces.Adapter3to3 ada3to3_p "Adapter for connections"
equation

  connect(terminal_p, connection3to4_p.terminal3)
  connect(terminal_n, connection3to4_n.terminal3)
  connect(connection3to4_p.ground4, ground_p.terminal)
  connect(ground_n.terminal, connection3to4_n.ground4)
  connect(connection3to4_n.terminal4, ada3to3_n.terminal)
  connect(ada3to3_n.terminals[1], conv1.terminal_n)
  connect(ada3to3_n.terminals[2], conv2.terminal_n)
  connect(ada3to3_n.terminals[3], conv3.terminal_n)
  connect(conv1.terminal_p, ada3to3_p.terminals[1])
  connect(conv2.terminal_p, ada3to3_p.terminals[2])
  connect(conv3.terminal_p, ada3to3_p.terminals[3])
  connect(ada3to3_p.terminal, connection3to4_p.terminal4)
end PartialConverterYY;


Test for the AC/AC converter model. This example model tests the

AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.ACACConverter model.

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.Examples;
model Converter "Test for the AC/AC converter model"
  extends BaseClasses.TransformerExample(
  V_primary = 480,
  V_secondary = 240,
  redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeWye probe_2,
  redeclare
      AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.ACACConverter
  tra(conversionFactor=0.5,eta=0.9));

equation
  connect(probe_2.term, tra.terminal_p)
end Converter;


Package with example models

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


Test for the AC/AC transformer model. This example model tests the

AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.ACACTransformer model.

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.Examples;
model Transformer "Test for the AC/AC transformer model"
  extends BaseClasses.TransformerExample(
  redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeWye probe_2,
  redeclare
      AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.ACACTransformer
  tra(VHigh=V_primary,
      VLow=V_secondary,
      XoverR=6,
      Zperc=sqrt(0.01^2 + 0.06^2),
      VABase=6000000));

equation
  connect(probe_2.term, load.terminal)
end Transformer;


Test for the AC/AC transformer model with Delta-Delta configuration. This example model tests the

AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.ACACTransformerDD model.

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.Examples;
model TransformerDD
  "Test for the AC/AC transformer model with Delta-Delta configuration"
  extends BaseClasses.TransformerExample(
  redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeDelta probe_2,
  redeclare
      AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.ACACTransformerDD
  tra(VHigh=V_primary,
      VLow=V_secondary,
      XoverR=6,
      Zperc=sqrt(0.01^2 + 0.06^2),
      VABase=6000000));

equation
  connect(probe_2.term, load.terminal)
end TransformerDD;


Test for the AC/AC transformer full model. This example model tests the

AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.ACACTransformerFull model.

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.Examples;
model TransformerFull "Test for the AC/AC transformer full model"
  extends BaseClasses.TransformerExample(
  redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeWye probe_2,
  redeclare
      AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.ACACTransformerFull
  tra(VHigh=V_primary,
      VLow=V_secondary,
      VABase=6000000,
      f=60,
      R1=0.005,
      L1=0.005*6,
      R2=0.005,
      L2=0.005*6,
      magEffects=true,
      Rm=10,
      Lm=10),
      load(initMode=AixLib.Electrical.Types.InitMode.linearized));

equation
  connect(probe_2.term, load.terminal)
end TransformerFull;


Test for the AC/AC transformer model with Delta-Wye configuration (step-down voltage). This example model tests the

AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.ACACTransformerStepDownDY model.

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.Examples;
model TransformerStepDownDY
  "Test for the AC/AC transformer model with Delta-Wye configuration (step-down voltage)"
  extends BaseClasses.TransformerExample(
  redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeWye probe_2,
  redeclare
      AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.ACACTransformerStepDownDY
  tra(VHigh=V_primary,
      VLow=V_secondary,
      XoverR=6,
      Zperc=sqrt(0.01^2 + 0.06^2),
      VABase=6000000));

equation
  connect(probe_2.term, load.terminal)
end TransformerStepDownDY;


Test for the AC/AC transformer model with Wye-Delta configuration (step-down voltage). This example model tests the

AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.ACACTransformerStepDownYD model.

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.Examples;
model TransformerStepDownYD
  "Test for the AC/AC transformer model with Wye-Delta configuration (step-down voltage)"
  extends BaseClasses.TransformerExample(
  redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeDelta probe_2,
  redeclare
      AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.ACACTransformerStepDownYD
  tra(VHigh=V_primary,
      VLow=V_secondary,
      XoverR=6,
      Zperc=sqrt(0.01^2 + 0.06^2),
      VABase=6000000));

equation
  connect(probe_2.term, load.terminal)
end TransformerStepDownYD;


Test for the AC/AC transformer model with Delta-Wye configuration (step-up voltage). This example model tests the

AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.ACACTransformerStepUpDY model.

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.Examples;
model TransformerStepUpDY
  "Test for the AC/AC transformer model with Delta-Wye configuration (step-up voltage)"
  extends BaseClasses.TransformerExample(
  V_primary = 4160,
  V_secondary = 12470,
  redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeWye probe_2,
  redeclare
      AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.ACACTransformerStepUpDY
  tra(VHigh=V_primary,
      VLow=V_secondary,
      XoverR=6,
      Zperc=sqrt(0.01^2 + 0.06^2),
      VABase=6000000));

equation
  connect(probe_2.term, load.terminal)
end TransformerStepUpDY;


Test for the AC/AC transformer model with Wye-Delta configuration (step-up voltage). This example model tests the

AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.ACACTransformerStepUpYD model.

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.Examples;
model TransformerStepUpYD
  "Test for the AC/AC transformer model with Wye-Delta configuration (step-up voltage)"
  extends BaseClasses.TransformerExample(
  V_primary = 4160,
  V_secondary = 12470,
  redeclare AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeDelta probe_2,
  redeclare
      AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.ACACTransformerStepUpYD
  tra(VHigh=V_primary,
      VLow=V_secondary,
      XoverR=6,
      Zperc=sqrt(0.01^2 + 0.06^2),
      VABase=6000000));

equation
  connect(probe_2.term, load.terminal)
end TransformerStepUpYD;


within AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.Examples;
package BaseClasses "This package contains base classes inherited by the examples"
extends Modelica.Icons.BasesPackage;
end BaseClasses;


This example represents the basic test for a transformer model. This model is the base classes used by the examples that are part of the package

AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.Examples.

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.Examples.BaseClasses;
model TransformerExample
  "This example represents the basic test for a transformer model"
  extends Modelica.Icons.Example;
  parameter Modelica.Units.SI.Voltage V_primary=12470
    "RMS Voltage on the primary side of the transformer";
  parameter Modelica.Units.SI.Voltage V_secondary=4160
    "RMS Voltage on the secondary side of the transformer";
  Sources.FixedVoltage sou(
    f=60,
    V=V_primary) "Voltage source"
  replaceable
    AixLib.Electrical.AC.ThreePhasesUnbalanced.Conversion.BaseClasses.PartialConverter
    tra "Transformer model"
  Loads.Resistive load(
    loadConn=AixLib.Electrical.Types.LoadConnection.wye_to_wyeg,
    P_nominal=-1800e3,
    V_nominal=V_secondary,
    linearized=true)       "Load model"
  Sensors.ProbeWye probe_Y_1(perUnit=false, V_nominal = V_primary)
    "Probe that measures the voltage in Y configuration, primary side"
  Sensors.ProbeDelta probe_D_1(perUnit=false, V_nominal = V_primary)
    "Probe that measures the voltage in D configuration, primary side"
  replaceable Sensors.BaseClasses.GeneralizedProbe probe_2 constrainedby
    Sensors.BaseClasses.GeneralizedProbe(perUnit=false,
    V_nominal=V_secondary)
    "Probe that measures the voltage at the secondary side"

equation
  connect(sou.terminal, tra.terminal_n)
  connect(probe_Y_1.term, tra.terminal_n)
  connect(probe_D_1.term, tra.terminal_n)
  connect(tra.terminal_p, load.terminal)
end TransformerExample;


within AixLib.Electrical.AC.ThreePhasesUnbalanced.Interfaces;
model Adapter3to3
  "Adapter from 3 single phase connectors to a connector with 3 phases"
  extends Modelica.Blocks.Icons.Block;

  AixLib.Electrical.Interfaces.Terminal terminals[3](
    redeclare final package PhaseSystem = PhaseSystems.OnePhase)
    "Generalized terminal"

  Interfaces.Terminal_p terminal "Connector with 3 lines"
equation
  for i in 1:3 loop
    terminal.phase[i].v     = terminals[i].v;
    terminal.phase[i].i     = -terminals[i].i;
    Connections.branch(terminal.phase[i].theta, terminals[i].theta);
    terminal.phase[i].theta = terminals[i].theta;
  end for;
end Adapter3to3;


Adapter between Terminal4 (3 phases and ground) and Terminal3 N. Adapter between electric connectors type n for AC three-phase unbalanced systems.
This adapter connects a connector without neutral cable to a connector without
neutral cable.

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Interfaces;
model Connection3to3Ground_n
  "Adapter between Terminal4 (3 phases and ground) and Terminal3 N"

  Terminal_n terminal4 "Connector for side with neutral"
  AixLib.Electrical.AC.OnePhase.Interfaces.Terminal_n ground4
    "Connector for neutral"
  Terminal_n terminal3 "Connector without neutral"
equation

  // Connection branches required to propagate the reference angle
  Connections.branch(terminal4.phase[1].theta, ground4.theta);
  terminal4.phase[1].theta = ground4.theta;
  for i in 1:3 loop
    Connections.branch(terminal3.phase[i].theta, terminal4.phase[i].theta);
    terminal3.phase[i].theta = terminal4.phase[i].theta;
  end for;

  // KVL and KCL
  terminal3.phase[1].v[:] = terminal4.phase[1].v[:] - ground4.v[:];
  terminal3.phase[2].v[:] = terminal4.phase[2].v[:] - ground4.v[:];
  terminal3.phase[3].v[:] = terminal4.phase[3].v[:] - ground4.v[:];

  terminal4.phase[1].i[:] + terminal4.phase[2].i[:] = -terminal4.phase[3].i[:] - ground4.i[:];
  terminal4.phase[1].i[:] = - terminal3.phase[1].i[:];
  terminal4.phase[2].i[:] = - terminal3.phase[2].i[:];
  terminal4.phase[3].i[:] = - terminal3.phase[3].i[:];
end Connection3to3Ground_n;


Adapter between Terminal4 (3 phases and ground) and Terminal3 P. Adapter between electric connectors type p for AC three-phase unbalanced systems.
This adapter connects a connector without neutral cable to a connector without
neutral cable.

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Interfaces;
model Connection3to3Ground_p
  "Adapter between Terminal4 (3 phases and ground) and Terminal3 P"

  Terminal_p terminal4 "Connector with neutral"
  AixLib.Electrical.AC.OnePhase.Interfaces.Terminal_p ground4
    "Connector for neutral"
  Terminal_p terminal3 "Connector without neutral"
equation

  // Connection branches required to propagate the reference angle
  Connections.branch(terminal4.phase[1].theta, ground4.theta);
  terminal4.phase[1].theta = ground4.theta;
  for i in 1:3 loop
    Connections.branch(terminal3.phase[i].theta, terminal4.phase[i].theta);
    terminal3.phase[i].theta = terminal4.phase[i].theta;
  end for;

  // KVL and KCL
  terminal3.phase[1].v[:] = terminal4.phase[1].v[:] - ground4.v[:];
  terminal3.phase[2].v[:] = terminal4.phase[2].v[:] - ground4.v[:];
  terminal3.phase[3].v[:] = terminal4.phase[3].v[:] - ground4.v[:];

  terminal4.phase[1].i[:] + terminal4.phase[2].i[:] = -terminal4.phase[3].i[:] - ground4.i[:];
  terminal4.phase[1].i[:] = - terminal3.phase[1].i[:];
  terminal4.phase[2].i[:] = - terminal3.phase[2].i[:];
  terminal4.phase[3].i[:] = - terminal3.phase[3].i[:];
end Connection3to3Ground_p;


Adapter between Terminal4 and Terminal3 N. Adapter between electric connectors type n for AC three-phase unbalanced systems.
This adapter connects a connector without neutral cable with a one that has it

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Interfaces;
model Connection3to4_n "Adapter between Terminal4 and Terminal3 N"

  Terminal4_n terminal4 "Connector with neutral"
  Terminal_n terminal3 "Connector without neutral"
equation

  // Connection branches required to propagate the reference angle
  Connections.branch(terminal4.phase[1].theta, terminal4.phase[4].theta);
  terminal4.phase[1].theta = terminal4.phase[4].theta;
  for i in 1:3 loop
    Connections.branch(terminal3.phase[i].theta, terminal4.phase[i].theta);
    terminal3.phase[i].theta = terminal4.phase[i].theta;
  end for;

  // KVL and KCL
  terminal3.phase[1].v[:] = terminal4.phase[1].v[:] - terminal4.phase[4].v[:];
  terminal3.phase[2].v[:] = terminal4.phase[2].v[:] - terminal4.phase[4].v[:];
  terminal3.phase[3].v[:] = terminal4.phase[3].v[:] - terminal4.phase[4].v[:];

  terminal4.phase[1].i[:] + terminal4.phase[2].i[:] = -terminal4.phase[3].i[:] - terminal4.phase[4].i[:];
  terminal4.phase[1].i[:] = - terminal3.phase[1].i[:];
  terminal4.phase[2].i[:] = - terminal3.phase[2].i[:];
  terminal4.phase[3].i[:] = - terminal3.phase[3].i[:];
end Connection3to4_n;


Adapter between Terminal4 and Terminal3 P. Adapter between electric connectors type p for AC three-phase unbalanced systems.
This adapter connects two connectors without and without a neutral cable.

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Interfaces;
model Connection3to4_p "Adapter between Terminal4 and Terminal3 P"

  Terminal4_p terminal4 "Connector with neutral"
  Terminal_p terminal3 "Connector without neutral"
equation

  // Connection branches required to propagate the reference angle
  Connections.branch(terminal4.phase[1].theta, terminal4.phase[4].theta);
  terminal4.phase[1].theta = terminal4.phase[4].theta;
  for i in 1:3 loop
    Connections.branch(terminal3.phase[i].theta, terminal4.phase[i].theta);
    terminal3.phase[i].theta = terminal4.phase[i].theta;
  end for;

  // KVL and KCL
  terminal3.phase[1].v[:] = terminal4.phase[1].v[:] - terminal4.phase[4].v[:];
  terminal3.phase[2].v[:] = terminal4.phase[2].v[:] - terminal4.phase[4].v[:];
  terminal3.phase[3].v[:] = terminal4.phase[3].v[:] - terminal4.phase[4].v[:];

  terminal4.phase[1].i[:] + terminal4.phase[2].i[:] = -terminal4.phase[3].i[:] - terminal4.phase[4].i[:];
  terminal4.phase[1].i[:] = - terminal3.phase[1].i[:];
  terminal4.phase[2].i[:] = - terminal3.phase[2].i[:];
  terminal4.phase[3].i[:] = - terminal3.phase[3].i[:];
end Connection3to4_p;


within AixLib.Electrical.AC.ThreePhasesUnbalanced;
package Interfaces "Package with interfaces for three-phase unbalanced AC models"
  extends Modelica.Icons.InterfacesPackage;
end Interfaces;


Partial model interface for a two port component without neutral cable. Interface model for an AC three-phase unbalanced component with two ports
and without neutral cable.

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Interfaces;
partial model TwoPort
  "Partial model interface for a two port component without neutral cable"

  Terminal_p terminal_p "Electric terminal side p"
  Terminal_n terminal_n "Electric terminal side n"
end TwoPort;


Partial model interface for a two port component with neutral cable. Interface model for an AC three-phase unbalanced component with two ports
and with neutral cable.

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Interfaces;
partial model TwoPort_N
  "Partial model interface for a two port component with neutral cable"

  Terminal4_p terminal_p "Electric terminal side p"
  Terminal4_n terminal_n "Electric terminal side n"
end TwoPort_N;


This model represent a connection between wye to delta. Adapter from Wye (Y) to Delta (D) connector.
A three-phase unbalanced connector has three AC single phase
connectors. Each AC single phase connector contains the phase voltage,
which is measured between the phase and the neutral.
This model converts the phase voltage to the line voltage, measured between
the phases.

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Interfaces;
model WyeToDelta "This model represent a connection between wye to delta"

  Terminal_n wye "Terminal Y"
  Terminal_n delta "Terminal D"
equation

  for i in 1:3 loop
    Connections.branch(wye.phase[i].theta, delta.phase[i].theta);
    wye.phase[i].theta = delta.phase[i].theta;
  end for;

  delta.phase[1].v[:] = wye.phase[1].v[:] - wye.phase[2].v[:];
  delta.phase[2].v[:] = wye.phase[2].v[:] - wye.phase[3].v[:];
  delta.phase[3].v[:] = wye.phase[3].v[:] - wye.phase[1].v[:];

  -wye.phase[1].i[:] + delta.phase[3].i[:] = delta.phase[1].i[:];
  -wye.phase[2].i[:] + delta.phase[1].i[:] = delta.phase[2].i[:];
  -wye.phase[3].i[:] + delta.phase[2].i[:] = delta.phase[3].i[:];
end WyeToDelta;


This model represent a connection between wye to wye grounded. Adapter from wye (Y) to wye grounded (Yg) connector.
A three-phase unbalanced connector has three AC single phase
connectors. Each AC single phase connector contains a the phase voltage,
which is measured
between the phase and the neutral. This model assures that the voltage of the neutral
cable is equal to zero.

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Interfaces;
model WyeToWyeGround
  "This model represent a connection between wye to wye grounded"

  Terminal_n wye "Terminal Y"
  Terminal_n wyeg "Terminal Y with ground connection"
  Connection3to3Ground_n connection3to4
    "Adapter between Termina3 and Terminal4"
  AixLib.Electrical.AC.OnePhase.Basics.Ground ground "Ground reference"
equation
  connect(wye, connection3to4.terminal3)
  connect(connection3to4.terminal4, wyeg)
  connect(connection3to4.ground4, ground.terminal)
end WyeToWyeGround;


Package with example models

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


Test for Y to D connection. This simple example shows how to use a Y to D adapter.

within AixLib.Electrical.AC.ThreePhasesUnbalanced.Interfaces.Examples;
model WyeToDelta "Test for Y to D connection"
  extends Modelica.Icons.Example;

  AixLib.Electrical.AC.ThreePhasesUnbalanced.Interfaces.WyeToDelta wyeToDelta
    "Conversion of the voltages from Y to D"
  AixLib.Electrical.AC.ThreePhasesUnbalanced.Sources.FixedVoltage V1(f=60, V=
        480) "Voltage source"
  AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeWye probe_Y(perUnit=
       false, V_nominal=480)
    "Probe that measures the voltage and the angles on each phase"
  AixLib.Electrical.AC.ThreePhasesUnbalanced.Sensors.ProbeWye probeD(perUnit=
        false, V_nominal=480)
    "Probe that measures the voltage and the angles on each phase"
equation
  connect(V1.terminal, wyeToDelta.wye)
  connect(probe_Y.term, wyeToDelta.wye)
  connect(probeD.term, wyeToDelta.delta)
end WyeToDelta;


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

AixLib.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 AixLib.Electrical.AC.ThreePhasesUnbalanced.Lines;
model Line "Model of an electrical line without neutral cable"
  extends AixLib.Electrical.AC.ThreePhasesUnbalanced.Interfaces.TwoPort(
    terminal_p(phase(v(each nominal = V_nominal))),
    terminal_n(phase(v(each nominal = V_nominal))));
  extends AixLib.Electrical.Transmission.BaseClasses.PartialBaseLine(
  V_nominal(start = 480),
  commercialCable = AixLib.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 R=R/3,
    final L=L/3,
    final mode=modelMode) "Impedance line 1"
  OnePhase.Lines.TwoPortRL phase2(
    final useHeatPort=true,
    final T_ref=T_ref,
    final M=M,
    final R=R/3,
    final L=L/3,
    final mode=modelMode) "Impedance line 2"
  OnePhase.Lines.TwoPortRL phase3(
    final useHeatPort=true,
    final T_ref=T_ref,
    final M=M,
    final R=R/3,
    final L=L/3,
    final mode=modelMode) "Impedance line 3"
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])
end Line;


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

AixLib.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 AixLib.Electrical.AC.ThreePhasesUnbalanced.Lines;
model Line_N "Model of an electrical line with neutral cable"
  extends AixLib.Electrical.AC.ThreePhasesUnbalanced.Interfaces.TwoPort_N(
    terminal_p(phase(v(each nominal = V_nominal))),
    terminal_n(phase(v(each nominal = V_nominal))));
  extends AixLib.Electrical.Transmission.BaseClasses.PartialBaseLine(
  V_nominal(start = 480),
  commercialCable = AixLib.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 AixLib.Electrical.AC.ThreePhasesUnbalanced.Lines;
model Network "Three phases unbalanced AC network without neutral cable"
  extends Transmission.BaseClasses.PartialNetwork(
    redeclare
      AixLib.Electrical.AC.ThreePhasesUnbalanced.Interfaces.Terminal_p terminal,
    redeclare replaceable Transmission.Grids.TestGrid2Nodes grid,
    redeclare AixLib.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] = AixLib.Electrical.PhaseSystems.OnePhase.systemVoltage(terminal[i].phase[1].v);
    VAbs[2,i] = AixLib.Electrical.PhaseSystems.OnePhase.systemVoltage(terminal[i].phase[2].v);
    VAbs[3,i] = AixLib.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 AixLib.Electrical.AC.ThreePhasesUnbalanced.Lines;
model Network_N "Three phases unbalanced AC network with neutral cable"
  extends Transmission.BaseClasses.PartialNetwork(
    redeclare
      AixLib.Electrical.AC.ThreePhasesUnbalanced.Interfaces.Terminal4_p terminal,
    redeclare replaceable Transmission.Grids.TestGrid2Nodes grid,
    redeclare AixLib.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] = AixLib.Electrical.PhaseSystems.OnePhase.systemVoltage(terminal[i].phase[1].v - terminal[i].phase[4].v);
    VAbs[2,i] = AixLib.Electrical.PhaseSystems.OnePhase.systemVoltage(terminal[i].phase[2].v - terminal[i].phase[4].v);
    VAbs[3,i] = AixLib.Electrical.PhaseSystems.OnePhase.systemVoltage(terminal[i].phase[3].v - terminal[i].phase[4].v);
  end for;
end Network_N;


within AixLib.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 AixLib.Electrical.AC.ThreePhasesUnbalanced.Lines;
model TwoPortInductance "Model of an inductance with two electrical ports"
  extends AixLib.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 AixLib.Electrical.AC.ThreePhasesUnbalanced.Lines;
model TwoPortInductance_N
  "Model of an inductance with two electrical ports and neutral line cable"
  extends AixLib.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 AixLib.Electrical.AC.ThreePhasesUnbalanced.Lines;
model TwoPortMatrixRL
  "Model of an RL line parameterized with impedance matrices"
  extends AixLib.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=AixLib.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=AixLib.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=AixLib.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=AixLib.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=AixLib.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=AixLib.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 = AixLib.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 AixLib.Electrical.AC.ThreePhasesUnbalanced.Lines;
model TwoPortMatrixRLC
  "PI model of a line parameterized with impedance and admittance matrices"
  extends AixLib.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=AixLib.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=AixLib.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=AixLib.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=AixLib.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=AixLib.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=AixLib.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 = AixLib.Electrical.PhaseSystems.OnePhase.product
    "Product between complex quantities";
  Modelica.Units.SI.Current Isr[3,2](start=zeros(3, AixLib.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, AixLib.Electrical.PhaseSystems.OnePhase.n),
      each stateSelect=StateSelect.prefer) "Shunt current on side p";
  Modelica.Units.SI.Current Ish_n[3,2](start=zeros(3, AixLib.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 AixLib.Electrical.AC.ThreePhasesUnbalanced.Lines;
model TwoPortMatrixRLC_N
  "PI model of a line parameterized with impedance and admittance matrices and neutral line"
  extends AixLib.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=AixLib.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=AixLib.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=AixLib.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=AixLib.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=AixLib.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=AixLib.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=AixLib.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=AixLib.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 = AixLib.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 AixLib.Electrical.AC.ThreePhasesUnbalanced.Lines;
model TwoPortMatrixRL_N
  "Model of an RL line parameterized with impedance matrices and neutral line"
  extends AixLib.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=AixLib.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=AixLib.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=AixLib.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=AixLib.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=AixLib.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=AixLib.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=AixLib.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=AixLib.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 = AixLib.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 AixLib.Electrical.AC.ThreePhasesUnbalanced.Lines;
model TwoPortResistance "Model of a resistance with two electrical ports"
  extends Modelica.Electrical.Analog.Interfaces.ConditionalHeatPort;
  extends AixLib.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 AixLib.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 AixLib.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 AixLib.Electrical.AC.ThreePhasesUnbalanced.Lines;
model TwoPortRL
  "Model of a resistive-inductive element with two electrical ports"
  extends Modelica.Electrical.Analog.Interfaces.ConditionalHeatPort;
  extends AixLib.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 AixLib.Electrical.Types.Load mode(
    min=AixLib.Electrical.Types.Load.FixedZ_steady_state,
    max=AixLib.Electrical.Types.Load.FixedZ_dynamic) = AixLib.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 AixLib.Electrical.AC.ThreePhasesUnbalanced.Lines;
model TwoPortRLC "Model of an RLC element with two electrical ports"
  extends Modelica.Electrical.Analog.Interfaces.ConditionalHeatPort;
  extends AixLib.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 AixLib.Electrical.Types.Load mode(
    min=AixLib.Electrical.Types.Load.FixedZ_steady_state,
    max=AixLib.Electrical.Types.Load.FixedZ_dynamic)=
    AixLib.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 AixLib.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 AixLib.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 AixLib.Electrical.Types.Load mode(
    min=AixLib.Electrical.Types.Load.FixedZ_steady_state,
    max=AixLib.Electrical.Types.Load.FixedZ_dynamic)=
    AixLib.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;