DPsim
Loading...
Searching...
No Matches
MNASimPowerComp.h
1// SPDX-License-Identifier: Apache-2.0
2
3#pragma once
4
5#include <dpsim-models/MNAStampUtils.h>
6#include <dpsim-models/SimPowerComp.h>
7#include <dpsim-models/Solver/MNAInterface.h>
8
9namespace CPS {
10
12template <typename VarType>
13class MNASimPowerComp : public SimPowerComp<VarType>, public MNAInterface {
14
15private:
16 Bool mHasPreStep;
17 Bool mHasPostStep;
18
19public:
20 using Type = VarType;
21 using Ptr = std::shared_ptr<MNASimPowerComp<VarType>>;
22 using List = std::vector<Ptr>;
23
26 Attribute<Matrix>::Ptr mRightVector;
27
29 Task::List mMnaTasks;
30
32 MNASimPowerComp(String uid, String name, Bool hasPreStep, Bool hasPostStep,
33 Logger::Level logLevel)
34 : SimPowerComp<VarType>(uid, name, logLevel), mHasPreStep(hasPreStep),
35 mHasPostStep(hasPostStep),
36 mRightVector(IdentifiedObject::mAttributes->createDynamic<Matrix>(
37 "right_vector")){};
38
40 explicit MNASimPowerComp(String name, Bool hasPreStep = true,
41 Bool hasPostStep = true,
42 Logger::Level logLevel = Logger::Level::off)
43 : MNASimPowerComp<VarType>(name, name, hasPreStep, hasPostStep,
44 logLevel){};
45
47 virtual ~MNASimPowerComp() = default;
48
49 // Implementation of MNAInterface methods
50 void mnaInitialize(Real omega, Real timeStep) final;
51 void mnaInitialize(Real omega, Real timeStep,
52 Attribute<Matrix>::Ptr leftVector) final;
53 void mnaApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) final;
54 void mnaApplyRightSideVectorStamp(Matrix &rightVector) final;
55 void mnaUpdateVoltage(const Matrix &leftVector) final;
56 void mnaUpdateCurrent(const Matrix &leftVector) final;
57 void mnaPreStep(Real time, Int timeStepCount) final;
58 void mnaPostStep(Real time, Int timeStepCount,
59 Attribute<Matrix>::Ptr &leftVector) final;
60 void mnaAddPreStepDependencies(AttributeBase::List &prevStepDependencies,
61 AttributeBase::List &attributeDependencies,
62 AttributeBase::List &modifiedAttributes) final;
63 void mnaAddPostStepDependencies(AttributeBase::List &prevStepDependencies,
64 AttributeBase::List &attributeDependencies,
65 AttributeBase::List &modifiedAttributes,
66 Attribute<Matrix>::Ptr &leftVector) final;
67 void mnaInitializeHarm(Real omega, Real timeStep,
68 std::vector<Attribute<Matrix>::Ptr> leftVector) final;
69 void mnaApplySystemMatrixStampHarm(SparseMatrixRow &systemMatrix,
70 Int freqIdx) final;
71 void mnaApplyRightSideVectorStampHarm(Matrix &sourceVector) final;
72 void mnaApplyRightSideVectorStampHarm(Matrix &sourceVector,
73 Int freqIdx) final;
74
75 // MNA Interface methods that can be overridden by components
76 virtual void mnaCompInitialize(Real omega, Real timeStep,
77 Attribute<Matrix>::Ptr leftVector);
78 virtual void mnaCompApplySystemMatrixStamp(SparseMatrixRow &systemMatrix);
79 virtual void mnaCompApplyRightSideVectorStamp(Matrix &rightVector);
80 virtual void mnaCompUpdateVoltage(const Matrix &leftVector);
81 virtual void mnaCompUpdateCurrent(const Matrix &leftVector);
82 virtual void mnaCompPreStep(Real time, Int timeStepCount);
83 virtual void mnaCompPostStep(Real time, Int timeStepCount,
84 Attribute<Matrix>::Ptr &leftVector);
85 virtual void
86 mnaCompAddPreStepDependencies(AttributeBase::List &prevStepDependencies,
87 AttributeBase::List &attributeDependencies,
88 AttributeBase::List &modifiedAttributes);
89 virtual void
90 mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies,
91 AttributeBase::List &attributeDependencies,
92 AttributeBase::List &modifiedAttributes,
93 Attribute<Matrix>::Ptr &leftVector);
94 virtual void
95 mnaCompInitializeHarm(Real omega, Real timeStep,
96 std::vector<Attribute<Matrix>::Ptr> leftVector);
97 virtual void mnaCompApplySystemMatrixStampHarm(SparseMatrixRow &systemMatrix,
98 Int freqIdx);
99 virtual void mnaCompApplyRightSideVectorStampHarm(Matrix &sourceVector);
100 virtual void mnaCompApplyRightSideVectorStampHarm(Matrix &sourceVector,
101 Int freqIdx);
102
103 const Task::List &mnaTasks() const final;
104 Attribute<Matrix>::Ptr getRightVector() const final;
105
106 class MnaPreStep : public CPS::Task {
107 public:
108 explicit MnaPreStep(MNASimPowerComp<VarType> &comp)
109 : Task(**comp.mName + ".MnaPreStep"), mComp(comp) {
110 mComp.mnaAddPreStepDependencies(
111 mPrevStepDependencies, mAttributeDependencies, mModifiedAttributes);
112 }
113 void execute(Real time, Int timeStepCount) override {
114 mComp.mnaPreStep(time, timeStepCount);
115 };
116
117 private:
119 };
120
121 class MnaPostStep : public CPS::Task {
122 public:
123 MnaPostStep(MNASimPowerComp<VarType> &comp,
124 Attribute<Matrix>::Ptr leftVector)
125 : Task(**comp.mName + ".MnaPostStep"), mComp(comp),
126 mLeftVector(leftVector) {
127 mComp.mnaAddPostStepDependencies(mPrevStepDependencies,
128 mAttributeDependencies,
129 mModifiedAttributes, mLeftVector);
130 }
131 void execute(Real time, Int timeStepCount) override {
132 mComp.mnaPostStep(time, timeStepCount, mLeftVector);
133 };
134
135 private:
137 Attribute<Matrix>::Ptr mLeftVector;
138 };
139};
140} // namespace CPS
const Attribute< String >::Ptr mName
Human readable name.
String uid()
Returns unique id.
AttributeList::Ptr mAttributes
Attribute List.
Interface to be implemented by all models used by the MNA solver.
void mnaAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) final
Add MNA pre step dependencies.
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.
void mnaApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) final
Stamps system matrix.
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.
const Task::List & mnaTasks() const final
Return list of MNA tasks.
void mnaInitialize(Real omega, Real timeStep) final
Initializes variables of components.
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 mnaPreStep(Real time, Int timeStepCount) final
MNA pre step operations.
void mnaApplySystemMatrixStampHarm(SparseMatrixRow &systemMatrix, Int freqIdx) final
Stamps system matrix considering the frequency index.
void mnaApplyRightSideVectorStamp(Matrix &rightVector) final
Stamps right side (source) vector.
SimPowerComp(String uid, String name, Logger::Level logLevel=Logger::Level::off)
Basic constructor that takes UID, name and log level.
Tasks to be defined by every component.
Definition Task.h:25