Precalculation of machine matrices

within PowerSystems.Examples.AC3ph;
package Precalculation "Precalculation of machine matrices"
extends Modelica.Icons.ExamplesPackage;

model Z_matrixTrDat0 "z-matrix from transient data, n=1, 0"
  extends Modelica.Icons.Example;

  parameter SI.AngularFrequency omega_nom=2*pi*60 "nom ang frequency";
  parameter Boolean use_xtr=true "use t_closed and x_transient?"
  parameter SI.Time[:] tc_d={0.684058354104098}
      "time constant closed-loop d-axis {tc_d', tc_d'', ..}"
  parameter SI.Time[:] tc_q=fill(0,0)
      "time constant closed-loop q-axis {tc_q', tc_q'', ..}"
  parameter SI.Time[:] to_d={4.17178865983301}
      "time constant open-loop d-axis {to_d', to_d'', ..}"
  parameter SI.Time[:] to_q=fill(0,0)
      "time constant open-loop q-axis {to_q', to_q'', ..}"
  parameter SIpu.Reactance[n_d] xtr_d={0.290231213872833}
      "trans reactance d-axis {xtr_d', xtr_d'', ..}"
  parameter SIpu.Reactance[n_q] xtr_q=fill(0,0)
      "trans reactance q-axis {xtr_q', xtr_q'', ..}"
  parameter SIpu.Reactance x_d=1.77 "syn reactance d-axis";
  parameter SIpu.Reactance x_q=1.77 "syn reactance q-axis";
  parameter SIpu.Reactance xsig_s=0.17 "leakage reactance stator";
  parameter SIpu.Resistance r_s=0.005 "resistance stator";
  parameter SIpu.Current if0=0 "induced field current at v_s=V_nom/0deg"
  parameter SI.Angle alpha_if0=0
      "angle(if0) at v_s=Vnom/0deg (sign: i_f behind v_s)"

  Real[n_d+1] zr_d;
  Real[n_d+1, n_d+1] zx_d;
  Real[n_q+1] zr_q;
  Real[n_q+1, n_q+1] zx_q;

  protected
  final parameter Boolean use_trans=true;
  final parameter Integer n_d=size(tc_d,1);
  final parameter Integer n_q=size(tc_q,1);
  function T_open = PowerSystems.Utilities.Precalculation.T_open;
  function z_matrix = PowerSystems.Utilities.Precalculation.z_fromTransDat;

algorithm
  (zr_d, zx_d) := z_matrix(n_d, omega_nom*tc_d, T_open(x_d, xtr_d, omega_nom*tc_d), x_d, xsig_s, r_s, if0, alpha_if0+pi, 0, true);
  (zr_q, zx_q) := z_matrix(n_q, omega_nom*tc_q, T_open(x_q, xtr_q, omega_nom*tc_q), x_q, xsig_s, r_s, 0, 0, 0, false);
end Z_matrixTrDat0;

model Z_matrixTrDat1 "z-matrix from transient data, n=1"
  extends Modelica.Icons.Example;

  parameter SI.AngularFrequency omega_nom=2*pi*60 "nom ang frequency";
  parameter Boolean use_xtr=true "use t_closed and x_transient?"
  parameter SI.Time[:] tc_d={0.684058354104098}
      "time constant closed-loop d-axis {tc_d', tc_d'', ..}"
  parameter SI.Time[:] tc_q={0.684058354104098}
      "time constant closed-loop q-axis {tc_q', tc_q'', ..}"
  parameter SI.Time[:] to_d={4.17178865983301}
      "time constant open-loop d-axis {to_d', to_d'', ..}"
  parameter SI.Time[:] to_q={4.17178865983301}
      "time constant open-loop q-axis {to_q', to_q'', ..}"
  parameter SIpu.Reactance[n_d] xtr_d={0.290231213872833}
      "trans reactance d-axis {xtr_d', xtr_d'', ..}"
  parameter SIpu.Reactance[n_q] xtr_q={0.290231213872833}
      "trans reactance q-axis {xtr_q', xtr_q'', ..}"
  parameter SIpu.Reactance x_d=1.77 "syn reactance d-axis";
  parameter SIpu.Reactance x_q=1.77 "syn reactance q-axis";
  parameter SIpu.Reactance xsig_s=0.17 "leakage reactance stator";
  parameter SIpu.Resistance r_s=0.005 "resistance stator";
  parameter SIpu.Current if0=0 "induced field current at v_s=V_nom/0deg"
  parameter SI.Angle alpha_if0=0
      "angle(if0) at v_s=Vnom/0deg (sign: i_f behind v_s)"

  Real[n_d+1] zr_d;
  Real[n_d+1, n_d+1] zx_d;
  Real[n_q+1] zr_q;
  Real[n_q+1, n_q+1] zx_q;

  protected
  final parameter Boolean use_trans=true;
  final parameter Integer n_d=size(tc_d,1);
  final parameter Integer n_q=size(tc_q,1);
  function T_open = PowerSystems.Utilities.Precalculation.T_open;
  function z_matrix = PowerSystems.Utilities.Precalculation.z_fromTransDat;

algorithm
  (zr_d, zx_d) := z_matrix(n_d, omega_nom*tc_d, T_open(x_d, xtr_d, omega_nom*tc_d), x_d, xsig_s, r_s, if0, alpha_if0+pi, 0, true);
  (zr_q, zx_q) := z_matrix(n_q, omega_nom*tc_q, T_open(x_q, xtr_q, omega_nom*tc_q), x_q, xsig_s, r_s, 0, 0, 0, false);
end Z_matrixTrDat1;

model Z_matrixTrDat2 "z-matrix from transient data, n=2"
  extends Modelica.Icons.Example;

  parameter SI.AngularFrequency omega_nom=2*pi*60 "nom ang frequency";
  parameter Boolean use_xtr=true "use t_closed and x_transient?"
  parameter SI.Time[:] tc_d={0.859012450972537, 0.024700865536969}
      "time constant closed-loop d-axis {tc_d', tc_d'', ..}"
  parameter SI.Time[:] tc_q={0.703091060761508, 0.0226730319219496}
      "time constant closed-loop q-axis {tc_q', tc_q'', ..}"
  parameter SI.Time[:] to_d={4.65929627144818, 0.03185596214872}
      "time constant open-loop d-axis {to_d', to_d'', ..}"
  parameter SI.Time[:] to_q={4.5014019794843, 0.0318010302865584}
      "time constant open-loop q-axis {to_q', to_q'', ..}"
  parameter SIpu.Reactance[n_d] xtr_d={0.32862524982383, 0.253031064823128}
      "trans reactance d-axis {xtr_d', xtr_d'', ..}"
  parameter SIpu.Reactance[n_q] xtr_q={0.27962838258275, 0.197108545894619}
      "trans reactance q-axis {xtr_q', xtr_q'', ..}"
  parameter SIpu.Reactance x_d=1.77 "syn reactance d-axis";
  parameter SIpu.Reactance x_q=1.77 "syn reactance q-axis";
  parameter SIpu.Reactance xsig_s=0.17 "leakage reactance stator";
  parameter SIpu.Resistance r_s=0.005 "resistance stator";
  parameter SIpu.Current if0=0.834830547142614
      "induced field current at v_s=V_nom/0deg"
  parameter SI.Angle alpha_if0=-1.7713318143478
      "angle(if0) at v_s=Vnom/0deg (sign: i_f behind v_s)"
  parameter Real tol=1e-6 "tolerance precalculation"

  Real[n_d+1] zr_d;
  Real[n_d+1, n_d+1] zx_d;
  Real[n_q+1] zr_q;
  Real[n_q+1, n_q+1] zx_q;

  protected
  final parameter Boolean use_trans=true;
  final parameter Integer n_d=size(tc_d,1);
  final parameter Integer n_q=size(tc_q,1);
  function T_open = PowerSystems.Utilities.Precalculation.T_open;
  function z_matrix = PowerSystems.Utilities.Precalculation.z_fromTransDat;

algorithm
  (zr_d, zx_d) := z_matrix(n_d, omega_nom*tc_d, T_open(x_d, xtr_d, omega_nom*tc_d), x_d, xsig_s, r_s, if0, alpha_if0+pi, tol, true);
  (zr_q, zx_q) := z_matrix(n_q, omega_nom*tc_q, T_open(x_q, xtr_q, omega_nom*tc_q), x_q, xsig_s, r_s, 0, 0, 0, false);
end Z_matrixTrDat2;

model Z_matrixTrDat3 "z-matrix from transient data, n=3"
  extends Modelica.Icons.Example;

  parameter SI.AngularFrequency omega_nom=2*pi*60 "nom ang frequency";
  parameter Boolean use_xtr=true "use t_closed and x_transient?"
  parameter SI.Time[:] tc_d={0.869154751730285, 0.028837675377106, 0.00293370131083294}
      "time constant closed-loop d-axis {tc_d', tc_d'', ..}"
  parameter SI.Time[:] tc_q={0.710852334653427, 0.0267828156988275, 0.00180497772676905}
      "time constant closed-loop q-axis {tc_q', tc_q'', ..}"
  parameter SI.Time[:] to_d={4.79391852025869, 0.0390156982688014, 0.00331427152151774}
      "time constant open-loop d-axis {to_d', to_d'', ..}"
  parameter SI.Time[:] to_q={4.63399482930977, 0.0387883296526998, 0.00197933074735668}
      "time constant open-loop q-axis {to_q', to_q'', ..}"
  parameter SIpu.Reactance[n_d] xtr_d={0.324237799805601, 0.238193407852477, 0.209956053815068}
      "trans reactance d-axis {xtr_d', xtr_d'', ..}"
  parameter SIpu.Reactance[n_q] xtr_q={0.275669319075036, 0.187924853846143, 0.170964423631031}
      "trans reactance q-axis {xtr_q', xtr_q'', ..}"
  parameter SIpu.Reactance x_d=1.77 "syn reactance d-axis";
  parameter SIpu.Reactance x_q=1.77 "syn reactance q-axis";
  parameter SIpu.Reactance xsig_s=0.17 "leakage reactance stator";
  parameter SIpu.Resistance r_s=0.005 "resistance stator";
  parameter SIpu.Current if0=0.47426409312074
      "induced field current at v_s=V_nom/0deg"
  parameter SI.Angle alpha_if0=-2.5862242826262
      "angle(if0) at v_s=Vnom/0deg (sign: i_f behind v_s)"
  parameter Real tol=1e-6 "tolerance precalculation"

  Real[n_d + 1] zr_d;
  Real[n_d + 1, n_d + 1] zx_d;
  Real[n_q + 1] zr_q;
  Real[n_q + 1, n_q + 1] zx_q;

  protected
  final parameter Boolean use_trans=true;
  final parameter Integer n_d=size(tc_d,1);
  final parameter Integer n_q=size(tc_q,1);
  function T_open = PowerSystems.Utilities.Precalculation.T_open;
  function z_matrix = PowerSystems.Utilities.Precalculation.z_fromTransDat;

algorithm
  (zr_d, zx_d) := z_matrix(n_d, omega_nom*tc_d, T_open(x_d, xtr_d, omega_nom*tc_d), x_d, xsig_s, r_s, if0, alpha_if0+pi, tol, true);
  (zr_q, zx_q) := z_matrix(n_q, omega_nom*tc_q, T_open(x_q, xtr_q, omega_nom*tc_q), x_q, xsig_s, r_s, 0, 0, 0, false);
end Z_matrixTrDat3;

model Z_matrixEqCirc0 "z-matrix from equivalent circuit, n=1, 0"
  extends Modelica.Icons.Example;

  parameter SI.AngularFrequency omega_nom=2*pi*60 "nom ang frequency";
  parameter SIpu.Reactance x_d=1.77 "syn reactance d-axis";
  parameter SIpu.Reactance x_q=1.77 "syn reactance q-axis";
  parameter SIpu.Reactance xsig_s=0.17 "leakage reactance stator";
  parameter SIpu.Resistance r_s=0.005 "resistance stator";

  parameter SIpu.Reactance[:] xsig_rd={0.13}
      "leakage reactance rotor d-axis {f}"
  parameter SIpu.Reactance[:] xsig_rq=fill(0,0)
      "leakage reactance rotor q-axis{Q}"
  parameter SIpu.Reactance[size(xsig_rd,1)-1] xm_d=fill(0,0)
      "coupling-reactance d-axis"
  parameter SIpu.Resistance[size(xsig_rd,1)] r_rd={0.0011}
      "resistance rotor d-axis{f}"
  parameter SIpu.Resistance[size(xsig_rq,1)] r_rq=fill(0,0)
      "resistance rotor q-axis{Q}"

  Real[n_d+1] zr_d;
  Real[n_d+1, n_d+1] zx_d;
  Real[n_q+1] zr_q;
  Real[n_q+1, n_q+1] zx_q;

  protected
  final parameter Boolean use_trans=false;
  final parameter Integer n_d=size(xsig_rd,1);
  final parameter Integer n_q=size(xsig_rq,1);
  function z_matrix = PowerSystems.Utilities.Precalculation.z_fromEqCirc;

algorithm
  (zr_d, zx_d) := z_matrix(n_d, x_d, xsig_s, r_s, xm_d, xsig_rd, r_rd);
  (zr_q, zx_q) := z_matrix(n_q, x_q, xsig_s, r_s, zeros(n_q-1), xsig_rq, r_rq);
end Z_matrixEqCirc0;

model Z_matrixEqCirc1 "z-matrix from equivalent circuit, n=1"
  extends Modelica.Icons.Example;

  parameter SI.AngularFrequency omega_nom=2*pi*60 "nom ang frequency";
  parameter SIpu.Reactance x_d=1.77 "syn reactance d-axis";
  parameter SIpu.Reactance x_q=1.77 "syn reactance q-axis";
  parameter SIpu.Reactance xsig_s=0.17 "leakage reactance stator";
  parameter SIpu.Resistance r_s=0.005 "resistance stator";

  parameter SIpu.Reactance[:] xsig_rd={0.13}
      "leakage reactance rotor d-axis {f}"
  parameter SIpu.Reactance[:] xsig_rq={0.13}
      "leakage reactance rotor q-axis{Q}"
  parameter SIpu.Reactance[size(xsig_rd,1)-1] xm_d=fill(0,0)
      "coupling-reactance d-axis"
  parameter SIpu.Resistance[size(xsig_rd,1)] r_rd={0.0011}
      "resistance rotor d-axis{f}"
  parameter SIpu.Resistance[size(xsig_rq,1)] r_rq={0.0011}
      "resistance rotor q-axis{Q}"

  Real[n_d+1] zr_d;
  Real[n_d+1, n_d+1] zx_d;
  Real[n_q+1] zr_q;
  Real[n_q+1, n_q+1] zx_q;

  protected
  final parameter Boolean use_trans=false;
  final parameter Integer n_d=size(xsig_rd,1);
  final parameter Integer n_q=size(xsig_rq,1);
  function z_matrix = PowerSystems.Utilities.Precalculation.z_fromEqCirc;

