DPsim
MathUtils.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 <dpsim-models/Definitions.h>
12 
13 namespace CPS {
14 
15 class Math {
16 public:
17  typedef Real (*DeriveFnPtr)(Matrix inputs);
18 
19  // #### Angular Operations ####
20  static Real radtoDeg(Real rad);
21 
22  static Real degToRad(Real deg);
23 
24  static Real phase(Complex value);
25 
26  static Real phaseDeg(Complex value);
27 
28  static Real abs(Complex value);
29 
30  static Matrix abs(const MatrixComp &mat);
31 
32  static Matrix phase(const MatrixComp &mat);
33 
34  static Complex polar(Real abs, Real phase);
35  static Complex polarDeg(Real abs, Real phase);
36 
37  // #### Vector Operations ####
38  //
39  // | Re(row,0)_harm1 | Re(row,colOffset)_harm1 |
40  // | Im(row,0)_harm1 | Im(row,colOffset)_harm1 |
41  // | Re(row,0)_harm2 | Re(row,colOffset)_harm2 |
42  // | Im(row,0)_harm2 | Im(row,colOffset)_harm2 |
43 
44  static void setVectorElement(Matrix &mat, Matrix::Index row, Complex value,
45  Int maxFreq = 1, Int freqIdx = 0,
46  Matrix::Index colOffset = 0);
47 
48  static void addToVectorElement(Matrix &mat, Matrix::Index row, Complex value,
49  Int maxFreq = 1, Int freqIdx = 0);
50 
51  static Complex complexFromVectorElement(const Matrix &mat, Matrix::Index row,
52  Int maxFreq = 1, Int freqIdx = 0);
53 
54  static void addToVectorElement(Matrix &mat, Matrix::Index row, Real value);
55  static void setVectorElement(Matrix &mat, Matrix::Index row, Real value);
56 
57  static Real realFromVectorElement(const Matrix &mat, Matrix::Index row);
58 
59  // #### Matric Operations ####
60  //
61  // | Re-Re(row,col)_harm1 | Im-Re(row,col)_harm1 | Interharmonics harm1-harm2
62  // | Re-Im(row,col)_harm1 | Im-Im(row,col)_harm1 | Interharmonics harm1-harm2
63  // | Interharmonics harm1-harm2 | Re(row,col)_harm2 | Re(row,col)_harm2 |
64  // | Interharmonics harm1-harm2 | Im(row,col)_harm2 | Im(row,col)_harm2 |
65  static void setMatrixElement(SparseMatrixRow &mat, Matrix::Index row,
66  Matrix::Index column, Complex value,
67  Int maxFreq = 1, Int freqIdx = 0);
68 
69  static void addToMatrixElement(SparseMatrixRow &mat, Matrix::Index row,
70  Matrix::Index column, Complex value,
71  Int maxFreq = 1, Int freqIdx = 0);
72 
73  static void addToMatrixElement(SparseMatrixRow &mat, Matrix::Index row,
74  Matrix::Index column, Matrix value,
75  Int maxFreq = 1, Int freqIdx = 0);
76 
77  static void setMatrixElement(SparseMatrixRow &mat, Matrix::Index row,
78  Matrix::Index column, Real value);
79 
80  static void addToMatrixElement(SparseMatrixRow &mat, std::vector<UInt> rows,
81  std::vector<UInt> columns, Complex value);
82 
83  static void addToMatrixElement(SparseMatrixRow &mat, Matrix::Index row,
84  Matrix::Index column, Real value);
85  static void addToMatrixElement(SparseMatrixRow &mat, std::vector<UInt> rows,
86  std::vector<UInt> columns, Real value);
87 
88  static void invertMatrix(const Matrix &mat, Matrix &matInv);
89 
90  // #### Integration Methods ####
91  static Matrix StateSpaceTrapezoidal(Matrix states, Matrix A, Matrix B,
92  Real dt, Matrix u_new, Matrix u_old);
93  static Matrix StateSpaceTrapezoidal(Matrix states, Matrix A, Matrix B,
94  Matrix C, Real dt, Matrix u_new,
95  Matrix u_old);
96  static Matrix StateSpaceTrapezoidal(Matrix states, Matrix A, Matrix B,
97  Real dt, Matrix u);
98  static Matrix StateSpaceTrapezoidal(Matrix states, Matrix A, Matrix B,
99  Matrix C, Real dt, Matrix u);
100  static Matrix StateSpaceTrapezoidal(Matrix states, Matrix A, Matrix input,
101  Real dt);
102  static Real StateSpaceTrapezoidal(Real states, Real A, Real B, Real C,
103  Real dt, Real u);
104  static Real StateSpaceTrapezoidal(Real states, Real A, Real B, Real dt,
105  Real u);
106 
107  static Matrix StateSpaceEuler(Matrix states, Matrix A, Matrix B, Real dt,
108  Matrix u);
109  static Matrix StateSpaceEuler(Matrix states, Matrix A, Matrix B, Matrix C,
110  Real dt, Matrix u);
111  static Matrix StateSpaceEuler(Matrix states, Matrix A, Matrix input, Real dt);
112  static Real StateSpaceEuler(Real states, Real A, Real B, Real dt, Real u);
113  static Real StateSpaceEuler(Real states, Real A, Real B, Real C, Real dt,
114  Real u);
115 
117  static void calculateStateSpaceTrapezoidalMatrices(const Matrix &A,
118  const Matrix &B,
119  const Matrix &C,
120  const Real &dt, Matrix &Ad,
121  Matrix &Bd, Matrix &Cd);
123  static Matrix applyStateSpaceTrapezoidalMatrices(const Matrix &Ad,
124  const Matrix &Bd,
125  const Matrix &Cd,
126  const Matrix &statesPrevStep,
127  const Matrix &inputCurrStep,
128  const Matrix &inputPrevStep);
129 
130  static void FFT(std::vector<Complex> &samples);
131 
132  static Complex rotatingFrame2to1(Complex f2, Real theta1, Real theta2);
133 
135  static MatrixComp singlePhaseVariableToThreePhase(Complex var_1ph);
136 
138  static Matrix singlePhaseParameterToThreePhase(Real parameter);
139 
141  static Matrix singlePhasePowerToThreePhase(Real power);
142 };
143 } // namespace CPS
static Matrix singlePhasePowerToThreePhase(Real power)
To convert single phase power to symmetrical three phase.
Definition: MathUtils.cpp:217
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.
Definition: MathUtils.cpp:324
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...
Definition: MathUtils.cpp:341
static Matrix singlePhaseParameterToThreePhase(Real parameter)
To convert single phase parameters to symmetrical three phase ones.
Definition: MathUtils.cpp:211
static MatrixComp singlePhaseVariableToThreePhase(Complex var_1ph)
To convert single phase complex variables (voltages, currents) to symmetrical three phase ones.
Definition: MathUtils.cpp:205