512 if (x.rows() != mStateSize || x.cols() != 1)
513 throw std::invalid_argument(
514 "SSN_GFM state vector has an invalid dimension.");
516 if (u.rows() != mInputSize || u.cols() != 1)
517 throw std::invalid_argument(
518 "SSN_GFM input vector has an invalid dimension.");
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);
531 getInverseParkTransformMatrix(theta);
533 using ThreePhaseRow = Eigen::Matrix<Real, 1, 3, Eigen::RowMajor>;
534 const ThreePhaseRow tD = parkTransform.row(0);
535 const ThreePhaseRow tQ = parkTransform.row(1);
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);
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();
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;
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;
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;
572 const InputRow dIGridDByU = -tD / mRc;
573 const InputRow dIGridQByU = -tQ / mRc;
575 StateRow dPByX = 1.5 * (iGridD * dVcDByX + vcD * dIGridDByX +
576 iGridQ * dVcQByX + vcQ * dIGridQByX);
577 const InputRow dPByU = 1.5 * (vcD * dIGridDByU + vcQ * dIGridQByU);
579 StateRow dQByX = 1.5 * (iGridD * dVcQByX + vcQ * dIGridDByX -
580 iGridQ * dVcDByX - vcD * dIGridQByX);
581 const InputRow dQByU = 1.5 * (vcQ * dIGridDByU - vcD * dIGridQByU);
583 dPByX(0, Theta) = 0.0;
584 dQByX(0, Theta) = 0.0;
586 const Real pccVoltageMagnitude = std::sqrt(vcD * vcD + vcQ * vcQ);
587 StateRow dPccVoltageMagnitudeByX = StateRow::Zero();
589 if (pccVoltageMagnitude > 1e-12) {
590 dPccVoltageMagnitudeByX =
591 (vcD * dVcDByX + vcQ * dVcQByX) / pccVoltageMagnitude;
592 dPccVoltageMagnitudeByX(0, Theta) = 0.0;
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();
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;
609 const StateRow dVoltageErrorDByX = unitVoltageMagnitude -
610 mVirtualResistance * dIfDByX +
611 mVirtualReactance * dIfQByX - dVcDByX;
612 const StateRow dVoltageErrorQByX =
613 -mVirtualResistance * dIfQByX - mVirtualReactance * dIfDByX - dVcQByX;
615 const StateRow dCurrentReferenceDByX =
616 mGridCurrentFeedforward * dIGridDByX -
617 mCf * (vcQ * unitOmega + omega * dVcQByX) +
618 mKpVoltage * dVoltageErrorDByX + mKiVoltage * unitVoltageIntegratorD;
619 const InputRow dCurrentReferenceDByU = mGridCurrentFeedforward * dIGridDByU;
621 const StateRow dCurrentReferenceQByX =
622 mGridCurrentFeedforward * dIGridQByX +
623 mCf * (vcD * unitOmega + omega * dVcDByX) +
624 mKpVoltage * dVoltageErrorQByX + mKiVoltage * unitVoltageIntegratorQ;
625 const InputRow dCurrentReferenceQByU = mGridCurrentFeedforward * dIGridQByU;
627 const StateRow dCurrentErrorDByX = dCurrentReferenceDByX - dIfDByX;
628 const StateRow dCurrentErrorQByX = dCurrentReferenceQByX - dIfQByX;
629 const InputRow dCurrentErrorDByU = dCurrentReferenceDByU;
630 const InputRow dCurrentErrorQByU = dCurrentReferenceQByU;
632 const StateRow dCapacitorCurrentDByX = dIfDByX - dIGridDByX;
633 const StateRow dCapacitorCurrentQByX = dIfQByX - dIGridQByX;
634 const InputRow dCapacitorCurrentDByU = -dIGridDByU;
635 const InputRow dCapacitorCurrentQByU = -dIGridQByU;
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;
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;
653 A.setZero(mStateSize, mStateSize);
654 B.setZero(mStateSize, mInputSize);
655 C.setZero(mOutputSize, mStateSize);
656 D.setZero(mOutputSize, mInputSize);
658 A.row(PFiltered) = mPowerFilterCutoff * dPByX;
659 A(PFiltered, PFiltered) -= mPowerFilterCutoff;
660 B.row(PFiltered) = mPowerFilterCutoff * dPByU;
662 A.row(QFiltered) = mPowerFilterCutoff * dQByX;
663 A(QFiltered, QFiltered) -= mPowerFilterCutoff;
664 B.row(QFiltered) = mPowerFilterCutoff * dQByU;
666 const Real omegaDenominator = regularizedOmega(omega);
667 const Real omegaDenominatorDerivative =
668 std::abs(omega) >= std::abs(omegaDenominator) ? 1.0 : 0.0;
670 A(Omega, PFiltered) = -1.0 / (mVirtualInertia * omegaDenominator);
671 A(Omega, Omega) = (-(mPRef - pFiltered) * omegaDenominatorDerivative /
672 (omegaDenominator * omegaDenominator) -
673 mDampingCoefficient) /
676 A(Theta, Omega) = 1.0;
678 if (mReactiveDroopCutoff > 0.0) {
679 A(VoltageMagnitude, QFiltered) =
680 -mReactiveDroopCutoff * mReactivePowerDroop;
681 A(VoltageMagnitude, VoltageMagnitude) = -mReactiveDroopCutoff;
683 A(VoltageMagnitude, QFiltered) = -mReactiveIntegralGain;
684 A.row(VoltageMagnitude) -= mVoltageDroopGain * dPccVoltageMagnitudeByX;
687 A.row(VoltageIntegratorD) = dVoltageErrorDByX;
688 A.row(VoltageIntegratorQ) = dVoltageErrorQByX;
690 A.row(CurrentIntegratorD) = dCurrentErrorDByX;
691 B.row(CurrentIntegratorD) = dCurrentErrorDByU;
692 A.row(CurrentIntegratorQ) = dCurrentErrorQByX;
693 B.row(CurrentIntegratorQ) = dCurrentErrorQByU;
695 A.row(DelayVoltageD) = mDelayBandwidth * dConverterVoltageReferenceDByX;
696 A(DelayVoltageD, DelayVoltageD) -= mDelayBandwidth;
697 B.row(DelayVoltageD) = mDelayBandwidth * dConverterVoltageReferenceDByU;
699 A.row(DelayVoltageQ) = mDelayBandwidth * dConverterVoltageReferenceQByX;
700 A(DelayVoltageQ, DelayVoltageQ) -= mDelayBandwidth;
701 B.row(DelayVoltageQ) = mDelayBandwidth * dConverterVoltageReferenceQByU;
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);
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;
715 C.block(0, VcA, 3, 3) = -identity / mRc;
724 A.setZero(mStateSize, mStateSize);
725 B.setZero(mStateSize, mInputSize);
726 C.setZero(mOutputSize, mStateSize);
727 D.setZero(mOutputSize, mInputSize);
729 Matrix fPlus = Matrix::Zero(mStateSize, 1);
730 Matrix fMinus = Matrix::Zero(mStateSize, 1);
732 Matrix gPlus = Matrix::Zero(mOutputSize, 1);
733 Matrix gMinus = Matrix::Zero(mOutputSize, 1);
736 for (
Int column = 0; column < mStateSize; ++column) {
738 mJacobianAbsoluteStep +
739 mJacobianRelativeStep * std::max(1.0, std::abs(x(column, 0)));
744 xPlus(column, 0) += step;
745 xMinus(column, 0) -= step;
747 evaluateStateDerivative(xPlus, u, fPlus);
748 evaluateStateDerivative(xMinus, u, fMinus);
750 evaluateOutput(xPlus, u, gPlus);
751 evaluateOutput(xMinus, u, gMinus);
753 A.col(column) = (fPlus - fMinus) / (2.0 * step);
754 C.col(column) = (gPlus - gMinus) / (2.0 * step);
758 for (
Int column = 0; column < mInputSize; ++column) {
760 mJacobianAbsoluteStep +
761 mJacobianRelativeStep * std::max(1.0, std::abs(u(column, 0)));
766 uPlus(column, 0) += step;
767 uMinus(column, 0) -= step;
769 evaluateStateDerivative(x, uPlus, fPlus);
770 evaluateStateDerivative(x, uMinus, fMinus);
772 evaluateOutput(x, uPlus, gPlus);
773 evaluateOutput(x, uMinus, gMinus);
775 B.col(column) = (fPlus - fMinus) / (2.0 * step);
776 D.col(column) = (gPlus - gMinus) / (2.0 * step);
883 throw std::logic_error(
"setParameters() must be called before "
884 "initializeFromNodesAndTerminals().");
886 const Real omegaInitialization = 2.0 *
PI * frequency;
887 const Complex imaginaryUnit(0.0, 1.0);
889 const Complex powerReference(mPRef, mQRef);
897 MatrixComp iInjectionPhasor = MatrixComp::Zero(3, 1);
906 const Complex vcA = vcPhasor(0, 0);
909 iInjectionPhasor.setZero();
917 const Complex currentA = std::conj(powerReference / (1.5 * vcA));
924 const MatrixComp nextVcPhasor = uPhasor + mRc * nextInjectionCurrent;
926 iInjectionPhasor = nextInjectionCurrent;
929 vcPhasor = nextVcPhasor;
933 vcPhasor = nextVcPhasor;
940 iInjectionPhasor + imaginaryUnit * omegaInitialization * mCf * vcPhasor;
946 vcPhasor + (mRf + imaginaryUnit * omegaInitialization * mLf) * ifPhasor;
948 const Matrix vcAbc0 = vcPhasor.real();
949 const Matrix ifAbc0 = ifPhasor.real();
950 const Matrix iGridAbc0 = iInjectionPhasor.real();
951 const Matrix converterVoltageAbc0 = converterVoltagePhasor.real();
957 const Complex virtualImpedance(mVirtualResistance, mVirtualReactance);
958 const MatrixComp emfPhasor = vcPhasor + virtualImpedance * ifPhasor;
959 const Real theta0 = std::arg(emfPhasor(0, 0));
963 const Matrix vcDq0 = parkTransform * vcAbc0;
964 const Matrix ifDq0 = parkTransform * ifAbc0;
965 const Matrix iGridDq0 = parkTransform * iGridAbc0;
966 const Matrix converterVoltageDq0 = parkTransform * converterVoltageAbc0;
968 const Real vcD0 = vcDq0(0, 0);
969 const Real vcQ0 = vcDq0(1, 0);
971 const Real ifD0 = ifDq0(0, 0);
972 const Real ifQ0 = ifDq0(1, 0);
974 const Real iGridD0 = iGridDq0(0, 0);
975 const Real iGridQ0 = iGridDq0(1, 0);
977 const Real pInitial = 1.5 * (vcD0 * iGridD0 + vcQ0 * iGridQ0);
979 const Real qInitial = 1.5 * (vcQ0 * iGridD0 - vcD0 * iGridQ0);
981 const Real iCapD0 = ifD0 - iGridD0;
982 const Real iCapQ0 = ifQ0 - iGridQ0;
984 Matrix x0 = Matrix::Zero(mStateSize, 1);
986 x0(PFiltered, 0) = pInitial;
987 x0(QFiltered, 0) = qInitial;
989 x0(Omega, 0) = omegaInitialization;
990 x0(Theta, 0) = theta0;
993 x0(VoltageMagnitude, 0) = std::abs(emfPhasor(0, 0));
997 mVoltageSetpoint = x0(VoltageMagnitude, 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;
1017 x0(VoltageIntegratorD, 0) =
1018 (ifD0 - mGridCurrentFeedforward * iGridD0 +
1019 omegaInitialization * mCf * vcQ0 - mKpVoltage * voltageErrorD0) /
1028 x0(VoltageIntegratorQ, 0) =
1029 (ifQ0 - mGridCurrentFeedforward * iGridQ0 -
1030 omegaInitialization * mCf * vcD0 - mKpVoltage * voltageErrorQ0) /
1040 x0(CurrentIntegratorD, 0) =
1041 (converterVoltageDq0(0, 0) - vcD0 + omegaInitialization * mLf * ifQ0 +
1042 mActiveDampingGain * iCapD0) /
1050 x0(CurrentIntegratorQ, 0) =
1051 (converterVoltageDq0(1, 0) - vcQ0 - omegaInitialization * mLf * ifD0 +
1052 mActiveDampingGain * iCapQ0) /
1056 x0(DelayVoltageD, 0) = converterVoltageDq0(0, 0);
1057 x0(DelayVoltageQ, 0) = converterVoltageDq0(1, 0);
1059 x0.block(VcA, 0, 3, 1) = vcAbc0;
1060 x0.block(IfA, 0, 3, 1) = ifAbc0;
1090 Matrix stateDerivative = Matrix::Zero(mStateSize, 1);
1094 const Matrix nonlinearOutput = [&]() {
1095 Matrix output = Matrix::Zero(mOutputSize, 1);
1104 "\n--- SSN GFM initialization ---"
1105 "\nInput voltage u: {:s}"
1106 "\nInterface current y: {:s}"
1108 "\nState derivative norm: {:.6e}"
1109 "\nNonlinear output: {:s}"
1110 "\nSSN output: {:s}"
1111 "\nOutput mismatch 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 ---",
1124 mW.norm(),
mYHist.norm(), pInitial, qInitial, vcD0, vcQ0, iGridD0,
1125 iGridQ0, ifD0, ifQ0, converterVoltageDq0(0, 0),
1126 converterVoltageDq0(1, 0));