DPsim
Loading...
Searching...
No Matches
Simulation.h
1// SPDX-License-Identifier: Apache-2.0
2
3#pragma once
4
5#include "dpsim/MNASolverFactory.h"
6#include <vector>
7
8#include <dpsim-models/Attribute.h>
9#include <dpsim-models/Definitions.h>
10#include <dpsim-models/Logger.h>
11#include <dpsim-models/SimNode.h>
12#include <dpsim-models/SystemTopology.h>
13#include <dpsim/Config.h>
14#include <dpsim/DataLogger.h>
15#include <dpsim/Event.h>
16#include <dpsim/Interface.h>
17#include <dpsim/Scheduler.h>
18#include <dpsim/Solver.h>
19
20#ifdef WITH_GRAPHVIZ
21#include <dpsim-models/Graph.h>
22#endif
23
24namespace DPsim {
26class CommandLineArgs;
27
32public:
33 typedef std::shared_ptr<Simulation> Ptr;
34
36 const CPS::Attribute<String>::Ptr mName;
40 const CPS::Attribute<Real>::Ptr mFinalTime;
42 const CPS::Attribute<Real>::Ptr mTimeStep;
43
48 const CPS::Attribute<Bool>::Ptr mSplitSubnets;
49
53 const CPS::Attribute<Bool>::Ptr mSteadyStateInit;
54
55protected:
57 Real mTime = 0;
64
66 std::chrono::time_point<std::chrono::steady_clock> mSimulationStartTimePoint;
68 std::chrono::time_point<std::chrono::steady_clock> mSimulationEndTimePoint;
70 std::chrono::duration<double> mSimulationCalculationTime;
71
72 // #### Logging ####
74 CPS::Logger::Level mLogLevel;
76 std::vector<Real> mStepTimes;
78 Bool mLogStepTimes = true;
79
80 // #### Solver Settings ####
82 CPS::Domain mDomain = CPS::Domain::DP;
84 Solver::Type mSolverType = Solver::Type::MNA;
86 Solver::Behaviour mSolverBehaviour = Solver::Behaviour::Simulation;
88 Solver::List mSolvers;
90 DirectLinearSolverImpl mDirectImpl = DirectLinearSolverImpl::Undef;
92 DirectLinearSolverConfiguration mDirectLinearSolverConfiguration;
94 Bool mInitFromNodesAndTerminals = true;
99
102 CPS::IdentifiedObject::List mTearComponents = CPS::IdentifiedObject::List();
108 Bool mFreqParallel = false;
110 Bool mInitialized = false;
111
112 // #### Initialization ####
116 Real mSteadStIniAccLimit = 0.0001;
117
118 // #### Task dependencies und scheduling ####
120 std::shared_ptr<Scheduler> mScheduler;
122 CPS::Task::List mTasks;
125
127 std::vector<Interface::Ptr> mInterfaces;
128
131 DataLogger::Ptr logger;
134 };
135
137 DataLoggerInterface::List mLoggers;
138
140 void create();
142 template <typename VarType> void createSolvers();
144 template <typename VarType> void createMNASolver();
146 void prepSchedule();
147
150
151public:
153 CPS::Logger::Log mLog;
154
156 Simulation(String name, CommandLineArgs &args);
157
159 Simulation(String name,
160 CPS::Logger::Level logLevel = CPS::Logger::Level::info);
161
163 virtual ~Simulation() {}
164
165 // #### Simulation Settings ####
167 void setSystem(const CPS::SystemTopology &system) { mSystem = system; }
169 void setTimeStep(Real timeStep) { **mTimeStep = timeStep; }
171 void setFinalTime(Real finalTime) { **mFinalTime = finalTime; }
173 void setDomain(CPS::Domain domain = CPS::Domain::DP) { mDomain = domain; }
175 void setSolverType(Solver::Type solverType = Solver::Type::MNA) {
176 mSolverType = solverType;
177 }
179 void setSolverAndComponentBehaviour(Solver::Behaviour behaviour) {
180 mSolverBehaviour = behaviour;
181 }
182
183 void setDirectLinearSolverImplementation(DirectLinearSolverImpl directImpl) {
184 mDirectImpl = directImpl;
185 }
187 void setDirectLinearSolverConfiguration(
188 const DirectLinearSolverConfiguration &configuration) {
189 mDirectLinearSolverConfiguration = configuration;
190 }
192 void setMaxNumberOfIterations(int maxIterations) {
193 mMaxIterations = maxIterations;
194 }
196 void doInitFromNodesAndTerminals(Bool f = true) {
197 mInitFromNodesAndTerminals = f;
198 }
200 void doSplitSubnets(Bool splitSubnets = true) {
201 **mSplitSubnets = splitSubnets;
202 }
204 void setTearingComponents(CPS::IdentifiedObject::List tearComponents =
205 CPS::IdentifiedObject::List()) {
206 mTearComponents = tearComponents;
207 }
209 void setScheduler(const std::shared_ptr<Scheduler> &scheduler) {
210 mScheduler = scheduler;
211 }
212
213 void doFrequencyParallelization(Bool value) { mFreqParallel = value; }
215 void doSystemMatrixRecomputation(Bool value) {
217 }
219 void doStateSpaceExtraction(Bool value = true) {
220 mStateSpaceExtraction = value;
221 }
222
224 void setLogStepTimes(Bool f) { mLogStepTimes = f; }
225
226 // #### Initialization ####
228 void doSteadyStateInit(Bool f) { **mSteadyStateInit = f; }
233
234 // #### Simulation Control ####
236 void initialize();
238 void start();
240 void stop();
242 Real next();
244 void run();
246 virtual Real step();
248 void sync() const;
250 void schedule();
251
253 void addEvent(Event::Ptr e) { mEvents.addEvent(e); }
255 void addLogger(DataLoggerInterface::Ptr logger) {
256 mLoggers.push_back(logger);
257 }
258
259 void logStepTimes(String logName);
261 void checkForOverruns(String logName);
262
264 void logLUTimes();
265
267 void addInterface(Interface::Ptr eint) {
268 eint->setLogger(mLog);
269 mInterfaces.push_back(eint);
270 }
271
272#ifdef WITH_GRAPHVIZ
274 CPS::Graph::Graph dependencyGraph();
275#endif
276
277 // #### Getter ####
278 String name() const { return **mName; }
279 Real time() const { return mTime; }
280 Real finalTime() const { return **mFinalTime; }
281 Int timeStepCount() const { return mTimeStepCount; }
282 Real timeStep() const { return **mTimeStep; }
283 DataLogger::List &loggers() { return mLoggers; }
284 std::shared_ptr<Scheduler> scheduler() { return mScheduler; }
285 std::vector<Real> &stepTimes() { return mStepTimes; }
290 const MNAStateSpaceExtractor &
291 getStateSpaceExtractor(UInt solverIndex = 0) const;
292
293 // #### Set component attributes during simulation ####
295 // void setIdObjAttr(const String &comp, const String &attr, Real value);
296 // void setIdObjAttr(const String &comp, const String &attr, Complex value);
297
298 // #### Get component attributes during simulation ####
299 CPS::AttributeBase::Ptr getIdObjAttribute(const String &comp,
300 const String &attr);
301
302 void logIdObjAttribute(const String &comp, const String &attr);
304 void logAttribute(String name, CPS::AttributeBase::Ptr attr);
305};
306} // namespace DPsim
Base class of objects having attributes to access member variables.
std::unordered_map< CPS::Task::Ptr, std::deque< CPS::Task::Ptr > > Edges
Definition Scheduler.h:31
void doFrequencyParallelization(Bool value)
Compute phasors of different frequencies in parallel.
Definition Simulation.h:213
Bool mStateSpaceExtraction
Enable extraction of the MNA-coupled discrete-time state matrix.
Definition Simulation.h:98
void setSteadStIniAccLimit(Real v)
set steady state initialization accuracy limit
Definition Simulation.h:232
std::chrono::duration< double > mSimulationCalculationTime
Measured calculation time for simulation.
Definition Simulation.h:70
void logLUTimes()
Write LU decomposition times measurements to log file.
void sync() const
Synchronize simulation with remotes by exchanging intial state over interfaces.
CPS::IdentifiedObject::List mTearComponents
Definition Simulation.h:102
CPS::Logger::Level mLogLevel
Simulation log level.
Definition Simulation.h:74
virtual Real step()
Solve system A * x = z for x and current time.
Scheduler::Edges mTaskInEdges
Task dependencies as incoming / outgoing edges.
Definition Simulation.h:124
const CPS::Attribute< Real >::Ptr mTimeStep
Simulation timestep.
Definition Simulation.h:42
void setSteadStIniTimeLimit(Real v)
set steady state initialization time limit
Definition Simulation.h:230
const CPS::Attribute< Bool >::Ptr mSteadyStateInit
Definition Simulation.h:53
Real next()
Run until next time step.
void logStepTimes(String logName)
Write step time measurements to log file.
void addLogger(DataLoggerInterface::Ptr logger)
Add a new data logger.
Definition Simulation.h:255
String mSolverPluginName
If there we use a solver plugin, this specifies its name (excluding .so)
Definition Simulation.h:38
void initialize()
Create solver instances etc.
void setScheduler(const std::shared_ptr< Scheduler > &scheduler)
Set the scheduling method.
Definition Simulation.h:209
std::vector< Real > mStepTimes
(Real) time needed for the timesteps
Definition Simulation.h:76
const CPS::Attribute< Real >::Ptr mFinalTime
Final time of the simulation.
Definition Simulation.h:40
void prepSchedule()
Prepare schedule for simulation.
void setSolverAndComponentBehaviour(Solver::Behaviour behaviour)
set solver and component to initialization or simulation behaviour
Definition Simulation.h:179
Int mTimeStepCount
Number of step which have been executed for this simulation.
Definition Simulation.h:59
Real mSteadStIniTimeLimit
steady state initialization time limit
Definition Simulation.h:114
virtual ~Simulation()
Desctructor.
Definition Simulation.h:163
CPS::Task::List mTasks
List of all tasks to be scheduled.
Definition Simulation.h:122
const CPS::Attribute< Bool >::Ptr mSplitSubnets
Definition Simulation.h:48
std::shared_ptr< Scheduler > mScheduler
Scheduler used for task scheduling.
Definition Simulation.h:120
void setLogStepTimes(Bool f)
Definition Simulation.h:224
std::vector< Interface::Ptr > mInterfaces
Vector of Interfaces.
Definition Simulation.h:127
Bool mLogStepTimes
activate collection of step times
Definition Simulation.h:78
void create()
Helper function for constructors.
CPS::SystemTopology mSystem
System list.
Definition Simulation.h:63
void checkForOverruns(String logName)
Check for overruns.
Simulation(String name, CommandLineArgs &args)
Creates simulation with name and CommandLineArgs.
CPS::AttributeBase::Ptr getIdObjAttribute(const String &comp, const String &attr)
CHECK: Can these be deleted? getIdObjAttribute + "**attr =" should suffice.
void start()
Start simulation without advancing in time.
EventQueue mEvents
The simulation event queue.
Definition Simulation.h:61
void doSteadyStateInit(Bool f)
activate steady state initialization
Definition Simulation.h:228
void addEvent(Event::Ptr e)
Schedule an event in the simulation.
Definition Simulation.h:253
void doStateSpaceExtraction(Bool value=true)
Enable extraction of the MNA-coupled discrete-time state matrix.
Definition Simulation.h:219
void schedule()
Create the schedule for the independent tasks.
std::chrono::time_point< std::chrono::steady_clock > mSimulationStartTimePoint
Start time point to measure calculation time.
Definition Simulation.h:66
Real mTime
Time variable that is incremented at every step.
Definition Simulation.h:57
Bool mSystemMatrixRecomputation
Enable recomputation of system matrix during simulation.
Definition Simulation.h:96
Real mSteadStIniAccLimit
steady state initialization accuracy limit
Definition Simulation.h:116
void createMNASolver()
Subroutine for MNA only because there are many MNA options.
void run()
Run simulation until total time is elapsed.
void logAttribute(String name, CPS::AttributeBase::Ptr attr)
CHECK: Can we store the attribute name / UID intrinsically inside the attribute?
DataLoggerInterface::List mLoggers
The data loggers.
Definition Simulation.h:137
const MNAStateSpaceExtractor & getStateSpaceExtractor(UInt solverIndex=0) const
void createSolvers()
Create solvers depending on simulation settings.
const CPS::Attribute< String >::Ptr mName
Simulation name.
Definition Simulation.h:36
CPS::Logger::Log mLog
Simulation logger.
Definition Simulation.h:153
void stop()
Stop simulation including scheduler and interfaces.
std::chrono::time_point< std::chrono::steady_clock > mSimulationEndTimePoint
End time point to measure calculation time.
Definition Simulation.h:68
DataLogger::Ptr logger
Simulation data logger.
Definition Simulation.h:131