DPsim
Loading...
Searching...
No Matches
Definitions.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 <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 P_SNUB_TRANSFORMER 1e-3 // 0.1% of rated power
54#define Q_SNUB_TRANSFORMER 5e-4 // 0.05% of rated power
55#define DOUBLE_EPSILON 1E-12
56
57namespace CPS {
58
59// ### Types ###
60typedef unsigned int UInt;
61typedef int Int;
62typedef double Real;
63typedef std::complex<Real> Complex;
64typedef bool Bool;
65typedef std::string String;
66
68typedef Eigen::Matrix<Complex, Eigen::Dynamic, 1> VectorComp;
70typedef Eigen::Matrix<Real, Eigen::Dynamic, 1> Vector;
72typedef Eigen::SparseMatrix<Real, Eigen::ColMajor> SparseMatrix;
74typedef Eigen::SparseMatrix<Real, Eigen::RowMajor> SparseMatrixRow;
76typedef Eigen::SparseMatrix<Complex, Eigen::ColMajor> SparseMatrixComp;
78typedef Eigen::SparseMatrix<Complex, Eigen::RowMajor> SparseMatrixCompRow;
80typedef Eigen::Matrix<Real, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>
81 Matrix;
83typedef Eigen::Matrix<Complex, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>
84 MatrixComp;
86typedef Eigen::Matrix<Int, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>
87 MatrixInt;
89typedef Eigen::Matrix<Real, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>
90 MatrixRow;
92typedef Eigen::PartialPivLU<Matrix> LUFactorized;
94typedef Eigen::SparseLU<SparseMatrix> LUFactorizedSparse;
96template <typename VarType>
97using MatrixVar =
98 Eigen::Matrix<VarType, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>;
100template <int rows, int cols>
101using MatrixFixedSize = Eigen::Matrix<Real, rows, cols, Eigen::ColMajor>;
103template <int rows, int cols>
104using MatrixFixedSizeComp = Eigen::Matrix<Complex, rows, cols, Eigen::ColMajor>;
105
106// ### Constants ###
108static const Complex jComp(0.0, 1.0);
110
111// ### Enumerations ###
112enum class NumericalMethod { Euler, Trapezoidal };
113enum class PhaseType { A, B, C, ABC, Single };
114enum class Domain { SP, DP, EMT };
115enum class PowerflowBusType { PV, PQ, VD, None };
116enum class GeneratorType {
117 PVNode,
118 IdealVoltageSource,
119 IdealCurrentSource,
120 TransientStability,
121 FullOrder,
122 FullOrderVBR,
123 SG6aOrderVBR,
124 SG6bOrderVBR,
125 SG5OrderVBR,
126 SG4OrderVBR,
127 SG3OrderVBR,
128 SG4OrderPCM,
129 SG4OrderTPM,
130 SG6OrderPCM,
131 None
132};
133enum class SGOrder { SG3Order, SG4Order, SG5Order, SG6aOrder, SG6bOrder };
134
135// ### Exceptions ###
136class Exception : public std::exception {};
137class AccessException : public Exception {};
138class TypeException : public Exception {};
141
142class SystemError {
143protected:
144 std::error_code mErrorCode;
145 String mDescription;
146
147public:
148 SystemError(const String &desc, int e)
149 : mErrorCode(e, std::system_category()), mDescription(desc){};
150
151 SystemError(const String &desc)
152 : mErrorCode(errno, std::system_category()), mDescription(desc){};
153
154 SystemError() : mErrorCode(errno, std::system_category()){};
155
156 String descr() const {
157 std::ostringstream os;
158
159 os << "System Error: " << mDescription << ": " << mErrorCode.message()
160 << "(" << mErrorCode.value() << ")";
161
162 return os.str();
163 };
164};
165} // namespace CPS