DPsim
Loading...
Searching...
No Matches
EMT_Ph3_SynchronGeneratorIdeal.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
17
19 String uid, String name, Bool useSubcomponents, Logger::Level logLevel,
20 CPS::GeneratorType sourceType)
21 : CompositePowerComp<Real>(uid, name, true, true, logLevel),
22 mRefVoltage(mAttributes->createDynamic<MatrixComp>("V_ref")),
23 mRefCurrent(mAttributes->createDynamic<MatrixComp>("I_ref")),
24 mSrcFreq(mAttributes->createDynamic<Real>("f_src")) {
26 mSourceType = sourceType;
27 mUseSubcomponents = useSubcomponents;
28
31 else
33
35 **mIntfVoltage = Matrix::Zero(3, 1);
36 **mIntfCurrent = Matrix::Zero(3, 1);
37}
38
42
47
50 return;
51 mSubCompCreated = true;
52
53 if (!mUseSubcomponents)
54 return;
55
56 if (mSourceType == CPS::GeneratorType::IdealVoltageSource) {
57 mSubVoltageSource =
59 addMNASubComponent(mSubVoltageSource,
62 } else {
63 mSubCurrentSource =
65 addMNASubComponent(mSubCurrentSource,
68 }
69
70 mSubComponents[0]->connect({SimNode::GND, node(0)});
71
73 mSubComponents[0]->setVirtualNodeAt(mVirtualNodes[0], 0);
74
76 mSubComponents[0]->setTerminalAt(terminal(0), 1);
77
79 mRefVoltage->setReference(
82 mRefCurrent->setReference(
84 mSrcFreq->setReference(mSubComponents[0]->attributeTyped<Real>("f_src"));
85}
86
88 Real frequency) {
89 if (!mUseSubcomponents) {
90 **mSrcFreq = frequency;
91 if (mSourceType == CPS::GeneratorType::IdealVoltageSource) {
92 **mRefVoltage =
94 } else {
95 const Complex voltage = initialSingleVoltage(0);
96 const Complex power = terminal(0)->singlePower();
97 const Complex current = std::abs(voltage) > 0.
98 ? std::conj(power / voltage / sqrt(3.))
99 : Complex(0., 0.);
101 }
102 }
103 // The voltage-source representation has its internal source node behind
104 // the ideal source. Seed that node from the solved terminal voltage so the
105 // child VoltageSource can derive its reference phasor automatically.
106 // Without this, the virtual node stays at zero and the child source starts
107 // with a zero-voltage reference even though initWithPowerflow() populated
108 // the external terminal.
109 if (mSourceType == CPS::GeneratorType::IdealVoltageSource &&
110 !mVirtualNodes.empty() && mVirtualNodes[0]) {
111 mVirtualNodes[0]->setInitialVoltage(initialSingleVoltage(0));
112 }
113
114 SPDLOG_LOGGER_INFO(mSLog,
115 "\n--- Initialization from powerflow ---"
116 "\nTerminal 0 voltage: {:s}"
117 "\nTerminal 0 power: {:s}"
118 "\n--- Initialization from powerflow finished ---",
120 Logger::complexToString(terminal(0)->singlePower()));
121}
122
124 AttributeBase::List &prevStepDependencies,
125 AttributeBase::List &attributeDependencies,
126 AttributeBase::List &modifiedAttributes) {
127 if (!mUseSubcomponents) {
129 attributeDependencies.push_back(mRefVoltage);
130 else
131 attributeDependencies.push_back(mRefCurrent);
132 attributeDependencies.push_back(mSrcFreq);
133 modifiedAttributes.push_back(mIntfVoltage);
134 modifiedAttributes.push_back(mIntfCurrent);
135 modifiedAttributes.push_back(mRightVector);
136 return;
137 }
138 prevStepDependencies.push_back(mIntfCurrent);
139 prevStepDependencies.push_back(mIntfVoltage);
140 modifiedAttributes.push_back(mRightVector);
141}
142
144 Int timeStepCount) {
145 if (!mUseSubcomponents) {
146 if (mSourceType == CPS::GeneratorType::IdealVoltageSource) {
147 for (Int phase = 0; phase < 3; ++phase) {
148 const Complex reference = (**mRefVoltage)(phase, 0);
149 (**mIntfVoltage)(phase, 0) =
150 RMS3PH_TO_PEAK1PH * Math::abs(reference) *
151 std::cos(2. * PI * **mSrcFreq * time + Math::phase(reference));
152 }
153 } else {
154 for (Int phase = 0; phase < 3; ++phase) {
155 const Complex reference = (**mRefCurrent)(phase, 0);
156 (**mIntfCurrent)(phase, 0) =
157 RMS_TO_PEAK * Math::abs(reference) *
158 std::cos(2. * PI * **mSrcFreq * time + Math::phase(reference));
159 }
160 }
161 }
163}
164
166 SparseMatrixRow &systemMatrix) {
167 if (mUseSubcomponents ||
170 return;
171 for (Int phase = 0; phase < 3; ++phase) {
172 const UInt nodeIndex = matrixNodeIndex(0, phase);
173 const UInt sourceIndex =
174 mVirtualNodes[0]->matrixNodeIndex(static_cast<PhaseType>(phase));
175 Math::addToMatrixElement(systemMatrix, nodeIndex, sourceIndex, 1.);
176 Math::addToMatrixElement(systemMatrix, sourceIndex, nodeIndex, 1.);
177 }
178}
179
181 Matrix &rightVector) {
182 if (mUseSubcomponents)
183 return;
184 if (mSourceType == CPS::GeneratorType::IdealVoltageSource) {
185 for (Int phase = 0; phase < 3; ++phase)
187 rightVector,
188 mVirtualNodes[0]->matrixNodeIndex(static_cast<PhaseType>(phase)),
189 (**mIntfVoltage)(phase, 0));
190 } else if (terminalNotGrounded(0)) {
191 for (Int phase = 0; phase < 3; ++phase)
192 Math::setVectorElement(rightVector, matrixNodeIndex(0, phase),
193 -(**mIntfCurrent)(phase, 0));
194 }
195}
196
198 AttributeBase::List &prevStepDependencies,
199 AttributeBase::List &attributeDependencies,
200 AttributeBase::List &modifiedAttributes,
201 Attribute<Matrix>::Ptr &leftVector) {
202 attributeDependencies.push_back(leftVector);
203 modifiedAttributes.push_back(mIntfVoltage);
204 modifiedAttributes.push_back(mIntfCurrent);
205}
206
208 Real time, Int timeStepCount, Attribute<Matrix>::Ptr &leftVector) {
209 mnaCompUpdateCurrent(**leftVector);
210 mnaCompUpdateVoltage(**leftVector);
211}
212
214 const Matrix &leftvector) {
215 if (mUseSubcomponents) {
216 **mIntfCurrent = **mSubComponents[0]->mIntfCurrent;
217 return;
218 }
220 for (Int phase = 0; phase < 3; ++phase)
222 leftvector,
223 mVirtualNodes[0]->matrixNodeIndex(static_cast<PhaseType>(phase)));
224}
225
227 const Matrix &leftVector) {
228 if (mUseSubcomponents) {
229 **mIntfVoltage = **mSubComponents[0]->mIntfVoltage;
230 return;
231 }
232 (**mIntfVoltage).setZero();
233 if (terminalNotGrounded(0))
234 for (Int phase = 0; phase < 3; ++phase)
235 (**mIntfVoltage)(phase, 0) =
236 Math::realFromVectorElement(leftVector, matrixNodeIndex(0, phase));
237}
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)
void mnaCompApplyRightSideVectorStamp(Matrix &rightVector) override
CompositePowerComp(String uid, String name, Bool hasPreStep, Bool hasPostStep, Logger::Level logLevel)
void mnaCompUpdateCurrent(const Matrix &leftVector) override
Updates current through the component.
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 mnaParentAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
Add MNA post step dependencies.
void mnaParentAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override
Add MNA pre step dependencies.
void mnaParentPreStep(Real time, Int timeStepCount) override
MNA pre step operations.
const Attribute< MatrixComp >::Ptr mRefVoltage
CHECK: Is this actually necessary? It is never read from within the component's code.
const Attribute< MatrixComp >::Ptr mRefCurrent
void mnaParentApplyRightSideVectorStamp(Matrix &rightVector) override
void mnaParentApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) override
SynchronGeneratorIdeal(String uid, String name, Logger::Level logLevel=Logger::Level::off, CPS::GeneratorType sourceType=CPS::GeneratorType::IdealVoltageSource)
Defines UID, name, component parameters and logging level.
void createSubComponents() override
Constructs and registers MNA subcomponents; idempotent.
void mnaCompUpdateVoltage(const Matrix &leftVector) override
Updates voltage across component.
void mnaParentPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
MNA post step operations.
void initializeParentFromNodesAndTerminals(Real frequency) override
Initializes component from power flow data.
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 complexToString(const Complex &num)
Definition Logger.cpp:63
static String phasorToString(const Complex &num)
Definition Logger.cpp:57
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 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)
SimTerminal< Real >::Ptr terminal(UInt index)
void setVirtualNodeNumber(UInt num)
const Attribute< MatrixVar< Real > >::Ptr mIntfVoltage
std::shared_ptr< SimPowerComp< VarType > > Ptr
SimNode< Real >::List mVirtualNodes
std::vector< std::shared_ptr< SimPowerComp< Real > > > mSubComponents
Bool terminalNotGrounded(UInt index)
Complex initialSingleVoltage(UInt index)
void setTerminalNumber(UInt num)
Logger::Level mLogLevel
Component logger control for internal variables.
Logger::Log mSLog
Component logger.
static std::shared_ptr< SynchronGeneratorIdeal > make(Args &&...args)
Definition PtrFactory.h:19
#define PI
Definition Definitions.h:43
#define RMS3PH_TO_PEAK1PH
Definition Definitions.h:50
#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: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
GeneratorType
unsigned int UInt
Definition Definitions.h:58
Eigen::SparseMatrix< Real, Eigen::RowMajor > SparseMatrixRow
Sparse matrix for real numbers (row major).
Definition Definitions.h:72