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