DPsim
Loading...
Searching...
No Matches
EMT_Ph3_SSN_GFL.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Institute for Automation of Complex Power Systems, EONERC, RWTH Aachen University
2// SPDX-License-Identifier: MPL-2.0
3
4#include <cmath>
5#include <stdexcept>
6
9
10using namespace CPS;
11
13 : TwoTerminalVTypeVariableSSNComp(uid, name, logLevel), mLf(0.0), mCf(0.0),
14 mRf(0.0), mRc(0.0), mOmegaN(0.0), mKpPLL(0.0), mKiPLL(0.0),
15 mOmegaCutoff(0.0), mPRef(0.0), mQRef(0.0), mKpPowerCtrl(0.0),
16 mKiPowerCtrl(0.0), mKpCurrCtrl(0.0), mKiCurrCtrl(0.0),
17 mVcD(mAttributes->create<Real>("vc_d")),
18 mVcQ(mAttributes->create<Real>("vc_q")),
19 mIrcD(mAttributes->create<Real>("irc_d")),
20 mIrcQ(mAttributes->create<Real>("irc_q")),
21 mPInst(mAttributes->create<Real>("p_inst")),
22 mQInst(mAttributes->create<Real>("q_inst")),
23 mOmegaPLL(mAttributes->create<Real>("omega_pll")) {
24 **mIntfVoltage = Matrix::Zero(mInputSize, 1);
25 **mIntfCurrent = Matrix::Zero(mOutputSize, 1);
26
27 **mVcD = 0.0;
28 **mVcQ = 0.0;
29 **mIrcD = 0.0;
30 **mIrcQ = 0.0;
31 **mPInst = 0.0;
32 **mQInst = 0.0;
33 **mOmegaPLL = 0.0;
34}
35
36std::vector<String> EMT::Ph3::SSN_GFL::getLocalStateNames() const {
37 return {
38 "theta_pll", "phi_pll", "p_filtered", "q_filtered", "phi_d",
39 "phi_q", "gamma_d", "gamma_q", "vc_a", "vc_b",
40 "vc_c", "if_a", "if_b", "if_c",
41 };
42}
43
44std::vector<EMT::SSNComp::LocalAbcStateBlock>
46 return {
47 {{static_cast<UInt>(VcA), static_cast<UInt>(VcB), static_cast<UInt>(VcC)},
48 "vc"},
49 {{static_cast<UInt>(IfA), static_cast<UInt>(IfB), static_cast<UInt>(IfC)},
50 "if"},
51 };
52}
53
55 Real omegaN, Real kpPLL, Real kiPLL,
56 Real omegaCutoff, Real pRef, Real qRef,
57 Real kpPowerCtrl, Real kiPowerCtrl,
58 Real kpCurrCtrl, Real kiCurrCtrl) {
59 if (!Math::isFinite(lf) || !Math::isFinite(cf) || !Math::isFinite(rf) ||
60 !Math::isFinite(rc) || !Math::isFinite(omegaN) ||
61 !Math::isFinite(kpPLL) || !Math::isFinite(kiPLL) ||
62 !Math::isFinite(omegaCutoff) || !Math::isFinite(pRef) ||
63 !Math::isFinite(qRef) || !Math::isFinite(kpPowerCtrl) ||
64 !Math::isFinite(kiPowerCtrl) || !Math::isFinite(kpCurrCtrl) ||
65 !Math::isFinite(kiCurrCtrl))
66 throw std::invalid_argument("SSN_GFL parameters must be finite.");
67
68 if (lf <= 0.0)
69 throw std::invalid_argument("Filter inductance lf must be positive.");
70
71 if (cf <= 0.0)
72 throw std::invalid_argument("Filter capacitance cf must be positive.");
73
74 if (rf < 0.0)
75 throw std::invalid_argument("Filter resistance rf must be non-negative.");
76
77 if (rc <= 0.0)
78 throw std::invalid_argument("Coupling resistance rc must be positive.");
79
80 if (omegaN <= 0.0)
81 throw std::invalid_argument(
82 "Nominal angular frequency omegaN must be positive.");
83
84 if (omegaCutoff < 0.0)
85 throw std::invalid_argument(
86 "Power-filter cutoff frequency omegaCutoff must be non-negative.");
87
88 if (kiPLL == 0.0)
89 throw std::invalid_argument("PLL integral gain kiPLL must be non-zero.");
90
91 if (kiPowerCtrl == 0.0)
92 throw std::invalid_argument(
93 "Power-control integral gain kiPowerCtrl must be non-zero.");
94
95 if (kiCurrCtrl == 0.0)
96 throw std::invalid_argument(
97 "Current-control integral gain kiCurrCtrl must be non-zero.");
98
99 mLf = lf;
100 mCf = cf;
101 mRf = rf;
102 mRc = rc;
103
104 mOmegaN = omegaN;
105 mKpPLL = kpPLL;
106 mKiPLL = kiPLL;
107
108 mOmegaCutoff = omegaCutoff;
109 mPRef = pRef;
110 mQRef = qRef;
111 mKpPowerCtrl = kpPowerCtrl;
112 mKiPowerCtrl = kiPowerCtrl;
113 mKpCurrCtrl = kpCurrCtrl;
114 mKiCurrCtrl = kiCurrCtrl;
115
116 const Matrix x0 = Matrix::Zero(mStateSize, 1);
117 const Matrix u0 = Matrix::Zero(mInputSize, 1);
118
119 Matrix aMatrix;
120 Matrix bMatrix;
121 Matrix cMatrix;
122 Matrix dMatrix;
123 Matrix eVector;
124 Matrix fVector;
125
126 buildStateSpaceModel(x0, u0, aMatrix, bMatrix, cMatrix, dMatrix, eVector,
127 fVector);
128
129 VTypeVariableSSNComp::setParameters(aMatrix, bMatrix, cMatrix, dMatrix,
130 eVector, fVector);
131}
132
134EMT::Ph3::SSN_GFL::getParkTransformMatrix(Real theta) const {
135 MatrixFixedSize<2, 3> transform;
136 const Real scale = std::sqrt(2.0 / 3.0);
137
138 transform.row(0) << scale * std::cos(theta),
139 scale * std::cos(theta - 2.0 * PI / 3.0),
140 scale * std::cos(theta + 2.0 * PI / 3.0);
141
142 transform.row(1) << -scale * std::sin(theta),
143 -scale * std::sin(theta - 2.0 * PI / 3.0),
144 -scale * std::sin(theta + 2.0 * PI / 3.0);
145
146 return transform;
147}
148
150EMT::Ph3::SSN_GFL::getInverseParkTransformMatrix(Real theta) const {
151 MatrixFixedSize<3, 2> transform;
152 const Real scale = std::sqrt(2.0 / 3.0);
153
154 transform << scale * std::cos(theta), -scale * std::sin(theta),
155 scale * std::cos(theta - 2.0 * PI / 3.0),
156 -scale * std::sin(theta - 2.0 * PI / 3.0),
157 scale * std::cos(theta + 2.0 * PI / 3.0),
158 -scale * std::sin(theta + 2.0 * PI / 3.0);
159
160 return transform;
161}
162
163void EMT::Ph3::SSN_GFL::evaluateStateDerivative(const Matrix &x,
164 const Matrix &u,
165 Matrix &stateDerivative) const {
166 if (x.rows() != mStateSize || x.cols() != 1)
167 throw std::invalid_argument(
168 "SSN_GFL state vector has an invalid dimension.");
169
170 if (u.rows() != mInputSize || u.cols() != 1)
171 throw std::invalid_argument(
172 "SSN_GFL input vector has an invalid dimension.");
173
174 stateDerivative.setZero(mStateSize, 1);
175
176 const Real thetaPLL = x(ThetaPLL, 0);
177 const Real phiPLL = x(PhiPLL, 0);
178 const Real pFiltered = x(PFiltered, 0);
179 const Real qFiltered = x(QFiltered, 0);
180 const Real phiD = x(PhiD, 0);
181 const Real phiQ = x(PhiQ, 0);
182 const Real gammaD = x(GammaD, 0);
183 const Real gammaQ = x(GammaQ, 0);
184
185 const MatrixFixedSize<3, 1> vcAbc = x.block(VcA, 0, 3, 1);
186 const MatrixFixedSize<3, 1> ifAbc = x.block(IfA, 0, 3, 1);
187
188 const MatrixFixedSize<2, 3> parkTransform = getParkTransformMatrix(thetaPLL);
189 const MatrixFixedSize<3, 2> inverseParkTransform =
190 getInverseParkTransformMatrix(thetaPLL);
191
192 // Positive current is physical inverter injection into the grid.
193 const MatrixFixedSize<3, 1> iGridAbc = (vcAbc - u) / mRc;
194
195 const MatrixFixedSize<2, 1> vcDq = parkTransform * vcAbc;
196 const MatrixFixedSize<2, 1> iGridDq = parkTransform * iGridAbc;
197
198 const Real vcD = vcDq(0, 0);
199 const Real vcQ = vcDq(1, 0);
200 const Real iGridD = iGridDq(0, 0);
201 const Real iGridQ = iGridDq(1, 0);
202
203 // The power-invariant Park transformation requires no additional 3/2
204 // scaling. These equations intentionally match the reference GFL model.
205 const Real pInstantaneous = vcD * iGridD + vcQ * iGridQ;
206 const Real qInstantaneous = -vcD * iGridQ + vcQ * iGridD;
207
208 // ----------------------------------------------------------------------
209 // 1. PLL
210 // ----------------------------------------------------------------------
211 stateDerivative(ThetaPLL, 0) = mOmegaN + mKpPLL * vcQ + mKiPLL * phiPLL;
212 stateDerivative(PhiPLL, 0) = vcQ;
213
214 // ----------------------------------------------------------------------
215 // 2. Active- and reactive-power measurement filters
216 // ----------------------------------------------------------------------
217 stateDerivative(PFiltered, 0) = mOmegaCutoff * (pInstantaneous - pFiltered);
218 stateDerivative(QFiltered, 0) = mOmegaCutoff * (qInstantaneous - qFiltered);
219
220 // ----------------------------------------------------------------------
221 // 3. Outer power controller
222 // ----------------------------------------------------------------------
223 stateDerivative(PhiD, 0) = mPRef - pFiltered;
224 stateDerivative(PhiQ, 0) = qFiltered - mQRef;
225
226 const Real currentReferenceD =
227 mKpPowerCtrl * (mPRef - pFiltered) + mKiPowerCtrl * phiD;
228 const Real currentReferenceQ =
229 mKpPowerCtrl * (qFiltered - mQRef) + mKiPowerCtrl * phiQ;
230
231 // ----------------------------------------------------------------------
232 // 4. Inner current controller
233 // ----------------------------------------------------------------------
234 const Real currentErrorD = currentReferenceD - iGridD;
235 const Real currentErrorQ = currentReferenceQ - iGridQ;
236
237 stateDerivative(GammaD, 0) = currentErrorD;
238 stateDerivative(GammaQ, 0) = currentErrorQ;
239
240 const Real converterVoltageReferenceD =
241 mKpCurrCtrl * currentErrorD + mKiCurrCtrl * gammaD;
242 const Real converterVoltageReferenceQ =
243 mKpCurrCtrl * currentErrorQ + mKiCurrCtrl * gammaQ;
244
245 MatrixFixedSize<2, 1> converterVoltageReferenceDq;
246 converterVoltageReferenceDq << converterVoltageReferenceD,
247 converterVoltageReferenceQ;
248
249 const MatrixFixedSize<3, 1> converterVoltageReferenceAbc =
250 inverseParkTransform * converterVoltageReferenceDq;
251
252 // ----------------------------------------------------------------------
253 // 5. Electrical filter plant
254 // ----------------------------------------------------------------------
255 const MatrixFixedSize<3, 1> vcDerivative =
256 ifAbc / mCf + (u - vcAbc) / (mCf * mRc);
257 const MatrixFixedSize<3, 1> ifDerivative =
258 (converterVoltageReferenceAbc - vcAbc - mRf * ifAbc) / mLf;
259
260 stateDerivative.block(VcA, 0, 3, 1) = vcDerivative;
261 stateDerivative.block(IfA, 0, 3, 1) = ifDerivative;
262}
263
264void EMT::Ph3::SSN_GFL::evaluateOutput(const Matrix &x, const Matrix &u,
265 Matrix &output) const {
266 if (x.rows() != mStateSize || x.cols() != 1)
267 throw std::invalid_argument(
268 "SSN_GFL state vector has an invalid dimension.");
269
270 if (u.rows() != mInputSize || u.cols() != 1)
271 throw std::invalid_argument(
272 "SSN_GFL input vector has an invalid dimension.");
273
274 const Matrix vcAbc = x.block(VcA, 0, 3, 1);
275
276 // SSN current entering the component. The controller uses the opposite
277 // physical current direction as inverter injection into the grid.
278 output = (u - vcAbc) / mRc;
279}
280
281void EMT::Ph3::SSN_GFL::calculateAnalyticalJacobians(const Matrix &x,
282 const Matrix &u, Matrix &A,
283 Matrix &B, Matrix &C,
284 Matrix &D) const {
285 if (x.rows() != mStateSize || x.cols() != 1)
286 throw std::invalid_argument(
287 "SSN_GFL state vector has an invalid dimension.");
288
289 if (u.rows() != mInputSize || u.cols() != 1)
290 throw std::invalid_argument(
291 "SSN_GFL input vector has an invalid dimension.");
292
293 const Real thetaPLL = x(ThetaPLL, 0);
294 const Real pFiltered = x(PFiltered, 0);
295 const Real qFiltered = x(QFiltered, 0);
296 const Real phiD = x(PhiD, 0);
297 const Real phiQ = x(PhiQ, 0);
298 const Real gammaD = x(GammaD, 0);
299 const Real gammaQ = x(GammaQ, 0);
300 const MatrixFixedSize<3, 1> vcAbc = x.block(VcA, 0, 3, 1);
301
302 const MatrixFixedSize<3, 3> identity3 = Matrix::Identity(3, 3);
303
304 const MatrixFixedSize<2, 3> parkTransform = getParkTransformMatrix(thetaPLL);
305 const Eigen::Matrix<Real, 1, 3, Eigen::RowMajor> tD = parkTransform.row(0);
306 const Eigen::Matrix<Real, 1, 3, Eigen::RowMajor> tQ = parkTransform.row(1);
307
308 const MatrixFixedSize<3, 2> inverseParkTransform =
309 getInverseParkTransformMatrix(thetaPLL);
310 const MatrixFixedSize<3, 1> sD = inverseParkTransform.col(0);
311 const MatrixFixedSize<3, 1> sQ = inverseParkTransform.col(1);
312
313 // Exact derivatives of the orthonormal Park transformation used by the
314 // reference model.
315 const Eigen::Matrix<Real, 1, 3, Eigen::RowMajor> dTdTheta = tQ;
316 const Eigen::Matrix<Real, 1, 3, Eigen::RowMajor> dTqTheta = -tD;
317 const MatrixFixedSize<3, 1> dSdTheta = sQ;
318 const MatrixFixedSize<3, 1> dSqTheta = -sD;
319
320 const MatrixFixedSize<3, 1> iGridAbc = (vcAbc - u) / mRc;
321
322 const Real vcD = (tD * vcAbc)(0, 0);
323 const Real vcQ = (tQ * vcAbc)(0, 0);
324 const Real iGridD = (tD * iGridAbc)(0, 0);
325 const Real iGridQ = (tQ * iGridAbc)(0, 0);
326
327 const Eigen::Matrix<Real, 1, 3, Eigen::RowMajor> dVcDByVc = tD;
328 const Eigen::Matrix<Real, 1, 3, Eigen::RowMajor> dVcQByVc = tQ;
329 const Eigen::Matrix<Real, 1, 3, Eigen::RowMajor> dIGridDByVc = tD / mRc;
330 const Eigen::Matrix<Real, 1, 3, Eigen::RowMajor> dIGridQByVc = tQ / mRc;
331 const Eigen::Matrix<Real, 1, 3, Eigen::RowMajor> dIGridDByU = -tD / mRc;
332 const Eigen::Matrix<Real, 1, 3, Eigen::RowMajor> dIGridQByU = -tQ / mRc;
333
334 const Real dVcDByTheta = (dTdTheta * vcAbc)(0, 0);
335 const Real dVcQByTheta = (dTqTheta * vcAbc)(0, 0);
336 const Real dIGridDByTheta = (dTdTheta * iGridAbc)(0, 0);
337 const Real dIGridQByTheta = (dTqTheta * iGridAbc)(0, 0);
338
339 // Instantaneous active-power Jacobian.
340 const Real dPByTheta = iGridD * dVcDByTheta + vcD * dIGridDByTheta +
341 iGridQ * dVcQByTheta + vcQ * dIGridQByTheta;
342 const Eigen::Matrix<Real, 1, 3, Eigen::RowMajor> dPByVc =
343 iGridD * dVcDByVc + vcD * dIGridDByVc + iGridQ * dVcQByVc +
344 vcQ * dIGridQByVc;
345 const Eigen::Matrix<Real, 1, 3, Eigen::RowMajor> dPByU =
346 vcD * dIGridDByU + vcQ * dIGridQByU;
347
348 // Instantaneous reactive-power Jacobian.
349 const Real dQByTheta = -iGridQ * dVcDByTheta - vcD * dIGridQByTheta +
350 iGridD * dVcQByTheta + vcQ * dIGridDByTheta;
351 const Eigen::Matrix<Real, 1, 3, Eigen::RowMajor> dQByVc =
352 -iGridQ * dVcDByVc - vcD * dIGridQByVc + iGridD * dVcQByVc +
353 vcQ * dIGridDByVc;
354 const Eigen::Matrix<Real, 1, 3, Eigen::RowMajor> dQByU =
355 -vcD * dIGridQByU + vcQ * dIGridDByU;
356
357 const Real currentReferenceD =
358 -mKpPowerCtrl * pFiltered + mKiPowerCtrl * phiD + mKpPowerCtrl * mPRef;
359 const Real currentReferenceQ =
360 mKpPowerCtrl * qFiltered + mKiPowerCtrl * phiQ - mKpPowerCtrl * mQRef;
361
362 const Real converterVoltageReferenceD = -mKpCurrCtrl * iGridD +
363 mKiCurrCtrl * gammaD +
364 mKpCurrCtrl * currentReferenceD;
365 const Real converterVoltageReferenceQ = -mKpCurrCtrl * iGridQ +
366 mKiCurrCtrl * gammaQ +
367 mKpCurrCtrl * currentReferenceQ;
368
369 const Real dVoltageReferenceDByTheta = -mKpCurrCtrl * dIGridDByTheta;
370 const Eigen::Matrix<Real, 1, 3, Eigen::RowMajor> dVoltageReferenceDByVc =
371 -mKpCurrCtrl * dIGridDByVc;
372 const Eigen::Matrix<Real, 1, 3, Eigen::RowMajor> dVoltageReferenceDByU =
373 -mKpCurrCtrl * dIGridDByU;
374
375 const Real dVoltageReferenceQByTheta = -mKpCurrCtrl * dIGridQByTheta;
376 const Eigen::Matrix<Real, 1, 3, Eigen::RowMajor> dVoltageReferenceQByVc =
377 -mKpCurrCtrl * dIGridQByVc;
378 const Eigen::Matrix<Real, 1, 3, Eigen::RowMajor> dVoltageReferenceQByU =
379 -mKpCurrCtrl * dIGridQByU;
380
381 MatrixFixedSize<3, 14> dConverterVoltageAbcByX =
382 MatrixFixedSize<3, 14>::Zero();
383 MatrixFixedSize<3, 3> dConverterVoltageAbcByU = MatrixFixedSize<3, 3>::Zero();
384
385 dConverterVoltageAbcByX.col(ThetaPLL) =
386 dSdTheta * converterVoltageReferenceD +
387 dSqTheta * converterVoltageReferenceQ + sD * dVoltageReferenceDByTheta +
388 sQ * dVoltageReferenceQByTheta;
389
390 dConverterVoltageAbcByX.col(PFiltered) += sD * (-mKpCurrCtrl * mKpPowerCtrl);
391 dConverterVoltageAbcByX.col(PhiD) += sD * (mKpCurrCtrl * mKiPowerCtrl);
392 dConverterVoltageAbcByX.col(GammaD) += sD * mKiCurrCtrl;
393
394 dConverterVoltageAbcByX.col(QFiltered) += sQ * (mKpCurrCtrl * mKpPowerCtrl);
395 dConverterVoltageAbcByX.col(PhiQ) += sQ * (mKpCurrCtrl * mKiPowerCtrl);
396 dConverterVoltageAbcByX.col(GammaQ) += sQ * mKiCurrCtrl;
397
398 dConverterVoltageAbcByX.block(0, VcA, 3, 3) +=
399 sD * dVoltageReferenceDByVc + sQ * dVoltageReferenceQByVc;
400
401 dConverterVoltageAbcByU =
402 sD * dVoltageReferenceDByU + sQ * dVoltageReferenceQByU;
403
404 A.setZero(mStateSize, mStateSize);
405 B.setZero(mStateSize, mInputSize);
406 C.setZero(mOutputSize, mStateSize);
407 D.setZero(mOutputSize, mInputSize);
408
409 // PLL rows.
410 A(ThetaPLL, ThetaPLL) = mKpPLL * dVcQByTheta;
411 A(ThetaPLL, PhiPLL) = mKiPLL;
412 A.block(ThetaPLL, VcA, 1, 3) = mKpPLL * dVcQByVc;
413
414 A(PhiPLL, ThetaPLL) = dVcQByTheta;
415 A.block(PhiPLL, VcA, 1, 3) = dVcQByVc;
416
417 // Power-filter rows.
418 A(PFiltered, ThetaPLL) = mOmegaCutoff * dPByTheta;
419 A(PFiltered, PFiltered) = -mOmegaCutoff;
420 A.block(PFiltered, VcA, 1, 3) = mOmegaCutoff * dPByVc;
421 B.block(PFiltered, 0, 1, 3) = mOmegaCutoff * dPByU;
422
423 A(QFiltered, ThetaPLL) = mOmegaCutoff * dQByTheta;
424 A(QFiltered, QFiltered) = -mOmegaCutoff;
425 A.block(QFiltered, VcA, 1, 3) = mOmegaCutoff * dQByVc;
426 B.block(QFiltered, 0, 1, 3) = mOmegaCutoff * dQByU;
427
428 // Outer power-loop integrators.
429 A(PhiD, PFiltered) = -1.0;
430 A(PhiQ, QFiltered) = 1.0;
431
432 // Inner current-loop integrators.
433 A(GammaD, PFiltered) = -mKpPowerCtrl;
434 A(GammaD, PhiD) = mKiPowerCtrl;
435 A(GammaD, ThetaPLL) = -dIGridDByTheta;
436 A.block(GammaD, VcA, 1, 3) = -dIGridDByVc;
437 B.block(GammaD, 0, 1, 3) = -dIGridDByU;
438
439 A(GammaQ, QFiltered) = mKpPowerCtrl;
440 A(GammaQ, PhiQ) = mKiPowerCtrl;
441 A(GammaQ, ThetaPLL) = -dIGridQByTheta;
442 A.block(GammaQ, VcA, 1, 3) = -dIGridQByVc;
443 B.block(GammaQ, 0, 1, 3) = -dIGridQByU;
444
445 // Electrical filter plant.
446 A.block(VcA, VcA, 3, 3) = -1.0 / (mCf * mRc) * identity3;
447 A.block(VcA, IfA, 3, 3) = 1.0 / mCf * identity3;
448 B.block(VcA, 0, 3, 3) = 1.0 / (mCf * mRc) * identity3;
449
450 A.block(IfA, 0, 3, mStateSize) = (1.0 / mLf) * dConverterVoltageAbcByX;
451 A.block(IfA, VcA, 3, 3) += -1.0 / mLf * identity3;
452 A.block(IfA, IfA, 3, 3) += -mRf / mLf * identity3;
453 B.block(IfA, 0, 3, 3) = (1.0 / mLf) * dConverterVoltageAbcByU;
454
455 // SSN output y = (u - vc) / Rc.
456 C.block(0, VcA, 3, 3) = -1.0 / mRc * identity3;
457 D = 1.0 / mRc * identity3;
458}
459
460void EMT::Ph3::SSN_GFL::buildStateSpaceModel(const Matrix &x, const Matrix &u,
461 Matrix &A, Matrix &B, Matrix &C,
462 Matrix &D, Matrix &E, Matrix &F) {
463 calculateAnalyticalJacobians(x, u, A, B, C, D);
464
465 evaluateStateDerivative(x, u, mStateDerivativeScratch);
466 evaluateOutput(x, u, mOutputScratch);
467
468 // Local affine offsets at the operating point.
469 E = mStateDerivativeScratch;
470 E.noalias() -= A * x;
471 E.noalias() -= B * u;
472 F = mOutputScratch;
473 F.noalias() -= C * x;
474 F.noalias() -= D * u;
475}
476
478 buildStateSpaceModel(**mX, **mIntfVoltage, mA, mB, mC, mD,
479 mStateOffsetScratch, mOutputOffsetScratch);
480
481 setStateOffset(mStateOffsetScratch);
482 setOutputOffset(mOutputOffsetScratch);
483
484 // The PLL and dq/abc transformations make the local affine state-space
485 // model time varying. The SSN equivalent is therefore rebuilt every step.
486 return true;
487}
488
490 const Matrix &x = **mX;
491
492 const Matrix parkTransform = getParkTransformMatrix(x(ThetaPLL, 0));
493 const Matrix vcAbc = x.block(VcA, 0, 3, 1);
494 const Matrix iGridAbc = (vcAbc - u) / mRc;
495
496 **mVcD = (parkTransform.row(0) * vcAbc)(0, 0);
497 **mVcQ = (parkTransform.row(1) * vcAbc)(0, 0);
498 **mIrcD = (parkTransform.row(0) * iGridAbc)(0, 0);
499 **mIrcQ = (parkTransform.row(1) * iGridAbc)(0, 0);
500
501 **mPInst = **mVcD * **mIrcD + **mVcQ * **mIrcQ;
502 **mQInst = -**mVcD * **mIrcQ + **mVcQ * **mIrcD;
503
504 **mOmegaPLL = mOmegaN + mKpPLL * **mVcQ + mKiPLL * x(PhiPLL, 0);
505}
506
508 if (!mParametersSet)
509 throw std::logic_error("setParameters() must be called before "
510 "initializeFromNodesAndTerminals().");
511
512 // Initialize EMT abc filter states and dq-frame controller states.
513 const Real omega = 2.0 * PI * frequency;
514 const Complex imaginaryUnit(0.0, 1.0);
515 const Complex powerReference(mPRef, mQRef);
516
517 const MatrixComp uPhasor = buildInitialInputFromNodes(frequency);
518
519 MatrixComp vcPhasor = uPhasor;
520 MatrixComp injectionCurrentPhasor = MatrixComp::Zero(3, 1);
521
522 for (Int iteration = 0; iteration < mInitializationMaxIterations;
523 ++iteration) {
524 const Complex vcA = vcPhasor(0, 0);
525
526 if (std::abs(vcA) < mInitializationTolerance) {
527 injectionCurrentPhasor.setZero();
528 break;
529 }
530
531 // Peak-valued phase phasors use S = 1.5*V_peak*conj(I_peak).
532 const Complex currentA = std::conj(powerReference / (1.5 * vcA));
533
534 MatrixComp nextInjectionCurrent(3, 1);
535 nextInjectionCurrent << currentA, currentA * SHIFT_TO_PHASE_B,
536 currentA * SHIFT_TO_PHASE_C;
537
538 const MatrixComp nextVcPhasor = uPhasor + mRc * nextInjectionCurrent;
539
540 injectionCurrentPhasor = nextInjectionCurrent;
541
542 if ((nextVcPhasor - vcPhasor).norm() < mInitializationTolerance) {
543 vcPhasor = nextVcPhasor;
544 break;
545 }
546
547 vcPhasor = nextVcPhasor;
548 }
549
550 const MatrixComp ifPhasor =
551 imaginaryUnit * omega * mCf * vcPhasor + injectionCurrentPhasor;
552 const MatrixComp converterVoltageReferencePhasor =
553 vcPhasor + (mRf + imaginaryUnit * omega * mLf) * ifPhasor;
554
555 const Matrix vcAbc0 = vcPhasor.real();
556 const Matrix ifAbc0 = ifPhasor.real();
557 const Matrix injectionCurrentAbc0 = injectionCurrentPhasor.real();
558 const Matrix converterVoltageReferenceAbc0 =
559 converterVoltageReferencePhasor.real();
560
561 const Real theta0 = std::arg(vcPhasor(0, 0));
562 const Matrix parkTransform = getParkTransformMatrix(theta0);
563
564 const Matrix vcDq0 = parkTransform * vcAbc0;
565 const Matrix injectionCurrentDq0 = parkTransform * injectionCurrentAbc0;
566 const Matrix converterVoltageReferenceDq0 =
567 parkTransform * converterVoltageReferenceAbc0;
568
569 const Real vcD0 = vcDq0(0, 0);
570 const Real vcQ0 = vcDq0(1, 0);
571 const Real iGridD0 = injectionCurrentDq0(0, 0);
572 const Real iGridQ0 = injectionCurrentDq0(1, 0);
573
574 const Real pInitial = vcD0 * iGridD0 + vcQ0 * iGridQ0;
575 const Real qInitial = -vcD0 * iGridQ0 + vcQ0 * iGridD0;
576
577 Matrix x0 = Matrix::Zero(mStateSize, 1);
578
579 x0(ThetaPLL, 0) = theta0;
580 x0(PhiPLL, 0) = (omega - mOmegaN) / mKiPLL;
581 x0(PFiltered, 0) = pInitial;
582 x0(QFiltered, 0) = qInitial;
583
584 x0(PhiD, 0) = (iGridD0 + mKpPowerCtrl * (pInitial - mPRef)) / mKiPowerCtrl;
585 x0(PhiQ, 0) = (iGridQ0 - mKpPowerCtrl * (qInitial - mQRef)) / mKiPowerCtrl;
586
587 const Real currentReferenceD = -mKpPowerCtrl * pInitial +
588 mKiPowerCtrl * x0(PhiD, 0) +
589 mKpPowerCtrl * mPRef;
590 const Real currentReferenceQ = mKpPowerCtrl * qInitial +
591 mKiPowerCtrl * x0(PhiQ, 0) -
592 mKpPowerCtrl * mQRef;
593
594 x0(GammaD, 0) = (converterVoltageReferenceDq0(0, 0) +
595 mKpCurrCtrl * (iGridD0 - currentReferenceD)) /
596 mKiCurrCtrl;
597 x0(GammaQ, 0) = (converterVoltageReferenceDq0(1, 0) +
598 mKpCurrCtrl * (iGridQ0 - currentReferenceQ)) /
599 mKiCurrCtrl;
600
601 x0.block(VcA, 0, 3, 1) = vcAbc0;
602 x0.block(IfA, 0, 3, 1) = ifAbc0;
603
604 **mX = x0;
605 **mIntfVoltage = uPhasor.real();
606 **mIntfCurrent = ((uPhasor - vcPhasor) / mRc).real();
607
608 // Update local continuous-time matrices at the initialized operating point.
611
612 SPDLOG_LOGGER_INFO(mSLog,
613 "\n--- SSN GFL phasor/dq initialization ---"
614 "\nInput u: {:s}"
615 "\nOutput y: {:s}"
616 "\nState x: {:s}"
617 "\nP/Q init: [{:.6e}, {:.6e}]"
618 "\nVc dq: [{:.6e}, {:.6e}]"
619 "\nIgrid dq: [{:.6e}, {:.6e}]"
620 "\n--- SSN GFL initialization finished ---",
623 Logger::matrixToString(**mX), pInitial, qInitial, vcD0,
624 vcQ0, iGridD0, iGridQ0);
625}
626
628
630 Matrix stateDerivative = Matrix::Zero(mStateSize, 1);
631 evaluateStateDerivative(**mX, **mIntfVoltage, stateDerivative);
632 return stateDerivative;
633}
634
636
Matrix getInterfaceVoltage() const
std::vector< String > getLocalStateNames() const override final
Bool updateComponentParameters() override final
Matrix getInterfaceCurrent() const
std::vector< SSNComp::LocalAbcStateBlock > getLocalAbcStateBlocks() const override final
void updateLogAttributes(const Matrix &u) const override final
Update derived attributes used for logging/inspection.
void setParameters(Real lf, Real cf, Real rf, Real rc, Real omegaN, Real kpPLL, Real kiPLL, Real omegaCutoff, Real pRef, Real qRef, Real kpPowerCtrl, Real kiPowerCtrl, Real kpCurrCtrl, Real kiCurrCtrl)
Configure the GFL filter, PLL, power loop and current loop.
SSN_GFL(String uid, String name, Logger::Level logLevel=Logger::Level::off)
void initializeFromNodesAndTerminals(Real frequency) override final
Initializes Component variables according to power flow data stored in Nodes.
Matrix getStateDerivative() const
TwoTerminalVTypeVariableSSNComp(String uid, String name, Logger::Level logLevel=Logger::Level::off)
const Attribute< Matrix >::Ptr mX
Definition EMT_SSNComp.h:48
void setParameters(const Matrix &A, const Matrix &B, const Matrix &C, const Matrix &D)
static constexpr Real mInitializationTolerance
static constexpr Int mInitializationMaxIterations
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
static bool isFinite(Real value)
const Attribute< MatrixVar< Real > >::Ptr mIntfCurrent
const Attribute< MatrixVar< Real > >::Ptr mIntfVoltage
bool mParametersSet
Flag indicating that parameters are set via setParameters() function.
Logger::Log mSLog
Component logger.
#define PI
Definition Definitions.h:43
#define SHIFT_TO_PHASE_C
Definition Definitions.h:47
#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
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
bool Bool
Definition Definitions.h:62
unsigned int UInt
Definition Definitions.h:58