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;
97
100 CPS::IdentifiedObject::List mTearComponents = CPS::IdentifiedObject::List();
106 Bool mFreqParallel = false;
108 Bool mInitialized = false;
109
110 // #### Initialization ####
114 Real mSteadStIniAccLimit = 0.0001;
115
116 // #### Task dependencies und scheduling ####
118 std::shared_ptr<Scheduler> mScheduler;
120 CPS::Task::List mTasks;
123
125 std::vector<Interface::Ptr> mInterfaces;
126
129 DataLogger::Ptr logger;
132 };
133
135 DataLoggerInterface::List mLoggers;
136
138 void create();
140 template <typename VarType> void createSolvers();
142 template <typename VarType> void createMNASolver();
144 void prepSchedule();
145
148
149public:
151 CPS::Logger::Log mLog;
152
154 Simulation(String name, CommandLineArgs &args);
155
157 Simulation(String name,
158 CPS::Logger::Level logLevel = CPS::Logger::Level::info);
159
161 virtual ~Simulation() {}
162
163 // #### Simulation Settings ####
165 void setSystem(const CPS::SystemTopology &system) { mSystem = system; }
167 void setTimeStep(Real timeStep) { **mTimeStep = timeStep; }
169 void setFinalTime(Real finalTime) { **mFinalTime = finalTime; }
171 void setDomain(CPS::Domain domain = CPS::Domain::DP) { mDomain = domain; }
173 void setSolverType(Solver::Type solverType = Solver::Type::MNA) {
174 mSolverType = solverType;
175 }
177 void setSolverAndComponentBehaviour(Solver::Behaviour behaviour) {
178 mSolverBehaviour = behaviour;
179 }
180
181 void setDirectLinearSolverImplementation(DirectLinearSolverImpl directImpl) {
182 mDirectImpl = directImpl;
183 }
185 void setDirectLinearSolverConfiguration(
186 const DirectLinearSolverConfiguration &configuration) {
187 mDirectLinearSolverConfiguration = configuration;
188 }
190 void setMaxNumberOfIterations(int maxIterations) {
191 mMaxIterations = maxIterations;
192 }
194 void doInitFromNodesAndTerminals(Bool f = true) {
195 mInitFromNodesAndTerminals = f;
196 }
198 void doSplitSubnets(Bool splitSubnets = true) {
199 **mSplitSubnets = splitSubnets;
200 }
202 void setTearingComponents(CPS::IdentifiedObject::List tearComponents =
203 CPS::IdentifiedObject::List()) {
204 mTearComponents = tearComponents;
205 }
207 void setScheduler(const std::shared_ptr<Scheduler> &scheduler) {
208 mScheduler = scheduler;
209 }
210
211 void doFrequencyParallelization(Bool value) { mFreqParallel = value; }
213 void doSystemMatrixRecomputation(Bool value) {
215 }
218 void setLogStepTimes(Bool f) { mLogStepTimes = f; }
219
220 // #### Initialization ####
222 void doSteadyStateInit(Bool f) { **mSteadyStateInit = f; }
227
228 // #### Simulation Control ####
230 void initialize();
232 void start();
234 void stop();
236 Real next();
238 void run();
240 virtual Real step();
242 void sync() const;
244 void schedule();
245
247 void addEvent(Event::Ptr e) { mEvents.addEvent(e); }
249 void addLogger(DataLoggerInterface::Ptr logger) {
250 mLoggers.push_back(logger);
251 }
252
253 void logStepTimes(String logName);
255 void checkForOverruns(String logName);
256
258 void logLUTimes();
259
261 void addInterface(Interface::Ptr eint) {
262 eint->setLogger(mLog);
263 mInterfaces.push_back(eint);
264 }
265
266#ifdef WITH_GRAPHVIZ
268 CPS::Graph::Graph dependencyGraph();
269#endif
270
271 // #### Getter ####
272 String name() const { return **mName; }
273 Real time() const { return mTime; }
274 Real finalTime() const { return **mFinalTime; }
275 Int timeStepCount() const { return mTimeStepCount; }
276 Real timeStep() const { return **mTimeStep; }
277 DataLogger::List &loggers() { return mLoggers; }
278 std::shared_ptr<Scheduler> scheduler() { return mScheduler; }
279 std::vector<Real> &stepTimes() { return mStepTimes; }
280
281 // #### Set component attributes during simulation ####
283 // void setIdObjAttr(const String &comp, const String &attr, Real value);
284 // void setIdObjAttr(const String &comp, const String &attr, Complex value);
285
286 // #### Get component attributes during simulation ####
287 CPS::AttributeBase::Ptr getIdObjAttribute(const String &comp,
288 const String &attr);
289
290 void logIdObjAttribute(const String &comp, const String &attr);
292 void logAttribute(String name, CPS::AttributeBase::Ptr attr);
293};
294} // 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:211
void setSteadStIniAccLimit(Real v)
set steady state initialization accuracy limit
Definition Simulation.h:226
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:100
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:122
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:224
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:249
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:207
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:177
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:112
virtual ~Simulation()
Desctructor.
Definition Simulation.h:161
CPS::Task::List mTasks
List of all tasks to be scheduled.
Definition Simulation.h:120
const CPS::Attribute< Bool >::Ptr mSplitSubnets
Definition Simulation.h:48
std::shared_ptr< Scheduler > mScheduler
Scheduler used for task scheduling.
Definition Simulation.h:118
void setLogStepTimes(Bool f)
Definition Simulation.h:218
std::vector< Interface::Ptr > mInterfaces
Vector of Interfaces.
Definition Simulation.h:125
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:222
void addEvent(Event::Ptr e)
Schedule an event in the simulation.
Definition Simulation.h:247
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:114
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:135
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:151
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:129