DPsim
Loading...
Searching...
No Matches
DP_Ph1_PQLoadCS.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
15
17 Logger::Level logLevel)
18 : CompositePowerComp<Complex>(uid, name, true, true, logLevel),
19 mUseSubcomponents(useSubcomponents),
20 mActivePower(mAttributes->create<Real>("P", 0)),
21 mReactivePower(mAttributes->create<Real>("Q", 0)),
22 mNomVoltage(mAttributes->create<Real>("V_nom")) {
24 **mIntfVoltage = MatrixComp::Zero(1, 1);
25 **mIntfCurrent = MatrixComp::Zero(1, 1);
26}
27
29 Real reactivePower, Real nomVolt,
30 Logger::Level logLevel)
31 : PQLoadCS(uid, name, logLevel) {
32
33 setParameters(activePower, reactivePower, nomVolt);
34}
35
38
39DP::Ph1::PQLoadCS::PQLoadCS(String name, Real activePower, Real reactivePower,
40 Real nomVolt, Logger::Level logLevel)
41 : PQLoadCS(name, name, activePower, reactivePower, nomVolt, logLevel) {}
42
43void DP::Ph1::PQLoadCS::setParameters(Real activePower, Real reactivePower,
44 Real nomVolt) {
45 **mActivePower = activePower;
46 **mReactivePower = reactivePower;
47 **mNomVoltage = nomVolt;
48 mParametersSet = true;
49}
50
57
60 return;
62 return;
63 mSubCompCreated = true;
64
66 std::make_shared<DP::Ph1::CurrentSource>(**mName + "_cs", mLogLevel);
67 // A positive power should result in a positive current to ground.
68 mSubCurrentSource->connect({mTerminals[0]->node(), SimNode::GND});
72}
73
75 // Read power from terminals here, not in createSubComponents(): power-flow results aren't available that early.
76 if (**mActivePower == 0 && **mReactivePower == 0 && !mParametersSet) {
77 **mActivePower = mTerminals[0]->singleActivePower();
78 **mReactivePower = mTerminals[0]->singleReactivePower();
80 }
82 Complex current =
83 (**mNomVoltage != 0) ? std::conj(power / **mNomVoltage) : Complex(0);
84 mDirectCurrentRef = current;
86 mSubCurrentSource->setParameters(current);
87 mSubCurrentSource->initializeFromNodesAndTerminals(frequency);
89 } else {
90 (**mIntfVoltage)(0, 0) = initialSingleVoltage(0);
91 (**mIntfCurrent)(0, 0) = mDirectCurrentRef;
92 }
93
94 SPDLOG_LOGGER_INFO(mSLog,
95 "\n--- Initialization from powerflow ---"
96 "\nVoltage across: {:s}"
97 "\nCurrent: {:s}"
98 "\nTerminal 0 voltage: {:s}"
99 "\nCurrent set point: {:s}"
100 "\n--- Initialization from powerflow finished ---",
104 Logger::phasorToString(current));
105}
106
112
114 SparseMatrixRow &systemMatrix) {
115 (void)systemMatrix;
116}
117
124
126 // Calculate new current set point.
127 Complex power = {**mActivePower, **mReactivePower};
128 Complex current = power / **mNomVoltage;
129 //Complex current = power / (**mIntfVoltage)(0,0);
130
131 mDirectCurrentRef = std::conj(current);
133 **mSubCurrentSource->mCurrentRef = mDirectCurrentRef;
134 SPDLOG_LOGGER_DEBUG(mSLog,
135 "\n--- update set points ---"
136 "\npower: {:s}"
137 "\nCurrent: {:s}",
139 Logger::phasorToString(std::conj(current)));
140}
141
143 AttributeBase::List &prevStepDependencies,
144 AttributeBase::List &attributeDependencies,
145 AttributeBase::List &modifiedAttributes) {
146 attributeDependencies.push_back(mActivePower);
147 attributeDependencies.push_back(mReactivePower);
148 attributeDependencies.push_back(mNomVoltage);
150 modifiedAttributes.push_back(mRightVector);
151}
152
154 AttributeBase::List &prevStepDependencies,
155 AttributeBase::List &attributeDependencies,
156 AttributeBase::List &modifiedAttributes,
157 Attribute<Matrix>::Ptr &leftVector) {
159 attributeDependencies.push_back(leftVector);
160 modifiedAttributes.push_back(mIntfCurrent);
161 modifiedAttributes.push_back(mIntfVoltage);
162}
163
168
170 if (mUseSubcomponents) {
171 **mIntfCurrent = mSubCurrentSource->intfCurrent();
172 **mIntfVoltage = mSubCurrentSource->intfVoltage();
173 }
174}
175
177 Attribute<Matrix>::Ptr &leftVector) {
179 if (!mUseSubcomponents) {
180 (**mIntfVoltage)(0, 0) =
183 mNumFreqs, 0)
184 : Complex(0., 0.);
185 (**mIntfCurrent)(0, 0) = mDirectCurrentRef;
186 }
187}
std::vector< Ptr > List
Definition Attribute.h:123
AttributePointer< Attribute< T > > Ptr
Definition Attribute.h:249
void addMNASubComponent(typename SimPowerComp< Complex >::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)
PQLoadCS(String uid, String name, Logger::Level logLevel=Logger::Level::off)
Defines UID, name and logging level.
void initializeParentFromNodesAndTerminals(Real frequency) override
Initializes component from power flow data.
void mnaParentAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override
void mnaParentPreStep(Real time, Int timeStepCount) override
MNA pre and post step operations.
void mnaParentPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
void mnaParentInitialize(Real omega, Real timeStep, Attribute< Matrix >::Ptr leftVector) override
SimPowerComp< Complex >::Ptr clone(String name) override
DEPRECATED: Delete method.
void createSubComponents() override
Constructs and registers MNA subcomponents; idempotent.
void updateSetPoint()
True after createSubComponents() runs; prevents double-construction.
const Attribute< Real >::Ptr mActivePower
std::shared_ptr< DP::Ph1::CurrentSource > mSubCurrentSource
Internal current source.
void setParameters(Real activePower, Real reactivePower, Real nomVolt)
void mnaParentAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
void mnaParentApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) override
const Attribute< Real >::Ptr mReactivePower
void mnaParentApplyRightSideVectorStamp(Matrix &rightVector) override
const Attribute< Real >::Ptr mNomVoltage
const Attribute< String >::Ptr mName
Human readable name.
String uid()
Returns unique id.
AttributeList::Ptr mAttributes
Attribute List.
spdlog::level::level_enum Level
Definition Logger.h:33
static String phasorToString(const Complex &num)
Definition Logger.cpp:57
Attribute< Matrix >::Ptr mRightVector
static Complex complexFromVectorElement(const Matrix &mat, Matrix::Index row, Int maxFreq=1, Int freqIdx=0)
static void setVectorElement(Matrix &mat, Matrix::Index row, Complex value, Int maxFreq=1, Int freqIdx=0, Matrix::Index colOffset=0)
UInt matrixNodeIndex(UInt nodeIndex)
const Attribute< MatrixVar< Complex > >::Ptr mIntfCurrent
SimTerminal< Complex >::List mTerminals
const Attribute< MatrixVar< Complex > >::Ptr mIntfVoltage
std::shared_ptr< SimPowerComp< VarType > > Ptr
Bool terminalNotGrounded(UInt index)
Complex initialSingleVoltage(UInt index)
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< PQLoadCS > make(Args &&...args)
Definition PtrFactory.h:19
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
bool Bool
Definition Definitions.h:62
Eigen::SparseMatrix< Real, Eigen::RowMajor > SparseMatrixRow
Sparse matrix for real numbers (row major).
Definition Definitions.h:72