algorithm
  (zr_d, zx_d) := z_matrix(n_d, x_d, xsig_s, r_s, xm_d, xsig_rd, r_rd);
  (zr_q, zx_q) := z_matrix(n_q, x_q, xsig_s, r_s, zeros(n_q-1), xsig_rq, r_rq);
end Z_matrixEqCirc1;

model Z_matrixEqCirc2 "z-matrix from equivalent circuit, n=2"
  extends Modelica.Icons.Example;

  parameter SI.AngularFrequency omega_nom=2*pi*60 "nom ang frequency";
  parameter SIpu.Reactance x_d=1.77 "syn reactance d-axis";
  parameter SIpu.Reactance x_q=1.77 "syn reactance q-axis";
  parameter SIpu.Reactance xsig_s=0.17 "leakage reactance stator";
  parameter SIpu.Resistance r_s=0.005 "resistance stator";

  parameter SIpu.Reactance[:] xsig_rd={0.13, 0.035}
      "leakage reactance rotor d-axis {f, D}"
  parameter SIpu.Reactance[:] xsig_rq={0.13, 0.035}
      "leakage reactance rotor q-axis{Q1, Q2}"
  parameter SIpu.Reactance[size(xsig_rd,1)-1] xm_d={0.06}
      "coupling-reactance d-axis"
  parameter SIpu.Resistance[size(xsig_rd,1)] r_rd={0.0011, 0.012}
      "resistance rotor d-axis{f, D}"
  parameter SIpu.Resistance[size(xsig_rq,1)] r_rq={0.0011, 0.012}
      "resistance rotor q-axis{Q1, Q2}"

  Real[n_d+1] zr_d;
  Real[n_d+1, n_d+1] zx_d;
  Real[n_q+1] zr_q;
  Real[n_q+1, n_q+1] zx_q;

  protected
  final parameter Boolean use_trans=false;
  final parameter Integer n_d=size(xsig_rd,1);
  final parameter Integer n_q=size(xsig_rq,1);
  function z_matrix = PowerSystems.Utilities.Precalculation.z_fromEqCirc;

algorithm
  (zr_d, zx_d) := z_matrix(n_d, x_d, xsig_s, r_s, xm_d, xsig_rd, r_rd);
  (zr_q, zx_q) := z_matrix(n_q, x_q, xsig_s, r_s, zeros(n_q-1), xsig_rq, r_rq);
end Z_matrixEqCirc2;

model Z_matrixEqCirc3 "z-matrix from equivalent circuit, n=3"
  extends Modelica.Icons.Example;

  parameter SI.AngularFrequency omega_nom=2*pi*60 "nom ang frequency";
  parameter SIpu.Reactance x_d=1.77 "syn reactance d-axis";
  parameter SIpu.Reactance x_q=1.77 "syn reactance q-axis";
  parameter SIpu.Reactance xsig_s=0.17 "leakage reactance stator";
  parameter SIpu.Resistance r_s=0.005 "resistance stator";

  parameter SIpu.Reactance[:] xsig_rd={0.13, 0.035, 0.001}
      "leakage reactance rotor d-axis {f, D1, D2}"
  parameter SIpu.Reactance[:] xsig_rq={0.13, 0.035, 0.001}
      "leakage reactance rotor q-axis{Q1, Q2, Q3}"
  parameter SIpu.Reactance[size(xsig_rd,1)-1] xm_d={0.02, 0.04}
      "coupling-reactance d-axis"
  parameter SIpu.Resistance[size(xsig_rd,1)] r_rd={0.0011, 0.012, 0.03}
      "resistance rotor d-axis{f, D1, D2}"
  parameter SIpu.Resistance[size(xsig_rq,1)] r_rq={0.0011, 0.012, 0.03}
      "resistance rotor q-axis{Q1, Q2, Q3}"

  Real[n_d+1] zr_d;
  Real[n_d+1, n_d+1] zx_d;
  Real[n_q+1] zr_q;
  Real[n_q+1, n_q+1] zx_q;

  protected
  final parameter Boolean use_trans=false;
  final parameter Integer n_d=size(xsig_rd,1);
  final parameter Integer n_q=size(xsig_rq,1);
  function z_matrix = PowerSystems.Utilities.Precalculation.z_fromEqCirc;

algorithm
  (zr_d, zx_d) := z_matrix(n_d, x_d, xsig_s, r_s, xm_d, xsig_rd, r_rd);
  (zr_q, zx_q) := z_matrix(n_q, x_q, xsig_s, r_s, zeros(n_q-1), xsig_rq, r_rq);
end Z_matrixEqCirc3;

model TransDatFromEqCirc "Calculates transient data from equivalent circuit"
  extends Modelica.Icons.Example;

  parameter SI.AngularFrequency omega_nom=2*pi*60 "nom ang frequency";
  parameter SIpu.Reactance x_d=1.77 "syn reactance d-axis";
  parameter SIpu.Reactance x_q=1.77 "syn reactance q-axis";
  parameter SIpu.Reactance xsig_s=0.17 "leakage reactance stator";
  parameter SIpu.Resistance r_s=0.005 "resistance stator";

  parameter SIpu.Reactance[:] xsig_rd={0.13, 0.035}
      "leakage reactance rotor d-axis {f, D}"
  parameter SIpu.Reactance[:] xsig_rq={0.13, 0.035}
      "leakage reactance rotor q-axis{Q1, Q2}"
  parameter SIpu.Reactance[size(xsig_rd,1)-1] xm_d={0.06}
      "coupling-reactance d-axis"
  parameter SIpu.Resistance[size(xsig_rd,1)] r_rd={0.0011, 0.012}
      "resistance rotor d-axis{f, D}"
  parameter SIpu.Resistance[size(xsig_rq,1)] r_rq={0.0011, 0.012}
      "resistance rotor q-axis{Q1, Q2}"

  output SI.Time[n_d] tc_d "time constant closed-loop d-axis";
  output SI.Time[n_d] to_d "time constant open-loop d-axis";
  output SIpu.Reactance[n_d] xtr_d(each unit="1") "transient reactance d-axis";
  output SI.Time[n_q] tc_q "time constant closed-loop q-axis";
  output SI.Time[n_q] to_q "time constant open-loop q-axis";
  output SIpu.Reactance[n_q] xtr_q(each unit="1") "transient reactance q-axis";
  protected
  final parameter Boolean use_trans=false;
  final parameter Integer n_d=size(xsig_rd,1);
  final parameter Integer n_q=size(xsig_rq,1);
  function transientData=PowerSystems.Utilities.Precalculation.transientData;
  SI.Angle[n_d] Tc_d "Time constant closed-loop d-axis";
  SI.Angle[n_d] To_d "Time constant open-loop d-axis";
  SI.Angle[n_q] Tc_q "Time constant closed-loop q-axis";
  SI.Angle[n_q] To_q "Time constant open-loop q-axis";

algorithm
  (Tc_d, To_d, xtr_d) := transientData(n_d, x_d, xsig_s, r_s, xm_d, xsig_rd, r_rd);
  tc_d := Tc_d/omega_nom;
  to_d := To_d/omega_nom;
  (Tc_q, To_q, xtr_q) := transientData(n_q, x_q, xsig_s, r_s, zeros(n_q-1), xsig_rq, r_rq);
  tc_q := Tc_q/omega_nom;
  to_q := To_q/omega_nom;
end TransDatFromEqCirc;

model EqCircFromTransDat "Calculates equivalent circuit from transient data"
  extends Modelica.Icons.Example;

  parameter SI.AngularFrequency omega_nom=2*pi*60 "nom ang frequency";
  parameter Boolean use_xtr=true "use t_closed and x_transient?"
  parameter SI.Time[:] tc_d={0.859012450972537, 0.024700865536969}
      "time constant closed-loop d-axis {tc_d', tc_d'', ..}"
  parameter SI.Time[:] tc_q={0.703091060761508, 0.0226730319219496}
      "time constant closed-loop q-axis {tc_q', tc_q'', ..}"
  parameter SI.Time[:] to_d={4.65929627144818, 0.03185596214872}
      "time constant open-loop d-axis {to_d', to_d'', ..}"
  parameter SI.Time[:] to_q={4.5014019794843, 0.0318010302865584}
      "time constant open-loop q-axis {to_q', to_q'', ..}"
  parameter SIpu.Reactance[n_d] xtr_d={0.32862524982383, 0.253031064823128}
      "trans reactance d-axis {xtr_d', xtr_d'', ..}"
  parameter SIpu.Reactance[n_q] xtr_q={0.27962838258275, 0.197108545894619}
      "trans reactance q-axis {xtr_q', xtr_q'', ..}"
  parameter SIpu.Reactance x_d=1.77 "syn reactance d-axis";
  parameter SIpu.Reactance x_q=1.77 "syn reactance q-axis";
  parameter SIpu.Reactance xsig_s=0.17 "leakage reactance stator";
  parameter SIpu.Resistance r_s=0.005 "resistance stator";
  parameter SIpu.Current if0=0.834830547142614
      "induced field current at v_s=V_nom/0deg"
  parameter SI.Angle alpha_if0=-1.7713318143478
      "angle(if0) at v_s=Vnom/0deg (sign: i_f behind v_s)"
  parameter Real tol=1e-6 "tolerance precalculation"

  output SIpu.Resistance[n_d] r_rd(each unit="1");
  output SIpu.Reactance[n_d] xsig_rd(each unit="1");
  output SIpu.Reactance[n_d+1] xm_d(each unit="1");
  output SIpu.Resistance[n_q] r_rq(each unit="1");
  output SIpu.Reactance[n_q] xsig_rq(each unit="1");
  output SIpu.Reactance[n_q+1] xm_q(each unit="1");

  protected
  final parameter Boolean use_trans=true;
  final parameter Integer n_d=size(tc_d,1);
  final parameter Integer n_q=size(tc_q,1);
  function T_open = PowerSystems.Utilities.Precalculation.T_open;
  function equiCircuit = PowerSystems.Utilities.Precalculation.equiCircuit;

algorithm
  (r_rd, xsig_rd, xm_d) := equiCircuit(n_d, omega_nom*tc_d, T_open(x_d, xtr_d, omega_nom*tc_d), x_d, xsig_s, r_s, if0, alpha_if0+pi, tol, true);
  (r_rq, xsig_rq, xm_q) := equiCircuit(n_q, omega_nom*tc_q, T_open(x_q, xtr_q, omega_nom*tc_q), x_q, xsig_s, r_s, 0, 0, 0, false);
end EqCircFromTransDat;
end Precalculation;


Transformation dq0

within PowerSystems.Examples.AC3ph;
package Transformation "Transformation dq0"
  extends Modelica.Icons.ExamplesPackage;

  model PhaseShifts "Phase shift primary-secondary"
    extends Modelica.Icons.Example;

    inner PowerSystems.System system

    PowerSystems.AC3ph.Sources.Voltage voltage
    PowerSystems.AC3ph.Sensors.PVImeter meter1
    PowerSystems.AC3ph.Nodes.BusBar bus
    PowerSystems.AC3ph.Transformers.TrafoStray trafo1(
      redeclare record Data =
      PowerSystems.AC3ph.Transformers.Parameters.TrafoStray(V_nom={1,10}),
      redeclare model Topology_p = PowerSystems.AC3ph.Ports.Topology.Y,
      redeclare model Topology_n = PowerSystems.AC3ph.Ports.Topology.Y)
    PowerSystems.AC3ph.Sensors.PVImeter meter12(V_nom=10)
    PowerSystems.AC3ph.ImpedancesYD.Resistor res12(V_nom=10, r=1000)
    PowerSystems.AC3ph.Transformers.TrafoStray trafo2(
      redeclare model Topology_p = PowerSystems.AC3ph.Ports.Topology.Y,
      redeclare model Topology_n = PowerSystems.AC3ph.Ports.Topology.Delta,
      redeclare record Data =
        PowerSystems.AC3ph.Transformers.Parameters.TrafoStray(V_nom={1,10}))
    PowerSystems.AC3ph.Sensors.PVImeter meter22(V_nom=10)
    PowerSystems.AC3ph.ImpedancesYD.Resistor res22(V_nom=10, r=1000)
    PowerSystems.AC3ph.Nodes.GroundOne grd

    PowerSystems.AC3ph.Transformers.TrafoStray trafo3(
        redeclare model Topology_p = PowerSystems.AC3ph.Ports.Topology.PAR,
      redeclare record Data =
          PowerSystems.AC3ph.Transformers.Parameters.TrafoStray (V_nom={1,10},
            dv_tap={0.1,0}),
      tap_1=-2)
    PowerSystems.AC3ph.Sensors.PVImeter meter32(V_nom=10)
    PowerSystems.AC3ph.ImpedancesYD.Resistor res32(V_nom=10, r=1000)
  equation
    connect(voltage.term,meter1. term_p)
    connect(meter1.term_n,bus. term)
    connect(bus.term, trafo1.term_p)
    connect(trafo1.term_n, meter12.term_p)
    connect(meter12.term_n, res12.term)
    connect(bus.term, trafo2.term_p)
    connect(trafo2.term_n, meter22.term_p)
    connect(meter22.term_n, res22.term)
    connect(grd.term, voltage.neutral)
    connect(trafo3.term_n, meter32.term_p)
    connect(meter32.term_n, res32.term)
    connect(trafo3.term_p, bus.term)
  end PhaseShifts;

  model TapChanger "Tap changing primary and secondary"
    extends Modelica.Icons.Example;

    inner PowerSystems.System system
    PowerSystems.AC3ph.Sources.Voltage voltage
    PowerSystems.AC3ph.Sensors.PVImeter meter1
    PowerSystems.AC3ph.Nodes.BusBar bus
    PowerSystems.AC3ph.Transformers.TrafoStray trafo1(
      redeclare record Data =
      PowerSystems.AC3ph.Transformers.Parameters.TrafoStray (
      V_nom={1,10},
      dv_tap={0.1,0.1}),
      use_tap_1_in=true,
      use_tap_2_in=true)
    PowerSystems.AC3ph.Sensors.PVImeter meter12(V_nom=10)
    PowerSystems.AC3ph.ImpedancesYD.Resistor res12(V_nom=10, r=1000)
    PowerSystems.AC3ph.Transformers.TrafoStray trafo2(
      redeclare record Data =
      PowerSystems.AC3ph.Transformers.Parameters.TrafoStray (
      V_nom={1,10},
      dv_tap={0.1,0.1}),
      use_tap_1_in=true,
      use_tap_2_in=true)
    PowerSystems.AC3ph.Sensors.PVImeter meter22(V_nom=10)
    PowerSystems.AC3ph.ImpedancesYD.Resistor res22(V_nom=10, r=1000)
    PowerSystems.Control.Relays.TapChangerRelay tapRelay2(
      preset_1={0,0},
      preset_2={0,-1,0,1},
      t_switch_2={1,2,3})
    PowerSystems.Control.Relays.TapChangerRelay tapRelay1(
      preset_1={0,-1,0,1},
      preset_2={0,0},
      t_switch_1={1,2,3})
    PowerSystems.AC3ph.Nodes.GroundOne grd

  equation
    connect(voltage.term, meter1.term_p)
    connect(meter1.term_n, bus.term)
    connect(bus.term, trafo1.term_p)
    connect(trafo1.term_n, meter12.term_p)
    connect(meter12.term_n, res12.term)
    connect(bus.term, trafo2.term_p)
    connect(trafo2.term_n, meter22.term_p)
    connect(meter22.term_n, res22.term)
    connect(grd.term, voltage.neutral)
    connect(tapRelay1.tap_1, trafo1.tap_1_in)
    connect(tapRelay1.tap_2, trafo1.tap_2_in)
    connect(tapRelay2.tap_1, trafo2.tap_1_in)
    connect(tapRelay2.tap_2, trafo2.tap_2_in)
  end TapChanger;

  model ThreeWinding "Three winding transformer"
    extends Modelica.Icons.Example;

    inner PowerSystems.System system

    PowerSystems.AC3ph.Sources.Voltage voltage
    PowerSystems.AC3ph.Sensors.PVImeter meter1
    PowerSystems.AC3ph.Nodes.BusBar bus
    PowerSystems.AC3ph.Transformers.Trafo3Stray trafo3Stray
    PowerSystems.AC3ph.Sensors.PVImeter meter12(V_nom=10)
    PowerSystems.AC3ph.ImpedancesYD.Resistor res12(V_nom=10, r=1000)
    PowerSystems.AC3ph.Sensors.PVImeter meter22(V_nom=10)
    PowerSystems.AC3ph.ImpedancesYD.Resistor res22(V_nom=10, r=1000)
    PowerSystems.AC3ph.Nodes.GroundOne grd

  equation
    connect(voltage.term,meter1. term_p)
    connect(meter1.term_n,bus. term)
    connect(meter12.term_n, res12.term)
    connect(meter22.term_n, res22.term)
    connect(bus.term, trafo3Stray.term_p)
    connect(trafo3Stray.term_na, meter12.term_p)
    connect(trafo3Stray.term_nb, meter22.term_p)
    connect(grd.term, voltage.neutral)
  end ThreeWinding;
