DPsim
Loading...
Searching...
No Matches
EMT_Ph3_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
10
11using namespace CPS;
12
14 : GFL(uid, name, true, logLevel) {}
15
17 Logger::Level logLevel)
18 : CompositePowerComp<Real>(uid, name, true, true, logLevel),
19 mUseSubcomponents(useSubcomponents),
20 mVcd(mAttributes->create<Real>("vc_d", 0.0)),
21 mVcq(mAttributes->create<Real>("vc_q", 0.0)),
22 mIgridD(mAttributes->create<Real>("igrid_d", 0.0)),
23 mIgridQ(mAttributes->create<Real>("igrid_q", 0.0)),
24 mPInst(mAttributes->create<Real>("p_inst", 0.0)),
25 mQInst(mAttributes->create<Real>("q_inst", 0.0)),
26 mOmegaPLL(mAttributes->create<Real>("omega_pll", 0.0)),
27 mVsref(mAttributes->create<Matrix>("vs_ref", Matrix::Zero(3, 1))),
28 mVs(mAttributes->createDynamic<Matrix>("vs")),
30 "direct_source_voltage", Matrix::Zero(3, 1))),
31 mPllOutput(mAttributes->createDynamic<Matrix>("pll_output")),
32 mPowerctrlInputs(mAttributes->createDynamic<Matrix>("powerctrl_inputs")),
34 mAttributes->createDynamic<Matrix>("powerctrl_outputs")),
35 mPowerctrlStates(mAttributes->createDynamic<Matrix>("powerctrl_states")) {
36
38
39 // Two internal nodes are used for source, resistor, and inductor connection.
42
43 **mIntfVoltage = Matrix::Zero(3, 1);
44 **mIntfCurrent = Matrix::Zero(3, 1);
45
46 SPDLOG_LOGGER_INFO(mSLog, "Create {} {}", type(), name);
47
48 // -------------------------------------------------------------------------
49 // Electrical subcomponents
50 // -------------------------------------------------------------------------
57
67 // The controlled source pre-step is handled by the parent.
71 }
72
73 // -------------------------------------------------------------------------
74 // Control subcomponents
75 // -------------------------------------------------------------------------
77
79 **mName + "_PowerControllerVSI", mLogLevel);
80
81 // Actual internal source voltage for logging.
83 mVs->setReference(mSubControlledVoltageSource->mIntfVoltage);
84 else
85 mVs->setReference(mDirectSourceVoltageAttr);
86
87 // PLL input is the PCC q-axis voltage.
88 mPLL->mInputRef->setReference(mVcq);
89 mPllOutput->setReference(mPLL->mOutputCurr);
90
91 // Power controller measurements:
92 // Vc = PCC voltage
93 // Igrid = positive converter-to-grid current
94 mPowerControllerVSI->mVc_d->setReference(mVcd);
95 mPowerControllerVSI->mVc_q->setReference(mVcq);
96 mPowerControllerVSI->mIrc_d->setReference(mIgridD);
97 mPowerControllerVSI->mIrc_q->setReference(mIgridQ);
98
99 mPowerctrlInputs->setReference(mPowerControllerVSI->mInputCurr);
100 mPowerctrlStates->setReference(mPowerControllerVSI->mStateCurr);
101 mPowerctrlOutputs->setReference(mPowerControllerVSI->mOutputCurr);
102}
103
106
107 copy->setParameters(mOmegaN, mVnom, mPRef, mQRef);
108
109 copy->setControllerParameters(mKpPLL, mKiPLL, mKpPowerCtrl, mKiPowerCtrl,
111
112 copy->setFilterParameters(mLf, mCf, mRf);
113
115 copy->setInitialStateValues(mPInitManual, mQInitManual, mPhiDInitManual,
118 }
119
120 copy->withControl(mWithControl);
121
122 return copy;
123}
124
125void EMT::Ph3::GFL::setParameters(Real sysOmega, Real sysVoltNom, Real pRef,
126 Real qRef) {
127
128 if (!Math::isFinite(sysOmega) || !Math::isFinite(sysVoltNom) ||
129 !Math::isFinite(pRef) || !Math::isFinite(qRef))
130 throw std::invalid_argument("GFL parameters must be finite.");
131
132 if (sysOmega <= 0.0)
133 throw std::invalid_argument(
134 "GFL nominal angular frequency must be positive.");
135
136 if (sysVoltNom <= 0.0)
137 throw std::invalid_argument("GFL nominal voltage must be positive.");
138
139 mOmegaN = sysOmega;
140 mVnom = sysVoltNom;
141 mPRef = pRef;
142 mQRef = qRef;
143
144 mPowerControllerVSI->setParameters(mPRef, mQRef);
145
146 mParametersSet = true;
147
148 SPDLOG_LOGGER_INFO(mSLog,
149 "GFL general parameters:"
150 "\n V_nom = {} V"
151 "\n omega_nom = {} rad/s"
152 "\n P_ref = {} W"
153 "\n Q_ref = {} var",
155}
156
158 Real kpPowerCtrl, Real kiPowerCtrl,
159 Real kpCurrCtrl, Real kiCurrCtrl,
160 Real omegaCutoff) {
161
162 if (!mParametersSet)
163 throw std::logic_error("GFL::setParameters() must be called before "
164 "setControllerParameters().");
165
166 if (!Math::isFinite(kpPLL) || !Math::isFinite(kiPLL) ||
167 !Math::isFinite(kpPowerCtrl) || !Math::isFinite(kiPowerCtrl) ||
168 !Math::isFinite(kpCurrCtrl) || !Math::isFinite(kiCurrCtrl) ||
169 !Math::isFinite(omegaCutoff))
170 throw std::invalid_argument("GFL controller parameters must be finite.");
171
172 if (kiPLL == 0.0)
173 throw std::invalid_argument("GFL PLL integral gain must be non-zero.");
174
175 if (kiPowerCtrl == 0.0)
176 throw std::invalid_argument(
177 "GFL power-controller integral gain must be non-zero.");
178
179 if (kiCurrCtrl == 0.0)
180 throw std::invalid_argument(
181 "GFL current-controller integral gain must be non-zero.");
182
183 if (omegaCutoff <= 0.0)
184 throw std::invalid_argument(
185 "GFL power-filter cutoff frequency must be positive.");
186
187 mKpPLL = kpPLL;
188 mKiPLL = kiPLL;
189
190 mKpPowerCtrl = kpPowerCtrl;
191 mKiPowerCtrl = kiPowerCtrl;
192
193 mKpCurrCtrl = kpCurrCtrl;
194 mKiCurrCtrl = kiCurrCtrl;
195
196 mOmegaCutoff = omegaCutoff;
197
198 // Fix relative to the legacy class:
199 // PLL nominal frequency is omega_nom, NOT the power-filter cutoff.
200 mPLL->setParameters(mKpPLL, mKiPLL, mOmegaN);
201 mPLL->composeStateSpaceMatrices();
202
203 mPowerControllerVSI->setControllerParameters(
205
207}
208
210
211 if (!Math::isFinite(lf) || !Math::isFinite(cf) || !Math::isFinite(rf))
212 throw std::invalid_argument("GFL filter parameters must be finite.");
213
214 if (lf <= 0.0)
215 throw std::invalid_argument("GFL filter inductance Lf must be positive.");
216
217 if (cf <= 0.0)
218 throw std::invalid_argument("GFL filter capacitance Cf must be positive.");
219
220 if (rf < 0.0)
221 throw std::invalid_argument(
222 "GFL filter resistance Rf must be non-negative.");
223
224 mLf = lf;
225 mCf = cf;
226 mRf = rf;
227
228 if (mUseSubcomponents) {
232 }
233
235
236 SPDLOG_LOGGER_INFO(mSLog,
237 "GFL filter parameters:"
238 "\n Lf = {} H"
239 "\n Cf = {} F"
240 "\n Rf = {} Ohm"
241 "\n Rc = removed",
242 mLf, mCf, mRf);
243}
244
246 Real phiQInit, Real gammaDInit,
247 Real gammaQInit) {
248
249 if (!Math::isFinite(pInit) || !Math::isFinite(qInit) ||
250 !Math::isFinite(phiDInit) || !Math::isFinite(phiQInit) ||
251 !Math::isFinite(gammaDInit) || !Math::isFinite(gammaQInit))
252 throw std::invalid_argument("GFL initial state values must be finite.");
253
254 mPInitManual = pInit;
255 mQInitManual = qInit;
256 mPhiDInitManual = phiDInit;
257 mPhiQInitManual = phiQInit;
258 mGammaDInitManual = gammaDInit;
259 mGammaQInitManual = gammaQInit;
260
262
263 mPowerControllerVSI->setInitialStateValues(pInit, qInit, phiDInit, phiQInit,
264 gammaDInit, gammaQInit);
265}
266
269
270 MatrixFixedSize<2, 3> transform;
271
272 const Real k = std::sqrt(2.0 / 3.0);
273 const Real c = std::cos(theta);
274 const Real s = std::sin(theta);
275 const Real c120 = 0.5 * std::sqrt(3.0);
276
277 transform << k * c, k * (-0.5 * c + c120 * s), k * (-0.5 * c - c120 * s),
278
279 -k * s, k * (0.5 * s + c120 * c), k * (0.5 * s - c120 * c);
280
281 return transform;
282}
283
286
287 MatrixFixedSize<3, 2> transform;
288
289 const Real k = std::sqrt(2.0 / 3.0);
290 const Real c = std::cos(theta);
291 const Real s = std::sin(theta);
292 const Real c120 = 0.5 * std::sqrt(3.0);
293
294 transform << k * c, -k * s,
295
296 k * (-0.5 * c + c120 * s), k * (0.5 * s + c120 * c),
297
298 k * (-0.5 * c - c120 * s), k * (0.5 * s - c120 * c);
299
300 return transform;
301}
302
305 const Matrix &fabc) const {
306
307 const MatrixFixedSize<3, 1> phases = fabc;
308 return getParkTransformMatrixPowerInvariant(theta) * phases;
309}
310
313 const Matrix &fdq) const {
314
315 const MatrixFixedSize<2, 1> dq = fdq;
317}
318
320
321 // Correct voltage measurement:
322 // capacitor voltage == PCC voltage because Rc has been removed and Cf is
323 // connected directly at the external terminal.
324 const Matrix vPccAbc = **mIntfVoltage;
325
326 // Correct current measurement:
327 // mIntfCurrent follows DPsim's component convention and points from the
328 // external network into the converter. The GFL controller uses positive
329 // converter-to-grid injection.
330 const Matrix iGridAbc = -**mIntfCurrent;
331
332 const auto vDq = parkTransformPowerInvariant(theta, vPccAbc);
333
334 const auto iDq = parkTransformPowerInvariant(theta, iGridAbc);
335
336 **mVcd = vDq(0, 0);
337 **mVcq = vDq(1, 0);
338
339 **mIgridD = iDq(0, 0);
340 **mIgridQ = iDq(1, 0);
341}
342
344 **mPInst = **mVcd * **mIgridD + **mVcq * **mIgridQ;
345
346 **mQInst = -**mVcd * **mIgridQ + **mVcq * **mIgridD;
347
348 // PLL output is [theta, phi_pll].
349 const Real phiPLL = (**mPllOutput)(1, 0);
350
351 **mOmegaPLL = mOmegaN + mKpPLL * **mVcq + mKiPLL * phiPLL;
352}
353
355
356 if (!mParametersSet)
357 throw std::logic_error(
358 "GFL::setParameters() must be called before initialization.");
359
361 throw std::logic_error(
362 "GFL::setFilterParameters() must be called before initialization.");
363
365 throw std::logic_error(
366 "GFL::setControllerParameters() must be called before initialization.");
367
368 const Real omega = 2.0 * PI * frequency;
369
370 const Complex j(0.0, 1.0);
371
372 // -------------------------------------------------------------------------
373 // PCC voltage from power flow
374 // -------------------------------------------------------------------------
375 MatrixComp vPcc = MatrixComp::Zero(3, 1);
376
377 vPcc(0, 0) = RMS3PH_TO_PEAK1PH * initialSingleVoltage(0);
378
379 vPcc(1, 0) = vPcc(0, 0) * SHIFT_TO_PHASE_B;
380
381 vPcc(2, 0) = vPcc(0, 0) * SHIFT_TO_PHASE_C;
382
383 if (std::abs(vPcc(0, 0)) < 1e-12)
384 throw std::runtime_error("GFL cannot initialize from zero PCC voltage.");
385
386 // -------------------------------------------------------------------------
387 // Positive converter-to-grid current from requested P/Q
388 // -------------------------------------------------------------------------
389 const Complex sRef(mPRef, mQRef);
390
391 const Complex iGridA = std::conj((2.0 / 3.0) * sRef / vPcc(0, 0));
392
393 MatrixComp iGrid = MatrixComp::Zero(3, 1);
394
395 iGrid(0, 0) = iGridA;
396 iGrid(1, 0) = iGridA * SHIFT_TO_PHASE_B;
397 iGrid(2, 0) = iGridA * SHIFT_TO_PHASE_C;
398
399 // Filter steady state.
400 const MatrixComp iCf = j * omega * mCf * vPcc;
401
402 const MatrixComp iF = iGrid + iCf;
403
404 const MatrixComp vInductorInput = vPcc + j * omega * mLf * iF;
405
406 const MatrixComp vSource = vInductorInput + mRf * iF;
407
408 // Parent virtual nodes.
409 mVirtualNodes[0]->setInitialVoltage(PEAK1PH_TO_RMS3PH * vSource);
410
411 mVirtualNodes[1]->setInitialVoltage(PEAK1PH_TO_RMS3PH * vInductorInput);
412
413 // Parent external interface.
414 **mIntfVoltage = vPcc.real();
415
416 // DPsim interface current points from grid into component.
417 **mIntfCurrent = -iGrid.real();
418
419 // -------------------------------------------------------------------------
420 // Controlled source
421 // -------------------------------------------------------------------------
422 if (mUseSubcomponents) {
423 mSubControlledVoltageSource->setParameters(
424 mVirtualNodes[0]->initialVoltage(), 0.0);
426 mSubResistorF->connect({mVirtualNodes[0], mVirtualNodes[1]});
427 mSubInductorF->connect({mVirtualNodes[1], mTerminals[0]->node()});
428 mSubCapacitorF->connect({mTerminals[0]->node(), SimNode::GND});
429 } else {
430 mVirtualNodes[2]->setInitialVoltage(Matrix::Zero(3, 1));
431 mDirectInductorVoltage = (vPcc - vInductorInput).real();
432 // The child inductor is oriented from the converter-side virtual node to
433 // the PCC; its terminal voltage is Vpcc - VinductorInput = -jωLf·iF.
434 mDirectInductorCurrent = -iF.real();
435 mDirectCapacitorCurrent = -iCf.real();
436 mDirectSourceVoltage = vSource.real();
437 // Match the legacy controlled voltage-source attribute at t=0: its
438 // interface voltage is still zero until its first pre-step. The direct
439 // source attribute is populated in mnaParentPreStep before the first
440 // solve, while the branch value already carries the initialized command.
441 }
442
443 // Subcomponents are initialized recursively by CompositePowerComp.
444
445 // -------------------------------------------------------------------------
446 // Controller steady-state initialization
447 // -------------------------------------------------------------------------
448 const Real theta0 = std::arg(vPcc(0, 0));
449
450 const Matrix vPccAbc0 = vPcc.real();
451
452 const Matrix iGridAbc0 = iGrid.real();
453
454 const Matrix vSourceAbc0 = vSource.real();
455
456 const auto vDq0 = parkTransformPowerInvariant(theta0, vPccAbc0);
457
458 const auto iDq0 = parkTransformPowerInvariant(theta0, iGridAbc0);
459
460 const auto vSourceDq0 = parkTransformPowerInvariant(theta0, vSourceAbc0);
461
462 **mVcd = vDq0(0, 0);
463
464 **mVcq = vDq0(1, 0);
465
466 **mIgridD = iDq0(0, 0);
467
468 **mIgridQ = iDq0(1, 0);
469
470 const Real pInit = **mVcd * **mIgridD + **mVcq * **mIgridQ;
471
472 const Real qInit = -**mVcd * **mIgridQ + **mVcq * **mIgridD;
473
474 // PLL state = [theta, phi_pll].
475 Matrix pllStateInit = Matrix::Zero(2, 1);
476
477 Matrix pllOutputInit = Matrix::Zero(2, 1);
478
479 pllStateInit(0, 0) = theta0;
480
481 pllStateInit(1, 0) = (omega - mOmegaN) / mKiPLL;
482
483 pllOutputInit = pllStateInit;
484
485 mPLL->setInitialValues(**mVcq, pllStateInit, pllOutputInit);
486
488 // Choose controller integrators so that:
489 // i_ref_dq = i_grid_dq
490 // v_ref_dq = required steady-state source voltage.
491 const Real phiDInit =
492 (**mIgridD + mKpPowerCtrl * (pInit - mPRef)) / mKiPowerCtrl;
493
494 const Real phiQInit =
495 (**mIgridQ - mKpPowerCtrl * (qInit - mQRef)) / mKiPowerCtrl;
496
497 const Real iRefD = mKpPowerCtrl * (mPRef - pInit) + mKiPowerCtrl * phiDInit;
498
499 const Real iRefQ = mKpPowerCtrl * (qInit - mQRef) + mKiPowerCtrl * phiQInit;
500
501 const Real gammaDInit =
502 (vSourceDq0(0, 0) + mKpCurrCtrl * (**mIgridD - iRefD)) / mKiCurrCtrl;
503
504 const Real gammaQInit =
505 (vSourceDq0(1, 0) + mKpCurrCtrl * (**mIgridQ - iRefQ)) / mKiCurrCtrl;
506
507 mPowerControllerVSI->setInitialStateValues(pInit, qInit, phiDInit, phiQInit,
508 gammaDInit, gammaQInit);
509 }
510
511 // Avoid a zero source command before the first mnaParentPreStep().
512 **mVsref = vSourceAbc0;
513
515
516 SPDLOG_LOGGER_INFO(
517 mSLog,
518 "\n--- EMT_Ph3_GFL initialization ---"
519 "\nPCC voltage abc: {:s}"
520 "\nGrid injection abc: {:s}"
521 "\nFilter current abc: {:s}"
522 "\nSource voltage abc: {:s}"
523 "\nP/Q init: [{:.6e}, {:.6e}]"
524 "\nVc dq: [{:.6e}, {:.6e}]"
525 "\nIgrid dq: [{:.6e}, {:.6e}]"
526 "\n--- Initialization finished ---",
528 Logger::matrixToString(iF.real()), Logger::matrixToString(vSourceAbc0),
529 pInit, qInit, **mVcd, **mVcq, **mIgridD, **mIgridQ);
530}
531
533 Attribute<Matrix>::Ptr leftVector) {
534
535 mTimeStep = timeStep;
536
537 if (!mUseSubcomponents) {
538 mDirectResistorConductance = Matrix::Identity(3, 3) / std::max(mRf, 1e-12);
540 (timeStep / (2. * mLf)) * Matrix::Identity(3, 3);
542 (2. * mCf / timeStep) * Matrix::Identity(3, 3);
547 mDirectCapacitorConductance * (**mIntfVoltage);
548 }
549
550 // Initialize power controller first. Its initial output is the desired
551 // source voltage in dq when the automatically computed states are used.
552 mPowerControllerVSI->initializeStateSpaceModel(omega, timeStep, leftVector);
553
554 // PowerControllerVSI::initializeStateSpaceModel() currently assigns its
555 // internal cutoff member from the system omega argument. Restore the
556 // explicitly configured controller cutoff so this GFL also works correctly
557 // when omegaCutoff != omega_nom.
558 mPowerControllerVSI->setControllerParameters(
560
561 mPLL->setSimulationParameters(timeStep);
562
563 // Keep the PF-derived source-voltage command from initialization.
565
566 mMnaTasks.push_back(std::make_shared<ControlPreStep>(*this));
567
568 mMnaTasks.push_back(std::make_shared<ControlStep>(*this));
569}
570
572 SparseMatrixRow &systemMatrix) {
574 return;
575
577 const UInt sourceNode = mVirtualNodes[0]->matrixNodeIndex(PhaseType::A);
578 const UInt filterNode = mVirtualNodes[1]->matrixNodeIndex(PhaseType::A);
579 const UInt sourceCurrent = mVirtualNodes[2]->matrixNodeIndex(PhaseType::A);
581 systemMatrix, sourceNode, filterNode,
582 true, true, mSLog);
584 mDirectInductorConductance, systemMatrix, filterNode, matrixNodeIndex(0),
585 true, terminalNotGrounded(0), mSLog);
587 systemMatrix, matrixNodeIndex(0), 0,
588 terminalNotGrounded(0), false, mSLog);
589 for (UInt phase = 0; phase < 3; ++phase) {
590 Math::addToMatrixElement(systemMatrix, sourceNode + phase,
591 sourceCurrent + phase, 1.);
592 Math::addToMatrixElement(systemMatrix, sourceCurrent + phase,
593 sourceNode + phase, 1.);
594 }
595}
596
599 return;
600
601 const UInt filterNode = mVirtualNodes[1]->matrixNodeIndex(PhaseType::A);
602 const UInt sourceCurrent = mVirtualNodes[2]->matrixNodeIndex(PhaseType::A);
603 for (UInt phase = 0; phase < 3; ++phase) {
604 Math::setVectorElement(rightVector, sourceCurrent + phase,
605 mDirectSourceVoltage(phase, 0));
606 Math::setVectorElement(rightVector, filterNode + phase,
607 mDirectInductorHistory(phase, 0));
608 if (terminalNotGrounded(0)) {
609 const UInt terminalIndex = matrixNodeIndex(0, phase);
610 Math::setVectorElement(rightVector, terminalIndex,
611 -mDirectInductorHistory(phase, 0) +
612 mDirectCapacitorHistory(phase, 0));
613 }
614 }
615}
616
618 AttributeBase::List &prevStepDependencies,
619 AttributeBase::List &attributeDependencies,
620 AttributeBase::List &modifiedAttributes) {
621
622 mPLL->signalAddPreStepDependencies(prevStepDependencies,
623 attributeDependencies, modifiedAttributes);
624
625 mPowerControllerVSI->signalAddPreStepDependencies(
626 prevStepDependencies, attributeDependencies, modifiedAttributes);
627}
628
629void EMT::Ph3::GFL::controlPreStep(Real time, Int timeStepCount) {
630
631 mPLL->signalPreStep(time, timeStepCount);
632
633 mPowerControllerVSI->signalPreStep(time, timeStepCount);
634}
635
637 AttributeBase::List &prevStepDependencies,
638 AttributeBase::List &attributeDependencies,
639 AttributeBase::List &modifiedAttributes) {
640
641 // Measurements are updated inside controlStep() to avoid scheduler cycles.
642 mPLL->signalAddStepDependencies(prevStepDependencies, attributeDependencies,
643 modifiedAttributes);
644
645 mPowerControllerVSI->signalAddStepDependencies(
646 prevStepDependencies, attributeDependencies, modifiedAttributes);
647
648 // The control calculation needs the solved PCC quantities from the network.
649 attributeDependencies.push_back(mIntfCurrent);
650
651 attributeDependencies.push_back(mIntfVoltage);
652
653 // Only the externally consumed controller output is a scheduler-visible
654 // modification of this parent task.
655 modifiedAttributes.push_back(mVsref);
656}
657
658void EMT::Ph3::GFL::controlStep(Real time, Int timeStepCount) {
659
660 // Predict PLL angle to the measurement instant.
661 Real thetaMeasurement = mPLL->mOutputPrev->get()(0, 0);
662
663 if (timeStepCount > 0) {
664 const Real phiPLLPrev = mPLL->mOutputPrev->get()(1, 0);
665
666 const Real omegaPLLPrev = mOmegaN + mKpPLL * **mVcq + mKiPLL * phiPLLPrev;
667
668 thetaMeasurement += mTimeStep * omegaPLLPrev;
669 }
670
671 updateMeasurementAttributes(thetaMeasurement);
672
673 // Update PLL and cascaded P/Q-current controller.
674 mPLL->signalStep(time, timeStepCount);
675
676 mPowerControllerVSI->signalStep(time, timeStepCount);
677
678 // Rotate the command to the next EMT sample.
679 const Real thetaPLL = (**mPllOutput)(0, 0);
680
681 const Real phiPLL = (**mPllOutput)(1, 0);
682
683 const Real omegaPLL = mOmegaN + mKpPLL * **mVcq + mKiPLL * phiPLL;
684
685 const Real thetaCommand = thetaPLL + mTimeStep * omegaPLL;
686
687 **mVsref =
689
691}
692
694 AttributeBase::List &prevStepDependencies,
695 AttributeBase::List &attributeDependencies,
696 AttributeBase::List &modifiedAttributes) {
697
698 prevStepDependencies.push_back(mVsref);
699
700 prevStepDependencies.push_back(mIntfCurrent);
701
702 prevStepDependencies.push_back(mIntfVoltage);
703
704 attributeDependencies.push_back(mPowerControllerVSI->mOutputPrev);
705
706 attributeDependencies.push_back(mPLL->mOutputPrev);
707
708 modifiedAttributes.push_back(mRightVector);
709}
710
711void EMT::Ph3::GFL::mnaParentPreStep(Real time, Int timeStepCount) {
712
713 if (mUseSubcomponents) {
714 if (mWithControl) {
715 // VoltageSource::mVoltageRef uses RMS line-line-equivalent scaling,
716 // whereas mVsref stores instantaneous phase-peak abc values.
718 **mVsref);
719 }
720 std::dynamic_pointer_cast<MNAInterface>(mSubControlledVoltageSource)
721 ->mnaPreStep(time, timeStepCount);
722 } else {
723 if (mWithControl)
730 mDirectCapacitorConductance * (**mIntfVoltage);
731 }
732
734}
735
737 AttributeBase::List &prevStepDependencies,
738 AttributeBase::List &attributeDependencies,
739 AttributeBase::List &modifiedAttributes,
740 Attribute<Matrix>::Ptr &leftVector) {
741
742 attributeDependencies.push_back(leftVector);
743
744 modifiedAttributes.push_back(mIntfVoltage);
745
746 modifiedAttributes.push_back(mIntfCurrent);
747}
748
750 Attribute<Matrix>::Ptr &leftVector) {
751
752 // Subcomponent post-step tasks run before the parent, so the inductor current
753 // has already been updated when this executes.
754 mnaCompUpdateCurrent(**leftVector);
755
756 mnaCompUpdateVoltage(**leftVector);
757}
758
760
761 // Net current entering the complete converter component.
762 if (mUseSubcomponents) {
763 **mIntfCurrent = mSubInductorF->mIntfCurrent->get() -
764 mSubCapacitorF->mIntfCurrent->get();
765 return;
766 }
767
768 const UInt filterNode = mVirtualNodes[1]->matrixNodeIndex(PhaseType::A);
769 Matrix terminalVoltage = Matrix::Zero(3, 1);
770 if (terminalNotGrounded(0))
771 for (UInt phase = 0; phase < 3; ++phase)
772 terminalVoltage(phase, 0) =
773 Math::realFromVectorElement(leftVector, matrixNodeIndex(0, phase));
774
775 // The legacy inductor/capacitor children update their terminal voltages
776 // from the solved vector before updating their histories. The parent
777 // interface attribute still contains the previous step's value here, so
778 // using it would introduce an unintended one-step delay in direct mode.
779 mDirectInductorVoltage = terminalVoltage;
780 for (UInt phase = 0; phase < 3; ++phase)
781 mDirectInductorVoltage(phase, 0) -=
782 Math::realFromVectorElement(leftVector, filterNode + phase);
788}
789
791
792 if (mUseSubcomponents) {
793 for (auto virtualNode : mVirtualNodes)
794 virtualNode->mnaUpdateVoltage(leftVector);
795 } else {
796 mVirtualNodes[0]->mnaUpdateVoltage(leftVector);
797 mVirtualNodes[1]->mnaUpdateVoltage(leftVector);
799 }
800
801 (**mIntfVoltage).setZero();
802
803 if (terminalNotGrounded(0)) {
804 (**mIntfVoltage)(0, 0) =
806
807 (**mIntfVoltage)(1, 0) =
809
810 (**mIntfVoltage)(2, 0) =
812 }
813}
std::vector< Ptr > List
Definition Attribute.h:123
AttributePointer< Attribute< T > > Ptr
Definition Attribute.h:249
void addMNASubComponent(typename SimPowerComp< Real >::Ptr subc, MNA_SUBCOMP_TASK_ORDER preStepOrder, MNA_SUBCOMP_TASK_ORDER postStepOrder, Bool contributeToRightVector)
void mnaCompApplyRightSideVectorStamp(Matrix &rightVector) override
CompositePowerComp(String uid, String name, Bool hasPreStep, Bool hasPostStep, Logger::Level logLevel)
Matrix mDirectCapacitorCurrent
Definition EMT_Ph3_GFL.h:99
void setInitialStateValues(Real pInit, Real qInit, Real phiDInit, Real phiQInit, Real gammaDInit, Real gammaQInit)
const Attribute< Real >::Ptr mIgridQ
MatrixFixedSize< 2, 1 > parkTransformPowerInvariant(Real theta, const Matrix &fabc) const
const Attribute< Matrix >::Ptr mPowerctrlStates
const Attribute< Matrix >::Ptr mPowerctrlInputs
Power-controller logging vectors.
void controlPreStep(Real time, Int timeStepCount)
void mnaParentPreStep(Real time, Int timeStepCount) override
void updateMeasurementAttributes(Real theta)
void addControlStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes)
Matrix mDirectInductorVoltage
Definition EMT_Ph3_GFL.h:96
SimPowerComp< Real >::Ptr clone(String name) override
Returns a modified copy of the component with the given suffix added to the name and without.
void mnaCompUpdateCurrent(const Matrix &leftVector) override
void mnaCompUpdateVoltage(const Matrix &leftVector) override
std::shared_ptr< EMT::Ph3::Capacitor > mSubCapacitorF
Definition EMT_Ph3_GFL.h:91
void addControlPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes)
const Attribute< Matrix >::Ptr mPowerctrlOutputs
void setFilterParameters(Real lf, Real cf, Real rf)
Filter parameters. No Rc exists in this model.
Matrix mDirectInductorConductance
Definition EMT_Ph3_GFL.h:94
const Attribute< Real >::Ptr mVcd
PCC voltage in controller dq frame.
const Attribute< Real >::Ptr mVcq
Matrix mDirectInductorCurrent
Definition EMT_Ph3_GFL.h:97
void setControllerParameters(Real kpPLL, Real kiPLL, Real kpPowerCtrl, Real kiPowerCtrl, Real kpCurrCtrl, Real kiCurrCtrl, Real omegaCutoff)
Controller parameters.
const Attribute< Real >::Ptr mPInst
Instantaneous controller-frame active/reactive power.
Bool mControllerParametersSet
Definition EMT_Ph3_GFL.h:68
Matrix mDirectCapacitorHistory
void setParameters(Real sysOmega, Real sysVoltNom, Real pRef, Real qRef)
General operating parameters.
const Attribute< Matrix >::Ptr mPllOutput
PLL output [theta, phi_pll].
void mnaParentPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
std::shared_ptr< Signal::PLL > mPLL
Definition EMT_Ph3_GFL.h:82
std::shared_ptr< EMT::Ph3::Inductor > mSubInductorF
Definition EMT_Ph3_GFL.h:90
const Attribute< Real >::Ptr mOmegaPLL
Estimated PLL angular frequency [rad/s].
Matrix mDirectSourceVoltage
void mnaParentApplyRightSideVectorStamp(Matrix &rightVector) override
void initializeParentFromNodesAndTerminals(Real frequency) override
const Attribute< Matrix >::Ptr mVs
Actual controlled source voltage.
std::shared_ptr< Signal::PowerControllerVSI > mPowerControllerVSI
Definition EMT_Ph3_GFL.h:83
const Attribute< Matrix >::Ptr mVsref
Controlled source voltage reference in abc instantaneous peak values.
std::shared_ptr< EMT::Ph3::VoltageSource > mSubControlledVoltageSource
Definition EMT_Ph3_GFL.h:88
const Attribute< Real >::Ptr mIgridD
Positive converter-to-grid current in controller dq frame.
void mnaParentAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
MatrixFixedSize< 3, 1 > inverseParkTransformPowerInvariant(Real theta, const Matrix &fdq) const
void controlStep(Real time, Int timeStepCount)
std::shared_ptr< EMT::Ph3::Resistor > mSubResistorF
Definition EMT_Ph3_GFL.h:89
Matrix mDirectCapacitorConductance
Definition EMT_Ph3_GFL.h:95
const Attribute< Matrix >::Ptr mDirectSourceVoltageAttr
Storage for the source voltage in direct mode, exposed through mVs.
void mnaParentApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) override
MatrixFixedSize< 2, 3 > getParkTransformMatrixPowerInvariant(Real theta) const
void updatePowerAndFrequencyAttributes()
Matrix mDirectInductorHistory
Definition EMT_Ph3_GFL.h:98
Matrix mDirectResistorConductance
Definition EMT_Ph3_GFL.h:93
MatrixFixedSize< 3, 2 > getInverseParkTransformMatrixPowerInvariant(Real theta) const
void mnaParentInitialize(Real omega, Real timeStep, Attribute< Matrix >::Ptr leftVector) override
void mnaParentAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override
const Attribute< Real >::Ptr mQInst
GFL(String uid, String name, Logger::Level logLevel=Logger::Level::off)
const Attribute< String >::Ptr mName
Human readable name.
String uid()
Returns unique id.
String type()
Get component type (cross-platform)
AttributeList::Ptr mAttributes
Attribute List.
spdlog::level::level_enum Level
Definition Logger.h:33
static String matrixToString(const Matrix &mat)
Definition Logger.cpp:31
Attribute< Matrix >::Ptr mRightVector
static void stampConductanceMatrix(const Matrix &conductanceMat, SparseMatrixRow &mat, UInt node1Index, UInt node2Index, Bool isTerminal1NotGrounded, Bool isTerminal2NotGrounded, const Logger::Log &mSLog)
static Real realFromVectorElement(const Matrix &mat, Matrix::Index row)
static void setVectorElement(Matrix &mat, Matrix::Index row, Complex value, Int maxFreq=1, Int freqIdx=0, Matrix::Index colOffset=0)
static Matrix singlePhaseParameterToThreePhase(Real parameter)
To convert single phase parameters to symmetrical three phase ones.
static bool isFinite(Real value)
static void addToMatrixElement(SparseMatrixRow &mat, Matrix::Index row, Matrix::Index column, Complex value, Int maxFreq=1, Int freqIdx=0)
static Ptr GND
Definition SimNode.h:37
UInt matrixNodeIndex(UInt nodeIndex)
const Attribute< MatrixVar< Real > >::Ptr mIntfCurrent
SimTerminal< Real >::List mTerminals
void setVirtualNodeNumber(UInt num)
const Attribute< MatrixVar< Real > >::Ptr mIntfVoltage
MatrixComp initialVoltage(UInt index)
SimNode< Real >::Ptr virtualNode(UInt index)
std::shared_ptr< SimPowerComp< VarType > > Ptr
SimNode< Real >::List mVirtualNodes
Bool terminalNotGrounded(UInt index)
Complex initialSingleVoltage(UInt index)
void setTerminalNumber(UInt num)
Logger::Level mLogLevel
Component logger control for internal variables.
bool mParametersSet
Flag indicating that parameters are set via setParameters() function.
Logger::Log mSLog
Component logger.
static std::shared_ptr< VoltageSource > make(Args &&...args)
Definition PtrFactory.h:19
#define PI
Definition Definitions.h:43
#define RMS3PH_TO_PEAK1PH
Definition Definitions.h:50
#define SHIFT_TO_PHASE_C
Definition Definitions.h:47
#define PEAK1PH_TO_RMS3PH
Definition Definitions.h:51
#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
Eigen::SparseMatrix< Real, Eigen::RowMajor > SparseMatrixRow
Sparse matrix for real numbers (row major).
Definition Definitions.h:72