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());
23std::vector<EMT::SSNComp::LocalAbcStateBlock>
36void EMT::SSNComp::setParameters(
const Matrix &A,
const Matrix &B,
37 const Matrix &C,
const Matrix &D) {
38 mParametersSet =
false;
40 if (A.rows() != A.cols())
41 throw std::invalid_argument(
"A must be square.");
43 if (B.rows() != A.rows() || B.cols() != mInputSize)
44 throw std::invalid_argument(
"B has invalid dimensions.");
46 if (C.rows() != mOutputSize || C.cols() != A.rows())
47 throw std::invalid_argument(
"C has invalid dimensions.");
49 if (D.rows() != mOutputSize || D.cols() != mInputSize)
50 throw std::invalid_argument(
"D has invalid dimensions.");
57 **mX = Matrix::Zero(mA.rows(), 1);
59 mdA = Matrix::Zero(mA.rows(), mA.cols());
60 mdB = Matrix::Zero(mB.rows(), mB.cols());
62 mW = Matrix::Zero(mOutputSize, mInputSize);
63 mYHist = Matrix::Zero(mOutputSize, 1);
65 mParametersSet =
true;
68Matrix EMT::SSNComp::calculateHistoryVector()
const {
69 return mC * (mdA * (**mX) + mdB * (**inputAttribute()));
73EMT::SSNComp::calculateSteadyStateStateFromInput(
const MatrixComp &u,
74 Real frequency)
const {
75 const Real omega = 2.0 * PI * frequency;
77 Complex(0.0, omega) * MatrixComp::Identity(mA.rows(), mA.cols()) -
80 return h.inverse() * mB.cast<Complex>() * u;
84EMT::SSNComp::calculateSteadyStateOutputFromInput(
const MatrixComp &x,
85 const MatrixComp &u)
const {
86 return mC.cast<Complex>() * x + mD.cast<Complex>() * u;
89void EMT::SSNComp::updateState(
const Matrix &uOld,
const Matrix &uNew) {
90 **mX = mdA * (**mX) + mdB * (uNew + uOld);
97void EMT::SSNComp::recomputeDiscreteModel() {
106void EMT::SSNComp::mnaCompInitialize(Real, Real timeStep,
107 Attribute<Matrix>::Ptr) {
109 throw std::logic_error(
110 "setParameters() must be called before initialization.");
112 mTimeStep = timeStep;
113 updateMatrixNodeIndices();
115 recomputeDiscreteModel();
116 mYHist = calculateHistoryVector();
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());
128void EMT::SSNComp::mnaCompPreStep(Real time, Int timeStepCount) {
129 updateStateSpaceModel();
130 mYHist = calculateHistoryVector();
131 mnaCompApplyRightSideVectorStamp(**mRightVector);
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);
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.