DPsim
Loading...
Searching...
No Matches
MNASimPowerComp.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2
3#pragma once
4
8
9namespace CPS {
10
12template <typename VarType>
13class MNASimPowerComp : public SimPowerComp<VarType>, public MNAInterface {
14
15private:
16 Bool mHasPreStep;
17 Bool mHasPostStep;
18
19protected:
21 void appendTerminalRightVectorStampIndices(std::vector<UInt> &indices,
22 UInt phasesPerTerminal) {
23 indices.clear();
24 for (UInt terminalIdx = 0; terminalIdx < this->terminalNumber();
25 ++terminalIdx) {
26 if (!this->terminalNotGrounded(terminalIdx))
27 continue;
28 for (UInt phase = 0; phase < phasesPerTerminal; ++phase)
29 indices.push_back(this->matrixNodeIndex(terminalIdx, phase));
30 }
31 }
32
33public:
34 using Type = VarType;
35 using Ptr = std::shared_ptr<MNASimPowerComp<VarType>>;
36 using List = std::vector<Ptr>;
37
41
42protected:
43 // Cached payload used by per-step component code. The Attribute remains
44 // available for dependency tracking and external inspection.
46
47public:
50
52 MNASimPowerComp(String uid, String name, Bool hasPreStep, Bool hasPostStep,
53 Logger::Level logLevel)
54 : SimPowerComp<VarType>(uid, name, logLevel), mHasPreStep(hasPreStep),
55 mHasPostStep(hasPostStep),
57 "right_vector")),
59
61 explicit MNASimPowerComp(String name, Bool hasPreStep = true,
62 Bool hasPostStep = true,
63 Logger::Level logLevel = Logger::Level::off)
64 : MNASimPowerComp<VarType>(name, name, hasPreStep, hasPostStep,
65 logLevel){};
66
68 virtual ~MNASimPowerComp() = default;
69
70 // Implementation of MNAInterface methods
71 void mnaInitialize(Real omega, Real timeStep) final;
72 void mnaInitialize(Real omega, Real timeStep,
73 Attribute<Matrix>::Ptr leftVector) final;
74 void mnaApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) final;
75 void mnaApplyRightSideVectorStamp(Matrix &rightVector) final;
76 void mnaUpdateVoltage(const Matrix &leftVector) final;
77 void mnaUpdateCurrent(const Matrix &leftVector) final;
78 void mnaPreStep(Real time, Int timeStepCount) final;
79 void mnaPostStep(Real time, Int timeStepCount,
80 Attribute<Matrix>::Ptr &leftVector) final;
81 void mnaAddPreStepDependencies(AttributeBase::List &prevStepDependencies,
82 AttributeBase::List &attributeDependencies,
83 AttributeBase::List &modifiedAttributes) final;
84 void mnaAddPostStepDependencies(AttributeBase::List &prevStepDependencies,
85 AttributeBase::List &attributeDependencies,
86 AttributeBase::List &modifiedAttributes,
87 Attribute<Matrix>::Ptr &leftVector) final;
88 void mnaInitializeHarm(Real omega, Real timeStep,
89 std::vector<Attribute<Matrix>::Ptr> leftVector) final;
91 Int freqIdx) final;
92 void mnaApplyRightSideVectorStampHarm(Matrix &sourceVector) final;
94 Int freqIdx) final;
95
96 // MNA Interface methods that can be overridden by components
97 virtual void mnaCompInitialize(Real omega, Real timeStep,
98 Attribute<Matrix>::Ptr leftVector);
99 virtual void mnaCompApplySystemMatrixStamp(SparseMatrixRow &systemMatrix);
100 virtual void mnaCompApplyRightSideVectorStamp(Matrix &rightVector);
101 virtual void mnaCompUpdateVoltage(const Matrix &leftVector);
102 virtual void mnaCompUpdateCurrent(const Matrix &leftVector);
103 virtual void mnaCompPreStep(Real time, Int timeStepCount);
104 virtual void mnaCompPostStep(Real time, Int timeStepCount,
105 Attribute<Matrix>::Ptr &leftVector);
106 virtual void
108 AttributeBase::List &attributeDependencies,
109 AttributeBase::List &modifiedAttributes);
110 virtual void
112 AttributeBase::List &attributeDependencies,
113 AttributeBase::List &modifiedAttributes,
114 Attribute<Matrix>::Ptr &leftVector);
115 virtual void
116 mnaCompInitializeHarm(Real omega, Real timeStep,
117 std::vector<Attribute<Matrix>::Ptr> leftVector);
118 virtual void mnaCompApplySystemMatrixStampHarm(SparseMatrixRow &systemMatrix,
119 Int freqIdx);
120 virtual void mnaCompApplyRightSideVectorStampHarm(Matrix &sourceVector);
121 virtual void mnaCompApplyRightSideVectorStampHarm(Matrix &sourceVector,
122 Int freqIdx);
123
124 const Task::List &mnaTasks() const final;
125 Attribute<Matrix>::Ptr getRightVector() const final;
126
127 class MnaPreStep : public CPS::Task {
128 public:
130 : Task(**comp.mName + ".MnaPreStep"), mComp(comp) {
132 mComp.mnaAddPreStepDependencies(
134 }
135 void execute(Real time, Int timeStepCount) override {
136 mComp.mnaPreStep(time, timeStepCount);
137 };
138 void executeFastImpl(Real time, Int timeStepCount) {
139 mComp.mnaPreStep(time, timeStepCount);
140 }
141
142 private:
144 };
145
146 class MnaPostStep : public CPS::Task {
147 public:
149 Attribute<Matrix>::Ptr leftVector)
150 : Task(**comp.mName + ".MnaPostStep"), mComp(comp),
151 mLeftVector(leftVector) {
153 mComp.mnaAddPostStepDependencies(mPrevStepDependencies,
155 mModifiedAttributes, mLeftVector);
156 }
157 void execute(Real time, Int timeStepCount) override {
158 mComp.mnaPostStep(time, timeStepCount, mLeftVector);
159 };
160 void executeFastImpl(Real time, Int timeStepCount) {
161 mComp.mnaPostStep(time, timeStepCount, mLeftVector);
162 }
163
164 private:
166 Attribute<Matrix>::Ptr mLeftVector;
167 };
168};
169} // namespace CPS
std::vector< Ptr > List
Definition Attribute.h:123
AttributePointer< Attribute< T > > Ptr
Definition Attribute.h:249
String uid()
Returns unique id.
AttributeList::Ptr mAttributes
Attribute List.
spdlog::level::level_enum Level
Definition Logger.h:33
Interface to be implemented by all models used by the MNA solver.
void executeFastImpl(Real time, Int timeStepCount)
MnaPostStep(MNASimPowerComp< VarType > &comp, Attribute< Matrix >::Ptr leftVector)
void execute(Real time, Int timeStepCount) override
void execute(Real time, Int timeStepCount) override
void executeFastImpl(Real time, Int timeStepCount)
MnaPreStep(MNASimPowerComp< VarType > &comp)
virtual void mnaCompPreStep(Real time, Int timeStepCount)
void mnaAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) final
Add MNA pre step dependencies.
virtual void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector)
virtual void mnaCompInitializeHarm(Real omega, Real timeStep, std::vector< Attribute< Matrix >::Ptr > leftVector)
Attribute< Matrix >::Ptr getRightVector() const final
virtual ~MNASimPowerComp()=default
Destructor - does not do anything.
Task::List mMnaTasks
List of tasks that relate to using MNA for this component (usually pre-step and/or post-step)
void mnaApplyRightSideVectorStampHarm(Matrix &sourceVector) final
Stamps right side (source) vector considering the frequency index.
std::vector< Ptr > List
virtual void mnaCompUpdateVoltage(const Matrix &leftVector)
void mnaApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) final
Stamps system matrix.
virtual void mnaCompAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes)
virtual void mnaCompInitialize(Real omega, Real timeStep, Attribute< Matrix >::Ptr leftVector)
void mnaAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) final
virtual void mnaCompApplySystemMatrixStamp(SparseMatrixRow &systemMatrix)
void mnaUpdateCurrent(const Matrix &leftVector) final
Update interface current from MNA system result.
void mnaUpdateVoltage(const Matrix &leftVector) final
Update interface voltage from MNA system result.
virtual void mnaCompUpdateCurrent(const Matrix &leftVector)
const Task::List & mnaTasks() const final
Return list of MNA tasks.
void appendTerminalRightVectorStampIndices(std::vector< UInt > &indices, UInt phasesPerTerminal)
Cache all non-ground terminal rows used by nodal RHS stamps.
void mnaInitialize(Real omega, Real timeStep) final
Initializes variables of components.
virtual void mnaCompApplyRightSideVectorStampHarm(Matrix &sourceVector)
std::shared_ptr< MNASimPowerComp< VarType > > Ptr
MNASimPowerComp(String uid, String name, Bool hasPreStep, Bool hasPostStep, Logger::Level logLevel)
Basic constructor that takes UID, name and log level.
Attribute< Matrix >::Ptr mRightVector
MNASimPowerComp(String name, Bool hasPreStep=true, Bool hasPostStep=true, Logger::Level logLevel=Logger::Level::off)
Basic constructor that takes name and log level and sets the UID to name as well.
void mnaInitializeHarm(Real omega, Real timeStep, std::vector< Attribute< Matrix >::Ptr > leftVector) final
void mnaPreStep(Real time, Int timeStepCount) final
MNA pre step operations.
void mnaApplySystemMatrixStampHarm(SparseMatrixRow &systemMatrix, Int freqIdx) final
Stamps system matrix considering the frequency index.
virtual void mnaCompApplyRightSideVectorStamp(Matrix &rightVector)
void mnaApplyRightSideVectorStamp(Matrix &rightVector) final
Stamps right side (source) vector.
virtual void mnaCompPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector)
void mnaPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) final
virtual void mnaCompApplySystemMatrixStampHarm(SparseMatrixRow &systemMatrix, Int freqIdx)
UInt terminalNumber()
Returns nominal number of Terminals for this component type.
SimPowerComp(String uid, String name, Logger::Level logLevel=Logger::Level::off)
Basic constructor that takes UID, name and log level.
Bool terminalNotGrounded(UInt index)
Tasks to be defined by every component.
Definition Task.h:25
std::vector< AttributeBase::Ptr > mModifiedAttributes
Definition Task.h:70
std::vector< AttributeBase::Ptr > mPrevStepDependencies
Definition Task.h:71
std::vector< AttributeBase::Ptr > mAttributeDependencies
Definition Task.h:69
Task()
Definition Task.h:30
std::vector< Ptr > List
Definition Task.h:28
std::string mName
Definition Task.h:68
void setFastExecutor(TaskType *task)
Definition Task.h:45
Eigen::Matrix< Real, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > Matrix
Dense matrix for real numbers.
Definition Definitions.h:79
std::string String
Definition Definitions.h:63
double Real
Definition Definitions.h:60
int Int
Definition Definitions.h:59
bool Bool
Definition Definitions.h:62
unsigned int UInt
Definition Definitions.h:58
Eigen::SparseMatrix< Real, Eigen::RowMajor > SparseMatrixRow
Sparse matrix for real numbers (row major).
Definition Definitions.h:72