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
44 const CPS::Attribute<Bool>::Ptr mPFKeepLastSolution;
45
49 const CPS::Attribute<Bool>::Ptr mPFSolverUseSparse;
50
55 const CPS::Attribute<Bool>::Ptr mSplitSubnets;
56
60 const CPS::Attribute<Bool>::Ptr mSteadyStateInit;
61
62protected:
64 Real mTime = 0;
71
73 std::chrono::time_point<std::chrono::steady_clock> mSimulationStartTimePoint;
75 std::chrono::time_point<std::chrono::steady_clock> mSimulationEndTimePoint;
77 std::chrono::duration<double> mSimulationCalculationTime;
78
79 // #### Logging ####
81 CPS::Logger::Level mLogLevel;
83 std::vector<Real> mStepTimes;
85 Bool mLogStepTimes = true;
86
87 // #### Solver Settings ####
89 CPS::Domain mDomain = CPS::Domain::DP;
91 Solver::Type mSolverType = Solver::Type::MNA;
93 Solver::Behaviour mSolverBehaviour = Solver::Behaviour::Simulation;
95 Solver::List mSolvers;
97 DirectLinearSolverImpl mDirectImpl = DirectLinearSolverImpl::Undef;
99 DirectLinearSolverConfiguration mDirectLinearSolverConfiguration;
101 Bool mInitFromNodesAndTerminals = true;
106
109 CPS::IdentifiedObject::List mTearComponents = CPS::IdentifiedObject::List();
115 Bool mFreqParallel = false;
117 Bool mInitialized = false;
118
119 // #### Initialization ####
123 Real mSteadStIniAccLimit = 0.0001;
124
125 // #### Task dependencies und scheduling ####
127 std::shared_ptr<Scheduler> mScheduler;
129 CPS::Task::List mTasks;
132
134 std::vector<Interface::Ptr> mInterfaces;
135
138 DataLogger::Ptr logger;
141 };
142
144 DataLoggerInterface::List mLoggers;
145
147 void create();
149 template <typename VarType> void createSolvers();
151 template <typename VarType> void createMNASolver();
153 void prepSchedule();
154
157
158public:
160 CPS::Logger::Log mLog;
161
163 Simulation(String name, CommandLineArgs &args);
164
166 Simulation(String name,
167 CPS::Logger::Level logLevel = CPS::Logger::Level::info);
168
170 virtual ~Simulation() {}
171
172 // #### Simulation Settings ####
174 void setSystem(const CPS::SystemTopology &system) { mSystem = system; }
176 void setTimeStep(Real timeStep) { **mTimeStep = timeStep; }
178 void setFinalTime(Real finalTime) { **mFinalTime = finalTime; }
180 void setDomain(CPS::Domain domain = CPS::Domain::DP) { mDomain = domain; }
182 void setSolverType(Solver::Type solverType = Solver::Type::MNA) {
183 mSolverType = solverType;
184 }
186 void setSolverAndComponentBehaviour(Solver::Behaviour behaviour) {
187 mSolverBehaviour = behaviour;
188 }
189
190 void setDirectLinearSolverImplementation(DirectLinearSolverImpl directImpl) {
191 mDirectImpl = directImpl;
192 }
194 void setDirectLinearSolverConfiguration(
195 const DirectLinearSolverConfiguration &configuration) {
196 mDirectLinearSolverConfiguration = configuration;
197 }
199 void setMaxNumberOfIterations(int maxIterations) {
200 mMaxIterations = maxIterations;
201 }
203 void doInitFromNodesAndTerminals(Bool f = true) {
204 mInitFromNodesAndTerminals = f;
205 }
207 void doSplitSubnets(Bool splitSubnets = true) {
208 **mSplitSubnets = splitSubnets;
209 }
211 void setTearingComponents(CPS::IdentifiedObject::List tearComponents =
212 CPS::IdentifiedObject::List()) {
213 mTearComponents = tearComponents;
214 }
216 void setScheduler(const std::shared_ptr<Scheduler> &scheduler) {
217 mScheduler = scheduler;
218 }
219
220 void doFrequencyParallelization(Bool value) { mFreqParallel = value; }
222 void doSystemMatrixRecomputation(Bool value) {
224 }
226 void doStateSpaceExtraction(Bool value = true) {
227 mStateSpaceExtraction = value;
228 }
229
231 void setLogStepTimes(Bool f) { mLogStepTimes = f; }
232
233 // #### Initialization ####
235 void doSteadyStateInit(Bool f) { **mSteadyStateInit = f; }
240
241 void setPFKeepLastSolution(Bool value);
242
243 Bool getPFKeepLastSolution() const;
244
245 void setPFSolverUseSparse(Bool value);
246
247 Bool getPFSolverUseSparse() const;
248
249 // #### Simulation Control ####
251 void initialize();
253 void start();
255 void stop();
257 Real next();
259 void run();
261 virtual Real step();
263 void sync() const;
265 void schedule();
266
268 void addEvent(Event::Ptr e) { mEvents.addEvent(e); }
270 void addLogger(DataLoggerInterface::Ptr logger) {
271 mLoggers.push_back(logger);
272 }
273
274 void logStepTimes(String logName);
276 void checkForOverruns(String logName);
277
279 void logLUTimes();
280
282 void addInterface(Interface::Ptr eint) {
283 eint->setLogger(mLog);
284 mInterfaces.push_back(eint);
285 }
286
287#ifdef WITH_GRAPHVIZ
289 CPS::Graph::Graph dependencyGraph();
290#endif
291
292 // #### Getter ####
293 String name() const { return **mName; }
294 Real time() const { return mTime; }
295 Real finalTime() const { return **mFinalTime; }
296 Int timeStepCount() const { return mTimeStepCount; }
297 Real timeStep() const { return **mTimeStep; }
298 DataLogger::List &loggers() { return mLoggers; }
299 std::shared_ptr<Scheduler> scheduler() { return mScheduler; }
300 std::vector<Real> &stepTimes() { return mStepTimes; }
305 const MNAStateSpaceExtractor &
306 getStateSpaceExtractor(UInt solverIndex = 0) const;
307
308 // #### Set component attributes during simulation ####
310 // void setIdObjAttr(const String &comp, const String &attr, Real value);
311 // void setIdObjAttr(const String &comp, const String &attr, Complex value);
312
313 // #### Get component attributes during simulation ####
314 CPS::AttributeBase::Ptr getIdObjAttribute(const String &comp,
315 const String &attr);
316
317 void logIdObjAttribute(const String &comp, const String &attr);
319 void logAttribute(String name, CPS::AttributeBase::Ptr attr);
320};
321} // 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:220
Bool mStateSpaceExtraction
Enable extraction of the MNA-coupled discrete-time state matrix.
Definition Simulation.h:105
void setSteadStIniAccLimit(Real v)
set steady state initialization accuracy limit
Definition Simulation.h:239
std::chrono::duration< double > mSimulationCalculationTime
Measured calculation time for simulation.
Definition Simulation.h:77
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:109
CPS::Logger::Level mLogLevel
Simulation log level.
Definition Simulation.h:81
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:131
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:237
const CPS::Attribute< Bool >::Ptr mSteadyStateInit
Definition Simulation.h:60
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:270
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:216
std::vector< Real > mStepTimes
(Real) time needed for the timesteps
Definition Simulation.h:83
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:186
Int mTimeStepCount
Number of step which have been executed for this simulation.
Definition Simulation.h:66
Real mSteadStIniTimeLimit
steady state initialization time limit
Definition Simulation.h:121
virtual ~Simulation()
Desctructor.
Definition Simulation.h:170
CPS::Task::List mTasks
List of all tasks to be scheduled.
Definition Simulation.h:129
const CPS::Attribute< Bool >::Ptr mSplitSubnets
Definition Simulation.h:55
std::shared_ptr< Scheduler > mScheduler
Scheduler used for task scheduling.
Definition Simulation.h:127
void setLogStepTimes(Bool f)
Definition Simulation.h:231
std::vector< Interface::Ptr > mInterfaces
Vector of Interfaces.
Definition Simulation.h:134
Bool mLogStepTimes
activate collection of step times
Definition Simulation.h:85
void create()
Helper function for constructors.
CPS::SystemTopology mSystem
System list.
Definition Simulation.h:70
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:68
void doSteadyStateInit(Bool f)
activate steady state initialization
Definition Simulation.h:235
void addEvent(Event::Ptr e)
Schedule an event in the simulation.
Definition Simulation.h:268
void doStateSpaceExtraction(Bool value=true)
Enable extraction of the MNA-coupled discrete-time state matrix.
Definition Simulation.h:226
void schedule()
Create the schedule for the independent tasks.
const CPS::Attribute< Bool >::Ptr mPFSolverUseSparse
Definition Simulation.h:49
std::chrono::time_point< std::chrono::steady_clock > mSimulationStartTimePoint
Start time point to measure calculation time.
Definition Simulation.h:73
Real mTime
Time variable that is incremented at every step.
Definition Simulation.h:64
Bool mSystemMatrixRecomputation
Enable recomputation of system matrix during simulation.
Definition Simulation.h:103
Real mSteadStIniAccLimit
steady state initialization accuracy limit
Definition Simulation.h:123
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:144
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:160
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:75
DataLogger::Ptr logger
Simulation data logger.
Definition Simulation.h:138