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

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


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


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


info

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


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

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


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

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

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

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


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


DC DC converter

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

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

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

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

  // Voltage relation
  v_p = v_n*conversionFactor;

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

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


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


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

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


Package with example models

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


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


Terminal n for DC electrical systems

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


Terminal p for DC electrical systems

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


Model of a DC electrical line

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

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


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


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

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

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

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


Model of a two port DC resistance

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

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


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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

end DCLinesLinearized;


Package with example models

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


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

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

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


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

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

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


Model of a generic DC load

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

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

  if linearized then

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

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

  end if;

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


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


Ideal linear electrical resistor

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


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

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


Package with example models

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


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

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


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

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


Sensor for power, voltage and current

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

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

  connect(terminal_n, terminal_p)
end GeneralizedSensor;


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


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

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


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


Model of a constant DC voltage source

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


Package with models for DC sources

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


Simple PV model

within Buildings.Electrical.DC.Sources;
model PVSimple "Simple PV model"
  extends Buildings.Electrical.Interfaces.PartialPV(
    redeclare package PhaseSystem = PhaseSystems.TwoConductor,
    redeclare Interfaces.Terminal_p terminal);
protected
   Loads.Conductor con(
    mode=Types.Load.VariableZ_P_input,
    V_nominal=V_nominal)
    "Conductor, used to interface power with electrical circuit"
equation
  connect(con.terminal, terminal)

  connect(solarPower.y, con.Pow)
  connect(P, solarPower.y)
end PVSimple;


Simple PV model with orientation

within Buildings.Electrical.DC.Sources;
model PVSimpleOriented "Simple PV model with orientation"
  extends Buildings.Electrical.Interfaces.PartialPVOriented(
    redeclare package PhaseSystem = PhaseSystems.TwoConductor,
    redeclare Interfaces.Terminal_p terminal,
    redeclare Buildings.Electrical.DC.Sources.PVSimple panel(V_nominal=V_nominal));
end PVSimpleOriented;


Model of a generoic DC voltage source

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


within Buildings.Electrical.DC.Sources;
model WindTurbine
  "Wind turbine with power output based on table as a function of wind speed"
  extends Buildings.Electrical.Interfaces.PartialWindTurbine(
    redeclare package PhaseSystem =
        Buildings.Electrical.PhaseSystems.TwoConductor,
    redeclare Buildings.Electrical.DC.Interfaces.Terminal_p terminal);
protected
  Loads.Conductor con(
    mode=Buildings.Electrical.Types.Load.VariableZ_P_input,
    V_nominal=V_nominal)
    "Conductor, used to interface the power with the electrical circuit"
equation
  connect(con.terminal, terminal)
  connect(gain.y, con.Pow)
end WindTurbine;


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


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

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

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


Package with example models

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


Example for the PVSimple model with constant load. This model illustrates the use of the photovoltaic model.
The total solar irradiation is computed based
on a weather data file.
The PV is connected to a circuit that has a constant voltage
source and a resistance.
This voltage source may be a DC grid to which the
circuit is connected.
The power sensor shows how much electrical power is consumed or fed into the voltage source.
In actual systems, the voltage source may be an AC/DC converter.

within Buildings.Electrical.DC.Sources.Examples;
model PVSimple "Example for the PVSimple model with constant load"
  extends Modelica.Icons.Example;
  Buildings.Electrical.DC.Sources.PVSimple     pv(A=10, V_nominal=12)
    "PV module"
  Modelica.Electrical.Analog.Basic.Ground ground
  Buildings.Electrical.DC.Loads.Resistor    res(R=0.5, V_nominal=12)
    "Resistance"
  Buildings.Electrical.DC.Sources.ConstantVoltage    sou(V=12) "Voltage source"
  Buildings.BoundaryConditions.SolarIrradiation.DiffusePerez HDifTil(til=
        0.34906585039887, azi=-0.78539816339745)
                           "Diffuse irradiation on tilted surface"
  Buildings.BoundaryConditions.SolarIrradiation.DirectTiltedSurface HDirTil(til=
        0.34906585039887, azi=-0.78539816339745)
                           "Direct irradiation on tilted surface"
  Buildings.BoundaryConditions.WeatherData.ReaderTMY3 weaDat(
      computeWetBulbTemperature=false,
      filNam=Modelica.Utilities.Files.loadResource("modelica://Buildings/Resources/weatherdata/USA_CA_San.Francisco.Intl.AP.724940_TMY3.mos"))
  Modelica.Blocks.Math.Add G "Total irradiation on tilted surface"
  Buildings.Electrical.DC.Lines.TwoPortResistance lin(R=0.05)
    "Transmission line"
  Buildings.Electrical.DC.Sensors.GeneralizedSensor sen "Sensor"
equation
  connect(weaDat.weaBus, HDifTil.weaBus)
  connect(weaDat.weaBus, HDirTil.weaBus)
  connect(HDifTil.H, G.u1)
  connect(HDirTil.H, G.u2)
  connect(G.y, pv.G)
  connect(sou.terminal, res.terminal)
  connect(lin.terminal_n, res.terminal)
  connect(lin.terminal_p, sen.terminal_n)
  connect(sen.terminal_p, pv.terminal)
  connect(sou.n, ground.p)
end PVSimple;


