DPsim
PLL.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/PLL.h>
10 
11 using namespace CPS;
12 using namespace CPS::Signal;
13 
14 PLL::PLL(String name, Logger::Level logLevel)
15  : SimSignalComp(name, name, logLevel),
16  mInputRef(mAttributes->createDynamic<Real>("input_ref")),
18  mInputPrev(mAttributes->create<Matrix>("input_prev", Matrix::Zero(2, 1))),
19  mStatePrev(mAttributes->create<Matrix>("state_prev", Matrix::Zero(2, 1))),
20  mOutputPrev(
21  mAttributes->create<Matrix>("output_prev", Matrix::Zero(2, 1))),
22  mInputCurr(mAttributes->create<Matrix>("input_curr", Matrix::Zero(2, 1))),
23  mStateCurr(mAttributes->create<Matrix>("state_curr", Matrix::Zero(2, 1))),
24  mOutputCurr(
25  mAttributes->create<Matrix>("output_curr", Matrix::Zero(2, 1))) {}
26 
27 void PLL::setParameters(Real kpPLL, Real kiPLL, Real omegaNom) {
28  mKp = kpPLL;
29  mKi = kiPLL;
30  mOmegaNom = omegaNom;
31  SPDLOG_LOGGER_INFO(mSLog, "Kp = {}, Ki = {}", mKp, mKi);
32 
33  // First entry of input vector is constant omega
34  (**mInputCurr)(0, 0) = mOmegaNom;
35 }
36 
37 void PLL::setSimulationParameters(Real timestep) {
38  mTimeStep = timestep;
39  SPDLOG_LOGGER_INFO(mSLog, "Integration step = {}", mTimeStep);
40 }
41 
42 void PLL::setInitialValues(Real input_init, Matrix state_init,
43  Matrix output_init) {
44  (**mInputCurr)(1, 0) = input_init;
45  **mStateCurr = state_init;
46  **mOutputCurr = output_init;
47 
48  SPDLOG_LOGGER_INFO(mSLog, "Initial values:");
49  SPDLOG_LOGGER_INFO(mSLog,
50  "inputCurrInit = ({}, {}), stateCurrInit = ({}, {}), "
51  "outputCurrInit = ({}, {})",
52  (**mInputCurr)(0, 0), (**mInputCurr)(1, 0),
53  (**mInputPrev)(0, 0), (**mInputPrev)(1, 0),
54  (**mStateCurr)(0, 0), (**mStateCurr)(1, 0),
55  (**mStatePrev)(0, 0), (**mStatePrev)(1, 0));
56 }
57 
59  mA << 0, mKi, 0, 0;
60  mB << 1, mKp, 0, 1;
61  mC << 1, 0, 0, 1;
62  mD << 0, 0, 0, 0;
63 
64  SPDLOG_LOGGER_INFO(mSLog, "State space matrices:");
65  SPDLOG_LOGGER_INFO(mSLog, "A = \n{}", mA);
66  SPDLOG_LOGGER_INFO(mSLog, "B = \n{}", mB);
67  SPDLOG_LOGGER_INFO(mSLog, "C = \n{}", mC);
68  SPDLOG_LOGGER_INFO(mSLog, "D = \n{}", mD);
69 }
70 
72  AttributeBase::List &prevStepDependencies,
73  AttributeBase::List &attributeDependencies,
74  AttributeBase::List &modifiedAttributes) {
75  prevStepDependencies.push_back(mInputCurr);
76  prevStepDependencies.push_back(mOutputCurr);
77  modifiedAttributes.push_back(mInputPrev);
78  modifiedAttributes.push_back(mOutputPrev);
79 };
80 
81 void PLL::signalPreStep(Real time, Int timeStepCount) {
82  **mInputPrev = **mInputCurr;
83  **mStatePrev = **mStateCurr;
85 }
86 
87 void PLL::signalAddStepDependencies(AttributeBase::List &prevStepDependencies,
88  AttributeBase::List &attributeDependencies,
89  AttributeBase::List &modifiedAttributes) {
90  attributeDependencies.push_back(mInputRef);
91  modifiedAttributes.push_back(mInputCurr);
92  modifiedAttributes.push_back(mOutputCurr);
93 };
94 
95 void PLL::signalStep(Real time, Int timeStepCount) {
96  (**mInputCurr)(1, 0) = **mInputRef;
97 
98  SPDLOG_LOGGER_TRACE(mSLog, "Time {}:", time);
99  SPDLOG_LOGGER_TRACE(mSLog,
100  "Input values: inputCurr = ({}, {}), inputPrev = ({}, "
101  "{}), stateCurr = ({}, {}), statePrev = ({}, {})",
102  (**mInputCurr)(0, 0), (**mInputCurr)(1, 0),
103  (**mInputPrev)(0, 0), (**mInputPrev)(1, 0),
104  (**mStateCurr)(0, 0), (**mStateCurr)(1, 0),
105  (**mStatePrev)(0, 0), (**mStatePrev)(1, 0));
106 
107  **mStateCurr = Math::StateSpaceTrapezoidal(**mStatePrev, mA, mB, mTimeStep,
108  **mInputCurr, **mInputPrev);
109  **mOutputCurr = mC * **mStateCurr + mD * **mInputCurr;
110 
111  SPDLOG_LOGGER_TRACE(mSLog, "State values: stateCurr = ({}, {})",
112  (**mStateCurr)(0, 0), (**mStateCurr)(1, 0));
113  SPDLOG_LOGGER_TRACE(mSLog, "Output values: outputCurr = ({}, {}):",
114  (**mOutputCurr)(0, 0), (**mOutputCurr)(1, 0));
115 }
116 
117 Task::List PLL::getTasks() {
118  return Task::List(
119  {std::make_shared<PreStep>(*this), std::make_shared<Step>(*this)});
120 }
void setInitialValues(Real input_init, Matrix state_init, Matrix output_init)
Setter for initial values.
Definition: PLL.cpp:42
const Attribute< Matrix >::Ptr mOutputPrev
Previous Output.
Definition: PLL.h:54
Real mKp
Proportional constant of PI controller.
Definition: PLL.h:23
Real mTimeStep
Integration time step.
Definition: PLL.h:30
void signalStep(Real time, Int timeStepCount)
step operations
Definition: PLL.cpp:95
Matrix mD
matrix D of state space model
Definition: PLL.h:39
const Attribute< Real >::Ptr mInputRef
This is never explicitely set to reference anything, so the outside code is responsible for setting u...
Definition: PLL.h:43
const Attribute< Matrix >::Ptr mInputCurr
Current Input.
Definition: PLL.h:48
const Attribute< Matrix >::Ptr mStatePrev
Previous State.
Definition: PLL.h:50
void signalAddStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes)
add step dependencies
Definition: PLL.cpp:87
Matrix mC
matrix C of state space model
Definition: PLL.h:37
const Attribute< Matrix >::Ptr mOutputCurr
Current Output.
Definition: PLL.h:56
void setSimulationParameters(Real timestep)
Setter for simulation parameters.
Definition: PLL.cpp:37
Matrix mB
matrix B of state space model
Definition: PLL.h:35
Real mOmegaNom
Nominal frequency.
Definition: PLL.h:28
void composeStateSpaceMatrices()
Composition of A, B, C, D matrices based on PLL parameters.
Definition: PLL.cpp:58
Matrix mA
matrix A of state space model
Definition: PLL.h:33
void signalAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes)
pre step dependencies
Definition: PLL.cpp:71
const Attribute< Matrix >::Ptr mStateCurr
Current State.
Definition: PLL.h:52
const Attribute< Matrix >::Ptr mInputPrev
Previous Input.
Definition: PLL.h:46
void signalPreStep(Real time, Int timeStepCount)
pre step operations
Definition: PLL.cpp:81
Real mKi
Integration constant of PI controller.
Definition: PLL.h:25
Logger::Log mSLog
Component logger.