DPsim
Loading...
Searching...
No Matches
EMT_Ph3_CurrentSource.cpp
Go to the documentation of this file.
1/* Copyright 2017-2020 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
14 Logger::Level logLevel)
15 : MNASimPowerComp<Real>(uid, name, true, true, logLevel),
16 mCurrentRef(mAttributes->create<MatrixComp>("I_ref")), // rms-value
17 mSrcFreq(mAttributes->createDynamic<Real>("f_src")),
18 mSigOut(mAttributes->createDynamic<Complex>("sigOut")) {
22 **mIntfVoltage = Matrix::Zero(3, 1);
23 **mIntfCurrent = Matrix::Zero(3, 1);
24}
27 copy->setParameters(attributeTyped<MatrixComp>("I_ref")->get(),
28 attributeTyped<Real>("f_src")->get());
29 return copy;
30}
31
33 Real srcFreq) {
34 auto srcSigSine = Signal::SineWaveGenerator::make(**mName + "_sw");
35 // Complex(1,0) is used as initialPhasor for signal generator as only phase is used
36 srcSigSine->setParameters(Complex(1, 0), srcFreq);
37 mSrcSig = srcSigSine;
38
39 **mCurrentRef = currentRef;
40 mSrcFreq->setReference(mSrcSig->mFreq);
41
42 SPDLOG_LOGGER_INFO(mSLog,
43 "\nCurrent reference phasor [I]: {:s}"
44 "\nFrequency [Hz]: {:s}",
46 Logger::realToString(srcFreq));
47
48 mParametersSet = true;
49}
50
52 SPDLOG_LOGGER_INFO(
53 mSLog, "\n--- Initialization from node voltages and terminal ---");
54 if (!mParametersSet) {
55 auto srcSigSine =
56 Signal::SineWaveGenerator::make(**mName + "_sw", Logger::Level::off);
57 // Complex(1,0) is used as initialPhasor for signal generator as only phase is used
58 srcSigSine->setParameters(Complex(1, 0), frequency);
59 mSrcSig = srcSigSine;
60
62 Complex s_ref = terminal(1)->singlePower() - terminal(0)->singlePower();
63
64 // Current flowing from T1 to T0 (rms value)
65 Complex i_ref = std::conj(s_ref / v_ref / sqrt(3.));
66
68 mSrcFreq->setReference(mSrcSig->attributeTyped<Real>("freq"));
69
70 SPDLOG_LOGGER_INFO(mSLog,
71 "\nReference current: {:s}"
72 "\nReference voltage: {:s}"
73 "\nReference power: {:s}"
74 "\nTerminal 0 voltage: {:s}"
75 "\nTerminal 1 voltage: {:s}"
76 "\nTerminal 0 power: {:s}"
77 "\nTerminal 1 power: {:s}",
83 Logger::complexToString(terminal(0)->singlePower()),
84 Logger::complexToString(terminal(1)->singlePower()));
85 } else {
86 SPDLOG_LOGGER_INFO(
87 mSLog,
88 "\nInitialization from node voltages and terminal omitted (parameter "
89 "already set)."
90 "\nReference voltage: {:s}",
92 }
93 SPDLOG_LOGGER_INFO(
94 mSLog, "\n--- Initialization from node voltages and terminal ---");
95 mSLog->flush();
96}
97
102
104 Matrix &rightVector) {
105 if (terminalNotGrounded(1)) {
106 Math::setVectorElement(rightVector, matrixNodeIndex(1, 0),
107 -(**mIntfCurrent)(0, 0));
108 Math::setVectorElement(rightVector, matrixNodeIndex(1, 1),
109 -(**mIntfCurrent)(1, 0));
110 Math::setVectorElement(rightVector, matrixNodeIndex(1, 2),
111 -(**mIntfCurrent)(2, 0));
112 }
113 if (terminalNotGrounded(0)) {
114 Math::setVectorElement(rightVector, matrixNodeIndex(0, 0),
115 (**mIntfCurrent)(0, 0));
116 Math::setVectorElement(rightVector, matrixNodeIndex(0, 1),
117 (**mIntfCurrent)(1, 0));
118 Math::setVectorElement(rightVector, matrixNodeIndex(0, 2),
119 (**mIntfCurrent)(2, 0));
120 }
121}
122
124 if (mSrcSig != nullptr) {
125 mSrcSig->step(time);
126 for (int i = 0; i < 3; i++) {
127 (**mIntfCurrent)(i, 0) = RMS_TO_PEAK * Math::abs((**mCurrentRef)(i, 0)) *
128 cos(Math::phase(mSrcSig->getSignal()) +
129 Math::phase((**mCurrentRef)(i, 0)));
130 }
131 } else {
132 **mIntfCurrent = RMS_TO_PEAK * (**mCurrentRef).real();
133 }
134 SPDLOG_LOGGER_DEBUG(mSLog, "\nUpdate current: {:s}",
136}
137
139 AttributeBase::List &prevStepDependencies,
140 AttributeBase::List &attributeDependencies,
141 AttributeBase::List &modifiedAttributes) {
142 attributeDependencies.push_back(mCurrentRef);
143 modifiedAttributes.push_back(mRightVector);
144 modifiedAttributes.push_back(mIntfVoltage);
145}
146
151
153 AttributeBase::List &prevStepDependencies,
154 AttributeBase::List &attributeDependencies,
155 AttributeBase::List &modifiedAttributes,
156 Attribute<Matrix>::Ptr &leftVector) {
157 attributeDependencies.push_back(leftVector);
158 modifiedAttributes.push_back(mIntfVoltage);
159};
160
162 Real time, Int timeStepCount, Attribute<Matrix>::Ptr &leftVector) {
163 mnaCompUpdateVoltage(**leftVector);
164}
165
167 // v1 - v0
168 **mIntfVoltage = Matrix::Zero(3, 1);
169 if (terminalNotGrounded(1)) {
170 (**mIntfVoltage)(0, 0) =
172 (**mIntfVoltage)(1, 0) =
174 (**mIntfVoltage)(2, 0) =
176 }
177 if (terminalNotGrounded(0)) {
178 (**mIntfVoltage)(0, 0) =
179 (**mIntfVoltage)(0, 0) -
181 (**mIntfVoltage)(1, 0) =
182 (**mIntfVoltage)(1, 0) -
184 (**mIntfVoltage)(2, 0) =
185 (**mIntfVoltage)(2, 0) -
187 }
188}
std::vector< Ptr > List
Definition Attribute.h:123
AttributePointer< Attribute< T > > Ptr
Definition Attribute.h:250
void mnaCompUpdateVoltage(const Matrix &leftVector) override
Returns voltage through the component.
CurrentSource(String uid, String name, Logger::Level logLevel=Logger::Level::off)
Defines UID, name and logging level.
const Attribute< Real >::Ptr mSrcFreq
const Attribute< MatrixComp >::Ptr mCurrentRef
const Attribute< Complex >::Ptr mSigOut
void setParameters(MatrixComp currentRef, Real srcFreq=50.0)
Setter for reference current.
void mnaCompApplyRightSideVectorStamp(Matrix &rightVector) override
Stamps right side (source) vector.
void initializeFromNodesAndTerminals(Real frequency) override
Initializes component from power flow data.
void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
Add MNA post step dependencies.
void mnaCompAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override
Add MNA pre step dependencies.
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 mnaCompInitialize(Real omega, Real timeStep, Attribute< Matrix >::Ptr leftVector) override
Initializes internal variables of the component.
void mnaCompPreStep(Real time, Int timeStepCount) override
MNA pre step operations.
void mnaCompPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
MNA post step operations.
const Attribute< String >::Ptr mName
Human readable name.
Attribute< T >::Ptr attributeTyped(const String &name) const
Return pointer to an attribute.
String uid()
Returns unique id.
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 complexToString(const Complex &num)
Definition Logger.cpp:63
static String matrixToString(const Matrix &mat)
Definition Logger.cpp:31
static String realToString(const Real &num)
Definition Logger.cpp:69
static String phasorToString(const Complex &num)
Definition Logger.cpp:57
MNASimPowerComp(String uid, String name, Bool hasPreStep, Bool hasPostStep, Logger::Level logLevel)
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)
Definition MathUtils.cpp:73
static Real phase(Complex value)
Definition MathUtils.cpp:23
static Real abs(Complex value)
Definition MathUtils.cpp:27
static MatrixComp singlePhaseVariableToThreePhase(Complex var_1ph)
To convert single phase complex variables (voltages, currents) to symmetrical three phase ones.
UInt matrixNodeIndex(UInt nodeIndex)
const Attribute< MatrixVar< Real > >::Ptr mIntfCurrent
SimTerminal< Real >::Ptr terminal(UInt index)
void setVirtualNodeNumber(UInt num)
const Attribute< MatrixVar< Real > >::Ptr mIntfVoltage
std::shared_ptr< SimPowerComp< VarType > > Ptr
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< CurrentSource > make(Args &&...args)
Definition PtrFactory.h:19
#define RMS_TO_PEAK
Definition Definitions.h:48
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
std::complex< Real > Complex
Definition Definitions.h:63
Eigen::Matrix< Complex, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > MatrixComp
Dense matrix for complex numbers.
Definition Definitions.h:84