Example for the PVSimpleOriented model with constant load. This model illustrates the use of the photovoltaic model.
The total solar irradiation is computed internally by the PV
model through a connection to the weather bus.
The PV is connected to a circuit that has a constant voltage
source and a resistance.
This voltage source may be a DC grid to which the
circuit is connected.
The power sensor shows how much electrical power is consumed or fed into the voltage source.
In actual systems, the voltage source may be an AC/DC converter.

within Buildings.Electrical.DC.Sources.Examples;
model PVSimpleOriented
  "Example for the PVSimpleOriented model with constant load"
  extends Modelica.Icons.Example;
  Buildings.Electrical.DC.Sources.PVSimpleOriented
                                               pv(A=10,
    V_nominal=12,
    til=0.34906585039887,
    azi=-0.78539816339745) "PV module"
  Modelica.Electrical.Analog.Basic.Ground ground
  Buildings.Electrical.DC.Loads.Resistor    res(R=0.5, V_nominal=12)
    "Resistance"
  Buildings.Electrical.DC.Sources.ConstantVoltage    sou(V=12) "Voltage source"
  Buildings.BoundaryConditions.WeatherData.ReaderTMY3 weaDat(
      computeWetBulbTemperature=false, filNam=Modelica.Utilities.Files.loadResource("modelica://Buildings/Resources/weatherdata/USA_CA_San.Francisco.Intl.AP.724940_TMY3.mos"))
  Buildings.Electrical.DC.Lines.TwoPortResistance lin(R=0.05)
    "Transmission line"
  Buildings.Electrical.DC.Sensors.GeneralizedSensor sen "Sensor"
equation
  connect(sou.terminal, res.terminal)
  connect(lin.terminal_n, res.terminal)
  connect(lin.terminal_p, sen.terminal_n)
  connect(sen.terminal_p, pv.terminal)
  connect(sou.n, ground.p)
  connect(weaDat.weaBus, pv.weaBus)
end PVSimpleOriented;


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

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


Example for the WindTurbine model. This model illustrates the use of the wind turbine model which is connected to a DC voltage source and a resistance.
This voltage source may be a DC 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 Buildings.Electrical.DC.Sources.Examples;
model WindTurbine "Example for the WindTurbine model"
  extends Modelica.Icons.Example;
  Buildings.Electrical.DC.Sources.WindTurbine           tur(
    table=[3.5, 0;
           5.5,   100;
           12, 900;
           14, 1000;
           25, 1000], h=10,
    V_nominal=12) "Wind turbine"
  Buildings.BoundaryConditions.WeatherData.ReaderTMY3 weaDat(
      computeWetBulbTemperature=false, filNam=Modelica.Utilities.Files.loadResource("modelica://Buildings/Resources/weatherdata/USA_CA_San.Francisco.Intl.AP.724940_TMY3.mos"))
  Buildings.BoundaryConditions.WeatherData.Bus weaBus
  Modelica.Electrical.Analog.Basic.Ground ground
  Buildings.Electrical.DC.Loads.Resistor    res(R=0.5, V_nominal=12)
    "Resistance"
  Buildings.Electrical.DC.Sources.ConstantVoltage    sou(V=12) "Voltage source"
  Buildings.Electrical.DC.Lines.TwoPortResistance lin(R=0.05)
    "Transmission line"
  Buildings.Electrical.DC.Sensors.GeneralizedSensor sen "Sensor"
equation
  connect(weaDat.weaBus,weaBus)
  connect(weaBus.winSpe,tur. vWin)
  connect(sou.terminal, res.terminal)
  connect(lin.terminal_n, res.terminal)
  connect(lin.terminal_p, sen.terminal_n)
  connect(sen.terminal_p, tur.terminal)
  connect(sou.n, ground.p)
end WindTurbine;


Simple model of a battery. Simple model of a battery.

within Buildings.Electrical.DC.Storage;
model Battery "Simple model of a battery"
  parameter Modelica.Units.SI.Efficiency etaCha(max=1) = 0.9
    "Efficiency during charging";
  parameter Modelica.Units.SI.Efficiency etaDis(max=1) = 0.9
    "Efficiency during discharging";
 parameter Real SOC_start(min=0, max=1, unit="1")=0.1 "Initial state of charge";
  parameter Modelica.Units.SI.Energy EMax(min=0, displayUnit="kW.h")
    "Maximum available charge";
  parameter Modelica.Units.SI.Voltage V_nominal
    "Nominal voltage (V_nominal >= 0)";
 Modelica.Blocks.Interfaces.RealInput P(unit="W")
    "Power stored in battery (if positive), or extracted from battery (if negative)"
  Modelica.Blocks.Interfaces.RealOutput SOC(min=0, max=1, unit="1")
    "State of charge"
  Buildings.Electrical.DC.Interfaces.Terminal_p terminal "Generalized terminal"
protected
  Buildings.Electrical.DC.Storage.BaseClasses.Charge cha(
    final EMax=EMax,
    final SOC_start=SOC_start,
    final etaCha=etaCha,
    final etaDis=etaDis) "Charge model"
  Loads.Conductor bat(
    final mode=Buildings.Electrical.Types.Load.VariableZ_P_input,
    final V_nominal=V_nominal) "Power exchanged with battery pack"
  Modelica.Blocks.Math.Gain gain(final k=-1)

equation
  connect(cha.SOC, SOC)
  connect(cha.P, P)

  connect(bat.terminal, terminal)
  connect(P, gain.u)
  connect(gain.y, bat.Pow)
end Battery;


within Buildings.Electrical.DC;
package Storage "Package with models for DC electrical storage"
  extends Modelica.Icons.Package;
