DPsim
Loading...
Searching...
No Matches
DP_Ph3_AvVoltSourceInverterStateSpace.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
12namespace {
13const Real K23 = std::sqrt(2.0 / 3.0);
14const Real K32 = std::sqrt(1.5);
15} // namespace
16
18 String uid, String name, Logger::Level logLevel, Bool enableNegSeqControl)
19 : MixedVTypeVariableSSNComp(uid, name, enableNegSeqControl ? 10 : 8, 6,
20 logLevel),
21 mEnableNegSeqControl(enableNegSeqControl),
22 mVcD(mAttributes->create<Real>("vc_d")),
23 mVcQ(mAttributes->create<Real>("vc_q")),
24 mIrcD(mAttributes->create<Real>("irc_d")),
25 mIrcQ(mAttributes->create<Real>("irc_q")),
26 mPInst(mAttributes->create<Real>("p_inst")),
27 mQInst(mAttributes->create<Real>("q_inst")),
28 mOmegaPLL(mAttributes->create<Real>("omega_pll")),
29 mIrcNd(mAttributes->create<Real>("irc_n_d")),
30 mIrcNq(mAttributes->create<Real>("irc_n_q")) {
31 **mVcD = 0.0;
32 **mVcQ = 0.0;
33 **mIrcD = 0.0;
34 **mIrcQ = 0.0;
35 **mPInst = 0.0;
36 **mQInst = 0.0;
37 **mOmegaPLL = 0.0;
38 **mIrcNd = 0.0;
39 **mIrcNq = 0.0;
40}
41
43 Real lf, Real cf, Real rf, Real rc, Real omegaN, Real kpPLL, Real kiPLL,
44 Real omegaCutoff, Real pRef, Real qRef, Real kpPowerCtrl, Real kiPowerCtrl,
45 Real kpCurrCtrl, Real kiCurrCtrl, Real iRefNd, Real iRefNq) {
46 if (lf <= 0.0)
47 throw std::invalid_argument("Filter inductance lf must be positive.");
48
49 if (cf <= 0.0)
50 throw std::invalid_argument("Filter capacitance cf must be positive.");
51
52 if (rf < 0.0)
53 throw std::invalid_argument("Filter resistance rf must be non-negative.");
54
55 if (rc <= 0.0)
56 throw std::invalid_argument("Coupling resistance rc must be positive.");
57
58 if (omegaN <= 0.0)
59 throw std::invalid_argument(
60 "Nominal angular frequency omegaN must be positive.");
61
62 if (omegaCutoff < 0.0)
63 throw std::invalid_argument(
64 "Power-filter cutoff frequency omegaCutoff must be non-negative.");
65
66 if (kiPLL == 0.0)
67 throw std::invalid_argument("PLL integral gain kiPLL must be non-zero.");
68
69 if (kiPowerCtrl == 0.0)
70 throw std::invalid_argument(
71 "Power-control integral gain kiPowerCtrl must be non-zero.");
72
73 if (kiCurrCtrl == 0.0)
74 throw std::invalid_argument(
75 "Current-control integral gain kiCurrCtrl must be non-zero.");
76
77 mLf = lf;
78 mInvLf = 1.0 / lf;
79 mCf = cf;
80 mRf = rf;
81 mRc = rc;
82
83 mOmegaN = omegaN;
84 mKpPLL = kpPLL;
85 mKiPLL = kiPLL;
86
87 mOmegaCutoff = omegaCutoff;
88 mPRef = pRef;
89 mQRef = qRef;
90 mKpPowerCtrl = kpPowerCtrl;
91 mKiPowerCtrl = kiPowerCtrl;
92 mKpCurrCtrl = kpCurrCtrl;
93 mKiCurrCtrl = kiCurrCtrl;
94 mIRefNd = iRefNd;
95 mIRefNq = iRefNq;
96
97 const Matrix x0 = Matrix::Zero(stateSize(), 1);
98 const Matrix u0 = Matrix::Zero(6, 1);
99
100 Matrix aMatrix, bMatrix, cMatrix, dMatrix, eVector, fVector;
101 buildStateSpaceModel(x0, u0, aMatrix, bMatrix, cMatrix, dMatrix, eVector,
102 fVector);
103
104 MixedVTypeVariableSSNComp::setParameters(aMatrix, bMatrix, cMatrix, dMatrix,
105 eVector, fVector);
106}
107
108DP::Ph3::AvVoltSourceInverterStateSpace::NegSeqTerms
109DP::Ph3::AvVoltSourceInverterStateSpace::computeNegSeqTerms(
110 const Matrix &x, const Complex3 &vc, const Complex3 &uEnv,
111 const Complex3 &redistFactor, const Complex3 &projCoeff,
112 const Complex &expJPsi) const {
113 NegSeqTerms terms;
114
115 // Negative-sequence measurement from nV = vc_a + a^2*vc_b + a*vc_c.
116 Complex nV(0.0, 0.0);
117 Complex nU(0.0, 0.0);
118 for (Int p = 0; p < 3; ++p) {
119 nV += redistFactor[p] * vc[p];
120 nU += redistFactor[p] * uEnv[p];
121 }
122
123 const Complex nI = (nV - nU) / mRc;
124 terms.ircNDQ = 0.5 * K23 * expJPsi * std::conj(nI);
125
126 // hIrcN[p] is the conj(vc_p) coefficient of ircNDQ.
127 for (Int p = 0; p < 3; ++p)
128 terms.hIrcN[p] = 0.5 * K23 * expJPsi * projCoeff[p] / mRc;
129
130 const Real vRefNd = -mKpCurrCtrl * terms.ircNDQ.real() +
131 mKiCurrCtrl * x(GammaND, 0) + mKpCurrCtrl * mIRefNd;
132 const Real vRefNq = -mKpCurrCtrl * terms.ircNDQ.imag() +
133 mKiCurrCtrl * x(GammaNQ, 0) + mKpCurrCtrl * mIRefNq;
134 terms.vRefNDQ = Complex(vRefNd, vRefNq);
135 terms.vRefNEnv0 = K23 * std::conj(terms.vRefNDQ) * expJPsi;
136
137 return terms;
138}
139
140void DP::Ph3::AvVoltSourceInverterStateSpace::buildStateSpaceModel(
141 const Matrix &x, const Matrix &u, Matrix &A, Matrix &B, Matrix &C,
142 Matrix &D, Matrix &E, Matrix &F) const {
143 // Unpack the operating point. psi := theta0 - thetaN (never a state itself).
144 const Real psi = x(Psi, 0);
145 const Real phiPLL = x(PhiPLL, 0);
146 const Real pF = x(PFiltered, 0);
147 const Real qF = x(QFiltered, 0);
148 const Real phiD = x(PhiD, 0);
149 const Real phiQ = x(PhiQ, 0);
150 const Real gammaD = x(GammaD, 0);
151 const Real gammaQ = x(GammaQ, 0);
152
153 const Complex j(0.0, 1.0);
154
155 Complex3 vc;
156 Complex3 iF;
157 for (Int p = 0; p < 3; ++p) {
158 vc[p] = Complex(x(mVcReCol[p], 0), x(mVcImCol[p], 0));
159 iF[p] = Complex(x(mIfReCol[p], 0), x(mIfImCol[p], 0));
160 }
161 const Complex3 uEnv = {Complex(u(0, 0), u(1, 0)), Complex(u(2, 0), u(3, 0)),
162 Complex(u(4, 0), u(5, 0))};
163
164 // Positive-sequence projection coefficients and inverse-Park redistribution factors.
165 const Complex3 projCoeff = {Complex(1.0, 0.0), SHIFT_TO_PHASE_C,
167 const Complex3 redistFactor = {std::conj(projCoeff[0]),
168 std::conj(projCoeff[1]),
169 std::conj(projCoeff[2])};
170
171 const Complex rot = std::exp(-j * psi);
172 const Complex expJPsi = std::conj(rot);
173
174 // Positive-sequence dq measurements from pV = vc_a + a*vc_b + a^2*vc_c.
175 Complex pV(0.0, 0.0);
176 Complex pU(0.0, 0.0);
177 for (Int p = 0; p < 3; ++p) {
178 pV += projCoeff[p] * vc[p];
179 pU += projCoeff[p] * uEnv[p];
180 }
181 const Complex pI = (pV - pU) / mRc;
182
183 const Complex vcDQ = 0.5 * K23 * rot * pV;
184 const Complex ircDQ = 0.5 * K23 * rot * pI;
185 const Real vcD = vcDQ.real();
186 const Real vcQ = vcDQ.imag();
187 const Real ircD = ircDQ.real();
188 const Real ircQ = ircDQ.imag();
189
190 // gVc[p] = d(vcDQ)/d(vc_p); gIrcVc/gIrcU = d(ircDQ)/d(vc_p), d(ircDQ)/d(u_p).
191 Complex3 gVc;
192 Complex3 gIrcVc;
193 Complex3 gIrcU;
194 for (Int p = 0; p < 3; ++p) {
195 gVc[p] = 0.5 * K23 * rot * projCoeff[p];
196 gIrcVc[p] = gVc[p] / mRc;
197 gIrcU[p] = -gVc[p] / mRc;
198 }
199
200 // Power: p+jq = vcDQ*conj(ircDQ); psi-independent, so no Psi column below.
201 const Complex pq = vcDQ * std::conj(ircDQ);
202 const Real pInst = pq.real();
203 const Real qInst = pq.imag();
204
205 // Outer power control and inner current control, single dq frame.
206 const Real iRefD =
207 -mKpPowerCtrl * pF + mKiPowerCtrl * phiD + mKpPowerCtrl * mPRef;
208 const Real iRefQ =
209 mKpPowerCtrl * qF + mKiPowerCtrl * phiQ - mKpPowerCtrl * mQRef;
210 const Complex iRefDQ(iRefD, iRefQ);
211 const Complex gammaDQ(gammaD, gammaQ);
212
213 const Complex vRefDQ =
214 -mKpCurrCtrl * ircDQ + mKiCurrCtrl * gammaDQ + mKpCurrCtrl * iRefDQ;
215
216 // Positive-sequence bridge-voltage reference, distributed via the inverse Park.
217 const Complex vRefEnv0 = K23 * vRefDQ * expJPsi;
218
219 // Negative-sequence measurement + PI control (baseband +j*psi loop). Left at
220 // its zero default when the loop is off, which drops it out of everything below.
221 NegSeqTerms neg;
222 if (mEnableNegSeqControl)
223 neg = computeNegSeqTerms(x, vc, uEnv, redistFactor, projCoeff, expJPsi);
224
225 const Real ircND = neg.ircNDQ.real();
226 const Real ircNQ = neg.ircNDQ.imag();
227
228 // Total reference: positive (redistFactor) + negative (projCoeff) injection.
229 Complex3 vRef;
230 for (Int p = 0; p < 3; ++p)
231 vRef[p] = redistFactor[p] * vRefEnv0 + projCoeff[p] * neg.vRefNEnv0;
232
233 // RHS f(x,u) (x_dot = f(x,u)).
234 Complex3 vcDot;
235 Complex3 ifDot;
236 for (Int p = 0; p < 3; ++p) {
237 vcDot[p] =
238 iF[p] / mCf + (uEnv[p] - vc[p]) / (mCf * mRc) - j * mOmegaN * vc[p];
239 ifDot[p] = (vRef[p] - vc[p] - mRf * iF[p]) * mInvLf - j * mOmegaN * iF[p];
240 }
241
242 // E is the caller-owned, same-sized affine-offset workspace. Use it for the
243 // derivative first, then subtract the two Jacobian products in place below.
244 E.setZero(stateSize(), 1);
245 E(Psi, 0) = mKpPLL * vcQ + mKiPLL * phiPLL;
246 E(PhiPLL, 0) = vcQ;
247 E(PFiltered, 0) = mOmegaCutoff * (pInst - pF);
248 E(QFiltered, 0) = mOmegaCutoff * (qInst - qF);
249 E(PhiD, 0) = mPRef - pF;
250 E(PhiQ, 0) = qF - mQRef;
251 E(GammaD, 0) = iRefD - ircD;
252 E(GammaQ, 0) = iRefQ - ircQ;
253 if (mEnableNegSeqControl) {
254 E(GammaND, 0) = mIRefNd - ircND;
255 E(GammaNQ, 0) = mIRefNq - ircNQ;
256 }
257 for (Int p = 0; p < 3; ++p) {
258 E(mVcReCol[p], 0) = vcDot[p].real();
259 E(mVcImCol[p], 0) = vcDot[p].imag();
260 E(mIfReCol[p], 0) = ifDot[p].real();
261 E(mIfImCol[p], 0) = ifDot[p].imag();
262 }
263
264 // Analytic Jacobian A = df/dx, B = df/du.
265 A.setZero(stateSize(), stateSize());
266 B.setZero(stateSize(), 6);
267
268 // PLL rows: only vcQ feeds them; d(vcQ)/dpsi = -vcD (d(vcDQ)/dpsi = -j*vcDQ).
269 A(Psi, Psi) = mKpPLL * (-vcD);
270 A(Psi, PhiPLL) = mKiPLL;
271 A(PhiPLL, Psi) = -vcD;
272 for (Int p = 0; p < 3; ++p) {
273 const Real dVcQdRe = gVc[p].imag();
274 const Real dVcQdIm = gVc[p].real();
275 A(Psi, mVcReCol[p]) = mKpPLL * dVcQdRe;
276 A(Psi, mVcImCol[p]) = mKpPLL * dVcQdIm;
277 A(PhiPLL, mVcReCol[p]) = dVcQdRe;
278 A(PhiPLL, mVcImCol[p]) = dVcQdIm;
279 }
280
281 // Power-filter rows: d(p+jq)/dv = dvcDQ/dv*conj(ircDQ) + vcDQ*conj(dircDQ/dv). No Psi column.
282 A(PFiltered, PFiltered) = -mOmegaCutoff;
283 A(QFiltered, QFiltered) = -mOmegaCutoff;
284 for (Int p = 0; p < 3; ++p) {
285 const Complex dpqVcRe =
286 gVc[p] * std::conj(ircDQ) + vcDQ * std::conj(gIrcVc[p]);
287 const Complex dpqVcIm =
288 (j * gVc[p]) * std::conj(ircDQ) + vcDQ * std::conj(j * gIrcVc[p]);
289 A(PFiltered, mVcReCol[p]) = mOmegaCutoff * dpqVcRe.real();
290 A(QFiltered, mVcReCol[p]) = mOmegaCutoff * dpqVcRe.imag();
291 A(PFiltered, mVcImCol[p]) = mOmegaCutoff * dpqVcIm.real();
292 A(QFiltered, mVcImCol[p]) = mOmegaCutoff * dpqVcIm.imag();
293
294 const Complex dpqURe = vcDQ * std::conj(gIrcU[p]);
295 const Complex dpqUIm = vcDQ * std::conj(j * gIrcU[p]);
296 B(PFiltered, mUReCol[p]) = mOmegaCutoff * dpqURe.real();
297 B(QFiltered, mUReCol[p]) = mOmegaCutoff * dpqURe.imag();
298 B(PFiltered, mUImCol[p]) = mOmegaCutoff * dpqUIm.real();
299 B(QFiltered, mUImCol[p]) = mOmegaCutoff * dpqUIm.imag();
300 }
301
302 A(PhiD, PFiltered) = -1.0;
303 A(PhiQ, QFiltered) = 1.0;
304
305 // Current-control integrators: gammaD_dot = iRefD - ircD, etc.
306 A(GammaD, PFiltered) = -mKpPowerCtrl;
307 A(GammaD, PhiD) = mKiPowerCtrl;
308 A(GammaD, Psi) = -ircQ;
309 A(GammaQ, QFiltered) = mKpPowerCtrl;
310 A(GammaQ, PhiQ) = mKiPowerCtrl;
311 A(GammaQ, Psi) = ircD;
312 for (Int p = 0; p < 3; ++p) {
313 const Real dIrcDdVcRe = gIrcVc[p].real();
314 const Real dIrcDdVcIm = -gIrcVc[p].imag();
315 const Real dIrcQdVcRe = gIrcVc[p].imag();
316 const Real dIrcQdVcIm = gIrcVc[p].real();
317 A(GammaD, mVcReCol[p]) = -dIrcDdVcRe;
318 A(GammaD, mVcImCol[p]) = -dIrcDdVcIm;
319 A(GammaQ, mVcReCol[p]) = -dIrcQdVcRe;
320 A(GammaQ, mVcImCol[p]) = -dIrcQdVcIm;
321
322 const Real dIrcDdURe = gIrcU[p].real();
323 const Real dIrcDdUIm = -gIrcU[p].imag();
324 const Real dIrcQdURe = gIrcU[p].imag();
325 const Real dIrcQdUIm = gIrcU[p].real();
326 B(GammaD, mUReCol[p]) = -dIrcDdURe;
327 B(GammaD, mUImCol[p]) = -dIrcDdUIm;
328 B(GammaQ, mUReCol[p]) = -dIrcQdURe;
329 B(GammaQ, mUImCol[p]) = -dIrcQdUIm;
330 }
331
332 // Negative-sequence current integrators: gammaND_dot = iRefNd - ircND, etc.
333 // psi enters via rotN = e^{+j*psi}: d(ircND)/dpsi = -ircNQ, d(ircNQ)/dpsi = ircND.
334 if (mEnableNegSeqControl) {
335 A(GammaND, Psi) = ircNQ;
336 A(GammaNQ, Psi) = -ircND;
337 for (Int p = 0; p < 3; ++p) {
338 A(GammaND, mVcReCol[p]) = -neg.hIrcN[p].real();
339 A(GammaND, mVcImCol[p]) = -neg.hIrcN[p].imag();
340 A(GammaNQ, mVcReCol[p]) = -neg.hIrcN[p].imag();
341 A(GammaNQ, mVcImCol[p]) = neg.hIrcN[p].real();
342 B(GammaND, mUReCol[p]) = neg.hIrcN[p].real();
343 B(GammaND, mUImCol[p]) = neg.hIrcN[p].imag();
344 B(GammaNQ, mUReCol[p]) = neg.hIrcN[p].imag();
345 B(GammaNQ, mUImCol[p]) = -neg.hIrcN[p].real();
346 }
347 }
348
349 // Filter capacitor rows (Vc_dot), fully decoupled per phase.
350 for (Int p = 0; p < 3; ++p) {
351 const Int reRow = mVcReCol[p];
352 const Int imRow = mVcImCol[p];
353 A(reRow, reRow) = -1.0 / (mCf * mRc);
354 A(reRow, imRow) = mOmegaN;
355 A(imRow, reRow) = -mOmegaN;
356 A(imRow, imRow) = -1.0 / (mCf * mRc);
357 A(reRow, mIfReCol[p]) = 1.0 / mCf;
358 A(imRow, mIfImCol[p]) = 1.0 / mCf;
359 B(reRow, mUReCol[p]) = 1.0 / (mCf * mRc);
360 B(imRow, mUImCol[p]) = 1.0 / (mCf * mRc);
361 }
362
363 // Filter inductor rows (If_dot): the per-phase coupling via vRef_p = redistFactor[p]*vRefEnv0.
364 RefSensitivities sens;
365
366 const Complex dVRefEnv0DPsi =
367 j * K23 * expJPsi * (mKpCurrCtrl * ircDQ + vRefDQ);
368
369 // d(vRefDQ)/dOwnVar for the six own-frame control states.
370 const Complex dVRefEnv0DpF = K23 * expJPsi * (mKpCurrCtrl * (-mKpPowerCtrl));
371 const Complex dVRefEnv0DqF = K23 * expJPsi * j * (mKpCurrCtrl * mKpPowerCtrl);
372 const Complex dVRefEnv0DPhiD = K23 * expJPsi * (mKpCurrCtrl * mKiPowerCtrl);
373 const Complex dVRefEnv0DPhiQ =
374 K23 * expJPsi * j * (mKpCurrCtrl * mKiPowerCtrl);
375 const Complex dVRefEnv0DGammaD = K23 * expJPsi * mKiCurrCtrl;
376 const Complex dVRefEnv0DGammaQ = K23 * expJPsi * j * mKiCurrCtrl;
377
378 sens.posOwn = {dVRefEnv0DPsi, dVRefEnv0DpF, dVRefEnv0DqF,
379 dVRefEnv0DPhiD, dVRefEnv0DPhiQ, dVRefEnv0DGammaD,
380 dVRefEnv0DGammaQ};
381
382 // d(vRefDQ)/d(vc_p), d(vRefDQ)/d(u_p) via vRefDQ's -kpCurrCtrl*ircDQ term.
383 for (Int p = 0; p < 3; ++p) {
384 sens.posVcRe[p] = K23 * expJPsi * (-mKpCurrCtrl * gIrcVc[p]);
385 sens.posVcIm[p] = K23 * expJPsi * (-mKpCurrCtrl * j * gIrcVc[p]);
386 sens.posURe[p] = K23 * expJPsi * (-mKpCurrCtrl * gIrcU[p]);
387 sens.posUIm[p] = K23 * expJPsi * (-mKpCurrCtrl * j * gIrcU[p]);
388 }
389
390 // Derivatives of vRefNEnv0 = K23*conj(vRefNDQ)*expJPsi wrt psi, the negative-loop
391 // states and vc_p/u_p.
392 if (mEnableNegSeqControl) {
393 sens.negPsi =
394 j * K23 * expJPsi *
395 (mKpCurrCtrl * std::conj(neg.ircNDQ) + std::conj(neg.vRefNDQ));
396 sens.negGammaND = K23 * expJPsi * mKiCurrCtrl;
397 sens.negGammaNQ = -j * K23 * expJPsi * mKiCurrCtrl;
398 for (Int p = 0; p < 3; ++p) {
399 const Complex base =
400 mKpCurrCtrl * std::conj(neg.hIrcN[p]) * K23 * expJPsi;
401 sens.negVcRe[p] = -base;
402 sens.negVcIm[p] = -j * base;
403 sens.negURe[p] = base;
404 sens.negUIm[p] = j * base;
405 }
406 }
407
408 buildInductorRows(redistFactor, projCoeff, sens, A, B);
409
410 // Offset E = f(x,u) - A*x - B*u.
411 E.noalias() -= A * x;
412 E.noalias() -= B * u;
413
414 // SSN output: y_p = (u_p - vc_p)/Rc (exact, no relinearization needed).
415 C.setZero(6, stateSize());
416 for (Int p = 0; p < 3; ++p) {
417 C(2 * p, mVcReCol[p]) = -1.0 / mRc;
418 C(2 * p + 1, mVcImCol[p]) = -1.0 / mRc;
419 }
420
421 D.setZero(6, 6);
422 for (Int p = 0; p < 3; ++p) {
423 D(2 * p, mUReCol[p]) = 1.0 / mRc;
424 D(2 * p + 1, mUImCol[p]) = 1.0 / mRc;
425 }
426
427 F.setZero(6, 1);
428}
429
430void DP::Ph3::AvVoltSourceInverterStateSpace::buildInductorRows(
431 const Complex3 &redistFactor, const Complex3 &projCoeff,
432 const RefSensitivities &sens, Matrix &A, Matrix &B) const {
433 for (Int pOut = 0; pOut < 3; ++pOut) {
434 const Int reRow = mIfReCol[pOut];
435 const Int imRow = mIfImCol[pOut];
436
437 // Own-phase direct terms: -(vc_p + Rf*iF_p)/Lf - j*omegaN*iF_p.
438 A(reRow, mVcReCol[pOut]) = -mInvLf;
439 A(imRow, mVcImCol[pOut]) = -mInvLf;
440 A(reRow, reRow) = -mRf * mInvLf;
441 A(imRow, imRow) = -mRf * mInvLf;
442 A(reRow, imRow) = mOmegaN;
443 A(imRow, reRow) = -mOmegaN;
444
445 // vRef_p coupling through the shared single-dq-frame control chain.
446 for (Int k = 0; k < 7; ++k) {
447 const Complex dVRef = redistFactor[pOut] * sens.posOwn[k];
448 A(reRow, mOwnCol[k]) += dVRef.real() * mInvLf;
449 A(imRow, mOwnCol[k]) += dVRef.imag() * mInvLf;
450 }
451 for (Int pSrc = 0; pSrc < 3; ++pSrc) {
452 const Complex dVRefVcRe = redistFactor[pOut] * sens.posVcRe[pSrc];
453 const Complex dVRefVcIm = redistFactor[pOut] * sens.posVcIm[pSrc];
454 A(reRow, mVcReCol[pSrc]) += dVRefVcRe.real() * mInvLf;
455 A(imRow, mVcReCol[pSrc]) += dVRefVcRe.imag() * mInvLf;
456 A(reRow, mVcImCol[pSrc]) += dVRefVcIm.real() * mInvLf;
457 A(imRow, mVcImCol[pSrc]) += dVRefVcIm.imag() * mInvLf;
458
459 const Complex dVRefURe = redistFactor[pOut] * sens.posURe[pSrc];
460 const Complex dVRefUIm = redistFactor[pOut] * sens.posUIm[pSrc];
461 B(reRow, mUReCol[pSrc]) += dVRefURe.real() * mInvLf;
462 B(imRow, mUReCol[pSrc]) += dVRefURe.imag() * mInvLf;
463 B(reRow, mUImCol[pSrc]) += dVRefUIm.real() * mInvLf;
464 B(imRow, mUImCol[pSrc]) += dVRefUIm.imag() * mInvLf;
465 }
466
467 // Negative-sequence injection coupling: vRef_p += projCoeff[pOut]*vRefNEnv0.
468 // The sensitivities stay zero when the loop is off, so this is a no-op then.
469 const Complex dNPsi = projCoeff[pOut] * sens.negPsi;
470 A(reRow, Psi) += dNPsi.real() * mInvLf;
471 A(imRow, Psi) += dNPsi.imag() * mInvLf;
472 if (mEnableNegSeqControl) {
473 const Complex dNGammaND = projCoeff[pOut] * sens.negGammaND;
474 A(reRow, GammaND) += dNGammaND.real() * mInvLf;
475 A(imRow, GammaND) += dNGammaND.imag() * mInvLf;
476 const Complex dNGammaNQ = projCoeff[pOut] * sens.negGammaNQ;
477 A(reRow, GammaNQ) += dNGammaNQ.real() * mInvLf;
478 A(imRow, GammaNQ) += dNGammaNQ.imag() * mInvLf;
479 }
480 for (Int pSrc = 0; pSrc < 3; ++pSrc) {
481 const Complex dNVcRe = projCoeff[pOut] * sens.negVcRe[pSrc];
482 const Complex dNVcIm = projCoeff[pOut] * sens.negVcIm[pSrc];
483 A(reRow, mVcReCol[pSrc]) += dNVcRe.real() * mInvLf;
484 A(imRow, mVcReCol[pSrc]) += dNVcRe.imag() * mInvLf;
485 A(reRow, mVcImCol[pSrc]) += dNVcIm.real() * mInvLf;
486 A(imRow, mVcImCol[pSrc]) += dNVcIm.imag() * mInvLf;
487
488 const Complex dNURe = projCoeff[pOut] * sens.negURe[pSrc];
489 const Complex dNUIm = projCoeff[pOut] * sens.negUIm[pSrc];
490 B(reRow, mUReCol[pSrc]) += dNURe.real() * mInvLf;
491 B(imRow, mUReCol[pSrc]) += dNURe.imag() * mInvLf;
492 B(reRow, mUImCol[pSrc]) += dNUIm.real() * mInvLf;
493 B(imRow, mUImCol[pSrc]) += dNUIm.imag() * mInvLf;
494 }
495 }
496}
497
499 // Relinearized every step (mirrors EMT/Ph1); change-check intentionally skipped.
500 buildStateSpaceModel(**mX, packComplex((**inputAttribute())), mA, mB, mC, mD,
501 mParameterStateOffset, mParameterOutputOffset);
502
503 setStateOffset(mParameterStateOffset);
504 setOutputOffset(mParameterOutputOffset);
505
506 return true;
507}
508
510 const Matrix &u) const {
511 const Matrix &x = **mX;
512
513 const Real psi = x(Psi, 0);
514 const Complex rot = std::exp(Complex(0.0, -psi));
515 const Complex expJPsi = std::conj(rot);
516 const Complex3 projCoeff = {Complex(1.0, 0.0), SHIFT_TO_PHASE_C,
518
519 Complex pV(0.0, 0.0);
520 Complex pU(0.0, 0.0);
521 Complex nV(0.0, 0.0);
522 Complex nU(0.0, 0.0);
523 for (Int p = 0; p < 3; ++p) {
524 const Complex vc(x(mVcReCol[p], 0), x(mVcImCol[p], 0));
525 const Complex uEnv(u(mUReCol[p], 0), u(mUImCol[p], 0));
526 pV += projCoeff[p] * vc;
527 pU += projCoeff[p] * uEnv;
528 nV += std::conj(projCoeff[p]) * vc;
529 nU += std::conj(projCoeff[p]) * uEnv;
530 }
531 const Complex pI = (pV - pU) / mRc;
532
533 const Complex vcDQ = 0.5 * K23 * rot * pV;
534 const Complex ircDQ = 0.5 * K23 * rot * pI;
535
536 **mVcD = vcDQ.real();
537 **mVcQ = vcDQ.imag();
538 **mIrcD = ircDQ.real();
539 **mIrcQ = ircDQ.imag();
540 if (mEnableNegSeqControl) {
541 const Complex nI = (nV - nU) / mRc;
542 const Complex ircNDQ = 0.5 * K23 * expJPsi * std::conj(nI);
543 **mIrcNd = ircNDQ.real();
544 **mIrcNq = ircNDQ.imag();
545 }
546
547 **mPInst = **mVcD * **mIrcD + **mVcQ * **mIrcQ;
548 **mQInst = -**mVcD * **mIrcQ + **mVcQ * **mIrcD;
549
550 **mOmegaPLL = mOmegaN + mKpPLL * **mVcQ + mKiPLL * x(PhiPLL, 0);
551}
552
554 Real frequency) {
555 if (!mParametersSet)
556 throw std::logic_error("setParameters() must be called before "
557 "initializeFromNodesAndTerminals().");
558
559 // Balanced init: solve the phase-a operating point, replicate across phases.
560 const Real omega = 2.0 * PI * frequency;
561 const Complex j(0.0, 1.0);
562 const Complex powerRef(mPRef, mQRef);
563
564 const MatrixComp uInit = buildInitialInputFromNodes(frequency);
565 const Complex ua = uInit(0, 0);
566
567 Complex vc = ua;
568 Complex irc(0.0, 0.0);
569
570 for (Int iter = 0; iter < mInitializationMaxIterations; ++iter) {
571 if (std::abs(vc) < mInitializationTolerance) {
572 irc = Complex(0.0, 0.0);
573 break;
574 }
575
576 const Complex iNext = std::conj(powerRef / (1.5 * vc));
577 const Complex vcNext = ua + mRc * iNext;
578
579 irc = iNext;
580
581 if (std::abs(vcNext - vc) < mInitializationTolerance) {
582 vc = vcNext;
583 break;
584 }
585
586 vc = vcNext;
587 }
588
589 const Complex ifCurrent = j * omega * mCf * vc + irc;
590 const Complex vRef = vc + (mRf + j * omega * mLf) * ifCurrent;
591
592 // thetaN(0) = 0, so psi0 = theta0 exactly.
593 const Real psi0 = std::arg(vc);
594 const Complex rot0 = std::exp(-j * psi0);
595
596 const Complex vcDQ = K32 * vc * rot0;
597 const Real vcD = vcDQ.real();
598 const Real vcQ = vcDQ.imag();
599 const Complex ircDQ = K32 * irc * rot0;
600 const Real ircD = ircDQ.real();
601 const Real ircQ = ircDQ.imag();
602
603 const Real pInit = vcD * ircD + vcQ * ircQ;
604 const Real qInit = -vcD * ircQ + vcQ * ircD;
605
606 const Real phiPLL0 = (omega - mOmegaN) / mKiPLL;
607 const Real phiD0 = (ircD + mKpPowerCtrl * (pInit - mPRef)) / mKiPowerCtrl;
608 const Real phiQ0 = (ircQ - mKpPowerCtrl * (qInit - mQRef)) / mKiPowerCtrl;
609
610 const Real iRefD0 =
611 -mKpPowerCtrl * pInit + mKiPowerCtrl * phiD0 + mKpPowerCtrl * mPRef;
612 const Real iRefQ0 =
613 mKpPowerCtrl * qInit + mKiPowerCtrl * phiQ0 - mKpPowerCtrl * mQRef;
614
615 const Complex vRefDQ0 = K32 * vRef * rot0;
616 const Real gammaD0 =
617 (vRefDQ0.real() + mKpCurrCtrl * (ircD - iRefD0)) / mKiCurrCtrl;
618 const Real gammaQ0 =
619 (vRefDQ0.imag() + mKpCurrCtrl * (ircQ - iRefQ0)) / mKiCurrCtrl;
620
622 const MatrixComp ifAbc = Math::singlePhaseVariableToThreePhase(ifCurrent);
623
624 Matrix x0 = Matrix::Zero(stateSize(), 1);
625 x0(Psi, 0) = psi0;
626 x0(PhiPLL, 0) = phiPLL0;
627 x0(PFiltered, 0) = pInit;
628 x0(QFiltered, 0) = qInit;
629 x0(PhiD, 0) = phiD0;
630 x0(PhiQ, 0) = phiQ0;
631 x0(GammaD, 0) = gammaD0;
632 x0(GammaQ, 0) = gammaQ0;
633 // Balanced start: the negative-loop integrators stay at their Zero() seed.
634
635 for (Int p = 0; p < 3; ++p) {
636 x0(mVcReCol[p], 0) = vcAbc(p, 0).real();
637 x0(mVcImCol[p], 0) = vcAbc(p, 0).imag();
638 x0(mIfReCol[p], 0) = ifAbc(p, 0).real();
639 x0(mIfImCol[p], 0) = ifAbc(p, 0).imag();
640 }
641
642 **mX = x0;
643 **mIntfVoltage = uInit;
644 (**mIntfCurrent)(0, 0) = (ua - vc) / mRc;
645 (**mIntfCurrent)(1, 0) = (uInit(1, 0) - vcAbc(1, 0)) / mRc;
646 (**mIntfCurrent)(2, 0) = (uInit(2, 0) - vcAbc(2, 0)) / mRc;
647
650
651 SPDLOG_LOGGER_INFO(mSLog,
652 "\n--- Inverter SSN mixed real+per-phase-complex "
653 "initialization ---"
654 "\nInput u: {:s}"
655 "\nOutput y: {:s}"
656 "\nState x: {:s}"
657 "\nP/Q init: [{:.6e}, {:.6e}]"
658 "\nVc dq (phase a): [{:.6e}, {:.6e}]"
659 "\nIinj dq (phase a): [{:.6e}, {:.6e}]"
660 "\n--- Initialization finished ---",
663 Logger::matrixToString(**mX), pInit, qInit, vcD, vcQ, ircD,
664 ircQ);
665}
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, Real iRefNd=0.0, Real iRefNq=0.0)
void initializeFromNodesAndTerminals(Real frequency) override
Initializes Component variables according to power flow data stored in Nodes.
AvVoltSourceInverterStateSpace(String uid, String name, Logger::Level logLevel=Logger::Level::off, Bool enableNegSeqControl=false)
Bool updateComponentParameters() override final
Rebuild A/B/C/D/E/F from the current state/input; returns true if the stamp changed.
Int stateSize() const
Total packed real state size: realStateCount + 2*complexStateCount.
void setParameters(const Matrix &A, const Matrix &B, const Matrix &C, const Matrix &D)
static Matrix packComplex(const MatrixComp &c)
Pack an m-vector of complex into a 2m real vector [Re0,Im0,Re1,Im1,...].
virtual MatrixComp buildInitialInputFromNodes(Real frequency)
Default: balanced envelope from v_terminal1 - v_terminal0.
Matrix mA
Continuous-time real model over the packed state and packed [Re,Im] input/output.
MixedVTypeVariableSSNComp(String uid, String name, Int realStateCount, Int complexStateCount, Logger::Level logLevel=Logger::Level::off)
String uid()
Returns unique id.
AttributeList::Ptr mAttributes
Attribute List.
spdlog::level::level_enum Level
Definition Logger.h:33
static String matrixCompToString(const MatrixComp &mat)
Definition Logger.cpp:37
static String matrixToString(const Matrix &mat)
Definition Logger.cpp:31
static MatrixComp singlePhaseVariableToThreePhase(Complex var_1ph)
To convert single phase complex variables (voltages, currents) to symmetrical three phase ones.
const Attribute< MatrixVar< Complex > >::Ptr mIntfCurrent
const Attribute< MatrixVar< Complex > >::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
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