4#include <dpsim-models/EMT/EMT_SSNComp.h>
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;
18 return static_cast<UInt
>(mA.rows());
27void EMT::SSNComp::setParameters(
const Matrix &A,
const Matrix &B,
28 const Matrix &C,
const Matrix &D) {
29 mParametersSet =
false;
31 if (A.rows() != A.cols())
32 throw std::invalid_argument(
"A must be square.");
34 if (B.rows() != A.rows() || B.cols() != mInputSize)
35 throw std::invalid_argument(
"B has invalid dimensions.");
37 if (C.rows() != mOutputSize || C.cols() != A.rows())
38 throw std::invalid_argument(
"C has invalid dimensions.");
40 if (D.rows() != mOutputSize || D.cols() != mInputSize)
41 throw std::invalid_argument(
"D has invalid dimensions.");
48 **mX = Matrix::Zero(mA.rows(), 1);
50 mdA = Matrix::Zero(mA.rows(), mA.cols());
51 mdB = Matrix::Zero(mB.rows(), mB.cols());
53 mW = Matrix::Zero(mOutputSize, mInputSize);
54 mYHist = Matrix::Zero(mOutputSize, 1);
56 mParametersSet =
true;
59Matrix EMT::SSNComp::calculateHistoryVector()
const {
60 return mC * (mdA * (**mX) + mdB * (**inputAttribute()));
64EMT::SSNComp::calculateSteadyStateStateFromInput(
const MatrixComp &u,
65 Real frequency)
const {
66 const Real omega = 2.0 * PI * frequency;
68 Complex(0.0, omega) * MatrixComp::Identity(mA.rows(), mA.cols()) -
71 return h.inverse() * mB.cast<Complex>() * u;
75EMT::SSNComp::calculateSteadyStateOutputFromInput(
const MatrixComp &x,
76 const MatrixComp &u)
const {
77 return mC.cast<Complex>() * x + mD.cast<Complex>() * u;
80void EMT::SSNComp::updateState(
const Matrix &uOld,
const Matrix &uNew) {
81 **mX = mdA * (**mX) + mdB * (uNew + uOld);
88void EMT::SSNComp::recomputeDiscreteModel() {
97void EMT::SSNComp::mnaCompInitialize(Real, Real timeStep,
98 Attribute<Matrix>::Ptr) {
100 throw std::logic_error(
101 "setParameters() must be called before initialization.");
103 mTimeStep = timeStep;
104 updateMatrixNodeIndices();
106 recomputeDiscreteModel();
107 mYHist = calculateHistoryVector();
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());
119void EMT::SSNComp::mnaCompPreStep(Real time, Int timeStepCount) {
120 updateStateSpaceModel();
121 mYHist = calculateHistoryVector();
122 mnaCompApplyRightSideVectorStamp(**mRightVector);
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);
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.