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