end Storage;


Model to compute the battery charge. This model represents the charge/discharge mechanism of a battery.

within Buildings.Electrical.DC.Storage.BaseClasses;
model Charge "Model to compute the battery charge"
  extends Modelica.Blocks.Icons.Block;
  parameter Modelica.Units.SI.Efficiency etaCha(max=1) = 0.9
    "Efficiency during charging";
  parameter Modelica.Units.SI.Efficiency etaDis(max=1) = 0.9
    "Efficiency during discharging";
  parameter Real SOC_start(min=0, max=1, unit="1")=0.1
    "Initial state of charge";
  parameter Modelica.Units.SI.Energy EMax(min=0, displayUnit="kW.h")
    "Maximum available charge";
  Modelica.Units.SI.Power PAct "Actual power";
  Modelica.Blocks.Interfaces.RealInput P(final quantity="Power",
                                         final unit="W")
  Modelica.Blocks.Interfaces.RealOutput SOC(min=0, max=1) "State of charge"

  // etaDisInv avoids use of a divGuarded macro in Dymola in the dynamics section
protected
  final parameter Real etaDisInv = 1/etaDis "Inverse of etaDis";
  final parameter Real invEMax = 1/EMax "Inverse of EMax";

  Boolean underCharged "Flag, true if battery is undercharged";
  Boolean overCharged "Flag, true if battery is overcharged";
initial equation
  pre(underCharged) = SOC_start < 0;
  pre(overCharged)  = SOC_start > 1;

  SOC = SOC_start;
equation
  // Charge balance of battery
  PAct = if P > 0 then etaCha*P else etaDisInv*P;
  der(SOC)=PAct*invEMax;

  // Equations to warn if state of charge exceeds 0 and 1
  underCharged = SOC < 0;
  overCharged = SOC > 1;
  when change(underCharged) or change(overCharged) then
    assert(SOC >= 0, "Warning: Battery is below minimum charge.",
    level=AssertionLevel.warning);
    assert(SOC <= 1, "Warning: Battery is above maximum charge.",
    level=AssertionLevel.warning);
  end when;
end Charge;


Base models for battery package

within Buildings.Electrical.DC.Storage;
package BaseClasses "Base models for battery package"
  extends Modelica.Icons.BasesPackage;
end BaseClasses;


Test model for battery. This model illustrates use of a battery connected to an DC voltage source
and a constant load.
The battery is charged every night at 23:00 until it is full.
At 14:00, it is discharged until it is empty.
This control is implemented using a finite state machine.
The charging and discharging power is assumed to be controlled to
a constant value of <i>10,000</i> Watts.

within Buildings.Electrical.DC.Storage.Examples;
model Battery "Test model for battery"
  extends Modelica.Icons.Example;
  Buildings.Electrical.DC.Storage.Battery     bat(EMax=40e3*3600, V_nominal=12)
    "Battery"
  Buildings.Electrical.DC.Sources.ConstantVoltage    sou(V=12) "Voltage source"
  Modelica.Electrical.Analog.Basic.Ground ground
  Buildings.Electrical.DC.Loads.Conductor             loa(
    P_nominal=0, mode=Buildings.Electrical.Types.Load.VariableZ_P_input,
    V_nominal=12) "Electrical load"
  Modelica.Blocks.Sources.Constant const1(k=-10e3)
    "Power consumption of the load"
  Modelica.Blocks.Sources.SampleTrigger startCharge(period=24*3600,
      startTime=23*3600)
  Modelica.Blocks.Logical.GreaterEqualThreshold greaterEqualThreshold(threshold=
       0.99)
  Modelica.Blocks.Sources.SampleTrigger startDischarge(period=24*3600,
      startTime=14*3600)
  Modelica.Blocks.Logical.LessEqualThreshold lessEqualThreshold(threshold=
        0.01)
  Modelica.Blocks.Logical.Switch chaSwi "Switch to charge battery"
  Modelica.Blocks.Logical.Switch disSwi "Switch to discharge battery"
  Modelica.Blocks.Sources.Constant PCha(k=1e4) "Charging power"
  Modelica.Blocks.Sources.Constant POff(k=0) "Off power"
  Modelica.Blocks.Sources.Constant PDis(k=-1e4) "Discharging power"
  Modelica.Blocks.Math.Add add
  Buildings.Electrical.DC.Sensors.GeneralizedSensor powSen "Power sensor"
  Modelica.StateGraph.InitialStep off(nIn=1, nOut=1)
                                      "Off state"
  Modelica.StateGraph.TransitionWithSignal toOn "Transition to on"
  Modelica.StateGraph.StepWithSignal charge(nIn=1, nOut=1)
                                            "State to charge battery"
  Modelica.StateGraph.TransitionWithSignal toHold "Transition to hold"
  Modelica.StateGraph.Step hold(nIn=1, nOut=1)
                                "Battery charge is hold"
  Modelica.StateGraph.TransitionWithSignal toDischarge
    "Transition to discharge"
  Modelica.StateGraph.StepWithSignal discharge(nIn=1, nOut=1)
                                               "State to discharge battery"
  Modelica.StateGraph.TransitionWithSignal toOff "Transition to off"
  inner Modelica.StateGraph.StateGraphRoot stateGraphRoot
