DPsim
Loading...
Searching...
No Matches
DP_SSNComp.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
6
7#include <algorithm>
8
9using namespace CPS;
10
12 Logger::Level logLevel)
13 : MNASimPowerComp<Complex>(uid, name, true, true, logLevel), mTimeStep(0.0),
14 mW(MatrixComp::Zero(outputSize, inputSize)),
15 mYHist(MatrixComp::Zero(outputSize, 1)), mInputSize(inputSize),
16 mOutputSize(outputSize), mX(mAttributes->create<MatrixComp>("x")) {
17 mParametersSet = false;
18}
19
20UInt DP::SSNComp::getStateCount() const { return static_cast<UInt>(mA.rows()); }
21
23
25
26const Matrix &DP::SSNComp::getC() const { return mC; }
27
29 const Matrix &C, const Matrix &D,
30 const Matrix &D1) {
31 mParametersSet = false;
32
33 if (A.rows() != A.cols())
34 throw std::invalid_argument("A must be square.");
35
36 if (B.rows() != A.rows() || B.cols() != mInputSize)
37 throw std::invalid_argument("B has invalid dimensions.");
38
39 if (C.rows() != mOutputSize || C.cols() != A.rows())
40 throw std::invalid_argument("C has invalid dimensions.");
41
42 if (D.rows() != mOutputSize || D.cols() != mInputSize)
43 throw std::invalid_argument("D has invalid dimensions.");
44 if (D1.size() != 0 && (D1.rows() != mOutputSize || D1.cols() != mInputSize))
45 throw std::invalid_argument("D1 has invalid dimensions.");
46
47 mA = A;
48 mB = B;
49 mC = C;
50 mD = D;
51 mD1 = D1.size() == 0 ? Matrix::Zero(mOutputSize, mInputSize) : D1;
52
53 **mX = MatrixComp::Zero(mA.rows(), 1);
54
55 mDiscreteA = MatrixComp::Zero(mA.rows(), mA.cols());
56 mDiscreteB = MatrixComp::Zero(mB.rows(), mB.cols());
57
58 mW = MatrixComp::Zero(mOutputSize, mInputSize);
59 mYHist = MatrixComp::Zero(mOutputSize, 1);
60 mStateUpdateInput.resize(mInputSize, 1);
61 mStateUpdate.resize(mA.rows(), 1);
62
63 mParametersSet = true;
64}
65
67 const Matrix::Index n = mA.rows();
68 const Matrix wI = omega * Matrix::Identity(n, n);
69
70 Matrix aAug = Matrix::Zero(2 * n, 2 * n);
71 aAug.topLeftCorner(n, n) = mA;
72 aAug.topRightCorner(n, n) = wI;
73 aAug.bottomLeftCorner(n, n) = -wI;
74 aAug.bottomRightCorner(n, n) = mA;
75 return aAug;
76}
77
79 const Matrix::Index n = mA.rows();
80
81 Matrix bAug = Matrix::Zero(2 * n, 2 * mInputSize);
82 bAug.topLeftCorner(n, mInputSize) = mB;
83 bAug.bottomRightCorner(n, mInputSize) = mB;
84 return bAug;
85}
86
88 const Matrix::Index n = mA.rows();
89
90 // Discretize the real-augmented model with the same helper as EMT-SSN.
91 const Matrix aAug = buildAugmentedA(omega);
92 const Matrix bAug = buildAugmentedB();
93 Matrix dAaug = Matrix::Zero(2 * n, 2 * n);
94 Matrix dBaug = Matrix::Zero(2 * n, 2 * mInputSize);
96 dBaug);
97
98 // Fold the [[P, -Q], [Q, P]] rotation blocks back into complex P + jQ.
99 mDiscreteA = dAaug.topLeftCorner(n, n).cast<Complex>() +
100 Complex(0., 1.) * dAaug.bottomLeftCorner(n, n).cast<Complex>();
101 mDiscreteB =
102 dBaug.block(0, 0, n, mInputSize).cast<Complex>() +
103 Complex(0., 1.) * dBaug.block(n, 0, n, mInputSize).cast<Complex>();
104
105 mW = mC.cast<Complex>() * mDiscreteB + mD.cast<Complex>();
106 mW += Complex(2. / mTimeStep, omega) * mD1.cast<Complex>();
107}
108
110 return mC.cast<Complex>() *
111 (mDiscreteA * (**mX) + mDiscreteB * (**inputAttribute()));
112}
113
115 mHistoryState.noalias() = mDiscreteA * (**mX);
116 mHistoryInput.noalias() = mDiscreteB * (**inputAttribute());
118 history.noalias() = mComplexC * mHistoryState;
119 history.noalias() -=
120 (2. / mTimeStep) * mD1.cast<Complex>() * (**inputAttribute());
121}
122
124 Real omega) const {
125 MatrixComp h =
126 Complex(0., omega) * MatrixComp::Identity(mA.rows(), mA.cols()) -
127 mA.cast<Complex>();
128
129 return h.inverse() * mB.cast<Complex>() * u;
130}
131
134 const MatrixComp &u) const {
135 return mC.cast<Complex>() * x + mD.cast<Complex>() * u;
136}
137
138void DP::SSNComp::updateState(const MatrixComp &uOld, const MatrixComp &uNew) {
139 mStateUpdateInput = uNew + uOld;
140 mStateUpdate.noalias() = mDiscreteA * (**mX);
142 **mX = mStateUpdate;
143}
144
146 // For linear components, the default implementation does nothing.
147}
148
150 Attribute<Matrix>::Ptr leftVector) {
151 if (!mParametersSet)
152 throw std::logic_error(
153 "setParameters() must be called before initialization.");
154
155 if (mNumFreqs != 1)
156 throw std::logic_error(
157 "DP SSN components currently support a single carrier frequency.");
158
159 mTimeStep = timeStep;
161
163 const UInt baseRowLimit = static_cast<UInt>(leftVector->get().rows() / 2);
166 for (UInt index : matrixNodeIndices(terminal))
167 if (index < baseRowLimit)
168 mRightVectorAffectedIndices.push_back(index);
169 }
170 }
172 ++virtualNode) {
174 if (index < baseRowLimit)
175 mRightVectorAffectedIndices.push_back(index);
176 }
177 std::sort(mRightVectorAffectedIndices.begin(),
180 std::unique(mRightVectorAffectedIndices.begin(),
183
185 mComplexC = mC.cast<Complex>();
186 mHistoryState.resize(mA.rows(), 1);
187 mHistoryInput.resize(mA.rows(), 1);
188 mYHist.resize(mOutputSize, 1);
190}
191
199
201 AttributeBase::List &prevStepDependencies,
202 AttributeBase::List &attributeDependencies,
203 AttributeBase::List &modifiedAttributes) {
204 modifiedAttributes.push_back(mRightVector);
205 prevStepDependencies.push_back(mX);
206 prevStepDependencies.push_back(inputAttribute());
207}
208
214
216 AttributeBase::List &prevStepDependencies,
217 AttributeBase::List &attributeDependencies,
218 AttributeBase::List &modifiedAttributes,
219 Attribute<Matrix>::Ptr &leftVector) {
220 attributeDependencies.push_back(leftVector);
221 modifiedAttributes.push_back(inputAttribute());
222 modifiedAttributes.push_back(outputAttribute());
223 modifiedAttributes.push_back(mX);
224}
std::vector< Ptr > List
Definition Attribute.h:123
AttributePointer< Attribute< T > > Ptr
Definition Attribute.h:249
MatrixComp mDiscreteB
Definition DP_SSNComp.h:38
void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override final
MatrixComp mStateUpdateInput
Reused post-step buffers for the trapezoidal state update.
Definition DP_SSNComp.h:48
virtual void updateStateSpaceModel()
Hook for variable/time-varying SSN components.
virtual MatrixComp calculateHistoryVector() const
Matrix mA
Continuous-time real physical model (user-provided).
Definition DP_SSNComp.h:29
const MatrixComp & getDiscreteA() const
Get the complex discrete state-transition operator of the envelope model.
MatrixComp mStateUpdate
Definition DP_SSNComp.h:49
UInt getStateCount() const
Get number of internal state variables of the SSN model.
virtual void updateState(const MatrixComp &uOld, const MatrixComp &uNew)
MatrixComp mYHist
Complex history term.
Definition DP_SSNComp.h:43
virtual Attribute< MatrixComp >::Ptr inputAttribute() const =0
Matrix buildAugmentedA(Real omega) const
Build A_aug = [[A, w I], [-w I, A]].
virtual MatrixComp calculateSteadyStateStateFromInput(const MatrixComp &u, Real omega) const
Steady-state envelope state X_ss = (j w I - A)^-1 B u.
MatrixComp mHistoryState
Reused workspaces for the per-step history calculation.
Definition DP_SSNComp.h:45
virtual void recomputeDiscreteModel(Real omega)
Discretize the augmented model and fold it into complex dA, dB, W.
void mnaCompAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override final
MatrixComp mW
Complex Norton matrix W = C dB + D + (2 / dt + j omega) D1.
Definition DP_SSNComp.h:41
void setParameters(const Matrix &A, const Matrix &B, const Matrix &C, const Matrix &D, const Matrix &D1=Matrix())
void mnaCompInitialize(Real omega, Real timeStep, Attribute< Matrix >::Ptr leftVector) override final
virtual void updateHistoryVector(MatrixComp &history)
const Attribute< MatrixComp >::Ptr mX
Complex envelope state X.
Definition DP_SSNComp.h:53
const Matrix & getC() const
Get the continuous-time output matrix of the SSN model.
MatrixComp mDiscreteA
Complex envelope discrete operators (folded from the augmented model).
Definition DP_SSNComp.h:37
std::vector< UInt > mRightVectorAffectedIndices
MNA rows that may receive this component's compact history stamp.
Definition DP_SSNComp.h:55
MatrixComp mHistoryInput
Definition DP_SSNComp.h:46
SSNComp(String uid, String name, Int inputSize, Int outputSize, Logger::Level logLevel=Logger::Level::off)
MatrixComp mComplexC
Definition DP_SSNComp.h:50
Matrix buildAugmentedB() const
Build B_aug = blkdiag(B, B).
const MatrixComp & getDiscreteB() const
Get the complex discrete input operator of the envelope model.
void mnaAddCompactRightSideVectorStamp(Matrix &rightVector) override final
virtual MatrixComp calculateSteadyStateOutputFromInput(const MatrixComp &x, const MatrixComp &u) const
virtual Attribute< MatrixComp >::Ptr outputAttribute() const =0
void mnaCompPreStep(Real time, Int timeStepCount) override final
Matrix mD1
Feedthrough from the envelope derivative of the input, for capacitive ports.
Definition DP_SSNComp.h:34
String uid()
Returns unique id.
AttributeList::Ptr mAttributes
Attribute List.
spdlog::level::level_enum Level
Definition Logger.h:33
MNASimPowerComp(String uid, String name, Bool hasPreStep, Bool hasPostStep, Logger::Level logLevel)
Attribute< Matrix >::Ptr mRightVector
virtual void mnaCompApplyRightSideVectorStamp(Matrix &rightVector)
static Complex complexFromVectorElement(const Matrix &mat, Matrix::Index row, Int maxFreq=1, Int freqIdx=0)
static void calculateStateSpaceTrapezoidalMatrices(const Matrix &A, const Matrix &B, const Matrix &C, const Real &dt, Matrix &Ad, Matrix &Bd, Matrix &Cd)
Calculate the discretized state space matrices Ad, Bd, Cd using trapezoidal rule.
static void addToVectorElement(Matrix &mat, Matrix::Index row, Complex value, Int maxFreq=1, Int freqIdx=0)
SimTerminal< Complex >::Ptr terminal(UInt index)
std::vector< UInt > virtualMatrixNodeIndices(UInt index)
SimNode< Complex >::Ptr virtualNode(UInt index)
Bool terminalNotGrounded(UInt index)
std::vector< UInt > matrixNodeIndices(UInt index)
bool mParametersSet
Flag indicating that parameters are set via setParameters() function.
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
unsigned int UInt
Definition Definitions.h:58