DPsim
Loading...
Searching...
No Matches
EMT_Ph3_CurrentSource.cpp
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
9#include <dpsim-models/EMT/EMT_Ph3_CurrentSource.h>
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")) {
19 mPhaseType = PhaseType::ABC;
20 setVirtualNodeNumber(0);
21 setTerminalNumber(2);
22 **mIntfVoltage = Matrix::Zero(3, 1);
23 **mIntfCurrent = Matrix::Zero(3, 1);
24}
25SimPowerComp<Real>::Ptr EMT::Ph3::CurrentSource::clone(String name) {
26 auto copy = CurrentSource::make(name, mLogLevel);
27 copy->setParameters(attributeTyped<MatrixComp>("I_ref")->get(),
28 attributeTyped<Real>("f_src")->get());
29 return copy;
30}
31
32void EMT::Ph3::CurrentSource::setParameters(MatrixComp currentRef,
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}",
45 Logger::matrixCompToString(currentRef),
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
61 Complex v_ref = initialSingleVoltage(1) - initialSingleVoltage(0);
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
67 **mCurrentRef = CPS::Math::singlePhaseVariableToThreePhase(i_ref);
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}",
78 Logger::phasorToString(i_ref),
79 Logger::phasorToString(v_ref),
80 Logger::complexToString(s_ref),
81 Logger::phasorToString(initialSingleVoltage(0)),
82 Logger::phasorToString(initialSingleVoltage(1)),
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}",
91 Logger::matrixCompToString(attributeTyped<MatrixComp>("I_ref")->get()));
92 }
93 SPDLOG_LOGGER_INFO(
94 mSLog, "\n--- Initialization from node voltages and terminal ---");
95 mSLog->flush();
96}
97
99 Real omega, Real timeStep, Attribute<Matrix>::Ptr leftVector) {
101}
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
123void EMT::Ph3::CurrentSource::updateCurrent(Real time) {
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}",
135 Logger::matrixToString(**mIntfCurrent));
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
147void EMT::Ph3::CurrentSource::mnaCompPreStep(Real time, Int timeStepCount) {
148 updateCurrent(time);
150}
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
166void EMT::Ph3::CurrentSource::mnaCompUpdateVoltage(const Matrix &leftVector) {
167 // v1 - v0
168 **mIntfVoltage = Matrix::Zero(3, 1);
169 if (terminalNotGrounded(1)) {
170 (**mIntfVoltage)(0, 0) =
171 Math::realFromVectorElement(leftVector, matrixNodeIndex(1, 0));
172 (**mIntfVoltage)(1, 0) =
173 Math::realFromVectorElement(leftVector, matrixNodeIndex(1, 1));
174 (**mIntfVoltage)(2, 0) =
175 Math::realFromVectorElement(leftVector, matrixNodeIndex(1, 2));
176 }
177 if (terminalNotGrounded(0)) {
178 (**mIntfVoltage)(0, 0) =
179 (**mIntfVoltage)(0, 0) -
180 Math::realFromVectorElement(leftVector, matrixNodeIndex(0, 0));
181 (**mIntfVoltage)(1, 0) =
182 (**mIntfVoltage)(1, 0) -
183 Math::realFromVectorElement(leftVector, matrixNodeIndex(0, 1));
184 (**mIntfVoltage)(2, 0) =
185 (**mIntfVoltage)(2, 0) -
186 Math::realFromVectorElement(leftVector, matrixNodeIndex(0, 2));
187 }
188}
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.
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.
MNASimPowerComp(String uid, String name, Bool hasPreStep, Bool hasPostStep, Logger::Level logLevel)
Attribute< Matrix >::Ptr mRightVector
static MatrixComp singlePhaseVariableToThreePhase(Complex var_1ph)
To convert single phase complex variables (voltages, currents) to symmetrical three phase ones.
const Attribute< MatrixVar< Real > >::Ptr mIntfCurrent
SimTerminal< Real >::Ptr terminal(UInt index)
const Attribute< MatrixVar< Real > >::Ptr mIntfVoltage
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.