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


This example illustrates how to use the AC/AC converter model. This example illustrates the use of a model that converts AC voltage to AC voltage.
The transformer model assumes a linear loss when transmitting the power.

within AixLib.Electrical.AC.OnePhase.Conversion.Examples;
model ACACConverter
  "This example illustrates how to use the AC/AC converter model"
  extends Modelica.Icons.Example;
  AixLib.Electrical.AC.OnePhase.Conversion.ACACConverter
    conACAC(eta=0.9, conversionFactor=60/120) "ACAC transformer"
  AixLib.Electrical.AC.OnePhase.Sources.FixedVoltage                 sou(
    definiteReference=true,
    f=60,
    V=120) "Voltage source"
  AixLib.Electrical.AC.OnePhase.Loads.Inductive load(
    mode=AixLib.Electrical.Types.Load.VariableZ_P_input,
    V_nominal=60) "Load model"
  Modelica.Blocks.Sources.Ramp ramp(
    duration=0.5,
    startTime=0.3,
    height=2000,
    offset=-1000) "Power consumed by the model"
equation
  connect(sou.terminal, conACAC.terminal_n)
  connect(conACAC.terminal_p, load.terminal)
  connect(ramp.y, load.Pow)
end ACACConverter;


This example illustrates how to use the AC/AC simplified transformer model. This example illustrates the use of a the AC/AC transformer model.
The example shows three different configurations:

within AixLib.Electrical.AC.OnePhase.Conversion.Examples;
model ACACTransformer
  "This example illustrates how to use the AC/AC simplified transformer model"
  extends Modelica.Icons.Example;
  AixLib.Electrical.AC.OnePhase.Conversion.ACACTransformer tra_load(
    Zperc=0.03,
    VABase=4000,
    XoverR=8,
    VHigh=120,
    VLow=60) "Transformer with load"
  AixLib.Electrical.AC.OnePhase.Sources.FixedVoltage sou(
    definiteReference=true,
    f=60,
    V=120) "Voltage source"
  AixLib.Electrical.AC.OnePhase.Loads.Inductive load(
    mode=AixLib.Electrical.Types.Load.VariableZ_P_input,
    pf=0.8,
    V_nominal=60) "Load model"
  Modelica.Blocks.Sources.Ramp ramp(
    duration=0.5,
    startTime=0.3,
    offset=0,
    height=-4000*0.8) "Load power consumption profile"
  AixLib.Electrical.AC.OnePhase.Conversion.ACACTransformer tra_cc(
    XoverR=8,
    Zperc=0.03,
    VABase=4000,
    VHigh=120,
    VLow=60) "Transformer with short circuit"
  AixLib.Electrical.AC.OnePhase.Loads.Impedance shortCircuit(R=1e-8)
    "Short circuit"
  AixLib.Electrical.AC.OnePhase.Conversion.ACACTransformer tra_void(
    XoverR=8,
    Zperc=0.03,
    VABase=4000,
    VHigh=120,
    VLow=60) "Transformer with secondary not connected"
  AixLib.Electrical.AC.OnePhase.Sources.FixedVoltage sou1(
    definiteReference=true,
    f=60,
    V=120) "Voltage source for open and short circuit tests"
equation
  connect(sou.terminal, tra_load.terminal_n)
  connect(tra_load.terminal_p, load.terminal)
  connect(ramp.y, load.Pow)
  connect(tra_cc.terminal_p, shortCircuit.terminal)
  connect(sou1.terminal, tra_cc.terminal_n)
  connect(sou1.terminal, tra_void.terminal_n)
end ACACTransformer;


This example illustrates how to use the AC/AC transformer model. This example illustrates the use of the AC/AC transformer model
that includes losses at the primary and secondary side and magnetization
effects.
The example shows three different configurations:

within AixLib.Electrical.AC.OnePhase.Conversion.Examples;
model ACACTransformerFull
  "This example illustrates how to use the AC/AC transformer model"
  extends Modelica.Icons.Example;
  AixLib.Electrical.AC.OnePhase.Conversion.ACACTransformerFull tra_load(
    R1=0.0001,
    L1=0.0001,
    R2=0.0001,
    L2=0.0001,
    VABase=4000,
    magEffects=true,
    Rm=10,
    Lm=10,
    VHigh=120,
    VLow=60,
    f=60) "Transformer with load"
  AixLib.Electrical.AC.OnePhase.Sources.FixedVoltage sou(
    definiteReference=true,
    f=60,
    V=120) "Voltage source"
  AixLib.Electrical.AC.OnePhase.Loads.Inductive load(
    mode=AixLib.Electrical.Types.Load.VariableZ_P_input,
    pf=0.8,
    V_nominal=60) "Load model"
  Modelica.Blocks.Sources.Ramp ramp(
    duration=0.5,
    startTime=0.3,
    offset=0,
    height=-4000*0.8) "Load power consumption profile"
  AixLib.Electrical.AC.OnePhase.Conversion.ACACTransformerFull
                                                              tra_cc(
    VABase=4000,
    R1=0.01,
    L1=0.01,
    R2=0.01,
    L2=0.01,
    magEffects=false,
    Rm=100,
    Lm=100,
    VHigh=120,
    VLow=60,
    f=60) "Transformer with short circuit connection"
  AixLib.Electrical.AC.OnePhase.Loads.Impedance shortCircuit(R=1e-8)
    "Short circuit"
  AixLib.Electrical.AC.OnePhase.Conversion.ACACTransformerFull tra_void(
    VABase=4000,
    R1=0.01,
    L1=0.01,
    R2=0.01,
    L2=0.01,
    magEffects=false,
    Rm=100,
    Lm=100,
    VHigh=120,
    VLow=60,
    f=60) "Transformer with open connection"
  AixLib.Electrical.AC.OnePhase.Sources.FixedVoltage sou1(
    definiteReference=true,
    f=60,
    V=120) "Voltage source for short circuit and open tests "
equation
  connect(sou.terminal, tra_load.terminal_n)
  connect(tra_load.terminal_p, load.terminal)
  connect(ramp.y, load.Pow)
  connect(tra_cc.terminal_p, shortCircuit.terminal)
  connect(sou1.terminal, tra_cc.terminal_n)
  connect(sou1.terminal, tra_void.terminal_n)
end ACACTransformerFull;


This example illustrates how to use the AC/DC converter model. This example illustrates the use of a model that converts AC voltage to DC voltage.
The transformer model assumes a linear loss when transmitting the power.

within AixLib.Electrical.AC.OnePhase.Conversion.Examples;
model ACDCConverter
  "This example illustrates how to use the AC/DC converter model"
  extends Modelica.Icons.Example;

  AixLib.Electrical.DC.Loads.Resistor res(R=1, V_nominal=60)
    "Resistive load"
  AixLib.Electrical.AC.OnePhase.Conversion.ACDCConverter conversion(
    eta=0.9,
    ground_AC=false,
    ground_DC=true,
    conversionFactor=60/120) "AC/DC transformer"
  AixLib.Electrical.AC.OnePhase.Sources.FixedVoltage sou(
    definiteReference=true,
    f=60,
    V=120) "Voltage source"
  AixLib.Electrical.DC.Loads.Conductor load(mode=AixLib.Electrical.Types.Load.VariableZ_P_input,
    V_nominal=60) "Variable resistive load"
  Modelica.Blocks.Sources.Ramp pow(
    duration=0.5,
    startTime=0.2,
    offset=-200,
    height=5200) "Variable load profile"
equation
  connect(sou.terminal, conversion.terminal_n)
  connect(conversion.terminal_p, res.terminal)
  connect(conversion.terminal_p, load.terminal)
  connect(pow.y, load.Pow)
end ACDCConverter;


Package with example models

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


Generator with a load and grid connection. This model illustrates a generator, an inductive load and a grid connection.
The power output of the generator is equal to its input signal, which is
a ramp function.
The output grid.P shows
the actual and apparent power, the power factor and
the phase angle.

within AixLib.Electrical.AC.OnePhase.Examples;
model GeneratorLoadGrid "Generator with a load and grid connection"
  extends Modelica.Icons.Example;

  Sources.Grid grid(
    f=60,
    V=120,
    phiSou=0.5235987755983) "Electrical grid"
  Sources.Generator sou(f=60, phiGen(displayUnit="rad")) "Gas turbine"
  AixLib.Electrical.AC.OnePhase.Loads.Inductive res(
    mode=AixLib.Electrical.Types.Load.FixedZ_steady_state,
    P_nominal=-5e3,
    V_nominal=120,
    pf=1) "Inductive load"
  Modelica.Blocks.Sources.Ramp ramp(
    height=1e4,
    duration=0.6,
    startTime=0.1,
    offset=0)
equation
  connect(ramp.y, sou.P)
  connect(sou.terminal, grid.terminal)
  connect(res.terminal, grid.terminal)
end GeneratorLoadGrid;


Model of a DC load connected to the grid. This model illustrates the use of a model for inductive load. The circuit on the left hand side
uses an inductive load, whereas the circuit on the right hand side uses a resistor and inductance in
series.
The parameters of the inductor and resistor are such that the real power and the phase angle are
identical (up to the numerical precision of the parameters) for the two systems.

within AixLib.Electrical.AC.OnePhase.Examples;
model GridDCLoad "Model of a DC load connected to the grid"
  extends Modelica.Icons.Example;
  AixLib.Electrical.AC.OnePhase.Sources.Grid
               grid(
    f=60,
    phiSou=0,
    V=120)
  AixLib.Electrical.AC.OnePhase.Conversion.ACDCConverter
                                                   idealACDCConverter1(
                               eta=0.9,
    ground_AC=false,
    conversionFactor=12/120)
  AixLib.Electrical.DC.Loads.Resistor    resistor(R=1, V_nominal=12)
equation
  connect(grid.terminal, idealACDCConverter1.terminal_n)
  connect(idealACDCConverter1.terminal_p, resistor.terminal)
end GridDCLoad;


Package with example models

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


within AixLib.Electrical.AC.OnePhase;
package Interfaces "Package with interfaces for one phase AC systems"
  extends Modelica.Icons.InterfacesPackage;
end Interfaces;


Terminal n for AC one phase systems

within AixLib.Electrical.AC.OnePhase.Interfaces;
connector Terminal_n "Terminal n for AC one phase systems"
  extends AixLib.Electrical.Interfaces.Terminal(
    redeclare replaceable package PhaseSystem =
        AixLib.Electrical.PhaseSystems.OnePhase);
end Terminal_n;


Terminal p for AC one phase systems

within AixLib.Electrical.AC.OnePhase.Interfaces;
connector Terminal_p "Terminal p for AC one phase systems"
  extends AixLib.Electrical.Interfaces.Terminal(
    redeclare replaceable package PhaseSystem =
        AixLib.Electrical.PhaseSystems.OnePhase);
end Terminal_p;


Model of an electrical line

within AixLib.Electrical.AC.OnePhase.Lines;
model Line "Model of an electrical line"
  extends AixLib.Electrical.Transmission.BaseClasses.PartialLine(
    V_nominal(start = 110),
    redeclare package PhaseSystem_p = PhaseSystems.OnePhase,
    redeclare package PhaseSystem_n = PhaseSystems.OnePhase,
    redeclare replaceable Interfaces.Terminal_n terminal_n,
    redeclare replaceable Interfaces.Terminal_p terminal_p,
    commercialCable = AixLib.Electrical.Transmission.Functions.selectCable_low(P_nominal, V_nominal));
protected
  replaceable TwoPortRL line(
    R=R/3,
    L=L/3,
    mode=modelMode) constrainedby
    AixLib.Electrical.Transmission.BaseClasses.PartialTwoPortRLC(
      useHeatPort=true,
      M=M,
      T_ref=T_ref) "Model of the line"

equation
  connect(cableTemp.port, line.heatPort)
  connect(line.terminal_n, terminal_n)
  connect(terminal_p, line.terminal_p)
end Line;


Single phase AC network. This model represents a generalized electrical AC single phase network.

