DPsim
Loading...
Searching...
No Matches
DP_Ph1_Inverter.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
9#include <cmath>
12
13using namespace CPS;
14using namespace std;
15
17 : MNASimPowerComp<Complex>(uid, name, true, true, logLevel) {
20 **mIntfVoltage = MatrixComp::Zero(1, 1);
21 **mIntfCurrent = MatrixComp::Zero(1, 1);
22}
23
24void DP::Ph1::Inverter::setParameters(const std::vector<Int> &carrierHarms,
25 const std::vector<Int> &modulHarms,
26 Real inputVoltage, Real ratio,
27 Real phase) {
28 mCarHarms = carrierHarms;
29 mModHarms = modulHarms;
30 mVin = inputVoltage;
31 mModIdx = ratio;
32 mPhaseMod = phase;
33 mPhasorsDirty = true;
34
35 mParametersSet = true;
36}
37
39
41 for (Int m = 2; m <= mMaxCarrierHarm; m = m + 2) {
42 mCarHarms.push_back(m);
43 SPDLOG_LOGGER_INFO(mSLog, "Add carrier harmonic {0}", m);
44 }
45 for (Int n = -mMaxModulHarm; n <= mMaxModulHarm; n = n + 2) {
46 mModHarms.push_back(n);
47 SPDLOG_LOGGER_INFO(mSLog, "Add modulation harmonic {0}", n);
48 }
49}
50
53
54 SPDLOG_LOGGER_INFO(mSLog, "\n--- Initialization ---");
55
56 // Check that both lists have the same length
57 mCarHarNum = static_cast<UInt>(mCarHarms.size());
58 mModHarNum = static_cast<UInt>(mModHarms.size());
59 if (mCarHarNum != mModHarNum) {
60 throw std::invalid_argument(
61 "Number of carrier and modulation harmonics must be equal.");
62 }
64
65 mPhasorMags = Matrix::Zero(mHarNum, 1);
66 mPhasorPhases = Matrix::Zero(mHarNum, 1);
67 mPhasorFreqs = Matrix::Zero(mHarNum, 1);
68
69 for (UInt h = 0; h < mHarNum; h++) {
72 }
73
74 // Precompute factorials
75 for (Int f = 0; f <= mMaxBesselSumIdx; f++) {
76 mFactorials.push_back(factorial(f));
77 }
78
79 for (UInt h = 0; h < mHarNum; h++) {
80 for (Int f = 0; f <= mMaxBesselSumIdx; f++)
82 }
83
84 SPDLOG_LOGGER_INFO(mSLog,
85 "\nFrequencies: \n{}"
86 "\nPhases: \n{}"
87 "\n--- End of initialization ---",
89}
90
92 // Compute fundamental content of grid frequency
94 (**mIntfVoltage)(0, 0) = Complex(0, mVfund * -1);
95
96 // Compute sideband harmonics for even multiplies of carrier frequency m
97 // and odd reference signal multiplies n
98 for (UInt h = 0; h < mHarNum; h++) {
100 mCarHarms[h] * mModIdx * PI / 2.);
101 //mPhasorMags(h, 0) = (4.*mVin/PI) * (Jn/mCarHarms[h]) * cos(mCarHarms[h] * PI/2.);
102 //(**mIntfVoltage)(0, h+1) = mPhasorMags(h, 0) * -1i;
103 (**mIntfVoltage)(0, h + 1) =
104 Complex(0, -1 * (4. * mVin / PI) * (Jn / mCarHarms[h]) *
105 cos(mCarHarms[h] * PI / 2.));
106 }
107
108 SPDLOG_LOGGER_DEBUG(mSLog,
109 "\n--- Phasor calculation ---"
110 "\n{}"
111 "\n{}"
112 "\n--- Phasor calculation end ---",
114 mPhasorsDirty = false;
115}
116
117// #### MNA functions ####
118
124
126 Real omega, Real timeStep,
127 std::vector<Attribute<Matrix>::Ptr> leftVectors) {
129
130 mMnaTasks.push_back(std::make_shared<MnaPreStepHarm>(*this));
131 mMnaTasks.push_back(std::make_shared<MnaPostStepHarm>(*this, leftVectors));
132 **mRightVector = Matrix::Zero(leftVectors[0]->get().rows(), mNumFreqs);
133
135}
136
138 SparseMatrixRow &systemMatrix) {
139 SPDLOG_LOGGER_INFO(mSLog, "--- Stamping into system matrix ---");
140
141 for (UInt freq = 0; freq < mNumFreqs; freq++) {
142 SPDLOG_LOGGER_INFO(mSLog, "Stamp frequency {:d}", freq);
143 if (terminalNotGrounded(0)) {
146 freq);
149 mNumFreqs, freq);
150 }
151
152 if (terminalNotGrounded(0)) {
153 SPDLOG_LOGGER_INFO(mSLog, "Add {:f} to system at ({:d},{:d})", 1.,
155 matrixNodeIndex(0));
156 SPDLOG_LOGGER_INFO(mSLog, "Add {:f} to system at ({:d},{:d})", 1.,
159 }
160 }
161 SPDLOG_LOGGER_INFO(mSLog, "--- Stamping into system matrix end ---");
162}
163
165 SparseMatrixRow &systemMatrix, Int freqIdx) {
166 SPDLOG_LOGGER_INFO(mSLog, "Stamp frequency {:d}", freqIdx);
167 if (terminalNotGrounded(0)) {
169 matrixNodeIndex(0), Complex(1, 0));
172 }
173
174 if (terminalNotGrounded(0)) {
175 SPDLOG_LOGGER_INFO(mSLog, "Add {:f} to system at ({:d},{:d})", 1.,
177 SPDLOG_LOGGER_INFO(mSLog, "Add {:f} to system at ({:d},{:d})", 1.,
179 }
180}
181
183 SPDLOG_LOGGER_DEBUG(mSLog, "Stamp harmonics into source vector");
184 for (UInt freq = 0; freq < mNumFreqs; freq++) {
185 if (terminalNotGrounded(0)) {
187 (**mIntfVoltage)(0, freq), mNumFreqs, freq);
188
189 SPDLOG_LOGGER_DEBUG(mSLog,
190 "Add {:s} to source vector at {:d}, harmonic {:d}",
192 mVirtualNodes[0]->matrixNodeIndex(), freq);
193 }
194 }
195}
196
198 Matrix &rightVector) {
199 SPDLOG_LOGGER_DEBUG(mSLog, "Stamp harmonics into source vector");
200 for (UInt freq = 0; freq < mNumFreqs; freq++) {
201 if (terminalNotGrounded(0)) {
203 (**mIntfVoltage)(0, freq), 1, 0, freq);
204 }
205 }
206}
207
209 Matrix &rightVector, Int freq) {
211 (**mIntfVoltage)(0, freq));
212}
213
215 AttributeBase::List &prevStepDependencies,
216 AttributeBase::List &attributeDependencies,
217 AttributeBase::List &modifiedAttributes) {
218 modifiedAttributes.push_back(mRightVector);
219 modifiedAttributes.push_back(mIntfVoltage);
220}
221
227
229 if (mInverter.mPhasorsDirty)
230 mInverter.calculatePhasors();
231 mInverter.mnaCompApplyRightSideVectorStampHarm(**mInverter.mRightVector);
232}
233
235 AttributeBase::List &prevStepDependencies,
236 AttributeBase::List &attributeDependencies,
237 AttributeBase::List &modifiedAttributes,
238 Attribute<Matrix>::Ptr &leftVector) {
239 attributeDependencies.push_back(leftVector);
240 modifiedAttributes.push_back(mIntfCurrent);
241}
242
244 Attribute<Matrix>::Ptr &leftVector) {}
245
247
248}
249
250// #### Math functions ####
251
254 Real Jn = 0;
255 for (Int k = 0; k <= k_max; ++k) {
256 Real Jn_k = pow(-1, k) / (double)factorial(k) * multInvFactorial(k + n) *
257 pow(x / 2., 2. * k + n);
258 Jn = Jn + Jn_k;
259 }
260 return Jn;
261}
262
265 Real Jn = 0;
266 for (Int k = 0; k <= k_max; ++k) {
267 Real Jn_k = pow(-1, k) / mFactorials[k] * mMultInvFactorials[k + n] *
268 pow(x / 2., 2. * k + n);
269 Jn = Jn + Jn_k;
270 }
271 return Jn;
272}
273
275 return (n == 1 || n == 0) ? 1 : factorial(n - 1) * n;
276}
277
279 if (n < 0)
280 return 0;
281 else
282 return 1. / (double)factorial(n);
283}
284
286 if (n <= 0)
287 return 0;
288 else
289 return 1. / std::tgamma(n);
290}
std::vector< Ptr > List
Definition Attribute.h:123
AttributePointer< Attribute< T > > Ptr
Definition Attribute.h:249
void execute(Real time, Int timeStepCount)
void execute(Real time, Int timeStepCount)
Int mMaxModulHarm
Maximum number of modulation signal harmonics.
void mnaCompInitializeHarm(Real omega, Real timeStep, std::vector< Attribute< Matrix >::Ptr > leftVectors) override
void setParameters(const std::vector< Int > &carrierHarms, const std::vector< Int > &modulHarms, Real inputVoltage, Real ratio, Real phase)
void mnaCompPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
std::vector< Int > mModHarms
Vector of modulation signal harmonics.
void mnaCompApplySystemMatrixStampHarm(SparseMatrixRow &systemMatrix, Int freqIdx) override
Real multInvIntGamma(Real n) const
Real mModIdx
Modulation Index.
void mnaCompAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override
Add MNA pre step dependencies.
Real mFreqMod
system frequency (should be updated every step)
void mnaCompApplyRightSideVectorStampHarm(Matrix &rightVector) override
void mnaCompApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) override
Stamps system matrix.
Real multInvFactorial(Int n) const
UInt mHarNum
Number of harmonics.
Int mMaxCarrierHarm
Maximum number of carrier signal harmonics.
Matrix mPhasorPhases
Vector of phasor phases.
Real mPhaseCar
Carrier phase.
void initialize(Matrix frequencies) override
Initialize components with correct network frequencies.
Real besselFirstKind_n(Int n, Int k_max, Real x) const
Bessel function.
Real besselFirstKind_n_opt(Int n, Int k_max, Real x)
Bessel function using look up tables for factorials.
Inverter(String name, String uid, Logger::Level logLevel=Logger::Level::off)
Defines UID, name and logging level.
std::map< Int, Real > mMultInvFactorials
Real mFreqCar
switching frequency (constant)
Matrix mPhasorMags
Vector of phasor magnitudes.
Real mVin
DC bus voltage.
void mnaCompApplyRightSideVectorStamp(Matrix &rightVector) override
Stamps right side (source) vector.
void initializeFromNodesAndTerminals(Real frequency) override
Initializes Component variables according to power flow data stored in Nodes.
std::vector< Real > mFactorials
long long factorial(Int n) const
Int mMaxBesselSumIdx
Maximum upper limit for Bessel function 1st kind summation.
void mnaCompPreStep(Real time, Int timeStepCount) override
Real mPhaseMod
Modulation phase.
std::vector< Int > mCarHarms
Vector of carrier signal harmonics.
Matrix mPhasorFreqs
Vector of phasor frequencies.
void mnaCompInitialize(Real omega, Real timeStep, Attribute< Matrix >::Ptr leftVector) override
Initializes internal variables of the component.
Real mVfund
voltage part of system fundamental
void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
Add MNA post step dependencies.
String uid()
Returns unique id.
spdlog::level::level_enum Level
Definition Logger.h:33
static String complexToString(const Complex &num)
Definition Logger.cpp:63
MNASimPowerComp(String uid, String name, Bool hasPreStep, Bool hasPostStep, Logger::Level logLevel)
Attribute< Matrix >::Ptr mRightVector
static void setVectorElement(Matrix &mat, Matrix::Index row, Complex value, Int maxFreq=1, Int freqIdx=0, Matrix::Index colOffset=0)
static void setMatrixElement(SparseMatrixRow &mat, Matrix::Index row, Matrix::Index column, Complex value, Int maxFreq=1, Int freqIdx=0)
UInt matrixNodeIndex(UInt nodeIndex)
const Attribute< MatrixVar< Complex > >::Ptr mIntfCurrent
virtual void initialize(Matrix frequencies)
Initialize components with correct network frequencies.
const Attribute< MatrixVar< Complex > >::Ptr mIntfVoltage
SimNode< Complex >::List mVirtualNodes
Bool terminalNotGrounded(UInt index)
bool mParametersSet
Flag indicating that parameters are set via setParameters() function.
Logger::Log mSLog
Component logger.
#define PI
Definition Definitions.h:43
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
unsigned int UInt
Definition Definitions.h:58
Eigen::SparseMatrix< Real, Eigen::RowMajor > SparseMatrixRow
Sparse matrix for real numbers (row major).
Definition Definitions.h:72