equation

  connect(POff.y, disSwi.u3)
  connect(POff.y, chaSwi.u3)
  connect(PDis.y, disSwi.u1)
  connect(PCha.y, chaSwi.u1)
  connect(add.y, bat.P)
  connect(bat.SOC, greaterEqualThreshold.u)
  connect(bat.SOC, lessEqualThreshold.u)

  connect(const1.y, loa.Pow)
  connect(loa.terminal, sou.terminal)
  connect(bat.terminal, powSen.terminal_p)
  connect(powSen.terminal_n, sou.terminal)
  connect(sou.n, ground.p)
  connect(startCharge.y, toOn.condition)
  connect(charge.active, chaSwi.u2)
  connect(greaterEqualThreshold.y, toHold.condition)
  connect(startDischarge.y, toDischarge.condition)
  connect(disSwi.u2, discharge.active)
  connect(discharge.outPort[1], toOff.inPort)
  connect(lessEqualThreshold.y, toOff.condition)
  connect(off.outPort[1], toOn.inPort)
  connect(toOn.outPort, charge.inPort[1])
  connect(charge.outPort[1], toHold.inPort)
  connect(toHold.outPort, hold.inPort[1])
  connect(hold.outPort[1], toDischarge.inPort)
  connect(toDischarge.outPort, discharge.inPort[1])
  connect(toOff.outPort, off.inPort[1])
  connect(disSwi.y, add.u1)
  connect(chaSwi.y, add.u2)
end Battery;


Package with example models

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


Package with example models

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


Example model that shows the impact of renewable sources on the electrical grid. This model shows the impact of renewables on the electric grid.
The sensors show how the voltage per unit fluctuates depending on
the building load and the power produced by the PV and the wind turbine.
To the right of the model is the post-processing that computes the
ratio of energy produced by the renewables over the energy consumed
by the loads.