within AixLib.Electrical.AC.OnePhase.Lines;
model Network "Single phase AC network"
  extends AixLib.Electrical.Transmission.BaseClasses.PartialNetwork(
    V_nominal(start = 110),
    redeclare Interfaces.Terminal_p terminal,
    redeclare replaceable Transmission.Grids.TestGrid2Nodes grid,
    redeclare Line lines(
      commercialCable=grid.cables,
      each use_C=use_C,
      each modelMode=modelMode));
  parameter Boolean use_C = false "If true, model the cable capacity"
  parameter AixLib.Electrical.Types.Load modelMode=Types.Load.FixedZ_steady_state
    "Select between steady state and dynamic model"
  Modelica.Units.SI.Voltage VAbs[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[i] = AixLib.Electrical.PhaseSystems.OnePhase.systemVoltage(terminal[i].v);
  end for;
end Network;


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


within AixLib.Electrical.AC.OnePhase.Lines;
model TwoPortInductance
  "Model of an inductive element with two electrical ports"
  extends
    AixLib.Electrical.Transmission.BaseClasses.PartialTwoPortInductance(
    redeclare package PhaseSystem_p = PhaseSystems.OnePhase,
    redeclare package PhaseSystem_n = PhaseSystems.OnePhase,
    redeclare replaceable Interfaces.Terminal_n terminal_n,
    redeclare replaceable Interfaces.Terminal_p terminal_p);
  parameter AixLib.Electrical.Types.Load mode(
    min=AixLib.Electrical.Types.Load.FixedZ_steady_state,
    max=AixLib.Electrical.Types.Load.VariableZ_y_input)=
    AixLib.Electrical.Types.Load.FixedZ_steady_state
    "Type of model (e.g., steady state, dynamic, prescribed power consumption, etc.)"
protected
  Modelica.Units.SI.AngularVelocity omega
    "Frequency of the quasi-stationary sine waves";
  Modelica.Units.SI.Angle theRef "Absolute angle of rotating reference system";

equation
  theRef = PhaseSystem_p.thetaRef(terminal_p.theta);
  omega = der(theRef);

  if mode==AixLib.Electrical.Types.Load.FixedZ_dynamic then
    // Dynamics of the system
    der(L*terminal_p.i) + L*omega*PhaseSystem_p.j(terminal_p.i) = terminal_p.v - terminal_n.v;

  else
    // Steady state relationship
    L*omega*PhaseSystem_p.j(terminal_p.i) = terminal_p.v - terminal_n.v;

  end if;
end TwoPortInductance;


within AixLib.Electrical.AC.OnePhase.Lines;
model TwoPortResistance "Model of a resistance with two electrical ports"
  extends
    AixLib.Electrical.Transmission.BaseClasses.PartialTwoPortResistance(
    redeclare package PhaseSystem_p = PhaseSystems.OnePhase,
    redeclare package PhaseSystem_n = PhaseSystems.OnePhase,
    redeclare replaceable Interfaces.Terminal_n terminal_n,
    redeclare replaceable Interfaces.Terminal_p terminal_p);
equation
  terminal_p.v - terminal_n.v = terminal_p.i*diagonal(ones(PhaseSystem_p.n)*R_actual);

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


within AixLib.Electrical.AC.OnePhase.Lines;
model TwoPortRL
  "Model of a resistive-inductive element with two electrical ports"
  extends AixLib.Electrical.Transmission.BaseClasses.PartialTwoPortRLC(
    final V_nominal=0,
    redeclare package PhaseSystem_p = PhaseSystems.OnePhase,
    redeclare package PhaseSystem_n = PhaseSystems.OnePhase,
    redeclare replaceable Interfaces.Terminal_n terminal_n,
    redeclare replaceable Interfaces.Terminal_p terminal_p,
    final C=0);
  parameter Modelica.Units.SI.Current i_start[PhaseSystem_p.n]=zeros(
      PhaseSystem_p.n)
    "Initial current phasor of the line (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.)"
protected
  Modelica.Units.SI.Current i_p[2](start=i_start, each stateSelect=StateSelect.prefer)
    "Current phasor at terminal p";
  Modelica.Units.SI.AngularVelocity omega
    "Frequency of the quasi-stationary sine waves";
  Modelica.Units.SI.Angle theRef "Absolute angle of rotating reference system";

initial equation
  if mode==AixLib.Electrical.Types.Load.FixedZ_dynamic then
    i_p = i_start;
  end if;
equation
  theRef = PhaseSystem_p.thetaRef(terminal_p.theta);
  omega = der(theRef);

  terminal_p.i = - terminal_n.i;
  i_p = terminal_p.i;

  if mode==AixLib.Electrical.Types.Load.FixedZ_dynamic then
    // Dynamics of the system
    der(L*i_p) + L*omega*PhaseSystem_p.j(i_p) +
      i_p*diagonal(ones(PhaseSystem_p.n)*R_actual)
       = terminal_p.v - terminal_n.v;

  else
    // steady state relationship
    L*omega*PhaseSystem_p.j(i_p) +
      i_p*diagonal(ones(PhaseSystem_p.n)*R_actual)
      = terminal_p.v - terminal_n.v;
  end if;

  // Joule losses
  LossPower = R_actual*(i_p[1]^2 + i_p[2]^2);
end TwoPortRL;


within AixLib.Electrical.AC.OnePhase.Lines;
model TwoPortRLC "Model of an RLC element with two electrical ports"
  extends AixLib.Electrical.Transmission.BaseClasses.PartialTwoPortRLC(
    V_nominal(start = 110),
    redeclare package PhaseSystem_p = PhaseSystems.OnePhase,
    redeclare package PhaseSystem_n = PhaseSystems.OnePhase,
    redeclare replaceable Interfaces.Terminal_n terminal_n(
      redeclare package PhaseSystem = PhaseSystem_n),
    redeclare replaceable Interfaces.Terminal_p terminal_p(
      redeclare package PhaseSystem = PhaseSystem_p));
  parameter Modelica.Units.SI.Voltage Vc_start[2]={V_nominal,0}
    "Initial voltage phasor of the capacitance located in the middle of the line"
  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.)"
protected
  Modelica.Units.SI.Voltage Vc[2](start=Vc_start, each stateSelect=StateSelect.prefer)
    "Voltage of the Capacitance located in the middle of the line";
  Modelica.Units.SI.Current Ic[2]
    "Currenbt of the capacitance located in the middle of the line";
  Modelica.Units.SI.AngularVelocity omega
    "Frequency of the quasi-stationary sine waves";
  Modelica.Units.SI.Angle theRef "Absolute angle of rotating reference system";

initial equation
  if C > 0 and mode == AixLib.Electrical.Types.Load.FixedZ_dynamic then
    Vc = Vc_start;
  end if;
equation
  theRef = PhaseSystem_p.thetaRef(terminal_p.theta);
  omega = der(theRef);

  terminal_p.i + terminal_n.i = Ic;

  L/2*omega*AixLib.Electrical.PhaseSystems.OnePhase.j(terminal_p.i) +
    terminal_p.i*diagonal(ones(PhaseSystem_p.n)*R_actual/2) = terminal_p.v - Vc;
  L/2*omega*AixLib.Electrical.PhaseSystems.OnePhase.j(terminal_n.i) +
    terminal_n.i*diagonal(ones(PhaseSystem_n.n)*R_actual/2) = terminal_n.v - Vc;

  if C > 0 then
    if mode == AixLib.Electrical.Types.Load.FixedZ_dynamic then
      // Dynamics of the system
      C*der(Vc) + omega*C*AixLib.Electrical.PhaseSystems.OnePhase.j(Vc) = Ic;
    else
      // steady state relationship
      omega*C*AixLib.Electrical.PhaseSystems.OnePhase.j(Vc) = Ic;
    end if;
  else
    // No capacitive effect, the voltage in the middle of the line is the linear
    // interpolation of the two phasors
    Vc = (terminal_p.v + terminal_n.v)/2;
  end if;

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


Test model for a single phase line that uses commercial cable information. This example demonstrates how to use a line model to connect
a source to a load that uses commercial cable information.

within AixLib.Electrical.AC.OnePhase.Lines.Examples;
model ACLine
  "Test model for a single phase line that uses commercial cable information"
  extends Modelica.Icons.Example;
  Sources.FixedVoltage E(      definiteReference=true,
    f=60,
    V=120) "Voltage source"
  Loads.Impedance R1(R=10) "Resistive load 1"
  Line line_1(
    P_nominal=5000,
    l=2000,
    mode=Types.CableMode.commercial,
    commercialCable=Transmission.LowVoltageCables.Cu50(),
    V_nominal=120) "Resistive line that connects to load 1"
  Line line_2a(
    P_nominal=5000,
    mode=Types.CableMode.commercial,
    commercialCable=Transmission.LowVoltageCables.Cu50(),
    l=1000,
    V_nominal=120) "Resistive line that connects to load 2"
  Loads.Impedance R2(
    R=10) "Resistive load 2"
  Line line_2b(
    P_nominal=5000,
    mode=Types.CableMode.commercial,
    commercialCable=Transmission.LowVoltageCables.Cu50(),
    l=1000,
    V_nominal=120) "Resistive line that connects to load 2"
  Line line_3a(
    P_nominal=5000,
    mode=Types.CableMode.commercial,
    commercialCable=Transmission.LowVoltageCables.Cu50(),
    l=4000,
    V_nominal=120) "Resistive line that connects to load 3"
  Line line_3b(
    P_nominal=5000,
    mode=Types.CableMode.commercial,
    commercialCable=Transmission.LowVoltageCables.Cu50(),
    l=4000,
    V_nominal=120) "Resistive line that connects to load 3"
  Loads.Impedance R3(
    R=10) "Resistive load 3"
  Line line_sc(
    P_nominal=5000,
    l=2000,
    mode=Types.CableMode.commercial,
    commercialCable=Transmission.LowVoltageCables.Cu50(),
    V_nominal=120) "Line that connects the source and the short circuit"
  Loads.Impedance load_sc(R=0) "Short circuit"
equation
  connect(line_1.terminal_p, R1.terminal)
  connect(E.terminal, line_1.terminal_n)
  connect(E.terminal, line_2a.terminal_n)
  connect(line_2a.terminal_p, line_2b.terminal_n)
  connect(line_2b.terminal_p, R2.terminal)
  connect(line_3a.terminal_p, R3.terminal)
  connect(line_3b.terminal_p, R3.terminal)
  connect(E.terminal, line_3a.terminal_n)
  connect(E.terminal, line_3b.terminal_n)
  connect(line_sc.terminal_p, load_sc.terminal)
  connect(E.terminal, line_sc.terminal_n)
end ACLine;


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

within AixLib.Electrical.AC.OnePhase.Lines.Examples;
model ACLine_L "Test model for a single phase inductive line"
  extends Modelica.Icons.Example;
  parameter Modelica.Units.SI.Inductance Lbase=10/2/Modelica.Constants.pi/60
    "Base value for the line inductances";
  Sources.FixedVoltage E(      definiteReference=true,
    f=60,
    V=120) "Voltage source"
  Loads.Impedance R1(R=10) "Resistive load 1"
  Loads.Impedance R2(
    R=10) "Resistive load 2"
  Loads.Impedance R3(
    R=10) "Resistive load 3"
  Loads.Impedance load_sc(R=0) "Short circuit"
  TwoPortInductance Lline_sc(L=Lbase)
    "Inductive line connected to the short circuit"
  TwoPortInductance Lline_1(L=Lbase) "Inductive line connected to load 1"
  TwoPortInductance Lline_2a(L=0.5*Lbase) "Inductive line connected to load 2"
  TwoPortInductance Lline_2b(L=0.5*Lbase) "Inductive line connected to load 2"
  TwoPortInductance Lline_3(L=2*Lbase) "Inductive line connected to load 3"
  TwoPortInductance Lline_3b(L=2*Lbase) "Inductive line connected to load 3"
equation
  connect(E.terminal, Lline_sc.terminal_n)
  connect(Lline_sc.terminal_p, load_sc.terminal)
  connect(E.terminal, Lline_1.terminal_n)
  connect(Lline_1.terminal_p, R1.terminal)
  connect(E.terminal, Lline_2a.terminal_n)
  connect(Lline_2a.terminal_p, Lline_2b.terminal_n)
  connect(Lline_2b.terminal_p, R2.terminal)
  connect(E.terminal, Lline_3.terminal_n)
  connect(E.terminal, Lline_3b.terminal_n)
  connect(Lline_3.terminal_p, R3.terminal)
  connect(Lline_3b.terminal_p, R3.terminal)
end ACLine_L;


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

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


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

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


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

within AixLib.Electrical.AC.OnePhase.Lines.Examples;
model ACSimpleGrid "Test model for a network model"
  extends Modelica.Icons.Example;
  Network network(
    redeclare AixLib.Electrical.Transmission.Grids.TestGrid2Nodes grid,
    V_nominal=120)
    "Network model that represents the connection between the source and the load"
  Loads.Inductive load(
    mode=Types.Load.VariableZ_P_input,
    V_nominal=120) "Load connected to the network"
  Sources.FixedVoltage E(f=60, V=120) "Voltage source"
  Modelica.Blocks.Sources.Ramp load_inputs(
    height=5000,
    duration=2,
    offset=-2000,
    startTime=0.5) "Input signal for the power consumption of the loads"
equation
  connect(load.terminal, network.terminal[2])
  connect(E.terminal, network.terminal[1])
  connect(load_inputs.y, load.Pow)
end ACSimpleGrid;


Package with example models

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


within AixLib.Electrical.AC.OnePhase.Loads;
model Capacitive "Model of a capacitive and resistive load"
  extends AixLib.Electrical.Interfaces.CapacitiveLoad(
    redeclare package PhaseSystem = PhaseSystems.OnePhase,
    redeclare replaceable Interfaces.Terminal_n terminal,
    V_nominal(start = 110));

