DPsim
Loading...
Searching...
No Matches
DP_Ph1_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
15
17 String uid, String name, Bool useSubcomponents, Logger::Level logLevel)
19 CompositePowerComp<Complex>(uid, name, true, true, logLevel),
20 mUseSubcomponents(useSubcomponents),
21 mEp(mAttributes->create<Complex>("Ep")),
22 mEp_abs(mAttributes->create<Real>("Ep_mag")),
23 mEp_phase(mAttributes->create<Real>("Ep_phase")),
24 mDelta_p(mAttributes->create<Real>("delta_r")),
25 mRefOmega(mAttributes->createDynamic<Real>("w_ref")),
26 mRefDelta(mAttributes->createDynamic<Real>("delta_ref")) {
29 **mIntfVoltage = MatrixComp::Zero(1, 1);
30 **mIntfCurrent = MatrixComp::Zero(1, 1);
31
32 mStates = Matrix::Zero(10, 1);
33}
34
42
44 Real nomPower, Real nomVolt, Real nomFreq, Real Ll, Real Lmd, Real Llfd,
45 Real inertia, Real D) {
46 setBaseParameters(nomPower, nomVolt, nomFreq);
47 SPDLOG_LOGGER_INFO(mSLog,
48 "\n--- Base Parameters ---"
49 "\nnomPower: {:f}"
50 "\nnomVolt: {:f}"
51 "\nnomFreq: {:f}",
53
54 // Input is in per unit but all values are converted to absolute values.
57
58 **mLl = Ll;
59 mLmd = Lmd;
60 **mLd = **mLl + mLmd;
61 mLlfd = Llfd;
62 mLfd = mLlfd + mLmd;
63 // M = 2*H where H = inertia
64 **mInertia = inertia;
65 // X'd in absolute values
66 mXpd = mNomOmega * (**mLd - mLmd * mLmd / mLfd) * mBase_L;
67 mLpd = (**mLd - mLmd * mLmd / mLfd) * mBase_L;
68
69 //The units of D are per unit power divided by per unit speed deviation.
70 // D is transformed to an absolute value to obtain Kd, which will be used in the swing equation
71 mKd = D * mNomPower / mNomOmega;
72
73 SPDLOG_LOGGER_INFO(mSLog,
74 "\n--- Parameters ---"
75 "\nimpedance: {:f}"
76 "\ninductance: {:f}"
77 "\ninertia: {:f}"
78 "\ndamping: {:f}",
79 mXpd, mLpd, **mInertia, mKd);
80}
81
83 Real nomPower, Real nomVolt, Real nomFreq, Int polePairNumber, Real Rs,
84 Real Lpd, Real inertiaJ, Real Kd) {
85 setBaseParameters(nomPower, nomVolt, nomFreq);
86 SPDLOG_LOGGER_INFO(mSLog,
87 "\n--- Base Parameters ---"
88 "\nnomPower: {:f}"
89 "\nnomVolt: {:f}"
90 "\nnomFreq: {:f}",
92
95
96 // M = 2*H where H = inertia
97 // H = J * 0.5 * omegaNom^2 / polePairNumber
98 **mInertia = calcHfromJ(inertiaJ, 2 * PI * nomFreq, polePairNumber);
99 // X'd in absolute values
100 mXpd = mNomOmega * Lpd;
101 mLpd = Lpd;
102
103 SPDLOG_LOGGER_INFO(mSLog,
104 "\n--- Parameters ---"
105 "\nimpedance: {:f}"
106 "\ninductance: {:f}"
107 "\ninertia: {:f}"
108 "\ndamping: {:f}",
109 mXpd, mLpd, **mInertia, mKd);
110}
111
113 Real nomPower, Real nomVolt, Real nomFreq, Real Xpd, Real inertia, Real Rs,
114 Real D) {
115 setBaseParameters(nomPower, nomVolt, nomFreq);
116 SPDLOG_LOGGER_INFO(mSLog,
117 "\n--- Base Parameters ---"
118 "\nnomPower: {:f}"
119 "\nnomVolt: {:f}"
120 "\nnomFreq: {:f}",
122
123 // Input is in per unit but all values are converted to absolute values.
126
127 // M = 2*H where H = inertia
128 **mInertia = inertia;
129 // X'd in absolute values
130 mXpd = Xpd * mBase_Z;
131 mLpd = Xpd * mBase_L;
132
133 **mRs = Rs;
134 //The units of D are per unit power divided by per unit speed deviation.
135 // D is transformed to an absolute value to obtain Kd, which will be used in the swing equation
136 mKd = D * mNomPower / mNomOmega;
137
138 SPDLOG_LOGGER_INFO(mSLog,
139 "\n--- Parameters ---"
140 "\nimpedance: {:f}"
141 "\ninductance: {:f}"
142 "\ninertia: {:f}"
143 "\ndamping: {:f}",
144 mXpd, mLpd, **mInertia, mKd);
145}
146
148 Bool convertWithOmegaMech) {
149 mConvertWithOmegaMech = convertWithOmegaMech;
150
151 SPDLOG_LOGGER_INFO(mSLog,
152 "\n--- Model flags ---"
153 "\nconvertWithOmegaMech: {:s}",
154 std::to_string(mConvertWithOmegaMech));
155}
156
158 Real mechPower) {
159 mInitElecPower = elecPower;
160 mInitMechPower = mechPower;
161}
162
165 return;
166 if (mSubCompCreated)
167 return;
168 mSubCompCreated = true;
169
170 // Create the inner voltage source (Ep). Parameters (mEp) depend on the
171 // power-flow solution and are not yet known here; they are applied in
172 // initializeFromNodesAndTerminals after the static calculation.
175 // Alias the sub-source's virtual node to parent VN1 so the KCL row in the
176 // system matrix uses the same node index as the parent's VN1.
177 mSubVoltageSource->setVirtualNodeAt(mVirtualNodes[1], 0);
181
182 // Create the inner inductor representing X'd.
184 mSubInductor->setParameters(mLpd);
185 mSubInductor->connect({mVirtualNodes[0], terminal(0)->node()});
188}
189
191 Real frequency) {
192 // Initialize omega mech with nominal system frequency
193 **mOmMech = mNomOmega;
194
195 // Static calculation based on load flow
196 (**mIntfVoltage)(0, 0) = initialSingleVoltage(0);
198 ? -terminal(0)->singlePower()
202
203 //I_intf is the current which is flowing into the Component, while mInitElecPower is flowing out of it
204 (**mIntfCurrent)(0, 0) = std::conj(-mInitElecPower / (**mIntfVoltage)(0, 0));
205
207
208 // Calculate initial emf behind reactance from power flow results
209 **mEp = (**mIntfVoltage)(0, 0) - mImpedance * (**mIntfCurrent)(0, 0);
210
211 // The absolute value of Ep is constant, only delta_p changes every step
212 **mEp_abs = Math::abs(**mEp);
213 // Delta_p is the angular position of mEp with respect to the synchronously rotating reference
214 **mDelta_p = Math::phase(**mEp);
215
216 // Update active electrical power that is compared with the mechanical power
218 ((**mIntfVoltage)(0, 0) * std::conj(-(**mIntfCurrent)(0, 0))).real();
219
220 // Start in steady state so that electrical and mech. power are the same
221 // because of the initial condition mOmMech = mNomOmega the damping factor is not considered at the initialisation
223
224 // Initialize node between X'd and Ep
225 mVirtualNodes[0]->setInitialVoltage(**mEp);
226
227 // Set emf on the already-created voltage source; the framework's generic
228 // sub-init loop will initialize it after this hook returns.
230 mSubVoltageSource->setParameters(**mEp);
231
232 SPDLOG_LOGGER_INFO(mSLog,
233 "\n--- Initialize according to powerflow ---"
234 "\nTerminal 0 voltage: {:e}<{:e}"
235 "\nVoltage behind reactance: {:e}<{:e}"
236 "\ninitial electrical power: {:e}+j{:e}"
237 "\nactive electrical power: {:e}"
238 "\nmechanical power: {:e}"
239 "\n--- End of powerflow initialization ---",
240 Math::abs((**mIntfVoltage)(0, 0)),
244}
245
247
248 // #### Calculations based on values from time step k ####
249 // Electrical power at time step k
251 ((**mIntfVoltage)(0, 0) * std::conj(-(**mIntfCurrent)(0, 0))).real();
252
253 // Mechanical speed derivative at time step k
254 // convert torque to power with actual rotor angular velocity or nominal omega
255 Real dOmMech;
257 dOmMech =
258 mNomOmega * mNomOmega / (2. * **mInertia * mNomPower * **mOmMech) *
260 else
261 dOmMech =
262 mNomOmega / (2. * **mInertia * mNomPower) *
264
265 // #### Calculate states for time step k+1 applying semi-implicit Euler ####
266 // Mechanical speed at time step k+1 applying Euler forward
268 **mOmMech = **mOmMech + mTimeStep * dOmMech;
269
270 // Derivative of rotor angle at time step k + 1
271 // if reference omega is set, calculate delta with respect to reference
272 Real dDelta_p = **mOmMech - (mUseOmegaRef ? **mRefOmega : mNomOmega);
273
274 // Rotor angle at time step k + 1 applying Euler backward
275 // Update emf - only phase changes
277 **mDelta_p = **mDelta_p + mTimeStep * dDelta_p;
278 **mEp = Complex(**mEp_abs * cos(**mDelta_p), **mEp_abs * sin(**mDelta_p));
279 }
280
282 **mMechPower, **mDelta_p, **mOmMech, dOmMech, dDelta_p,
283 (**mIntfVoltage)(0, 0).real(), (**mIntfVoltage)(0, 0).imag();
284 SPDLOG_LOGGER_DEBUG(mSLog, "\nStates, time {:f}: \n{:s}", time,
286}
287
289 Real omega, Real timeStep, Attribute<Matrix>::Ptr leftVector) {
290 mTimeStep = timeStep;
291 if (!mUseSubcomponents) {
292 mDirectAdmittance = MatrixComp::Zero(mNumFreqs, 1);
293 mDirectPreviousCurrentFactor = MatrixComp::Zero(mNumFreqs, 1);
294 mDirectHistory = MatrixComp::Zero(mNumFreqs, 1);
295 mDirectCurrent = MatrixComp::Zero(mNumFreqs, 1);
296 mDirectVoltage = MatrixComp::Zero(mNumFreqs, 1);
297 for (UInt freq = 0; freq < mNumFreqs; ++freq) {
298 const Real b = timeStep * 2. * PI * mFrequencies(freq, 0) / 2.;
299 const Real a = timeStep / (2. * mLpd);
300 mDirectAdmittance(freq, 0) =
301 Complex(a / (1. + b * b), -a * b / (1. + b * b));
303 Complex((1. - b * b) / (1. + b * b), -2. * b / (1. + b * b));
304 mDirectVoltage(freq, 0) = (**mIntfVoltage)(0, freq) - **mEp;
305 const Real harmonicOmega = 2. * PI * mFrequencies(freq, 0);
306 mDirectCurrent(freq, 0) =
307 harmonicOmega != 0.
308 ? mDirectVoltage(freq, 0) / Complex(0., harmonicOmega * mLpd)
309 : (**mIntfCurrent)(0, freq);
310 }
311 } else {
312 mMnaTasks.push_back(std::make_shared<AddBStep>(*this));
313 }
314}
315
317 SparseMatrixRow &systemMatrix) {
319 return;
320 const UInt emfNode = virtualNode(0)->matrixNodeIndex();
321 const UInt sourceCurrent = virtualNode(1)->matrixNodeIndex();
322 const UInt terminal = matrixNodeIndex(0);
323 for (UInt freq = 0; freq < mNumFreqs; ++freq) {
325 mDirectAdmittance(freq, 0), systemMatrix, emfNode, terminal, true,
327 Math::setMatrixElement(systemMatrix, sourceCurrent, emfNode,
328 Complex(1., 0.), mNumFreqs, freq);
329 Math::setMatrixElement(systemMatrix, emfNode, sourceCurrent,
330 Complex(1., 0.), mNumFreqs, freq);
331 }
332}
333
335 Matrix &rightVector) {
337 return;
338 const UInt emfNode = virtualNode(0)->matrixNodeIndex();
339 const UInt sourceCurrent = virtualNode(1)->matrixNodeIndex();
340 for (UInt freq = 0; freq < mNumFreqs; ++freq) {
341 mDirectHistory(freq, 0) =
342 mDirectAdmittance(freq, 0) * mDirectVoltage(freq, 0) +
344 if (terminalNotGrounded(0))
346 -mDirectHistory(freq, 0), mNumFreqs, freq);
347 Math::setVectorElement(rightVector, emfNode, mDirectHistory(freq, 0),
348 mNumFreqs, freq);
349 Math::setVectorElement(rightVector, sourceCurrent, **mEp, mNumFreqs, freq);
350 }
351}
352
354 AttributeBase::List &prevStepDependencies,
355 AttributeBase::List &attributeDependencies,
356 AttributeBase::List &modifiedAttributes) {
357 // other attributes generally also influence the pre step,
358 // but aren't marked as writable anyway
360 prevStepDependencies.push_back(mIntfVoltage);
361 if (!mUseSubcomponents) {
362 prevStepDependencies.push_back(mIntfCurrent);
363 modifiedAttributes.push_back(mRightVector);
364 }
365}
366
368 AttributeBase::List &prevStepDependencies,
369 AttributeBase::List &attributeDependencies,
370 AttributeBase::List &modifiedAttributes,
371 Attribute<Matrix>::Ptr &leftVector) {
372 attributeDependencies.push_back(leftVector);
373 modifiedAttributes.push_back(mIntfVoltage);
375 modifiedAttributes.push_back(mIntfCurrent);
376}
377
379 Int timeStepCount) {
380 step(time);
382 mSubVoltageSource->mVoltageRef->set(**mEp);
383 else
385}
386
388 Int timeStepCount) {
389 **mGenerator.mRightVector = mGenerator.mSubInductor->mRightVector->get() +
390 mGenerator.mSubVoltageSource->mRightVector->get();
391}
392
394 Real time, Int timeStepCount, Attribute<Matrix>::Ptr &leftVector) {
395 mnaCompUpdateVoltage(**leftVector);
396 mnaCompUpdateCurrent(**leftVector);
397}
398
400 const Matrix &leftVector) {
401 SPDLOG_LOGGER_DEBUG(mSLog, "Read voltage from {:d}", matrixNodeIndex(0));
402 (**mIntfVoltage)(0, 0) =
405 mDirectVoltage(0, 0) = (**mIntfVoltage)(0, 0) -
407 leftVector, virtualNode(0)->matrixNodeIndex());
408}
409
411 const Matrix &leftVector) {
412 SPDLOG_LOGGER_DEBUG(mSLog, "Read current from {:d}", matrixNodeIndex(0));
413 //Current flowing out of component
414 if (mUseSubcomponents) {
415 **mIntfCurrent = mSubInductor->mIntfCurrent->get();
416 } else {
417 mDirectCurrent(0, 0) =
418 mDirectAdmittance(0, 0) * mDirectVoltage(0, 0) + mDirectHistory(0, 0);
420 }
421}
422
424 Attribute<Real>::Ptr refOmegaPtr, Attribute<Real>::Ptr refDeltaPtr) {
425 mRefOmega->setReference(refOmegaPtr);
426 mRefDelta->setReference(refDeltaPtr);
427 mUseOmegaRef = true;
428
429 SPDLOG_LOGGER_INFO(mSLog, "Use of reference omega.");
430}
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]
void addMNASubComponent(typename SimPowerComp< Complex >::Ptr subc, MNA_SUBCOMP_TASK_ORDER preStepOrder, MNA_SUBCOMP_TASK_ORDER postStepOrder, Bool contributeToRightVector)
void mnaCompApplyRightSideVectorStamp(Matrix &rightVector) override
CompositePowerComp(String uid, String name, Bool hasPreStep, Bool hasPostStep, Logger::Level logLevel)
void mnaParentPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
Retrieves calculated voltage from simulation for next step.
void mnaCompUpdateCurrent(const Matrix &leftVector) override
std::shared_ptr< Inductor > mSubInductor
Inner inductor that represents the generator impedance.
void setStandardParametersSI(Real nomPower, Real nomVolt, Real nomFreq, Int polePairNumber, Real Rs, Real Lpd, Real inertiaJ, Real Kd=0)
Initializes the machine parameters.
void mnaParentApplyRightSideVectorStamp(Matrix &rightVector) override
Complex mImpedance
Equivalent impedance for loadflow calculation.
void mnaParentAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
const Attribute< Complex >::Ptr mEp
True after createSubComponents() runs; prevents double-construction.
void mnaParentAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override
SimPowerComp< Complex >::Ptr clone(String name) override
Returns a modified copy of the component with the given suffix added to the name and without.
void setStandardParametersPU(Real nomPower, Real nomVolt, Real nomFreq, Real Xpd, Real inertia, Real Rs=0, Real D=0)
Initializes the machine parameters.
const Attribute< Real >::Ptr mDelta_p
Angle by which the emf Ep is leading the terminal voltage.
void mnaParentInitialize(Real omega, Real timeStep, Attribute< Matrix >::Ptr leftVector) override
Initializes variables of component.
void setInitialValues(Complex elecPower, Real mechPower)
void setModelFlags(Bool convertWithOmegaMech)
Flags to modify model behavior.
Real mXpd
Absolute d-axis transient reactance X'd.
Bool mConvertWithOmegaMech
Flag for usage of actual mechanical speed for torque conversion (otherwise mNomOmega is used)
Bool mUseOmegaRef
Flag for usage of attribute of w_ref (otherwise mNomOmega is used)
void setFundamentalParametersPU(Real nomPower, Real nomVolt, Real nomFreq, Real Ll, Real Lmd, Real Llfd, Real inertia, Real D=0)
Initializes the machine parameters.
void createSubComponents() override
Constructs and registers MNA subcomponents without emf value; idempotent.
SynchronGeneratorTrStab(String uid, String name, Logger::Level logLevel=Logger::Level::off)
void setReferenceOmega(Attribute< Real >::Ptr refOmegaPtr, Attribute< Real >::Ptr refDeltaPtr)
void initializeParentFromNodesAndTerminals(Real frequency) override
std::shared_ptr< VoltageSource > mSubVoltageSource
Inner voltage source that represents the generator.
void mnaParentPreStep(Real time, Int timeStepCount) override
void mnaCompUpdateVoltage(const Matrix &leftVector) override
void mnaParentApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) override
Real mLpd
Absolute d-axis transient inductance.
const Attribute< String >::Ptr mName
Human readable name.
String uid()
Returns unique id.
AttributeList::Ptr mAttributes
Attribute List.
spdlog::level::level_enum Level
Definition Logger.h:33
static String matrixToString(const Matrix &mat)
Definition Logger.cpp:31
Attribute< Matrix >::Ptr mRightVector
static void stampAdmittance(Complex admittance, SparseMatrixRow &mat, UInt node1Index, UInt node2Index, Bool isTerminal1NotGrounded, Bool isTerminal2NotGrounded, const Logger::Log &mSLog, Int maxFreq=1, Int freqIdx=0)
static Complex complexFromVectorElement(const Matrix &mat, Matrix::Index row, Int maxFreq=1, Int freqIdx=0)
static void setVectorElement(Matrix &mat, Matrix::Index row, Complex value, Int maxFreq=1, Int freqIdx=0, Matrix::Index colOffset=0)
static Real phaseDeg(Complex value)
Definition MathUtils.cpp:69
static Real phase(Complex value)
Definition MathUtils.cpp:67
static void setMatrixElement(SparseMatrixRow &mat, Matrix::Index row, Matrix::Index column, Complex value, Int maxFreq=1, Int freqIdx=0)
static Real abs(Complex value)
Definition MathUtils.cpp:71
UInt matrixNodeIndex(UInt nodeIndex)
const Attribute< MatrixVar< Complex > >::Ptr mIntfCurrent
SimTerminal< Complex >::Ptr terminal(UInt index)
const Attribute< MatrixVar< Complex > >::Ptr mIntfVoltage
SimNode< Complex >::Ptr virtualNode(UInt index)
std::shared_ptr< SimPowerComp< VarType > > Ptr
SimNode< Complex >::List mVirtualNodes
Bool terminalNotGrounded(UInt index)
Complex initialSingleVoltage(UInt index)
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
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
bool Bool
Definition Definitions.h:62
unsigned int UInt
Definition Definitions.h:58
Eigen::SparseMatrix< Real, Eigen::RowMajor > SparseMatrixRow
Sparse matrix for real numbers (row major).
Definition Definitions.h:72