DPsim
Loading...
Searching...
No Matches
EMT_Ph3_SSN_GFM.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 <algorithm>
5#include <cmath>
6#include <stdexcept>
7
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), mNominalVoltage(0.0), mPRef(0.0),
15 mQRef(0.0), mVirtualInertia(0.0), mDampingCoefficient(0.0),
16 mVoltageDroopGain(0.0), mReactiveIntegralGain(0.0), mKpVoltage(0.0),
17 mKiVoltage(0.0), mKpCurrent(0.0), mKiCurrent(0.0),
18 mActiveDampingGain(0.0), mPowerFilterCutoff(0.0), mDelayBandwidth(0.0),
19 mVirtualResistance(0.0), mVirtualReactance(0.0),
20 mGridCurrentFeedforward(1.0), mReactivePowerDroop(0.0),
21 mReactiveDroopCutoff(0.0), mVoltageSetpoint(0.0),
22 mJacobianRelativeStep(1e-6), mJacobianAbsoluteStep(1e-8),
23 mPInst(mAttributes->create<Real>("p_inst")),
24 mQInst(mAttributes->create<Real>("q_inst")),
25 mOmegaGFM(mAttributes->create<Real>("omega_gfm")),
26 mThetaGFM(mAttributes->create<Real>("theta_gfm")),
27 mVoltageMagnitudeGFM(mAttributes->create<Real>("voltage_magnitude_gfm")),
28 mVcD(mAttributes->create<Real>("vc_d")),
29 mVcQ(mAttributes->create<Real>("vc_q")),
30 mIGridD(mAttributes->create<Real>("i_grid_d")),
31 mIGridQ(mAttributes->create<Real>("i_grid_q")),
32 mIfD(mAttributes->create<Real>("if_d")),
33 mIfQ(mAttributes->create<Real>("if_q")),
34 mVoltageReferenceD(mAttributes->create<Real>("v_ref_d")),
35 mVoltageReferenceQ(mAttributes->create<Real>("v_ref_q")) {
36
37 **mIntfVoltage = Matrix::Zero(3, 1);
38 **mIntfCurrent = Matrix::Zero(3, 1);
39
40 **mPInst = 0.0;
41 **mQInst = 0.0;
42 **mOmegaGFM = 0.0;
43 **mThetaGFM = 0.0;
44 **mVoltageMagnitudeGFM = 0.0;
45
46 **mVcD = 0.0;
47 **mVcQ = 0.0;
48
49 **mIGridD = 0.0;
50 **mIGridQ = 0.0;
51
52 **mIfD = 0.0;
53 **mIfQ = 0.0;
54
55 **mVoltageReferenceD = 0.0;
56 **mVoltageReferenceQ = 0.0;
57
58 const std::vector<String> stateAttributes = {"state_p_filtered_W",
59 "state_q_filtered_var",
60 "state_omega_rad_per_s",
61 "state_theta_rad",
62 "state_voltage_magnitude_V",
63 "state_voltage_integrator_d_Vs",
64 "state_voltage_integrator_q_Vs",
65 "state_current_integrator_d_As",
66 "state_current_integrator_q_As",
67 "state_delay_voltage_d_V",
68 "state_delay_voltage_q_V",
69 "state_capacitor_voltage_a_V",
70 "state_capacitor_voltage_b_V",
71 "state_capacitor_voltage_c_V",
72 "state_filter_current_a_A",
73 "state_filter_current_b_A",
74 "state_filter_current_c_A"};
75 mStateLogAttributes.reserve(stateAttributes.size());
76 for (const auto &attributeName : stateAttributes)
77 mStateLogAttributes.push_back(mAttributes->create<Real>(attributeName));
78}
79std::vector<String> EMT::Ph3::SSN_GFM::getLocalStateNames() const {
80 return {
81 "p_filtered",
82 "q_filtered",
83 "omega",
84 "theta",
85 "voltage_magnitude",
86
87 "voltage_integrator_d",
88 "voltage_integrator_q",
89
90 "current_integrator_d",
91 "current_integrator_q",
92
93 "delay_voltage_d",
94 "delay_voltage_q",
95
96 "vc_a",
97 "vc_b",
98 "vc_c",
99
100 "if_a",
101 "if_b",
102 "if_c",
103 };
104}
105
106std::vector<EMT::SSNComp::LocalAbcStateBlock>
108 return {
109 {{static_cast<Int>(VcA), static_cast<Int>(VcB), static_cast<Int>(VcC)},
110 "vc"},
111
112 {{static_cast<Int>(IfA), static_cast<Int>(IfB), static_cast<Int>(IfC)},
113 "if"},
114 };
115}
116
118 Real lf, Real cf, Real rf, Real rc, Real nominalVoltage, Real omegaN,
119 Real pRef, Real qRef, Real virtualInertia, Real dampingCoefficient,
120 Real voltageDroopGain, Real reactiveIntegralGain, Real kpVoltage,
121 Real kiVoltage, Real kpCurrent, Real kiCurrent, Real activeDampingGain,
122 Real powerFilterCutoff, Real delayBandwidth) {
123
124 if (lf <= 0.0)
125 throw std::invalid_argument("Filter inductance lf must be positive.");
126
127 if (cf <= 0.0)
128 throw std::invalid_argument("Filter capacitance cf must be positive.");
129
130 if (rf < 0.0)
131 throw std::invalid_argument("Filter resistance rf must be non-negative.");
132
133 if (rc <= 0.0)
134 throw std::invalid_argument("Coupling resistance rc must be positive.");
135
136 if (nominalVoltage <= 0.0)
137 throw std::invalid_argument("Nominal voltage must be positive.");
138
139 if (omegaN <= 0.0)
140 throw std::invalid_argument(
141 "Nominal angular frequency omegaN must be positive.");
142
143 if (virtualInertia <= 0.0)
144 throw std::invalid_argument("Virtual inertia must be positive.");
145
146 if (dampingCoefficient < 0.0)
147 throw std::invalid_argument("Damping coefficient must be non-negative.");
148
149 if (kiVoltage == 0.0)
150 throw std::invalid_argument(
151 "Voltage-controller integral gain must be non-zero.");
152
153 if (kiCurrent == 0.0)
154 throw std::invalid_argument(
155 "Current-controller integral gain must be non-zero.");
156
157 if (powerFilterCutoff < 0.0)
158 throw std::invalid_argument(
159 "Power-filter cutoff frequency must be non-negative.");
160
161 if (delayBandwidth <= 0.0)
162 throw std::invalid_argument("Delay bandwidth must be positive.");
163
164 mLf = lf;
165 mCf = cf;
166 mRf = rf;
167 mRc = rc;
168
169 mNominalVoltage = nominalVoltage;
170 mOmegaN = omegaN;
171
172 mPRef = pRef;
173 mQRef = qRef;
174
175 mVirtualInertia = virtualInertia;
176 mDampingCoefficient = dampingCoefficient;
177 mVoltageDroopGain = voltageDroopGain;
178 mReactiveIntegralGain = reactiveIntegralGain;
179
180 mKpVoltage = kpVoltage;
181 mKiVoltage = kiVoltage;
182
183 mKpCurrent = kpCurrent;
184 mKiCurrent = kiCurrent;
185
186 mActiveDampingGain = activeDampingGain;
187 mPowerFilterCutoff = powerFilterCutoff;
188 mDelayBandwidth = delayBandwidth;
189
190 // Only establish the dimensions here. Linearizing at x = 0, u = 0 is
191 // invalid for this model because omega, voltage magnitude and the Park
192 // transformation do not represent a physical operating point there.
193 // The first actual local model is created after operating-point
194 // initialization in initializeFromNodesAndTerminals().
195 VTypeVariableSSNComp::setParameters(Matrix::Zero(mStateSize, mStateSize),
196 Matrix::Zero(mStateSize, mInputSize),
197 Matrix::Zero(mOutputSize, mStateSize),
198 Matrix::Zero(mOutputSize, mInputSize),
199 Matrix::Zero(mStateSize, 1),
200 Matrix::Zero(mOutputSize, 1));
201}
202
204 Real absoluteStep) {
205
206 if (relativeStep <= 0.0)
207 throw std::invalid_argument(
208 "Relative finite-difference step must be positive.");
209
210 if (absoluteStep <= 0.0)
211 throw std::invalid_argument(
212 "Absolute finite-difference step must be positive.");
213
214 mJacobianRelativeStep = relativeStep;
215 mJacobianAbsoluteStep = absoluteStep;
216}
217
219 Real virtualReactance) {
220 if (virtualResistance < 0.0)
221 throw std::invalid_argument("Virtual resistance must be non-negative.");
222
223 mVirtualResistance = virtualResistance;
224 mVirtualReactance = virtualReactance;
225}
226
228 mGridCurrentFeedforward = scale;
229}
230
232 if (droopGain < 0.0)
233 throw std::invalid_argument("Reactive-droop gain must be non-negative.");
234 if (cutoff < 0.0)
235 throw std::invalid_argument("Reactive-droop cutoff must be non-negative.");
236
237 mReactivePowerDroop = droopGain;
238 mReactiveDroopCutoff = cutoff;
239}
240
242EMT::Ph3::SSN_GFM::getParkTransformMatrix(Real theta) const {
243
244 theta = std::remainder(theta, 2.0 * PI);
245 MatrixFixedSize<2, 3> transform;
246
247 constexpr Real scale = 2.0 / 3.0;
248
249 transform.row(0) << scale * std::cos(theta),
250 scale * std::cos(theta - 2.0 * PI / 3.0),
251 scale * std::cos(theta + 2.0 * PI / 3.0);
252
253 transform.row(1) << -scale * std::sin(theta),
254 -scale * std::sin(theta - 2.0 * PI / 3.0),
255 -scale * std::sin(theta + 2.0 * PI / 3.0);
256
257 return transform;
258}
259
261EMT::Ph3::SSN_GFM::getInverseParkTransformMatrix(Real theta) const {
262
263 theta = std::remainder(theta, 2.0 * PI);
264 MatrixFixedSize<3, 2> transform;
265
266 transform << std::cos(theta), -std::sin(theta),
267
268 std::cos(theta - 2.0 * PI / 3.0), -std::sin(theta - 2.0 * PI / 3.0),
269
270 std::cos(theta + 2.0 * PI / 3.0), -std::sin(theta + 2.0 * PI / 3.0);
271
272 return transform;
273}
274
275Real EMT::Ph3::SSN_GFM::regularizedOmega(Real omega) const {
276 constexpr Real minimumOmega = 1.0;
277
278 if (std::abs(omega) >= minimumOmega)
279 return omega;
280
281 return omega >= 0.0 ? minimumOmega : -minimumOmega;
282}
283
284void EMT::Ph3::SSN_GFM::evaluateStateDerivative(const Matrix &x,
285 const Matrix &u,
286 Matrix &stateDerivative) const {
287
288 if (x.rows() != mStateSize || x.cols() != 1)
289 throw std::invalid_argument(
290 "SSN_GFM state vector has an invalid dimension.");
291
292 if (u.rows() != mInputSize || u.cols() != 1)
293 throw std::invalid_argument(
294 "SSN_GFM input vector has an invalid dimension.");
295
296 stateDerivative.setZero(mStateSize, 1);
297
298 const Real pFiltered = x(PFiltered, 0);
299 const Real qFiltered = x(QFiltered, 0);
300 const Real omega = x(Omega, 0);
301 const Real theta = x(Theta, 0);
302 const Real voltageMagnitude = x(VoltageMagnitude, 0);
303
304 const Real voltageIntegratorD = x(VoltageIntegratorD, 0);
305 const Real voltageIntegratorQ = x(VoltageIntegratorQ, 0);
306
307 const Real currentIntegratorD = x(CurrentIntegratorD, 0);
308 const Real currentIntegratorQ = x(CurrentIntegratorQ, 0);
309
310 const Real delayVoltageD = x(DelayVoltageD, 0);
311 const Real delayVoltageQ = x(DelayVoltageQ, 0);
312
313 const MatrixFixedSize<3, 1> vcAbc = x.block(VcA, 0, 3, 1);
314 const MatrixFixedSize<3, 1> ifAbc = x.block(IfA, 0, 3, 1);
315
316 const MatrixFixedSize<2, 3> parkTransform = getParkTransformMatrix(theta);
317 const MatrixFixedSize<3, 2> inverseParkTransform =
318 getInverseParkTransformMatrix(theta);
319
320 // Positive physical grid current means inverter injection into the grid.
321 const MatrixFixedSize<3, 1> iGridAbc = (vcAbc - u) / mRc;
322
323 const MatrixFixedSize<2, 1> vcDq = parkTransform * vcAbc;
324 const MatrixFixedSize<2, 1> ifDq = parkTransform * ifAbc;
325 const MatrixFixedSize<2, 1> iGridDq = parkTransform * iGridAbc;
326
327 const Real vcD = vcDq(0, 0);
328 const Real vcQ = vcDq(1, 0);
329
330 const Real ifD = ifDq(0, 0);
331 const Real ifQ = ifDq(1, 0);
332
333 const Real iGridD = iGridDq(0, 0);
334 const Real iGridQ = iGridDq(1, 0);
335
336 // Capacitor current.
337 const Real iCapD = ifD - iGridD;
338 const Real iCapQ = ifQ - iGridQ;
339
340 // The Park transformation is amplitude invariant (2/3 scaling).
341 // Therefore three-phase instantaneous power requires the factor 3/2.
342 const Real pInstantaneous = 1.5 * (vcD * iGridD + vcQ * iGridQ);
343
344 const Real qInstantaneous = 1.5 * (vcQ * iGridD - vcD * iGridQ);
345
346 const Real pccVoltageMagnitude = std::sqrt(vcD * vcD + vcQ * vcQ);
347
348 // ----------------------------------------------------------------------
349 // 1. Measurement filters
350 // ----------------------------------------------------------------------
351
352 stateDerivative(PFiltered, 0) =
353 mPowerFilterCutoff * (pInstantaneous - pFiltered);
354
355 stateDerivative(QFiltered, 0) =
356 mPowerFilterCutoff * (qInstantaneous - qFiltered);
357
358 // ----------------------------------------------------------------------
359 // 2. VSG algorithm
360 //
361 // J * omega_dot =
362 // (P_ref - P_e) / omega
363 // - D * (omega - omega_n)
364 //
365 // theta_dot = omega
366 // ----------------------------------------------------------------------
367
368 stateDerivative(Omega, 0) = ((mPRef - pFiltered) / regularizedOmega(omega) -
369 mDampingCoefficient * (omega - mOmegaN)) /
370 mVirtualInertia;
371
372 stateDerivative(Theta, 0) = omega;
373
374 // ----------------------------------------------------------------------
375 // 3. Reactive-power/voltage excitation controller
376 //
377 // E_dot =
378 // Kq * (Q_ref - Q_e)
379 // + Ku * (U_n - U_pcc)
380 // ----------------------------------------------------------------------
381
382 if (mReactiveDroopCutoff > 0.0) {
383 // Proportional Q-V droop (opt-in, grid-connected): E lags a droop target
384 // E_set + Dq*(Qref-Qf). No integral, so no reactive windup on a stiff grid.
385 const Real droopTarget =
386 mVoltageSetpoint + mReactivePowerDroop * (mQRef - qFiltered);
387 stateDerivative(VoltageMagnitude, 0) =
388 mReactiveDroopCutoff * (droopTarget - voltageMagnitude);
389 } else {
390 // Integral excitation (default, islanded).
391 stateDerivative(VoltageMagnitude, 0) =
392 mReactiveIntegralGain * (mQRef - qFiltered) +
393 mVoltageDroopGain * (mNominalVoltage - pccVoltageMagnitude);
394 }
395
396 // EMF minus virtual-impedance drop Zv * if (Zv = Rv + jXv; zero = islanded).
397 // if (a filter-current state) avoids the 1/Rc sensitivity of iGrid.
398 const Real voltageReferenceD =
399 voltageMagnitude - (mVirtualResistance * ifD - mVirtualReactance * ifQ);
400 const Real voltageReferenceQ =
401 -(mVirtualResistance * ifQ + mVirtualReactance * ifD);
402
403 const Real voltageErrorD = voltageReferenceD - vcD;
404 const Real voltageErrorQ = voltageReferenceQ - vcQ;
405
406 stateDerivative(VoltageIntegratorD, 0) = voltageErrorD;
407 stateDerivative(VoltageIntegratorQ, 0) = voltageErrorQ;
408
409 // ----------------------------------------------------------------------
410 // 4. Voltage controller
411 //
412 // The feed-forward and decoupling terms correspond to:
413 //
414 // Cf * dv_d/dt = if_d - ig_d + omega * Cf * v_q
415 // Cf * dv_q/dt = if_q - ig_q - omega * Cf * v_d
416 // ----------------------------------------------------------------------
417
418 const Real currentReferenceD =
419 mGridCurrentFeedforward * iGridD - omega * mCf * vcQ +
420 mKpVoltage * voltageErrorD + mKiVoltage * voltageIntegratorD;
421
422 const Real currentReferenceQ =
423 mGridCurrentFeedforward * iGridQ + omega * mCf * vcD +
424 mKpVoltage * voltageErrorQ + mKiVoltage * voltageIntegratorQ;
425
426 const Real currentErrorD = currentReferenceD - ifD;
427 const Real currentErrorQ = currentReferenceQ - ifQ;
428
429 stateDerivative(CurrentIntegratorD, 0) = currentErrorD;
430 stateDerivative(CurrentIntegratorQ, 0) = currentErrorQ;
431
432 // ----------------------------------------------------------------------
433 // 5. Current controller and active damping
434 //
435 // Lf * dif_d/dt =
436 // v_inv_d - vc_d - Rf * if_d + omega * Lf * if_q
437 //
438 // Lf * dif_q/dt =
439 // v_inv_q - vc_q - Rf * if_q - omega * Lf * if_d
440 // ----------------------------------------------------------------------
441
442 const Real converterVoltageReferenceD =
443 vcD - omega * mLf * ifQ + mKpCurrent * currentErrorD +
444 mKiCurrent * currentIntegratorD - mActiveDampingGain * iCapD;
445
446 const Real converterVoltageReferenceQ =
447 vcQ + omega * mLf * ifD + mKpCurrent * currentErrorQ +
448 mKiCurrent * currentIntegratorQ - mActiveDampingGain * iCapQ;
449
450 // ----------------------------------------------------------------------
451 // 6. First-order converter/digital-delay approximation
452 // ----------------------------------------------------------------------
453
454 stateDerivative(DelayVoltageD, 0) =
455 mDelayBandwidth * (converterVoltageReferenceD - delayVoltageD);
456
457 stateDerivative(DelayVoltageQ, 0) =
458 mDelayBandwidth * (converterVoltageReferenceQ - delayVoltageQ);
459
460 MatrixFixedSize<2, 1> converterVoltageDq;
461 converterVoltageDq << delayVoltageD, delayVoltageQ;
462
463 // ----------------------------------------------------------------------
464 // 7. Electrical filter model in abc coordinates
465 //
466 // vc_dot = (if + (u - vc) / Rc) / Cf
467 // if_dot = (v_inv - vc - Rf * if) / Lf
468 // ----------------------------------------------------------------------
469
470 const MatrixFixedSize<3, 1> converterVoltageAbc =
471 inverseParkTransform * converterVoltageDq;
472
473 const MatrixFixedSize<3, 1> vcDerivative = (ifAbc + (u - vcAbc) / mRc) / mCf;
474
475 const MatrixFixedSize<3, 1> ifDerivative =
476 (converterVoltageAbc - vcAbc - mRf * ifAbc) / mLf;
477
478 stateDerivative.block(VcA, 0, 3, 1) = vcDerivative;
479 stateDerivative.block(IfA, 0, 3, 1) = ifDerivative;
480}
481
482void EMT::Ph3::SSN_GFM::evaluateOutput(const Matrix &x, const Matrix &u,
483 Matrix &output) const {
484
485 if (x.rows() != mStateSize || x.cols() != 1)
486 throw std::invalid_argument(
487 "SSN_GFM state vector has an invalid dimension.");
488
489 if (u.rows() != mInputSize || u.cols() != 1)
490 throw std::invalid_argument(
491 "SSN_GFM input vector has an invalid dimension.");
492
493 const MatrixFixedSize<3, 1> vcAbc = x.block(VcA, 0, 3, 1);
494
495 // SSN current entering the component.
496 //
497 // The physical inverter injection current is:
498 //
499 // i_injection = (vc - u) / Rc
500 //
501 // The component current used by the SSN stamp is the opposite:
502 //
503 // y = -i_injection = (u - vc) / Rc
504 output = (u - vcAbc) / mRc;
505}
506
508 const Matrix &u, Matrix &A,
509 Matrix &B, Matrix &C,
510 Matrix &D) const {
511
512 if (x.rows() != mStateSize || x.cols() != 1)
513 throw std::invalid_argument(
514 "SSN_GFM state vector has an invalid dimension.");
515
516 if (u.rows() != mInputSize || u.cols() != 1)
517 throw std::invalid_argument(
518 "SSN_GFM input vector has an invalid dimension.");
519
520 const Real pFiltered = x(PFiltered, 0);
521 const Real omega = x(Omega, 0);
522 const Real theta = x(Theta, 0);
523 const Real delayVoltageD = x(DelayVoltageD, 0);
524 const Real delayVoltageQ = x(DelayVoltageQ, 0);
525
526 const MatrixFixedSize<3, 1> vcAbc = x.block(VcA, 0, 3, 1);
527 const MatrixFixedSize<3, 1> ifAbc = x.block(IfA, 0, 3, 1);
528
529 const MatrixFixedSize<2, 3> parkTransform = getParkTransformMatrix(theta);
530 const MatrixFixedSize<3, 2> inverseParkTransform =
531 getInverseParkTransformMatrix(theta);
532
533 using ThreePhaseRow = Eigen::Matrix<Real, 1, 3, Eigen::RowMajor>;
534 const ThreePhaseRow tD = parkTransform.row(0);
535 const ThreePhaseRow tQ = parkTransform.row(1);
536 const MatrixFixedSize<3, 1> sD = inverseParkTransform.col(0);
537 const MatrixFixedSize<3, 1> sQ = inverseParkTransform.col(1);
538
539 const MatrixFixedSize<3, 1> iGridAbc = (vcAbc - u) / mRc;
540
541 const Real vcD = (tD * vcAbc)(0, 0);
542 const Real vcQ = (tQ * vcAbc)(0, 0);
543 const Real ifD = (tD * ifAbc)(0, 0);
544 const Real ifQ = (tQ * ifAbc)(0, 0);
545 const Real iGridD = (tD * iGridAbc)(0, 0);
546 const Real iGridQ = (tQ * iGridAbc)(0, 0);
547
548 using StateRow = Eigen::Matrix<Real, 1, mStateSize, Eigen::RowMajor>;
549 using InputRow = Eigen::Matrix<Real, 1, mInputSize, Eigen::RowMajor>;
550 StateRow dVcDByX = StateRow::Zero();
551 StateRow dVcQByX = StateRow::Zero();
552 StateRow dIfDByX = StateRow::Zero();
553 StateRow dIfQByX = StateRow::Zero();
554 StateRow dIGridDByX = StateRow::Zero();
555 StateRow dIGridQByX = StateRow::Zero();
556
557 dVcDByX(0, Theta) = vcQ;
558 dVcDByX.block(0, VcA, 1, 3) = tD;
559 dVcQByX(0, Theta) = -vcD;
560 dVcQByX.block(0, VcA, 1, 3) = tQ;
561
562 dIfDByX(0, Theta) = ifQ;
563 dIfDByX.block(0, IfA, 1, 3) = tD;
564 dIfQByX(0, Theta) = -ifD;
565 dIfQByX.block(0, IfA, 1, 3) = tQ;
566
567 dIGridDByX(0, Theta) = iGridQ;
568 dIGridDByX.block(0, VcA, 1, 3) = tD / mRc;
569 dIGridQByX(0, Theta) = -iGridD;
570 dIGridQByX.block(0, VcA, 1, 3) = tQ / mRc;
571
572 const InputRow dIGridDByU = -tD / mRc;
573 const InputRow dIGridQByU = -tQ / mRc;
574
575 StateRow dPByX = 1.5 * (iGridD * dVcDByX + vcD * dIGridDByX +
576 iGridQ * dVcQByX + vcQ * dIGridQByX);
577 const InputRow dPByU = 1.5 * (vcD * dIGridDByU + vcQ * dIGridQByU);
578
579 StateRow dQByX = 1.5 * (iGridD * dVcQByX + vcQ * dIGridDByX -
580 iGridQ * dVcDByX - vcD * dIGridQByX);
581 const InputRow dQByU = 1.5 * (vcQ * dIGridDByU - vcD * dIGridQByU);
582
583 dPByX(0, Theta) = 0.0;
584 dQByX(0, Theta) = 0.0;
585
586 const Real pccVoltageMagnitude = std::sqrt(vcD * vcD + vcQ * vcQ);
587 StateRow dPccVoltageMagnitudeByX = StateRow::Zero();
588
589 if (pccVoltageMagnitude > 1e-12) {
590 dPccVoltageMagnitudeByX =
591 (vcD * dVcDByX + vcQ * dVcQByX) / pccVoltageMagnitude;
592 dPccVoltageMagnitudeByX(0, Theta) = 0.0;
593 }
594
595 StateRow unitOmega = StateRow::Zero();
596 StateRow unitVoltageMagnitude = StateRow::Zero();
597 StateRow unitVoltageIntegratorD = StateRow::Zero();
598 StateRow unitVoltageIntegratorQ = StateRow::Zero();
599 StateRow unitCurrentIntegratorD = StateRow::Zero();
600 StateRow unitCurrentIntegratorQ = StateRow::Zero();
601
602 unitOmega(0, Omega) = 1.0;
603 unitVoltageMagnitude(0, VoltageMagnitude) = 1.0;
604 unitVoltageIntegratorD(0, VoltageIntegratorD) = 1.0;
605 unitVoltageIntegratorQ(0, VoltageIntegratorQ) = 1.0;
606 unitCurrentIntegratorD(0, CurrentIntegratorD) = 1.0;
607 unitCurrentIntegratorQ(0, CurrentIntegratorQ) = 1.0;
608
609 const StateRow dVoltageErrorDByX = unitVoltageMagnitude -
610 mVirtualResistance * dIfDByX +
611 mVirtualReactance * dIfQByX - dVcDByX;
612 const StateRow dVoltageErrorQByX =
613 -mVirtualResistance * dIfQByX - mVirtualReactance * dIfDByX - dVcQByX;
614
615 const StateRow dCurrentReferenceDByX =
616 mGridCurrentFeedforward * dIGridDByX -
617 mCf * (vcQ * unitOmega + omega * dVcQByX) +
618 mKpVoltage * dVoltageErrorDByX + mKiVoltage * unitVoltageIntegratorD;
619 const InputRow dCurrentReferenceDByU = mGridCurrentFeedforward * dIGridDByU;
620
621 const StateRow dCurrentReferenceQByX =
622 mGridCurrentFeedforward * dIGridQByX +
623 mCf * (vcD * unitOmega + omega * dVcDByX) +
624 mKpVoltage * dVoltageErrorQByX + mKiVoltage * unitVoltageIntegratorQ;
625 const InputRow dCurrentReferenceQByU = mGridCurrentFeedforward * dIGridQByU;
626
627 const StateRow dCurrentErrorDByX = dCurrentReferenceDByX - dIfDByX;
628 const StateRow dCurrentErrorQByX = dCurrentReferenceQByX - dIfQByX;
629 const InputRow dCurrentErrorDByU = dCurrentReferenceDByU;
630 const InputRow dCurrentErrorQByU = dCurrentReferenceQByU;
631
632 const StateRow dCapacitorCurrentDByX = dIfDByX - dIGridDByX;
633 const StateRow dCapacitorCurrentQByX = dIfQByX - dIGridQByX;
634 const InputRow dCapacitorCurrentDByU = -dIGridDByU;
635 const InputRow dCapacitorCurrentQByU = -dIGridQByU;
636
637 const StateRow dConverterVoltageReferenceDByX =
638 dVcDByX - mLf * (ifQ * unitOmega + omega * dIfQByX) +
639 mKpCurrent * dCurrentErrorDByX + mKiCurrent * unitCurrentIntegratorD -
640 mActiveDampingGain * dCapacitorCurrentDByX;
641 const InputRow dConverterVoltageReferenceDByU =
642 mKpCurrent * dCurrentErrorDByU -
643 mActiveDampingGain * dCapacitorCurrentDByU;
644
645 const StateRow dConverterVoltageReferenceQByX =
646 dVcQByX + mLf * (ifD * unitOmega + omega * dIfDByX) +
647 mKpCurrent * dCurrentErrorQByX + mKiCurrent * unitCurrentIntegratorQ -
648 mActiveDampingGain * dCapacitorCurrentQByX;
649 const InputRow dConverterVoltageReferenceQByU =
650 mKpCurrent * dCurrentErrorQByU -
651 mActiveDampingGain * dCapacitorCurrentQByU;
652
653 A.setZero(mStateSize, mStateSize);
654 B.setZero(mStateSize, mInputSize);
655 C.setZero(mOutputSize, mStateSize);
656 D.setZero(mOutputSize, mInputSize);
657
658 A.row(PFiltered) = mPowerFilterCutoff * dPByX;
659 A(PFiltered, PFiltered) -= mPowerFilterCutoff;
660 B.row(PFiltered) = mPowerFilterCutoff * dPByU;
661
662 A.row(QFiltered) = mPowerFilterCutoff * dQByX;
663 A(QFiltered, QFiltered) -= mPowerFilterCutoff;
664 B.row(QFiltered) = mPowerFilterCutoff * dQByU;
665
666 const Real omegaDenominator = regularizedOmega(omega);
667 const Real omegaDenominatorDerivative =
668 std::abs(omega) >= std::abs(omegaDenominator) ? 1.0 : 0.0;
669
670 A(Omega, PFiltered) = -1.0 / (mVirtualInertia * omegaDenominator);
671 A(Omega, Omega) = (-(mPRef - pFiltered) * omegaDenominatorDerivative /
672 (omegaDenominator * omegaDenominator) -
673 mDampingCoefficient) /
674 mVirtualInertia;
675
676 A(Theta, Omega) = 1.0;
677
678 if (mReactiveDroopCutoff > 0.0) {
679 A(VoltageMagnitude, QFiltered) =
680 -mReactiveDroopCutoff * mReactivePowerDroop;
681 A(VoltageMagnitude, VoltageMagnitude) = -mReactiveDroopCutoff;
682 } else {
683 A(VoltageMagnitude, QFiltered) = -mReactiveIntegralGain;
684 A.row(VoltageMagnitude) -= mVoltageDroopGain * dPccVoltageMagnitudeByX;
685 }
686
687 A.row(VoltageIntegratorD) = dVoltageErrorDByX;
688 A.row(VoltageIntegratorQ) = dVoltageErrorQByX;
689
690 A.row(CurrentIntegratorD) = dCurrentErrorDByX;
691 B.row(CurrentIntegratorD) = dCurrentErrorDByU;
692 A.row(CurrentIntegratorQ) = dCurrentErrorQByX;
693 B.row(CurrentIntegratorQ) = dCurrentErrorQByU;
694
695 A.row(DelayVoltageD) = mDelayBandwidth * dConverterVoltageReferenceDByX;
696 A(DelayVoltageD, DelayVoltageD) -= mDelayBandwidth;
697 B.row(DelayVoltageD) = mDelayBandwidth * dConverterVoltageReferenceDByU;
698
699 A.row(DelayVoltageQ) = mDelayBandwidth * dConverterVoltageReferenceQByX;
700 A(DelayVoltageQ, DelayVoltageQ) -= mDelayBandwidth;
701 B.row(DelayVoltageQ) = mDelayBandwidth * dConverterVoltageReferenceQByU;
702
704
705 A.block(VcA, VcA, 3, 3) = -identity / (mCf * mRc);
706 A.block(VcA, IfA, 3, 3) = identity / mCf;
707 B.block(VcA, 0, 3, 3) = identity / (mCf * mRc);
708
709 A.block(IfA, VcA, 3, 3) = -identity / mLf;
710 A.block(IfA, IfA, 3, 3) = -mRf * identity / mLf;
711 A.block(IfA, Theta, 3, 1) = (sQ * delayVoltageD - sD * delayVoltageQ) / mLf;
712 A.block(IfA, DelayVoltageD, 3, 1) = sD / mLf;
713 A.block(IfA, DelayVoltageQ, 3, 1) = sQ / mLf;
714
715 C.block(0, VcA, 3, 3) = -identity / mRc;
716 D = identity / mRc;
717}
718
720 const Matrix &u, Matrix &A,
721 Matrix &B, Matrix &C,
722 Matrix &D) const {
723
724 A.setZero(mStateSize, mStateSize);
725 B.setZero(mStateSize, mInputSize);
726 C.setZero(mOutputSize, mStateSize);
727 D.setZero(mOutputSize, mInputSize);
728
729 Matrix fPlus = Matrix::Zero(mStateSize, 1);
730 Matrix fMinus = Matrix::Zero(mStateSize, 1);
731
732 Matrix gPlus = Matrix::Zero(mOutputSize, 1);
733 Matrix gMinus = Matrix::Zero(mOutputSize, 1);
734
735 // State Jacobians A and C.
736 for (Int column = 0; column < mStateSize; ++column) {
737 const Real step =
738 mJacobianAbsoluteStep +
739 mJacobianRelativeStep * std::max(1.0, std::abs(x(column, 0)));
740
741 Matrix xPlus = x;
742 Matrix xMinus = x;
743
744 xPlus(column, 0) += step;
745 xMinus(column, 0) -= step;
746
747 evaluateStateDerivative(xPlus, u, fPlus);
748 evaluateStateDerivative(xMinus, u, fMinus);
749
750 evaluateOutput(xPlus, u, gPlus);
751 evaluateOutput(xMinus, u, gMinus);
752
753 A.col(column) = (fPlus - fMinus) / (2.0 * step);
754 C.col(column) = (gPlus - gMinus) / (2.0 * step);
755 }
756
757 // Input Jacobians B and D.
758 for (Int column = 0; column < mInputSize; ++column) {
759 const Real step =
760 mJacobianAbsoluteStep +
761 mJacobianRelativeStep * std::max(1.0, std::abs(u(column, 0)));
762
763 Matrix uPlus = u;
764 Matrix uMinus = u;
765
766 uPlus(column, 0) += step;
767 uMinus(column, 0) -= step;
768
769 evaluateStateDerivative(x, uPlus, fPlus);
770 evaluateStateDerivative(x, uMinus, fMinus);
771
772 evaluateOutput(x, uPlus, gPlus);
773 evaluateOutput(x, uMinus, gMinus);
774
775 B.col(column) = (fPlus - fMinus) / (2.0 * step);
776 D.col(column) = (gPlus - gMinus) / (2.0 * step);
777 }
778}
779
780void EMT::Ph3::SSN_GFM::buildStateSpaceModel(const Matrix &x, const Matrix &u,
781 Matrix &A, Matrix &B, Matrix &C,
782 Matrix &D, Matrix &E, Matrix &F) {
783
784 calculateAnalyticalJacobians(x, u, A, B, C, D);
785
786 evaluateStateDerivative(x, u, mStateDerivativeWorkspace);
787 evaluateOutput(x, u, mOutputWorkspace);
788
789 // Local affine offsets:
790 //
791 // E = f(x0,u0) - A*x0 - B*u0
792 // F = g(x0,u0) - C*x0 - D*u0
793 E = mStateDerivativeWorkspace;
794 E.noalias() -= A * x;
795 E.noalias() -= B * u;
796 F = mOutputWorkspace;
797 F.noalias() -= C * x;
798 F.noalias() -= D * u;
799}
800
802 const Matrix &linearizationState =
803 mImplicitNewtonActive ? mNewtonGuessState : **mX;
804 const Matrix &linearizationVoltage =
805 mImplicitNewtonActive ? mNewtonGuessVoltage : **mIntfVoltage;
806 buildStateSpaceModel(linearizationState, linearizationVoltage, mA, mB, mC, mD,
807 mStateOffsetWorkspace, mOutputOffsetWorkspace);
808
809 if (mImplicitNewtonActive) {
810 // Linearize only the unknown endpoint term in the trapezoidal residual:
811 // x1 - x0 - h/2(f0 + f(x1,u1)) = 0. The ordinary SSN discretizer
812 // evaluates its affine model at both endpoints, so adjust E such that its
813 // old-endpoint contribution remains the true f(x0,u0), while its new
814 // endpoint contribution is the Newton tangent at (xj,uj).
815 mStateOffsetWorkspace =
816 0.5 * (mNewtonBaseDerivative + mStateOffsetWorkspace -
817 mA * mNewtonBaseState - mB * mNewtonBaseVoltage);
818 }
819
820 setStateOffset(mStateOffsetWorkspace);
821 setOutputOffset(mOutputOffsetWorkspace);
822
823 // The dq/abc transformations and nonlinear GFM controls make the local
824 // state-space model time varying. The SSN equivalent must therefore be
825 // recomputed every simulation step.
826 return true;
827}
828
830
831 const Matrix &x = **mX;
832
833 for (Int index = 0; index < x.rows(); ++index)
834 **mStateLogAttributes.at(static_cast<std::size_t>(index)) = x(index, 0);
835
836 const Real theta = x(Theta, 0);
837
838 const MatrixFixedSize<2, 3> parkTransform = getParkTransformMatrix(theta);
839
840 const MatrixFixedSize<3, 1> vcAbc = x.block(VcA, 0, 3, 1);
841 const MatrixFixedSize<3, 1> ifAbc = x.block(IfA, 0, 3, 1);
842
843 const MatrixFixedSize<3, 1> iGridAbc = (vcAbc - u) / mRc;
844
845 const MatrixFixedSize<2, 1> vcDq = parkTransform * vcAbc;
846 const MatrixFixedSize<2, 1> ifDq = parkTransform * ifAbc;
847 const MatrixFixedSize<2, 1> iGridDq = parkTransform * iGridAbc;
848
849 const Real vcD = vcDq(0, 0);
850 const Real vcQ = vcDq(1, 0);
851
852 const Real iGridD = iGridDq(0, 0);
853 const Real iGridQ = iGridDq(1, 0);
854
855 const Real ifD = ifDq(0, 0);
856 const Real ifQ = ifDq(1, 0);
857
858 **mVcD = vcD;
859 **mVcQ = vcQ;
860
861 **mIGridD = iGridD;
862 **mIGridQ = iGridQ;
863
864 **mIfD = ifD;
865 **mIfQ = ifQ;
866
867 **mPInst = 1.5 * (vcD * iGridD + vcQ * iGridQ);
868 **mQInst = 1.5 * (vcQ * iGridD - vcD * iGridQ);
869
870 **mOmegaGFM = x(Omega, 0);
871 **mThetaGFM = theta;
872 **mVoltageMagnitudeGFM = x(VoltageMagnitude, 0);
873
874 // Reference the controller tracks, including the virtual-impedance drop.
875 **mVoltageReferenceD = x(VoltageMagnitude, 0) -
876 (mVirtualResistance * ifD - mVirtualReactance * ifQ);
877 **mVoltageReferenceQ = -(mVirtualResistance * ifQ + mVirtualReactance * ifD);
878}
879
881
882 if (!mParametersSet)
883 throw std::logic_error("setParameters() must be called before "
884 "initializeFromNodesAndTerminals().");
885
886 const Real omegaInitialization = 2.0 * PI * frequency;
887 const Complex imaginaryUnit(0.0, 1.0);
888
889 const Complex powerReference(mPRef, mQRef);
890
891 // Terminal voltage phasors using the inherited convention:
892 //
893 // u = terminal1 - terminal0
894 const MatrixComp uPhasor = buildInitialInputFromNodes(frequency);
895
896 MatrixComp vcPhasor = uPhasor;
897 MatrixComp iInjectionPhasor = MatrixComp::Zero(3, 1);
898
899 // Determine capacitor-voltage and injected-current phasors through the
900 // coupling resistance:
901 //
902 // vc = u + Rc * iInjection
903 for (Int iteration = 0; iteration < mInitializationMaxIterations;
904 ++iteration) {
905
906 const Complex vcA = vcPhasor(0, 0);
907
908 if (std::abs(vcA) < mInitializationTolerance) {
909 iInjectionPhasor.setZero();
910 break;
911 }
912
913 // With peak-valued phase phasors and the amplitude-invariant Park
914 // transform, total three-phase power is
915 //
916 // S = 1.5 * V_phase_peak * conj(I_phase_peak).
917 const Complex currentA = std::conj(powerReference / (1.5 * vcA));
918
919 MatrixComp nextInjectionCurrent(3, 1);
920
921 nextInjectionCurrent << currentA, currentA * SHIFT_TO_PHASE_B,
922 currentA * SHIFT_TO_PHASE_C;
923
924 const MatrixComp nextVcPhasor = uPhasor + mRc * nextInjectionCurrent;
925
926 iInjectionPhasor = nextInjectionCurrent;
927
928 if ((nextVcPhasor - vcPhasor).norm() < mInitializationTolerance) {
929 vcPhasor = nextVcPhasor;
930 break;
931 }
932
933 vcPhasor = nextVcPhasor;
934 }
935
936 // Capacitor-current relation:
937 //
938 // if = iGrid + j*omega*Cf*vc
939 const MatrixComp ifPhasor =
940 iInjectionPhasor + imaginaryUnit * omegaInitialization * mCf * vcPhasor;
941
942 // Converter bridge voltage:
943 //
944 // vInv = vc + (Rf + j*omega*Lf)*if
945 const MatrixComp converterVoltagePhasor =
946 vcPhasor + (mRf + imaginaryUnit * omegaInitialization * mLf) * ifPhasor;
947
948 const Matrix vcAbc0 = vcPhasor.real();
949 const Matrix ifAbc0 = ifPhasor.real();
950 const Matrix iGridAbc0 = iInjectionPhasor.real();
951 const Matrix converterVoltageAbc0 = converterVoltagePhasor.real();
952
953 // Align the dq frame with the internal EMF behind the virtual impedance,
954 // E0 = vc + (Rv + jXv)*if. At the operating point the voltage-loop reference
955 // is E0 in dq, so aligning to it makes both loop errors vanish (true
956 // equilibrium) even for nonzero Zv. Zv = 0 reduces to aligning with vc.
957 const Complex virtualImpedance(mVirtualResistance, mVirtualReactance);
958 const MatrixComp emfPhasor = vcPhasor + virtualImpedance * ifPhasor;
959 const Real theta0 = std::arg(emfPhasor(0, 0));
960
961 const MatrixFixedSize<2, 3> parkTransform = getParkTransformMatrix(theta0);
962
963 const Matrix vcDq0 = parkTransform * vcAbc0;
964 const Matrix ifDq0 = parkTransform * ifAbc0;
965 const Matrix iGridDq0 = parkTransform * iGridAbc0;
966 const Matrix converterVoltageDq0 = parkTransform * converterVoltageAbc0;
967
968 const Real vcD0 = vcDq0(0, 0);
969 const Real vcQ0 = vcDq0(1, 0);
970
971 const Real ifD0 = ifDq0(0, 0);
972 const Real ifQ0 = ifDq0(1, 0);
973
974 const Real iGridD0 = iGridDq0(0, 0);
975 const Real iGridQ0 = iGridDq0(1, 0);
976
977 const Real pInitial = 1.5 * (vcD0 * iGridD0 + vcQ0 * iGridQ0);
978
979 const Real qInitial = 1.5 * (vcQ0 * iGridD0 - vcD0 * iGridQ0);
980
981 const Real iCapD0 = ifD0 - iGridD0;
982 const Real iCapQ0 = ifQ0 - iGridQ0;
983
984 Matrix x0 = Matrix::Zero(mStateSize, 1);
985
986 x0(PFiltered, 0) = pInitial;
987 x0(QFiltered, 0) = qInitial;
988
989 x0(Omega, 0) = omegaInitialization;
990 x0(Theta, 0) = theta0;
991
992 // Internal EMF magnitude behind the virtual impedance (d-axis aligned).
993 x0(VoltageMagnitude, 0) = std::abs(emfPhasor(0, 0));
994
995 // Proportional-droop setpoint: the operating EMF, so the droop is centered at
996 // the initial point (E_dot = 0 when Qf = Qref at t = 0).
997 mVoltageSetpoint = x0(VoltageMagnitude, 0);
998
999 // Voltage-loop references at the operating point, same form as
1000 // evaluateStateDerivative(). With the E0-aligned frame both errors are ~0.
1001 const Real voltageReferenceD0 =
1002 x0(VoltageMagnitude, 0) -
1003 (mVirtualResistance * ifD0 - mVirtualReactance * ifQ0);
1004 const Real voltageReferenceQ0 =
1005 -(mVirtualResistance * ifQ0 + mVirtualReactance * ifD0);
1006 const Real voltageErrorD0 = voltageReferenceD0 - vcD0;
1007 const Real voltageErrorQ0 = voltageReferenceQ0 - vcQ0;
1008
1009 // Voltage controller:
1010 //
1011 // iRefD =
1012 // iGridD - omega*Cf*vcQ
1013 // + KpV*(E-vcD)
1014 // + KiV*xiVd
1015 //
1016 // Set iRefD = ifD and solve for xiVd.
1017 x0(VoltageIntegratorD, 0) =
1018 (ifD0 - mGridCurrentFeedforward * iGridD0 +
1019 omegaInitialization * mCf * vcQ0 - mKpVoltage * voltageErrorD0) /
1020 mKiVoltage;
1021
1022 // iRefQ =
1023 // iGridQ + omega*Cf*vcD
1024 // + KpV*(0-vcQ)
1025 // + KiV*xiVq
1026 //
1027 // Set iRefQ = ifQ and solve for xiVq.
1028 x0(VoltageIntegratorQ, 0) =
1029 (ifQ0 - mGridCurrentFeedforward * iGridQ0 -
1030 omegaInitialization * mCf * vcD0 - mKpVoltage * voltageErrorQ0) /
1031 mKiVoltage;
1032
1033 // Current controller steady-state integrators.
1034 //
1035 // vInvD =
1036 // vcD - omega*Lf*ifQ
1037 // + KpI*(iRefD-ifD)
1038 // + KiI*xiId
1039 // - Kc*iCapD
1040 x0(CurrentIntegratorD, 0) =
1041 (converterVoltageDq0(0, 0) - vcD0 + omegaInitialization * mLf * ifQ0 +
1042 mActiveDampingGain * iCapD0) /
1043 mKiCurrent;
1044
1045 // vInvQ =
1046 // vcQ + omega*Lf*ifD
1047 // + KpI*(iRefQ-ifQ)
1048 // + KiI*xiIq
1049 // - Kc*iCapQ
1050 x0(CurrentIntegratorQ, 0) =
1051 (converterVoltageDq0(1, 0) - vcQ0 - omegaInitialization * mLf * ifD0 +
1052 mActiveDampingGain * iCapQ0) /
1053 mKiCurrent;
1054
1055 // At steady state, the first-order delay output equals its input.
1056 x0(DelayVoltageD, 0) = converterVoltageDq0(0, 0);
1057 x0(DelayVoltageQ, 0) = converterVoltageDq0(1, 0);
1058
1059 x0.block(VcA, 0, 3, 1) = vcAbc0;
1060 x0.block(IfA, 0, 3, 1) = ifAbc0;
1061
1062 // Store the initialized state and terminal voltage.
1063 **mX = x0;
1064 **mIntfVoltage = uPhasor.real();
1065
1066 // Do not call updateComponentParameters() directly here.
1067 //
1068 // updateStateSpaceModel() performs:
1069 // 1. updateComponentParameters()
1070 // 2. recomputeDiscreteModel()
1071 //
1072 // This initializes mdA, mdB, mdE and mW.
1074
1075 // The MNA right-side-vector stamp uses mYHist. It must therefore already
1076 // be valid before the first network solution.
1078
1079 // Use the same Norton-equivalent equation as the SSN network stamp:
1080 //
1081 // y = W*u + yHist
1082 //
1083 // This avoids an initialization mismatch between mIntfCurrent and the
1084 // actual component stamp.
1085
1086 **mIntfCurrent = mW * (**mIntfVoltage) + mYHist;
1087
1089
1090 Matrix stateDerivative = Matrix::Zero(mStateSize, 1);
1091
1092 evaluateStateDerivative(**mX, **mIntfVoltage, stateDerivative);
1093
1094 const Matrix nonlinearOutput = [&]() {
1095 Matrix output = Matrix::Zero(mOutputSize, 1);
1096 evaluateOutput(**mX, **mIntfVoltage, output);
1097 return output;
1098 }();
1099
1100 const Matrix ssnOutput = mW * (**mIntfVoltage) + mYHist;
1101
1102 SPDLOG_LOGGER_INFO(
1103 mSLog,
1104 "\n--- SSN GFM initialization ---"
1105 "\nInput voltage u: {:s}"
1106 "\nInterface current y: {:s}"
1107 "\nState x: {:s}"
1108 "\nState derivative norm: {:.6e}"
1109 "\nNonlinear output: {:s}"
1110 "\nSSN output: {:s}"
1111 "\nOutput mismatch norm: {:.6e}"
1112 "\nW norm: {:.6e}"
1113 "\nHistory-vector norm: {:.6e}"
1114 "\nP/Q initial: [{:.6e}, {:.6e}]"
1115 "\nVc dq: [{:.6e}, {:.6e}]"
1116 "\nIGrid dq: [{:.6e}, {:.6e}]"
1117 "\nIf dq: [{:.6e}, {:.6e}]"
1118 "\nConverter voltage dq: [{:.6e}, {:.6e}]"
1119 "\n--- SSN GFM initialization finished ---",
1122 stateDerivative.norm(), Logger::matrixToString(nonlinearOutput),
1123 Logger::matrixToString(ssnOutput), (nonlinearOutput - ssnOutput).norm(),
1124 mW.norm(), mYHist.norm(), pInitial, qInitial, vcD0, vcQ0, iGridD0,
1125 iGridQ0, ifD0, ifQ0, converterVoltageDq0(0, 0),
1126 converterVoltageDq0(1, 0));
1127}
1128
1130
1132 Matrix stateDerivative = Matrix::Zero(mStateSize, 1);
1133
1134 evaluateStateDerivative(**mX, **mIntfVoltage, stateDerivative);
1135
1136 return stateDerivative;
1137}
1138
1140
1142
1144 if (mImplicitNewtonActive)
1145 throw std::logic_error("An implicit Newton step is already active.");
1146 mNewtonBaseState = **mX;
1147 mNewtonBaseVoltage = **mIntfVoltage;
1148 evaluateStateDerivative(mNewtonBaseState, mNewtonBaseVoltage,
1149 mNewtonBaseDerivative);
1150 mNewtonGuessState = mNewtonBaseState;
1151 mNewtonGuessVoltage = mNewtonBaseVoltage;
1152 mImplicitNewtonActive = true;
1153}
1154
1156 const Matrix &voltage) {
1157 if (!mImplicitNewtonActive)
1158 throw std::logic_error("No implicit Newton step is active.");
1159 if (state.rows() != mStateSize || state.cols() != 1 ||
1160 voltage.rows() != mInputSize || voltage.cols() != 1)
1161 throw std::invalid_argument(
1162 "Implicit Newton guess has invalid dimensions.");
1163 mNewtonGuessState = state;
1164 mNewtonGuessVoltage = voltage;
1165}
1166
1168 if (!mImplicitNewtonActive)
1169 throw std::logic_error("No implicit Newton step is active.");
1170 **mX = mNewtonBaseState;
1171 **mIntfVoltage = mNewtonBaseVoltage;
1172}
1173
1175 if (!mImplicitNewtonActive)
1176 throw std::logic_error("No implicit Newton step is active.");
1177
1178 Matrix currentDerivative(mStateSize, 1);
1179 Matrix currentOutput(mOutputSize, 1);
1180 evaluateStateDerivative(**mX, **mIntfVoltage, currentDerivative);
1181 evaluateOutput(**mX, **mIntfVoltage, currentOutput);
1182
1183 const Matrix stateResidual =
1184 **mX - mNewtonBaseState -
1185 (0.5 * mTimeStep) * (mNewtonBaseDerivative + currentDerivative);
1186 Real scaledResidual = 0.0;
1187 for (Int i = 0; i < mStateSize; ++i) {
1188 const Real scale = 1e-10 + 1e-8 * std::max(std::abs(mNewtonBaseState(i, 0)),
1189 std::abs((**mX)(i, 0)));
1190 scaledResidual =
1191 std::max(scaledResidual, std::abs(stateResidual(i, 0)) / scale);
1192 }
1193
1194 const Matrix tangentOutput =
1195 mC * (**mX) + mD * (**mIntfVoltage) + outputOffset();
1196 for (Int i = 0; i < mOutputSize; ++i) {
1197 const Real scale = 1e-10 + 1e-8 * std::max(std::abs(currentOutput(i, 0)),
1198 std::abs(tangentOutput(i, 0)));
1199 scaledResidual =
1200 std::max(scaledResidual,
1201 std::abs(currentOutput(i, 0) - tangentOutput(i, 0)) / scale);
1202 }
1203 return scaledResidual;
1204}
1205
1207 if (!mImplicitNewtonActive)
1208 throw std::logic_error("No implicit Newton step is active.");
1209 mImplicitNewtonActive = false;
1210}
void calculateNumericalJacobians(const Matrix &x, const Matrix &u, Matrix &A, Matrix &B, Matrix &C, Matrix &D) const
Numerically calculate A, B, C and D by central differences, as the reference for EMT_Ph3_SSN_GFM_Jaco...
void setImplicitNewtonGuess(const Matrix &state, const Matrix &voltage)
void setNumericalLinearizationParameters(Real relativeStep, Real absoluteStep)
Configure finite-difference steps for local linearization.
SSN_GFM(String uid, String name, Logger::Level logLevel=Logger::Level::off)
Matrix getStateDerivative() const
Bool updateComponentParameters() override final
Matrix getInterfaceCurrent() const
std::vector< SSNComp::LocalAbcStateBlock > getLocalAbcStateBlocks() const override final
Matrix getInterfaceVoltage() const
void updateLogAttributes(const Matrix &u) const override final
Update derived attributes used for logging/inspection.
void setReactivePowerDroop(Real droopGain, Real cutoff)
Opt-in proportional Q-V droop replacing the integral excitation. droopGain is Dq [V/var]; cutoff [rad...
void setParameters(Real lf, Real cf, Real rf, Real rc, Real nominalVoltage, Real omegaN, Real pRef, Real qRef, Real virtualInertia, Real dampingCoefficient, Real voltageDroopGain, Real reactiveIntegralGain, Real kpVoltage, Real kiVoltage, Real kpCurrent, Real kiCurrent, Real activeDampingGain, Real powerFilterCutoff, Real delayBandwidth)
Configure the GFM model.
std::vector< String > getLocalStateNames() const override final
void calculateAnalyticalJacobians(const Matrix &x, const Matrix &u, Matrix &A, Matrix &B, Matrix &C, Matrix &D) const
Analytically calculate A, B, C and D, as the simulation does.
void setGridCurrentFeedforward(Real scale)
Scale on the voltage-loop grid-current feedforward (default 1).
void setVirtualImpedance(Real virtualResistance, Real virtualReactance=0.0)
Opt-in virtual output impedance Zv = Rv + jXv, subtracted from the EMF reference across the filter cu...
void initializeFromNodesAndTerminals(Real frequency) override final
Initializes Component variables according to power flow data stored in Nodes.
TwoTerminalVTypeVariableSSNComp(String uid, String name, Logger::Level logLevel=Logger::Level::off)
const Attribute< Matrix >::Ptr mX
Definition EMT_SSNComp.h:48
void calculateHistoryVector(Matrix &history) const override final
void setParameters(const Matrix &A, const Matrix &B, const Matrix &C, const Matrix &D)
static constexpr Real mInitializationTolerance
void updateStateSpaceModel() override final
Hook for variable/time-varying SSN components.
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
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