DPsim
Loading...
Searching...
No Matches
EMT_SSNComp.cpp
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 <dpsim-models/EMT/EMT_SSNComp.h>
5
6using namespace CPS;
7
8EMT::SSNComp::SSNComp(String uid, String name, Int inputSize, Int outputSize,
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
36void EMT::SSNComp::setParameters(const Matrix &A, const Matrix &B,
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
68Matrix EMT::SSNComp::calculateHistoryVector() const {
69 return mC * (mdA * (**mX) + mdB * (**inputAttribute()));
70}
71
72MatrixComp
73EMT::SSNComp::calculateSteadyStateStateFromInput(const MatrixComp &u,
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
83MatrixComp
84EMT::SSNComp::calculateSteadyStateOutputFromInput(const MatrixComp &x,
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
93void EMT::SSNComp::updateLogAttributes(const Matrix &) const {
94 // the default implementation does nothing.
95}
96
97void EMT::SSNComp::recomputeDiscreteModel() {
98 Math::calculateStateSpaceTrapezoidalMatrices(mA, mB, mTimeStep, mdA, mdB);
99 mW = mC * mdB + mD;
100}
101
103 // For linear components, the default implementation does nothing.
104}
105
106void EMT::SSNComp::mnaCompInitialize(Real, Real timeStep,
107 Attribute<Matrix>::Ptr) {
108 if (!mParametersSet)
109 throw std::logic_error(
110 "setParameters() must be called before initialization.");
111
112 mTimeStep = timeStep;
113 updateMatrixNodeIndices();
114
115 recomputeDiscreteModel();
116 mYHist = calculateHistoryVector();
117}
118
119void EMT::SSNComp::mnaCompAddPreStepDependencies(
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
128void EMT::SSNComp::mnaCompPreStep(Real time, Int timeStepCount) {
129 updateStateSpaceModel();
130 mYHist = calculateHistoryVector();
131 mnaCompApplyRightSideVectorStamp(**mRightVector);
132}
133
134void EMT::SSNComp::mnaCompAddPostStepDependencies(
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}
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.
const Matrix & getC() const
Get continuous-time output matrix of the SSN model.
virtual void updateStateSpaceModel()
Hook for variable/time-varying SSN components.
virtual std::vector< LocalAbcStateBlock > getLocalAbcStateBlocks() const
UInt getStateCount() const
Get number of internal state variables of the SSN model.
Base class for all MNA components that are transmitting power.
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.