end Transformation;


AC transmission, dq0

within PowerSystems.Examples.AC3ph;
package Transmission "AC transmission, dq0"
  extends Modelica.Icons.ExamplesPackage;

model PowerTransfer "Power transfer between two nodes"
  extends Modelica.Icons.Example;

  inner PowerSystems.System system
  PowerSystems.Blocks.Signals.TransientPhasor transPh(
      t_change=30,
      t_duration=60,
      ph_end=2*pi,
      ph_start=0)
  PowerSystems.AC3ph.Sources.InfBus infBus1(V_nom=130e3, use_vPhasor_in=true)
  PowerSystems.AC3ph.Lines.RXline line(redeclare record Data =
    PowerSystems.AC3ph.Lines.Parameters.RXline (
    V_nom=130e3,
    S_nom=100e6),
    len=100e3)
  PowerSystems.AC3ph.Sensors.Psensor sensor
  PowerSystems.AC3ph.Sources.InfBus infBus2(V_nom=130e3)
  PowerSystems.AC3ph.Nodes.GroundOne grd1
  PowerSystems.AC3ph.Nodes.GroundOne grd2

equation
  connect(transPh.y,infBus1.vPhasor_in)
  connect(infBus1.term, sensor.term_p)
  connect(sensor.term_n, line.term_p)
    connect(line.term_n, infBus2.term)
  connect(grd1.term,infBus1. neutral)
    connect(grd2.term, infBus2.neutral)
end PowerTransfer;

model VoltageStability "Voltage stability"
  extends Modelica.Icons.Example;

  inner PowerSystems.System system
  PowerSystems.AC3ph.Sources.InfBus Vsource0(V_nom=400e3, alpha0=0)
  PowerSystems.AC3ph.Sources.InfBus infBus(V_nom=400e3, use_vPhasor_in=true)

  PowerSystems.AC3ph.Sensors.Vmeter Vmeter(V_nom=400e3)
  PowerSystems.AC3ph.Sensors.PVImeter meter0(S_nom=100e6)
  PowerSystems.AC3ph.Lines.RXline line0(redeclare record Data =
    PowerSystems.AC3ph.Lines.Parameters.RXline (
        V_nom=400e3,
        r=2e-3,
        x=0.25e-3), len=500e3)
  PowerSystems.AC3ph.Sources.InfBus Vsource1(V_nom=400e3, alpha0=
          0.087266462599716)
  PowerSystems.AC3ph.Sensors.PVImeter meter1(S_nom=100e6)
  PowerSystems.AC3ph.Lines.RXline line1(redeclare record Data =
    PowerSystems.AC3ph.Lines.Parameters.RXline (
        V_nom=400e3,
        r=2e-3,
        x=0.25e-3), len=500e3)
  PowerSystems.AC3ph.Sources.InfBus Vsource2(V_nom=400e3, alpha0=-0.087266462599716)
  PowerSystems.AC3ph.Sensors.PVImeter meter2(S_nom=100e6)
  PowerSystems.AC3ph.Lines.RXline line2(redeclare record Data =
    PowerSystems.AC3ph.Lines.Parameters.RXline (
        V_nom=400e3,
        r=2e-3,
        x=0.25e-3), len=500e3)
  PowerSystems.Blocks.Signals.TransientPhasor transPh(
      t_change=90,
      t_duration=120,
      a_start=1,
      a_end=0)
  PowerSystems.AC3ph.Nodes.GroundOne grd1
  PowerSystems.AC3ph.Nodes.GroundOne grd4
  PowerSystems.AC3ph.Nodes.GroundOne grd2
  PowerSystems.AC3ph.Nodes.GroundOne grd3

equation
  connect(transPh.y, infBus.vPhasor_in)
  connect(Vsource0.term, line0.term_p)
  connect(line0.term_n, meter0.term_p)
  connect(meter0.term_n, infBus.term)
  connect(Vsource1.term, line1.term_p)
  connect(line1.term_n, meter1.term_p)
  connect(meter1.term_n, infBus.term)
  connect(Vsource2.term, line2.term_p)
  connect(line2.term_n, meter2.term_p)
  connect(meter2.term_n, infBus.term)
  connect(infBus.term, Vmeter.term)
  connect(grd1.term, Vsource0.neutral)
  connect(grd2.term, Vsource1.neutral)
  connect(grd3.term, Vsource2.neutral)
  connect(infBus.neutral, grd4.term)