protected
  Modelica.Units.SI.Angle theRef "Absolute angle of rotating reference system";

initial equation
  if mode == AixLib.Electrical.Types.Load.FixedZ_dynamic then
    // q = Y[2]*{V_nominal, 0}/omega;
    // Steady state initialization
    der(q) = zeros(PhaseSystem.n);
  end if;
equation
  theRef = PhaseSystem.thetaRef(terminal.theta);
  omega = der(theRef);

  if mode == AixLib.Electrical.Types.Load.FixedZ_dynamic then

    // Use the dynamic phasorial representation
    Y[1] = -(P_nominal/pf)*pf/V_nominal^2;
    Y[2] = -(P_nominal/pf)*Modelica.Fluid.Utilities.regRoot(1 - pf^2, delta=0.001)/V_nominal^2;

    // Electric charge
    q = Y[2]*{v[1], v[2]}/omega;

    // Dynamics of the system
    der(q) + omega*j(q) + Y[1]*v = i;

  else

    // Use the power specified by the parameter or inputs
    if linearized then
      i[1] = -homotopy(actual= (v[2]*Q + v[1]*P)/(V_nominal^2), simplified= v[1]*Modelica.Constants.eps*1e3);
      i[2] = -homotopy(actual= (v[2]*P - v[1]*Q)/(V_nominal^2), simplified= v[2]*Modelica.Constants.eps*1e3);
    else
      if initMode == AixLib.Electrical.Types.InitMode.zero_current then
        i[1] = -homotopy(actual=(v[2]*Q + v[1]*P)/(v[1]^2 + v[2]^2), simplified=0.0);
        i[2] = -homotopy(actual=(v[2]*P - v[1]*Q)/(v[1]^2 + v[2]^2), simplified=0.0);
      else
        i[1] = -homotopy(actual=(v[2]*Q + v[1]*P)/(v[1]^2 + v[2]^2), simplified=(v[2]*Q + v[1]*P)/(V_nominal^2));
        i[2] = -homotopy(actual=(v[2]*P - v[1]*Q)/(v[1]^2 + v[2]^2), simplified=(v[2]*P - v[1]*Q)/(V_nominal^2));
      end if;

    end if;

    Y = {0, 0};
    q = {0, 0};

  end if;
end Capacitive;


Model of a generic impedance

within AixLib.Electrical.AC.OnePhase.Loads;
model Impedance "Model of a generic impedance"
  extends AixLib.Electrical.Interfaces.Impedance(
    redeclare replaceable package PhaseSystem = PhaseSystems.OnePhase,
    redeclare replaceable Interfaces.Terminal_n terminal);

protected
  Modelica.Units.SI.Angle theRef "Absolute angle of rotating reference system";
  Modelica.Units.SI.AngularVelocity omega
    "Frequency of the quasi-stationary sine waves";
  Modelica.Units.SI.Reactance X(start=1) "Complex component of the impedance";
equation
  theRef = PhaseSystem.thetaRef(terminal.theta);
  omega = der(theRef);

  if inductive then
    X = omega*L_internal;
  else
    X = -1/(omega*C_internal);
  end if;

  terminal.v = {{R_internal,-X}*terminal.i, {X,R_internal}*terminal.i};
end Impedance;


within AixLib.Electrical.AC.OnePhase.Loads;
model Inductive "Model of an inductive and resistive load"
  extends AixLib.Electrical.Interfaces.InductiveLoad(
    redeclare package PhaseSystem = PhaseSystems.OnePhase,
    redeclare replaceable Interfaces.Terminal_n terminal,
    V_nominal(start = 110));

protected
  Modelica.Units.SI.Angle theRef "Absolute angle of rotating reference system";

initial equation
  if mode == AixLib.Electrical.Types.Load.FixedZ_dynamic then
    // psi = Z[2]*{P_nominal/V_nominal, 0}/omega;
    // Steady state initialization
    der(psi) = zeros(PhaseSystem.n);
  end if;
equation
  theRef = PhaseSystem.thetaRef(terminal.theta);
  omega = der(theRef);

  if mode == AixLib.Electrical.Types.Load.FixedZ_dynamic then

    // Use the dynamic phasorial representation
    Z[1] = -pf*(V_nominal^2)/(P_nominal/pf);
    Z[2] = -Modelica.Fluid.Utilities.regRoot(1-pf^2, delta=0.001)*(V_nominal^2)/(P_nominal/pf);

    // Dynamics of the system
    der(psi) + omega*j(psi) + Z[1]*i = v;

    // Magnetic flux
    psi = Z[2]*{i[1], i[2]}/omega;

  else

    // Use the power specified by the parameter or inputs
    if linearized then
      i[1] = -homotopy(actual= (v[2]*Q + v[1]*P)/(V_nominal^2), simplified= v[1]*Modelica.Constants.eps*1e3);
      i[2] = -homotopy(actual= (v[2]*P - v[1]*Q)/(V_nominal^2), simplified= v[2]*Modelica.Constants.eps*1e3);
    else
      //PhaseSystem.phasePowers_vi(terminal.v, terminal.i) = PhaseSystem.phasePowers(P, Q);
      if initMode == AixLib.Electrical.Types.InitMode.zero_current then
        i[1] = -homotopy(actual = (v[2]*Q + v[1]*P)/(v[1]^2 + v[2]^2), simplified= 0.0);
        i[2] = -homotopy(actual = (v[2]*P - v[1]*Q)/(v[1]^2 + v[2]^2), simplified= 0.0);
      else
        i[1] = -homotopy(actual = (v[2]*Q + v[1]*P)/(v[1]^2 + v[2]^2), simplified= (v[2]*Q + v[1]*P)/(V_nominal^2));
        i[2] = -homotopy(actual = (v[2]*P - v[1]*Q)/(v[1]^2 + v[2]^2), simplified= (v[2]*P - v[1]*Q)/(V_nominal^2));
      end if;
    end if;

    Z = {0,0};
    psi = {0,0};

  end if;
end Inductive;


within AixLib.Electrical.AC.OnePhase;
package Loads "Package with load models for one phase AC systems"
  extends Modelica.Icons.VariantsPackage;
end Loads;


Model of a resistive load

within AixLib.Electrical.AC.OnePhase.Loads;
model Resistive "Model of a resistive load"
  extends AixLib.Electrical.Interfaces.ResistiveLoad(
    redeclare package PhaseSystem = PhaseSystems.OnePhase,
    redeclare replaceable Interfaces.Terminal_n terminal,
    V_nominal(start = 110));
equation

  if linearized then
    i[1] = -homotopy(actual = v[1]*P/V_nominal^2, simplified = v[1]*Modelica.Constants.eps*1e3);
    i[2] = -homotopy(actual = v[2]*P/V_nominal^2, simplified = v[2]*Modelica.Constants.eps*1e3);
  else
    if initMode == AixLib.Electrical.Types.InitMode.zero_current then
      i[1] = -homotopy(actual= v[1]*P/(v[1]^2 + v[2]^2),  simplified= 0.0);
      i[2] = -homotopy(actual= v[2]*P/(v[1]^2 + v[2]^2),  simplified= 0.0);
    else
      i[1] = -homotopy(actual= v[1]*P/(v[1]^2 + v[2]^2),
                       simplified= v[1]*P/V_nominal^2);
      i[2] = -homotopy(actual= v[2]*P/(v[1]^2 + v[2]^2),
                       simplified= v[2]*P/V_nominal^2);
    end if;
  end if;
end Resistive;


Example that illustrates the use of dynamic loads. This model compares two dynamic load models that use the dynamic
phasors.

within AixLib.Electrical.AC.OnePhase.Loads.Examples;
model DynamicLoads "Example that illustrates the use of dynamic loads"
  extends Modelica.Icons.Example;
  AixLib.Electrical.AC.OnePhase.Sources.FixedVoltage source(
    f=60,
    V=120) "Voltage source"
  AixLib.Electrical.AC.OnePhase.Loads.Capacitive dynRC(
    pf=0.8,
    mode=AixLib.Electrical.Types.Load.FixedZ_dynamic,
    P_nominal=-1200,
    V_nominal=120) "Dynamic RC load"
  AixLib.Electrical.AC.OnePhase.Lines.TwoPortResistance line(R=0.1)
    "Line resistance"
  AixLib.Electrical.AC.OnePhase.Loads.Inductive dynRL(
    pf=0.8,
    mode=AixLib.Electrical.Types.Load.FixedZ_dynamic,
    P_nominal=-1200,
    V_nominal=120) "Dynamic RL load"
equation
  connect(source.terminal, line.terminal_n)
  connect(line.terminal_p, dynRC.terminal)
  connect(dynRL.terminal, line.terminal_p)
end DynamicLoads;


Package with example models

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


Example that illustrates the use of the load models at constant voltage. This model illustrates the use of the load models.
The first two lines are inductive loads, followed by two capacitive loads and a resistive load.

The inductive load varRL and the capacitive load varRC
have a variable load specified by the inputs Pow and y
respectively.

All the loads have a nominal power of 1kW, and varRL is the only one
that at t=0 produces power 1kW and as the time increases it start to
consume up to 1kW.

within AixLib.Electrical.AC.OnePhase.Loads.Examples;
model ParallelLoads
  "Example that illustrates the use of the load models at constant voltage"
  extends Modelica.Icons.Example;
  AixLib.Electrical.AC.OnePhase.Loads.Inductive varRL(
    mode=AixLib.Electrical.Types.Load.VariableZ_P_input,
    linearized=false,
    V_nominal=120) "Variable inductor and resistor"
  AixLib.Electrical.AC.OnePhase.Sources.FixedVoltage source(f=60, V=120)
    "Voltage source"
  Modelica.Blocks.Sources.Ramp load_y(duration=0.5, startTime=0.2)
    "Input signal for RC load"
  AixLib.Electrical.AC.OnePhase.Loads.Inductive RL(
    P_nominal=-1e3,
    linearized=false,
    V_nominal=120) "Constant inductor and resistor"
  AixLib.Electrical.AC.OnePhase.Loads.Capacitive varRC(mode=AixLib.Electrical.Types.Load.VariableZ_y_input,
      P_nominal=-1e3,
    linearized=false,
    V_nominal=120) "Variable conductor and resistor"
  AixLib.Electrical.AC.OnePhase.Loads.Capacitive RC(mode=AixLib.Electrical.Types.Load.FixedZ_steady_state,
      P_nominal=-1e3,
    linearized=false,
    V_nominal=120) "Constant conductor and resistor"
  AixLib.Electrical.AC.OnePhase.Loads.Resistive R(
    P_nominal=-1e3,
    mode=AixLib.Electrical.Types.Load.FixedZ_steady_state,
    linearized=false,
    V_nominal=120) "Resistive load"
  Modelica.Blocks.Sources.Ramp load_P(
    startTime=0.2,
    duration=0.5,
    height=-2000,
    offset=1000) "Power signal for load varRL"
equation
  connect(source.terminal, varRL.terminal)
  connect(source.terminal, RL.terminal)
  connect(source.terminal, varRC.terminal)
  connect(source.terminal, R.terminal)
  connect(RC.terminal, R.terminal)
  connect(load_y.y, varRC.y)
  connect(load_P.y, varRL.Pow)
end ParallelLoads;


Example that illustrates the use of the load models at constant voltage. This model compares two resistive loads. Model R consumes or produces
a variable amount of power, while model R1 consumes a fixed power.

within AixLib.Electrical.AC.OnePhase.Loads.Examples;
model ParallelResistors
  "Example that illustrates the use of the load models at constant voltage"
  extends Modelica.Icons.Example;
  AixLib.Electrical.AC.OnePhase.Sources.FixedVoltage
    source(f=60, V=120) "Voltage source"
  Modelica.Blocks.Sources.Ramp load(duration=0.5, startTime=0.2,
    height=2400,
    offset=-1200) "Power signal for load R"
  AixLib.Electrical.AC.OnePhase.Loads.Resistive R(
    mode=AixLib.Electrical.Types.Load.VariableZ_P_input,
    V_nominal=120) "Variable resistive load"
  AixLib.Electrical.AC.OnePhase.Loads.Resistive R1(
    mode=AixLib.Electrical.Types.Load.FixedZ_steady_state, P_nominal=-1.2e3,
    V_nominal=120) "Fixed resistive load"
equation
  connect(source.terminal, R.terminal)
  connect(load.y, R.Pow)
  connect(source.terminal, R1.terminal)
end ParallelResistors;


Example that illustrates the use of the impedances. This model shows how to use the impedance model in different configurations:

within AixLib.Electrical.AC.OnePhase.Loads.Examples;
model TestImpedance "Example that illustrates the use of the impedances"
  extends Modelica.Icons.Example;
  AixLib.Electrical.AC.OnePhase.Sources.FixedVoltage V(f=60, V=120)
  AixLib.Electrical.AC.OnePhase.Loads.Impedance Z1(R=0,
    inductive=true,
    L=1/(2*Modelica.Constants.pi*60)) "Inductive impedance"
  AixLib.Electrical.AC.OnePhase.Loads.Impedance Z2(R=0,
    inductive=false,
    C=1/(2*Modelica.Constants.pi*60)) "Capacitive impedance"
  AixLib.Electrical.AC.OnePhase.Loads.Impedance Z3(R=1)
    "Resistive impedance"
  AixLib.Electrical.AC.OnePhase.Loads.Impedance Z4(
    R=1,
    L=1/(2*Modelica.Constants.pi*60)) "Inductive-resistive impedance"
  AixLib.Electrical.AC.OnePhase.Loads.Impedance Z5(
    R=1,
    inductive=false,
    C=1/(2*Modelica.Constants.pi*60)) "Capacitive-resistive impedance"
equation
  connect(V.terminal, Z1.terminal)
  connect(V.terminal, Z2.terminal)
  connect(V.terminal, Z3.terminal)
  connect(V.terminal, Z4.terminal)
  connect(V.terminal, Z5.terminal)
end TestImpedance;


Examples that illustrates how to replicate a three-phase balanced system. This model shows how a balanced three phase system can be represented with three
independent single phase circuits.

within AixLib.Electrical.AC.OnePhase.Loads.Examples;
model ThreePhases
  "Examples that illustrates how to replicate a three-phase balanced system"
  extends Modelica.Icons.Example;
  AixLib.Electrical.AC.OnePhase.Sources.FixedVoltage Va(
    definiteReference=true,
    f=60,
    V=120) "Source phase A"
  AixLib.Electrical.AC.OnePhase.Loads.Impedance Za(
    inductive=true,
    L=1/(2*Modelica.Constants.pi*60),
    R=12) "Impedance phase A"
  AixLib.Electrical.AC.OnePhase.Sources.FixedVoltage Vb(
    definiteReference=true, phiSou=-2.0943951023932,
    f=60,
    V=120) "Source phase B"
  AixLib.Electrical.AC.OnePhase.Loads.Impedance Zb(
    inductive=true,
    L=1/(2*Modelica.Constants.pi*60),
    R=12) "Impedance phase B"
  AixLib.Electrical.AC.OnePhase.Sources.FixedVoltage Vc(
    definiteReference=true, phiSou=2.0943951023932,
    f=60,
    V=120) "Source phase C"
  AixLib.Electrical.AC.OnePhase.Loads.Impedance Zc(
    inductive=true,
    L=1/(2*Modelica.Constants.pi*60),
    R=12) "Impedance phase C"
equation
  connect(Va.terminal, Za.terminal)
  connect(Vb.terminal, Zb.terminal)
  connect(Vc.terminal, Zc.terminal)
end ThreePhases;


Example that illustrates how using variable impedances. This model shows how to vary the resistance,
capacitance or inductance of an impedance model.

within AixLib.Electrical.AC.OnePhase.Loads.Examples;
model VariableImpedance
  "Example that illustrates how using variable impedances"
  extends Modelica.Icons.Example;
  AixLib.Electrical.AC.OnePhase.Sources.FixedVoltage V(f=60, V=120)
    "Voltage source"
  AixLib.Electrical.AC.OnePhase.Loads.Impedance Z_L(
    R=0,
    inductive=true,
    L=1/(2*Modelica.Constants.pi*60),
    use_L_in=true,
    LMin=1/(2*Modelica.Constants.pi*60),
    LMax=2/(2*Modelica.Constants.pi*60)) "Impedance with variable L"
  AixLib.Electrical.AC.OnePhase.Loads.Impedance Z_C(
    R=0,
    inductive=false,
    C=1/(2*Modelica.Constants.pi*60),
    use_C_in=true,
    CMin=1/(2*Modelica.Constants.pi*60),
    CMax=2/(2*Modelica.Constants.pi*60)) "Impedance with variable C"
  AixLib.Electrical.AC.OnePhase.Loads.Impedance Z_R(
    R=1,
    RMin=1,
    RMax=2,
    use_R_in=true,
    L=0) "Impedance with variable R"
  Modelica.Blocks.Sources.Ramp load(duration=0.5, startTime=0.2,
    height=1,
    offset=0) "Input signal for the loads"
equation
  connect(V.terminal, Z_L.terminal)
  connect(V.terminal, Z_C.terminal)
  connect(V.terminal, Z_R.terminal)
  connect(load.y, Z_R.y_R)
  connect(load.y, Z_C.y_C)
  connect(load.y, Z_L.y_L)
end VariableImpedance;


Sensor for power, voltage and current

within AixLib.Electrical.AC.OnePhase.Sensors;
model GeneralizedSensor "Sensor for power, voltage and current"
  extends AixLib.Electrical.Icons.GeneralizedSensor;
  extends AixLib.Electrical.Interfaces.PartialTwoPort(
    redeclare package PhaseSystem_p = PhaseSystems.OnePhase,
    redeclare package PhaseSystem_n = PhaseSystems.OnePhase,
    redeclare Interfaces.Terminal_n terminal_n,
    redeclare Interfaces.Terminal_p terminal_p);
  Modelica.Blocks.Interfaces.RealOutput V(final quantity="ElectricPotential",
                                          final unit="V")=
      AixLib.Electrical.PhaseSystems.OnePhase.systemVoltage(terminal_n.v)
    "Voltage"
  Modelica.Blocks.Interfaces.RealOutput I(final quantity="ElectricCurrent",
                                          final unit="A")=
    AixLib.Electrical.PhaseSystems.OnePhase.systemCurrent(terminal_n.i)
    "Current"
  Modelica.Blocks.Interfaces.RealOutput S[PhaseSystems.OnePhase.n](
                                          each final quantity="Power",
                                          each final unit="W")=
     AixLib.Electrical.PhaseSystems.OnePhase.phasePowers_vi(v=terminal_n.v, i=terminal_n.i)
    "Phase powers"
equation
  connect(terminal_n, terminal_p)
end GeneralizedSensor;


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


Model of a probe that measures RMS voltage and angle. This model represents a probe that measures the RMS voltage and the angle
of the voltage phasor at a given point.

within AixLib.Electrical.AC.OnePhase.Sensors;
model Probe "Model of a probe that measures RMS voltage and angle"
  extends Icons.GeneralizedProbe;
  parameter Modelica.Units.SI.Voltage V_nominal(min=0, start=110)
    "Nominal voltage (V_nominal >= 0)";
  parameter Boolean perUnit = true "If true, display voltage in p.u.";
  replaceable Interfaces.Terminal_n term "Electrical connector"
  Modelica.Blocks.Interfaces.RealOutput V(unit=if perUnit then "1" else "V")
    "Voltage phasor magnitude"
  Modelica.Blocks.Interfaces.RealOutput theta(unit="rad", displayUnit="deg")
    "Voltage phasor angle"
equation
  theta = AixLib.Electrical.PhaseSystems.OnePhase.phase(term.v);
  if perUnit then
    V = AixLib.Electrical.PhaseSystems.OnePhase.systemVoltage(term.v)/V_nominal;
  else
    V = AixLib.Electrical.PhaseSystems.OnePhase.systemVoltage(term.v);
  end if;
  term.i = zeros(AixLib.Electrical.PhaseSystems.OnePhase.n);
end Probe;


This example illustrates how to use the generalized sensor model. This example illustrates the use of the generalized sensor.

within AixLib.Electrical.AC.OnePhase.Sensors.Examples;
model GeneralizedSensor
  "This example illustrates how to use the generalized sensor model"
  extends Modelica.Icons.Example;
  AixLib.Electrical.AC.OnePhase.Sensors.GeneralizedSensor sen
    "Sensor that measures V, I, and S"
  AixLib.Electrical.AC.OnePhase.Loads.Capacitive loa(
    mode=AixLib.Electrical.Types.Load.FixedZ_dynamic,
    P_nominal=-100,
    V_nominal=120) "Constant load"
  AixLib.Electrical.AC.OnePhase.Sources.FixedVoltage sou(f=60, V=120)
    "Voltage source"
  AixLib.Electrical.AC.OnePhase.Lines.TwoPortResistance res(R=0.05)
    "Line resistance"
equation
  connect(sen.terminal_p, loa.terminal)
  connect(sou.terminal, res.terminal_n)
  connect(res.terminal_p, sen.terminal_n)
end GeneralizedSensor;


Package with example models

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


This example illustrates how to use the probe model. This example illustrates the use of the probe model.

within AixLib.Electrical.AC.OnePhase.Sensors.Examples;
model Probe "This example illustrates how to use the probe model"
  extends Modelica.Icons.Example;
  AixLib.Electrical.AC.OnePhase.Loads.Capacitive loaRC(
    mode=AixLib.Electrical.Types.Load.FixedZ_steady_state,
    P_nominal=-10000,
    V_nominal=120) "Constant load"
  AixLib.Electrical.AC.OnePhase.Sources.FixedVoltage sou(f=60, V=120)
    "Voltage source"
  AixLib.Electrical.AC.OnePhase.Lines.TwoPortResistance res1(R=0.1)
    "First line resistance"
  AixLib.Electrical.AC.OnePhase.Sensors.Probe probe_source(V_nominal=120)
    "Probe that measures at the voltage source"
  AixLib.Electrical.AC.OnePhase.Sensors.Probe probe_loadRC(V_nominal=120)
    "Probe that measures at the RC load"
  AixLib.Electrical.AC.OnePhase.Lines.TwoPortResistance res2(R=0.1)
    "Second line resistance"
  AixLib.Electrical.AC.OnePhase.Loads.Inductive loaRL(
    mode=AixLib.Electrical.Types.Load.FixedZ_steady_state, P_nominal=-10000,
    V_nominal=120) "Constant load"
  AixLib.Electrical.AC.OnePhase.Sensors.Probe probe_loadRL(V_nominal=120)
    "Probe that measures at the RL load"
equation
  connect(sou.terminal, res1.terminal_n)
  connect(res1.terminal_p, loaRC.terminal)
  connect(sou.terminal, probe_source.term)
  connect(loaRC.terminal, probe_loadRC.term)
  connect(sou.terminal, res2.terminal_n)
  connect(res2.terminal_p, loaRL.terminal)
  connect(loaRL.terminal, probe_loadRL.term)
end Probe;


Fixed single phase AC voltage source

within AixLib.Electrical.AC.OnePhase.Sources;
model FixedVoltage "Fixed single phase AC voltage source"
  extends AixLib.Electrical.Interfaces.Source(
    redeclare package PhaseSystem = PhaseSystems.OnePhase,
    redeclare replaceable Interfaces.Terminal_p terminal);
  parameter Modelica.Units.SI.Frequency f(start=60) "Frequency of the source";
  parameter Modelica.Units.SI.Voltage V(start=110) "RMS voltage of the source";
  parameter Modelica.Units.SI.Angle phiSou=0 "Phase shift of the source";
protected
  Modelica.Units.SI.Angle thetaRel
    "Absolute angle of rotating system as offset to thetaRef";
equation
  if Connections.isRoot(terminal.theta) then
    PhaseSystem.thetaRef(terminal.theta) =  2*Modelica.Constants.pi*f*time;
  end if;
  thetaRel = PhaseSystem.thetaRel(terminal.theta);
  terminal.v = PhaseSystem.phaseVoltages(V, thetaRel + phiSou);
end FixedVoltage;


Model of a generator

within AixLib.Electrical.AC.OnePhase.Sources;
model Generator "Model of a generator"
  extends AixLib.Electrical.Interfaces.Source(
    redeclare package PhaseSystem = PhaseSystems.OnePhase,
    redeclare replaceable Interfaces.Terminal_p terminal);
  parameter Modelica.Units.SI.Frequency f(start=60) "Frequency of the source";
  parameter Modelica.Units.SI.Angle phiGen(displayUnit="deg") = 0
    "Phase shift of the source";
  Modelica.Units.SI.Current I "value of current";
  Modelica.Blocks.Interfaces.RealInput P
    "Variable power generated by the source"
protected
  Modelica.Units.SI.Angle thetaRel
    "Absolute angle of rotating system as offset to thetaRef";
equation
  if Connections.isRoot(terminal.theta) then
      PhaseSystem.thetaRef(terminal.theta) =  2*Modelica.Constants.pi*f*time;
  end if;

  thetaRel = PhaseSystem.thetaRel(terminal.theta);

  terminal.i = PhaseSystem.phaseCurrents(I, thetaRel+phiGen);
  0 = PhaseSystem.activePower(terminal.v, terminal.i) + P;
end Generator;


Electrical grid. Model that can be used to represent the electrical grid supply.

