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
48}
49
50template <> void SimNode<Complex>::initialize() {
51 (**mVoltage)(0, 0) = (**mInitialVoltage)(0, 0);
52 if (phaseType() == PhaseType::ABC) {
53 (**mVoltage)(1, 0) = (**mInitialVoltage)(1, 0);
54 (**mVoltage)(2, 0) = (**mInitialVoltage)(2, 0);
55 }
56}
57
58template <typename VarType>
60 mFrequencies = frequencies;
61 mNumFreqs = static_cast<UInt>(mFrequencies.size());
62 Matrix::Index rowNum = phaseType() == PhaseType::ABC ? 3 : 1;
64}
65
66template <typename VarType>
67std::shared_ptr<TopologicalNode> SimNode<VarType>::clone(String name) {
68 auto nodeCpy = SimNode<VarType>::make(name, phaseType());
69 nodeCpy->setInitialVoltage(initialVoltage());
70 return nodeCpy;
71}
73template <typename VarType>
76 return (**mVoltage)(1, 0);
77 else if (phaseType == PhaseType::C)
78 return (**mVoltage)(2, 0);
79 else // phaseType == PhaseType::Single || mPhaseType == PhaseType::A
80 return (**mVoltage)(0, 0);
81}
83template <typename VarType>
88 return mMatrixNodeIndex[0];
89 }
90
91 return 0;
92 }
93
97 return mMatrixNodeIndex[0];
98 }
99
100 if (phaseType == PhaseType::B &&
101 (mPhaseType == PhaseType::B || mPhaseType == PhaseType::ABC)) {
102 return mMatrixNodeIndex[1];
103 }
104
105 if (phaseType == PhaseType::C &&
106 (mPhaseType == PhaseType::C || mPhaseType == PhaseType::ABC)) {
107 return mMatrixNodeIndex[2];
108 }
109
110 return 0;
111}
112
113template <typename VarType>
116 return {mMatrixNodeIndex[1]};
117 else if (mPhaseType == PhaseType::C)
118 return {mMatrixNodeIndex[2]};
119 else if (mPhaseType == PhaseType::ABC)
120 return mMatrixNodeIndex;
121 else // phaseType == PhaseType::Single || mPhaseType == PhaseType::A
122 return {mMatrixNodeIndex[0]};
123}
124
125template <typename VarType> MatrixVar<VarType> SimNode<VarType>::voltage() {
126 return **mVoltage;
127}
128
129template <typename VarType>
133
134template <typename VarType> const Task::List &SimNode<VarType>::mnaTasks() {
135 return mMnaTasks;
136}
137
138template <> void SimNode<Complex>::setVoltage(Complex newVoltage) {
139 (**mVoltage)(0, 0) = newVoltage;
140}
141
142template <> void SimNode<Complex>::setPower(Complex newPower) {
143 (**mApparentPower)(0, 0) = newPower;
144}
145
146template <> void SimNode<Real>::mnaUpdateVoltage(const Matrix &leftVector) {
147 if (mMatrixNodeIndex[0] >= 0)
148 (**mVoltage)(0, 0) =
149 Math::realFromVectorElement(leftVector, mMatrixNodeIndex[0]);
150 if (mPhaseType == PhaseType::ABC) {
151 if (mMatrixNodeIndex[1] >= 0)
152 (**mVoltage)(1, 0) =
153 Math::realFromVectorElement(leftVector, mMatrixNodeIndex[1]);
154 if (mMatrixNodeIndex[2] >= 0)
155 (**mVoltage)(2, 0) =
156 Math::realFromVectorElement(leftVector, mMatrixNodeIndex[2]);
157 }
158}
159
160template <> void SimNode<Complex>::mnaUpdateVoltage(const Matrix &leftVector) {
161 for (UInt freq = 0; freq < mNumFreqs; freq++) {
162 if (mMatrixNodeIndex[0] >= 0)
163 (**mVoltage)(0, freq) = Math::complexFromVectorElement(
164 leftVector, mMatrixNodeIndex[0], mNumFreqs, freq);
165 if (mPhaseType == PhaseType::ABC) {
166 if (mMatrixNodeIndex[1] >= 0)
167 (**mVoltage)(1, freq) = Math::complexFromVectorElement(
168 leftVector, mMatrixNodeIndex[1], mNumFreqs, freq);
169 if (mMatrixNodeIndex[2] >= 0)
170 (**mVoltage)(2, freq) = Math::complexFromVectorElement(
171 leftVector, mMatrixNodeIndex[2], mNumFreqs, freq);
172 }
173 }
174}
175
176template <>
177void SimNode<Real>::mnaUpdateVoltageHarm(const Matrix &leftVector,
178 Int freqIdx) {}
179
180template <>
182 Int freqIdx) {
183 if (mMatrixNodeIndex[0] >= 0)
184 (**mVoltage)(0, freqIdx) =
185 Math::complexFromVectorElement(leftVector, mMatrixNodeIndex[0]);
186 if (mPhaseType == PhaseType::ABC) {
187 if (mMatrixNodeIndex[1] >= 0)
188 (**mVoltage)(1, freqIdx) =
189 Math::complexFromVectorElement(leftVector, mMatrixNodeIndex[1]);
190 if (mMatrixNodeIndex[2] >= 0)
191 (**mVoltage)(2, freqIdx) =
192 Math::complexFromVectorElement(leftVector, mMatrixNodeIndex[2]);
193 }
194}
195
196template <>
198 std::vector<Attribute<Matrix>::Ptr> leftVectors) {}
199
200template <>
202 std::vector<Attribute<Matrix>::Ptr> leftVectors) {
203 mMnaTasks = {std::make_shared<MnaPostStepHarm>(*this, leftVectors)};
204}
205
206template <>
207void SimNode<Complex>::MnaPostStepHarm::execute(Real time, Int timeStepCount) {
208 for (UInt freq = 0; freq < mNode.mNumFreqs; freq++)
209 mNode.mnaUpdateVoltageHarm(**mLeftVectors[freq], freq);
210}
211
212template <>
213void SimNode<Real>::MnaPostStepHarm::execute(Real time, Int timeStepCount) {}
214
215template class CPS::SimNode<Real>;
216template class CPS::SimNode<Complex>;
AttributePointer< Attribute< T > > Ptr
Definition Attribute.h:250
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)
Definition MathUtils.cpp:94
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:84
void initialize()
Initialize mVoltage according to mInitialVoltage.
void setPower(VarType newPower)
Definition SimNode.h:86
Task::List mMnaTasks
Definition SimNode.h:28
std::shared_ptr< TopologicalNode > clone(String name)
Definition SimNode.cpp:67
const Task::List & mnaTasks()
Return list of MNA tasks.
Definition SimNode.cpp:134
UInt matrixNodeIndex(PhaseType phaseType=PhaseType::Single) override
Returns matrix index for specified phase.
Definition SimNode.cpp:84
MatrixVar< VarType > voltage()
Definition SimNode.cpp:125
void setMatrixNodeIndex(UInt phase, UInt matrixNodeIndex) override
Definition SimNode.cpp:130
const Attribute< MatrixVar< VarType > >::Ptr mVoltage
Definition SimNode.h:38
UInt mNumFreqs
Number of harmonics.
Definition SimNode.h:26
VarType singleVoltage(PhaseType phaseType=PhaseType::Single)
Definition SimNode.cpp:74
std::vector< UInt > matrixNodeIndices() override
Returns all matrix indices.
Definition SimNode.cpp:114
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:40
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:81
std::string String
Definition Definitions.h:65
double Real
Definition Definitions.h:62
int Int
Definition Definitions.h:61
Eigen::Matrix< VarType, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > MatrixVar
Definition Definitions.h:97
std::complex< Real > Complex
Definition Definitions.h:63
unsigned int UInt
Definition Definitions.h:60