end VoltageStability;

  model RXline "Single lumped line"
    extends Modelica.Icons.Example;

    inner PowerSystems.System system
    PowerSystems.AC3ph.Sources.InfBus infBus1(V_nom=400e3,
      v0=1.04,
      alpha0=0.5235987755983)
    PowerSystems.AC3ph.Breakers.Switch switch1(V_nom=400e3, I_nom=2500)
    PowerSystems.AC3ph.Lines.RXline line(redeclare record Data =
      PowerSystems.AC3ph.Lines.Parameters.RXline (
        V_nom=400e3,
        x=0.25e-3,
        r=0.02e-3), len=400e3)
    PowerSystems.AC3ph.Breakers.Switch switch2(V_nom=400e3, I_nom=2500)
    PowerSystems.AC3ph.Sources.InfBus infBus2(V_nom=400e3)
    PowerSystems.Control.Relays.SwitchRelay relay1(
      t_switch={0.2,0.65})
    PowerSystems.Control.Relays.SwitchRelay relay2(
      t_switch={0.3,0.7})
    PowerSystems.AC3ph.Sensors.PVImeter meter(V_nom=400e3, S_nom=1000e6)
    PowerSystems.AC3ph.Nodes.GroundOne grd1
    PowerSystems.AC3ph.Nodes.GroundOne grd2

  equation
    connect(relay1.y, switch1.control)
    connect(relay2.y, switch2.control)
    connect(infBus1.term, meter.term_p)
    connect(meter.term_n, switch1.term_p)
    connect(switch1.term_n, line.term_p)
    connect(line.term_n, switch2.term_p)
    connect(switch2.term_n, infBus2.term)
    connect(grd1.term,infBus1. neutral)
    connect(infBus2.neutral, grd2.term)
  end RXline;

  model Tline "Single PI-line"
    extends Modelica.Icons.Example;

    inner PowerSystems.System system
    PowerSystems.AC3ph.Sources.InfBus infBus1(V_nom=400e3,
      v0=1.04,
      alpha0=0.5235987755983)
    PowerSystems.AC3ph.Breakers.Breaker breaker1(V_nom=400e3, I_nom=2500)
    PowerSystems.AC3ph.Lines.Tline line(len=400e3, redeclare record Data =
          PowerSystems.AC3ph.Lines.Parameters.Line (
          V_nom=400e3,
          x=0.25e-3,
          r=0.02e-3))
    PowerSystems.AC3ph.Breakers.Breaker breaker2(V_nom=400e3, I_nom=2500)
    PowerSystems.AC3ph.Sources.InfBus infBus2(V_nom=400e3)
    PowerSystems.Control.Relays.SwitchRelay relay1(
      t_switch={0.2,0.65})
    PowerSystems.Control.Relays.SwitchRelay relay2(
      t_switch={0.3,0.7})
    PowerSystems.AC3ph.Sensors.PVImeter meter(V_nom=400e3, S_nom=1000e6)
    PowerSystems.AC3ph.Nodes.GroundOne grd1
    PowerSystems.AC3ph.Nodes.GroundOne grd2

  equation
    connect(relay1.y, breaker1.control)
    connect(relay2.y, breaker2.control)
    connect(infBus1.term, meter.term_p)
    connect(meter.term_n, breaker1.term_p)
    connect(breaker1.term_n, line.term_p)
    connect(line.term_n, breaker2.term_p)
    connect(breaker2.term_n, infBus2.term)
    connect(grd1.term,infBus1. neutral)
    connect(infBus2.neutral, grd2.term)
  end Tline;

  model FaultRXline "Faulted lumped line"
    extends Modelica.Icons.Example;

    inner PowerSystems.System system
    PowerSystems.AC3ph.Sources.InfBus infBus1(V_nom=400e3,
      v0=1.04,
      alpha0=0.5235987755983)
    PowerSystems.AC3ph.Breakers.Switch switch1(V_nom=400e3, I_nom=2500)
    PowerSystems.AC3ph.Lines.FaultRXline line(redeclare record Data =
      PowerSystems.AC3ph.Lines.Parameters.RXline (
        V_nom=400e3,
        x=0.25e-3,
        r=0.02e-3), len=400e3)
    PowerSystems.AC3ph.Breakers.Switch switch2(V_nom=400e3, I_nom=2500)
    PowerSystems.AC3ph.Sources.InfBus infBus2(V_nom=400e3)
    PowerSystems.Control.Relays.SwitchRelay relay1(
      t_switch={0.2,0.65})
    PowerSystems.Control.Relays.SwitchRelay relay2(
      t_switch={0.3,0.7})
    PowerSystems.AC3ph.Sensors.PVImeter meter(V_nom=400e3, S_nom=1000e6)
    PowerSystems.AC3ph.Faults.Fault_abc abc
    PowerSystems.AC3ph.Nodes.GroundOne grd1
    PowerSystems.AC3ph.Nodes.GroundOne grd2

  equation
    connect(relay1.y, switch1.control)
    connect(relay2.y, switch2.control)
    connect(infBus1.term, meter.term_p)
    connect(meter.term_n, switch1.term_p)
    connect(switch1.term_n, line.term_p)
    connect(line.term_n, switch2.term_p)
    connect(switch2.term_n, infBus2.term)
    connect(line.term_f, abc.term)
    connect(grd1.term,infBus1. neutral)
    connect(infBus2.neutral, grd2.term)
  end FaultRXline;

  model FaultTline "Faulted PI-line"
    extends Modelica.Icons.Example;

    inner PowerSystems.System system
    PowerSystems.AC3ph.Sources.InfBus infBus1(V_nom=400e3,
      v0=1.04,
      alpha0=0.5235987755983)
    PowerSystems.AC3ph.Breakers.Breaker breaker1(V_nom=400e3, I_nom=2500)
    PowerSystems.AC3ph.Lines.FaultTline line(len=400e3, redeclare record Data =
          PowerSystems.AC3ph.Lines.Parameters.Line (
          V_nom=400e3,
          x=0.25e-3,
          r=0.02e-3))
    PowerSystems.AC3ph.Breakers.Breaker breaker2(V_nom=400e3, I_nom=2500)
    PowerSystems.AC3ph.Sources.InfBus infBus2(V_nom=400e3)
    PowerSystems.Control.Relays.SwitchRelay relay1(
      t_switch={0.2,0.65})
    PowerSystems.Control.Relays.SwitchRelay relay2(
      t_switch={0.3,0.7})
    PowerSystems.AC3ph.Sensors.PVImeter meter(V_nom=400e3, S_nom=1000e6)
    PowerSystems.AC3ph.Faults.Fault_abc abc
    PowerSystems.AC3ph.Nodes.GroundOne grd1
    PowerSystems.AC3ph.Nodes.GroundOne grd2

  equation
    connect(relay1.y, breaker1.control)
    connect(relay2.y, breaker2.control)
    connect(infBus1.term, meter.term_p)
    connect(meter.term_n, breaker1.term_p)
    connect(breaker1.term_n, line.term_p)
    connect(line.term_n, breaker2.term_p)
    connect(breaker2.term_n, infBus2.term)
    connect(line.term_f, abc.term)
    connect(grd1.term,infBus1. neutral)
    connect(infBus2.neutral, grd2.term)
  end FaultTline;

  model DoubleRXline "Parallel lumped lines, one faulted"
    extends Modelica.Icons.Example;

    inner PowerSystems.System system
    PowerSystems.AC3ph.Sources.InfBus infBus1(V_nom=20e3, alpha0=
          0.5235987755983)
    PowerSystems.AC3ph.Transformers.TrafoStray trafo(
      redeclare record Data =
          PowerSystems.Examples.Data.Transformers.TrafoStray,
      redeclare model Topology_p = PowerSystems.AC3ph.Ports.Topology.Delta,
      redeclare model Topology_n = PowerSystems.AC3ph.Ports.Topology.Y)
    PowerSystems.AC3ph.Lines.RXline line(len=480000, redeclare record Data =
          PowerSystems.Examples.Data.Lines.OHline400kV)
    PowerSystems.AC3ph.Breakers.Switch switch1(V_nom=400e3, I_nom=2500)
    PowerSystems.AC3ph.Lines.FaultRXline lineF(
      redeclare record Data = PowerSystems.Examples.Data.Lines.OHline400kV,
        len=430000)
    PowerSystems.AC3ph.Breakers.Switch switch2(V_nom=400e3, I_nom=2500)
    PowerSystems.AC3ph.Faults.Fault_abc abc
    PowerSystems.AC3ph.Sources.InfBus InfBus2(V_nom=400e3, alpha0=
          0.5235987755983)
    PowerSystems.Control.Relays.SwitchRelay relay1(t_switch={0.15,0.2})
    PowerSystems.Control.Relays.SwitchRelay relay2(t_switch={0.153,0.21})
    PowerSystems.AC3ph.Sensors.PVImeter meterL(S_nom=1000e6, V_nom=400e3)
    PowerSystems.AC3ph.Sensors.PVImeter meterF(S_nom=1000e6, V_nom=400e3)
    PowerSystems.AC3ph.Nodes.GroundOne grd1
    PowerSystems.AC3ph.Nodes.GroundOne grd2
  equation
    connect(relay1.y, switch1.control)
    connect(relay2.y, switch2.control)
    connect(trafo.term_n, meterL.term_p)
    connect(meterL.term_n, line.term_p)
    connect(line.term_n, InfBus2.term)
    connect(trafo.term_n, switch1.term_p)
    connect(switch1.term_n, meterF.term_p)
    connect(meterF.term_n, lineF.term_p)
    connect(lineF.term_n, switch2.term_p)
    connect(switch2.term_n, InfBus2.term)
    connect(lineF.term_f, abc.term)
    connect(infBus1.term, trafo.term_p)
    connect(grd1.term,infBus1. neutral)
    connect(InfBus2.neutral, grd2.term)
  end DoubleRXline;

  model DoubleLine "Parallel lines, one faulted"
    extends Modelica.Icons.Example;

    inner PowerSystems.System system
    PowerSystems.AC3ph.Sources.InfBus infBus1(V_nom=20e3, alpha0=
          0.5235987755983)
    PowerSystems.AC3ph.Transformers.TrafoStray trafo(
      redeclare record Data =
        PowerSystems.Examples.Data.Transformers.TrafoStray,
      redeclare model Topology_p = PowerSystems.AC3ph.Ports.Topology.Delta,
      redeclare model Topology_n = PowerSystems.AC3ph.Ports.Topology.Y)
    PowerSystems.AC3ph.Lines.PIline line(
                                        len=480000, redeclare record Data =
          PowerSystems.Examples.Data.Lines.OHline_400kV)
    PowerSystems.AC3ph.Breakers.Switch switch1(V_nom=400e3, I_nom=2500)
    PowerSystems.AC3ph.Lines.FaultTline lineF(
      redeclare record Data =
          PowerSystems.Examples.Data.Lines.OHline_400kV, len=430000)
    PowerSystems.AC3ph.Breakers.Switch switch2(V_nom=400e3, I_nom=2500)
    PowerSystems.AC3ph.Faults.Fault_abc abc
    PowerSystems.AC3ph.Sources.InfBus InfBus2(V_nom=400e3, alpha0=
          0.5235987755983)
    PowerSystems.Control.Relays.SwitchRelay relay1(t_switch={0.15,0.2})
    PowerSystems.Control.Relays.SwitchRelay relay2(t_switch={0.153,0.21})
    PowerSystems.AC3ph.Sensors.PVImeter meterL(S_nom=1000e6, V_nom=400e3)
    PowerSystems.AC3ph.Sensors.PVImeter meterF(S_nom=1000e6, V_nom=400e3)
    PowerSystems.AC3ph.Nodes.GroundOne grd1
    PowerSystems.AC3ph.Nodes.GroundOne grd2

  equation
    connect(relay1.y, switch1.control)
    connect(relay2.y, switch2.control)
    connect(trafo.term_n, meterL.term_p)
    connect(meterL.term_n, line.term_p)
    connect(line.term_n, InfBus2.term)
    connect(trafo.term_n, switch1.term_p)
    connect(switch1.term_n, meterF.term_p)
    connect(meterF.term_n, lineF.term_p)
    connect(lineF.term_n, switch2.term_p)
    connect(switch2.term_n, InfBus2.term)
    connect(lineF.term_f, abc.term)
    connect(infBus1.term, trafo.term_p)
    connect(grd1.term, infBus1.neutral)
    connect(grd2.term, InfBus2.neutral)
  end DoubleLine;

  model DoubleRXlineTG
    "Parallel lumped lines with turbo generator, one line faulted"
    extends Modelica.Icons.Example;

    inner PowerSystems.System system
    PowerSystems.AC3ph.Generation.TurboGenerator turbGen(
      p_start=0.762922,
      redeclare model Generator = PowerSystems.AC3ph.Machines.Synchron_ee (
            redeclare replaceable record Data =
              PowerSystems.AC3ph.Machines.Parameters.Synchron_ee (
              V_nom=20e3,
              S_nom=1000e6,
              If_nom=2000), dynType=PowerSystems.Types.Dynamics.FreeInitial) "nth order",
      initType=PowerSystems.Types.Init.v_alpha,
      alpha_start=0.5235987755983)
    PowerSystems.AC3ph.Transformers.TrafoStray trafo(
      redeclare record Data =
        PowerSystems.Examples.Data.Transformers.TrafoStray,
      redeclare model Topology_p = PowerSystems.AC3ph.Ports.Topology.Delta,
      redeclare model Topology_n = PowerSystems.AC3ph.Ports.Topology.Y)
    PowerSystems.AC3ph.Lines.RXline line(
      len=480000,
      redeclare record Data = PowerSystems.Examples.Data.Lines.OHline400kV)
    PowerSystems.AC3ph.Breakers.Switch switch1(V_nom=400e3, I_nom=2500)
    PowerSystems.AC3ph.Lines.FaultRXline lineF(
      redeclare record Data = PowerSystems.Examples.Data.Lines.OHline400kV,
        len=430000)
    PowerSystems.AC3ph.Breakers.Switch switch2(V_nom=400e3, I_nom=2500)
    PowerSystems.AC3ph.Faults.Fault_abc abc(epsG=1e-5)
    PowerSystems.AC3ph.Sources.InfBus InfBus(V_nom=400e3, alpha0=
          0.5235987755983)
    PowerSystems.Control.Relays.SwitchRelay relay1(t_switch={0.15,0.2})
    PowerSystems.Control.Relays.SwitchRelay relay2(t_switch={0.153,0.21})
    PowerSystems.AC3ph.Sensors.PVImeter meterL(S_nom=1000e6, V_nom=400e3)
    PowerSystems.AC3ph.Sensors.PVImeter meterF(S_nom=1000e6, V_nom=400e3)
    PowerSystems.Control.Setpoints.Set_w_p_v cst_set
    PowerSystems.AC3ph.Nodes.GroundOne grd2
    PowerSystems.Common.Thermal.BoundaryV boundary(m=2)
  equation
    connect(relay1.y, switch1.control)
    connect(relay2.y, switch2.control)
    connect(cst_set.setpts, turbGen.setpts)
    connect(turbGen.term, trafo.term_p)
    connect(trafo.term_n, meterL.term_p)
    connect(meterL.term_n, line.term_p)
    connect(line.term_n, InfBus.term)
    connect(trafo.term_n, switch1.term_p)
    connect(switch1.term_n, meterF.term_p)
    connect(meterF.term_n, lineF.term_p)
    connect(lineF.term_n, switch2.term_p)
    connect(switch2.term_n, InfBus.term)
    connect(lineF.term_f, abc.term)
    connect(InfBus.neutral, grd2.term)
    connect(turbGen.heat, boundary.heat)
  end DoubleRXlineTG;

  model DoubleLineTG "Parallel lines with turbo generator, one line faulted"
    extends Modelica.Icons.Example;

    inner PowerSystems.System system
    PowerSystems.AC3ph.Generation.TurboGenerator turbGen(
      p_start=0.761825,
      redeclare model Generator = PowerSystems.AC3ph.Machines.Synchron_ee (
        redeclare replaceable record Data =
          PowerSystems.AC3ph.Machines.Parameters.Synchron_ee (
          V_nom=20e3,
          S_nom=1000e6,
          If_nom=2000), dynType=PowerSystems.Types.Dynamics.FreeInitial) "nth order",
      alpha_start=0.5235987755983)
    PowerSystems.AC3ph.Transformers.TrafoStray trafo(
      redeclare record Data =
        PowerSystems.Examples.Data.Transformers.TrafoStray,
      redeclare model Topology_p = PowerSystems.AC3ph.Ports.Topology.Delta,
      redeclare model Topology_n = PowerSystems.AC3ph.Ports.Topology.Y)
    PowerSystems.AC3ph.Lines.PIline line(
                                        len=480e3, redeclare record Data =
          PowerSystems.Examples.Data.Lines.OHline_400kV)
    PowerSystems.AC3ph.Breakers.Switch switch1(V_nom=400e3, I_nom=2500)
    PowerSystems.AC3ph.Lines.FaultTline lineF(
      redeclare record Data =
          PowerSystems.Examples.Data.Lines.OHline_400kV, len=430000)
    PowerSystems.AC3ph.Breakers.Switch switch2(V_nom=400e3, I_nom=2500)
    PowerSystems.AC3ph.Faults.Fault_abc abc(epsG=1e-5)
    PowerSystems.AC3ph.Sources.InfBus InfBus(V_nom=400e3, alpha0=
          0.5235987755983)
    PowerSystems.Control.Relays.SwitchRelay relay1(t_switch={0.15,0.2})
    PowerSystems.Control.Relays.SwitchRelay relay2(t_switch={0.153,0.21})
    PowerSystems.AC3ph.Sensors.PVImeter meterL(S_nom=1000e6, V_nom=400e3)
    PowerSystems.AC3ph.Sensors.PVImeter meterF(S_nom=1000e6, V_nom=400e3)
    PowerSystems.Control.Setpoints.Set_w_p_v cst_set
    PowerSystems.AC3ph.Nodes.GroundOne grd2
    PowerSystems.Common.Thermal.BoundaryV boundary(m=2)
  equation
    connect(relay1.y, switch1.control)
    connect(relay2.y, switch2.control)
    connect(cst_set.setpts, turbGen.setpts)
    connect(turbGen.term, trafo.term_p)
    connect(trafo.term_n, meterL.term_p)
    connect(meterL.term_n, line.term_p)
    connect(line.term_n, InfBus.term)
    connect(trafo.term_n, switch1.term_p)
    connect(switch1.term_n, meterF.term_p)
    connect(meterF.term_n, lineF.term_p)
    connect(lineF.term_n, switch2.term_p)
    connect(switch2.term_n, InfBus.term)
    connect(lineF.term_f, abc.term)
    connect(InfBus.neutral, grd2.term)
    connect(turbGen.heat, boundary.heat)
  end DoubleLineTG;
end Transmission;


Breaker example data

within PowerSystems.Examples.Data;
package Breakers "Breaker example data"
  extends Modelica.Icons.MaterialPropertiesPackage;
end Breakers;


Line example data

within PowerSystems.Examples.Data;
package Lines "Line example data"
  extends Modelica.Icons.MaterialPropertiesPackage;

  record OHline15kV1ph "Overhead RX-line 15kV 1-phase rail, example"
    extends PowerSystems.AC1ph_DC.Lines.Parameters.RXline(
      puUnits=true,
      V_nom=15e3,
      S_nom=100e6,
      f_nom=50/3,
      r={3e-3,3e-3},
      x=2e-3,
      x0=5e-3);
  end OHline15kV1ph;

  record OHline_15kV1ph "Overhead PI-line 15kV 1-phase rail, example"
    extends PowerSystems.AC1ph_DC.Lines.Parameters.Line(
      puUnits=true,
      V_nom=132e3,
      S_nom=100e6,
      f_nom=50/3,
      r={3e-3,3e-3},
      x=2e-3,
      x0=5e-3,
      g_pg=1e-10,
      g_pp=1e-10,
      b_pg=0.02e-3,
      b_pp=0.02e-3);
  end OHline_15kV1ph;

record OHline132kV1ph "Overhead RX-line 132kV 1-phase rail, example"
  extends PowerSystems.AC1ph_DC.Lines.Parameters.RXline(
    puUnits=true,
    V_nom=132e3,
    S_nom=100e6,
    f_nom=50/3,
    r={1.2e-3,1.2e-3},
    x=1e-3,
    x0=3e-3);
end OHline132kV1ph;

record OHline_132kV1ph "Overhead PI-line 132kV 1-phase rail, example"
  extends PowerSystems.AC1ph_DC.Lines.Parameters.Line(
    puUnits=true,
    V_nom=132e3,
    S_nom=100e6,
    f_nom=50/3,
    r={1.2e-3,1.2e-3},
    x=1e-3,
    x0=3e-3,
    g_pg=1e-10,
    g_pp=1e-10,
    b_pg=0.05e-3,
    b_pp=0.05e-3);
end OHline_132kV1ph;

  record Cable132kVDC "Cable RX-line 132kV DC, example"
    extends PowerSystems.AC1ph_DC.Lines.Parameters.RXline(
      puUnits=true,
      V_nom=132e3,
      S_nom=100e6,
      f_nom=50,
      r={0.65e-3,0.65e-3},
      x=0.8e-3,
      x0=3e-3);
  end Cable132kVDC;

  record Cable_132kVDC "Cable PI-line 132kV DC, example"
    extends PowerSystems.AC1ph_DC.Lines.Parameters.Line(
      puUnits=true,
      V_nom=132e3,
      S_nom=100e6,
      f_nom=50,
      r={0.65e-3,0.65e-3},
      x=0.08e-3,
      x0=3e-3,
      g_pg=1e-10,
      g_pp=1e-10,
      b_pg=0.003,
      b_pp=0.003);
  end Cable_132kVDC;

  record OHline132kV "Overhead RX-line 132kV 3-phase, example"
    extends PowerSystems.AC3ph.Lines.Parameters.RXline(
      puUnits=true,
      V_nom=132e3,
      S_nom=100e6,
      f_nom=50,
      r=1.2e-3,
      x=3e-3,
      x0=9e-3);
  end OHline132kV;

  record OHline_132kV "Overhead PI-line 132kV 3-phase, example"
    extends PowerSystems.AC3ph.Lines.Parameters.Line(
      puUnits=true,
      V_nom=132e3,
      S_nom=100e6,
      f_nom=50,
      r=1.2e-3,
      x=3e-3,
      x0=9e-3,
      g_pg=1e-10,
      g_pp=1e-10,
      b_pg=0.05e-3,
      b_pp=0.05e-3);
  end OHline_132kV;

  record OHline400kV "Overhead RX-line 400kV 3-phase, example"
    extends PowerSystems.AC3ph.Lines.Parameters.RXline(
      puUnits=true,
      V_nom=400e3,
      S_nom=100e6,
      f_nom=50,
      r=0.02e-3,
      x=0.25e-3,
      x0=0.75e-3);
  end OHline400kV;

  record OHline_400kV "Overhead PI-line 400kV 3-phase, example"
    extends PowerSystems.AC3ph.Lines.Parameters.Line(
      puUnits=true,
      V_nom=400e3,
      S_nom=100e6,
      f_nom=50,
      r=0.02e-3,
      x=0.25e-3,
      x0=0.75e-3,
      g_pg=1e-10,
      g_pp=1e-10,
      b_pg=0.003e-3,
      b_pp=0.003e-3);
  end OHline_400kV;

  record Cable132kV "Cable RX-line 132kV 3-phase, example"
    extends PowerSystems.AC3ph.Lines.Parameters.RXline(
      puUnits=true,
      V_nom=132e3,
      S_nom=100e6,
      f_nom=50,
      r=0.65e-3,
      x=0.8e-3,
      x0=3e-3);
  end Cable132kV;

  record Cable_132kV "Cable PI-line 132kV 3-phase, example"
    extends PowerSystems.AC3ph.Lines.Parameters.Line(
      puUnits=true,
      V_nom=132e3,
      S_nom=100e6,
      f_nom=50,
      r=0.65e-3,
      x=0.08e-3,
      x0=3e-3,
      g_pg=1e-10,
      g_pp=1e-10,
      b_pg=0.003,
      b_pp=0.003);
  end Cable_132kV;

  record Cable400kV "Cable RX-line 400kV 3-phase, example"
    extends PowerSystems.AC3ph.Lines.Parameters.RXline(
      puUnits=true,
      V_nom=400e3,
      S_nom=100e6,
      f_nom=50,
      r=0.05e-3,
      x=0.085e-3,
      x0=3e-3);
  end Cable400kV;

  record Cable_400kV "Cable PI-line 400kV 3-phase, example"
    extends PowerSystems.AC3ph.Lines.Parameters.Line(
      puUnits=true,
      V_nom=400e3,
      S_nom=100e6,
      f_nom=50,
      r=0.05e-3,
      x=0.085e-3,
      x0=3e-3,
      g_pg=1e-10,
      g_pp=1e-10,
      b_pg=0.025,
      b_pp=0.025);
  end Cable_400kV;
