DPsim
Loading...
Searching...
No Matches
DP_Ph3_SynchronGeneratorDQ.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
10
11//Added for testing/printing purposes:
12#include <fstream>
13#include <iostream>
14
15using namespace CPS;
16using namespace std;
17
18namespace {
19const Complex kPositiveSequenceRotation(std::cos(2. / 3. * PI),
20 std::sin(2. / 3. * PI));
21const Complex kPositiveSequenceRotationSquared =
22 std::pow(kPositiveSequenceRotation, 2);
23} // namespace
24
26 Logger::Level logLevel)
27 : MNASimPowerComp<Complex>(uid, name, true, true, logLevel),
31 **mIntfVoltage = MatrixComp::Zero(3, 1);
32 **mIntfCurrent = MatrixComp::Zero(3, 1);
33}
34
38
40
42 Real frequency) {
44 return;
45
46 // SystemTopology stores the generator's total three-phase PF power on the
47 // phase-A terminal record, matching the machine power base.
48 const Complex terminalPower = -terminal(0)->singlePower();
49 const Complex terminalVoltage = initialSingleVoltage(0);
50 const Real terminalVoltagePeak =
51 RMS3PH_TO_PEAK1PH * Math::abs(terminalVoltage);
53 terminalPower.real(), terminalPower.imag(), terminalVoltagePeak,
54 Math::phase(terminalVoltage), terminalPower.real());
55
56 SPDLOG_LOGGER_INFO(
57 mSLog,
58 "Initialized DP Ph3 synchronous generator from power flow: "
59 "P={} W, Q={} var, V_terminal_peak={} V, angle={} rad, f={} Hz",
60 terminalPower.real(), terminalPower.imag(), terminalVoltagePeak,
61 Math::phase(terminalVoltage), frequency);
62}
63
65 Real nomPower, Real nomVolt, Real nomFreq, Int poleNumber, Real nomFieldCur,
66 Real Rs, Real Ll, Real Lmd, Real Lmq, Real Rfd, Real Llfd, Real Rkd,
67 Real Llkd, Real Rkq1, Real Llkq1, Real Rkq2, Real Llkq2, Real inertia) {
69 nomPower, nomVolt, nomFreq, nomFieldCur, poleNumber, Rs, Ll, Lmd, Lmq,
70 Rfd, Llfd, Rkd, Llkd, Rkq1, Llkq1, Rkq2, Llkq2, inertia);
71}
72
74 Real nomPower, Real nomVolt, Real nomFreq, Int poleNumber, Real nomFieldCur,
75 Real Rs, Real Ll, Real Lmd, Real Lmq, Real Rfd, Real Llfd, Real Rkd,
76 Real Llkd, Real Rkq1, Real Llkq1, Real Rkq2, Real Llkq2, Real inertia,
77 Real initActivePower, Real initReactivePower, Real initTerminalVolt,
78 Real initVoltAngle, Real initMechPower) {
79
81 nomPower, nomVolt, nomFreq, nomFieldCur, poleNumber, Rs, Ll, Lmd, Lmq,
82 Rfd, Llfd, Rkd, Llkd, Rkq1, Llkq1, Rkq2, Llkq2, inertia);
83 Base::SynchronGenerator::setInitialValues(initActivePower, initReactivePower,
84 initTerminalVolt, initVoltAngle,
85 initMechPower);
86}
87
90 mSystemOmega = frequencies(0, 0);
91}
92
94 // #### Compensation ####
95 mCompensationOn = false;
96 mCompensationCurrent = MatrixComp::Zero(3, 1);
97 // Calculate real compensation resistance from per unit
99
100 // #### General setup ####
101 // Create matrices for state space representation
103
104 // steady state per unit initial value
106
107 mVdq0 = Matrix::Zero(3, 1);
108 mIdq0 = Matrix::Zero(3, 1);
109 if (mNumDampingWindings == 2) {
110 mVdq0 << mVsr(0, 0), mVsr(3, 0), mVsr(6, 0);
111 mIdq0 << mIsr(0, 0), mIsr(3, 0), mIsr(6, 0);
112 } else {
113 mVdq0 << mVsr(0, 0), mVsr(3, 0), mVsr(5, 0);
114 mIdq0 << mIsr(0, 0), mIsr(3, 0), mIsr(5, 0);
115 }
116
119}
120
122 SparseMatrixRow &systemMatrix) {
123 if (!mCompensationOn)
124 return;
125
126 Real conductance = 1. / mRcomp;
127
128 if (terminalNotGrounded(0)) {
130 matrixNodeIndices(0)[0], Complex(conductance, 0));
132 matrixNodeIndices(0)[1], Complex(conductance, 0));
134 matrixNodeIndices(0)[2], Complex(conductance, 0));
135 SPDLOG_LOGGER_INFO(mSLog, "Add {} to {}, {}", conductance,
137 SPDLOG_LOGGER_INFO(mSLog, "Add {} to {}, {}", conductance,
139 SPDLOG_LOGGER_INFO(mSLog, "Add {} to {}, {}", conductance,
141 }
142}
143
145 Matrix &rightVector) {
146 if (mCompensationOn)
148
149 // If the interface current is positive, it is flowing out of the connected node and into ground.
150 // Therefore, the generator is interfaced as a consumer but since the currents are reversed the equations
151 // are in generator mode.
152 if (terminalNotGrounded(0)) {
153 Math::setVectorElement(rightVector, matrixNodeIndex(0, 0),
154 -(**mIntfCurrent)(0, 0) +
156 Math::setVectorElement(rightVector, matrixNodeIndex(0, 1),
157 -(**mIntfCurrent)(1, 0) +
159 Math::setVectorElement(rightVector, matrixNodeIndex(0, 2),
160 -(**mIntfCurrent)(2, 0) +
162 }
163}
164
166 const Matrix &leftVector) {
167 (**mIntfVoltage)(0, 0) =
169 (**mIntfVoltage)(1, 0) =
171 (**mIntfVoltage)(2, 0) =
173}
174
176 AttributeBase::List &prevStepDependencies,
177 AttributeBase::List &attributeDependencies,
178 AttributeBase::List &modifiedAttributes,
179 Attribute<Matrix>::Ptr &leftVector) {
180 attributeDependencies.push_back(leftVector);
181 modifiedAttributes.push_back(mIntfVoltage);
182}
183
185 Real time, Int timeStepCount, Attribute<Matrix>::Ptr &leftVector) {
186 mnaCompUpdateVoltage(**leftVector);
187}
188
192
196
198
201 MatrixComp &abcVector) {
202 // Balanced positive-sequence projection, followed by e^(-j theta). Keep
203 // the sequence rotation constants shared instead of rebuilding 3x3
204 // transform matrices and powers of alpha at every generator timestep.
205 const Complex positiveSequence =
206 (abcVector(0, 0) + kPositiveSequenceRotation * abcVector(1, 0) +
207 kPositiveSequenceRotationSquared * abcVector(2, 0)) /
208 3.;
209 const Complex thetaCompInv(std::cos(theta), -std::sin(theta));
210 const Complex pnz = positiveSequence * thetaCompInv;
211
212 MatrixFixedSize<3, 1> dq0Vector;
213 dq0Vector << pnz.real(), pnz.imag(), 0.;
214
215 return dq0Vector;
216}
217
219 Real theta, const Eigen::Ref<const Matrix> &dq0) {
220 const Complex thetaComp(std::cos(theta), std::sin(theta));
221 const Complex positiveSequence = Complex(dq0(0, 0), dq0(1, 0)) * thetaComp;
222 MatrixFixedSizeComp<3, 1> abcCompVector;
223 abcCompVector << positiveSequence,
224 kPositiveSequenceRotationSquared * positiveSequence,
225 kPositiveSequenceRotation * positiveSequence;
226
227 return abcCompVector;
228}
229
231
std::vector< Ptr > List
Definition Attribute.h:123
AttributePointer< Attribute< T > > Ptr
Definition Attribute.h:249
Matrix mIsr
Vector of stator and rotor currents.
Real mSystemOmega
Simulation angular system speed.
Real mRcomp
Compensation Resistance.
Real mBase_I
base stator current peak
void setInitialValues(Real initActivePower, Real initReactivePower, Real initTerminalVolt, Real initVoltAngle, Real initMechPower)
Bool mCompensationOn
Determines if compensation elements are used.
SynchronGenerator(CPS::AttributeList::Ptr attributeList)
Constructor.
Bool mInitialValuesSet
Flag to remember when initial values are set.
Matrix mVsr
Vector of stator and rotor voltages.
Real mBase_Z
base stator impedance
Matrix mIdq0
dq0 current calculated from terminal current
void setBaseAndFundamentalPerUnitParameters(Real nomPower, Real nomVolt, Real nomFreq, Real nomFieldCur, Int poleNumber, Real Rs, Real Ll, Real Lmd, Real Lmq, Real Rfd, Real Llfd, Real Rkd, Real Llkd, Real Rkq1, Real Llkq1, Real Rkq2, Real Llkq2, Real inertia)
Initializes the base and fundamental machine parameters in per unit.
Matrix mVdq0
dq0 voltage calculated from terminal voltage
Real mBase_OmMech
base mechanical angular frequency
Int mNumDampingWindings
Number of damping windings in q.
Real mBase_V
base stator voltage (phase-to-ground peak)
const Attribute< Real >::Ptr mOmMech
rotor speed omega_r
const Attribute< Real >::Ptr mElecTorque
electrical torque
MatrixFixedSizeComp< 3, 1 > dq0ToAbcTransform(Real theta, const Eigen::Ref< const Matrix > &dq0)
Inverse Park transform as described in Krause.
void initialize(Real omega, Real timeStep)
void mnaCompApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) override
void initializeMatrixAndStates()
Build the per-unit steady-state machine state after PF values are loaded.
SynchronGeneratorDQ(String name, String uid, Logger::Level logLevel=Logger::Level::off)
Defines UID, name and logging level.
void trapezoidalFluxStates()
calculate flux states using trapezoidal rule - depcrecated
MatrixFixedSize< 3, 1 > abcToDq0Transform(Real theta, MatrixComp &abc)
Park transform as described in Krause.
virtual void mnaCompUpdateVoltage(const Matrix &leftVector) override
Retrieves calculated voltage from simulation for next step.
void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
Add MNA post step dependencies.
void initializeFromNodesAndTerminals(Real frequency) override
Initialize the operating point from a connected power-flow solution.
void mnaCompApplyRightSideVectorStamp(Matrix &rightVector) override
MatrixComp mCompensationCurrent
Compensation current source set point.
void setMachineParametersFundamentalPerUnit(Real nomPower, Real nomVolt, Real nomFreq, Int poleNumber, Real nomFieldCur, Real Rs, Real Ll, Real Lmd, Real Lmq, Real Rfd, Real Llfd, Real Rkd, Real Llkd, Real Rkq1, Real Llkq1, Real Rkq2, Real Llkq2, Real inertia)
void setParametersFundamentalPerUnit(Real nomPower, Real nomVolt, Real nomFreq, Int poleNumber, Real nomFieldCur, Real Rs, Real Ll, Real Lmd, Real Lmq, Real Rfd, Real Llfd, Real Rkd, Real Llkd, Real Rkq1, Real Llkq1, Real Rkq2, Real Llkq2, Real inertia, Real initActivePower, Real initReactivePower, Real initTerminalVolt, Real initVoltAngle, Real initMechPower)
void mnaCompPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
String uid()
Returns unique id.
AttributeList::Ptr mAttributes
Attribute List.
spdlog::level::level_enum Level
Definition Logger.h:33
MNASimPowerComp(String uid, String name, Bool hasPreStep, Bool hasPostStep, Logger::Level logLevel)
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 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)
UInt matrixNodeIndex(UInt nodeIndex)
const Attribute< MatrixVar< Complex > >::Ptr mIntfCurrent
virtual void initialize(Matrix frequencies)
Initialize components with correct network frequencies.
SimTerminal< Complex >::Ptr terminal(UInt index)
const Attribute< MatrixVar< Complex > >::Ptr mIntfVoltage
Bool terminalNotGrounded(UInt index)
Complex initialSingleVoltage(UInt index)
std::vector< UInt > matrixNodeIndices(UInt index)
Logger::Log mSLog
Component logger.
#define PI
Definition Definitions.h:43
#define RMS3PH_TO_PEAK1PH
Definition Definitions.h:50
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
Eigen::Matrix< Real, rows, cols, Eigen::ColMajor > MatrixFixedSize
Dense matrix for real numbers with fixed dimension.
Definition Definitions.h:99
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
Eigen::Matrix< Complex, rows, cols, Eigen::ColMajor > MatrixFixedSizeComp
Dense matrix for complex numbers with fixed dimension.
Eigen::SparseMatrix< Real, Eigen::RowMajor > SparseMatrixRow
Sparse matrix for real numbers (row major).
Definition Definitions.h:72