within Buildings.Electrical.Examples;
model RenewableSources
  "Example model that shows the impact of renewable sources on the electrical grid"
  extends Modelica.Icons.Example;
  parameter Modelica.Units.SI.Frequency f=60 "Nominal grid frequency";
  parameter Modelica.Units.SI.Voltage V_nominal=480 "Nominal grid voltage";
  parameter Modelica.Units.SI.Power PLoa_nominal=3500 "Nominal power of a load";
  parameter Modelica.Units.SI.Power PWin=PLoa_nominal*4
    "Nominal power of the wind turbine";
  parameter Modelica.Units.SI.Power PSun=PLoa_nominal*1.0
    "Nominal power of the PV";
  parameter Modelica.Units.SI.DensityOfHeatFlowRate W_m2_nominal=1000
    "Nominal solar power per unit area";
  parameter Real eff_PV = 0.12*0.85*0.9
    "Nominal solar power conversion efficiency (this should consider converion efficiency, area covered, AC/DC losses)";
  parameter Modelica.Units.SI.Area A_PV=PSun/eff_PV/W_m2_nominal
    "Nominal area of a P installation";

  AC.ThreePhasesBalanced.Sources.Grid gri(
    f=f,
    V=V_nominal,
    phiSou=0) "Grid model that provides power to the system"
  AC.ThreePhasesBalanced.Loads.Inductive loa1(mode=Buildings.Electrical.Types.Load.VariableZ_y_input,
    V_nominal=V_nominal,
    linearized=false,
    use_pf_in=false,
    pf=0.8,
    P_nominal=-PLoa_nominal) "Electrical load"
  AC.ThreePhasesBalanced.Loads.Inductive loa2(mode=Buildings.Electrical.Types.Load.VariableZ_y_input,
    V_nominal=V_nominal,
    linearized=false,
    use_pf_in=false,
    pf=0.9,
    P_nominal=-PLoa_nominal) "Electrical load"
  AC.ThreePhasesBalanced.Loads.Inductive loa3(mode=Buildings.Electrical.Types.Load.VariableZ_y_input,
    V_nominal=V_nominal,
    linearized=false,
    use_pf_in=false,
    pf=0.8,
    P_nominal=-PLoa_nominal) "Electrical load"
  AC.ThreePhasesBalanced.Loads.Inductive loa4(mode=Buildings.Electrical.Types.Load.VariableZ_y_input,
    V_nominal=V_nominal,
    linearized=false,
    use_pf_in=false,
    pf=0.88,
    P_nominal=-PLoa_nominal) "Electrical load"
  AC.ThreePhasesBalanced.Lines.Line line1(mode=Buildings.Electrical.Types.CableMode.automatic,
      l=1500,
    V_nominal=V_nominal,
    P_nominal=7*(PLoa_nominal + PSun) + PWin) "Electrical line"
  AC.ThreePhasesBalanced.Lines.Line line2(mode=Buildings.Electrical.Types.CableMode.automatic,
      l=300,
    V_nominal=V_nominal,
    P_nominal=3*(PLoa_nominal + PSun)) "Electrical line"
  AC.ThreePhasesBalanced.Lines.Line line3(mode=Buildings.Electrical.Types.CableMode.automatic,
      l=300,
    V_nominal=V_nominal,
    P_nominal=2*(PLoa_nominal + PSun)) "Electrical line"
  AC.ThreePhasesBalanced.Lines.Line line4(mode=Buildings.Electrical.Types.CableMode.automatic,
      l=300,
    V_nominal=V_nominal,
    P_nominal=(PLoa_nominal + PSun)) "Electrical line"
  AC.ThreePhasesBalanced.Lines.Line line5(mode=Buildings.Electrical.Types.CableMode.automatic,
      l=300,
    V_nominal=V_nominal,
    P_nominal=3*(PLoa_nominal + PSun) + PWin) "Electrical line"
  AC.ThreePhasesBalanced.Loads.Inductive loa5(mode=Buildings.Electrical.Types.Load.VariableZ_y_input,
    V_nominal=V_nominal,
    linearized=false,
    use_pf_in=false,
    pf=0.95,
    P_nominal=-PLoa_nominal) "Electrical load"
  AC.ThreePhasesBalanced.Loads.Inductive loa6(mode=Buildings.Electrical.Types.Load.VariableZ_y_input,
    V_nominal=V_nominal,
    linearized=false,
    use_pf_in=false,
    pf=0.8,
    P_nominal=-PLoa_nominal) "Electrical load"
  AC.ThreePhasesBalanced.Loads.Inductive loa7(mode=Buildings.Electrical.Types.Load.VariableZ_y_input,
    V_nominal=V_nominal,
    linearized=false,
    use_pf_in=false,
    pf=0.75,
    P_nominal=-PLoa_nominal) "Electrical load"
  AC.ThreePhasesBalanced.Lines.Line line6(mode=Buildings.Electrical.Types.CableMode.automatic,
      l=300,
    V_nominal=V_nominal,
    P_nominal=2*(PLoa_nominal + PSun) + PWin) "Electrical line"
  AC.ThreePhasesBalanced.Lines.Line line7(mode=Buildings.Electrical.Types.CableMode.automatic,
      l=300,
    V_nominal=V_nominal,
    P_nominal=(PLoa_nominal + PSun) + PWin) "Electrical line"
  AC.ThreePhasesBalanced.Sources.PVSimpleOriented pv1(
    eta_DCAC=0.89,
    A=A_PV,
    fAct=0.9,
    eta=0.12,
    linearized=false,
    V_nominal=V_nominal,
    pf=0.85,
    azi=Buildings.Types.Azimuth.S,
    til=0.5235987755983) "PV"
  AC.ThreePhasesBalanced.Sources.PVSimpleOriented pv2(
    eta_DCAC=0.89,
    A=A_PV,
    fAct=0.9,
    eta=0.12,
    linearized=false,
    V_nominal=V_nominal,
    pf=0.8,
    azi=Buildings.Types.Azimuth.E,
    til=0.5235987755983) "PV"
  AC.ThreePhasesBalanced.Sources.PVSimpleOriented pv3(
    eta_DCAC=0.89,
    A=A_PV,
    fAct=0.9,
    eta=0.12,
    linearized=false,
    V_nominal=V_nominal,
    pf=0.8,
    azi=Buildings.Types.Azimuth.W,
    til=0.34906585039887) "PV"
  AC.ThreePhasesBalanced.Sources.PVSimpleOriented pv4(
    eta_DCAC=0.89,
    A=A_PV,
    fAct=0.9,
    eta=0.12,
    linearized=false,
    V_nominal=V_nominal,
    pf=0.9,
    azi=Buildings.Types.Azimuth.S,
    til=0.5235987755983) "PV"
  AC.ThreePhasesBalanced.Sources.PVSimpleOriented pv5(
    eta_DCAC=0.89,
    A=A_PV,
    fAct=0.9,
    eta=0.12,
    linearized=false,
    V_nominal=V_nominal,
    pf=0.95,
    azi=Buildings.Types.Azimuth.W,
    til=0.61086523819802) "PV"
  AC.ThreePhasesBalanced.Sources.PVSimpleOriented pv6(
    eta_DCAC=0.89,
    A=A_PV,
    fAct=0.9,
    eta=0.12,
    linearized=false,
    V_nominal=V_nominal,
    pf=0.9,
    azi=Buildings.Types.Azimuth.E,
    til=0.43633231299858) "PV"
  AC.ThreePhasesBalanced.Sources.PVSimpleOriented pv7(
    eta_DCAC=0.89,
    A=A_PV,
    fAct=0.9,
    eta=0.12,
    linearized=false,
    V_nominal=V_nominal,
    pf=0.97,
    azi=Buildings.Types.Azimuth.S,
    til=0.5235987755983) "PV"
  AC.ThreePhasesBalanced.Sources.WindTurbine winTur(
    V_nominal=V_nominal,
    h=15,
    hRef=10,
    pf=0.94,
    eta_DCAC=0.92,
    nWin=0.4,
    tableOnFile=false,
    scale=PWin) "Wind turbine model"
  AC.ThreePhasesBalanced.Lines.Line line8(mode=Buildings.Electrical.Types.CableMode.automatic,
      l=300,
    V_nominal=V_nominal,
    P_nominal=PWin)
  BoundaryConditions.WeatherData.ReaderTMY3 weaDat(
      computeWetBulbTemperature=false,
      filNam=Modelica.Utilities.Files.loadResource("modelica://Buildings/Resources/weatherdata/USA_CA_San.Francisco.Intl.AP.724940_TMY3.mos"))
    "Weather data model"
  BoundaryConditions.WeatherData.Bus weaBus "Weather data bus"
  Modelica.Blocks.Sources.CombiTimeTable pow1(extrapolation=
        Modelica.Blocks.Types.Extrapolation.Periodic, table=[0,0.1; 2,0.1;
        4,0.1; 6,0.1; 8,0.8; 10,0.7; 12,0.3; 14,0.3;
        16,0.3; 18,0.8; 20,0.7; 22,0.3; 24,0.1],
    timeScale=3600)
    "Power consumption profile for load 1"
  Modelica.Blocks.Sources.CombiTimeTable pow2(extrapolation=
        Modelica.Blocks.Types.Extrapolation.Periodic, table=[0,0.1; 2,0.1;
        4,0.1; 6,0.1; 8,0.8; 10,0.7; 12,0.3; 14,0.3;
        16,0.3; 18,0.8; 20,0.7; 22,0.3; 24,0.1],
    timeScale=3600)
    "Power consumption profile for load 2"
  Modelica.Blocks.Sources.CombiTimeTable pow3(extrapolation=
        Modelica.Blocks.Types.Extrapolation.Periodic, table=[0,0.1; 2,0.1;
        4,0.1; 6,0.1; 8,0.8; 10,0.7; 12,0.3; 14,0.3;
        16,0.3; 18,0.8; 20,0.7; 22,0.3; 24,0.1],
    timeScale=3600)
    "Power consumption profile for load 3"
  Modelica.Blocks.Sources.CombiTimeTable pow4(extrapolation=
        Modelica.Blocks.Types.Extrapolation.Periodic, table=[0,0.2; 2,0.2;
        4,0.2; 6,0.2; 8,0.8; 10,0.8; 12,0.6; 14,0.3;
        16,0.3; 18,0.8; 20,0.8; 22,0.3; 24,0.3],
    timeScale=3600)
    "Power consumption profile for load 4"
  Modelica.Blocks.Sources.CombiTimeTable pow5(extrapolation=
        Modelica.Blocks.Types.Extrapolation.Periodic, table=[0,0.1; 2,0.1;
        4,0.1; 6,0.1; 8,0.5; 10,0.2; 12,0.3; 14,0.3;
        16,0.3; 18,0.9; 20,0.4; 22,0.2; 24,0.1],
    timeScale=3600)
    "Power consumption profile for load 5"
  Modelica.Blocks.Sources.CombiTimeTable pow6(extrapolation=
        Modelica.Blocks.Types.Extrapolation.Periodic, table=[0,0.1; 2,0.1;
        4,0.1; 6,0.1; 8,0.8; 10,0.7; 12,0.3; 14,0.3;
        16,0.3; 18,0.8; 20,0.7; 22,0.3; 24,0.1],
    timeScale=3600)
    "Power consumption profile for load 6"
  Modelica.Blocks.Sources.CombiTimeTable pow7(extrapolation=
        Modelica.Blocks.Types.Extrapolation.Periodic, table=[0,0.2; 2,0.1;
        4,0.1; 6,0.1; 8,0.4; 10,0.7; 12,0.3; 14,0.3;
        16,0.3; 18,0.2; 20,0.7; 22,0.3; 24,0.2],
    timeScale=3600)
    "Power consumption profile for load 7"
  AC.ThreePhasesBalanced.Sensors.Probe sen1(V_nominal=V_nominal, perUnit=true)
    "Voltage probe"
  AC.ThreePhasesBalanced.Sensors.Probe sen2(V_nominal=V_nominal, perUnit=true)
    "Voltage probe"
  AC.ThreePhasesBalanced.Sensors.Probe sen3(V_nominal=V_nominal, perUnit=true)
    "Voltage probe"
  Modelica.Blocks.Continuous.Integrator EWin
    "Energy produced by the wind turbine"
  Modelica.Blocks.Math.MultiSum PSol(nu=7, y(unit="W"))
    "Total produced solar power"
  Modelica.Blocks.Continuous.Integrator ESol
    "Energy produced by the solar panels"
  Modelica.Blocks.Sources.RealExpression PLoa(
    y=PLoa_nominal*(pow1.y[1] + pow2.y[1] + pow3.y[1] + pow4.y[1] + pow5.y[1] + pow6.y[1] + pow7.y[1]))
    "Total power consumed by the loads"
  Modelica.Blocks.Continuous.Integrator ELoa(y_start=1E-10)
    "Energy consumed by the loads"
  Modelica.Blocks.Math.Add EPro "Total produced power by renewables"
  Modelica.Blocks.Math.Division ERat "Ratio of produced over consumed energy"
