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:
19 typedef Real (*DeriveFnPtr)(Matrix inputs);
20
21 // #### Angular Operations ####
22 static Real radtoDeg(Real rad);
23
24 static Real degToRad(Real deg);
25
26 static Real phase(Complex value);
27
28 static Real phaseDeg(Complex value);
29
30 static Real abs(Complex value);
31
32 static Matrix abs(const MatrixComp &mat);
33
34 static Matrix phase(const MatrixComp &mat);
35
36 static Complex polar(Real abs, Real phase);
38
40 template <typename DerivedA, typename DerivedB>
41 static auto elementwiseProduct(const Eigen::MatrixBase<DerivedA> &a,
42 const Eigen::MatrixBase<DerivedB> &b) {
43 return (a.array() * b.array()).matrix();
44 }
45
46 // -Ofast/-ffast-math folds std::isnan/isfinite to false; test the IEEE-754
47 // exponent bits directly instead.
48 static bool isFinite(Real value);
49 static bool isFinite(Complex value);
50
51 // #### Vector Operations ####
52 //
53 // | Re(row,0)_harm1 | Re(row,colOffset)_harm1 |
54 // | Im(row,0)_harm1 | Im(row,colOffset)_harm1 |
55 // | Re(row,0)_harm2 | Re(row,colOffset)_harm2 |
56 // | Im(row,0)_harm2 | Im(row,colOffset)_harm2 |
57
58 static void setVectorElement(Matrix &mat, Matrix::Index row, Complex value,
59 Int maxFreq = 1, Int freqIdx = 0,
60 Matrix::Index colOffset = 0);
61
62 static void addToVectorElement(Matrix &mat, Matrix::Index row, Complex value,
63 Int maxFreq = 1, Int freqIdx = 0);
64
65 static Complex complexFromVectorElement(const Matrix &mat, Matrix::Index row,
66 Int maxFreq = 1, Int freqIdx = 0);
67
68 static void addToVectorElement(Matrix &mat, Matrix::Index row, Real value);
69 static void setVectorElement(Matrix &mat, Matrix::Index row, Real value);
70
71 static Real realFromVectorElement(const Matrix &mat, Matrix::Index row);
72
73 // #### Matric Operations ####
74 //
75 // | Re-Re(row,col)_harm1 | Im-Re(row,col)_harm1 | Interharmonics harm1-harm2
76 // | Re-Im(row,col)_harm1 | Im-Im(row,col)_harm1 | Interharmonics harm1-harm2
77 // | Interharmonics harm1-harm2 | Re(row,col)_harm2 | Re(row,col)_harm2 |
78 // | Interharmonics harm1-harm2 | Im(row,col)_harm2 | Im(row,col)_harm2 |
79 static void setMatrixElement(SparseMatrixRow &mat, Matrix::Index row,
80 Matrix::Index column, Complex value,
81 Int maxFreq = 1, Int freqIdx = 0);
82
83 static void addToMatrixElement(SparseMatrixRow &mat, Matrix::Index row,
84 Matrix::Index column, Complex value,
85 Int maxFreq = 1, Int freqIdx = 0);
86
87 static void addToMatrixElement(SparseMatrixRow &mat, Matrix::Index row,
88 Matrix::Index column, Matrix value,
89 Int maxFreq = 1, Int freqIdx = 0);
90
91 static void setMatrixElement(SparseMatrixRow &mat, Matrix::Index row,
92 Matrix::Index column, Real value);
93
94 static void addToMatrixElement(SparseMatrixRow &mat, std::vector<UInt> rows,
95 std::vector<UInt> columns, Complex value);
96
97 static void addToMatrixElement(SparseMatrixRow &mat, Matrix::Index row,
98 Matrix::Index column, Real value);
99 static void addToMatrixElement(SparseMatrixRow &mat, std::vector<UInt> rows,
100 std::vector<UInt> columns, Real value);
101
102 static void invertMatrix(const Matrix &mat, Matrix &matInv);
103
104 // #### Integration Methods ####
106 Real dt, Matrix u_new, Matrix u_old);
108 Matrix C, Real dt, Matrix u_new,
109 Matrix u_old);
111 Real dt, Matrix u);
113 Matrix C, Real dt, Matrix u);
114 static Matrix StateSpaceTrapezoidal(Matrix states, Matrix A, Matrix input,
115 Real dt);
116 static Real StateSpaceTrapezoidal(Real states, Real A, Real B, Real C,
117 Real dt, Real u);
118 static Real StateSpaceTrapezoidal(Real states, Real A, Real B, Real dt,
119 Real u);
120
121 static Matrix StateSpaceEuler(Matrix states, Matrix A, Matrix B, Real dt,
122 Matrix u);
124 Real dt, Matrix u);
125 static Matrix StateSpaceEuler(Matrix states, Matrix A, Matrix input, Real dt);
126 static Real StateSpaceEuler(Real states, Real A, Real B, Real dt, Real u);
127 static Real StateSpaceEuler(Real states, Real A, Real B, Real C, Real dt,
128 Real u);
129
132 const Matrix &B,
133 const Matrix &C,
134 const Real &dt, Matrix &Ad,
135 Matrix &Bd, Matrix &Cd);
137 const Matrix &B,
138 const Real &dt, Matrix &Ad,
139 Matrix &Bd);
142 const Matrix &Bd,
143 const Matrix &Cd,
144 const Matrix &statesPrevStep,
145 const Matrix &inputCurrStep,
146 const Matrix &inputPrevStep);
147
148 static void FFT(std::vector<Complex> &samples);
149
150 static Complex rotatingFrame2to1(Complex f2, Real theta1, Real theta2);
151
154
157
160
163 static std::pair<Real, Real>
164 pccPowerFromFilterPowerReference(Real pFilterRef, Real qFilterRef, Real rc,
165 Real vGridRmsLL);
166
167 // #### Reference Frame Transformations ####
168
169 static Matrix parkTransformPowerInvariant(Real theta, const Matrix &fabc);
170
172
174 const Matrix &fdq);
175
177};
178} // namespace CPS
static Complex polar(Real abs, Real phase)
Definition MathUtils.cpp:55
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)
Definition MathUtils.cpp:59
static Matrix StateSpaceEuler(Matrix states, Matrix A, Matrix B, Real dt, Matrix u)
static Complex complexFromVectorElement(const Matrix &mat, Matrix::Index row, Int maxFreq=1, Int freqIdx=0)
Definition MathUtils.cpp:94
static Real degToRad(Real deg)
Definition MathUtils.cpp:21
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)
Definition MathUtils.cpp:73
static void addToVectorElement(Matrix &mat, Matrix::Index row, Complex value, Int maxFreq=1, Int freqIdx=0)
Definition MathUtils.cpp:83
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:41
static Matrix inverseParkTransformMatrixPowerInvariant(Real theta)
static Real phaseDeg(Complex value)
Definition MathUtils.cpp:25
Real(* DeriveFnPtr)(Matrix inputs)
Definition MathUtils.h:19
static Real phase(Complex value)
Definition MathUtils.cpp:23
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 singlePhaseParameterToThreePhase(Real parameter)
To convert single phase parameters to symmetrical three phase ones.
static bool isFinite(Real value)
Definition MathUtils.cpp:63
static Real abs(Complex value)
Definition MathUtils.cpp:27
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:19
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:81
double Real
Definition Definitions.h:62
int Int
Definition Definitions.h:61
std::complex< Real > Complex
Definition Definitions.h:63
Eigen::Matrix< Complex, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > MatrixComp
Dense matrix for complex numbers.
Definition Definitions.h:84
Eigen::SparseMatrix< Real, Eigen::RowMajor > SparseMatrixRow
Sparse matrix for real numbers (row major).
Definition Definitions.h:74