end Lines;


Machine example data

within PowerSystems.Examples.Data;
package Machines "Machine example data"
  extends Modelica.Icons.MaterialPropertiesPackage;

record DCser1500V_1p5MVA "DC machine series excited, example"
  extends PowerSystems.AC1ph_DC.Machines.Parameters.DCser(
    pp=2,
    l_fd=0.15,
    r_fd=0.01,
    l_q=0.5,
    r_q=0.05,
    puUnits=true,
    V_nom=1500,
    S_nom=1.5e6,
    w_nom=157.079632679489661923);
end DCser1500V_1p5MVA;

record DCpar1500V_1p5MVA "DC machine parallel excited, example"
  extends PowerSystems.AC1ph_DC.Machines.Parameters.DCpar(
    pp=2,
    l_fd=100*pi,
    r_fd=100,
    l_q=0.5,
    r_q=0.05,
    puUnits=true,
    V_nom=1500,
    S_nom=1.5e6,
    w_nom=157.079632679489661923,
    Vf_nom=1500);
end DCpar1500V_1p5MVA;

record DCpm100V_1kVA "DC machine permanent magnet excited, example"
  extends PowerSystems.AC1ph_DC.Machines.Parameters.DCpm(
    pp=2,
    l_aq=0.5,
    r_aq=0.05,
    puUnits=true,
    V_nom(displayUnit="V")=100,
    S_nom(displayUnit="kVA")=1e3,
    w_nom=157.079632679489661923);
end DCpm100V_1kVA;

record BLDC100V_1kVA
    "BLDC machine (= synchronous pm, 3rd order model), example pu-units"
  extends PowerSystems.AC3ph.Machines.Parameters.Synchron3rd(
    puUnits=true,
    V_nom(displayUnit="V")=100*sqrt(3/2)/2,
    S_nom(displayUnit="kVA")=1e3,
    f_nom=60,
    neu_iso=false,
    pp=2,
    final excite=2,
    psi_pm(unit="1")=1.2,
    x_d=0.4,
    x_q=0.4,
    x_0=0.1,
    r_s=0.05,
    r_n=1);
end BLDC100V_1kVA;

record BLDC100V_1kVA_SI
    "BLDC machine (= synchronous pm, 3rd order model), example SI-units"
  extends PowerSystems.AC3ph.Machines.Parameters.Synchron3rd(
    puUnits=false,
    V_nom(displayUnit="V")=100*sqrt(3/2)/2,
    S_nom(displayUnit="kVA")=1e3,
    f_nom=60,
    neu_iso=false,
    pp=2,
    final excite=2,
    psi_pm(unit="1")=1.2,
    x_d=1.5,
    x_q=1.5,
    x_0=0.375,
    r_s=0.1875,
    r_n=1);
end BLDC100V_1kVA_SI;

  record Asynchron400V_30kVA "Asynchronous machine, example"
    extends PowerSystems.AC3ph.Machines.Parameters.Asynchron(
      neu_iso=false,
      pp=8,
      x=3,
      x_0=0.1,
      r_s=0.04,
      r_n=1,
      n_r=1,
      transDat=true,
      use_xtr=true,
      xtr={0.196667},
      tc={0.0130419},
      to={0.198944},
      xsig_s=0.1,
      xsig_r={0.1},
      r_r={0.04},
      puUnits=true,
      V_nom(displayUnit="V")=400,
      S_nom(displayUnit="kVA")=30e3,
      f_nom=50);
  end Asynchron400V_30kVA;

  record Asynchron3kV_1p5MVA "Asynchronous machine, example"
    extends PowerSystems.AC3ph.Machines.Parameters.Asynchron(
      neu_iso=false,
      pp=2,
      x=2.8,
      x_0=0.1,
      r_s=0.02,
      r_n=1,
      n_r=2,
      transDat=true,
      use_xtr=true,
      xtr={0.1, 0.075},
      tc={0.014, 0.4e-3},
      to={0.4, 2.8e-3},
      xsig_s=0.05,
      xsig_r={0.0529633, 0.0481803},
      r_r={0.0234304, 0.580595},
      puUnits=true,
      V_nom=3000,
      S_nom=1.5e6,
      f_nom=50);
  end Asynchron3kV_1p5MVA;

  record Synchron3rd_pm400V_30kVA
    "Synchronous machine pm, 3rd order model, example"
    extends PowerSystems.AC3ph.Machines.Parameters.Synchron3rd_pm(
      neu_iso=false,
      pp=2,
      psi_pm=1.1,
      x_d=0.4,
      x_q=0.4,
      x_0=0.1,
      r_s=0.03,
      r_n=1,
      puUnits=true,
      V_nom(displayUnit="V")=400,
      S_nom(displayUnit="kVA")=30e3,
      f_nom=50);
  end Synchron3rd_pm400V_30kVA;

  record Synchron_pm400V_30kVA "Synchronous machine pm, example"
    extends PowerSystems.AC3ph.Machines.Parameters.Synchron_pm(
      neu_iso=false,
      pp=2,
      psi_pm=1.1,
      x_d=0.4,
      x_q=0.4,
      x_0=0.1,
      r_s=0.03,
      r_n=1,
      transDat=true,
      use_xtr=true,
      xtr_d={0.142857},
      xtr_q={0.142857},
      tc_d={0.00994718},
      tc_q={0.00994718},
      to_d={0.0278521},
      to_q={0.0278521},
      use_if0=false,
      if0=0,
      alpha_if0=0,
      tol=1e-6,
      xsig_s=0.1,
      xsig_rd={0.05},
      xsig_rq={0.05},
      xm_d=fill(0, 0),
      r_rd={0.04},
      r_rq={0.04},
      puUnits=true,
      V_nom(displayUnit="V")=400,
      S_nom(displayUnit="kVA")=30e3,
      f_nom=50,
      If_nom=0);
  end Synchron_pm400V_30kVA;

  record Synchron3rd_pm560V_100kVA "Synchronous machine 3rd order pm, example"
    extends PowerSystems.AC3ph.Machines.Parameters.Synchron3rd_pm(
      neu_iso=false,
      pp=2,
      psi_pm=1.1,
      x_d=0.4,
      x_q=0.4,
      x_0=0.1,
      r_s=0.03,
      r_n=1,
      puUnits=true,
      V_nom(displayUnit="V")=560,
      S_nom(displayUnit="kVA")=100e3,
      f_nom=400);
  end Synchron3rd_pm560V_100kVA;

  record Synchron_pm560V_100kVA "Synchronous machine pm, example"
    extends PowerSystems.AC3ph.Machines.Parameters.Synchron_pm(
      neu_iso=false,
      pp=2,
      psi_pm=1.1,
      x_d=0.4,
      x_q=0.4,
      x_0=0.1,
      r_s=0.03,
      r_n=1,
      transDat=true,
      use_xtr=true,
      xtr_d={0.142857},
      xtr_q={0.142857},
      tc_d={0.0132629},
      tc_q={0.0132629},
      to_d={0.0371362},
      to_q={0.0371362},
      use_if0=false,
      if0=0,
      alpha_if0=0,
      tol=1e-6,
      xsig_s=0.1,
      xsig_rd={0.05},
      xsig_rq={0.05},
      xm_d=fill(0, 0),
      r_rd={0.03},
      r_rq={0.03},
      puUnits=true,
      V_nom(displayUnit="V")=560,
      S_nom(displayUnit="kVA")=100e3,
      f_nom=400,
      If_nom=0);
  end Synchron_pm560V_100kVA;

  record Synchron3rd_ee20kV_1200MVA
    "Synchronous machine, 3rd order model, example"
    extends PowerSystems.AC3ph.Machines.Parameters.Synchron3rd_ee(
      neu_iso=false,
      pp=1,
      x_d=2.29,
      x_q=1.95,
      x_0=0.1,
      r_s=0.004,
      r_n=1,
      puUnits=true,
      V_nom=20e3,
      S_nom=1200e6,
      f_nom=50);
  end Synchron3rd_ee20kV_1200MVA;

  record Synchron_ee20kV_1200MVA "Synchronous machine, example"
    extends PowerSystems.AC3ph.Machines.Parameters.Synchron_ee(
      neu_iso=false,
      pp=1,
      x_d=2.29,
      x_q=1.95,
      x_0=0.1,
      r_s=0.004,
      r_n=1,
      transDat=true,
      use_xtr=true,
      xtr_d={0.361123, 0.283696},
      xtr_q={0.464817, 0.303593},
      tc_d={0.93133, 0.0253919},
      tc_q={0.270197, 0.041458},
      to_d={5.94309, 0.0321197},
      to_q={1.207780, 0.0595723},
      use_if0=true,
      if0=0.286,
      alpha_if0=-0.97738438111682,
      tol=1e-6,
      xsig_s=0.2,
      xsig_rd={0.1294, 0.03498},
      xsig_rq={0.3948, 0.1527},
      xm_d={0.05965},
      r_rd={1.321e-3, 14.38e-3},
      r_rq={7.037e-3, 20.38e-3},
      puUnits=true,
      V_nom=20e3,
      S_nom=1200e6,
      f_nom=50,
      If_nom=8000);
  end Synchron_ee20kV_1200MVA;

  record Synchron3rd_ee60Hz_26kV_720MVA
    "Synchronous machine, 3rd order model, example"
    extends PowerSystems.AC3ph.Machines.Parameters.Synchron3rd_ee(
      neu_iso=false,
      pp=1,
      x_d=1.9,
      x_q=1.77,
      x_0=0.1,
      r_s=0.005,
      r_n=1,
      puUnits=true,
      V_nom=26e3,
      S_nom=720e6,
      f_nom=60);
  end Synchron3rd_ee60Hz_26kV_720MVA;

  record Synchron_ee60Hz_26kV_720MVA "Synchronous machine, example"
    extends PowerSystems.AC3ph.Machines.Parameters.Synchron_ee(
      neu_iso=false,
      pp=1,
      x_d=1.9,
      x_q=1.77,
      x_0=0.1,
      r_s=0.005,
      r_n=1,
      transDat=true,
      use_xtr=true,
      xtr_d={0.3290, 0.253},
      xtr_q={0.436, 0.2730},
      tc_d={0.7158, 0.02058},
      tc_q={0.2134, 0.03320},
      to_d={4.164, 0.02658},
      to_q={0.9307, 0.04936},
      use_if0=true,
      if0=0.835428,
      alpha_if0=-1.7910219718115,
      tol=1e-6,
      xsig_s=0.17,
      xsig_rd={0.1294, 0.03498},
      xsig_rq={0.3948, 0.1527},
      xm_d={0.05965},
      r_rd={1.321e-3, 14.38e-3},
      r_rq={7.037e-3, 20.38e-3},
      puUnits=true,
      V_nom=26e3,
      S_nom=720e6,
      f_nom=60,
      If_nom=1800);
  end Synchron_ee60Hz_26kV_720MVA;

  record SynchronIso20kV_500MVA
    "Synchronous machine (isotropic), 3rd order model"
    extends PowerSystems.AC3ph.Machines.Parameters.Synchron3rd_ee(
      neu_iso=false,
      pp=1,
      x_d=1.6,
      x_q=1.6,
      x_0=0.1,
      r_s=0.01,
      r_n=1,
      puUnits=true,
      V_nom=20e3,
      S_nom=500e6,
      f_nom=50);
  end SynchronIso20kV_500MVA;
end Machines;


Data

within PowerSystems.Examples;
package Data "Data"
  extends Modelica.Icons.MaterialPropertiesPackage;
end Data;


Breaker example data

within PowerSystems.Examples.Data;
package Semiconductors "Breaker example data"
  extends Modelica.Icons.MaterialPropertiesPackage;

  record IdealSC100V_10A "Ideal semiconductor parameters, example"
    extends PowerSystems.Semiconductors.Ideal.SCparameter(
      V_nom=100,
      I_nom=10,
      eps={1e-4,1e-4},
      Vf=1,
      Hsw_nom=2e-3,
      cT_loss=fill(0,0),
      T0_loss=300);
  end IdealSC100V_10A;

  record IdealSC1kV_100A "Ideal semiconductor parameters, example"
    extends PowerSystems.Semiconductors.Ideal.SCparameter(
      V_nom=100,
      I_nom=10,
      eps={1e-4,1e-4},
      Vf=2.5,
      Hsw_nom=0.25,
      cT_loss=fill(0,0),
      T0_loss=300);
  end IdealSC1kV_100A;

  record IdealSC3kV_500A "Ideal semiconductor parameters, example"
    extends PowerSystems.Semiconductors.Ideal.SCparameter(
      V_nom=3e3,
      I_nom=500,
      eps={1e-4,1e-4},
      Vf=3,
      Hsw_nom=5,
      cT_loss={0.005},
      T0_loss=300);
  end IdealSC3kV_500A;
end Semiconductors;


Transformer example data

within PowerSystems.Examples.Data;
package Transformers "Transformer example data"
  extends Modelica.Icons.MaterialPropertiesPackage;

