DPsim
Loading...
Searching...
No Matches
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
13namespace CPS {
14
15class Math {
16public:
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 // -Ofast/-ffast-math folds std::isnan/isfinite to false; test the IEEE-754
38 // exponent bits directly instead.
39 static bool isFinite(Real value);
40 static bool isFinite(Complex value);
41
42 // #### Vector Operations ####
43 //
44 // | Re(row,0)_harm1 | Re(row,colOffset)_harm1 |
45 // | Im(row,0)_harm1 | Im(row,colOffset)_harm1 |
46 // | Re(row,0)_harm2 | Re(row,colOffset)_harm2 |
47 // | Im(row,0)_harm2 | Im(row,colOffset)_harm2 |
48
49 static void setVectorElement(Matrix &mat, Matrix::Index row, Complex value,
50 Int maxFreq = 1, Int freqIdx = 0,
51 Matrix::Index colOffset = 0);
52
53 static void addToVectorElement(Matrix &mat, Matrix::Index row, Complex value,
54 Int maxFreq = 1, Int freqIdx = 0);
55
56 static Complex complexFromVectorElement(const Matrix &mat, Matrix::Index row,
57 Int maxFreq = 1, Int freqIdx = 0);
58
59 static void addToVectorElement(Matrix &mat, Matrix::Index row, Real value);
60 static void setVectorElement(Matrix &mat, Matrix::Index row, Real value);
61
62 static Real realFromVectorElement(const Matrix &mat, Matrix::Index row);
63
64 // #### Matric Operations ####
65 //
66 // | Re-Re(row,col)_harm1 | Im-Re(row,col)_harm1 | Interharmonics harm1-harm2
67 // | Re-Im(row,col)_harm1 | Im-Im(row,col)_harm1 | Interharmonics harm1-harm2
68 // | Interharmonics harm1-harm2 | Re(row,col)_harm2 | Re(row,col)_harm2 |
69 // | Interharmonics harm1-harm2 | Im(row,col)_harm2 | Im(row,col)_harm2 |
70 static void setMatrixElement(SparseMatrixRow &mat, Matrix::Index row,
71 Matrix::Index column, Complex value,
72 Int maxFreq = 1, Int freqIdx = 0);
73
74 static void addToMatrixElement(SparseMatrixRow &mat, Matrix::Index row,
75 Matrix::Index column, Complex value,
76 Int maxFreq = 1, Int freqIdx = 0);
77
78 static void addToMatrixElement(SparseMatrixRow &mat, Matrix::Index row,
79 Matrix::Index column, Matrix value,
80 Int maxFreq = 1, Int freqIdx = 0);
81
82 static void setMatrixElement(SparseMatrixRow &mat, Matrix::Index row,
83 Matrix::Index column, Real value);
84
85 static void addToMatrixElement(SparseMatrixRow &mat, std::vector<UInt> rows,
86 std::vector<UInt> columns, Complex value);
87
88 static void addToMatrixElement(SparseMatrixRow &mat, Matrix::Index row,
89 Matrix::Index column, Real value);
90 static void addToMatrixElement(SparseMatrixRow &mat, std::vector<UInt> rows,
91 std::vector<UInt> columns, Real value);
92
93 static void invertMatrix(const Matrix &mat, Matrix &matInv);
94
95 // #### Integration Methods ####
96 static Matrix StateSpaceTrapezoidal(Matrix states, Matrix A, Matrix B,
97 Real dt, Matrix u_new, Matrix u_old);
98 static Matrix StateSpaceTrapezoidal(Matrix states, Matrix A, Matrix B,
99 Matrix C, Real dt, Matrix u_new,
100 Matrix u_old);
101 static Matrix StateSpaceTrapezoidal(Matrix states, Matrix A, Matrix B,
102 Real dt, Matrix u);
103 static Matrix StateSpaceTrapezoidal(Matrix states, Matrix A, Matrix B,
104 Matrix C, Real dt, Matrix u);
105 static Matrix StateSpaceTrapezoidal(Matrix states, Matrix A, Matrix input,
106 Real dt);
107 static Real StateSpaceTrapezoidal(Real states, Real A, Real B, Real C,
108 Real dt, Real u);
109 static Real StateSpaceTrapezoidal(Real states, Real A, Real B, Real dt,
110 Real u);
111
112 static Matrix StateSpaceEuler(Matrix states, Matrix A, Matrix B, Real dt,
113 Matrix u);
114 static Matrix StateSpaceEuler(Matrix states, Matrix A, Matrix B, Matrix C,
115 Real dt, Matrix u);
116 static Matrix StateSpaceEuler(Matrix states, Matrix A, Matrix input, Real dt);
117 static Real StateSpaceEuler(Real states, Real A, Real B, Real dt, Real u);
118 static Real StateSpaceEuler(Real states, Real A, Real B, Real C, Real dt,
119 Real u);
120
122 static void calculateStateSpaceTrapezoidalMatrices(const Matrix &A,
123 const Matrix &B,
124 const Matrix &C,
125 const Real &dt, Matrix &Ad,
126 Matrix &Bd, Matrix &Cd);
127 static void calculateStateSpaceTrapezoidalMatrices(const Matrix &A,
128 const Matrix &B,
129 const Real &dt, Matrix &Ad,
130 Matrix &Bd);
132 static Matrix applyStateSpaceTrapezoidalMatrices(const Matrix &Ad,
133 const Matrix &Bd,
134 const Matrix &Cd,
135 const Matrix &statesPrevStep,
136 const Matrix &inputCurrStep,
137 const Matrix &inputPrevStep);
138
139 static void FFT(std::vector<Complex> &samples);
140
141 static Complex rotatingFrame2to1(Complex f2, Real theta1, Real theta2);
142
144 static MatrixComp singlePhaseVariableToThreePhase(Complex var_1ph);
145
147 static Matrix singlePhaseParameterToThreePhase(Real parameter);
148
150 static Matrix singlePhasePowerToThreePhase(Real power);
151
152 // #### Reference Frame Transformations ####
153
154 static Matrix parkTransformPowerInvariant(Real theta, const Matrix &fabc);
155
156 static Matrix parkTransformMatrixPowerInvariant(Real theta);
157
158 static Matrix inverseParkTransformPowerInvariant(Real theta,
159 const Matrix &fdq);
160
161 static Matrix inverseParkTransformMatrixPowerInvariant(Real theta);
162};
163} // namespace CPS
static Matrix singlePhasePowerToThreePhase(Real power)
To convert single phase power to symmetrical three phase.
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 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 MatrixComp singlePhaseVariableToThreePhase(Complex var_1ph)
To convert single phase complex variables (voltages, currents) to symmetrical three phase ones.