equation
  connect(gri.terminal, line1.terminal_n)
  connect(line1.terminal_p, loa1.terminal)
  connect(line1.terminal_p, line2.terminal_n)
  connect(line2.terminal_p, loa2.terminal)
  connect(line2.terminal_p, line3.terminal_n)
  connect(line3.terminal_p, loa3.terminal)
  connect(line3.terminal_p, line4.terminal_n)
  connect(line4.terminal_p, loa4.terminal)
  connect(line1.terminal_p, line5.terminal_n)
  connect(loa5.terminal, line5.terminal_p)
  connect(line5.terminal_p, line6.terminal_n)
  connect(line6.terminal_p, line7.terminal_n)
  connect(loa6.terminal, line6.terminal_p)
  connect(loa7.terminal, line7.terminal_p)
  connect(line1.terminal_p, pv1.terminal)
  connect(line2.terminal_p, pv2.terminal)
  connect(line3.terminal_p, pv3.terminal)
  connect(line4.terminal_p, pv4.terminal)
  connect(pv5.terminal, line5.terminal_p)
  connect(pv6.terminal, loa6.terminal)
  connect(pv7.terminal, loa7.terminal)
  connect(line7.terminal_p, line8.terminal_n)
  connect(line8.terminal_p, winTur.terminal)
  connect(weaDat.weaBus, pv1.weaBus)
  connect(weaDat.weaBus, pv2.weaBus)
  connect(weaDat.weaBus, pv3.weaBus)
  connect(weaDat.weaBus, pv4.weaBus)
  connect(weaDat.weaBus, weaBus)
  connect(weaBus.winSpe, winTur.vWin)
  connect(weaDat.weaBus, pv5.weaBus)
  connect(weaDat.weaBus, pv6.weaBus)
  connect(weaDat.weaBus, pv7.weaBus)
  connect(loa2.y, pow2.y[1])
  connect(pow1.y[1], loa1.y)
  connect(loa3.y, pow3.y[1])
  connect(loa4.y, pow4.y[1])
  connect(loa5.y, pow5.y[1])
  connect(loa6.y, pow6.y[1])
  connect(loa7.y, pow7.y[1])
  connect(sen1.term, line1.terminal_p)
  connect(sen2.term, line4.terminal_p)
  connect(sen3.term, line8.terminal_p)
  connect(EWin.u, winTur.P)
  connect(pv1.P, PSol.u[1])
  connect(PSol.u[2], pv2.P)
  connect(PSol.u[3], pv3.P)
  connect(PSol.u[4], pv4.P)
  connect(PSol.u[5], pv5.P)
  connect(PSol.y, ESol.u)
  connect(PSol.u[6], pv6.P)
  connect(PSol.u[7], pv7.P)
  connect(ELoa.u, PLoa.y)
  connect(EPro.u1, ESol.y)
  connect(EPro.u2, EWin.y)
  connect(ERat.u1, EPro.y)

  connect(ERat.u2, ELoa.y)
