DPsim
SimNode.h
1 /* Copyright 2017-2021 Institute for Automation of Complex Power Systems,
2  * EONERC, RWTH Aachen University
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
7  *********************************************************************************/
8 
9 #pragma once
10 
11 #include <dpsim-models/Task.h>
12 #include <dpsim-models/TopologicalNode.h>
13 
14 namespace CPS {
15 
16 template <typename VarType>
17 class SimNode : public TopologicalNode,
18  public std::enable_shared_from_this<SimNode<VarType>>,
19  public SharedFactory<SimNode<VarType>> {
20 protected:
22  std::vector<UInt> mMatrixNodeIndex = {0};
24  Matrix mFrequencies;
26  UInt mNumFreqs = 0;
28  Task::List mMnaTasks;
29 
30 public:
31  typedef VarType Type;
32  typedef std::shared_ptr<SimNode<VarType>> Ptr;
33  typedef std::vector<Ptr> List;
35  static Ptr GND;
36 
38  const typename Attribute<MatrixVar<VarType>>::Ptr mVoltage;
41 
43  SimNode(String uid, String name, std::vector<UInt> matrixNodeIndex,
44  PhaseType phaseType, const std::vector<Complex> &initialVoltage);
46  SimNode(PhaseType phaseType = PhaseType::Single);
49  SimNode(String name, PhaseType phaseType = PhaseType::Single,
50  const std::vector<Complex> &initialVoltage = {0, 0, 0})
51  : SimNode(name, name, {0, 0, 0}, phaseType, initialVoltage) {}
54  SimNode(String uid, String name, UInt matrixNodeIndex,
55  PhaseType phaseType = PhaseType::Single,
56  const std::vector<Complex> &initialVoltage = {0, 0, 0})
57  : SimNode(uid, name,
59  phaseType, initialVoltage) {}
62  SimNode(UInt matrixNodeIndex, PhaseType phaseType = PhaseType::Single)
63  : SimNode("N" + std::to_string(matrixNodeIndex),
64  "N" + std::to_string(matrixNodeIndex), matrixNodeIndex,
65  phaseType) {}
66 
68  void initialize();
70  void initialize(Matrix frequencies);
72  UInt matrixNodeIndex(PhaseType phaseType = PhaseType::Single) override;
74  std::vector<UInt> matrixNodeIndices() override;
76  VarType singleVoltage(PhaseType phaseType = PhaseType::Single);
78  MatrixVar<VarType> voltage();
80  void setMatrixNodeIndex(UInt phase, UInt matrixNodeIndex) override;
82  void setVoltage(VarType newVoltage) {}
84  void setPower(VarType newPower) {}
85 
86  // #### MNA Section ####
88  void mnaUpdateVoltage(const Matrix &leftVector);
90  void mnaInitializeHarm(std::vector<Attribute<Matrix>::Ptr> leftVector);
92  void mnaUpdateVoltageHarm(const Matrix &leftVector, Int freqIdx);
94  const Task::List &mnaTasks();
96  class MnaPostStepHarm : public Task {
97  public:
99  const std::vector<Attribute<Matrix>::Ptr> &leftVectors)
100  : Task(**node.mName + ".MnaPostStepHarm"), mNode(node),
101  mLeftVectors(leftVectors) {
102  for (UInt i = 0; i < mLeftVectors.size(); i++)
103  mAttributeDependencies.push_back(mLeftVectors[i]);
104  mModifiedAttributes.push_back(mNode.attribute("v"));
105  }
106  void execute(Real time, Int timeStepCount);
107 
108  private:
109  SimNode &mNode;
110  std::vector<Attribute<Matrix>::Ptr> mLeftVectors;
111  };
112 };
113 
114 namespace SP {
116 }
117 namespace DP {
118 typedef CPS::SimNode<Complex> SimNode;
119 }
120 namespace EMT {
121 typedef CPS::SimNode<Real> SimNode;
122 }
123 
124 template <typename VarType>
125 typename SimNode<VarType>::Ptr SimNode<VarType>::GND = SimNode<VarType>::make();
126 
127 template <> void SimNode<Real>::mnaUpdateVoltage(const Matrix &leftVector);
128 
129 template <> void SimNode<Complex>::mnaUpdateVoltage(const Matrix &leftVector);
130 
131 template <>
132 void SimNode<Complex>::mnaInitializeHarm(
133  std::vector<Attribute<Matrix>::Ptr> leftVector);
134 
135 template <> void SimNode<Complex>::setVoltage(Complex newVoltage);
136 
137 template <> void SimNode<Complex>::setPower(Complex newPower);
138 } // namespace CPS
const Attribute< String >::Ptr mName
Human readable name.
String uid()
Returns unique id.
AttributeBase::Ptr attribute(const String &name) const
Return pointer to an attribute.
Definition: SimNode.h:96
SimNode(String uid, String name, std::vector< UInt > matrixNodeIndex, PhaseType phaseType, const std::vector< Complex > &initialVoltage)
This very general constructor is used by other constructors.
Definition: SimNode.cpp:14
const Attribute< MatrixVar< VarType > >::Ptr mApparentPower
Power injected at node.
Definition: SimNode.h:40
void initialize()
Initialize mVoltage according to mInitialVoltage.
const Task::List & mnaTasks()
Return list of MNA tasks.
Definition: SimNode.cpp:111
SimNode(String uid, String name, UInt matrixNodeIndex, PhaseType phaseType=PhaseType::Single, const std::vector< Complex > &initialVoltage={0, 0, 0})
Definition: SimNode.h:54
UInt matrixNodeIndex(PhaseType phaseType=PhaseType::Single) override
Returns matrix index for specified phase.
Definition: SimNode.cpp:75
UInt mNumFreqs
Number of harmonics.
Definition: SimNode.h:26
std::vector< UInt > matrixNodeIndices() override
Returns all matrix indices.
Definition: SimNode.cpp:91
SimNode(String name, PhaseType phaseType=PhaseType::Single, const std::vector< Complex > &initialVoltage={0, 0, 0})
Definition: SimNode.h:49
SimNode(UInt matrixNodeIndex, PhaseType phaseType=PhaseType::Single)
Definition: SimNode.h:62
Matrix mFrequencies
List of considered network harmonics.
Definition: SimNode.h:24
Tasks to be defined by every component.
Definition: Task.h:25