DPsim
CompositePowerComp.cpp
1 // SPDX-License-Identifier: Apache-2.0
2 
3 #include <dpsim-models/CompositePowerComp.h>
4 
5 using namespace CPS;
6 
7 template <typename VarType>
9  typename SimPowerComp<VarType>::Ptr subc,
10  MNA_SUBCOMP_TASK_ORDER preStepOrder, MNA_SUBCOMP_TASK_ORDER postStepOrder,
11  Bool contributeToRightVector) {
12  this->mSubComponents.push_back(subc);
13  if (auto mnasubcomp =
14  std::dynamic_pointer_cast<MNASimPowerComp<VarType>>(subc)) {
15  this->mSubcomponentsMNA.push_back(mnasubcomp);
16 
17  if (contributeToRightVector) {
18  this->mRightVectorStamps.push_back(mnasubcomp->mRightVector);
19  }
20 
21  switch (preStepOrder) {
22  case MNA_SUBCOMP_TASK_ORDER::NO_TASK:
23  break;
24  case MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT: {
25  this->mSubcomponentsBeforePreStep.push_back(mnasubcomp);
26  break;
27  }
28  case MNA_SUBCOMP_TASK_ORDER::TASK_AFTER_PARENT: {
29  this->mSubcomponentsAfterPreStep.push_back(mnasubcomp);
30  break;
31  }
32  }
33  switch (postStepOrder) {
34  case MNA_SUBCOMP_TASK_ORDER::NO_TASK:
35  break;
36  case MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT: {
37  this->mSubcomponentsBeforePostStep.push_back(mnasubcomp);
38  break;
39  }
40  case MNA_SUBCOMP_TASK_ORDER::TASK_AFTER_PARENT: {
41  this->mSubcomponentsAfterPostStep.push_back(mnasubcomp);
42  break;
43  }
44  }
45  }
46 }
47 
48 template <typename VarType>
50  Real omega, Real timeStep, Attribute<Matrix>::Ptr leftVector) {
52 
53  for (auto subComp : mSubcomponentsMNA) {
54  subComp->mnaInitialize(omega, timeStep, leftVector);
55  }
56 
57  **this->mRightVector = Matrix::Zero(leftVector->get().rows(), 1);
58 
59  mnaParentInitialize(omega, timeStep, leftVector);
60 }
61 
62 template <typename VarType>
64  SparseMatrixRow &systemMatrix) {
65  for (auto subComp : mSubcomponentsMNA) {
66  subComp->mnaApplySystemMatrixStamp(systemMatrix);
67  }
68  mnaParentApplySystemMatrixStamp(systemMatrix);
69 }
70 
71 template <typename VarType>
73  Matrix &rightVector) {
74  rightVector.setZero();
75  for (auto stamp : mRightVectorStamps) {
76  if ((**stamp).size() != 0) {
77  rightVector += **stamp;
78  }
79  }
80  mnaParentApplyRightSideVectorStamp(rightVector);
81 }
82 
83 template <typename VarType>
84 void CompositePowerComp<VarType>::mnaCompPreStep(Real time, Int timeStepCount) {
85  for (auto subComp : mSubcomponentsBeforePreStep) {
86  subComp->mnaPreStep(time, timeStepCount);
87  }
88  mnaParentPreStep(time, timeStepCount);
89  for (auto subComp : mSubcomponentsAfterPreStep) {
90  subComp->mnaPreStep(time, timeStepCount);
91  }
92 }
93 
94 template <typename VarType>
96  Real time, Int timeStepCount, Attribute<Matrix>::Ptr &leftVector) {
97  for (auto subComp : mSubcomponentsBeforePostStep) {
98  subComp->mnaPostStep(time, timeStepCount, leftVector);
99  }
100  mnaParentPostStep(time, timeStepCount, leftVector);
101  for (auto subComp : mSubcomponentsAfterPostStep) {
102  subComp->mnaPostStep(time, timeStepCount, leftVector);
103  }
104 }
105 
106 template <typename VarType>
108  AttributeBase::List &prevStepDependencies,
109  AttributeBase::List &attributeDependencies,
110  AttributeBase::List &modifiedAttributes) {
111  for (auto subComp : mSubcomponentsMNA) {
112  subComp->mnaAddPreStepDependencies(
113  prevStepDependencies, attributeDependencies, modifiedAttributes);
114  }
115  mnaParentAddPreStepDependencies(prevStepDependencies, attributeDependencies,
116  modifiedAttributes);
117 }
118 
119 template <typename VarType>
121  AttributeBase::List &prevStepDependencies,
122  AttributeBase::List &attributeDependencies,
123  AttributeBase::List &modifiedAttributes,
124  Attribute<Matrix>::Ptr &leftVector) {
125  for (auto subComp : mSubcomponentsMNA) {
126  subComp->mnaAddPostStepDependencies(prevStepDependencies,
127  attributeDependencies,
128  modifiedAttributes, leftVector);
129  }
130  mnaParentAddPostStepDependencies(prevStepDependencies, attributeDependencies,
131  modifiedAttributes, leftVector);
132 }
133 
134 // Declare specializations to move definitions to .cpp
135 template class CPS::CompositePowerComp<Real>;
136 template class CPS::CompositePowerComp<Complex>;
virtual T & get()=0
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.
void mnaCompApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) override
Stamps system matrix.
void mnaCompPreStep(Real time, Int timeStepCount) override
MNA pre step operations.
void mnaCompApplyRightSideVectorStamp(Matrix &rightVector) override
Stamps right side (source) vector.
void mnaCompAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override
Add MNA pre step dependencies.
void mnaCompPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
MNA post step operations.
Base class for all MNA components that are transmitting power.
void updateMatrixNodeIndices()
Update the "cached" mMatrixNodeIndices and mMatrixNodeIndexIsGround members.