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::Single)
43 (**mVoltage)(0, 0) = (RMS3PH_TO_PEAK1PH * (**mInitialVoltage)(0, 0)).real();
44 else
47
48template <> void SimNode<Complex>::initialize() {
49 (**mVoltage)(0, 0) = (**mInitialVoltage)(0, 0);
50 if (phaseType() == PhaseType::ABC) {
51 (**mVoltage)(1, 0) = (**mInitialVoltage)(1, 0);
52 (**mVoltage)(2, 0) = (**mInitialVoltage)(2, 0);
53 }
54}
55
56template <typename VarType>
58 mFrequencies = frequencies;
59 mNumFreqs = static_cast<UInt>(mFrequencies.size());
60 Matrix::Index rowNum = phaseType() == PhaseType::ABC ? 3 : 1;
62}
63
64template <typename VarType>
65std::shared_ptr<TopologicalNode> SimNode<VarType>::clone(String name) {
66 auto nodeCpy = SimNode<VarType>::make(name, phaseType());
67 nodeCpy->setInitialVoltage(initialVoltage());
68 return nodeCpy;
69}
70
71template <typename VarType>
74 return (**mVoltage)(1, 0);
75 else if (phaseType == PhaseType::C)
76 return (**mVoltage)(2, 0);
77 else // phaseType == PhaseType::Single || mPhaseType == PhaseType::A
78 return (**mVoltage)(0, 0);
79}
81template <typename VarType>
97template <typename VarType>
100 return {mMatrixNodeIndex[1]};
101 else if (mPhaseType == PhaseType::C)
102 return {mMatrixNodeIndex[2]};
103 else if (mPhaseType == PhaseType::ABC)
104 return mMatrixNodeIndex;
105 else // phaseType == PhaseType::Single || mPhaseType == PhaseType::A
106 return {mMatrixNodeIndex[0]};
107}
108
109template <typename VarType> MatrixVar<VarType> SimNode<VarType>::voltage() {
110 return **mVoltage;
111}
112
113template <typename VarType>
117
118template <typename VarType> const Task::List &SimNode<VarType>::mnaTasks() {
119 return mMnaTasks;
120}
121
122template <> void SimNode<Complex>::setVoltage(Complex newVoltage) {
123 (**mVoltage)(0, 0) = newVoltage;
124}
125
126template <> void SimNode<Complex>::setPower(Complex newPower) {
127 (**mApparentPower)(0, 0) = newPower;
128}
129
130template <> void SimNode<Real>::mnaUpdateVoltage(const Matrix &leftVector) {
131 if (mMatrixNodeIndex[0] >= 0)
132 (**mVoltage)(0, 0) =
133 Math::realFromVectorElement(leftVector, mMatrixNodeIndex[0]);
134 if (mPhaseType == PhaseType::ABC) {
135 if (mMatrixNodeIndex[1] >= 0)
136 (**mVoltage)(1, 0) =
137 Math::realFromVectorElement(leftVector, mMatrixNodeIndex[1]);
138 if (mMatrixNodeIndex[2] >= 0)
139 (**mVoltage)(2, 0) =
140 Math::realFromVectorElement(leftVector, mMatrixNodeIndex[2]);
141 }
142}
143
144template <> void SimNode<Complex>::mnaUpdateVoltage(const Matrix &leftVector) {
145 for (UInt freq = 0; freq < mNumFreqs; freq++) {
146 if (mMatrixNodeIndex[0] >= 0)
147 (**mVoltage)(0, freq) = Math::complexFromVectorElement(
148 leftVector, mMatrixNodeIndex[0], mNumFreqs, freq);
149 if (mPhaseType == PhaseType::ABC) {
150 if (mMatrixNodeIndex[1] >= 0)
151 (**mVoltage)(1, freq) = Math::complexFromVectorElement(
152 leftVector, mMatrixNodeIndex[1], mNumFreqs, freq);
153 if (mMatrixNodeIndex[2] >= 0)
154 (**mVoltage)(2, freq) = Math::complexFromVectorElement(
155 leftVector, mMatrixNodeIndex[2], mNumFreqs, freq);
156 }
157 }
158}
159
160template <>
161void SimNode<Real>::mnaUpdateVoltageHarm(const Matrix &leftVector,
162 Int freqIdx) {}
163
164template <>
166 Int freqIdx) {
167 if (mMatrixNodeIndex[0] >= 0)
168 (**mVoltage)(0, freqIdx) =
169 Math::complexFromVectorElement(leftVector, mMatrixNodeIndex[0]);
170 if (mPhaseType == PhaseType::ABC) {
171 if (mMatrixNodeIndex[1] >= 0)
172 (**mVoltage)(1, freqIdx) =
173 Math::complexFromVectorElement(leftVector, mMatrixNodeIndex[1]);
174 if (mMatrixNodeIndex[2] >= 0)
175 (**mVoltage)(2, freqIdx) =
176 Math::complexFromVectorElement(leftVector, mMatrixNodeIndex[2]);
177 }
178}
179
180template <>
182 std::vector<Attribute<Matrix>::Ptr> leftVectors) {}
183
184template <>
186 std::vector<Attribute<Matrix>::Ptr> leftVectors) {
187 mMnaTasks = {std::make_shared<MnaPostStepHarm>(*this, leftVectors)};
188}
189
190template <>
191void SimNode<Complex>::MnaPostStepHarm::execute(Real time, Int timeStepCount) {
192 for (UInt freq = 0; freq < mNode.mNumFreqs; freq++)
193 mNode.mnaUpdateVoltageHarm(**mLeftVectors[freq], freq);
194}
195
196template <>
197void SimNode<Real>::MnaPostStepHarm::execute(Real time, Int timeStepCount) {}
198
199template class CPS::SimNode<Real>;
200template 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:65
const Task::List & mnaTasks()
Return list of MNA tasks.
Definition SimNode.cpp:118
UInt matrixNodeIndex(PhaseType phaseType=PhaseType::Single) override
Returns matrix index for specified phase.
Definition SimNode.cpp:82
MatrixVar< VarType > voltage()
Definition SimNode.cpp:109
void setMatrixNodeIndex(UInt phase, UInt matrixNodeIndex) override
Definition SimNode.cpp:114
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:72
std::vector< UInt > matrixNodeIndices() override
Returns all matrix indices.
Definition SimNode.cpp:98
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