DPsim
Loading...
Searching...
No Matches
PowerControllerVSI.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/Signal/PowerControllerVSI.h>
10
11using namespace CPS;
12using namespace CPS::Signal;
13
14PowerControllerVSI::PowerControllerVSI(String name, Logger::Level logLevel)
15 : SimSignalComp(name, name, logLevel),
17 mInputPrev(mAttributes->create<Matrix>("input_prev", Matrix::Zero(6, 1))),
18 mStatePrev(mAttributes->create<Matrix>("state_prev", Matrix::Zero(6, 1))),
19 mOutputPrev(
20 mAttributes->create<Matrix>("output_prev", Matrix::Zero(2, 1))),
21 mInputCurr(mAttributes->create<Matrix>("input_curr", Matrix::Zero(6, 1))),
22 mStateCurr(mAttributes->create<Matrix>("state_curr", Matrix::Zero(6, 1))),
23 mOutputCurr(
24 mAttributes->create<Matrix>("output_curr", Matrix::Zero(2, 1))),
25 mVc_d(mAttributes->createDynamic<Real>("Vc_d")),
26 mVc_q(mAttributes->createDynamic<Real>("Vc_q")),
27 mIrc_d(mAttributes->createDynamic<Real>("Irc_d")),
28 mIrc_q(mAttributes->createDynamic<Real>("Irc_q")) {
29
30 SPDLOG_LOGGER_INFO(mSLog, "Create {} {}", type(), name);
31}
32
33void PowerControllerVSI::setParameters(Real Pref, Real Qref) {
34 mPref = Pref;
35 mQref = Qref;
36
37 SPDLOG_LOGGER_INFO(mSLog, "General Parameters:");
38 SPDLOG_LOGGER_INFO(mSLog, "Active Power={} [W] Reactive Power={} [VAr]",
39 mPref, mQref);
40
41 // use Pref and Qref as init values for states P and Q
42 // init values for other states remain zero (if not changed using setInitialStateValues)
43 mPInit = Pref;
44 mQInit = Qref;
45}
46
48 Real Ki_powerCtrl,
49 Real Kp_currCtrl,
50 Real Ki_currCtrl,
51 Real Omega_cutoff) {
52
53 mKiPowerCtrld = Ki_powerCtrl;
54 mKiPowerCtrlq = Ki_powerCtrl;
55 mKpPowerCtrld = Kp_powerCtrl;
56 mKpPowerCtrlq = Kp_powerCtrl;
57 mKiCurrCtrld = Ki_currCtrl;
58 mKiCurrCtrlq = Ki_currCtrl;
59 mKpCurrCtrld = Kp_currCtrl;
60 mKpCurrCtrlq = Kp_currCtrl;
61 mOmegaCutoff = Omega_cutoff;
62
63 SPDLOG_LOGGER_INFO(mSLog, "Control Parameters:");
64 SPDLOG_LOGGER_INFO(mSLog, "Power Loop: K_i = {}, K_p = {}", Kp_powerCtrl,
65 Ki_powerCtrl);
66 SPDLOG_LOGGER_INFO(mSLog, "Current Loop: K_i = {}, K_p = {}", Kp_currCtrl,
67 Ki_currCtrl);
68 SPDLOG_LOGGER_INFO(mSLog, "Cut-Off Frequency = {}", Omega_cutoff);
69
70 // Set state space matrices using controller parameters
71 mA << -mOmegaCutoff, 0, 0, 0, 0, 0, 0, -mOmegaCutoff, 0, 0, 0, 0, -1, 0, 0, 0,
72 0, 0, 0, 1, 0, 0, 0, 0, -mKpPowerCtrld, 0, mKiPowerCtrld, 0, 0, 0, 0,
73 mKpPowerCtrlq, 0, mKiPowerCtrlq, 0, 0;
74
75 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,
76 mKpPowerCtrld, 0, 0, 0, -1, 0, 0, -mKpPowerCtrlq, 0, 0, 0, -1;
77
78 mC << -mKpPowerCtrld * mKpCurrCtrld, 0, mKpCurrCtrld * mKiPowerCtrld, 0,
79 mKiCurrCtrld, 0, 0, mKpPowerCtrlq * mKpCurrCtrlq, 0,
80 mKpCurrCtrlq * mKiPowerCtrlq, 0, mKiCurrCtrlq;
81
82 mD << mKpCurrCtrld * mKpPowerCtrld, 0, 0, 0, -mKpCurrCtrld, 0, 0,
83 -mKpCurrCtrlq * mKpPowerCtrlq, 0, 0, 0, -mKpCurrCtrlq;
84
85 SPDLOG_LOGGER_INFO(mSLog, "State space matrices:");
86 SPDLOG_LOGGER_INFO(mSLog, "A = \n{}", mA);
87 SPDLOG_LOGGER_INFO(mSLog, "B = \n{}", mB);
88 SPDLOG_LOGGER_INFO(mSLog, "C = \n{}", mC);
89 SPDLOG_LOGGER_INFO(mSLog, "D = \n{}", mD);
90}
91
93 Real phi_dInit, Real phi_qInit,
94 Real gamma_dInit,
95 Real gamma_qInit) {
96
97 mPInit = pInit;
98 mQInit = qInit;
99 mPhi_dInit = phi_dInit;
100 mPhi_qInit = phi_qInit;
101 mGamma_dInit = gamma_dInit;
102 mGamma_qInit = gamma_qInit;
103
104 SPDLOG_LOGGER_INFO(mSLog, "Initial State Value Parameters:");
105 SPDLOG_LOGGER_INFO(mSLog, "PInit = {}, QInit = {}", pInit, qInit);
106 SPDLOG_LOGGER_INFO(mSLog, "Phi_dInit = {}, Phi_qInit = {}", phi_dInit,
107 phi_qInit);
108 SPDLOG_LOGGER_INFO(mSLog, "Gamma_dInit = {}, Gamma_qInit = {}", gamma_dInit,
109 gamma_qInit);
110}
111
113 Real omega, Real timeStep, Attribute<Matrix>::Ptr leftVector) {
114 mTimeStep = timeStep;
115 mOmegaCutoff = omega;
116
117 // update B matrix due to its dependence on Irc
119
120 // initialization of input
121 **mInputCurr << mPref, mQref, **mVc_d, **mVc_q, **mIrc_d, **mIrc_q;
122 SPDLOG_LOGGER_INFO(mSLog, "Initialization of input: \n" +
123 Logger::matrixToString(**mInputCurr));
124
125 // initialization of states
126 **mStateCurr << mPInit, mQInit, mPhi_dInit, mPhi_qInit, mGamma_dInit,
127 mGamma_qInit;
128 SPDLOG_LOGGER_INFO(mSLog, "Initialization of states: \n" +
129 Logger::matrixToString(**mStateCurr));
130
131 // initialization of output
132 **mOutputCurr = mC * **mStateCurr + mD * **mInputCurr;
133 SPDLOG_LOGGER_INFO(mSLog, "Initialization of output: \n" +
134 Logger::matrixToString(**mOutputCurr));
135}
136
138 AttributeBase::List &prevStepDependencies,
139 AttributeBase::List &attributeDependencies,
140 AttributeBase::List &modifiedAttributes) {
141 prevStepDependencies.push_back(mInputCurr);
142 prevStepDependencies.push_back(mOutputCurr);
143 modifiedAttributes.push_back(mInputPrev);
144 modifiedAttributes.push_back(mOutputPrev);
145};
146
147void PowerControllerVSI::signalPreStep(Real time, Int timeStepCount) {
151}
152
154 AttributeBase::List &prevStepDependencies,
155 AttributeBase::List &attributeDependencies,
156 AttributeBase::List &modifiedAttributes) {
157 attributeDependencies.push_back(mVc_d);
158 attributeDependencies.push_back(mVc_q);
159 attributeDependencies.push_back(mIrc_d);
160 attributeDependencies.push_back(mIrc_q);
161 modifiedAttributes.push_back(mInputCurr);
162 modifiedAttributes.push_back(mOutputCurr);
163};
164
165void PowerControllerVSI::signalStep(Real time, Int timeStepCount) {
166 // update B matrix due to its dependence on Irc
168
169 // get current inputs
170 **mInputCurr << mPref, mQref, **mVc_d, **mVc_q, **mIrc_d, **mIrc_q;
171 SPDLOG_LOGGER_DEBUG(
172 mSLog,
173 "Time {}\n: inputCurr = \n{}\n , inputPrev = \n{}\n , statePrev = \n{}",
174 time, **mInputCurr, **mInputPrev, **mStatePrev);
175
176 // calculate new states
177 **mStateCurr = Math::StateSpaceTrapezoidal(**mStatePrev, mA, mB, mTimeStep,
179 SPDLOG_LOGGER_DEBUG(mSLog, "stateCurr = \n {}", **mStateCurr);
180
181 // calculate new outputs
182 **mOutputCurr = mC * **mStateCurr + mD * **mInputCurr;
183 SPDLOG_LOGGER_DEBUG(mSLog, "Output values: outputCurr = \n{}", **mOutputCurr);
184}
185
187 mB.coeffRef(0, 2) = mOmegaCutoff * **mIrc_d;
188 mB.coeffRef(0, 3) = mOmegaCutoff * **mIrc_q;
189 mB.coeffRef(1, 2) = -mOmegaCutoff * **mIrc_q;
190 mB.coeffRef(1, 3) = mOmegaCutoff * **mIrc_d;
191}
192
193Task::List PowerControllerVSI::getTasks() {
194 return Task::List(
195 {std::make_shared<PreStep>(*this), std::make_shared<Step>(*this)});
196}
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 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
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.
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
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 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.
Matrix mB
matrix B of state space model
Logger::Log mSLog
Component logger.