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
48 const CPS::Attribute<Real>::Ptr mPFBaseApparentPowerFallback;
49
51 const CPS::Attribute<CPS::UInt>::Ptr mPFMaxIterations;
52
56 const CPS::Attribute<Bool>::Ptr mPFSolverUseSparse;
57
60 const CPS::Attribute<Bool>::Ptr mPFEnforceReactiveLimits;
61
63 const CPS::Attribute<Real>::Ptr mPFBaseVoltageLooseTolerance;
64
66 const CPS::Attribute<Real>::Ptr mPFBaseVoltageStrictTolerance;
67
72 const CPS::Attribute<Bool>::Ptr mSplitSubnets;
73
77 const CPS::Attribute<Bool>::Ptr mSteadyStateInit;
78
79protected:
81 Real mTime = 0;
88
90 std::chrono::time_point<std::chrono::steady_clock> mSimulationStartTimePoint;
92 std::chrono::time_point<std::chrono::steady_clock> mSimulationEndTimePoint;
94 std::chrono::duration<double> mSimulationCalculationTime;
95
96 // #### Logging ####
98 CPS::Logger::Level mLogLevel;
100 std::vector<Real> mStepTimes;
102 Bool mLogStepTimes = true;
103
104 // #### Solver Settings ####
106 CPS::Domain mDomain = CPS::Domain::DP;
108 Solver::Type mSolverType = Solver::Type::MNA;
110 Solver::Behaviour mSolverBehaviour = Solver::Behaviour::Simulation;
112 Solver::List mSolvers;
114 DirectLinearSolverImpl mDirectImpl = DirectLinearSolverImpl::Undef;
116 DirectLinearSolverConfiguration mDirectLinearSolverConfiguration;
118 Bool mInitFromNodesAndTerminals = true;
123
126 CPS::IdentifiedObject::List mTearComponents = CPS::IdentifiedObject::List();
132 Bool mFreqParallel = false;
134 Bool mInitialized = false;
135
136 // #### Initialization ####
140 Real mSteadStIniAccLimit = 0.0001;
141
142 // #### Task dependencies und scheduling ####
144 std::shared_ptr<Scheduler> mScheduler;
146 CPS::Task::List mTasks;
149
151 std::vector<Interface::Ptr> mInterfaces;
152
155 DataLogger::Ptr logger;
158 };
159
161 DataLoggerInterface::List mLoggers;
162
164 void create();
166 template <typename VarType> void createSolvers();
168 template <typename VarType> void createMNASolver();
170 void prepSchedule();
171
174
175public:
177 CPS::Logger::Log mLog;
178
180 Simulation(String name, CommandLineArgs &args);
181
183 Simulation(String name,
184 CPS::Logger::Level logLevel = CPS::Logger::Level::info);
185
187 virtual ~Simulation() {}
188
189 // #### Simulation Settings ####
191 void setSystem(const CPS::SystemTopology &system) { mSystem = system; }
193 void setTimeStep(Real timeStep) { **mTimeStep = timeStep; }
195 void setFinalTime(Real finalTime) { **mFinalTime = finalTime; }
197 void setDomain(CPS::Domain domain = CPS::Domain::DP) { mDomain = domain; }
199 void setSolverType(Solver::Type solverType = Solver::Type::MNA) {
200 mSolverType = solverType;
201 }
203 void setSolverAndComponentBehaviour(Solver::Behaviour behaviour) {
204 mSolverBehaviour = behaviour;
205 }
206
207 void setDirectLinearSolverImplementation(DirectLinearSolverImpl directImpl) {
208 mDirectImpl = directImpl;
209 }
211 void setDirectLinearSolverConfiguration(
212 const DirectLinearSolverConfiguration &configuration) {
213 mDirectLinearSolverConfiguration = configuration;
214 }
216 void setMaxNumberOfIterations(int maxIterations) {
217 mMaxIterations = maxIterations;
218 }
220 void doInitFromNodesAndTerminals(Bool f = true) {
221 mInitFromNodesAndTerminals = f;
222 }
224 void doSplitSubnets(Bool splitSubnets = true) {
225 **mSplitSubnets = splitSubnets;
226 }
228 void setTearingComponents(CPS::IdentifiedObject::List tearComponents =
229 CPS::IdentifiedObject::List()) {
230 mTearComponents = tearComponents;
231 }
233 void setScheduler(const std::shared_ptr<Scheduler> &scheduler) {
234 mScheduler = scheduler;
235 }
236
237 void doFrequencyParallelization(Bool value) { mFreqParallel = value; }
239 void doSystemMatrixRecomputation(Bool value) {
241 }
243 void doStateSpaceExtraction(Bool value = true) {
244 mStateSpaceExtraction = value;
245 }
246
248 void setLogStepTimes(Bool f) { mLogStepTimes = f; }
249
250 // #### Initialization ####
252 void doSteadyStateInit(Bool f) { **mSteadyStateInit = f; }
257
258 void setPFKeepLastSolution(Bool value);
259
260 Bool getPFKeepLastSolution() const;
261
262 void setPFBaseApparentPowerFallback(Real value);
263
264 Real getPFBaseApparentPowerFallback() const;
265
266 void setPFMaxIterations(CPS::UInt value);
267
268 CPS::UInt getPFMaxIterations() const;
269
270 void setPFSolverUseSparse(Bool value);
271
272 Bool getPFSolverUseSparse() const;
273
274 void setPFSolverEnforceReactiveLimits(Bool value);
275
276 Bool getPFSolverEnforceReactiveLimits() const;
277
278 void setPFSolverBaseVoltageLooseTolerance(Real tolerance);
279
280 Real getPFSolverBaseVoltageLooseTolerance() const;
281
282 void setPFSolverBaseVoltageStrictTolerance(Real tolerance);
283
284 Real getPFSolverBaseVoltageStrictTolerance() const;
285
286 // #### Simulation Control ####
288 void initialize();
290 void start();
292 void stop();
294 Real next();
296 void run();
298 virtual Real step();
300 void sync() const;
302 void schedule();
303
305 void addEvent(Event::Ptr e) { mEvents.addEvent(e); }
307 void addLogger(DataLoggerInterface::Ptr logger) {
308 mLoggers.push_back(logger);
309 }
310
311 void logStepTimes(String logName);
313 void checkForOverruns(String logName);
314
316 void logLUTimes();
317
319 void addInterface(Interface::Ptr eint) {
320 eint->setLogger(mLog);
321 mInterfaces.push_back(eint);
322 }
323
324#ifdef WITH_GRAPHVIZ
326 CPS::Graph::Graph dependencyGraph();
327#endif
328
329 // #### Getter ####
330 String name() const { return **mName; }
331 Real time() const { return mTime; }
332 Real finalTime() const { return **mFinalTime; }
333 Int timeStepCount() const { return mTimeStepCount; }
334 Real timeStep() const { return **mTimeStep; }
335 DataLogger::List &loggers() { return mLoggers; }
336 std::shared_ptr<Scheduler> scheduler() { return mScheduler; }
337 std::vector<Real> &stepTimes() { return mStepTimes; }
342 const MNAStateSpaceExtractor &
343 getStateSpaceExtractor(UInt solverIndex = 0) const;
344
345 // #### Set component attributes during simulation ####
347 // void setIdObjAttr(const String &comp, const String &attr, Real value);
348 // void setIdObjAttr(const String &comp, const String &attr, Complex value);
349
350 // #### Get component attributes during simulation ####
351 CPS::AttributeBase::Ptr getIdObjAttribute(const String &comp,
352 const String &attr);
353
354 void logIdObjAttribute(const String &comp, const String &attr);
356 void logAttribute(String name, CPS::AttributeBase::Ptr attr);
357};
358} // 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:237
Bool mStateSpaceExtraction
Enable extraction of the MNA-coupled discrete-time state matrix.
Definition Simulation.h:122
void setSteadStIniAccLimit(Real v)
set steady state initialization accuracy limit
Definition Simulation.h:256
std::chrono::duration< double > mSimulationCalculationTime
Measured calculation time for simulation.
Definition Simulation.h:94
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:126
CPS::Logger::Level mLogLevel
Simulation log level.
Definition Simulation.h:98
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:148
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:254
const CPS::Attribute< CPS::UInt >::Ptr mPFMaxIterations
Maximum number of Newton-Raphson iterations for the PF solver.
Definition Simulation.h:51
const CPS::Attribute< Bool >::Ptr mSteadyStateInit
Definition Simulation.h:77
const CPS::Attribute< Real >::Ptr mPFBaseVoltageStrictTolerance
Strict tolerance between authoritative base-voltage sources in a zone; default 0.01.
Definition Simulation.h:66
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:307
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:233
std::vector< Real > mStepTimes
(Real) time needed for the timesteps
Definition Simulation.h:100
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:203
Int mTimeStepCount
Number of step which have been executed for this simulation.
Definition Simulation.h:83
const CPS::Attribute< Real >::Ptr mPFBaseApparentPowerFallback
Definition Simulation.h:48
Real mSteadStIniTimeLimit
steady state initialization time limit
Definition Simulation.h:138
virtual ~Simulation()
Desctructor.
Definition Simulation.h:187
CPS::Task::List mTasks
List of all tasks to be scheduled.
Definition Simulation.h:146
const CPS::Attribute< Bool >::Ptr mSplitSubnets
Definition Simulation.h:72
std::shared_ptr< Scheduler > mScheduler
Scheduler used for task scheduling.
Definition Simulation.h:144
void setLogStepTimes(Bool f)
Definition Simulation.h:248
std::vector< Interface::Ptr > mInterfaces
Vector of Interfaces.
Definition Simulation.h:151
Bool mLogStepTimes
activate collection of step times
Definition Simulation.h:102
void create()
Helper function for constructors.
CPS::SystemTopology mSystem
System list.
Definition Simulation.h:87
void checkForOverruns(String logName)
Check for overruns.
Simulation(String name, CommandLineArgs &args)
Creates simulation with name and CommandLineArgs.
const CPS::Attribute< Bool >::Ptr mPFEnforceReactiveLimits
Definition Simulation.h:60
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:85
void doSteadyStateInit(Bool f)
activate steady state initialization
Definition Simulation.h:252
void addEvent(Event::Ptr e)
Schedule an event in the simulation.
Definition Simulation.h:305
void doStateSpaceExtraction(Bool value=true)
Enable extraction of the MNA-coupled discrete-time state matrix.
Definition Simulation.h:243
void schedule()
Create the schedule for the independent tasks.
const CPS::Attribute< Bool >::Ptr mPFSolverUseSparse
Definition Simulation.h:56
std::chrono::time_point< std::chrono::steady_clock > mSimulationStartTimePoint
Start time point to measure calculation time.
Definition Simulation.h:90
Real mTime
Time variable that is incremented at every step.
Definition Simulation.h:81
Bool mSystemMatrixRecomputation
Enable recomputation of system matrix during simulation.
Definition Simulation.h:120
Real mSteadStIniAccLimit
steady state initialization accuracy limit
Definition Simulation.h:140
const CPS::Attribute< Real >::Ptr mPFBaseVoltageLooseTolerance
Loose tolerance for a zone's Load base-voltage vs. its rating; default 0.1.
Definition Simulation.h:63
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:161
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:177
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:92
DataLogger::Ptr logger
Simulation data logger.
Definition Simulation.h:155