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
21const Matrix &EMT::SSNComp::getDiscreteA() const { return mdA; }
22
23const Matrix &EMT::SSNComp::getDiscreteB() const { return mdB; }
24
25const Matrix &EMT::SSNComp::getC() const { return mC; }
26
27void EMT::SSNComp::setParameters(const Matrix &A, const Matrix &B,
28 const Matrix &C, const Matrix &D) {
29 mParametersSet = false;
30
31 if (A.rows() != A.cols())
32 throw std::invalid_argument("A must be square.");
33
34 if (B.rows() != A.rows() || B.cols() != mInputSize)
35 throw std::invalid_argument("B has invalid dimensions.");
36
37 if (C.rows() != mOutputSize || C.cols() != A.rows())
38 throw std::invalid_argument("C has invalid dimensions.");
39
40 if (D.rows() != mOutputSize || D.cols() != mInputSize)
41 throw std::invalid_argument("D has invalid dimensions.");
42
43 mA = A;
44 mB = B;
45 mC = C;
46 mD = D;
47
48 **mX = Matrix::Zero(mA.rows(), 1);
49
50 mdA = Matrix::Zero(mA.rows(), mA.cols());
51 mdB = Matrix::Zero(mB.rows(), mB.cols());
52
53 mW = Matrix::Zero(mOutputSize, mInputSize);
54 mYHist = Matrix::Zero(mOutputSize, 1);
55
56 mParametersSet = true;
57}
58
59Matrix EMT::SSNComp::calculateHistoryVector() const {
60 return mC * (mdA * (**mX) + mdB * (**inputAttribute()));
61}
62
63MatrixComp
64EMT::SSNComp::calculateSteadyStateStateFromInput(const MatrixComp &u,
65 Real frequency) const {
66 const Real omega = 2.0 * PI * frequency;
67 MatrixComp h =
68 Complex(0.0, omega) * MatrixComp::Identity(mA.rows(), mA.cols()) -
69 mA.cast<Complex>();
70
71 return h.inverse() * mB.cast<Complex>() * u;
72}
73
74MatrixComp
75EMT::SSNComp::calculateSteadyStateOutputFromInput(const MatrixComp &x,
76 const MatrixComp &u) const {
77 return mC.cast<Complex>() * x + mD.cast<Complex>() * u;
78}
79
80void EMT::SSNComp::updateState(const Matrix &uOld, const Matrix &uNew) {
81 **mX = mdA * (**mX) + mdB * (uNew + uOld);
82}
83
84void EMT::SSNComp::updateLogAttributes(const Matrix &) const {
85 // the default implementation does nothing.
86}
87
88void EMT::SSNComp::recomputeDiscreteModel() {
89 Math::calculateStateSpaceTrapezoidalMatrices(mA, mB, mTimeStep, mdA, mdB);
90 mW = mC * mdB + mD;
91}
92
94 // For linear components, the default implementation does nothing.
95}
96
97void EMT::SSNComp::mnaCompInitialize(Real, Real timeStep,
98 Attribute<Matrix>::Ptr) {
99 if (!mParametersSet)
100 throw std::logic_error(
101 "setParameters() must be called before initialization.");
102
103 mTimeStep = timeStep;
104 updateMatrixNodeIndices();
105
106 recomputeDiscreteModel();
107 mYHist = calculateHistoryVector();
108}
109
110void EMT::SSNComp::mnaCompAddPreStepDependencies(
111 AttributeBase::List &prevStepDependencies,
112 AttributeBase::List &attributeDependencies,
113 AttributeBase::List &modifiedAttributes) {
114 modifiedAttributes.push_back(mRightVector);
115 prevStepDependencies.push_back(mX);
116 prevStepDependencies.push_back(inputAttribute());
117}
118
119void EMT::SSNComp::mnaCompPreStep(Real time, Int timeStepCount) {
120 updateStateSpaceModel();
121 mYHist = calculateHistoryVector();
122 mnaCompApplyRightSideVectorStamp(**mRightVector);
123}
124
125void EMT::SSNComp::mnaCompAddPostStepDependencies(
126 AttributeBase::List &prevStepDependencies,
127 AttributeBase::List &attributeDependencies,
128 AttributeBase::List &modifiedAttributes,
129 Attribute<Matrix>::Ptr &leftVector) {
130 attributeDependencies.push_back(leftVector);
131 modifiedAttributes.push_back(inputAttribute());
132 modifiedAttributes.push_back(outputAttribute());
133 modifiedAttributes.push_back(mX);
134}
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.
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.