within AixLib.Electrical.AC.OnePhase.Sources;
model Grid "Electrical grid"

  replaceable AixLib.Electrical.AC.OnePhase.Interfaces.Terminal_p terminal
  parameter Modelica.Units.SI.Frequency f(start=60) "Frequency of the source";
  parameter Modelica.Units.SI.Voltage V(start=110) "RMS voltage of the source";
  parameter Modelica.Units.SI.Angle phiSou=0 "Phase shift angle of the source";
  AixLib.Electrical.AC.Interfaces.PowerOutput P
    "Power consumed from grid if positive, or fed to grid if negative"
  replaceable AixLib.Electrical.AC.OnePhase.Sources.FixedVoltage sou(
    potentialReference=true,
    definiteReference=true,
    final f=f,
    final V=V,
    final phiSou=phiSou) "Voltage source"
equation
  P.real = -sou.S[1];
  P.apparent = Modelica.Fluid.Utilities.regRoot(sou.S[2]^2 + sou.S[1]^2, delta = 0.01);
  P.phi = sou.phi;
  P.cosPhi = cos(sou.phi);
  connect(sou.terminal, terminal)
end Grid;


within AixLib.Electrical.AC.OnePhase;
package Sources "Package with sources models for one phase AC systems"
  extends Modelica.Icons.SourcesPackage;
end Sources;


Simple wind turbine model

within AixLib.Electrical.AC.OnePhase.Sources;
model WindTurbine "Simple wind turbine model"
  extends AixLib.Electrical.BaseClasses.WindTurbine.PartialWindTurbine(
    redeclare package PhaseSystem = AixLib.Electrical.PhaseSystems.OnePhase,
    redeclare replaceable Interfaces.Terminal_p terminal,
    V_nominal(start = 110));

  parameter Real pf(min=0, max=1) = 0.9 "Power factor"
  parameter Real eta_DCAC(min=0, max=1) = 0.9 "Efficiency of DC/AC conversion"
  replaceable AixLib.Electrical.AC.OnePhase.Loads.Capacitive load(
    final mode=AixLib.Electrical.Types.Load.VariableZ_P_input,
    final pf=pf,
    final P_nominal=0,
    final V_nominal=V_nominal) "Load model"
protected
  Modelica.Blocks.Math.Gain gain_DCAC(final k=eta_DCAC)
equation
  connect(load.terminal, terminal)
  connect(gain_DCAC.y, load.Pow)
  connect(gain.y, gain_DCAC.u)
end WindTurbine;


This example illustrates how using a fixed voltage source. This example shows how to use a fixed voltage generator model.

within AixLib.Electrical.AC.OnePhase.Sources.Examples;
model FixedVoltageSource
  "This example illustrates how using a fixed voltage source"
  extends Modelica.Icons.Example;
  AixLib.Electrical.AC.OnePhase.Loads.Inductive RL(
      P_nominal=-300, mode=AixLib.Electrical.Types.Load.FixedZ_steady_state,
    V_nominal=120) "Load model"
  FixedVoltage grid(
    f=60,
    V=120,
    phiSou=0.34906585039887) "AC one phase electrical grid"
  Sensors.Probe sen(V_nominal=120)
    "Probe that measures the voltage at the load"
equation
  connect(grid.terminal, RL.terminal)
  connect(grid.terminal, sen.term)
end FixedVoltageSource;


Package with example models

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


This example illustrates how using a variable power source. This example shows how to use a variable generator model. The model has to be used
together with a voltage source generator.

within AixLib.Electrical.AC.OnePhase.Sources.Examples;
model VariablePowerSource
  "This example illustrates how using a variable power source"
  extends Modelica.Icons.Example;
  AixLib.Electrical.AC.OnePhase.Sources.Generator generator(phiGen(displayUnit="deg") = 0.26179938779915, f=60)
    "AC generator model"
  Modelica.Blocks.Sources.Sine generation(
    offset=200,
    startTime=1,
    amplitude=100,
    f=0.05) "Generated power"
  AixLib.Electrical.AC.OnePhase.Loads.Inductive RL(mode=Types.Load.VariableZ_y_input,
      P_nominal=-300,
    V_nominal=120) "Load model"
  AixLib.Electrical.AC.OnePhase.Sources.Grid grid(f=60, V=120)
    "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;


Example for the WindTurbine AC model. This model illustrates the use of the wind turbine model which is connected to a AC voltage source and a resistance.
This voltage source can represent the grid to which the
circuit is connected.
Wind data for San Francisco, CA, are used.
The turbine cut-in wind speed is <i>3.5</i> m/s,
and hence it is off in the first day when the wind speed is low.

within AixLib.Electrical.AC.OnePhase.Sources.Examples;
model WindTurbine "Example for the WindTurbine AC model"
  extends Modelica.Icons.Example;
  AixLib.Electrical.AC.OnePhase.Sources.WindTurbine  tur(
    table=[3.5, 0;
           5.5,   100;
           12, 900;
           14, 1000;
           25, 1000], h=10,
    scale=10,
    V_nominal=120) "Wind turbine"
  AixLib.BoundaryConditions.WeatherData.ReaderTMY3 weaDat(
      computeWetBulbTemperature=false, filNam=Modelica.Utilities.Files.loadResource("modelica://AixLib/Resources/weatherdata/USA_CA_San.Francisco.Intl.AP.724940_TMY3.mos"))
    "Weather data"
  AixLib.BoundaryConditions.WeatherData.Bus weaBus "Weather bus"
  AixLib.Electrical.AC.OnePhase.Loads.Resistive
                                            res(P_nominal=-500, V_nominal=120)
    "Resistive line"
  AixLib.Electrical.AC.OnePhase.Sources.Grid sou(f=60, V=120)
    "Voltage source"
  AixLib.Electrical.AC.OnePhase.Lines.TwoPortResistance
                                                  lin(R=0.1)
    "Transmission line"
  AixLib.Electrical.AC.OnePhase.Sensors.GeneralizedSensor
                                                    sen "Generalized sensor"
equation
  connect(weaDat.weaBus,weaBus)
  connect(weaBus.winSpe,tur. vWin)
  connect(sou.terminal, lin.terminal_n)
  connect(sou.terminal, res.terminal)
  connect(lin.terminal_p, sen.terminal_n)
  connect(sen.terminal_p, tur.terminal)
end WindTurbine;


Three phases balanced AC systems

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


AC AC converter three phase balanced systems. This model represents a simplified conversion between two AC
three-phase balanced systems. The conversion losses are represented by a
constant efficiency <i>η</i>.

within AixLib.Electrical.AC.ThreePhasesBalanced.Conversion;
model ACACConverter "AC AC converter three phase balanced systems"
  extends AixLib.Electrical.AC.OnePhase.Conversion.ACACConverter(
    redeclare Interfaces.Terminal_n terminal_n,
    redeclare Interfaces.Terminal_p terminal_p);
end ACACConverter;


AC AC transformer three phase balanced systems. Simple transformer model for three-phase
balanced AC systems. The model does not include core and
magnetic losses.

within AixLib.Electrical.AC.ThreePhasesBalanced.Conversion;
model ACACTransformer "AC AC transformer three phase balanced systems"
  extends AixLib.Electrical.AC.OnePhase.Conversion.ACACTransformer(
    redeclare Interfaces.Terminal_n terminal_n,
    redeclare Interfaces.Terminal_p terminal_p);
end ACACTransformer;


AC AC transformer full model for three phase balanced systems. Model of a transformer for three-phase
balanced AC systems. The model includes core and magnetic losses.

within AixLib.Electrical.AC.ThreePhasesBalanced.Conversion;
model ACACTransformerFull
  "AC AC transformer full model for three phase balanced systems"
  extends AixLib.Electrical.AC.OnePhase.Conversion.ACACTransformerFull(
    redeclare Interfaces.Terminal_n terminal_n,
    redeclare Interfaces.Terminal_p terminal_p);
end ACACTransformerFull;


AC DC converter. This model represents a simplified conversion between a three-phase
balanced AC system and a DC systems.

within AixLib.Electrical.AC.ThreePhasesBalanced.Conversion;
model ACDCConverter "AC DC converter"
  extends AixLib.Electrical.AC.OnePhase.Conversion.ACDCConverter(
    redeclare Interfaces.Terminal_n terminal_n);
end ACDCConverter;


within AixLib.Electrical.AC.ThreePhasesBalanced;
package Conversion "Package with transformers and converter models for three-phase balanced AC systems"
  extends Modelica.Icons.Package;
end Conversion;


This example illustrates how to use the AC/AC converter model. This example illustrates the use of a model that converts AC voltage to AC voltage.
The transformer model assumes a linear loss when transmitting the power.

within AixLib.Electrical.AC.ThreePhasesBalanced.Conversion.Examples;
model ACACConverter
  "This example illustrates how to use the AC/AC converter model"
  extends Modelica.Icons.Example;

  AixLib.Electrical.AC.ThreePhasesBalanced.Conversion.ACACConverter
    conACAC(eta=0.9, conversionFactor=120/480) "ACAC transformer"
  AixLib.Electrical.AC.ThreePhasesBalanced.Sources.FixedVoltage sou(
    definiteReference=true,
    f=60,
    V=480) "Voltage source"
  AixLib.Electrical.AC.ThreePhasesBalanced.Loads.Inductive load(
    mode=AixLib.Electrical.Types.Load.VariableZ_P_input,
    V_nominal=120) "Load model"
  Modelica.Blocks.Sources.Ramp ramp(
    duration=0.5,
    startTime=0.3,
    height=2000,
    offset=-1000) "Power consumed by the model"
equation
  connect(sou.terminal, conACAC.terminal_n)
  connect(conACAC.terminal_p, load.terminal)
  connect(ramp.y, load.Pow)
end ACACConverter;


This example illustrates how to use the AC/AC simplified transformer model. This example illustrates the use of the AC/AC transformer model.
The example shows three different configurations:

within AixLib.Electrical.AC.ThreePhasesBalanced.Conversion.Examples;
model ACACTransformer
  "This example illustrates how to use the AC/AC simplified transformer model"
  extends Modelica.Icons.Example;
  AixLib.Electrical.AC.ThreePhasesBalanced.Conversion.ACACTransformer tra_load(
    Zperc=0.03,
    VABase=4000,
    XoverR=8,
    VHigh=480,
    VLow=120) "Transformer with load"
  AixLib.Electrical.AC.ThreePhasesBalanced.Sources.FixedVoltage sou(
    definiteReference=true,
    f=60,
    V=480) "Voltage source"
  AixLib.Electrical.AC.ThreePhasesBalanced.Loads.Inductive load(
    mode=AixLib.Electrical.Types.Load.VariableZ_P_input,
    pf=0.8,
    V_nominal=120) "Load model"
  Modelica.Blocks.Sources.Ramp ramp(
    duration=0.5,
    startTime=0.3,
    offset=0,
    height=-4000*0.8) "Load power consumption profile"
  AixLib.Electrical.AC.ThreePhasesBalanced.Conversion.ACACTransformer tra_cc(
    XoverR=8,
    Zperc=0.03,
    VABase=4000,
    VHigh=480,
    VLow=120) "Transformer with short circuit"
  AixLib.Electrical.AC.ThreePhasesBalanced.Loads.Impedance shortCircuit(R=1e-8)
    "Short circuit"
  AixLib.Electrical.AC.ThreePhasesBalanced.Conversion.ACACTransformer tra_void(
    XoverR=8,
    Zperc=0.03,
    VABase=4000,
    VHigh=480,
    VLow=120) "Transformer with secondary not connected"
  AixLib.Electrical.AC.ThreePhasesBalanced.Sources.FixedVoltage sou1(
    definiteReference=true,
    f=60,
    V=480) "Voltage source for open and short circuit tests"
equation
  connect(sou.terminal, tra_load.terminal_n)
  connect(tra_load.terminal_p, load.terminal)
  connect(ramp.y, load.Pow)
  connect(tra_cc.terminal_p, shortCircuit.terminal)
  connect(sou1.terminal, tra_cc.terminal_n)
  connect(sou1.terminal, tra_void.terminal_n)
end ACACTransformer;


This example illustrates how to use the AC/AC transformer model. This example illustrates the use of the AC/AC transformer model
that includes losses at the primary and secondary side and magnetization
effects.
The example shows three different configurations:

within AixLib.Electrical.AC.ThreePhasesBalanced.Conversion.Examples;
model ACACTransformerFull
  "This example illustrates how to use the AC/AC transformer model"
  extends Modelica.Icons.Example;
  AixLib.Electrical.AC.ThreePhasesBalanced.Conversion.ACACTransformerFull tra_load(
    R1=0.0001,
    L1=0.0001,
    R2=0.0001,
    L2=0.0001,
    VABase=4000,
    magEffects=true,
    Rm=10,
    Lm=10,
    VHigh=480,
    VLow=120,
    f=60) "Transformer with load"
  AixLib.Electrical.AC.ThreePhasesBalanced.Sources.FixedVoltage sou(
    definiteReference=true,
    f=60,
    V=480) "Voltage source"
  AixLib.Electrical.AC.ThreePhasesBalanced.Loads.Inductive load(
    mode=AixLib.Electrical.Types.Load.VariableZ_P_input,
    pf=0.8,
    V_nominal=120) "Load"
  Modelica.Blocks.Sources.Ramp ramp(
    duration=0.5,
    startTime=0.3,
    offset=0,
    height=-4000*0.8) "Load power consumption profile"
  AixLib.Electrical.AC.ThreePhasesBalanced.Conversion.ACACTransformerFull tra_cc(
    VABase=4000,
    R1=0.01,
    L1=0.01,
    R2=0.01,
    L2=0.01,
    magEffects=false,
    Rm=100,
    Lm=100,
    VHigh=480,
    VLow=120,
    f=60) "Transformer with short circuit connection"
  AixLib.Electrical.AC.ThreePhasesBalanced.Loads.Impedance shortCircuit(R=1e-8)
    "Short circuit"
  AixLib.Electrical.AC.ThreePhasesBalanced.Conversion.ACACTransformerFull tra_void(
    VABase=4000,
    R1=0.01,
    L1=0.01,
    R2=0.01,
    L2=0.01,
    magEffects=false,
    Rm=100,
    Lm=100,
    VHigh=480,
    VLow=120,
    f=60) "Transformer with open connection"
  AixLib.Electrical.AC.ThreePhasesBalanced.Sources.FixedVoltage sou1(
    definiteReference=true,
    f=60,
    V=480) "Voltage source for short circuit and open tests"
equation
  connect(sou.terminal, tra_load.terminal_n)
  connect(tra_load.terminal_p, load.terminal)
  connect(ramp.y, load.Pow)
  connect(tra_cc.terminal_p, shortCircuit.terminal)
  connect(sou1.terminal, tra_cc.terminal_n)
  connect(sou1.terminal, tra_void.terminal_n)
end ACACTransformerFull;


This example illustrates how to use the AC/DC converter model. This example illustrates the use of a model that converts AC voltage to DC voltage.
The transformer model assumes a linear loss when transmitting the power.

within AixLib.Electrical.AC.ThreePhasesBalanced.Conversion.Examples;
model ACDCConverter
  "This example illustrates how to use the AC/DC converter model"
  extends Modelica.Icons.Example;
  AixLib.Electrical.DC.Loads.Resistor res(
    R=1,
    V_nominal=120,
    i(each start=0))
    "Resistive load"
  AixLib.Electrical.AC.ThreePhasesBalanced.Conversion.ACDCConverter conversion(
    eta=0.9,
    ground_AC=false,
    ground_DC=true,
    conversionFactor=120/480) "AC/DC transformer"
  AixLib.Electrical.AC.ThreePhasesBalanced.Sources.FixedVoltage sou(
    definiteReference=true,
    f=60,
    V=480) "Voltage source"
  AixLib.Electrical.DC.Loads.Conductor load(
    mode=AixLib.Electrical.Types.Load.VariableZ_P_input,
    V_nominal=120) "Variable resistive load"
  Modelica.Blocks.Sources.Ramp pow(
    duration=0.5,
    startTime=0.2,
    offset=-200,
    height=5200) "Variable load profile"
equation
  connect(sou.terminal, conversion.terminal_n)
  connect(conversion.terminal_p, res.terminal)
  connect(conversion.terminal_p, load.terminal)
  connect(pow.y, load.Pow)
end ACDCConverter;


Package with example models

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


within AixLib.Electrical.AC.ThreePhasesBalanced;
package Interfaces "Package interfaces for three-phase balanced AC systems"
  extends Modelica.Icons.InterfacesPackage;
end Interfaces;


within AixLib.Electrical.AC.ThreePhasesBalanced.Interfaces;
connector Terminal_n "AC three-phase balanced terminal ('negative')"
  extends AixLib.Electrical.Interfaces.Terminal(
    redeclare replaceable package PhaseSystem = PhaseSystems.OnePhase);
end Terminal_n;


within AixLib.Electrical.AC.ThreePhasesBalanced.Interfaces;
connector Terminal_p "AC three-phase balanced terminal ('positive')"
  extends AixLib.Electrical.Interfaces.Terminal(
    redeclare replaceable package PhaseSystem = PhaseSystems.OnePhase);
end Terminal_p;


Model of an electrical line. This model represents a cable for three-phase balanced AC systems. The model is based on

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

within AixLib.Electrical.AC.ThreePhasesBalanced.Lines;
model Line "Model of an electrical line"
  extends AixLib.Electrical.AC.OnePhase.Lines.Line(
    redeclare Interfaces.Terminal_n terminal_n,
    redeclare Interfaces.Terminal_p terminal_p,
    V_nominal(start=480),
    redeclare TwoPortRLC line(
     R=R,
     L=L,
     C=C,
     V_nominal=V_nominal));
end Line;


Three phases balanced AC network. This model represents a generalized electrical AC three-phase balanced network.

