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
56namespace CPS {
57
58// ### Types ###
59typedef unsigned int UInt;
60typedef int Int;
61typedef double Real;
62typedef std::complex<Real> Complex;
63typedef bool Bool;
64typedef std::string String;
65
67typedef Eigen::Matrix<Complex, Eigen::Dynamic, 1> VectorComp;
69typedef Eigen::Matrix<Real, Eigen::Dynamic, 1> Vector;
71typedef Eigen::SparseMatrix<Real, Eigen::ColMajor> SparseMatrix;
73typedef Eigen::SparseMatrix<Real, Eigen::RowMajor> SparseMatrixRow;
75typedef Eigen::SparseMatrix<Complex, Eigen::ColMajor> SparseMatrixComp;
77typedef Eigen::SparseMatrix<Complex, Eigen::RowMajor> SparseMatrixCompRow;
79typedef Eigen::Matrix<Real, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>
80 Matrix;
82typedef Eigen::Matrix<Complex, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>
83 MatrixComp;
85typedef Eigen::Matrix<Int, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>
86 MatrixInt;
88typedef Eigen::Matrix<Real, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>
89 MatrixRow;
91typedef Eigen::PartialPivLU<Matrix> LUFactorized;
93typedef Eigen::SparseLU<SparseMatrix> LUFactorizedSparse;
95template <typename VarType>
96using MatrixVar =
97 Eigen::Matrix<VarType, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>;
99template <int rows, int cols>
100using MatrixFixedSize = Eigen::Matrix<Real, rows, cols, Eigen::ColMajor>;
102template <int rows, int cols>
103using MatrixFixedSizeComp = Eigen::Matrix<Complex, rows, cols, Eigen::ColMajor>;
104
105// ### Constants ###
107static const Complex jComp(0.0, 1.0);
109
110// ### Enumerations ###
111enum class NumericalMethod { Euler, Trapezoidal };
112enum class PhaseType { A, B, C, ABC, Single };
113enum class Domain { SP, DP, EMT };
114enum class PowerflowBusType { PV, PQ, VD, None };
115enum class GeneratorType {
116 PVNode,
117 IdealVoltageSource,
118 IdealCurrentSource,
119 TransientStability,
120 FullOrder,
121 FullOrderVBR,
122 SG6aOrderVBR,
123 SG6bOrderVBR,
124 SG5OrderVBR,
125 SG4OrderVBR,
126 SG3OrderVBR,
127 SG4OrderPCM,
128 SG4OrderTPM,
129 SG6OrderPCM,
130 None
131};
132enum class SGOrder { SG3Order, SG4Order, SG5Order, SG6aOrder, SG6bOrder };
133
134// ### Exceptions ###
135class Exception : public std::exception {};
136class AccessException : public Exception {};
137class TypeException : public Exception {};
140
141class SystemError {
142protected:
143 std::error_code mErrorCode;
144 String mDescription;
145
146public:
147 SystemError(const String &desc, int e)
148 : mErrorCode(e, std::system_category()), mDescription(desc){};
149
150 SystemError(const String &desc)
151 : mErrorCode(errno, std::system_category()), mDescription(desc){};
152
153 SystemError() : mErrorCode(errno, std::system_category()){};
154
155 String descr() const {
156 std::ostringstream os;
157
158 os << "System Error: " << mDescription << ": " << mErrorCode.message()
159 << "(" << mErrorCode.value() << ")";
160
161 return os.str();
162 };
163};
164} // namespace CPS