DPsim
DP_Ph1_PQLoadCS.cpp
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 
9 #include <dpsim-models/DP/DP_Ph1_PQLoadCS.h>
10 
11 using namespace CPS;
12 
13 DP::Ph1::PQLoadCS::PQLoadCS(String uid, String name, Logger::Level logLevel)
14  : CompositePowerComp<Complex>(uid, name, true, true, logLevel),
15  mActivePower(mAttributes->create<Real>("P", 0)),
16  mReactivePower(mAttributes->create<Real>("Q", 0)),
17  mNomVoltage(mAttributes->create<Real>("V_nom")) {
18  setTerminalNumber(1);
19  **mIntfVoltage = MatrixComp::Zero(1, 1);
20  **mIntfCurrent = MatrixComp::Zero(1, 1);
21 }
22 
23 DP::Ph1::PQLoadCS::PQLoadCS(String uid, String name, Real activePower,
24  Real reactivePower, Real nomVolt,
25  Logger::Level logLevel)
26  : PQLoadCS(uid, name, logLevel) {
27 
28  setParameters(activePower, reactivePower, nomVolt);
29 }
30 
31 DP::Ph1::PQLoadCS::PQLoadCS(String name, Logger::Level logLevel)
32  : PQLoadCS(name, name, logLevel) {}
33 
34 DP::Ph1::PQLoadCS::PQLoadCS(String name, Real activePower, Real reactivePower,
35  Real nomVolt, Logger::Level logLevel)
36  : PQLoadCS(name, name, activePower, reactivePower, nomVolt, logLevel) {}
37 
38 void DP::Ph1::PQLoadCS::setParameters(Real activePower, Real reactivePower,
39  Real nomVolt) {
40  **mActivePower = activePower;
41  **mReactivePower = reactivePower;
42  **mNomVoltage = nomVolt;
43  mParametersSet = true;
44 }
45 
48  auto copy = PQLoadCS::make(name, mLogLevel);
49  copy->setParameters(**mActivePower, **mReactivePower, **mNomVoltage);
50  return copy;
51 }
52 
54  // Get power from Terminals if it was not set previously.
55  if (**mActivePower == 0 && **mReactivePower == 0 && !mParametersSet) {
56  **mActivePower = mTerminals[0]->singleActivePower();
57  **mReactivePower = mTerminals[0]->singleReactivePower();
58  **mNomVoltage = std::abs(mTerminals[0]->initialSingleVoltage());
59  }
60  Complex power = Complex(**mActivePower, **mReactivePower);
61 
62  Complex current;
63  if (**mNomVoltage != 0)
64  current = std::conj(power / **mNomVoltage);
65  else
66  current = 0;
67 
68  mSubCurrentSource =
69  std::make_shared<DP::Ph1::CurrentSource>(**mName + "_cs", mLogLevel);
70  mSubCurrentSource->setParameters(current);
71  // A positive power should result in a positive current to ground.
72  mSubCurrentSource->connect({mTerminals[0]->node(), SimNode::GND});
73  mSubCurrentSource->initializeFromNodesAndTerminals(frequency);
74  addMNASubComponent(mSubCurrentSource,
75  MNA_SUBCOMP_TASK_ORDER::TASK_AFTER_PARENT,
76  MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, true);
77  updateIntfValues();
78 
79  SPDLOG_LOGGER_INFO(mSLog,
80  "\n--- Initialization from powerflow ---"
81  "\nVoltage across: {:s}"
82  "\nCurrent: {:s}"
83  "\nTerminal 0 voltage: {:s}"
84  "\nCurrent set point: {:s}"
85  "\n--- Initialization from powerflow finished ---",
86  Logger::phasorToString((**mIntfVoltage)(0, 0)),
87  Logger::phasorToString((**mIntfCurrent)(0, 0)),
88  Logger::phasorToString(initialSingleVoltage(0)),
89  Logger::phasorToString(current));
90 }
91 
92 void DP::Ph1::PQLoadCS::updateSetPoint() {
93  // Calculate new current set point.
94  Complex power = {**mActivePower, **mReactivePower};
95  Complex current = power / **mNomVoltage;
96  //Complex current = power / (**mIntfVoltage)(0,0);
97 
98  **mSubCurrentSource->mCurrentRef = std::conj(current);
99  SPDLOG_LOGGER_DEBUG(mSLog,
100  "\n--- update set points ---"
101  "\npower: {:s}"
102  "\nCurrent: {:s}",
103  Logger::phasorToString(power),
104  Logger::phasorToString(std::conj(current)));
105 }
106 
107 void DP::Ph1::PQLoadCS::mnaParentAddPreStepDependencies(
108  AttributeBase::List &prevStepDependencies,
109  AttributeBase::List &attributeDependencies,
110  AttributeBase::List &modifiedAttributes) {
111  attributeDependencies.push_back(mActivePower);
112  attributeDependencies.push_back(mReactivePower);
113  attributeDependencies.push_back(mNomVoltage);
114 }
115 
116 void DP::Ph1::PQLoadCS::mnaParentAddPostStepDependencies(
117  AttributeBase::List &prevStepDependencies,
118  AttributeBase::List &attributeDependencies,
119  AttributeBase::List &modifiedAttributes,
120  Attribute<Matrix>::Ptr &leftVector) {
121  modifiedAttributes.push_back(mIntfCurrent);
122  modifiedAttributes.push_back(mIntfVoltage);
123 }
124 
125 void DP::Ph1::PQLoadCS::mnaParentPreStep(Real time, Int timeStepCount) {
126  updateSetPoint();
127  mnaCompApplyRightSideVectorStamp(**mRightVector);
128 }
129 
130 void DP::Ph1::PQLoadCS::updateIntfValues() {
131  **mIntfCurrent = mSubCurrentSource->intfCurrent();
132  **mIntfVoltage = mSubCurrentSource->intfVoltage();
133 }
134 
135 void DP::Ph1::PQLoadCS::mnaParentPostStep(Real time, Int timeStepCount,
136  Attribute<Matrix>::Ptr &leftVector) {
137  updateIntfValues();
138 }
Base class for composite power components.
PQ-load represented by a current source.
PQLoadCS(String uid, String name, Logger::Level logLevel=Logger::Level::off)
Defines UID, name and logging level.
void mnaParentPreStep(Real time, Int timeStepCount) override
MNA pre and post step operations.
SimPowerComp< Complex >::Ptr clone(String name) override
DEPRECATED: Delete method.
void initializeFromNodesAndTerminals(Real frequency) override
Initializes component from power flow data.
Base class for all components that are transmitting power.
Definition: SimPowerComp.h:17
const Attribute< MatrixVar< Complex > >::Ptr mIntfCurrent
Current through component.
Definition: SimPowerComp.h:47
const Attribute< MatrixVar< Complex > >::Ptr mIntfVoltage
Voltage between terminals.
Definition: SimPowerComp.h:45