record TrafoIdeal1ph "Ideal trafo, 1-phase, example"
  extends PowerSystems.AC1ph_DC.Transformers.Parameters.TrafoIdeal1ph(
    puUnits=true,
    V_nom={15000,1400},
    S_nom=5e6,
    f_nom=50/3,
    tap_neutral={3,3},
    dv_tap={0.025,0.025});
end TrafoIdeal1ph;

record TrafoStray1ph "Trafo with ideal magnetic coupling, 1-phase, example"
  extends PowerSystems.AC1ph_DC.Transformers.Parameters.TrafoStray1ph(
    puUnits=true,
    V_nom={15000,1400},
    S_nom=5e6,
    f_nom=50/3,
    tap_neutral={3,3},
    dv_tap={0.025,0.025},
    r={0.03,0.03},
    x={0.05,0.05});
end TrafoStray1ph;

record TrafoMag1ph "Trafo with magnetic coupling, 1-phase, example"
  extends PowerSystems.AC1ph_DC.Transformers.Parameters.TrafoMag1ph(
    puUnits=true,
    V_nom={15000,1400},
    S_nom=5e6,
    f_nom=50/3,
    tap_neutral={3,3},
    dv_tap={0.025,0.025},
    r={0.03,0.03},
    x={0.05,0.05},
    redc=500,
    xm=500);
end TrafoMag1ph;

record TrafoSat1ph "Trafo with saturation, 1-phase, example"
  extends PowerSystems.AC1ph_DC.Transformers.Parameters.TrafoSat1ph(
    puUnits=true,
    V_nom={15000,1400},
    S_nom=5e6,
    f_nom=50/3,
    tap_neutral={3,3},
    dv_tap={0.025,0.025},
    r={0.03,0.03},
    x={0.05,0.05},
    redc=500,
    xm=500,
    psi_sat(unit="1")=1.5,
    xm_sat=1);

end TrafoSat1ph;

record TrafoIdeal "Ideal trafo, 3-phase, example"
  extends PowerSystems.AC3ph.Transformers.Parameters.TrafoIdeal(
    puUnits=true,
    V_nom={20,400}*1e3,
    S_nom=1000e6,
    f_nom=50,
    tap_neutral={3,3},
    dv_tap={0.025,0.025},
    r_n={1,1});
end TrafoIdeal;

record TrafoStray "Trafo with ideal magnetic coupling, 3-phase, example"
  extends PowerSystems.AC3ph.Transformers.Parameters.TrafoStray(
    puUnits=true,
    V_nom={20,400}*1e3,
    S_nom=1000e6,
    f_nom=50,
    tap_neutral={3,3},
    dv_tap={0.025,0.025},
    r_n={1,1},
    r={0.005,0.005},
    x={0.05,0.05},
    x0={x[1],x[2]});
end TrafoStray;

record TrafoMag "Trafo with magnetic coupling, 3-phase, example"
  extends PowerSystems.AC3ph.Transformers.Parameters.TrafoMag(
    puUnits=true,
    V_nom={20,400}*1e3,
    S_nom=1000e6,
    f_nom=50,
    tap_neutral={3,3},
    dv_tap={0.025,0.025},
    r_n={1,1},
    r={0.005,0.005},
    x={0.05,0.05},
    x0={x[1],x[2]},
    redc=500,
    xm=500,
    xm0=50);
end TrafoMag;

record TrafoSat "Trafo with saturation, 3-phase, example"
  extends PowerSystems.AC3ph.Transformers.Parameters.TrafoSat(
    puUnits=true,
    V_nom={20,400}*1e3,
    S_nom=1000e6,
    f_nom=50,
    tap_neutral={3,3},
    dv_tap={0.025,0.025},
    r_n={1,1},
    r={0.005,0.005},
    x={0.05,0.05},
    x0={x[1],x[2]},
    redc=500,
    xm=500,
    xm0=50,
    psi_sat(unit="1")=1.5,
    xm_sat=1);
end TrafoSat;
end Transformers;


Turbine example data

within PowerSystems.Examples.Data;
package Turbines "Turbine example data"
  extends Modelica.Icons.MaterialPropertiesPackage;

  record SteamTurboGroup1200MW "Steam turbo-group, example"
    extends PowerSystems.Mechanics.TurboGroups.Parameters.SteamTurboGroup(
     w_nom(displayUnit="rpm")=314.15926535898,
     P_nom={480e6,240e6,240e6,240e6},
     J_turb={20000,200000,200000,200000},
     J_gen=62000,
     J_aux={460,830},
     stiff={260,355,750,750,750,220}*1e6);
  end SteamTurboGroup1200MW;

  record GasTurbineGear "Small GT with gear, example"
    extends PowerSystems.Mechanics.TurboGroups.Parameters.GasTurbineGear(
      w_nom=1576.7653528367,
      P_nom={12, -2}*1e6,
      J_turb=40,
      J_comp=50,
      J_gear1={0.6,12},
      J_gear2={5,200},
      J_acc=6,
      J_cpl=40,
      J_gen=2500,
      ratio={15057,5067,1500},
      stiff_sh={3,5.5,100,2500,250,200}*1e6,
      stiff_cpl=130*1e6);
  end GasTurbineGear;

  record HydroTurbine "Hydro turbine, example"
    extends PowerSystems.Mechanics.TurboGroups.Parameters.HydroTurbine(
      w_nom=314.15926535898,
      P_nom=20e6,
      J_turb=1000,
      J_shaft=5,
      J_gen=500,
      stiff=300e6);
  end HydroTurbine;

  record Diesel "Diesel, example"
    extends PowerSystems.Mechanics.TurboGroups.Parameters.Diesel(
      w_nom=157.07963267949,
      P_nom=100e3,
      J_turb=20,
      J_gen=20,
      stiff=1e6);
  end Diesel;

  record WindTurbineGear "Wind turbine with gear, example"
    extends PowerSystems.Mechanics.TurboGroups.Parameters.WindTurbineGear(
      w_nom=1.0471975511966,
      P_nom=30e3,
      J_turb=10,
      J_gear={0.3,0.1,0.03},
      J_gen=0.5,
      ratio={1,6,42},
      stiff_sh={16,1}*1e4);
  end WindTurbineGear;
end Turbines;


Text book example and variants of it

within PowerSystems.Examples.Generic;
package Network "Text book example and variants of it"
  extends Modelica.Icons.ExamplesPackage;

  model NetworkLoop
    "Simple textbook example for a steady-state power flow calculation"
    extends Modelica.Icons.Example;

    replaceable package PhaseSystem =
        PhaseSystems.ThreePhase_dq "Default phase system"

    PowerSystems.Generic.FixedVoltageSource
      fixedVoltageSource1(V=10e3, redeclare package PhaseSystem=PhaseSystem)
    PowerSystems.Generic.Impedance impedance1(R=2, L=0,
    redeclare package PhaseSystem = PhaseSystem)
    PowerSystems.Generic.Impedance impedance2(R=4, L=0,
    redeclare package PhaseSystem = PhaseSystem)
    PowerSystems.Generic.Impedance impedance3(R=2, L=0,
    redeclare package PhaseSystem = PhaseSystem)
    PowerSystems.Generic.Impedance impedance4(L=0, R=1,
    redeclare package PhaseSystem = PhaseSystem)
    PowerSystems.Generic.Impedance impedance5(L=0, R=3,
    redeclare package PhaseSystem = PhaseSystem)
    PowerSystems.Generic.FixedCurrent fixedCurrent3(I=50,
      redeclare package PhaseSystem = PhaseSystem)
    PowerSystems.Generic.FixedCurrent fixedCurrent1(I=55,
      redeclare package PhaseSystem = PhaseSystem)
    PowerSystems.Generic.FixedCurrent fixedCurrent2(I=45,
      redeclare package PhaseSystem = PhaseSystem)
    PowerSystems.Generic.FixedCurrent fixedCurrent4(I=60,
      redeclare package PhaseSystem = PhaseSystem)
    PowerSystems.Generic.Transformer transformer1(ratio=10/10.4156,
      redeclare package PhaseSystem = PhaseSystem)
    PowerSystems.Generic.Transformer transformer2(ratio=10/10, redeclare
        package PhaseSystem=PhaseSystem)
    inner System system
  equation
    connect(impedance1.terminal_n, impedance2.terminal_p)
    connect(impedance2.terminal_n, impedance3.terminal_p)
    connect(impedance4.terminal_p, impedance5.terminal_n)
    connect(fixedCurrent1.terminal, impedance1.terminal_n)
    connect(fixedCurrent2.terminal, impedance3.terminal_p)
    connect(fixedCurrent4.terminal, impedance5.terminal_n)
    connect(fixedVoltageSource1.terminal, transformer1.terminal_p)
    connect(transformer1.terminal_n, impedance1.terminal_p)
    connect(transformer2.terminal_n, impedance5.terminal_p)
    connect(transformer2.terminal_p, fixedVoltageSource1.terminal)
    connect(impedance3.terminal_n, fixedCurrent3.terminal)
    connect(impedance4.terminal_n, impedance3.terminal_n)
  end NetworkLoop;

  model NetworkLoop_d "NetworkLoop example with phase system ThreePhase_d"
    extends NetworkLoop(
      redeclare replaceable package PhaseSystem =
        PowerSystems.PhaseSystems.ThreePhase_d);
  end NetworkLoop_d;

  model NetworkLoop_dq "NetworkLoop example with phase system ThreePhase_dq"
    extends NetworkLoop(
      redeclare replaceable package PhaseSystem =
        PowerSystems.PhaseSystems.ThreePhase_dq);
  end NetworkLoop_dq;

  model NetworkLoop_dq0 "NetworkLoop example with phase system ThreePhase_dq0"
    extends NetworkLoop(
      redeclare replaceable package PhaseSystem =
        PowerSystems.PhaseSystems.ThreePhase_dq0);
  end NetworkLoop_dq0;

  model NetworkOpened
    "Steady-state power flow calculation with two voltage sources"
    extends Modelica.Icons.Example;

    PowerSystems.Generic.FixedVoltageSource fixedVoltageSource1(V=10e3)
    PowerSystems.Generic.FixedVoltageSource fixedVoltageSource2(V=10e3)
    PowerSystems.Generic.Impedance impedance1(R=2, L=0)
    PowerSystems.Generic.Impedance impedance2(L=0, R=4)
    PowerSystems.Generic.Impedance impedance3(R=2, L=0)
    PowerSystems.Generic.Impedance impedance4(L=0, R=1)
    PowerSystems.Generic.Impedance impedance5(L=0, R=3)
    PowerSystems.Generic.FixedCurrent
                         fixedCurrent3(I=50)
    PowerSystems.Generic.FixedCurrent
                         fixedCurrent1(I=55)
    PowerSystems.Generic.FixedCurrent
                         fixedCurrent2(I=45)
    PowerSystems.Generic.FixedCurrent
                         fixedCurrent4(I=60)
    PowerSystems.Generic.Transformer transformer1(ratio=10/10.4156)
    PowerSystems.Generic.Transformer transformer2(ratio=10/10)
    inner System system
  equation
    connect(impedance1.terminal_n, impedance2.terminal_p)
    connect(impedance2.terminal_n, impedance3.terminal_p)
    connect(impedance3.terminal_n, impedance4.terminal_n)
    connect(impedance4.terminal_p, impedance5.terminal_n)
    connect(impedance3.terminal_n, fixedCurrent3.terminal)
    connect(fixedCurrent1.terminal, impedance1.terminal_n)
    connect(fixedCurrent2.terminal, impedance3.terminal_p)
    connect(fixedCurrent4.terminal, impedance5.terminal_n)
    connect(fixedVoltageSource1.terminal, transformer1.terminal_p)
    connect(transformer1.terminal_n, impedance1.terminal_p)
    connect(fixedVoltageSource2.terminal, transformer2.terminal_p)
    connect(transformer2.terminal_n, impedance5.terminal_p)
  end NetworkOpened;

  model NetworkControlled "Dynamic power flow calculation with two generators"
    extends Modelica.Icons.Example;

    PowerSystems.Generic.Impedance impedance1(R=2, L=0)
    PowerSystems.Generic.Impedance impedance2(L=0, R=4)
    PowerSystems.Generic.Impedance impedance3(R=2, L=0)
    PowerSystems.Generic.Impedance impedance4(L=0, R=1)
    PowerSystems.Generic.Impedance impedance5(L=0, R=3)
    PowerSystems.Generic.FixedCurrent
                         fixedCurrent3(I=50)
    PowerSystems.Generic.FixedCurrent
                         fixedCurrent1(I=55)
    PowerSystems.Generic.FixedCurrent
                         fixedCurrent2(I=45)
    PowerSystems.Generic.FixedCurrent
                         fixedCurrent4(I=60)
    PowerSystems.Generic.Transformer transformer1(ratio=10/10.4156)
    PowerSystems.Generic.Transformer transformer2(ratio=10/10)
    PowerSystems.Generic.Generator generator1
    Modelica.Mechanics.Rotational.Components.Inertia inertia1(
      J=1e3,
      w(start=system.w_nom/generator1.pp),
      a(start=0))
    PowerSystems.Generic.Generator generator2(pp=8)
    Modelica.Mechanics.Rotational.Components.Inertia inertia2(
                                                   J=1e3,
      w(start=system.w_nom/generator2.pp),
      a(start=0))
    Modelica.Mechanics.Rotational.Sources.Torque turbine1(useSupport=false)
    Modelica.Mechanics.Rotational.Sensors.SpeedSensor angularVelocity
    Modelica.Mechanics.Rotational.Sources.Torque turbine2(useSupport=false)
    Modelica.Blocks.Sources.Trapezoid disturbance(
      width=30,
      rising=0,
      falling=0,
      period=60,
      amplitude=2e3,
      offset=2e3)
    Modelica.Blocks.Sources.Constant const(k=system.f_nom)
    Modelica.Blocks.Continuous.LimPID frequencyPowerControl(
      controllerType=Modelica.Blocks.Types.SimpleController.PI,
      Td=0,
      yMax=1e5,
      k=1e6/50,
      Ti=10)
    Modelica.Blocks.Math.Gain frequency(k=1/(2*pi))
    inner System system(fType=PowerSystems.Types.SystemFrequency.Average)
    Interfaces.Sender sender1(H=0.5*inertia1.J*inertia1.w^2/1e6, w=generator1.w)
    Interfaces.Sender sender2(H=0.5*inertia2.J*inertia2.w^2/1e6, w=generator2.w)
  initial equation
    if system.dynType == Types.Dynamics.SteadyInitial then
      inertia1.a = 0;
    else
      inertia1.w = system.omega/generator1.pp;
    end if;
    inertia1.phi = system.theta/generator1.pp;

  equation
    connect(impedance1.terminal_n, impedance2.terminal_p)
    connect(impedance2.terminal_n, impedance3.terminal_p)
    connect(impedance3.terminal_n, impedance4.terminal_n)
    connect(impedance4.terminal_p, impedance5.terminal_n)
    connect(impedance3.terminal_n, fixedCurrent3.terminal)
    connect(fixedCurrent1.terminal, impedance1.terminal_n)
    connect(fixedCurrent2.terminal, impedance3.terminal_p)
    connect(fixedCurrent4.terminal, impedance5.terminal_n)
    connect(transformer1.terminal_n, impedance1.terminal_p)
    connect(transformer2.terminal_n, impedance5.terminal_p)
    connect(inertia1.flange_b, generator1.flange)
    connect(generator1.terminal, transformer1.terminal_p)
    connect(inertia2.flange_b, generator2.flange)
    connect(generator2.terminal, transformer2.terminal_p)
    connect(turbine1.flange, inertia1.flange_a)
    connect(turbine2.flange, inertia2.flange_a)
    connect(disturbance.y, turbine2.tau)
    connect(angularVelocity.flange, inertia1.flange_b)
    connect(frequencyPowerControl.y, turbine1.tau)
    connect(const.y, frequencyPowerControl.u_s)
    connect(angularVelocity.w, frequency.u)
    connect(frequency.y, frequencyPowerControl.u_m)
    connect(system.receiveFreq, sender1.sendFreq)
    connect(system.receiveFreq, sender2.sendFreq)
  end NetworkControlled;
