DPsim
Loading...
Searching...
No Matches
EMT_Ph3_SynchronGeneratorTrStab.cpp
Go to the documentation of this file.
1/* Copyright 2017-2021 Institute for Automation of Complex Power Systems,
2 * EONERC, RWTH Aachen University
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
7 *********************************************************************************/
8
10using namespace CPS;
11
13 Real theta, const Matrix &fabc) {
14 // Calculates fdq = Tdq * fabc
15 // Assumes that d-axis starts aligned with phase a
17 Matrix dqvector = Tdq * fabc;
18 return dqvector;
19}
20
22 Real theta) {
23 // Return park matrix for theta
24 // Assumes that d-axis starts aligned with phase a
25 Matrix Tdq = Matrix::Zero(2, 3);
26 Real k = sqrt(2. / 3.);
27 Tdq << k * cos(theta), k * cos(theta - 2. * M_PI / 3.),
28 k * cos(theta + 2. * M_PI / 3.), -k * sin(theta),
29 -k * sin(theta - 2. * M_PI / 3.), -k * sin(theta + 2. * M_PI / 3.);
30 return Tdq;
31}
32
36 CompositePowerComp<Real>(uid, name, true, true, logLevel),
37 mStatorVoltagePrevious(mAttributes->create<Matrix>("v_stator_prev")),
38 mStatorSourceVoltage(mAttributes->create<Matrix>("v_stator_source")),
39 mStatorHistoryCurrent(mAttributes->create<Matrix>("i_stator_history")),
40 mEp(mAttributes->create<Complex>("Ep")),
41 mEp_abs(mAttributes->create<Real>("Ep_mag")),
42 mEp_phase(mAttributes->create<Real>("Ep_phase")),
43 mDelta_p(mAttributes->create<Real>("delta_r")),
44 mRefOmega(mAttributes->createDynamic<Real>("w_ref")),
45 mRefDelta(mAttributes->createDynamic<Real>("delta_ref")) {
48 **mIntfVoltage = Matrix::Zero(3, 1);
49 **mIntfCurrent = Matrix::Zero(3, 1);
50 **mStatorVoltagePrevious = Matrix::Zero(3, 1);
51 **mStatorSourceVoltage = Matrix::Zero(3, 1);
52 **mStatorHistoryCurrent = Matrix::Zero(3, 1);
53
54 mStates = Matrix::Zero(10, 1);
55}
56
64
66 Real nomPower, Real nomVolt, Real nomFreq, Real Ll, Real Lmd, Real Llfd,
67 Real inertia, Real D) {
68 setBaseParameters(nomPower, nomVolt, nomFreq);
69
70 // Input is in per unit but all values are converted to absolute values.
73
74 **mLl = Ll;
75 mLmd = Lmd;
76 **mLd = **mLl + mLmd;
77 mLlfd = Llfd;
78 mLfd = mLlfd + mLmd;
79 // M = 2*H where H = inertia
80 **mInertia = inertia;
81 // X'd in absolute values
82 mXpd = mNomOmega * (**mLd - mLmd * mLmd / mLfd) * mBase_L;
83 mLpd = (**mLd - mLmd * mLmd / mLfd) * mBase_L;
84
85 SPDLOG_LOGGER_INFO(mSLog,
86 "\n--- Parameters ---"
87 "\nimpedance: {:f}"
88 "\ninductance: {:f}",
89 mXpd, mLpd);
90}
91
93 Real nomPower, Real nomVolt, Real nomFreq, Int polePairNumber, Real Rs,
94 Real Lpd, Real inertiaJ, Real Kd) {
95 setBaseParameters(nomPower, nomVolt, nomFreq);
96
99
100 // M = 2*H where H = inertia
101 // H = J * 0.5 * omegaNom^2 / polePairNumber
102 **mInertia = calcHfromJ(inertiaJ, 2 * PI * nomFreq, polePairNumber);
103 // X'd in absolute values
104 mXpd = mNomOmega * Lpd;
105 mLpd = Lpd;
106
107 SPDLOG_LOGGER_INFO(mSLog,
108 "\n--- Parameters ---"
109 "\nimpedance: {:f}"
110 "\ninductance: {:f}",
111 mXpd, mLpd);
112}
113
115 Real nomPower, Real nomVolt, Real nomFreq, Real Xpd, Real inertia, Real Rs,
116 Real D) {
117 setBaseParameters(nomPower, nomVolt, nomFreq);
118
119 // Input is in per unit but all values are converted to absolute values.
122
123 // M = 2*H where H = inertia
124 **mInertia = inertia;
125 // X'd in absolute values
126 mXpd = Xpd * mBase_Z;
127 mLpd = Xpd * mBase_L;
128
129 // mImpedance is formed in SI units below, so the pu resistance must be
130 // converted to ohms just like the transient reactance.
131 **mRs = Rs * mBase_Z;
132 //The units of D are per unit power divided by per unit speed deviation.
133 // D is transformed to an absolute value to obtain Kd, which will be used in the swing equation
134 mKd = D * mNomPower / mNomOmega;
135
136 SPDLOG_LOGGER_INFO(mSLog,
137 "\n--- Parameters ---"
138 "\nimpedance: {:f}"
139 "\ninductance: {:f}",
140 mXpd, mLpd);
141}
142
144 Real mechPower) {
145 mInitElecPower = elecPower;
146 mInitMechPower = mechPower;
147}
148
150 // This model stamps its stator R-L Norton companion directly.
151 mSubCompCreated = true;
152}
153
155 Real frequency) {
156 // Initialize omega mech with nominal system frequency
157 **mOmMech = mNomOmega;
158
160 ? -terminal(0)->singlePower()
162 if (mInitMechPower == 0 && mInitElecPower != Complex(0, 0))
164
165 // use complex interface quantities for initialization calculations
166 MatrixComp intfVoltageComplex = MatrixComp::Zero(3, 1);
167 MatrixComp intfCurrentComplex = MatrixComp::Zero(3, 1);
168 // Power-flow voltages are phase-to-phase RMS; the EMT model works with
169 // phase-to-ground peak values.
170 intfVoltageComplex(0, 0) = RMS3PH_TO_PEAK1PH * initialSingleVoltage(0);
171 intfVoltageComplex(1, 0) = intfVoltageComplex(0, 0) * SHIFT_TO_PHASE_B;
172 intfVoltageComplex(2, 0) = intfVoltageComplex(0, 0) * SHIFT_TO_PHASE_C;
173 intfCurrentComplex(0, 0) =
174 std::conj(2. / 3. * mInitElecPower / intfVoltageComplex(0, 0));
175 intfCurrentComplex(1, 0) = intfCurrentComplex(0, 0) * SHIFT_TO_PHASE_B;
176 intfCurrentComplex(2, 0) = intfCurrentComplex(0, 0) * SHIFT_TO_PHASE_C;
177
178 //save real interface quantities calculated from complex ones
179 **mIntfVoltage = intfVoltageComplex.real();
180 **mIntfCurrent = intfCurrentComplex.real();
181
183
184 // Calculate initial emf behind reactance from power flow results
185 **mEp = intfVoltageComplex(0, 0) + mImpedance * intfCurrentComplex(0, 0);
186
187 // The absolute value of Ep is constant, only delta_p changes every step
188 **mEp_abs = Math::abs(**mEp);
189 // Delta_p is the angular position of mEp with respect to the synchronously rotating reference
190 **mDelta_p = Math::phase(**mEp);
191
192 // // Update active electrical power that is compared with the mechanical power
194 (3. / 2. * intfVoltageComplex(0, 0) * std::conj(intfCurrentComplex(0, 0)))
195 .real();
196 // mElecActivePower = ( (mEp - (**mIntfVoltage)(0,0)) / mImpedance * (**mIntfVoltage)(0,0) ).real();
197 // For infinite power bus
198 // mElecActivePower = (Math::abs(mEp) * Math::abs((**mIntfVoltage)(0,0)) / mXpd) * sin(mDelta_p);
199
200 // Start in steady state so that electrical and mech. power are the same
201 // because of the initial condition mOmMech = mNomOmega the damping factor is not considered at the initialisation
203
205 for (UInt phase = 0; phase < 3; ++phase)
206 (**mStatorSourceVoltage)(phase, 0) =
207 Math::abs(emfPhasors(phase, 0)) *
208 std::cos(Math::phase(emfPhasors(phase, 0)));
210
211 SPDLOG_LOGGER_INFO(mSLog,
212 "\n--- Initialize according to powerflow ---"
213 "\nTerminal 0 voltage: {:e}<{:e}"
214 "\nVoltage behind reactance: {:e}<{:e}"
215 "\ninitial electrical power: {:e}+j{:e}"
216 "\nactive electrical power: {:e}"
217 "\nmechanical power: {:e}"
218 "\n--- End of powerflow initialization ---",
219 Math::abs((**mIntfVoltage)(0, 0)),
223}
224
226 // #### Calculations on input of time step k ####
227 // Transform interface quantities to synchronously rotating DQ reference frame
230 // Update electrical power (minus sign to calculate generated power from consumed current)
231 **mElecActivePower = intfVoltageDQ(0, 0) * intfCurrentDQ(0, 0) +
232 intfVoltageDQ(1, 0) * intfCurrentDQ(1, 0);
233
234 // Keep the damping coefficient established by the parameter setter. It is
235 // already converted from pu power / pu speed to W / (rad/s); replacing it
236 // here with the nominal power both discarded the user parameter and changed
237 // its units.
238
239 // #### Calculate state for time step k+1 ####
240 // semi-implicit Euler or symplectic Euler method for mechanical equations
241 Real dOmMech =
242 mNomOmega / (2. * **mInertia * mNomPower) *
245 **mOmMech = **mOmMech + mTimeStep * dOmMech;
246 Real dDelta_p = **mOmMech - mNomOmega;
248 **mDelta_p = **mDelta_p + mTimeStep * dDelta_p;
249 // Update emf - only phase changes
251 **mEp = Complex(**mEp_abs * cos(**mDelta_p), **mEp_abs * sin(**mDelta_p));
252
253 // Update nominal system angle
255
256 // mStates << Math::abs(mEp), Math::phaseDeg(mEp), mElecActivePower, mMechPower,
257 // mDelta_p, mOmMech, dOmMech, dDelta_p, (**mIntfVoltage)(0,0).real(), (**mIntfVoltage)(0,0).imag();
258 // SPDLOG_LOGGER_DEBUG(mSLog, "\nStates, time {:f}: \n{:s}", time, Logger::matrixToString(mStates));
259}
260
262 Real omega, Real timeStep, Attribute<Matrix>::Ptr leftVector) {
263 mTimeStep = timeStep;
264 const Real denominator = 2. * mLpd + **mRs * timeStep;
265 mStatorConductance = Matrix::Identity(3, 3) * (timeStep / denominator);
266 mStatorCurrentDecay = (2. * mLpd - **mRs * timeStep) / denominator;
267}
268
270 SparseMatrixRow &systemMatrix) {
271 for (UInt phase = 0; phase < 3; ++phase)
272 Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, phase),
273 matrixNodeIndex(0, phase),
274 mStatorConductance(phase, phase));
275}
276
278 Matrix &rightVector) {
279 for (UInt phase = 0; phase < 3; ++phase)
280 Math::addToVectorElement(rightVector, matrixNodeIndex(0, phase),
281 (**mIntfCurrent)(phase, 0));
282}
283
285 AttributeBase::List &prevStepDependencies,
286 AttributeBase::List &attributeDependencies,
287 AttributeBase::List &modifiedAttributes) {
288 prevStepDependencies.push_back(mIntfVoltage);
289 prevStepDependencies.push_back(mIntfCurrent);
290 prevStepDependencies.push_back(mStatorVoltagePrevious);
291 modifiedAttributes.push_back(mStatorSourceVoltage);
292 modifiedAttributes.push_back(mStatorHistoryCurrent);
293 modifiedAttributes.push_back(mRightVector);
294};
295
297 AttributeBase::List &prevStepDependencies,
298 AttributeBase::List &attributeDependencies,
299 AttributeBase::List &modifiedAttributes,
300 Attribute<Matrix>::Ptr &leftVector) {
301 attributeDependencies.push_back(leftVector);
302 modifiedAttributes.push_back(mIntfVoltage);
303 modifiedAttributes.push_back(mIntfCurrent);
304 modifiedAttributes.push_back(mStatorVoltagePrevious);
305};
306
308 Int timeStepCount) {
309 step(time);
311 for (UInt phase = 0; phase < 3; ++phase)
312 (**mStatorSourceVoltage)(phase, 0) =
313 Math::abs(emfPhasors(phase, 0)) *
314 std::cos(2. * PI * mNomFreq * time + Math::phase(emfPhasors(phase, 0)));
317 const Matrix sourceInjection =
319 **mRightVector = Matrix::Zero((**mRightVector).rows(), 1);
320 for (UInt phase = 0; phase < 3; ++phase)
322 sourceInjection(phase, 0));
323}
324
326 Real time, Int timeStepCount, Attribute<Matrix>::Ptr &leftVector) {
327 mnaCompUpdateVoltage(**leftVector);
328 mnaCompUpdateCurrent(**leftVector);
329}
330
332 const Matrix &leftVector) {
333 SPDLOG_LOGGER_DEBUG(mSLog, "Read voltage from {:d}", matrixNodeIndex(0));
334 (**mIntfVoltage)(0, 0) =
336 (**mIntfVoltage)(1, 0) =
338 (**mIntfVoltage)(2, 0) =
340}
341
343 const Matrix &leftVector) {
344 SPDLOG_LOGGER_DEBUG(mSLog, "Read current from {:d}", matrixNodeIndex(0));
345 const Matrix branchVoltage = **mIntfVoltage - **mStatorSourceVoltage;
346 **mIntfCurrent =
347 -mStatorConductance * branchVoltage + **mStatorHistoryCurrent;
348 **mStatorVoltagePrevious = branchVoltage;
349}
std::vector< Ptr > List
Definition Attribute.h:123
AttributePointer< Attribute< T > > Ptr
Definition Attribute.h:249
Real mNomFreq
nominal frequency fn [Hz]
Real mTimeStep
Simulation time step.
Real mLmd
d-axis mutual inductance Lmd [H]
SynchronGenerator(CPS::AttributeList::Ptr attributeList)
Constructor.
Real mLlfd
field leakage inductance Llfd [H]
const Attribute< Real >::Ptr mMechPower
mechanical Power Pm [W]
Real mNomVolt
nominal voltage Vn [V] (phase-to-phase RMS)
Real mBase_L
base stator inductance
Real mBase_Z
base stator impedance
const Attribute< Real >::Ptr mRs
stator resistance Rs [Ohm]
const Attribute< Real >::Ptr mElecActivePower
Active part of the electrical power.
StateType mStateType
specifies if the machine parameters are transformed to per unit
Real mNomOmega
nominal angular frequency wn [Hz]
const Attribute< Real >::Ptr mLl
leakage inductance Ll [H]
void setBaseParameters(Real nomPower, Real nomVolt, Real nomFreq)
Real mLfd
field inductance Lfd [H]
const Attribute< Real >::Ptr mLd
d-axis inductance Ld [H]
Real mNomPower
nominal power Pn [VA]
const Attribute< Real >::Ptr mOmMech
rotor speed omega_r
Real calcHfromJ(Real J, Real omegaNominal, Int polePairNumber)
const Attribute< Real >::Ptr mInertia
inertia constant H [s] for per unit or moment of inertia J [kg*m^2]
CompositePowerComp(String uid, String name, Bool hasPreStep, Bool hasPostStep, Logger::Level logLevel)
void setStandardParametersPU(Real nomPower, Real nomVolt, Real nomFreq, Real Xpd, Real inertia, Real Rs=0, Real D=0)
Initializes the machine parameters.
void mnaParentAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
const Attribute< Matrix >::Ptr mStatorVoltagePrevious
Stator R-L branch Norton companion state.
void mnaParentAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override
void mnaParentInitialize(Real omega, Real timeStep, Attribute< Matrix >::Ptr leftVector) override
Initializes variables of component.
void setStandardParametersSI(Real nomPower, Real nomVolt, Real nomFreq, Int polePairNumber, Real Rs, Real Lpd, Real inertiaJ, Real Kd=0)
Initializes the machine parameters.
void mnaCompUpdateCurrent(const Matrix &leftVector) override
SimPowerComp< Real >::Ptr clone(String name) override
Returns a modified copy of the component with the given suffix added to the name and without.
void mnaParentPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
Retrieves calculated voltage from simulation for next step.
SynchronGeneratorTrStab(String uid, String name, Logger::Level logLevel=Logger::Level::off)
void mnaParentApplyRightSideVectorStamp(Matrix &rightVector) override
const Attribute< Complex >::Ptr mEp
True after createSubComponents() runs; prevents double-construction.
void mnaCompUpdateVoltage(const Matrix &leftVector) override
Matrix parkTransformPowerInvariant(Real theta, const Matrix &fabc)
void mnaParentPreStep(Real time, Int timeStepCount) override
Complex mImpedance
Equivalent impedance for loadflow calculation.
void initializeParentFromNodesAndTerminals(Real frequency) override
void mnaParentApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) override
Stamp the stator R-L Norton conductance and history current.
const Attribute< Real >::Ptr mDelta_p
Angle by which the emf Ep is leading the terminal voltage.
void setInitialValues(Complex elecPower, Real mechPower)
void createSubComponents() override
Constructs and registers MNA subcomponents without emf value; idempotent.
void setFundamentalParametersPU(Real nomPower, Real nomVolt, Real nomFreq, Real Ll, Real Lmd, Real Llfd, Real inertia, Real D=0)
Initializes the machine parameters.
Real mLpd
Absolute d-axis transient inductance.
Real mXpd
Absolute d-axis transient reactance X'd.
String uid()
Returns unique id.
AttributeList::Ptr mAttributes
Attribute List.
spdlog::level::level_enum Level
Definition Logger.h:33
Attribute< Matrix >::Ptr mRightVector
static Real realFromVectorElement(const Matrix &mat, Matrix::Index row)
static void addToVectorElement(Matrix &mat, Matrix::Index row, Complex value, Int maxFreq=1, Int freqIdx=0)
static Real phaseDeg(Complex value)
Definition MathUtils.cpp:69
static Real phase(Complex value)
Definition MathUtils.cpp:67
static Real abs(Complex value)
Definition MathUtils.cpp:71
static void addToMatrixElement(SparseMatrixRow &mat, Matrix::Index row, Matrix::Index column, Complex value, Int maxFreq=1, Int freqIdx=0)
static MatrixComp singlePhaseVariableToThreePhase(Complex var_1ph)
To convert single phase complex variables (voltages, currents) to symmetrical three phase ones.
UInt matrixNodeIndex(UInt nodeIndex)
const Attribute< MatrixVar< Real > >::Ptr mIntfCurrent
SimTerminal< Real >::Ptr terminal(UInt index)
void setVirtualNodeNumber(UInt num)
const Attribute< MatrixVar< Real > >::Ptr mIntfVoltage
std::shared_ptr< SimPowerComp< VarType > > Ptr
Complex initialSingleVoltage(UInt index)
void setTerminalNumber(UInt num)
Logger::Level mLogLevel
Component logger control for internal variables.
Logger::Log mSLog
Component logger.
static std::shared_ptr< SynchronGeneratorTrStab > make(Args &&...args)
Definition PtrFactory.h:19
#define PI
Definition Definitions.h:43
#define RMS3PH_TO_PEAK1PH
Definition Definitions.h:50
#define SHIFT_TO_PHASE_C
Definition Definitions.h:47
#define M_PI
Definition Definitions.h:41
#define SHIFT_TO_PHASE_B
Definition Definitions.h:46
Eigen::Matrix< Real, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > Matrix
Dense matrix for real numbers.
Definition Definitions.h:79
std::string String
Definition Definitions.h:63
double Real
Definition Definitions.h:60
int Int
Definition Definitions.h:59
std::complex< Real > Complex
Definition Definitions.h:61
Eigen::Matrix< Complex, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > MatrixComp
Dense matrix for complex numbers.
Definition Definitions.h:82
unsigned int UInt
Definition Definitions.h:58
Eigen::SparseMatrix< Real, Eigen::RowMajor > SparseMatrixRow
Sparse matrix for real numbers (row major).
Definition Definitions.h:72