DPsim
Loading...
Searching...
No Matches
EMT_Ph3_NetworkInjection.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
13
14using namespace CPS;
15
19
21 Bool useSubcomponents,
22 Logger::Level logLevel)
23 : CompositePowerComp<Real>(uid, name, true, true, logLevel),
24 mVoltageRef(mAttributes->createDynamic<MatrixComp>("V_ref")),
25 mSrcFreq(mAttributes->createDynamic<Real>("f_src")) {
26 mUseSubcomponents = useSubcomponents;
28 setVirtualNodeNumber(mUseSubcomponents ? 0 : 1);
30 **mIntfVoltage = Matrix::Zero(3, 1);
31 **mIntfCurrent = Matrix::Zero(3, 1);
32
33 SPDLOG_LOGGER_INFO(mSLog, "Create {} {}", this->type(), name);
34
35 // Create electrical sub components
36 if (mUseSubcomponents) {
37 mSubVoltageSource =
38 std::make_shared<EMT::Ph3::VoltageSource>(**mName + "_vs", mLogLevel);
39 addMNASubComponent(mSubVoltageSource,
42 mSubVoltageSource->mVoltageRef->setReference(mVoltageRef);
43 mSubVoltageSource->mSrcFreq->setReference(mSrcFreq);
44 }
45}
46
48 auto copy = NetworkInjection::make(name, mUseSubcomponents, mLogLevel);
49 copy->setParameters(**mVoltageRef);
50 return copy;
51}
52
54 Real srcFreq) {
55 mParametersSet = true;
56
57 if (mUseSubcomponents)
58 mSubVoltageSource->setParameters(voltageRef, srcFreq);
59 else {
60 auto source = Signal::SineWaveGenerator::make(**mName + "_sw");
61 source->mFreq->setReference(mSrcFreq);
62 source->setParameters(Complex(1, 0), srcFreq);
63 mSrcSig = source;
64 **mVoltageRef = voltageRef;
65 **mSrcFreq = srcFreq;
66 }
67
68 SPDLOG_LOGGER_INFO(mSLog,
69 "\nVoltage Ref={:s} [V]"
70 "\nFrequency={:s} [Hz]",
72 Logger::realToString(srcFreq));
73}
74
76 Real freqStart, Real rocof,
77 Real timeStart, Real duration,
78 bool smoothRamp) {
79 mParametersSet = true;
80
81 if (mUseSubcomponents)
82 mSubVoltageSource->setParameters(voltageRef, freqStart, rocof, timeStart,
83 duration, smoothRamp);
84 else {
85 auto source = Signal::FrequencyRampGenerator::make(**mName + "_fr");
86 source->mFreq->setReference(mSrcFreq);
87 source->setParameters(Complex(1, 0), freqStart, rocof, timeStart, duration,
88 smoothRamp);
89 mSrcSig = source;
90 **mVoltageRef = voltageRef;
91 **mSrcFreq = freqStart;
92 }
93
94 SPDLOG_LOGGER_INFO(
95 mSLog,
96 "\nVoltage Ref={:s} [V]"
97 "\nInitial frequency={:s} [Hz]"
98 "\nRamp ROCOF={:s} [Hz/s]"
99 "\nRamp duration={:s} [s]"
100 "\nRamp nadir={:s} [Hz]",
101 Logger::matrixCompToString(voltageRef), Logger::realToString(freqStart),
103 Logger::realToString(freqStart + rocof * duration));
104}
105
107 Real modulationFrequency,
108 Real modulationAmplitude,
109 Real baseFrequency /*= 0.0*/,
110 bool zigzag /*= false*/) {
111 mParametersSet = true;
112
113 if (mUseSubcomponents)
114 mSubVoltageSource->setParameters(voltageRef, modulationFrequency,
115 modulationAmplitude, baseFrequency,
116 zigzag);
117 else {
118 auto source = Signal::CosineFMGenerator::make(**mName + "_fm");
119 source->mFreq->setReference(mSrcFreq);
120 source->setParameters(Complex(1, 0), modulationFrequency,
121 modulationAmplitude, baseFrequency, zigzag);
122 mSrcSig = source;
123 **mVoltageRef = voltageRef;
124 **mSrcFreq = baseFrequency;
125 }
126
127 SPDLOG_LOGGER_INFO(mSLog,
128 "\nVoltage Ref={:s} [V]"
129 "\nFrequency={:s} [Hz]",
130 Logger::matrixCompToString(voltageRef),
131 Logger::realToString(baseFrequency));
132}
133
135 Real frequency) {
136 if (!mUseSubcomponents) {
137 if (!mParametersSet) {
138 auto source = Signal::SineWaveGenerator::make(**mName + "_sw");
139 source->mFreq->setReference(mSrcFreq);
140 source->setParameters(Complex(1, 0), frequency);
141 mSrcSig = source;
144 **mSrcFreq = frequency;
145 }
146 return;
147 }
148 // Connect electrical subcomponents
149 mSubVoltageSource->connect({SimNode::GND, node(0)});
150}
151
152// #### MNA functions ####
154 Matrix &rightVector) {
155 if (!mUseSubcomponents) {
156 for (UInt phase = 0; phase < 3; ++phase)
158 rightVector,
159 mVirtualNodes[0]->matrixNodeIndex(static_cast<PhaseType>(phase)),
160 (**mIntfVoltage)(phase, 0));
161 return;
162 }
163 SPDLOG_LOGGER_DEBUG(mSLog, "Right Side Vector: {:s}",
164 Logger::matrixToString(rightVector));
165}
166
168 SparseMatrixRow &systemMatrix) {
169 if (mUseSubcomponents)
170 return;
171 for (UInt phase = 0; phase < 3; ++phase) {
172 const UInt sourceCurrent =
173 mVirtualNodes[0]->matrixNodeIndex(static_cast<PhaseType>(phase));
174 if (terminalNotGrounded(0)) {
175 Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, phase),
176 sourceCurrent, 1.);
177 Math::addToMatrixElement(systemMatrix, sourceCurrent,
178 matrixNodeIndex(0, phase), 1.);
179 }
180 }
181}
182
187
189 Matrix &rightVector) {
190 if (mUseSubcomponents) {
192 return;
193 }
194 for (UInt phase = 0; phase < 3; ++phase) {
195 const UInt index =
196 mVirtualNodes[0]->matrixNodeIndex(static_cast<PhaseType>(phase));
198 rightVector, index, Math::realFromVectorElement(**mRightVector, index));
199 }
200}
201
203 AttributeBase::List &prevStepDependencies,
204 AttributeBase::List &attributeDependencies,
205 AttributeBase::List &modifiedAttributes) {
206 prevStepDependencies.push_back(mIntfCurrent);
207 prevStepDependencies.push_back(mIntfVoltage);
208 if (!mUseSubcomponents) {
209 attributeDependencies.push_back(mVoltageRef);
210 attributeDependencies.push_back(mSrcFreq);
211 }
212 modifiedAttributes.push_back(mRightVector);
213}
214
216 Int timeStepCount) {
217 if (!mUseSubcomponents) {
218 if (mSrcSig != nullptr) {
219 mSrcSig->step(time);
220 for (UInt phase = 0; phase < 3; ++phase)
221 (**mIntfVoltage)(phase, 0) =
222 RMS3PH_TO_PEAK1PH * Math::abs((**mVoltageRef)(phase, 0)) *
223 cos(Math::phase(mSrcSig->getSignal()) +
224 Math::phase((**mVoltageRef)(phase, 0)));
225 } else
226 **mIntfVoltage = RMS3PH_TO_PEAK1PH * (**mVoltageRef).real();
227 }
229}
230
232 AttributeBase::List &prevStepDependencies,
233 AttributeBase::List &attributeDependencies,
234 AttributeBase::List &modifiedAttributes,
235 Attribute<Matrix>::Ptr &leftVector) {
236 attributeDependencies.push_back(leftVector);
237 modifiedAttributes.push_back(mIntfVoltage);
238 modifiedAttributes.push_back(mIntfCurrent);
239}
240
242 Real time, Int timeStepCount, Attribute<Matrix>::Ptr &leftVector) {
243 mnaCompUpdateCurrent(**leftVector);
244 mnaCompUpdateVoltage(**leftVector);
245}
246
248 const Matrix &leftVector) {
249 if (mUseSubcomponents)
250 **mIntfVoltage = **mSubVoltageSource->mIntfVoltage;
251}
252
254 const Matrix &leftVector) {
255 if (mUseSubcomponents)
256 **mIntfCurrent = **mSubVoltageSource->mIntfCurrent;
257 else
258 for (UInt phase = 0; phase < 3; ++phase)
260 leftVector,
261 mVirtualNodes[0]->matrixNodeIndex(static_cast<PhaseType>(phase)));
262}
std::vector< Ptr > List
Definition Attribute.h:123
AttributePointer< Attribute< T > > Ptr
Definition Attribute.h:249
void addMNASubComponent(typename SimPowerComp< Real >::Ptr subc, MNA_SUBCOMP_TASK_ORDER preStepOrder, MNA_SUBCOMP_TASK_ORDER postStepOrder, Bool contributeToRightVector)
Bool mnaHasCompactRightSideVectorStamp() const override
Composite stamps touch only their cached terminal and virtual-node rows.
void mnaAddCompactRightSideVectorStamp(Matrix &rightVector) override
void mnaCompApplyRightSideVectorStamp(Matrix &rightVector) override
CompositePowerComp(String uid, String name, Bool hasPreStep, Bool hasPostStep, Logger::Level logLevel)
void mnaParentApplyRightSideVectorStamp(Matrix &rightVector) override
Stamps right side (source) vector.
void setParameters(MatrixComp voltageRef, Real srcFreq=50.0)
Setter for reference voltage parameters.
void initializeParentFromNodesAndTerminals(Real frequency) override
Initializes component from power flow data.
void mnaCompUpdateVoltage(const Matrix &leftVector) override
Updates voltage across component.
void mnaParentAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
Add MNA post step dependencies.
void mnaCompUpdateCurrent(const Matrix &leftVector) override
Returns current through the component.
void mnaParentPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
MNA post step operations.
Bool mnaHasCompactRightSideVectorStamp() const override
Composite stamps touch only their cached terminal and virtual-node rows.
void mnaAddCompactRightSideVectorStamp(Matrix &rightVector) override
SimPowerComp< Real >::Ptr clone(String name) override
Returns a modified copy of the component with the given suffix added to the name and without.
void mnaParentPreStep(Real time, Int timeStepCount) override
MNA pre step operations.
const CPS::Attribute< MatrixComp >::Ptr mVoltageRef
const CPS::Attribute< Real >::Ptr mSrcFreq
void mnaParentAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override
Add MNA pre step dependencies.
NetworkInjection(String uid, String name, Logger::Level loglevel=Logger::Level::off)
Defines UID, name, component parameters and logging level.
void mnaParentApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) override
const Attribute< String >::Ptr mName
Human readable name.
String uid()
Returns unique id.
String type()
Get component type (cross-platform)
AttributeList::Ptr mAttributes
Attribute List.
spdlog::level::level_enum Level
Definition Logger.h:33
static String matrixCompToString(const MatrixComp &mat)
Definition Logger.cpp:37
static String matrixToString(const Matrix &mat)
Definition Logger.cpp:31
static String realToString(const Real &num)
Definition Logger.cpp:69
Attribute< Matrix >::Ptr mRightVector
static Real realFromVectorElement(const Matrix &mat, Matrix::Index row)
static void setVectorElement(Matrix &mat, Matrix::Index row, Complex value, Int maxFreq=1, Int freqIdx=0, Matrix::Index colOffset=0)
static void addToVectorElement(Matrix &mat, Matrix::Index row, Complex value, Int maxFreq=1, Int freqIdx=0)
static Real phase(Complex value)
Definition MathUtils.cpp:67
static Real abs(Complex value)
Definition MathUtils.cpp:71
static void addToMatrixElement(SparseMatrixRow &mat, Matrix::Index row, Matrix::Index column, Complex value, Int maxFreq=1, Int freqIdx=0)
static MatrixComp singlePhaseVariableToThreePhase(Complex var_1ph)
To convert single phase complex variables (voltages, currents) to symmetrical three phase ones.
static Ptr GND
Definition SimNode.h:37
UInt matrixNodeIndex(UInt nodeIndex)
const Attribute< MatrixVar< Real > >::Ptr mIntfCurrent
SimNode< Real >::Ptr node(UInt index)
void setVirtualNodeNumber(UInt num)
const Attribute< MatrixVar< Real > >::Ptr mIntfVoltage
std::shared_ptr< SimPowerComp< VarType > > Ptr
SimNode< Real >::List mVirtualNodes
Bool terminalNotGrounded(UInt index)
Complex initialSingleVoltage(UInt index)
void setTerminalNumber(UInt num)
Logger::Level mLogLevel
Component logger control for internal variables.
bool mParametersSet
Flag indicating that parameters are set via setParameters() function.
Logger::Log mSLog
Component logger.
static std::shared_ptr< NetworkInjection > 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
std::complex< Real > Complex
Definition Definitions.h:61
Eigen::Matrix< Complex, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > MatrixComp
Dense matrix for complex numbers.
Definition Definitions.h:82
bool Bool
Definition Definitions.h:62
unsigned int UInt
Definition Definitions.h:58
Eigen::SparseMatrix< Real, Eigen::RowMajor > SparseMatrixRow
Sparse matrix for real numbers (row major).
Definition Definitions.h:72