DPsim
Loading...
Searching...
No Matches
MathUtils.h
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#pragma once
10
11#include <utility>
12
14
15namespace CPS {
16
17class Math {
18public:
22 Bool mPrevious;
23
24 public:
25 explicit ScopedZeroSkipping(Bool enabled);
29 };
30
32
33 typedef Real (*DeriveFnPtr)(Matrix inputs);
34
35 // #### Angular Operations ####
36 static Real radtoDeg(Real rad);
37
38 static Real degToRad(Real deg);
39
40 static Real phase(Complex value);
41
42 static Real phaseDeg(Complex value);
43
44 static Real abs(Complex value);
45
46 static Matrix abs(const MatrixComp &mat);
47
48 static Matrix phase(const MatrixComp &mat);
49
50 static Complex polar(Real abs, Real phase);
52
54 template <typename DerivedA, typename DerivedB>
55 static auto elementwiseProduct(const Eigen::MatrixBase<DerivedA> &a,
56 const Eigen::MatrixBase<DerivedB> &b) {
57 return (a.array() * b.array()).matrix();
58 }
59
60 // -Ofast/-ffast-math folds std::isnan/isfinite to false; test the IEEE-754
61 // exponent bits directly instead.
62 static bool isFinite(Real value);
63 static bool isFinite(Complex value);
64
65 // #### Vector Operations ####
66 //
67 // | Re(row,0)_harm1 | Re(row,colOffset)_harm1 |
68 // | Im(row,0)_harm1 | Im(row,colOffset)_harm1 |
69 // | Re(row,0)_harm2 | Re(row,colOffset)_harm2 |
70 // | Im(row,0)_harm2 | Im(row,colOffset)_harm2 |
71
72 static void setVectorElement(Matrix &mat, Matrix::Index row, Complex value,
73 Int maxFreq = 1, Int freqIdx = 0,
74 Matrix::Index colOffset = 0);
75
76 static void addToVectorElement(Matrix &mat, Matrix::Index row, Complex value,
77 Int maxFreq = 1, Int freqIdx = 0);
78
79 static Complex complexFromVectorElement(const Matrix &mat, Matrix::Index row,
80 Int maxFreq = 1, Int freqIdx = 0);
81
82 static void addToVectorElement(Matrix &mat, Matrix::Index row, Real value);
83 static void setVectorElement(Matrix &mat, Matrix::Index row, Real value);
84
85 static Real realFromVectorElement(const Matrix &mat, Matrix::Index row);
86
87 // #### Matric Operations ####
88 //
89 // | Re-Re(row,col)_harm1 | Im-Re(row,col)_harm1 | Interharmonics harm1-harm2
90 // | Re-Im(row,col)_harm1 | Im-Im(row,col)_harm1 | Interharmonics harm1-harm2
91 // | Interharmonics harm1-harm2 | Re(row,col)_harm2 | Re(row,col)_harm2 |
92 // | Interharmonics harm1-harm2 | Im(row,col)_harm2 | Im(row,col)_harm2 |
93 static void setMatrixElement(SparseMatrixRow &mat, Matrix::Index row,
94 Matrix::Index column, Complex value,
95 Int maxFreq = 1, Int freqIdx = 0);
96
97 static void addToMatrixElement(SparseMatrixRow &mat, Matrix::Index row,
98 Matrix::Index column, Complex value,
99 Int maxFreq = 1, Int freqIdx = 0);
100
101 static void addToMatrixElement(SparseMatrixRow &mat, Matrix::Index row,
102 Matrix::Index column, Matrix value,
103 Int maxFreq = 1, Int freqIdx = 0);
104
105 static void addToMatrixElement(SparseMatrixCompRow &mat, Matrix::Index row,
106 Matrix::Index column, Complex value);
107
108 static void setMatrixElement(SparseMatrixRow &mat, Matrix::Index row,
109 Matrix::Index column, Real value);
110
111 static void addToMatrixElement(SparseMatrixRow &mat, std::vector<UInt> rows,
112 std::vector<UInt> columns, Complex value);
113
114 static void addToMatrixElement(SparseMatrixRow &mat, Matrix::Index row,
115 Matrix::Index column, Real value);
116 static void addToMatrixElement(SparseMatrixRow &mat, std::vector<UInt> rows,
117 std::vector<UInt> columns, Real value);
118
121
122 static void invertMatrix(const Matrix &mat, Matrix &matInv);
123
124 // #### Integration Methods ####
126 Real dt, Matrix u_new, Matrix u_old);
128 Matrix C, Real dt, Matrix u_new,
129 Matrix u_old);
131 Real dt, Matrix u);
133 Matrix C, Real dt, Matrix u);
134 static Matrix StateSpaceTrapezoidal(Matrix states, Matrix A, Matrix input,
135 Real dt);
138 static Matrix StateSpaceTrapezoidalNoCopy(const Matrix &states,
139 const Matrix &A, const Matrix &B,
140 Real dt, const Matrix &u_new,
141 const Matrix &u_old);
142 static Matrix StateSpaceTrapezoidalNoCopy(const Matrix &states,
143 const Matrix &A, const Matrix &B,
144 const Matrix &C, Real dt,
145 const Matrix &u_new,
146 const Matrix &u_old);
147 static Matrix StateSpaceTrapezoidalNoCopy(const Matrix &states,
148 const Matrix &A, const Matrix &B,
149 Real dt, const Matrix &u);
150 static Matrix StateSpaceTrapezoidalNoCopy(const Matrix &states,
151 const Matrix &A, const Matrix &B,
152 const Matrix &C, Real dt,
153 const Matrix &u);
154 static Matrix StateSpaceTrapezoidalNoCopy(const Matrix &states,
155 const Matrix &A,
156 const Matrix &input, Real dt);
157 static Real StateSpaceTrapezoidal(Real states, Real A, Real B, Real C,
158 Real dt, Real u);
159 static Real StateSpaceTrapezoidal(Real states, Real A, Real B, Real dt,
160 Real u);
161
162 static Matrix StateSpaceEuler(Matrix states, Matrix A, Matrix B, Real dt,
163 Matrix u);
165 Real dt, Matrix u);
166 static Matrix StateSpaceEuler(Matrix states, Matrix A, Matrix input, Real dt);
167 static Matrix StateSpaceEulerNoCopy(const Matrix &states, const Matrix &A,
168 const Matrix &B, Real dt,
169 const Matrix &u);
170 static Matrix StateSpaceEulerNoCopy(const Matrix &states, const Matrix &A,
171 const Matrix &B, const Matrix &C, Real dt,
172 const Matrix &u);
173 static Matrix StateSpaceEulerNoCopy(const Matrix &states, const Matrix &A,
174 const Matrix &input, Real dt);
175 static Real StateSpaceEuler(Real states, Real A, Real B, Real dt, Real u);
176 static Real StateSpaceEuler(Real states, Real A, Real B, Real C, Real dt,
177 Real u);
178
181 const Matrix &B,
182 const Matrix &C,
183 const Real &dt, Matrix &Ad,
184 Matrix &Bd, Matrix &Cd);
186 const Matrix &B,
187 const Real &dt, Matrix &Ad,
188 Matrix &Bd);
191 const Matrix &Bd,
192 const Matrix &Cd,
193 const Matrix &statesPrevStep,
194 const Matrix &inputCurrStep,
195 const Matrix &inputPrevStep);
196
197 static void FFT(std::vector<Complex> &samples);
198
199 static Complex rotatingFrame2to1(Complex f2, Real theta1, Real theta2);
200
203
206
209
212 static std::pair<Real, Real>
213 pccPowerFromFilterPowerReference(Real pFilterRef, Real qFilterRef, Real rc,
214 Real vGridRmsLL);
215
216 // #### Reference Frame Transformations ####
217
218 static Matrix parkTransformPowerInvariant(Real theta, const Matrix &fabc);
219
221
223 const Matrix &fdq);
224
226};
227} // namespace CPS
ScopedZeroSkipping(const ScopedZeroSkipping &)=delete
ScopedZeroSkipping & operator=(const ScopedZeroSkipping &)=delete
static void clearSparseMatrixValueCache(SparseMatrixRow &mat)
static Complex polar(Real abs, Real phase)
Definition MathUtils.cpp:99
static std::pair< Real, Real > pccPowerFromFilterPowerReference(Real pFilterRef, Real qFilterRef, Real rc, Real vGridRmsLL)
static Matrix singlePhasePowerToThreePhase(Real power)
To convert single phase power to symmetrical three phase.
static Complex polarDeg(Real abs, Real phase)
static Matrix StateSpaceEuler(Matrix states, Matrix A, Matrix B, Real dt, Matrix u)
static Matrix StateSpaceTrapezoidalNoCopy(const Matrix &states, const Matrix &A, const Matrix &B, Real dt, const Matrix &u_new, const Matrix &u_old)
static Complex complexFromVectorElement(const Matrix &mat, Matrix::Index row, Int maxFreq=1, Int freqIdx=0)
static Real degToRad(Real deg)
Definition MathUtils.cpp:65
static Real realFromVectorElement(const Matrix &mat, Matrix::Index row)
static void calculateStateSpaceTrapezoidalMatrices(const Matrix &A, const Matrix &B, const Matrix &C, const Real &dt, Matrix &Ad, Matrix &Bd, Matrix &Cd)
Calculate the discretized state space matrices Ad, Bd, Cd using trapezoidal rule.
static void setVectorElement(Matrix &mat, Matrix::Index row, Complex value, Int maxFreq=1, Int freqIdx=0, Matrix::Index colOffset=0)
static Bool zeroSkippingEnabled()
Definition MathUtils.cpp:60
static void addToVectorElement(Matrix &mat, Matrix::Index row, Complex value, Int maxFreq=1, Int freqIdx=0)
static Complex rotatingFrame2to1(Complex f2, Real theta1, Real theta2)
static auto elementwiseProduct(const Eigen::MatrixBase< DerivedA > &a, const Eigen::MatrixBase< DerivedB > &b)
Elementwise product of two same-shaped Eigen expressions.
Definition MathUtils.h:55
static Matrix inverseParkTransformMatrixPowerInvariant(Real theta)
static Real phaseDeg(Complex value)
Definition MathUtils.cpp:69
Real(* DeriveFnPtr)(Matrix inputs)
Definition MathUtils.h:33
static Real phase(Complex value)
Definition MathUtils.cpp:67
static void setMatrixElement(SparseMatrixRow &mat, Matrix::Index row, Matrix::Index column, Complex value, Int maxFreq=1, Int freqIdx=0)
static Matrix parkTransformPowerInvariant(Real theta, const Matrix &fabc)
static Matrix applyStateSpaceTrapezoidalMatrices(const Matrix &Ad, const Matrix &Bd, const Matrix &Cd, const Matrix &statesPrevStep, const Matrix &inputCurrStep, const Matrix &inputPrevStep)
Apply the trapezoidal based state space matrices Ad, Bd, Cd to get the states at the current time ste...
static Matrix StateSpaceEulerNoCopy(const Matrix &states, const Matrix &A, const Matrix &B, Real dt, const Matrix &u)
static void prepareSparseMatrixValueCache(SparseMatrixRow &mat)
static Matrix singlePhaseParameterToThreePhase(Real parameter)
To convert single phase parameters to symmetrical three phase ones.
static bool isFinite(Real value)
static Real abs(Complex value)
Definition MathUtils.cpp:71
static void addToMatrixElement(SparseMatrixRow &mat, Matrix::Index row, Matrix::Index column, Complex value, Int maxFreq=1, Int freqIdx=0)
static void invertMatrix(const Matrix &mat, Matrix &matInv)
static Matrix inverseParkTransformPowerInvariant(Real theta, const Matrix &fdq)
static Matrix parkTransformMatrixPowerInvariant(Real theta)
static void FFT(std::vector< Complex > &samples)
static Real radtoDeg(Real rad)
Definition MathUtils.cpp:63
static MatrixComp singlePhaseVariableToThreePhase(Complex var_1ph)
To convert single phase complex variables (voltages, currents) to symmetrical three phase ones.
static Matrix StateSpaceTrapezoidal(Matrix states, Matrix A, Matrix B, Real dt, Matrix u_new, Matrix u_old)
Eigen::Matrix< Real, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > Matrix
Dense matrix for real numbers.
Definition Definitions.h:79
Eigen::SparseMatrix< Complex, Eigen::RowMajor > SparseMatrixCompRow
Sparse matrix for complex numbers (row major).
Definition Definitions.h:76
double Real
Definition Definitions.h:60
int Int
Definition Definitions.h:59
std::complex< Real > Complex
Definition Definitions.h:61
Eigen::Matrix< Complex, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > MatrixComp
Dense matrix for complex numbers.
Definition Definitions.h:82
bool Bool
Definition Definitions.h:62
Eigen::SparseMatrix< Real, Eigen::RowMajor > SparseMatrixRow
Sparse matrix for real numbers (row major).
Definition Definitions.h:72