DPsim
DP_Ph1_Inverter.h
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 #pragma once
10 
11 #include <map>
12 
13 #include <dpsim-models/MNASimPowerComp.h>
14 #include <dpsim-models/Solver/MNAInterface.h>
15 
16 namespace CPS {
17 namespace DP {
18 namespace Ph1 {
22 class Inverter : public MNASimPowerComp<Complex>,
23  public SharedFactory<Inverter> {
24 protected:
25  // #### Model specific variables ####
27  Real mVin = 360;
29  Real mFreqMod = 50;
31  Real mOmMod = 2. * PI * mFreqMod;
33  Real mFreqCar = 10e3;
35  Real mOmCar = 2. * PI * mFreqCar;
37  Real mModIdx = 0.87;
38  //mMr = sqrt(2)*mV_grid/mV_in;
40  Real mPhaseCar = 0;
42  Real mPhaseMod = 0;
43 
45  UInt mHarNum;
47  Int mMaxCarrierHarm = 2;
49  Int mMaxModulHarm = 3;
51  UInt mCarHarNum;
53  UInt mModHarNum;
55  MatrixInt mHarmMap;
57  Int mMaxBesselSumIdx = 20;
59  std::vector<Int> mCarHarms;
61  std::vector<Int> mModHarms;
62 
64  Real mVfund = 0;
66  Matrix mPhasorFreqs;
68  Matrix mPhasorMags;
70  Matrix mPhasorPhases;
72  std::vector<Real> mFactorials;
74  std::map<Int, Real> mMultInvFactorials;
75 
76  void generateFrequencies();
77 
78  // #### Math functions ####
79 
81  Real besselFirstKind_n(Int n, Int k_max, Real x) const;
83  Real besselFirstKind_n_opt(Int n, Int k_max, Real x);
84  long long factorial(Int n) const;
85  Real multInvFactorial(Int n) const;
86  Real multInvIntGamma(Real n) const;
87 
88 public:
90  Inverter(String name, String uid,
91  Logger::Level logLevel = Logger::Level::off);
93  Inverter(String name, Logger::Level logLevel = Logger::Level::off)
94  : Inverter(name, name, logLevel) {}
95 
96  // #### General ####
98  void initializeFromNodesAndTerminals(Real frequency) override;
100  void initialize(Matrix frequencies) override;
102  void setParameters(const std::vector<Int> &carrierHarms,
103  const std::vector<Int> &modulHarms, Real inputVoltage,
104  Real ratio, Real phase);
106  void calculatePhasors();
107 
108  // #### MNA Functions ####
110  void mnaCompInitialize(Real omega, Real timeStep,
111  Attribute<Matrix>::Ptr leftVector) override;
112  void mnaCompInitializeHarm(
113  Real omega, Real timeStep,
114  std::vector<Attribute<Matrix>::Ptr> leftVectors) override;
116  void mnaCompApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) override;
117  void mnaCompApplySystemMatrixStampHarm(SparseMatrixRow &systemMatrix,
118  Int freqIdx) override;
120  void mnaCompApplyRightSideVectorStamp(Matrix &rightVector) override;
121  void mnaCompApplyRightSideVectorStampHarm(Matrix &rightVector) override;
122  void mnaCompApplyRightSideVectorStampHarm(Matrix &sourceVector,
123  Int freqIdx) override;
126  AttributeBase::List &prevStepDependencies,
127  AttributeBase::List &attributeDependencies,
128  AttributeBase::List &modifiedAttributes) override;
130  void
131  mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies,
132  AttributeBase::List &attributeDependencies,
133  AttributeBase::List &modifiedAttributes,
134  Attribute<Matrix>::Ptr &leftVector) override;
135  void mnaCompPreStep(Real time, Int timeStepCount) override;
136  void mnaCompPostStep(Real time, Int timeStepCount,
137  Attribute<Matrix>::Ptr &leftVector) override;
138 
139  class MnaPreStepHarm : public CPS::Task {
140  public:
141  MnaPreStepHarm(Inverter &inverter)
142  : Task(**inverter.mName + ".MnaPreStepHarm"), mInverter(inverter) {
143  mModifiedAttributes.push_back(mInverter.attribute("right_vector"));
144  mModifiedAttributes.push_back(mInverter.attribute("v_intf"));
145  }
146  void execute(Real time, Int timeStepCount);
147 
148  private:
149  Inverter &mInverter;
150  };
151 
152  class MnaPostStepHarm : public CPS::Task {
153  public:
154  MnaPostStepHarm(Inverter &inverter,
155  const std::vector<Attribute<Matrix>::Ptr> &leftVectors)
156  : Task(**inverter.mName + ".MnaPostStepHarm"), mInverter(inverter),
157  mLeftVectors(leftVectors) {
158  for (UInt i = 0; i < mLeftVectors.size(); i++)
159  mAttributeDependencies.push_back(mLeftVectors[i]);
160  mModifiedAttributes.push_back(mInverter.attribute("i_intf"));
161  }
162  void execute(Real time, Int timeStepCount);
163 
164  private:
165  Inverter &mInverter;
166  std::vector<Attribute<Matrix>::Ptr> mLeftVectors;
167  };
168 };
169 } // namespace Ph1
170 } // namespace DP
171 } // namespace CPS
Single phase inverter model.
Int mMaxModulHarm
Maximum number of modulation signal harmonics.
std::vector< Int > mModHarms
Vector of modulation signal harmonics.
Real mModIdx
Modulation Index.
Real mOmCar
switching angular frequency
void mnaCompAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override
Add MNA pre step dependencies.
Inverter(String name, Logger::Level logLevel=Logger::Level::off)
Defines name and logging level.
Real mFreqMod
system frequency (should be updated every step)
void mnaCompApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) override
Stamps system matrix.
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.
Real mOmMod
system angular frequency
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.
Int mMaxBesselSumIdx
Maximum upper limit for Bessel function 1st kind summation.
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.
const Attribute< String >::Ptr mName
Human readable name.
String uid()
Returns unique id.
AttributeBase::Ptr attribute(const String &name) const
Return pointer to an attribute.
Base class for all MNA components that are transmitting power.
Tasks to be defined by every component.
Definition: Task.h:25