end Network;


Examples for Generic components

within PowerSystems.Examples;
package Generic "Examples for Generic components"
  extends Modelica.Icons.ExamplesPackage;
end Generic;


Small examples with Generic components

within PowerSystems.Examples.Generic;
package Test "Small examples with Generic components"
  extends Modelica.Icons.ExamplesPackage;

  model ImpedanceTest
    extends Modelica.Icons.Example;

    PowerSystems.Generic.FixedVoltageSource source
    PowerSystems.Generic.Impedance
      load(R=30, L=10/314)
    PowerSystems.Generic.Ground ground
    inner PowerSystems.System system
  equation
    connect(source.terminal, load.terminal_p)
    connect(load.terminal_n, ground.terminal)
  end ImpedanceTest;

  model AdmittanceTest
    extends Modelica.Icons.Example;

    PowerSystems.Generic.FixedVoltageSource source
    PowerSystems.Generic.Admittance
      load(G=30/(30*30 + 10*10), C=10/(30*30 + 10*10)/314)
    PowerSystems.Generic.Ground ground
    inner PowerSystems.System system
  equation
    connect(source.terminal, load.terminal_p)
    connect(load.terminal_n, ground.terminal)
  end AdmittanceTest;

  model InductiveLoadTest
    extends Modelica.Icons.Example;

    PowerSystems.Generic.FixedVoltageSource source
    PowerSystems.Generic.Impedance
      line(R=1.2, L=1.6/314)
    PowerSystems.Generic.Impedance
      load(R=30, L=10/314)
    PowerSystems.Generic.Ground ground
    inner PowerSystems.System system
  equation
    connect(source.terminal, line.terminal_p)
    connect(load.terminal_n, ground.terminal)
    connect(line.terminal_n, load.terminal_p)
  end InductiveLoadTest;

  model FixedCurrentTest
    extends Modelica.Icons.Example;

    PowerSystems.Generic.FixedVoltageSource source
    PowerSystems.Generic.Impedance
      line(R=1.2, L=1.6/314)
    PowerSystems.Generic.FixedCurrent load(I=173.448, phi=-0.356)
    inner PowerSystems.System system
  equation
    connect(line.terminal_n, load.terminal)
    connect(source.terminal, line.terminal_p)
  end FixedCurrentTest;

  model FixedLoadTest
    extends Modelica.Icons.Example;

    PowerSystems.Generic.FixedVoltageSource source
    PowerSystems.Generic.Impedance
      line(R=1.2, L=1.6/314)
    PowerSystems.Generic.FixedLoad load(P=2.7076e6, phi=atan(1000/3000))
    inner PowerSystems.System system
  equation
    connect(source.terminal, line.terminal_p)
    connect(line.terminal_n, load.terminal)
  end FixedLoadTest;

  model PMeterTest
    extends Modelica.Icons.Example;

    PowerSystems.Generic.FixedVoltageSource source
    PowerSystems.Generic.Sensors.PMeter pMeter
    PowerSystems.Generic.FixedLoad load(P=2.7076e6, phi=atan(1000/3000))
    inner PowerSystems.System system
  equation
  connect(source.terminal, pMeter.terminal_p)
  connect(pMeter.terminal_n, load.terminal)
  end PMeterTest;

  model GeneratorTest
    extends Modelica.Icons.Example;

    PowerSystems.Generic.Generator generator
    Modelica.Mechanics.Rotational.Components.Inertia inertia(J=1e3,
      phi(start=0, fixed=true),
      w(start=2*pi*50, fixed=true))
    Modelica.Mechanics.Rotational.Sources.ConstantTorque constantTorque(useSupport=false, tau_constant=3e6/50/2/pi)
    PowerSystems.Generic.FixedLoad fixedLoad(P=3e6,phi=0.3)
    inner PowerSystems.System system
  equation
  connect(inertia.flange_b, generator.flange)
    connect(constantTorque.flange, inertia.flange_a)
  connect(generator.terminal, fixedLoad.terminal)
  end GeneratorTest;

  model GeneratorTest2
    extends Modelica.Icons.Example;

    PowerSystems.Generic.Generator generator
    Modelica.Mechanics.Rotational.Components.Inertia inertia(J=1e3,
      phi(start=0, fixed=true),
      w(start=50*2*pi, fixed=true))
    Modelica.Mechanics.Rotational.Sources.ConstantTorque constantTorque(useSupport=false, tau_constant=3e6/50/2/2/pi)
    PowerSystems.Generic.FixedLoad fixedLoad(P=3e6,phi=0.3)
    PowerSystems.Generic.Generator generator1(synchronous=false)
    Modelica.Mechanics.Rotational.Components.Inertia inertia1(J=1e3,
    phi(start=0, fixed=true),
    w(start=50*2*pi, fixed=true))
    Modelica.Mechanics.Rotational.Sources.ConstantTorque constantTorque1(useSupport=false, tau_constant=3e6/50/2/pi)
    PowerSystems.Generic.Impedance impedance
    PowerSystems.Generic.Impedance impedance1
    inner PowerSystems.System system
  equation
    connect(inertia.flange_b, generator.flange)
    connect(constantTorque.flange, inertia.flange_a)
    connect(inertia1.flange_b, generator1.flange)
    connect(constantTorque1.flange, inertia1.flange_a)
    connect(generator.terminal, impedance.terminal_p)
    connect(impedance.terminal_n, fixedLoad.terminal)
    connect(generator1.terminal, impedance1.terminal_p)
    connect(impedance1.terminal_n, fixedLoad.terminal)
  end GeneratorTest2;

end Test;


