Simulation.h 5.08 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/** The simulation
 *
 * @file
 * @author Markus Mirz <mmirz@eonerc.rwth-aachen.de>
 * @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
 * @license GNU General Public License (version 3)
 *
 * DPsim
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *********************************************************************************/

24
#pragma once
Markus Mirz's avatar
Markus Mirz committed
25
26
27

#include <iostream>
#include <vector>
28

29
//#include "Config.h"
30
31
32
33
34

#ifdef WITH_RT
  #include <signal.h>
#endif

35
#include "Definitions.h"
Markus Mirz's avatar
Markus Mirz committed
36
#include "Components.h"
37
#include "Logger.h"
38
#include "SystemModel.h"
39
#include "ExternalInterface.h"
Markus Mirz's avatar
Markus Mirz committed
40

41
namespace DPsim {
42
	typedef std::shared_ptr<BaseComponent> ElementPtr;
Markus Mirz's avatar
Markus Mirz committed
43
	typedef std::vector<ElementPtr> ElementList;
44
45
46

	struct switchConfiguration {
		Real switchTime;
47
		UInt systemIndex;
48
49
	};

50
51
52
53
54
55
	/* Possible methods to achieve execution in real time. */
	enum RTMethod {
		RTExceptions, // use a normal timer and throw an exception in the signal handler if the timestep wasn't completed yet
		RTTimerFD,    // read on a timerfd after every step
	};

56
57
58
	class TimerExpiredException {
	};

59
60
	class Simulation {

61
	private:
62
		Logger* mLogger;
63
64
65
66
		/// Final time of the simulation
		Real mFinalTime;
		/// Time variable that is incremented at every step
		Real mTime;
Markus Mirz's avatar
Markus Mirz committed
67
68
69
70
		/// Last simulation time step when log was updated
		Int mLastLogTimeStep;
		/// Down sampling rate
		Int mDownSampleRate;
71
72
73
74
75
76
		/// Index of the next switching
		UInt mCurrentSwitchTimeIndex;
		/// Vector of switch times
		std::vector<switchConfiguration> mSwitchEventVector;
		/// Structure that holds all system information.
		SystemModel mSystemModel;
Markus Mirz's avatar
Markus Mirz committed
77
78
		/// Stores a list of circuit elements that are used to generate the system matrix
		ElementList mElements;
79
		/// Circuit list vector
Markus Mirz's avatar
Markus Mirz committed
80
		std::vector<ElementList> mElementsVector;
81
82
83
		/// Vector of ExternalInterfaces
		std::vector<ExternalInterface*> mExternalInterfaces;

84
		uint64_t mRtTimerCount = 0;
85

86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
		bool FirstTime = true;
		bool ClearingFault = false;
		bool aCleared = false;
		bool bCleared = false;
		bool cCleared = false;
		Int NumClearedPhases = 0;

		/// Fault Current phase a
		Real mIfa;
		/// Fault Current phase b
		Real mIfb;
		/// Fault Current phase c
		Real mIfc;

		/// Fault Current phase a last time step
		Real mIfa_hist;
		/// Fault Current phase b
		Real mIfb_hist;
		/// Fault Current phase c
		Real mIfc_hist;

Steffen Vogel's avatar
Steffen Vogel committed
107
	public:
108
		/// Sets parameters to default values.
109
		Simulation();
Steffen Vogel's avatar
Steffen Vogel committed
110
		/// Creates system matrix according to
111
		Simulation(ElementList elements, Real om, Real dt, Real tf, Logger& logger, SimulationType simType = SimulationType::DynPhasor, Int downSampleRate = 1);
112
113
		~Simulation();

Markus Mirz's avatar
Markus Mirz committed
114
115
		/// TODO: check that every system matrix has the same dimensions
		void initialize(ElementList elements);
116
		/// Solve system A * x = z for x and current time
Markus Mirz's avatar
Markus Mirz committed
117
		Int step(bool blocking = true);
118
		/// Solve system A * x = z for x and current time. Log current values of both vectors.
Markus Mirz's avatar
Markus Mirz committed
119
		Int step(Logger& leftSideVectorLog, Logger& rightSideVectorLog, bool blocking = true);
Markus Mirz's avatar
Markus Mirz committed
120
		void switchSystemMatrix(Int systemMatrixIndex);
121
		void setSwitchTime(Real switchTime, Int systemIndex);
122
		void increaseByTimeStep();
123
		void addExternalInterface(ExternalInterface*);
124
		void clearFault(Real Node1, Real Node2, Real Node3);
125

126
127
		void setNumericalMethod(NumericalMethod numMethod);

Markus Mirz's avatar
Markus Mirz committed
128
129
130
		Real getTime() { return mTime; }
		Real getFinalTime() { return mFinalTime; }
		Real getTimeStep() { return mSystemModel.getTimeStep(); }
131
132
		Matrix & getLeftSideVector() { return mSystemModel.getLeftSideVector(); }
		Matrix & getRightSideVector() { return mSystemModel.getRightSideVector(); }
133
		Matrix & getSystemMatrix() { return mSystemModel.getCurrentSystemMatrix(); }
Markus Mirz's avatar
Markus Mirz committed
134
		Int stepGeneratorTest(Logger& leftSideVectorLog, Logger& rightSideVectorLog,
Markus Mirz's avatar
Markus Mirz committed
135
			ElementPtr generator, Real time);
Markus Mirz's avatar
Markus Mirz committed
136
		Int stepGeneratorVBR(Logger& leftSideVectorLog, Logger& rightSideVectorLog,
Markus Mirz's avatar
Markus Mirz committed
137
			ElementPtr generator, Real time);
138

Markus Mirz's avatar
Markus Mirz committed
139
		void addSystemTopology(ElementList newElements);
140

141
#ifdef WITH_RT
142
143
144
145
146
147
148
149
		/* Perform the main simulation loop in real time.
		 *
		 * @param rtMethod The method with which the realtime execution is achieved.
		 * @param startSynch If true, the simulation waits for the first external value before starting the timing.
		 * @param logger Logger which is used to log general information.
		 * @param llogger Logger which is used to log the left-side (solution).
		 * @param rlogger Logger which is used to log the right-side vector.
		 */
150
151
		void runRT(RTMethod rtMethod, bool startSynch, Logger& logger, Logger& llogger, Logger &rlogger);
		static void alarmHandler(int, siginfo_t*, void*);
152
#endif /* WITH_RT */
153
154
155
	};

}
Markus Mirz's avatar
Markus Mirz committed
156