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
6using namespace CPS;
7
9 Logger::Level logLevel)
10 : MNASimPowerComp<Real>(uid, name, true, true, logLevel), mTimeStep(0.0),
11 mW(Matrix::Zero(outputSize, inputSize)),
12 mYHist(Matrix::Zero(outputSize, 1)), mInputSize(inputSize),
13 mOutputSize(outputSize), mX(mAttributes->create<Matrix>("x")) {
14 mParametersSet = false;
15}
16
18 return static_cast<UInt>(mA.rows());
19}
20
21std::vector<String> EMT::SSNComp::getLocalStateNames() const { return {}; }
22
23std::vector<EMT::SSNComp::LocalAbcStateBlock>
25 // Default: no abc-frame state metadata. Derived components should override
26 // this only for states known to form physical abc triples.
27 return {};
28}
29
30const Matrix &EMT::SSNComp::getDiscreteA() const { return mdA; }
31
32const Matrix &EMT::SSNComp::getDiscreteB() const { return mdB; }
33
34const Matrix &EMT::SSNComp::getC() const { return mC; }
35
37 const Matrix &C, const Matrix &D) {
38 mParametersSet = false;
39
40 if (A.rows() != A.cols())
41 throw std::invalid_argument("A must be square.");
42
43 if (B.rows() != A.rows() || B.cols() != mInputSize)
44 throw std::invalid_argument("B has invalid dimensions.");
45
46 if (C.rows() != mOutputSize || C.cols() != A.rows())
47 throw std::invalid_argument("C has invalid dimensions.");
48
49 if (D.rows() != mOutputSize || D.cols() != mInputSize)
50 throw std::invalid_argument("D has invalid dimensions.");
51
52 mA = A;
53 mB = B;
54 mC = C;
55 mD = D;
56
57 **mX = Matrix::Zero(mA.rows(), 1);
58
59 mdA = Matrix::Zero(mA.rows(), mA.cols());
60 mdB = Matrix::Zero(mB.rows(), mB.cols());
61
62 mW = Matrix::Zero(mOutputSize, mInputSize);
63 mYHist = Matrix::Zero(mOutputSize, 1);
64
65 mParametersSet = true;
66}
67
69 return mC * (mdA * (**mX) + mdB * (**inputAttribute()));
70}
71
74 Real frequency) const {
75 const Real omega = 2.0 * PI * frequency;
76 MatrixComp h =
77 Complex(0.0, omega) * MatrixComp::Identity(mA.rows(), mA.cols()) -
78 mA.cast<Complex>();
79
80 return h.inverse() * mB.cast<Complex>() * u;
81}
82
85 const MatrixComp &u) const {
86 return mC.cast<Complex>() * x + mD.cast<Complex>() * u;
87}
88
89void EMT::SSNComp::updateState(const Matrix &uOld, const Matrix &uNew) {
90 **mX = mdA * (**mX) + mdB * (uNew + uOld);
91}
92
94 // the default implementation does nothing.
95}
96
101
103 // For linear components, the default implementation does nothing.
104}
105
108 if (!mParametersSet)
109 throw std::logic_error(
110 "setParameters() must be called before initialization.");
111
112 mTimeStep = timeStep;
114
117}
118
120 AttributeBase::List &prevStepDependencies,
121 AttributeBase::List &attributeDependencies,
122 AttributeBase::List &modifiedAttributes) {
123 modifiedAttributes.push_back(mRightVector);
124 prevStepDependencies.push_back(mX);
125 prevStepDependencies.push_back(inputAttribute());
126}
127
133
135 AttributeBase::List &prevStepDependencies,
136 AttributeBase::List &attributeDependencies,
137 AttributeBase::List &modifiedAttributes,
138 Attribute<Matrix>::Ptr &leftVector) {
139 attributeDependencies.push_back(leftVector);
140 modifiedAttributes.push_back(inputAttribute());
141 modifiedAttributes.push_back(outputAttribute());
142 modifiedAttributes.push_back(mX);
143}
std::vector< Ptr > List
Definition Attribute.h:123
AttributePointer< Attribute< T > > Ptr
Definition Attribute.h:250
void setParameters(const Matrix &A, const Matrix &B, const Matrix &C, const Matrix &D)
virtual std::vector< String > getLocalStateNames() const
virtual void updateLogAttributes(const Matrix &u) const
Update derived attributes used for logging/inspection.
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 Attribute< Matrix >::Ptr mX
Definition EMT_SSNComp.h:43
virtual void recomputeDiscreteModel()
virtual Attribute< Matrix >::Ptr inputAttribute() const =0
const Matrix & getC() const
Get continuous-time output matrix of the SSN model.
void mnaCompAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override final
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)
virtual Matrix calculateHistoryVector() const
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
virtual std::vector< LocalAbcStateBlock > getLocalAbcStateBlocks() const
UInt getStateCount() const
Get number of internal state variables of the SSN model.
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.
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:81
std::string String
Definition Definitions.h:65
double Real
Definition Definitions.h:62
int Int
Definition Definitions.h:61
std::complex< Real > Complex
Definition Definitions.h:63
Eigen::Matrix< Complex, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > MatrixComp
Dense matrix for complex numbers.
Definition Definitions.h:84
unsigned int UInt
Definition Definitions.h:60