within AixLib.Electrical.AC.ThreePhasesBalanced.Lines;
model Network "Three phases balanced AC network"
  extends AixLib.Electrical.Transmission.BaseClasses.PartialNetwork(
    V_nominal(start = 480),
    redeclare Interfaces.Terminal_p terminal,
    redeclare replaceable Transmission.Grids.TestGrid2Nodes grid,
    redeclare Lines.Line lines(
    redeclare replaceable
        AixLib.Electrical.Transmission.LowVoltageCables.Generic commercialCable=grid.cables));
  Modelica.Units.SI.Voltage VAbs[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[i] = AixLib.Electrical.PhaseSystems.OnePhase.systemVoltage(terminal[i].v);
  end for;
end Network;


within AixLib.Electrical.AC.ThreePhasesBalanced;
package Lines "Package with line models for three-phase balanced AC systems"
  extends Modelica.Icons.Package;
end Lines;


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

within AixLib.Electrical.AC.ThreePhasesBalanced.Lines;
model TwoPortInductance "Model of an inductance with two electrical ports"
  extends AixLib.Electrical.AC.OnePhase.Lines.TwoPortInductance(
    redeclare Interfaces.Terminal_n terminal_n,
    redeclare Interfaces.Terminal_p terminal_p);
end TwoPortInductance;


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

within AixLib.Electrical.AC.ThreePhasesBalanced.Lines;
model TwoPortResistance "Model of a resistance with two electrical ports"
  extends AixLib.Electrical.AC.OnePhase.Lines.TwoPortResistance(
    redeclare Interfaces.Terminal_n terminal_n,
    redeclare Interfaces.Terminal_p terminal_p);
end TwoPortResistance;


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

within AixLib.Electrical.AC.ThreePhasesBalanced.Lines;
model TwoPortRL
  "Model of a resistive-inductive element with two electrical ports"
  extends AixLib.Electrical.AC.OnePhase.Lines.TwoPortRL(
    redeclare Interfaces.Terminal_n terminal_n,
    redeclare Interfaces.Terminal_p terminal_p);
end TwoPortRL;


Model of a RLC element with two electrical ports. RLC impedance that connects two AC three-phase
balanced interfaces. This model can be used to represent a
cable in a three-phase balanced AC system.

within AixLib.Electrical.AC.ThreePhasesBalanced.Lines;
model TwoPortRLC "Model of a RLC element with two electrical ports"
  extends AixLib.Electrical.AC.OnePhase.Lines.TwoPortRLC(
    redeclare Interfaces.Terminal_n terminal_n,
    redeclare Interfaces.Terminal_p terminal_p);
end TwoPortRLC;


Test model for single phase lines that use commercial cables. This example demonstrates how to use a line model to connect
a source to a load that uses commercial cables.

within AixLib.Electrical.AC.ThreePhasesBalanced.Lines.Examples;
model ACLine "Test model for single phase lines that use commercial cables"
  extends Modelica.Icons.Example;
  ThreePhasesBalanced.Sources.FixedVoltage E(definiteReference=true,
    f=60,
    V=480) "Voltage source"
  ThreePhasesBalanced.Loads.Impedance R1(R=10) "Resistive load 1"
  ThreePhasesBalanced.Lines.Line line_1(
    P_nominal=5000,
    l=2000,
    mode=Types.CableMode.commercial,
    commercialCable=Transmission.LowVoltageCables.Cu50(),
    V_nominal=480) "Resistive line that connects to load 1"
  ThreePhasesBalanced.Lines.Line line_2a(
    P_nominal=5000,
    mode=Types.CableMode.commercial,
    commercialCable=Transmission.LowVoltageCables.Cu50(),
    l=1000,
    V_nominal=480) "Resistive line that connects to load 2"
  ThreePhasesBalanced.Loads.Impedance R2(R=10) "Resistive load 2"
  ThreePhasesBalanced.Lines.Line line_2b(
    P_nominal=5000,
    mode=Types.CableMode.commercial,
    commercialCable=Transmission.LowVoltageCables.Cu50(),
    l=1000,
    V_nominal=480) "Resistive line that connects to load 2"
  ThreePhasesBalanced.Lines.Line line_3a(
    P_nominal=5000,
    mode=Types.CableMode.commercial,
    commercialCable=Transmission.LowVoltageCables.Cu50(),
    l=4000,
    V_nominal=480) "Resistive line that connects to load 3"
  ThreePhasesBalanced.Lines.Line line_3b(
    P_nominal=5000,
    mode=Types.CableMode.commercial,
    commercialCable=Transmission.LowVoltageCables.Cu50(),
    l=4000,
    V_nominal=480) "Resistive line that connects to load 3"
  ThreePhasesBalanced.Loads.Impedance R3(R=10) "Resistive load 3"
  ThreePhasesBalanced.Lines.Line line_sc(
    P_nominal=5000,
    l=2000,
    mode=Types.CableMode.commercial,
    commercialCable=Transmission.LowVoltageCables.Cu50(),
    V_nominal=480) "Line that connects the source and the short circuit"
  ThreePhasesBalanced.Loads.Impedance load_sc(R=0) "Short circuit"
equation
  connect(line_1.terminal_p, R1.terminal)
  connect(E.terminal, line_1.terminal_n)
  connect(E.terminal, line_2a.terminal_n)
  connect(line_2a.terminal_p, line_2b.terminal_n)
  connect(line_2b.terminal_p, R2.terminal)
  connect(line_3a.terminal_p, R3.terminal)
  connect(line_3b.terminal_p, R3.terminal)
  connect(E.terminal, line_3a.terminal_n)
  connect(E.terminal, line_3b.terminal_n)
  connect(line_sc.terminal_p, load_sc.terminal)
  connect(E.terminal, line_sc.terminal_n)
end ACLine;


Test model for a single phase line that uses medium voltage commercial cable information . This example demonstrates how to use a line model to connect
a source to a load that uses medium voltage commercial cable information.

within AixLib.Electrical.AC.ThreePhasesBalanced.Lines.Examples;
model ACLineMedium
  "Test model for a single phase line that uses medium voltage commercial cable information "
  extends Modelica.Icons.Example;
  ThreePhasesBalanced.Sources.FixedVoltage E(definiteReference=true, V=15000,
    f=60) "Voltage source"
  ThreePhasesBalanced.Loads.Impedance R1(R=10) "Resistive load 1"
  ThreePhasesBalanced.Lines.Line line_1(
    P_nominal=5000,
    l=2000,
    redeclare AixLib.Electrical.Transmission.MediumVoltageCables.Generic
          commercialCable = AixLib.Electrical.Transmission.MediumVoltageCables.Annealed_Al_10(),
    V_nominal=15000) "Resistive line that connects to load 1"
  ThreePhasesBalanced.Lines.Line line_2a(
    P_nominal=5000,
    l=1000,
    redeclare AixLib.Electrical.Transmission.MediumVoltageCables.Generic
          commercialCable = AixLib.Electrical.Transmission.MediumVoltageCables.Annealed_Al_10(),
    V_nominal=15000) "Resistive line that connects to load 2"
  ThreePhasesBalanced.Loads.Impedance R2(R=10) "Resistive load 2"
  ThreePhasesBalanced.Lines.Line line_2b(
    P_nominal=5000,
    l=1000,
    redeclare AixLib.Electrical.Transmission.MediumVoltageCables.Generic
          commercialCable = AixLib.Electrical.Transmission.MediumVoltageCables.Annealed_Al_10(),
    V_nominal=15000) "Resistive line that connects to load 2"
  ThreePhasesBalanced.Lines.Line line_3a(
    P_nominal=5000,
    l=4000,
    redeclare AixLib.Electrical.Transmission.MediumVoltageCables.Generic
          commercialCable = AixLib.Electrical.Transmission.MediumVoltageCables.Annealed_Al_10(),
    V_nominal=15000) "Resistive line that connects to load 3"
  ThreePhasesBalanced.Lines.Line line_3b(
    P_nominal=5000,
    l=4000,
    redeclare AixLib.Electrical.Transmission.MediumVoltageCables.Generic
          commercialCable = AixLib.Electrical.Transmission.MediumVoltageCables.Annealed_Al_10(),
    V_nominal=15000) "Resistive line that connects to load 3"
  ThreePhasesBalanced.Loads.Impedance R3(R=10) "Resistive load 3"
  ThreePhasesBalanced.Lines.Line line_sc(
    P_nominal=5000,
    l=2000,
    redeclare AixLib.Electrical.Transmission.MediumVoltageCables.Generic
          commercialCable = AixLib.Electrical.Transmission.MediumVoltageCables.Annealed_Al_10(),
    V_nominal=15000) "Line that connects the source and the short circuit"
  ThreePhasesBalanced.Loads.Impedance load_sc(R=0) "Short circuit"
equation
  connect(line_1.terminal_p, R1.terminal)
  connect(E.terminal, line_1.terminal_n)
  connect(E.terminal, line_2a.terminal_n)
  connect(line_2a.terminal_p, line_2b.terminal_n)
  connect(line_2b.terminal_p, R2.terminal)
  connect(line_3a.terminal_p, R3.terminal)
  connect(line_3b.terminal_p, R3.terminal)
  connect(E.terminal, line_3a.terminal_n)
  connect(E.terminal, line_3b.terminal_n)
  connect(line_sc.terminal_p, load_sc.terminal)
  connect(E.terminal, line_sc.terminal_n)
end ACLineMedium;


Test model for single phase inductive lines. This example demonstrates how to use a purely inductive line model to connect
a source to a load.

within AixLib.Electrical.AC.ThreePhasesBalanced.Lines.Examples;
model ACLine_L "Test model for single phase inductive lines"
  extends Modelica.Icons.Example;
  parameter Modelica.Units.SI.Inductance Lbase=10/2/Modelica.Constants.pi/60
    "Base value for the line inductances";
  ThreePhasesBalanced.Sources.FixedVoltage E(definiteReference=true,
    f=60,
    V=480) "Voltage source"
  ThreePhasesBalanced.Loads.Impedance R1(R=10) "Resistive load 1"
  ThreePhasesBalanced.Loads.Impedance R2(R=10) "Resistive load 2"
  ThreePhasesBalanced.Loads.Impedance R3(R=10) "Resistive load 3"
  ThreePhasesBalanced.Loads.Impedance load_sc(R=0) "Short circuit"
  ThreePhasesBalanced.Lines.TwoPortInductance Lline_sc(L=Lbase)
    "Inductive line connected to the short circuit"
  ThreePhasesBalanced.Lines.TwoPortInductance Lline_1(L=Lbase)
    "Inductive line connected to load 1"
  ThreePhasesBalanced.Lines.TwoPortInductance Lline_2a(L=0.5*Lbase)
    "Inductive line connected to load 2"
  ThreePhasesBalanced.Lines.TwoPortInductance Lline_2b(L=0.5*Lbase)
    "Inductive line connected to load 2"
  ThreePhasesBalanced.Lines.TwoPortInductance Lline_3(L=2*Lbase)
    "Inductive line connected to load 3"
  ThreePhasesBalanced.Lines.TwoPortInductance Lline_3b(L=2*Lbase)
    "Inductive line connected to load 3"
equation
  connect(E.terminal, Lline_sc.terminal_n)
  connect(Lline_sc.terminal_p, load_sc.terminal)
  connect(E.terminal, Lline_1.terminal_n)
  connect(Lline_1.terminal_p, R1.terminal)
  connect(E.terminal, Lline_2a.terminal_n)
  connect(Lline_2a.terminal_p, Lline_2b.terminal_n)
  connect(Lline_2b.terminal_p, R2.terminal)
  connect(E.terminal, Lline_3.terminal_n)
  connect(E.terminal, Lline_3b.terminal_n)
  connect(Lline_3.terminal_p, R3.terminal)
  connect(Lline_3b.terminal_p, R3.terminal)
end ACLine_L;


Test model for single phase resistive lines. This example demonstrates how to use a resistive line model to connect
a source to a load.

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


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

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


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

within AixLib.Electrical.AC.ThreePhasesBalanced.Lines.Examples;
model ACSimpleGrid "Test model for a network model"
  extends Modelica.Icons.Example;
  ThreePhasesBalanced.Lines.Network network(
    redeclare AixLib.Electrical.Transmission.Grids.TestGrid2Nodes grid,
      V_nominal=480)
    "Network model that represents the connection between the source and the load"
  ThreePhasesBalanced.Loads.Inductive load(
    P_nominal=2500,
    mode=Types.Load.VariableZ_P_input,
    V_nominal=480) "Load connected to the network"
  ThreePhasesBalanced.Sources.FixedVoltage E(f=60, V=480) "Voltage source"
  Modelica.Blocks.Sources.Ramp load_inputs(
    height=5000,
    duration=2,
    offset=-2000,
    startTime=0.5) "Input signal for the power consumption of the loads"
equation
  connect(load.terminal, network.terminal[2])
  connect(E.terminal, network.terminal[1])
  connect(load_inputs.y, load.Pow)
end ACSimpleGrid;


Test model for a network model with medium voltage. This example demonstrates how to use a network model to connect
a source to a load (using a medium voltage cable). In this simple case the network has two nodes
that are connected by a commercial line cable.

within AixLib.Electrical.AC.ThreePhasesBalanced.Lines.Examples;
model ACSimpleGridMedium "Test model for a network model with medium voltage"
  extends Modelica.Icons.Example;
  ThreePhasesBalanced.Lines.Network network(
      redeclare AixLib.Electrical.Transmission.Grids.TestGrid2NodesMedium grid,
      lines(redeclare
        AixLib.Electrical.Transmission.MediumVoltageCables.Generic
        commercialCable = network.grid.cables,
        each V_nominal = network.V_nominal),
    V_nominal=15000)
    "Network model that represents the connection between the source and the load"
  ThreePhasesBalanced.Loads.Inductive load(mode=Types.Load.VariableZ_P_input,
    P_nominal=250000,
    V_nominal=15000) "Load connected to the network"
  ThreePhasesBalanced.Sources.FixedVoltage E(V=15000, f=60) "Voltage source"
  Modelica.Blocks.Sources.Ramp load_inputs(
    height=5000,
    duration=2,
    offset=-2000,
    startTime=0.5) "Input signal for the power consumption of the loads"
equation
  connect(load.terminal, network.terminal[2])
  connect(E.terminal, network.terminal[1])
  connect(load_inputs.y, load.Pow)
end ACSimpleGridMedium;


Package with example models

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


Model of a capacitive and resistive load. Model of a capacitive load. See

AixLib.Electrical.AC.OnePhase.Loads.Capacitive for more information.

within AixLib.Electrical.AC.ThreePhasesBalanced.Loads;
model Capacitive "Model of a capacitive and resistive load"
  extends AixLib.Electrical.AC.OnePhase.Loads.Capacitive(
    redeclare Interfaces.Terminal_n terminal,
    V_nominal(start=480));
end Capacitive;


Model of a resistive load

within AixLib.Electrical.AC.ThreePhasesBalanced.Loads;
model Impedance "Model of a resistive load"
  extends AixLib.Electrical.Interfaces.Impedance(
    redeclare package PhaseSystem = PhaseSystems.OnePhase,
    redeclare Interfaces.Terminal_n terminal);

  parameter Boolean star = true
    "Type of load connection: true = star, false = triangle"

protected
  Modelica.Units.SI.Angle theRef "Absolute angle of rotating reference system";
  Modelica.Units.SI.AngularVelocity omega
    "Frequency of the quasi-stationary sine waves";
  Modelica.Units.SI.Reactance X(start=1) "Complex component of the impedance";

equation
  theRef = PhaseSystem.thetaRef(terminal.theta);
  omega = der(theRef);

  // Inductance of each line
  if inductive then
    X = omega*L_internal;
  else
    X = -1/(omega*C_internal);
  end if;

  // Ohm's law
  if star then
    terminal.v = {{R_internal,-X}*terminal.i, {X,R_internal}*terminal.i};
  else
    terminal.v = {{R_internal/3,-X/3}*terminal.i, {X/3,R_internal/3}*terminal.i};
  end if;
end Impedance;


Model of an inductive and resistive load. Model of an inductive load. See

AixLib.Electrical.AC.OnePhase.Loads.Inductive for more information.

within AixLib.Electrical.AC.ThreePhasesBalanced.Loads;
model Inductive "Model of an inductive and resistive load"
  extends AixLib.Electrical.AC.OnePhase.Loads.Inductive(
    redeclare Interfaces.Terminal_n terminal,
    V_nominal(start=480));
end Inductive;


within AixLib.Electrical.AC.ThreePhasesBalanced;
package Loads "Package with load models for three-phase balanced AC systems"
  extends Modelica.Icons.VariantsPackage;
end Loads;


Model of a resistive load. Model of a resistive load. See

AixLib.Electrical.AC.OnePhase.Loads.Resistive for more information.

within AixLib.Electrical.AC.ThreePhasesBalanced.Loads;
model Resistive "Model of a resistive load"
  extends AixLib.Electrical.AC.OnePhase.Loads.Resistive(
    redeclare Interfaces.Terminal_n terminal,
    V_nominal(start=480));
end Resistive;


Example that illustrates the use of the impedance models. This model illustrates the use of the impedance models.
The impedances have unitary values such that the RMS value of the voltage and of the current are the same.

within AixLib.Electrical.AC.ThreePhasesBalanced.Loads.Examples;
model Impedances "Example that illustrates the use of the impedance models"
   extends Modelica.Icons.Example;
  Sources.FixedVoltage sou(f=60, V=480) "Voltage source"
  Impedance Z1(R=0,
    inductive=true,
    L=1/(2*Modelica.Constants.pi*60),
    star=true) "Impedance purely inductive"
  Impedance Z2(R=1, star=true) "Impedance purely resistive"
  Impedance Z3(R=0,
    inductive=false,
    C=1/(2*Modelica.Constants.pi*60),
    star=true) "Impedance purely capacitive"
  Impedance Z4(
    inductive=false,
    R=1,
    C=1/(2*Modelica.Constants.pi*60),
    star=true) "Impedance capacitive"
  Impedance Z5(
    R=1,
    inductive=true,
    L=1/(2*Modelica.Constants.pi*60),
    star=true) "Impedance inductive"
equation
  connect(sou.terminal, Z1.terminal)
  connect(sou.terminal, Z2.terminal)
  connect(sou.terminal, Z3.terminal)
  connect(sou.terminal, Z4.terminal)
  connect(sou.terminal, Z5.terminal)
end Impedances;


Package with example models

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


Example that illustrates the use of the load models. This model illustrates the use of the three-phase unbalanced load models.

within AixLib.Electrical.AC.ThreePhasesBalanced.Loads.Examples;
model ParallelLoads "Example that illustrates the use of the load models"
  extends Modelica.Icons.Example;
  Sources.FixedVoltage E(f=60, V=480) "Voltage source"
  Resistive R(P_nominal=-2000, V_nominal=480) "Resistive load"
  Inductive RL_pf(
    pf=0.8,
    P_nominal=-2000,
    use_pf_in=true,
    V_nominal=480) "Inductive load with variable power factor"
  Modelica.Blocks.Sources.Ramp load(              startTime=0.2, duration=0.3)
    "Power signal profile"
  Inductive varRL_y(                                   P_nominal=-2000,
    V_nominal=480,
    mode=AixLib.Electrical.Types.Load.VariableZ_y_input)
    "Inductive load with y as input"
  Capacitive varRC_y(                                   P_nominal=-2000,
    V_nominal=480,
    mode=AixLib.Electrical.Types.Load.VariableZ_y_input)
    "Capacitive load with y as input"
  Inductive varRL_P(pf=0.8,
    V_nominal=480,
    mode=AixLib.Electrical.Types.Load.VariableZ_P_input)
    "Inductive load with P as input"
  Modelica.Blocks.Sources.Ramp pow(
    startTime=0.2,
    duration=0.3,
    height=4000,
    offset=-2000) "Power consumption profile"
  Modelica.Blocks.Sources.Ramp pf(
    height=0.2,
    duration=0.2,
    offset=0.8,
    startTime=0.7) "Power factor profile"
equation
  connect(E.terminal, R.terminal)
  connect(E.terminal, RL_pf.terminal)
  connect(E.terminal, varRL_y.terminal)
  connect(E.terminal, varRC_y.terminal)
  connect(load.y, varRL_y.y)

  connect(load.y, varRC_y.y)
  connect(E.terminal, varRL_P.terminal)
  connect(pow.y, varRL_P.Pow)
  connect(pf.y, RL_pf.pf_in)
end ParallelLoads;


Example that provides a comparison between AC one phase and three-phase balanced. This model illustrates the use of the impedance models and how the three-phase balanced model
can reproduce the same results obtained using three separate one phase circuits.

within AixLib.Electrical.AC.ThreePhasesBalanced.Loads.Examples;
model ThreePhases
  "Example that provides a comparison between AC one phase and three-phase balanced"
  extends Modelica.Icons.Example;
  Modelica.Units.SI.Power errorY=sqrt((sen_Y.S[1] - (sen_a.S[1] + sen_b.S[1] +
      sen_c.S[1]))^2 + (sen_Y.S[2] - (sen_a.S[2] + sen_b.S[2] + sen_c.S[2]))^2)
    "Difference of the power consumption in the star (Y) connection";
  Modelica.Units.SI.Power errorD=sqrt((sen_D.S[1] - (sen_ab.S[1] + sen_bc.S[1]
       + sen_ca.S[1]))^2 + (sen_D.S[2] - (sen_ab.S[2] + sen_bc.S[2] + sen_ca.S[
      2]))^2)
    "Difference of the power consumption in the triangle (D) connection";
  Sources.FixedVoltage sou(definiteReference=true,
    f=60,
    V=480) "Three phases balanced voltage source"
  Impedance RL_star(
    R=1,
    inductive=true,
    L=1/(2*Modelica.Constants.pi*60)) "Impedance with Y connection"
  OnePhase.Sources.FixedVoltage sou_a(V=480/sqrt(3), definiteReference=true,
    f=60) "Voltage source phase a"
  OnePhase.Sources.FixedVoltage sou_b(
    V=480/sqrt(3),
    definiteReference=true,
    phiSou=2.0943951023932,
    f=60) "Voltage source phase b"
  OnePhase.Sources.FixedVoltage sou_c(
    V=480/sqrt(3),
    definiteReference=true,
    phiSou=-2.0943951023932,
    f=60) "Voltage source phase c"
  OnePhase.Loads.Impedance RL_a(
    R=1,
    inductive=true,
    L=1/(2*Modelica.Constants.pi*60)) "Impedance on phase A"
  OnePhase.Loads.Impedance RL_b(
    R=1,
    inductive=true,
    L=1/(2*Modelica.Constants.pi*60)) "Impedance on phase B"
  OnePhase.Loads.Impedance RL_c(
    R=1,
    inductive=true,
    L=1/(2*Modelica.Constants.pi*60)) "Impedance on phase C"
  Impedance RL_tri(
    R=1,
    inductive=true,
    L=1/(2*Modelica.Constants.pi*60),
    star=false) "Impedance with D connection"
  OnePhase.Sources.FixedVoltage sou_ab(
    V=480,
    phiSou=-0.5235987755983,
    definiteReference=true,
    f=60) "Voltage source line ab"
  OnePhase.Sources.FixedVoltage sou_bc(
    phiSou=1.5707963267949,
    V=480,
    definiteReference=true,
    f=60) "Voltage source line bc"
  OnePhase.Sources.FixedVoltage sou_ca(
    phiSou=-3.6651914291881,
    V=480,
    definiteReference=true,
    f=60) "Voltage source line ca"
  OnePhase.Loads.Impedance RL_ab(
    R=1,
    inductive=true,
    L=1/(2*Modelica.Constants.pi*60)) "Impedance on line AB"
  OnePhase.Loads.Impedance RL_bc(
    R=1,
    inductive=true,
    L=1/(2*Modelica.Constants.pi*60)) "Impedance on line BC"
  OnePhase.Loads.Impedance RL_c1(
    R=1,
    inductive=true,
    L=1/(2*Modelica.Constants.pi*60)) "Impedance on linease CA"
  OnePhase.Sensors.GeneralizedSensor sen_a
    "Sensor located on phase A (Y connection)"
  OnePhase.Sensors.GeneralizedSensor sen_ab
    "Sensor located on line AB (D connection)"
  Sensors.GeneralizedSensor sen_Y "Sensor for Y connection (balanced case)"
  Sensors.GeneralizedSensor sen_D "Sensor for D connection (balanced case)"
  OnePhase.Sensors.GeneralizedSensor sen_b
    "Sensor located on phase B (Y connection)"
  OnePhase.Sensors.GeneralizedSensor sen_c
    "Sensor located on phase C (Y connection)"
  OnePhase.Sensors.GeneralizedSensor sen_bc
    "Sensor located on line BC (D connection)"
  OnePhase.Sensors.GeneralizedSensor sen_ca
    "Sensor located on line CA (D connection)"
equation
  connect(sou_a.terminal, sen_a.terminal_n)
  connect(sen_a.terminal_p, RL_a.terminal)
  connect(sou_ab.terminal, sen_ab.terminal_n)
  connect(sen_ab.terminal_p, RL_ab.terminal)
  connect(sou.terminal, sen_Y.terminal_n)
  connect(sen_Y.terminal_p, RL_star.terminal)
  connect(sou.terminal, sen_D.terminal_n)
  connect(sen_D.terminal_p, RL_tri.terminal)
  connect(sou_b.terminal, sen_b.terminal_n)
  connect(sen_b.terminal_p, RL_b.terminal)
  connect(sou_c.terminal, sen_c.terminal_n)
  connect(sen_c.terminal_p, RL_c.terminal)
  connect(sou_bc.terminal, sen_bc.terminal_n)
  connect(sen_bc.terminal_p, RL_bc.terminal)
  connect(sou_ca.terminal, sen_ca.terminal_n)
  connect(sen_ca.terminal_p, RL_c1.terminal)
end ThreePhases;


Sensor for power, voltage and current

within AixLib.Electrical.AC.ThreePhasesBalanced.Sensors;
model GeneralizedSensor "Sensor for power, voltage and current"
  extends AixLib.Electrical.Icons.GeneralizedSensor;
  extends AixLib.Electrical.Interfaces.PartialTwoPort(
    redeclare package PhaseSystem_p = PhaseSystems.OnePhase,
    redeclare package PhaseSystem_n = PhaseSystems.OnePhase,
    redeclare Interfaces.Terminal_n terminal_n(redeclare package PhaseSystem =
          PhaseSystem_n),
    redeclare Interfaces.Terminal_p terminal_p(redeclare package PhaseSystem =
          PhaseSystem_p));
  Modelica.Blocks.Interfaces.RealOutput V(
    final quantity="ElectricPotential",
    final unit="V")=
      AixLib.Electrical.PhaseSystems.OnePhase.systemVoltage(terminal_n.v)
    "Voltage"
  Modelica.Blocks.Interfaces.RealOutput I(
    final quantity="ElectricCurrent",
    final unit="A")=
      AixLib.Electrical.PhaseSystems.OnePhase.systemCurrent(terminal_n.i)
    "Current"
  Modelica.Blocks.Interfaces.RealOutput S[PhaseSystems.OnePhase.n](
    each final quantity="Power",
    each final unit="W")=
      AixLib.Electrical.PhaseSystems.OnePhase.phasePowers_vi(v=terminal_n.v, i=terminal_n.i)
    "Phase powers"
equation
  connect(terminal_n, terminal_p)
end GeneralizedSensor;


within AixLib.Electrical.AC.ThreePhasesBalanced;
package Sensors "Package with sensors models for three-phase balanced AC systems"
extends Modelica.Icons.SensorsPackage;
end Sensors;


Model of a probe that measures RMS voltage and angle. This model represents a probe that measures the RMS voltage and the angle
of the voltage phasor at a given point.

within AixLib.Electrical.AC.ThreePhasesBalanced.Sensors;
model Probe "Model of a probe that measures RMS voltage and angle"
  extends OnePhase.Sensors.Probe(
  redeclare AixLib.Electrical.AC.ThreePhasesBalanced.Interfaces.Terminal_n term,
  V_nominal(start=480));
end Probe;


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

within AixLib.Electrical.AC.ThreePhasesBalanced.Sensors.Examples;
model GeneralizedSensor "Example model for generalized sensor"
  extends Modelica.Icons.Example;
  AixLib.Electrical.AC.ThreePhasesBalanced.Sensors.GeneralizedSensor
    sen "Sensor model"
  AixLib.Electrical.AC.ThreePhasesBalanced.Loads.Capacitive loa(
                   V_nominal=480, P_nominal=-100) "Constant load"
  AixLib.Electrical.AC.ThreePhasesBalanced.Sources.FixedVoltage
    sou(f=60, V=480) "Voltage source"
equation
  connect(sen.terminal_p, loa.terminal)
  connect(sen.terminal_n, sou.terminal)
end GeneralizedSensor;


Package with example models

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


This example illustrates how to use the probe model. This example illustrates the use of the probe model.

within AixLib.Electrical.AC.ThreePhasesBalanced.Sensors.Examples;
model Probe "This example illustrates how to use the probe model"
  extends Modelica.Icons.Example;
  AixLib.Electrical.AC.ThreePhasesBalanced.Loads.Capacitive loaRC(
    mode=AixLib.Electrical.Types.Load.FixedZ_steady_state,
    P_nominal=-10000,
    V_nominal=480) "Constant load"
  AixLib.Electrical.AC.ThreePhasesBalanced.Sources.FixedVoltage sou(f=60, V=
        480) "Voltage source"
  AixLib.Electrical.AC.ThreePhasesBalanced.Lines.TwoPortResistance res1(R=0.1)
    "First line resistance"
  AixLib.Electrical.AC.ThreePhasesBalanced.Sensors.Probe probe_source(V_nominal=
       480) "Probe that measures at the voltage source"
  AixLib.Electrical.AC.ThreePhasesBalanced.Sensors.Probe probe_loadRC(V_nominal=
       480) "Probe that measures at the RC load"
  AixLib.Electrical.AC.ThreePhasesBalanced.Lines.TwoPortResistance res2(R=0.1)
    "Second line resistance"
  AixLib.Electrical.AC.ThreePhasesBalanced.Loads.Inductive loaRL(
    mode=AixLib.Electrical.Types.Load.FixedZ_steady_state,
    P_nominal=-10000,
    V_nominal=480) "Constant load"
  AixLib.Electrical.AC.ThreePhasesBalanced.Sensors.Probe probe_loadRL(V_nominal=
       480) "Probe that measures at the RL load"
equation
  connect(sou.terminal, res1.terminal_n)
  connect(res1.terminal_p, loaRC.terminal)
  connect(sou.terminal, probe_source.term)
  connect(loaRC.terminal, probe_loadRC.term)
  connect(sou.terminal, res2.terminal_n)
  connect(res2.terminal_p, loaRL.terminal)
  connect(loaRL.terminal, probe_loadRL.term)
end Probe;


Fixed three-phase balanced AC voltage source. This is a constant voltage source, specifying the complex voltage
by the RMS voltage and the phase shift.

within AixLib.Electrical.AC.ThreePhasesBalanced.Sources;
model FixedVoltage "Fixed three-phase balanced AC voltage source"
  extends AixLib.Electrical.AC.OnePhase.Sources.FixedVoltage(
    redeclare Interfaces.Terminal_p terminal,
    f(start=60),
    V(start=480));
end FixedVoltage;


Model of a generator. Model of an inductive generator.

within AixLib.Electrical.AC.ThreePhasesBalanced.Sources;
model Generator "Model of a generator"
  extends AixLib.Electrical.AC.OnePhase.Sources.Generator(
    redeclare Interfaces.Terminal_p terminal,
    f(start=60));
end Generator;


Electrical grid. Model that can be used to represent the electrical grid supply.
See
AixLib.Electrical.AC.OnePhase.Sources.Grid for
more information.

within AixLib.Electrical.AC.ThreePhasesBalanced.Sources;
model Grid "Electrical grid"
  extends AixLib.Electrical.AC.OnePhase.Sources.Grid(
    redeclare Interfaces.Terminal_p terminal,
    f(start=60),
    V(start=480),
    redeclare AixLib.Electrical.AC.ThreePhasesBalanced.Sources.FixedVoltage sou);
end Grid;


within AixLib.Electrical.AC.ThreePhasesBalanced;
package Sources "Package with source models for three-phase balanced AC systems"
  extends Modelica.Icons.SourcesPackage;
end Sources;


This example illustrates how using a fixed voltage source. This example shows how to use a fixed voltage generator model.

within AixLib.Electrical.AC.ThreePhasesBalanced.Sources.Examples;
model FixedVoltageSource
  "This example illustrates how using a fixed voltage source"
  extends Modelica.Icons.Example;
  AixLib.Electrical.AC.ThreePhasesBalanced.Loads.Inductive RL(
      P_nominal=-300, mode=AixLib.Electrical.Types.Load.FixedZ_steady_state,
    V_nominal=480) "Load model"
  ThreePhasesBalanced.Sources.FixedVoltage grid(
    f=60,
    V=480,
    phiSou=0.34906585039887) "AC one phase electrical grid"
  ThreePhasesBalanced.Sensors.Probe sen(V_nominal=480)
    "Probe that measures the voltage at the load"
equation
  connect(grid.terminal, RL.terminal)
  connect(grid.terminal, sen.term)
end FixedVoltageSource;


Package with example models

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