end RenewableSources;


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


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

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


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

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

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


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

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


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

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

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

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

  if not use_C_in then
    y_C_internal = 0;
  end if;

  if not use_L_in then
    y_L_internal = 0;
  end if;

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

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

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


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

Buildings.Electrical.Interfaces.Load.

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

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


Partial model for a generic load

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

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

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

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

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

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

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

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

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

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

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

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


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


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

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


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

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

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


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

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

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

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


Base model for a PV system

within Buildings.Electrical.Interfaces;
model PartialPV "Base model for a PV system"
  extends Buildings.Electrical.Interfaces.PartialPvBase;
  replaceable package PhaseSystem =
      Buildings.Electrical.PhaseSystems.PartialPhaseSystem constrainedby
    Buildings.Electrical.PhaseSystems.PartialPhaseSystem "Phase system"
  parameter Modelica.Units.SI.Voltage V_nominal(min=0, start=110)
    "Nominal voltage (V_nominal >= 0)"
  Modelica.Blocks.Interfaces.RealInput G(unit="W/m2")
    "Total solar irradiation per unit area"

  replaceable Buildings.Electrical.Interfaces.Terminal terminal(
    redeclare final package PhaseSystem = PhaseSystem) "Generalized terminal"

protected
  Modelica.Blocks.Sources.RealExpression solarPower(y=A*fAct*eta*G)
    "Solar energy converted at the PV panel"

equation
  assert(solarPower.y>=0, "Solar power must be positive");
end PartialPV;


Partial model with basic parameters for PVs. This model contains the minimum set of parameters necessary to describe
a PV system.
The model defines also an output P for the power generated by the PVs.

within Buildings.Electrical.Interfaces;
partial model PartialPvBase "Partial model with basic parameters for PVs"
  parameter Modelica.Units.SI.Area A "Gross surface area (including area not covered with active PV cells)";
  parameter Real fAct(min=0, max=1, unit="1") = 0.9
    "Fraction of surface area with active solar cells";
  parameter Real eta(min=0, max=1, unit="1") = 0.12
    "Module conversion efficiency";
  Modelica.Blocks.Interfaces.RealOutput P(unit="W") "Generated power"
end PartialPvBase;


within Buildings.Electrical.Interfaces;
model PartialPVOriented "Base model of a PV system with orientation"
  extends Buildings.Electrical.Interfaces.PartialPvBase;

  replaceable package PhaseSystem =
      Buildings.Electrical.PhaseSystems.PartialPhaseSystem constrainedby
    Buildings.Electrical.PhaseSystems.PartialPhaseSystem "Phase system"

  parameter Modelica.Units.SI.Angle til "Surface tilt"
  parameter Modelica.Units.SI.Angle azi "Surface azimuth"
  parameter Modelica.Units.SI.Voltage V_nominal(min=0, start=110)
    "Nominal voltage (V_nominal >= 0)"

  replaceable Buildings.Electrical.Interfaces.Terminal terminal(
    redeclare final package PhaseSystem = PhaseSystem) "Generalized terminal"

  replaceable PartialPV panel constrainedby PartialPV(
    redeclare final package PhaseSystem = PhaseSystem,
    final A=A,
    final fAct=fAct,
    final eta=eta) "PV panel"

  BoundaryConditions.WeatherData.Bus weaBus "Weather data"
protected
  BoundaryConditions.SolarIrradiation.DiffusePerez HDifTil(
    final til=til,
    final azi=azi) "Diffuse irradiation on tilted surface"
  BoundaryConditions.SolarIrradiation.DirectTiltedSurface HDirTil(
    final til=til,
    final azi=azi) "Direct irradiation on tilted surface"

  Modelica.Blocks.Math.Add G "Total irradiation on tilted surface"