within PowerSystems.Examples;
package PowerWorld "Demonstrate stabilization of wind power in Eurosyslib work package 5.3"
  extends Modelica.Icons.ExamplesPackage;

  model PowerWorld "Interoperation of wind power and thermal power"
    extends Modelica.Icons.Example;

    Components.WindFarm windFarm(redeclare package PhaseSystem =
      PowerSystems.PhaseSystems.DirectCurrent)
    Components.City city
    Components.LoadDispatcher dispatcher
    Components.PowerPlant powerPlant(primaryControlMax=40)
    PowerSystems.Generic.Transformer trafoPlant(ratio=10/380)
    PowerSystems.Generic.Transformer distribution(ratio=380/50)
    PowerSystems.Generic.Inverter HVDC(potentialReference=false)
    Components.HydroPlant hydroPlant(primaryControlMax=200)
    PowerSystems.Generic.Transformer trafoHydro(ratio=380/10)
    PowerSystems.Generic.Impedance linePlant(R=1, L=1/314)
    PowerSystems.Generic.Impedance lineWind(R=1, L=1/314)
    PowerSystems.Generic.Impedance lineHydro(R=1, L=1/314)
    Modelica.Blocks.Sources.RealExpression frequency(y=system.omega/2/pi)
      "Average frequency"
    inner System system(dynType=PowerSystems.Types.Dynamics.FixedInitial,
      fType=PowerSystems.Types.SystemFrequency.Average)
  equation
    connect(powerPlant.terminal, trafoPlant.terminal_p)
    connect(distribution.terminal_n, city.terminal)
    connect(windFarm.terminal, HVDC.terminal_dc)
    connect(dispatcher.plantDispatch, powerPlant.plantDispatch)
    connect(dispatcher.hydroDispatch, hydroPlant.hydroDispatch)
    connect(trafoPlant.terminal_n, linePlant.terminal_p)
    connect(linePlant.terminal_n, distribution.terminal_p)
    connect(HVDC.terminal, lineWind.terminal_p)
    connect(lineWind.terminal_n, distribution.terminal_p)
    connect(lineHydro.terminal_n, distribution.terminal_p)
    connect(frequency.y, dispatcher.frequency)
    connect(hydroPlant.terminal, trafoHydro.terminal_n)
    connect(trafoHydro.terminal_p, lineHydro.terminal_p)
  end PowerWorld;

  package Components "Aggregated components for PowerWorld example"
    extends Modelica.Icons.VariantsPackage;

    model PowerPlant "Thermal Power Plant with primary and secondary control"
      extends PowerSystems.Generic.Ports.PartialSource(
        final potentialReference=true);

      parameter Boolean Modakond = false "Install Modakond for condensate stop"
      parameter Real primaryControlMax(unit="MW") = 40
      "Maximum power for primary frequency control"

      Real P_generator(unit="MW") = -generator.p[1]/1e6;
      Real P_control(unit="MW") = -(P_generator - plantDispatch[1]);

    PowerSystems.Generic.Generator generator(redeclare package PhaseSystem =
          PhaseSystem, definiteReference=definiteReference)
      Modelica.Mechanics.Rotational.Components.Inertia rotor(
                       J=10e6,
      a(start=0),
        w(fixed=false, start=system.w_nom/generator.pp))
      Modelica.Mechanics.Rotational.Sources.Torque turbine
      Modelica.Mechanics.Rotational.Sensors.SpeedSensor angularVelocity
      Modelica.Blocks.Sources.Constant reference(k=system.f_nom)
      Modelica.Blocks.Continuous.LimPID primaryControl(
                                               k=0.05*800/0.2,
        controllerType=Modelica.Blocks.Types.SimpleController.P,
        yMax=primaryControlMax) "UCTE: df of 200 mHz corresponds to 5% of load"
      Modelica.Blocks.Interfaces.RealInput[3] plantDispatch(each unit="MW")
      Modelica.Blocks.Math.Add loadControl(k1=1e6/50/2/pi, k2=1e6/50/2/pi)
      Modelica.Blocks.Continuous.FirstOrder evaporator(T=60, y_start=490,
      initType=Modelica.Blocks.Types.Init.InitialState)
      Modelica.Blocks.Continuous.FirstOrder superheater1(T=60, y_start=490,
      initType=Modelica.Blocks.Types.Init.InitialState)
      Modelica.Blocks.Continuous.FirstOrder superheater2(T=60, y_start=490,
      initType=Modelica.Blocks.Types.Init.InitialState)
      Modelica.Blocks.Math.Add fuel
      Modelica.Blocks.Continuous.TransferFunction transferFunction(b={180,1}, a=
           {60,1},
      initType=Modelica.Blocks.Types.Init.SteadyState)
      "achieve a power ramp of 2% per minute, minus 0.8% tolerance"
      Modelica.Blocks.Math.Add add
      Modelica.Blocks.Continuous.LimIntegrator hotwellLevel(
        k=1,
        outMax=10,
        outMin=0,
        y(unit="m")) "level of hotwell"
      Modelica.Blocks.Math.Max on
      Modelica.Blocks.Math.Sign condStop
      Modelica.Blocks.Math.Min off
      Modelica.Blocks.Math.Add accumulation(k2=1e-2, k1=if Modakond then 5e-4 else
                  0)
      Modelica.Blocks.Sources.Constant zero(k=0)
      Modelica.Blocks.Continuous.FirstOrder throttleDynamics(T=120, k=-300/800,
      initType=Modelica.Blocks.Types.Init.SteadyState)
      Modelica.Blocks.Math.Add pressure(k1=300/800)
      Modelica.Blocks.Math.Min throttling
      Modelica.Blocks.Sources.Constant throttleMin(k=if Modakond then 0 else
            Modelica.Constants.inf)
      Modelica.Blocks.Sources.Constant throttleReserve(k=if Modakond then 0 else
                  5)
      Modelica.Blocks.Math.Add pressureLoss
      Modelica.Blocks.Continuous.Integrator throttleCosts(k=1/365)
      Modelica.Blocks.Math.Gain frequency(k=1/(2*pi))
  protected
      outer System system;

  public
      Interfaces.Sender sender(w=generator.w, H=0.5*rotor.J*rotor.w^2/800e6)
    initial equation
      if Connections.isRoot(generator.terminal.theta) then
        if system.dynType == Types.Dynamics.SteadyInitial then
          rotor.a = 0;
        else
          rotor.w = system.omega/generator.pp;
        end if;
        rotor.phi = system.theta/generator.pp;
      end if;
    equation
      connect(sender.sendFreq, system.receiveFreq);
      connect(rotor.flange_b, angularVelocity.flange)
      connect(primaryControl.y, loadControl.u1)
      connect(evaporator.y, superheater1.u)
      connect(superheater1.y, superheater2.u)
      connect(superheater2.y, loadControl.u2)
      connect(loadControl.y, turbine.tau)
      connect(turbine.flange, rotor.flange_a)
      connect(rotor.flange_b, generator.flange)
      connect(fuel.y, evaporator.u)
      connect(transferFunction.y, fuel.u2)
      connect(add.y, transferFunction.u)
      connect(loadControl.u1, add.u2)
      connect(accumulation.y, hotwellLevel.u)
      connect(loadControl.u1, condStop.u)
      connect(condStop.y, on.u1)
      connect(condStop.y, off.u2)
      connect(zero.y, on.u2)
      connect(zero.y, off.u1)
      connect(on.y, accumulation.u1)
      connect(off.y, accumulation.u2)
      connect(primaryControl.y, throttling.u2)
      connect(throttleMin.y, throttling.u1)
      connect(throttling.y, throttleDynamics.u)
      connect(superheater2.y, pressure.u1)
      connect(throttleDynamics.y, pressureLoss.u2)
      connect(throttleReserve.y, pressureLoss.u1)
      connect(pressureLoss.y, pressure.u2)
      connect(pressureLoss.y, throttleCosts.u)

      connect(plantDispatch[2], add.u1)
      connect(plantDispatch[1], fuel.u1)
      connect(reference.y, primaryControl.u_s)
      connect(angularVelocity.w, frequency.u)
      connect(frequency.y, primaryControl.u_m)
    connect(generator.terminal, terminal)
    end PowerPlant;

    model HydroPlant
      extends PowerSystems.Generic.Ports.PartialSource(
        final potentialReference=true);

      parameter Real primaryControlMax(unit="MW") = 200
      "Maximum power for primary frequency control"

      Real P_generator(unit="MW") = -generator.p[1]/1e6;
      Real P_control(unit="MW") = -(P_generator - hydroDispatch[1]);

      Modelica.Blocks.Interfaces.RealInput[3] hydroDispatch(each unit="MW")
      Modelica.Blocks.Continuous.Integrator reservoirLevel(y_start=10, k=-1/5e4,
        y(unit="m"))
      Modelica.Mechanics.Rotational.Sources.Torque reservoirTurbine
      Modelica.Mechanics.Rotational.Components.Inertia rotor(J=1e6,
      a(start=0),
        w(fixed=false, start=system.w_nom/generator.pp))
    PowerSystems.Generic.Generator generator(redeclare package PhaseSystem =
          PhaseSystem, definiteReference=definiteReference)
      Modelica.Mechanics.Rotational.Sensors.SpeedSensor angularVelocity
      Modelica.Blocks.Sources.Constant reference(k=system.f_nom)
      Modelica.Blocks.Continuous.LimPID primaryControl(
        k=50/0.2,
        controllerType=Modelica.Blocks.Types.SimpleController.P,
        yMax=primaryControlMax) "200 mHz corresponds to 50 MW"
      Modelica.Blocks.Math.Gain powerControl(k=1e6/50/2/pi)
      Modelica.Blocks.Continuous.TransferFunction controlDynamics(a={1,1}, b={1},
        initType=Modelica.Blocks.Types.Init.InitialState)
      Modelica.Blocks.Math.Add add
      Modelica.Blocks.Math.Gain riverControl(k=1e6/50/2/pi)
      Modelica.Mechanics.Rotational.Sources.Torque riverTurbine
      Modelica.Mechanics.Rotational.Components.Inertia rotorRiver(
                       J=0.5e6,
      a(start=0),
        w(fixed=false, start=system.w_nom/generator.pp))
      Modelica.Blocks.Math.Gain frequency(k=1/(2*pi))
  protected
      outer System system;

  public
      Interfaces.Sender sender(w=generator.w, H=0.5*rotor.J*rotor.w^2/200e6)
    initial equation
      if Connections.isRoot(generator.terminal.theta) then
        if system.dynType == Types.Dynamics.SteadyInitial then
          rotor.a = 0;
        else
          rotor.w = system.omega/generator.pp;
        end if;
        rotor.phi = system.theta/generator.pp;
      end if;
    equation
      connect(sender.sendFreq, system.receiveFreq);
      connect(rotor.flange_b, angularVelocity.flange)

      connect(reservoirTurbine.flange, rotor.flange_a)
      connect(rotor.flange_b, generator.flange)
      connect(powerControl.y, reservoirTurbine.tau)
      connect(controlDynamics.y, reservoirLevel.u)
      connect(controlDynamics.y, powerControl.u)
      connect(add.y, controlDynamics.u)
      connect(hydroDispatch[2], add.u2)

      connect(hydroDispatch[1], riverControl.u)
      connect(riverControl.y, riverTurbine.tau)
      connect(reference.y, primaryControl.u_s)
      connect(primaryControl.y, add.u1)
      connect(riverTurbine.flange, rotorRiver.flange_a)
      connect(rotorRiver.flange_b, generator.flange)
      connect(angularVelocity.w, frequency.u)
      connect(frequency.y, primaryControl.u_m)
    connect(generator.terminal, terminal)
    end HydroPlant;

    model WindFarm
      extends PowerSystems.Generic.Ports.PartialSource(
        final potentialReference=false, final definiteReference=false);
      parameter Boolean cut_out = false
      "stop producing energy as wind exceeds cut-out speed of 20-25 m/s"

      Modelica.Blocks.Sources.CombiTimeTable wind(
        tableName="tab",
        fileName=PowerWorldResources + "LoadData.txt",
        tableOnFile=true,
        table=fill(
                0.0,
                0,
                10))
      PowerSystems.Generic.PrescribedPowerSource mills(
        redeclare package PhaseSystem = PhaseSystem,
        definiteReference = definiteReference)
      Modelica.Blocks.Sources.Trapezoid disturbance(
        rising=120,
        period=86400,
        offset=1,
        width=1800,
        falling=1800,
        startTime=20040,
        amplitude=if cut_out then -1 else 0)
      Modelica.Blocks.Math.Product product
    Modelica.Blocks.Math.Gain MW2W(k=1e6)
    equation

      connect(wind.y[4], product.u2)

      connect(disturbance.y, product.u1)
    connect(mills.terminal, terminal)
    connect(product.y, MW2W.u)
    connect(MW2W.y, mills.P)
    end WindFarm;

    model LoadDispatcher

      Modelica.Blocks.Sources.CombiTimeTable data(
        tableName="tab",
        fileName=PowerWorldResources + "LoadData.txt",
        tableOnFile=true,
        table=fill(
            0.0,
            0,
            10))
      Modelica.Blocks.Interfaces.RealOutput[3] plantDispatch(each unit="MW")
      Modelica.Blocks.Interfaces.RealOutput loadForcast(unit="MW")
      Modelica.Blocks.Interfaces.RealOutput windForcast(unit="MW")
      Modelica.Blocks.Sources.Constant hydroBase(k=25)
      Modelica.Blocks.Sources.Constant primaryControl(k=40)
      Modelica.Blocks.Interfaces.RealOutput[3] hydroDispatch(each unit="MW")
      Modelica.Blocks.Sources.Constant controlHydro(k=200)
      Modelica.Blocks.Interfaces.RealInput frequency(unit="Hz")
      Modelica.Blocks.Continuous.LimPID secondaryControl(   k=60/0.4,
        controllerType=Modelica.Blocks.Types.SimpleController.PI,
        Ti=600,
        yMax=60) "400 mHz corresponds to 60 MW"
      Modelica.Blocks.Sources.Constant reference(k=system.f_nom)
      Modelica.Blocks.Math.Add3 plantSchedule(k2=-1, k3=-1,
        y(unit="MW"))
      Modelica.Blocks.Sources.Trapezoid hydroDaily(
        rising=86400/4,
        width=86400/4,
        falling=86400/4,
        period=86400,
        startTime=86400/8,
        amplitude=50,
        offset=-25,
        y(unit="MW"))
      Modelica.Blocks.Math.Add add
      Modelica.Blocks.Math.Gain distributionLoss(k=1)
  protected
      outer System system;
    equation
      connect(primaryControl.y, plantDispatch[3])
      connect(hydroBase.y, hydroDispatch[1])
      connect(controlHydro.y, hydroDispatch[3])
      connect(data.y[3], windForcast)
      connect(secondaryControl.y, plantDispatch[2])
      connect(windForcast, plantSchedule.u2)
      connect(plantSchedule.y, plantDispatch[1])
      connect(hydroDaily.y, hydroDispatch[2])
      connect(hydroDaily.y, add.u1)
      connect(hydroBase.y, add.u2)
      connect(add.y, plantSchedule.u3)
      connect(frequency, secondaryControl.u_m)
      connect(reference.y, secondaryControl.u_s)
      connect(data.y[1], loadForcast)
      connect(loadForcast, distributionLoss.u)
      connect(distributionLoss.y, plantSchedule.u1)
    end LoadDispatcher;

    model City
      extends PowerSystems.Generic.Ports.PartialLoad;
      Modelica.Blocks.Sources.CombiTimeTable data(
        tableName="tab",
        fileName=PowerWorldResources + "LoadData.txt",
        tableOnFile=true,
        table=fill(
                0.0,
                0,
                10))
      PowerSystems.Generic.PrescribedPowerLoad load(phi=0.34906585039887)
    Modelica.Blocks.Math.Gain MW2W(k=1e6)
    equation
    connect(terminal, load.terminal)
    connect(data.y[2], MW2W.u)
    connect(MW2W.y, load.P)
    end City;

  end Components;

  package Test "Component tests for PowerWorld example"
    extends Modelica.Icons.ExamplesPackage;

    model PowerPlantTest1 "Test primary control"
      extends Modelica.Icons.Example;
      Components.PowerPlant powerPlant
      Modelica.Blocks.Sources.Constant schedule(k=450)
      PowerSystems.Generic.PrescribedPowerLoad prescribedLoad(phi=
                                                              0.34906585039887)
      Modelica.Blocks.Sources.Step step(startTime=10,
                                        height=-40,
                                        offset=490)
      Modelica.Blocks.Sources.Constant secondary(k=40)
      Modelica.Blocks.Sources.Constant primary(k=40)
    Modelica.Blocks.Math.Gain MW2W(k=1e6)
    inner PowerSystems.System system
    equation
      connect(powerPlant.terminal, prescribedLoad.terminal)
      connect(secondary.y, powerPlant.plantDispatch[2])
      connect(schedule.y, powerPlant.plantDispatch[1])
      connect(primary.y, powerPlant.plantDispatch[3])
    connect(step.y, MW2W.u)
    connect(MW2W.y, prescribedLoad.P)
    end PowerPlantTest1;

    model PowerPlantTest2 "Test secondary control"
      extends Modelica.Icons.Example;
      Components.PowerPlant powerPlant
      Modelica.Blocks.Sources.Constant schedule(k=490)
      PowerSystems.Generic.PrescribedPowerLoad prescribedLoad(phi=
                                                              0.34906585039887)
      Modelica.Blocks.Sources.Ramp ramp(offset=490,
                                        height=48,
                                        duration=180,
                                        startTime=100)
      Modelica.Blocks.Sources.Step secondary(height=48, startTime=100)
      Modelica.Blocks.Sources.Constant primary(k=40)
    Modelica.Blocks.Math.Gain MW2W(k=1e6)
    inner PowerSystems.System system
    equation
      connect(powerPlant.terminal, prescribedLoad.terminal)
      connect(schedule.y, powerPlant.plantDispatch[1])
      connect(secondary.y, powerPlant.plantDispatch[2])
      connect(primary.y, powerPlant.plantDispatch[3])
    connect(ramp.y, MW2W.u)
    connect(MW2W.y, prescribedLoad.P)
    end PowerPlantTest2;

    model PowerPlantTest3 "Test connection to a large net"
      extends Modelica.Icons.Example;
      Components.PowerPlant powerPlant
      Modelica.Blocks.Sources.Constant schedule(k=490)
      PowerSystems.Generic.FixedVoltageSource largeGrid
      Modelica.Blocks.Sources.Ramp secondary(startTime=100,
                                             height=300,
                                             duration=0)
      PowerSystems.Generic.Impedance
        line(R=1, L=1/314)
      Modelica.Blocks.Sources.Constant primary(k=40)
    inner PowerSystems.System system
    equation
      connect(schedule.y, powerPlant.plantDispatch[1])
      connect(secondary.y, powerPlant.plantDispatch[2])
      connect(powerPlant.terminal, line.terminal_p)
    connect(line.terminal_n, largeGrid.terminal)
      connect(primary.y, powerPlant.plantDispatch[3])
    end PowerPlantTest3;

    model HydroPlantTest1 "Test primary control"
      extends Modelica.Icons.Example;
      Components.HydroPlant hydroPlant(primaryControlMax=310)
      Modelica.Blocks.Sources.Constant schedule(k=50)
      PowerSystems.Generic.PrescribedPowerLoad prescribedLoad
      Modelica.Blocks.Sources.Trapezoid trapezoid(startTime=100,
                                                  offset=50,
                                                  amplitude=300,
                                                  width=300,
                                                  falling=300,
                                                  nperiod=1,
                                                  rising=60,
                                                  period=900)
      Modelica.Blocks.Sources.Constant primary(k=400)
      Modelica.Blocks.Sources.Constant secondary(k=0)
    Modelica.Blocks.Math.Gain MW2W(k=1e6)
    inner PowerSystems.System system
    equation
      connect(schedule.y, hydroPlant.hydroDispatch[1])
      connect(hydroPlant.terminal, prescribedLoad.terminal)
      connect(primary.y, hydroPlant.hydroDispatch[3])
      connect(secondary.y, hydroPlant.hydroDispatch[2])
    connect(trapezoid.y, MW2W.u)
    connect(MW2W.y, prescribedLoad.P)
    end HydroPlantTest1;

    model HydroPlantTest2 "Test secondary control"
      extends Modelica.Icons.Example;
      Components.HydroPlant hydroPlant
      Modelica.Blocks.Sources.Constant schedule(k=50)
      Modelica.Blocks.Sources.Constant primary(k=400)
      Modelica.Blocks.Sources.Trapezoid secondary(amplitude=100,
                                                  offset=-50,
                                                  rising=86400/4,
                                                  width=86400/4,
                                                  falling=86400/4,
                                                  period=86400,
                                                  startTime=86400/8)
      PowerSystems.Generic.FixedVoltageSource largeGrid
      PowerSystems.Generic.Impedance
        line(R=1, L=1/314)
    inner PowerSystems.System system
    equation
      connect(schedule.y, hydroPlant.hydroDispatch[1])
      connect(primary.y, hydroPlant.hydroDispatch[3])
      connect(secondary.y, hydroPlant.hydroDispatch[2])
      connect(line.terminal_n, largeGrid.terminal)
      connect(hydroPlant.terminal, line.terminal_p)
    end HydroPlantTest2;

    model WindFarmGridTest "WindFarm connected to a large net"
      extends Modelica.Icons.Example;
      Components.WindFarm windFarm
      PowerSystems.Generic.FixedVoltageSource largeGrid
    inner PowerSystems.System system
    equation
    connect(windFarm.terminal, largeGrid.terminal)
    end WindFarmGridTest;

    model WindFarmHVDCTest "WindFarm connected to a large net via HVDC"
      extends Modelica.Icons.Example;
      Components.WindFarm windFarm(redeclare package PhaseSystem =
            PowerSystems.PhaseSystems.DirectCurrent)

      PowerSystems.Generic.FixedVoltageSource largeGrid
      PowerSystems.Generic.Inverter
        inverter
      inner PowerSystems.System system
    equation
      connect(windFarm.terminal, inverter.terminal_dc)
    connect(inverter.terminal, largeGrid.terminal)
    end WindFarmHVDCTest;

    model CityTest
      extends Modelica.Icons.Example;
      PowerSystems.Generic.FixedVoltageSource largeGrid
      Components.City city
      inner PowerSystems.System system
    equation
    connect(largeGrid.terminal, city.terminal)
    end CityTest;

    model LoadDispatcherTest
      extends Modelica.Icons.Example;
      Components.LoadDispatcher loadDispatcher
      Modelica.Blocks.Sources.Trapezoid frequency(amplitude=2,
                                                  rising=15,
                                                  falling=15,
                                                  period=60,
                                                  offset=49,
                                                  width=15)
      inner System system
    equation
      connect(frequency.y, loadDispatcher.frequency)
    end LoadDispatcherTest;
  end Test;

  constant String PowerWorldResources = Modelica.Utilities.Files.loadResource("modelica://PowerSystems.Examples.PowerWorld/Resources/");
end PowerWorld;