DPsim
Loading...
Searching...
No Matches
Definitions.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 <cerrno>
12#include <cmath>
13#include <complex>
14#include <exception>
15#include <memory>
16#include <string>
17#include <system_error>
18
19#ifdef __clang__
20#elif defined(__GNUC__)
21#pragma GCC diagnostic push
22#if __GNUC__ >= 7
23#pragma GCC diagnostic ignored \
24 "-Wint-in-bool-context" // https://eigen.tuxfamily.org/bz/show_bug.cgi?id=1402
25#endif
26#endif
27
28#include <Eigen/Dense>
29#include <Eigen/Sparse>
30
31#ifdef __clang__
32#elif defined(__GNUC__)
33#pragma GCC diagnostic pop
34#endif
35
36// VS2017 doesn't define M_PI unless _USE_MATH_DEFINES is included before cmath
37// which is hard to guarantee, so we make sure it is defined here
38#define DPS_PI PI
39#ifndef PI
40#ifndef M_PI
41#define M_PI 3.14159265358979323846
42#endif
43#define PI M_PI
44#endif
45
46#define SHIFT_TO_PHASE_B Complex(cos(-2 * M_PI / 3), sin(-2 * M_PI / 3))
47#define SHIFT_TO_PHASE_C Complex(cos(2 * M_PI / 3), sin(2 * M_PI / 3))
48#define RMS_TO_PEAK sqrt(2.)
49#define PEAK_TO_RMS sqrt(1 / 2.)
50#define RMS3PH_TO_PEAK1PH sqrt(2. / 3.)
51#define PEAK1PH_TO_RMS3PH sqrt(3. / 2.)
52
53#define DOUBLE_EPSILON 1E-12
54
55namespace CPS {
56
57// ### Types ###
58typedef unsigned int UInt;
59typedef int Int;
60typedef double Real;
61typedef std::complex<Real> Complex;
62typedef bool Bool;
63typedef std::string String;
64
66typedef Eigen::Matrix<Complex, Eigen::Dynamic, 1> VectorComp;
68typedef Eigen::Matrix<Real, Eigen::Dynamic, 1> Vector;
70typedef Eigen::SparseMatrix<Real, Eigen::ColMajor> SparseMatrix;
72typedef Eigen::SparseMatrix<Real, Eigen::RowMajor> SparseMatrixRow;
74typedef Eigen::SparseMatrix<Complex, Eigen::ColMajor> SparseMatrixComp;
76typedef Eigen::SparseMatrix<Complex, Eigen::RowMajor> SparseMatrixCompRow;
78typedef Eigen::Matrix<Real, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>
81typedef Eigen::Matrix<Complex, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>
84typedef Eigen::Matrix<Int, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>
87typedef Eigen::Matrix<Real, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>
90typedef Eigen::PartialPivLU<Matrix> LUFactorized;
92typedef Eigen::SparseLU<SparseMatrix> LUFactorizedSparse;
94template <typename VarType>
95using MatrixVar =
96 Eigen::Matrix<VarType, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>;
98template <int rows, int cols>
99using MatrixFixedSize = Eigen::Matrix<Real, rows, cols, Eigen::ColMajor>;
101template <int rows, int cols>
102using MatrixFixedSizeComp = Eigen::Matrix<Complex, rows, cols, Eigen::ColMajor>;
103
104// ### Constants ###
106static const Complex jComp(0.0, 1.0);
108
109// ### Enumerations ###
111enum class PhaseType { A, B, C, ABC, Single, DC };
112enum class Domain { SP, DP, EMT };
116enum class PowerflowBusType { PV, PQ, VD, None };
135
137
138// ### Exceptions ###
139class Exception : public std::exception {};
140class AccessException : public Exception {};
141class TypeException : public Exception {};
144
146protected:
147 std::error_code mErrorCode;
149
150public:
151 SystemError(const String &desc, int e)
152 : mErrorCode(e, std::system_category()), mDescription(desc){};
153
154 SystemError(const String &desc)
155 : mErrorCode(errno, std::system_category()), mDescription(desc){};
156
157 SystemError() : mErrorCode(errno, std::system_category()){};
158
159 String descr() const {
160 std::ostringstream os;
161
162 os << "System Error: " << mDescription << ": " << mErrorCode.message()
163 << "(" << mErrorCode.value() << ")";
164
165 return os.str();
166 };
167};
168} // namespace CPS
String descr() const
std::error_code mErrorCode
SystemError(const String &desc, int e)
SystemError(const String &desc)
PowerflowBusType
Eigen::SparseMatrix< Real, Eigen::ColMajor > SparseMatrix
Sparse matrix for real numbers.
Definition Definitions.h:70
Eigen::Matrix< Real, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > Matrix
Dense matrix for real numbers.
Definition Definitions.h:79
std::string String
Definition Definitions.h:63
Eigen::SparseMatrix< Complex, Eigen::ColMajor > SparseMatrixComp
Sparse matrix for complex numbers.
Definition Definitions.h:74
Eigen::Matrix< Int, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > MatrixInt
Dense matrix for integers.
Definition Definitions.h:85
Eigen::PartialPivLU< Matrix > LUFactorized
Definition Definitions.h:90
Eigen::SparseMatrix< Complex, Eigen::RowMajor > SparseMatrixCompRow
Sparse matrix for complex numbers (row major).
Definition Definitions.h:76
Eigen::Matrix< Real, rows, cols, Eigen::ColMajor > MatrixFixedSize
Dense matrix for real numbers with fixed dimension.
Definition Definitions.h:99
double Real
Definition Definitions.h:60
Eigen::Matrix< Complex, Eigen::Dynamic, 1 > VectorComp
Dense vector for complex numbers.
Definition Definitions.h:66
CouplingMethod
Eigen::Matrix< Real, Eigen::Dynamic, 1 > Vector
Dense vector for real numbers.
Definition Definitions.h:68
Eigen::Matrix< Real, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > MatrixRow
Definition Definitions.h:88
int Int
Definition Definitions.h:59
Eigen::Matrix< VarType, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > MatrixVar
Definition Definitions.h:95
std::complex< Real > Complex
Definition Definitions.h:61
WindingReference
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
GeneratorType
unsigned int UInt
Definition Definitions.h:58
Eigen::Matrix< Complex, rows, cols, Eigen::ColMajor > MatrixFixedSizeComp
Dense matrix for complex numbers with fixed dimension.
Eigen::SparseLU< SparseMatrix > LUFactorizedSparse
Definition Definitions.h:92
NumericalMethod
Eigen::SparseMatrix< Real, Eigen::RowMajor > SparseMatrixRow
Sparse matrix for real numbers (row major).
Definition Definitions.h:72