DPsim
Loading...
Searching...
No Matches
CompositePowerComp.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2
4
5#include <algorithm>
6#include <type_traits>
7
8using namespace CPS;
9
10template <typename VarType>
12 Real frequency) {
13 this->createSubComponents();
14
16
17 for (auto subComp : this->mSubComponents) {
18 subComp->initialize(this->mFrequencies);
19 subComp->initializeFromNodesAndTerminals(frequency);
20 }
21}
22
23template <typename VarType>
25 typename SimPowerComp<VarType>::Ptr subc,
26 MNA_SUBCOMP_TASK_ORDER preStepOrder, MNA_SUBCOMP_TASK_ORDER postStepOrder,
27 Bool contributeToRightVector) {
28 this->mSubComponents.push_back(subc);
29 if (auto mnasubcomp =
30 std::dynamic_pointer_cast<MNASimPowerComp<VarType>>(subc)) {
31 this->mSubcomponentsMNA.push_back(mnasubcomp);
32
33 if (contributeToRightVector) {
34 this->mRightVectorStamps.push_back(mnasubcomp->mRightVector);
35 this->mRightVectorStampComponents.push_back(subc);
36 }
37
38 switch (preStepOrder) {
40 break;
42 this->mSubcomponentsPreStepBeforeParent.push_back(mnasubcomp);
43 break;
44 }
46 this->mSubcomponentsPreStepAfterParent.push_back(mnasubcomp);
47 break;
48 }
49 }
50 switch (postStepOrder) {
52 break;
54 this->mSubcomponentsPostStepBeforeParent.push_back(mnasubcomp);
55 break;
56 }
58 this->mSubcomponentsPostStepAfterParent.push_back(mnasubcomp);
59 break;
60 }
61 }
62 }
63}
64
65template <typename VarType>
67 Real omega, Real timeStep, Attribute<Matrix>::Ptr leftVector) {
69
70 for (auto subComp : mSubcomponentsMNA) {
71 subComp->mnaInitialize(omega, timeStep, leftVector);
72 }
73
74 **this->mRightVector = Matrix::Zero(leftVector->get().rows(), 1);
75
76 // RHS stamps are dense vectors for historical reasons, but a subcomponent
77 // can only write at its connected terminal and virtual-node indices. Cache
78 // those locations once so the composite can aggregate the stamps without
79 // clearing and adding full network-sized vectors on every step.
80 const UInt vectorSize = static_cast<UInt>(leftVector->get().rows());
81 const UInt baseRowLimit = [&]() {
82 if constexpr (std::is_same_v<VarType, Complex>)
83 return vectorSize / (2 * this->mNumFreqs);
84 else
85 return vectorSize;
86 }();
87 auto appendNodeIndices = [baseRowLimit](SimPowerComp<VarType> &component,
88 std::vector<UInt> &indices) {
89 for (UInt terminal = 0; terminal < component.terminalNumberConnected();
90 ++terminal) {
91 if (component.node(terminal)->isGround())
92 continue;
93 for (UInt index : component.matrixNodeIndices(terminal)) {
94 if (index < baseRowLimit)
95 indices.push_back(index);
96 }
97 }
99 ++virtualNode) {
100 for (UInt index : component.virtualMatrixNodeIndices(virtualNode)) {
101 if (index < baseRowLimit)
102 indices.push_back(index);
103 }
104 }
105 // A nested composite may contribute at its children's internal virtual
106 // nodes as well as at its own terminals. Its MNA initialization has
107 // already cached those rows before this parent is initialized.
108 if (auto nested = dynamic_cast<CompositePowerComp<VarType> *>(&component))
109 indices.insert(indices.end(), nested->mRightVectorAffectedIndices.begin(),
110 nested->mRightVectorAffectedIndices.end());
111 std::sort(indices.begin(), indices.end());
112 indices.erase(std::unique(indices.begin(), indices.end()), indices.end());
113 };
114
115 mRightVectorStampIndices.clear();
116 mRightVectorStampIndices.reserve(mRightVectorStampComponents.size());
117 mRightVectorAffectedIndices.clear();
118 appendNodeIndices(*this, mRightVectorAffectedIndices);
119 for (const auto &component : mRightVectorStampComponents) {
120 std::vector<UInt> indices;
121 appendNodeIndices(*component, indices);
122 mRightVectorAffectedIndices.insert(mRightVectorAffectedIndices.end(),
123 indices.begin(), indices.end());
124 mRightVectorStampIndices.push_back(std::move(indices));
125 }
126 std::sort(mRightVectorAffectedIndices.begin(),
127 mRightVectorAffectedIndices.end());
128 mRightVectorAffectedIndices.erase(
129 std::unique(mRightVectorAffectedIndices.begin(),
130 mRightVectorAffectedIndices.end()),
131 mRightVectorAffectedIndices.end());
132 mRightVectorStampIndicesInitialized = true;
133
134 mnaParentInitialize(omega, timeStep, leftVector);
135}
136
137template <typename VarType>
139 SparseMatrixRow &systemMatrix) {
140 for (auto subComp : mSubcomponentsMNA) {
141 subComp->mnaApplySystemMatrixStamp(systemMatrix);
142 }
144}
145
146template <typename VarType>
148 Matrix &rightVector) {
149 if (!mRightVectorStampIndicesInitialized) {
150 rightVector.setZero();
151 for (const auto &stamp : mRightVectorStamps) {
152 if ((**stamp).size() != 0)
153 rightVector += **stamp;
154 }
155 } else {
156 if constexpr (std::is_same_v<VarType, Complex>) {
157 for (UInt freq = 0; freq < this->mNumFreqs; ++freq)
158 for (UInt index : mRightVectorAffectedIndices)
159 Math::setVectorElement(rightVector, index, Complex(0., 0.),
160 this->mNumFreqs, freq);
161 } else {
162 for (UInt index : mRightVectorAffectedIndices)
163 rightVector(index, 0) = 0.;
164 }
165 for (std::size_t stampIndex = 0; stampIndex < mRightVectorStamps.size();
166 ++stampIndex) {
167 const auto &stamp = mRightVectorStamps[stampIndex];
168 if ((**stamp).size() == 0)
169 continue;
170 if constexpr (std::is_same_v<VarType, Complex>) {
171 for (UInt freq = 0; freq < this->mNumFreqs; ++freq)
172 for (UInt index : mRightVectorStampIndices[stampIndex])
173 Math::addToVectorElement(rightVector, index,
175 **stamp, index, this->mNumFreqs, freq),
176 this->mNumFreqs, freq);
177 } else {
178 for (UInt index : mRightVectorStampIndices[stampIndex])
179 rightVector(index, 0) += (**stamp)(index, 0);
180 }
181 }
182 }
184}
185
186template <typename VarType>
188 Matrix &rightVector) {
189 if (!mRightVectorStampIndicesInitialized) {
190 rightVector += **this->mRightVector;
191 return;
192 }
193
194 const Matrix &stamp = **this->mRightVector;
195 if constexpr (std::is_same_v<VarType, Complex>) {
196 for (UInt freq = 0; freq < this->mNumFreqs; ++freq)
197 for (UInt index : mRightVectorAffectedIndices)
199 rightVector, index,
200 Math::complexFromVectorElement(stamp, index, this->mNumFreqs, freq),
201 this->mNumFreqs, freq);
202 } else {
203 for (UInt index : mRightVectorAffectedIndices)
204 rightVector(index, 0) += stamp(index, 0);
205 }
206}
207
208template <typename VarType>
210 for (const auto &subComp : mSubcomponentsPreStepBeforeParent) {
211 subComp->mnaPreStep(time, timeStepCount);
212 }
213 mnaParentPreStep(time, timeStepCount);
214 for (const auto &subComp : mSubcomponentsPreStepAfterParent) {
215 subComp->mnaPreStep(time, timeStepCount);
216 }
217}
218
219template <typename VarType>
221 Real time, Int timeStepCount, Attribute<Matrix>::Ptr &leftVector) {
222 for (const auto &subComp : mSubcomponentsPostStepBeforeParent) {
223 subComp->mnaPostStep(time, timeStepCount, leftVector);
224 }
225 mnaParentPostStep(time, timeStepCount, leftVector);
226 for (const auto &subComp : mSubcomponentsPostStepAfterParent) {
227 subComp->mnaPostStep(time, timeStepCount, leftVector);
228 }
229}
230
231template <typename VarType>
233 AttributeBase::List &prevStepDependencies,
234 AttributeBase::List &attributeDependencies,
235 AttributeBase::List &modifiedAttributes) {
236 for (auto subComp : mSubcomponentsMNA) {
237 subComp->mnaAddPreStepDependencies(
238 prevStepDependencies, attributeDependencies, modifiedAttributes);
239 }
240 mnaParentAddPreStepDependencies(prevStepDependencies, attributeDependencies,
241 modifiedAttributes);
242}
243
244template <typename VarType>
246 AttributeBase::List &prevStepDependencies,
247 AttributeBase::List &attributeDependencies,
248 AttributeBase::List &modifiedAttributes,
249 Attribute<Matrix>::Ptr &leftVector) {
250 for (auto subComp : mSubcomponentsMNA) {
251 subComp->mnaAddPostStepDependencies(prevStepDependencies,
252 attributeDependencies,
253 modifiedAttributes, leftVector);
254 }
255 mnaParentAddPostStepDependencies(prevStepDependencies, attributeDependencies,
256 modifiedAttributes, leftVector);
257}
258
259// Declare specializations to move definitions to .cpp
260template class CPS::CompositePowerComp<Real>;
std::vector< Ptr > List
Definition Attribute.h:123
AttributePointer< Attribute< T > > Ptr
Definition Attribute.h:249
Base class for composite power components.
virtual void mnaParentApplySystemMatrixStamp(SparseMatrixRow &systemMatrix)
void mnaCompInitialize(Real omega, Real timeStep, Attribute< Matrix >::Ptr leftVector) override
Initializes variables of components.
void addMNASubComponent(typename SimPowerComp< VarType >::Ptr subc, MNA_SUBCOMP_TASK_ORDER preStepOrder, MNA_SUBCOMP_TASK_ORDER postStepOrder, Bool contributeToRightVector)
Add a new subcomponent implementing MNA methods.
void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
Add MNA post step dependencies.
virtual void initializeParentFromNodesAndTerminals(Real frequency)=0
virtual void mnaParentApplyRightSideVectorStamp(Matrix &rightVector)
void initializeFromNodesAndTerminals(Real frequency) override final
void mnaAddCompactRightSideVectorStamp(Matrix &rightVector) override
virtual void mnaParentAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes)
void mnaCompApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) override
Stamps system matrix.
virtual void mnaParentPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector)
void mnaCompPreStep(Real time, Int timeStepCount) override
MNA pre step operations.
void mnaCompApplyRightSideVectorStamp(Matrix &rightVector) override
Stamps right side (source) vector.
virtual void mnaParentAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector)
void mnaCompAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override
Add MNA pre step dependencies.
virtual void mnaParentPreStep(Real time, Int timeStepCount)
void mnaCompPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
MNA post step operations.
CompositePowerComp(String uid, String name, Bool hasPreStep, Bool hasPostStep, Logger::Level logLevel)
virtual void createSubComponents()
Constructs and registers MNA subcomponents; called before collectVirtualNodes so virtual nodes exist ...
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
static Complex complexFromVectorElement(const Matrix &mat, Matrix::Index row, Int maxFreq=1, Int freqIdx=0)
static void setVectorElement(Matrix &mat, Matrix::Index row, Complex value, Int maxFreq=1, Int freqIdx=0, Matrix::Index colOffset=0)
static void addToVectorElement(Matrix &mat, Matrix::Index row, Complex value, Int maxFreq=1, Int freqIdx=0)
Base class for all components that are transmitting power.
SimNode< VarType >::Ptr node(UInt index)
Get pointer to node.
Matrix mFrequencies
List of considered network frequencies.
SimTerminal< Complex >::Ptr terminal(UInt index)
std::vector< UInt > virtualMatrixNodeIndices(UInt index)
Get vector of simulation node numbers from virtual Node.
UInt virtualNodesNumber()
Returns nominal number of virtual nodes for this component type.
SimNode< Complex >::Ptr virtualNode(UInt index)
UInt terminalNumberConnected()
Returns the number of connected Terminals.
std::shared_ptr< SimPowerComp< VarType > > Ptr
std::vector< std::shared_ptr< SimPowerComp< VarType > > > mSubComponents
void updateMatrixNodeIndices()
Update the "cached" mMatrixNodeIndices and mMatrixNodeIndexIsGround members.
std::vector< UInt > matrixNodeIndices(UInt index)
TODO replace with access to mMatrixNodeIndices.
Eigen::Matrix< Real, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > Matrix
Dense matrix for real numbers.
Definition Definitions.h:79
MNA_SUBCOMP_TASK_ORDER
double Real
Definition Definitions.h:60
int Int
Definition Definitions.h:59
std::complex< Real > Complex
Definition Definitions.h:61
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