DPsim
Loading...
Searching...
No Matches
PowerControllerVSI.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
11#include <limits>
12
13using namespace CPS;
14using namespace CPS::Signal;
15
17 : SimSignalComp(name, name, logLevel),
19 mInputPrev(mAttributes->create<Matrix>("input_prev", Matrix::Zero(6, 1))),
20 mStatePrev(mAttributes->create<Matrix>("state_prev", Matrix::Zero(6, 1))),
22 mAttributes->create<Matrix>("output_prev", Matrix::Zero(2, 1))),
23 mInputCurr(mAttributes->create<Matrix>("input_curr", Matrix::Zero(6, 1))),
24 mStateCurr(mAttributes->create<Matrix>("state_curr", Matrix::Zero(6, 1))),
26 mAttributes->create<Matrix>("output_curr", Matrix::Zero(2, 1))),
27 mVc_d(mAttributes->createDynamic<Real>("Vc_d")),
28 mVc_q(mAttributes->createDynamic<Real>("Vc_q")),
29 mIrc_d(mAttributes->createDynamic<Real>("Irc_d")),
30 mIrc_q(mAttributes->createDynamic<Real>("Irc_q")) {
31
32 SPDLOG_LOGGER_INFO(mSLog, "Create {} {}", type(), name);
33}
34
36 mPref = Pref;
37 mQref = Qref;
38
39 SPDLOG_LOGGER_INFO(mSLog, "General Parameters:");
40 SPDLOG_LOGGER_INFO(mSLog, "Active Power={} [W] Reactive Power={} [VAr]",
41 mPref, mQref);
42
43 // use Pref and Qref as init values for states P and Q
44 // init values for other states remain zero (if not changed using setInitialStateValues)
45 mPInit = Pref;
46 mQInit = Qref;
47}
48
50 Real Ki_powerCtrl,
51 Real Kp_currCtrl,
52 Real Ki_currCtrl,
53 Real Omega_cutoff) {
54
55 mKiPowerCtrld = Ki_powerCtrl;
56 mKiPowerCtrlq = Ki_powerCtrl;
57 mKpPowerCtrld = Kp_powerCtrl;
58 mKpPowerCtrlq = Kp_powerCtrl;
59 mKiCurrCtrld = Ki_currCtrl;
60 mKiCurrCtrlq = Ki_currCtrl;
61 mKpCurrCtrld = Kp_currCtrl;
62 mKpCurrCtrlq = Kp_currCtrl;
63 mOmegaCutoff = Omega_cutoff;
64
65 SPDLOG_LOGGER_INFO(mSLog, "Control Parameters:");
66 SPDLOG_LOGGER_INFO(mSLog, "Power Loop: K_i = {}, K_p = {}", Kp_powerCtrl,
67 Ki_powerCtrl);
68 SPDLOG_LOGGER_INFO(mSLog, "Current Loop: K_i = {}, K_p = {}", Kp_currCtrl,
69 Ki_currCtrl);
70 SPDLOG_LOGGER_INFO(mSLog, "Cut-Off Frequency = {}", Omega_cutoff);
71
72 // Set state space matrices using controller parameters
73 mA << -mOmegaCutoff, 0, 0, 0, 0, 0, 0, -mOmegaCutoff, 0, 0, 0, 0, -1, 0, 0, 0,
74 0, 0, 0, 1, 0, 0, 0, 0, -mKpPowerCtrld, 0, mKiPowerCtrld, 0, 0, 0, 0,
76
77 mB << 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0,
78 mKpPowerCtrld, 0, 0, 0, -1, 0, 0, -mKpPowerCtrlq, 0, 0, 0, -1;
79
83
84 mD << mKpCurrCtrld * mKpPowerCtrld, 0, 0, 0, -mKpCurrCtrld, 0, 0,
86
87 SPDLOG_LOGGER_INFO(mSLog, "State space matrices:");
88 SPDLOG_LOGGER_INFO(mSLog, "A = \n{}", mA);
89 SPDLOG_LOGGER_INFO(mSLog, "B = \n{}", mB);
90 SPDLOG_LOGGER_INFO(mSLog, "C = \n{}", mC);
91 SPDLOG_LOGGER_INFO(mSLog, "D = \n{}", mD);
92}
93
95 Real phi_dInit, Real phi_qInit,
96 Real gamma_dInit,
97 Real gamma_qInit) {
98
99 mPInit = pInit;
100 mQInit = qInit;
101 mPhi_dInit = phi_dInit;
102 mPhi_qInit = phi_qInit;
103 mGamma_dInit = gamma_dInit;
104 mGamma_qInit = gamma_qInit;
106
107 SPDLOG_LOGGER_INFO(mSLog, "Initial State Value Parameters:");
108 SPDLOG_LOGGER_INFO(mSLog, "PInit = {}, QInit = {}", pInit, qInit);
109 SPDLOG_LOGGER_INFO(mSLog, "Phi_dInit = {}, Phi_qInit = {}", phi_dInit,
110 phi_qInit);
111 SPDLOG_LOGGER_INFO(mSLog, "Gamma_dInit = {}, Gamma_qInit = {}", gamma_dInit,
112 gamma_qInit);
113}
114
116 Real iQ, Real vSourceD,
117 Real vSourceQ) {
119 return;
120
121 // At equilibrium the power states equal their references, the power-loop
122 // integrators cancel the measured d/q currents, and the current-loop
123 // integrators produce the required converter-side voltage.
124 mPInit = pRef;
125 mQInit = qRef;
126 mPhi_dInit = std::abs(mKiPowerCtrld) > std::numeric_limits<Real>::epsilon()
127 ? iD / mKiPowerCtrld
128 : 0.;
129 mPhi_qInit = std::abs(mKiPowerCtrlq) > std::numeric_limits<Real>::epsilon()
130 ? iQ / mKiPowerCtrlq
131 : 0.;
132 mGamma_dInit = std::abs(mKiCurrCtrld) > std::numeric_limits<Real>::epsilon()
133 ? vSourceD / mKiCurrCtrld
134 : 0.;
135 mGamma_qInit = std::abs(mKiCurrCtrlq) > std::numeric_limits<Real>::epsilon()
136 ? vSourceQ / mKiCurrCtrlq
137 : 0.;
138}
139
141 Real omega, Real timeStep, Attribute<Matrix>::Ptr leftVector) {
142 mTimeStep = timeStep;
143 mOmegaCutoff = omega;
144
145 // update B matrix due to its dependence on Irc
147
148 const Matrix identity = Matrix::Identity(mA.rows(), mA.cols());
149 const Matrix f1 = identity + (mTimeStep / 2.) * mA;
150 const Matrix f2 = identity - (mTimeStep / 2.) * mA;
151 mF2Inverse = f2.inverse();
152 mDiscreteA = mF2Inverse * f1;
153 mTransformedB.noalias() = mF2Inverse * (mTimeStep / 2.) * mB;
154
155 // initialization of input
156 **mInputCurr << mPref, mQref, **mVc_d, **mVc_q, **mIrc_d, **mIrc_q;
157 SPDLOG_LOGGER_INFO(mSLog, "Initialization of input: \n" +
159
160 // initialization of states
163 SPDLOG_LOGGER_INFO(mSLog, "Initialization of states: \n" +
165
166 // initialization of output
167 **mOutputCurr = mC * **mStateCurr + mD * **mInputCurr;
168 SPDLOG_LOGGER_INFO(mSLog, "Initialization of output: \n" +
170}
171
173 AttributeBase::List &prevStepDependencies,
174 AttributeBase::List &attributeDependencies,
175 AttributeBase::List &modifiedAttributes) {
176 prevStepDependencies.push_back(mInputCurr);
177 prevStepDependencies.push_back(mOutputCurr);
178 modifiedAttributes.push_back(mInputPrev);
179 modifiedAttributes.push_back(mOutputPrev);
180};
181
182void PowerControllerVSI::signalPreStep(Real time, Int timeStepCount) {
186}
187
189 AttributeBase::List &prevStepDependencies,
190 AttributeBase::List &attributeDependencies,
191 AttributeBase::List &modifiedAttributes) {
192 attributeDependencies.push_back(mVc_d);
193 attributeDependencies.push_back(mVc_q);
194 attributeDependencies.push_back(mIrc_d);
195 attributeDependencies.push_back(mIrc_q);
196 modifiedAttributes.push_back(mInputCurr);
197 modifiedAttributes.push_back(mOutputCurr);
198};
199
200void PowerControllerVSI::signalStep(Real time, Int timeStepCount) {
201 // update B matrix due to its dependence on Irc
203
204 // get current inputs
205 **mInputCurr << mPref, mQref, **mVc_d, **mVc_q, **mIrc_d, **mIrc_q;
206 SPDLOG_LOGGER_DEBUG(
207 mSLog,
208 "Time {}\n: inputCurr = \n{}\n , inputPrev = \n{}\n , statePrev = \n{}",
209 time, **mInputCurr, **mInputPrev, **mStatePrev);
210
211 // calculate new states
212 mInputSum.noalias() = **mInputCurr + **mInputPrev;
213 mTransformedB.noalias() = mF2Inverse * (mTimeStep / 2.) * mB;
214 (**mStateCurr).noalias() =
216 SPDLOG_LOGGER_DEBUG(mSLog, "stateCurr = \n {}", **mStateCurr);
217
218 // calculate new outputs
219 **mOutputCurr = mC * **mStateCurr + mD * **mInputCurr;
220 SPDLOG_LOGGER_DEBUG(mSLog, "Output values: outputCurr = \n{}", **mOutputCurr);
221}
222
224 mB.coeffRef(0, 2) = mOmegaCutoff * **mIrc_d;
225 mB.coeffRef(0, 3) = mOmegaCutoff * **mIrc_q;
226 mB.coeffRef(1, 2) = -mOmegaCutoff * **mIrc_q;
227 mB.coeffRef(1, 3) = mOmegaCutoff * **mIrc_d;
228}
229
231 return Task::List(
232 {std::make_shared<PreStep>(*this), std::make_shared<Step>(*this)});
233}
std::vector< Ptr > List
Definition Attribute.h:123
AttributePointer< Attribute< T > > Ptr
Definition Attribute.h:249
String type()
Get component type (cross-platform)
AttributeList::Ptr mAttributes
Attribute List.
spdlog::level::level_enum Level
Definition Logger.h:33
static String matrixToString(const Matrix &mat)
Definition Logger.cpp:31
Real mTimeStep
Simulation time step.
void setControllerParameters(Real Kp_powerCtrl, Real Ki_powerCtrl, Real Kp_currCtrl, Real Ki_currCtrl, Real Omega_cutoff)
Setter for parameters of control loops.
const Attribute< Matrix >::Ptr mInputPrev
Previous Input.
Matrix mDiscreteA
Cached trapezoidal factors; A and time step are constant during a run.
Matrix mD
matrix D of state space model
Matrix mC
matrix C of state space model
void setParameters(Real Pref, Real Qref)
Setter for general parameters.
void signalAddStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes)
add step dependencies
const Attribute< Real >::Ptr mVc_q
Real mPInit
initial values for states
void setInitialStateValues(Real pInit, Real qInit, Real phi_dInit, Real phi_qInit, Real gamma_dInit, Real gamma_qInit)
Setter for initial state values.
const Attribute< Real >::Ptr mIrc_q
void updateBMatrixStateSpaceModel()
Update B matrix due to its dependence on the input.
const Attribute< Matrix >::Ptr mStateCurr
Current State.
void signalStep(Real time, Int timeStepCount)
step operations
PowerControllerVSI(String name, Logger::Level logLevel=Logger::Level::off)
void initializeStateSpaceModel(Real omega, Real timeStep, Attribute< Matrix >::Ptr leftVector)
Initialize vectors of state space model.
const Attribute< Real >::Ptr mVc_d
These are never explicitely set to reference anything, so the outside code is responsible for setting...
void initializeSteadyState(Real pRef, Real qRef, Real iD, Real iQ, Real vSourceD, Real vSourceQ)
void signalPreStep(Real time, Int timeStepCount)
pre step operations
const Attribute< Matrix >::Ptr mOutputCurr
Current Output.
void signalAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes)
pre step dependencies
const Attribute< Matrix >::Ptr mInputCurr
Current Input.
const Attribute< Matrix >::Ptr mStatePrev
Previous State.
Matrix mA
matrix A of state space model
const Attribute< Matrix >::Ptr mOutputPrev
Previous Output.
const Attribute< Real >::Ptr mIrc_d
Matrix mB
matrix B of state space model
SimSignalComp(String uid, String name, Logger::Level logLevel=Logger::Level::off)
std::vector< Ptr > List
Definition Task.h:28
Logger::Log mSLog
Component logger.
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