DPsim
Loading...
Searching...
No Matches
SimNode.cpp
Go to the documentation of this file.
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
10
11using namespace CPS;
12
13template <typename VarType>
32
33template <typename VarType>
35 : SimNode("gnd", "gnd", {0, 0, 0}, phaseType, {0, 0, 0}) {
36 mIsGround = true;
37 **mInitialVoltage = MatrixComp::Zero(3, 1);
38 **mVoltage = MatrixVar<VarType>::Zero(3, 1);
39}
40
41template <> void SimNode<Real>::initialize() {
42 if (phaseType() == PhaseType::DC)
43 (**mVoltage)(0, 0) = (**mInitialVoltage)(0, 0).real();
44 else if (phaseType() == PhaseType::Single)
45 (**mVoltage)(0, 0) = (RMS3PH_TO_PEAK1PH * (**mInitialVoltage)(0, 0)).real();
46 else
49
50template <> void SimNode<Complex>::initialize() {
51 (**mVoltage)(0, 0) = mComplexInitialVoltageScale * (**mInitialVoltage)(0, 0);
52 if (phaseType() == PhaseType::ABC) {
53 (**mVoltage)(1, 0) =
54 mComplexInitialVoltageScale * (**mInitialVoltage)(1, 0);
55 (**mVoltage)(2, 0) =
56 mComplexInitialVoltageScale * (**mInitialVoltage)(2, 0);
57 }
58}
59
60template <typename VarType>
62 mFrequencies = frequencies;
63 mNumFreqs = static_cast<UInt>(mFrequencies.size());
64 Matrix::Index rowNum = phaseType() == PhaseType::ABC ? 3 : 1;
66}
67
68template <typename VarType>
69std::shared_ptr<TopologicalNode> SimNode<VarType>::clone(String name) {
70 auto nodeCpy = SimNode<VarType>::make(name, phaseType());
71 nodeCpy->setInitialVoltage(initialVoltage());
72 nodeCpy->setComplexInitialVoltageScale(mComplexInitialVoltageScale);
73 return nodeCpy;
74}
75
76template <typename VarType>
79 return (**mVoltage)(1, 0);
81 return (**mVoltage)(2, 0);
82 else // phaseType == PhaseType::Single || mPhaseType == PhaseType::A
83 return (**mVoltage)(0, 0);
85
86template <typename VarType>
91 return mMatrixNodeIndex[0];
92 }
93
94 return 0;
95 }
96
97 if ((phaseType == PhaseType::A || phaseType == PhaseType::Single) &&
98 (mPhaseType == PhaseType::Single || mPhaseType == PhaseType::A ||
99 mPhaseType == PhaseType::ABC)) {
100 return mMatrixNodeIndex[0];
101 }
103 if (phaseType == PhaseType::B &&
105 return mMatrixNodeIndex[1];
106 }
107
108 if (phaseType == PhaseType::C &&
109 (mPhaseType == PhaseType::C || mPhaseType == PhaseType::ABC)) {
110 return mMatrixNodeIndex[2];
111 }
112
113 return 0;
114}
115
116template <typename VarType>
119 return {mMatrixNodeIndex[1]};
120 else if (mPhaseType == PhaseType::C)
121 return {mMatrixNodeIndex[2]};
122 else if (mPhaseType == PhaseType::ABC)
123 return mMatrixNodeIndex;
124 else // phaseType == PhaseType::Single || mPhaseType == PhaseType::A
125 return {mMatrixNodeIndex[0]};
126}
127
128template <typename VarType> MatrixVar<VarType> SimNode<VarType>::voltage() {
129 return **mVoltage;
130}
131
132template <typename VarType>
136
137template <typename VarType> const Task::List &SimNode<VarType>::mnaTasks() {
138 return mMnaTasks;
139}
140
141template <> void SimNode<Complex>::setVoltage(Complex newVoltage) {
142 (**mVoltage)(0, 0) = newVoltage;
143}
144
145template <> void SimNode<Complex>::setPower(Complex newPower) {
146 (**mApparentPower)(0, 0) = newPower;
147}
148
149template <> void SimNode<Real>::mnaUpdateVoltage(const Matrix &leftVector) {
150 if (mMatrixNodeIndex[0] >= 0)
151 (**mVoltage)(0, 0) =
152 Math::realFromVectorElement(leftVector, mMatrixNodeIndex[0]);
153 if (mPhaseType == PhaseType::ABC) {
154 if (mMatrixNodeIndex[1] >= 0)
155 (**mVoltage)(1, 0) =
156 Math::realFromVectorElement(leftVector, mMatrixNodeIndex[1]);
157 if (mMatrixNodeIndex[2] >= 0)
158 (**mVoltage)(2, 0) =
159 Math::realFromVectorElement(leftVector, mMatrixNodeIndex[2]);
160 }
161}
162
163template <> void SimNode<Complex>::mnaUpdateVoltage(const Matrix &leftVector) {
164 for (UInt freq = 0; freq < mNumFreqs; freq++) {
165 if (mMatrixNodeIndex[0] >= 0)
166 (**mVoltage)(0, freq) = Math::complexFromVectorElement(
167 leftVector, mMatrixNodeIndex[0], mNumFreqs, freq);
168 if (mPhaseType == PhaseType::ABC) {
169 if (mMatrixNodeIndex[1] >= 0)
170 (**mVoltage)(1, freq) = Math::complexFromVectorElement(
171 leftVector, mMatrixNodeIndex[1], mNumFreqs, freq);
172 if (mMatrixNodeIndex[2] >= 0)
173 (**mVoltage)(2, freq) = Math::complexFromVectorElement(
174 leftVector, mMatrixNodeIndex[2], mNumFreqs, freq);
175 }
176 }
177}
178
179template <>
180void SimNode<Real>::mnaUpdateVoltageHarm(const Matrix &leftVector,
181 Int freqIdx) {}
182
183template <>
185 Int freqIdx) {
186 if (mMatrixNodeIndex[0] >= 0)
187 (**mVoltage)(0, freqIdx) =
188 Math::complexFromVectorElement(leftVector, mMatrixNodeIndex[0]);
189 if (mPhaseType == PhaseType::ABC) {
190 if (mMatrixNodeIndex[1] >= 0)
191 (**mVoltage)(1, freqIdx) =
192 Math::complexFromVectorElement(leftVector, mMatrixNodeIndex[1]);
193 if (mMatrixNodeIndex[2] >= 0)
194 (**mVoltage)(2, freqIdx) =
195 Math::complexFromVectorElement(leftVector, mMatrixNodeIndex[2]);
196 }
197}
198
199template <>
201 std::vector<Attribute<Matrix>::Ptr> leftVectors) {}
202
203template <>
205 std::vector<Attribute<Matrix>::Ptr> leftVectors) {
206 mMnaTasks = {std::make_shared<MnaPostStepHarm>(*this, leftVectors)};
207}
208
209template <>
210void SimNode<Complex>::MnaPostStepHarm::execute(Real time, Int timeStepCount) {
211 for (UInt freq = 0; freq < mNode.mNumFreqs; freq++)
212 mNode.mnaUpdateVoltageHarm(**mLeftVectors[freq], freq);
213}
214
215template <>
216void SimNode<Real>::MnaPostStepHarm::execute(Real time, Int timeStepCount) {}
217
218template class CPS::SimNode<Real>;
219template class CPS::SimNode<Complex>;
AttributePointer< Attribute< T > > Ptr
Definition Attribute.h:249
String uid()
Returns unique id.
AttributeList::Ptr mAttributes
Attribute List.
static Complex complexFromVectorElement(const Matrix &mat, Matrix::Index row, Int maxFreq=1, Int freqIdx=0)
static Real realFromVectorElement(const Matrix &mat, Matrix::Index row)
void execute(Real time, Int timeStepCount)
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
void mnaUpdateVoltage(const Matrix &leftVector)
void setVoltage(VarType newVoltage)
Definition SimNode.h:90
void initialize()
Initialize mVoltage according to mInitialVoltage.
void setPower(VarType newPower)
Definition SimNode.h:92
Task::List mMnaTasks
Definition SimNode.h:30
std::shared_ptr< TopologicalNode > clone(String name)
Definition SimNode.cpp:69
const Task::List & mnaTasks()
Return list of MNA tasks.
Definition SimNode.cpp:137
UInt matrixNodeIndex(PhaseType phaseType=PhaseType::Single) override
Returns matrix index for specified phase.
Definition SimNode.cpp:87
MatrixVar< VarType > voltage()
Definition SimNode.cpp:128
void setMatrixNodeIndex(UInt phase, UInt matrixNodeIndex) override
Definition SimNode.cpp:133
const Attribute< MatrixVar< VarType > >::Ptr mVoltage
Definition SimNode.h:40
UInt mNumFreqs
Number of harmonics.
Definition SimNode.h:26
VarType singleVoltage(PhaseType phaseType=PhaseType::Single)
Definition SimNode.cpp:77
std::vector< UInt > matrixNodeIndices() override
Returns all matrix indices.
Definition SimNode.cpp:117
Real mComplexInitialVoltageScale
Optional conversion applied when a complex node initializes from PF.
Definition SimNode.h:28
void mnaUpdateVoltageHarm(const Matrix &leftVector, Int freqIdx)
void mnaInitializeHarm(std::vector< Attribute< Matrix >::Ptr > leftVector)
const Attribute< MatrixVar< VarType > >::Ptr mApparentPower
Power injected at node.
Definition SimNode.h:42
Matrix mFrequencies
List of considered network harmonics.
Definition SimNode.h:24
std::vector< UInt > mMatrixNodeIndex
Definition SimNode.h:22
std::vector< Ptr > List
Definition Task.h:28
const Attribute< MatrixComp >::Ptr mInitialVoltage
PhaseType phaseType() const
MatrixComp initialVoltage() const
static std::shared_ptr< SimNode< VarType > > make(Args &&...args)
Definition PtrFactory.h:19
#define RMS3PH_TO_PEAK1PH
Definition Definitions.h:50
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
Eigen::Matrix< VarType, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > MatrixVar
Definition Definitions.h:95
std::complex< Real > Complex
Definition Definitions.h:61
unsigned int UInt
Definition Definitions.h:58