equation
  connect(panel.P, P)
  connect(terminal, panel.terminal)
  connect(HDifTil.H,G. u1)
  connect(HDirTil.H,G. u2)
  connect(G.y, panel.G)
  connect(HDifTil.weaBus, weaBus)
  connect(weaBus, HDirTil.weaBus)
end PartialPVOriented;


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

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

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

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


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

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


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

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

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

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


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

Buildings.Electrical.Interfaces.Load.

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


Partial model of a generic source.

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


Generalized electric terminal

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


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

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

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

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


DC system

within Buildings.Electrical.PhaseSystems;
package DirectCurrent "DC system"
  extends PartialPhaseSystem(phaseSystemName="DirectCurrent", n=1, m=0);

  redeclare function extends j "Direct current has no complex component"
  algorithm
    y := zeros(n);
  end j;

 redeclare function extends rotate
  "Rotate a vector of an angle theta (anti-counterclock)"
 algorithm
    y[n] := x[n];
 end rotate;

 redeclare function extends thetaRel
  "Return absolute angle of rotating system as offset to thetaRef"
 algorithm
    thetaRel := 0;
 end thetaRel;

 redeclare function extends thetaRef
  "Return absolute angle of rotating reference system"
 algorithm
    thetaRef := 0;
 end thetaRef;

 redeclare function extends phase "Return phase"
 algorithm
    phase := 0;
 end phase;

  redeclare replaceable function extends phaseVoltages
  "Return phase to neutral voltages"
  algorithm
    v := {V};
  end phaseVoltages;

  redeclare function extends phaseCurrents "Return phase currents"
  algorithm
    i := {I};
  end phaseCurrents;

  redeclare function extends phasePowers "Return phase powers"
  algorithm
    p := {P};
  end phasePowers;

  redeclare function extends phasePowers_vi "Return phase powers"
  algorithm
    p := {v*i};
  end phasePowers_vi;

  redeclare replaceable function extends systemVoltage
  "Return system voltage as function of phase voltages"
  algorithm
    V := v[1];
  end systemVoltage;

  redeclare function extends systemCurrent
  "Return system current as function of phase currents"
  algorithm
    I := i[1];
  end systemCurrent;

  redeclare function extends activePower
  "Return total power as function of phase powers"
  algorithm
    P := v*i;
  end activePower;
end DirectCurrent;


Single phase two connectors AC system

within Buildings.Electrical.PhaseSystems;
package OnePhase "Single phase two connectors AC system"
  extends PartialPhaseSystem(phaseSystemName="OnePhase", n=2, m=1);

  redeclare function extends j "Return vector rotated by 90 degrees"
  algorithm
    y := {-x[2], x[1]};
  end j;

  redeclare function extends rotate
  "Rotate a vector of an angle theta (anti-counterclock)"
  algorithm
    y[1] := cos(theta)*x[1] - sin(theta)*x[2];
    y[2] := sin(theta)*x[1] + cos(theta)*x[2];
  end rotate;

  redeclare function extends product
  "Multiply two complex numbers represented by vectors x[2] and y[2]"
  algorithm
    z := {x[1]*y[1] - x[2]*y[2], x[1]*y[2] + x[2]*y[1]};
  end product;

  redeclare function extends divide
  "Divide two complex numbers represented by vectors x[2] and y[2]"
  algorithm
    z := {x[1]*y[1] + x[2]*y[2], x[2]*y[1] - x[1]*y[2]}/(y[1]^2 + y[2]^2);
  end divide;

  redeclare function extends thetaRel
  "Return absolute angle of rotating system as offset to thetaRef"
  algorithm
    thetaRel := 0;
  end thetaRel;

  redeclare function extends thetaRef
  "Return absolute angle of rotating reference system"
  algorithm
    thetaRef := theta[1];
  end thetaRef;

  redeclare function extends phase "Return phase"
  algorithm
    phase := atan2(x[2], x[1]);
  end phase;

  redeclare function extends phaseVoltages "Return phase to neutral voltages"
  algorithm
    v := {V*cos(phi), V*sin(phi)};
  end phaseVoltages;

  redeclare function extends phaseCurrents "Return phase currents"
  algorithm
    i := {I*cos(phi), I*sin(phi)};
  end phaseCurrents;

  redeclare function extends phasePowers "Return phase powers"
  algorithm
    p := {P, P*tan(phi)};
  end phasePowers;

  redeclare function extends phasePowers_vi "Return phase powers"
  algorithm
    p := {v[1]*i[1] + v[2]*i[2], v[2]*i[1] - v[1]*i[2]};
  end phasePowers_vi;

  redeclare function extends systemVoltage
  "Return system voltage as function of phase voltages"
  algorithm
    V := Modelica.Fluid.Utilities.regRoot(v*v, delta = 1e-5);
  end systemVoltage;

  redeclare function extends systemCurrent
  "Return system current as function of phase currents"
  algorithm
    I := Modelica.Fluid.Utilities.regRoot(i*i, delta = 1e-5);
  end systemCurrent;

  redeclare function extends activePower
  "Return total power as function of phase powers"
  algorithm
    // P = v[1]*i[1] + v[2]*i[2]
    P := v*i;
  end activePower;
end OnePhase;


Phase systems used in power connectors

within Buildings.Electrical;
package PhaseSystems "Phase systems used in power connectors"
  extends Modelica.Icons.Package;
  import      Modelica.Units.SI;
  import Modelica.Constants.pi;
end PhaseSystems;