DPsim
Loading...
Searching...
No Matches
EMT_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
5
6#include <algorithm>
7
8using namespace CPS;
9
11 Logger::Level logLevel)
12 : MNASimPowerComp<Real>(uid, name, true, true, logLevel), mTimeStep(0.0),
13 mW(Matrix::Zero(outputSize, inputSize)),
14 mYHist(Matrix::Zero(outputSize, 1)), mInputSize(inputSize),
15 mOutputSize(outputSize), mX(mAttributes->create<Matrix>("x")),
16 mPreviousInput(Matrix::Zero(inputSize, 1)) {
17 mParametersSet = false;
18}
19
21 return static_cast<UInt>(mA.rows());
22}
23
24std::vector<String> EMT::SSNComp::getLocalStateNames() const { return {}; }
25
26std::vector<EMT::SSNComp::LocalAbcStateBlock>
28 // Default: no abc-frame state metadata. Derived components should override
29 // this only for states known to form physical abc triples.
30 return {};
31}
32
33const Matrix &EMT::SSNComp::getDiscreteA() const { return mdA; }
34
35const Matrix &EMT::SSNComp::getDiscreteB() const { return mdB; }
36
37const Matrix &EMT::SSNComp::getC() const { return mC; }
38
40 const Matrix &C, const Matrix &D,
41 const Matrix &D1) {
42 mParametersSet = false;
43
44 if (A.rows() != A.cols())
45 throw std::invalid_argument("A must be square.");
46
47 if (B.rows() != A.rows() || B.cols() != mInputSize)
48 throw std::invalid_argument("B has invalid dimensions.");
49
50 if (C.rows() != mOutputSize || C.cols() != A.rows())
51 throw std::invalid_argument("C has invalid dimensions.");
52
53 if (D.rows() != mOutputSize || D.cols() != mInputSize)
54 throw std::invalid_argument("D has invalid dimensions.");
55 if (D1.size() != 0 && (D1.rows() != mOutputSize || D1.cols() != mInputSize))
56 throw std::invalid_argument("D1 has invalid dimensions.");
57
58 mA = A;
59 mB = B;
60 mC = C;
61 mD = D;
62 mD1 = D1.size() == 0 ? Matrix::Zero(mOutputSize, mInputSize) : D1;
63
64 **mX = Matrix::Zero(mA.rows(), 1);
65
66 mdA = Matrix::Zero(mA.rows(), mA.cols());
67 mdB = Matrix::Zero(mB.rows(), mB.cols());
68
69 mW = Matrix::Zero(mOutputSize, mInputSize);
70 mYHist = Matrix::Zero(mOutputSize, 1);
71
72 mParametersSet = true;
73}
74
76 // Algebraic SSN components have no dynamic state. Their discrete history is
77 // identically zero; feedthrough remains represented by mW = D and is
78 // handled by the component's MNA stamp/update path.
79 if (mA.rows() == 0) {
80 history.noalias() = -(2. / mTimeStep) * mD1 * inputValue();
81 return;
82 }
83
84 mHistoryState.noalias() = mdA * stateValue();
85 mHistoryInput.noalias() = mdB * inputValue();
87 history.noalias() = mC * mHistoryState;
88 history.noalias() -= (2. / mTimeStep) * mD1 * inputValue();
89}
90
93 Real frequency) const {
94 const Real omega = 2.0 * PI * frequency;
95 MatrixComp h =
96 Complex(0.0, omega) * MatrixComp::Identity(mA.rows(), mA.cols()) -
97 mA.cast<Complex>();
98
99 return h.inverse() * mB.cast<Complex>() * u;
100}
101
104 const MatrixComp &u) const {
105 return mC.cast<Complex>() * x + mD.cast<Complex>() * u;
106}
107
108void EMT::SSNComp::updateState(const Matrix &uOld, const Matrix &uNew) {
109 if (mA.rows() == 0)
110 return;
111
112 **mX = mdA * (**mX) + mdB * (uNew + uOld);
113}
114
116 // the default implementation does nothing.
117}
118
123
125 // For linear components, the default implementation does nothing.
126}
127
129 Attribute<Matrix>::Ptr leftVector) {
130 if (!mParametersSet)
131 throw std::logic_error(
132 "setParameters() must be called before initialization.");
133
134 mTimeStep = timeStep;
135 mXValue = &mX->get();
136 mInputValue = &inputAttribute()->get();
138
142 for (UInt index : matrixNodeIndices(terminal))
143 if (index < static_cast<UInt>(leftVector->get().rows()))
144 mRightVectorAffectedIndices.push_back(index);
145 }
146 }
148 ++virtualNode) {
150 if (index < static_cast<UInt>(leftVector->get().rows()))
151 mRightVectorAffectedIndices.push_back(index);
152 }
153 std::sort(mRightVectorAffectedIndices.begin(),
156 std::unique(mRightVectorAffectedIndices.begin(),
159
161 mHistoryState.resize(mA.rows(), 1);
162 mHistoryInput.resize(mA.rows(), 1);
164}
165
168 rightVector(index, 0) += (*mRightVectorValue)(index, 0);
169}
170
172 AttributeBase::List &prevStepDependencies,
173 AttributeBase::List &attributeDependencies,
174 AttributeBase::List &modifiedAttributes) {
175 modifiedAttributes.push_back(mRightVector);
176 prevStepDependencies.push_back(mX);
177 prevStepDependencies.push_back(inputAttribute());
178}
179
185
187 AttributeBase::List &prevStepDependencies,
188 AttributeBase::List &attributeDependencies,
189 AttributeBase::List &modifiedAttributes,
190 Attribute<Matrix>::Ptr &leftVector) {
191 attributeDependencies.push_back(leftVector);
192 modifiedAttributes.push_back(inputAttribute());
193 modifiedAttributes.push_back(outputAttribute());
194 modifiedAttributes.push_back(mX);
195}
std::vector< Ptr > List
Definition Attribute.h:123
AttributePointer< Attribute< T > > Ptr
Definition Attribute.h:249
Matrix mD1
Feedthrough from the time derivative of the input, for capacitive ports.
Definition EMT_SSNComp.h:43
virtual std::vector< String > getLocalStateNames() const
virtual void updateLogAttributes(const Matrix &u) const
Update derived attributes used for logging/inspection.
Matrix mHistoryState
Reused workspaces for the per-step linear history evaluation.
Definition EMT_SSNComp.h:35
const Matrix & getDiscreteA() const
Get discrete state transition matrix used by the trapezoidal SSN model.
const Matrix & getDiscreteB() const
Get discrete input matrix used by the trapezoidal SSN model.
void mnaCompPreStep(Real time, Int timeStepCount) override final
const Matrix * mXValue
Definition EMT_SSNComp.h:49
const Attribute< Matrix >::Ptr mX
Definition EMT_SSNComp.h:48
virtual void recomputeDiscreteModel()
virtual Attribute< Matrix >::Ptr inputAttribute() const =0
const Matrix & getC() const
Get continuous-time output matrix of the SSN model.
const Matrix & stateValue() const
Definition EMT_SSNComp.h:55
void mnaCompAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override final
const Matrix & inputValue() const
Definition EMT_SSNComp.h:56
void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override final
virtual void updateState(const Matrix &uOld, const Matrix &uNew)
const Matrix * mInputValue
Definition EMT_SSNComp.h:50
virtual void updateStateSpaceModel()
Hook for variable/time-varying SSN components.
SSNComp(String uid, String name, Int inputSize, Int outputSize, Logger::Level logLevel=Logger::Level::off)
virtual MatrixComp calculateSteadyStateStateFromInput(const MatrixComp &u, Real frequency) const
virtual Attribute< Matrix >::Ptr outputAttribute() const =0
Matrix mPreviousInput
Reused storage for the previous SSN input during post-step updates.
Definition EMT_SSNComp.h:52
virtual std::vector< LocalAbcStateBlock > getLocalAbcStateBlocks() const
void mnaAddCompactRightSideVectorStamp(Matrix &rightVector) override final
UInt getStateCount() const
Get number of internal state variables of the SSN model.
std::vector< UInt > mRightVectorAffectedIndices
Definition EMT_SSNComp.h:53
virtual void calculateHistoryVector(Matrix &history) const
void setParameters(const Matrix &A, const Matrix &B, const Matrix &C, const Matrix &D, const Matrix &D1=Matrix())
virtual MatrixComp calculateSteadyStateOutputFromInput(const MatrixComp &x, const MatrixComp &u) const
void mnaCompInitialize(Real omega, Real timeStep, Attribute< Matrix >::Ptr leftVector) override final
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 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.
SimTerminal< Real >::Ptr terminal(UInt index)
std::vector< UInt > virtualMatrixNodeIndices(UInt index)
SimNode< Real >::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.
#define PI
Definition Definitions.h:43
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