DPsim
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 #include <nlohmann/json.hpp>
20 
21 #ifdef WITH_GRAPHVIZ
22 #include <dpsim-models/Graph.h>
23 #endif
24 
25 using json = nlohmann::json;
26 
27 namespace DPsim {
29 class CommandLineArgs;
30 
35 public:
36  typedef std::shared_ptr<Simulation> Ptr;
37 
46 
52 
57 
58 protected:
60  Real mTime = 0;
62  Int mTimeStepCount = 0;
67 
69  std::chrono::time_point<std::chrono::steady_clock> mSimulationStartTimePoint;
71  std::chrono::time_point<std::chrono::steady_clock> mSimulationEndTimePoint;
73  std::chrono::duration<double> mSimulationCalculationTime;
74 
75  // #### Logging ####
77  CPS::Logger::Level mLogLevel;
79  std::vector<Real> mStepTimes;
80 
81  // #### Solver Settings ####
83  CPS::Domain mDomain = CPS::Domain::DP;
85  Solver::Type mSolverType = Solver::Type::MNA;
87  Solver::Behaviour mSolverBehaviour = Solver::Behaviour::Simulation;
89  Solver::List mSolvers;
91  DirectLinearSolverImpl mDirectImpl = DirectLinearSolverImpl::Undef;
93  DirectLinearSolverConfiguration mDirectLinearSolverConfiguration;
95  Bool mInitFromNodesAndTerminals = true;
98 
101  CPS::IdentifiedObject::List mTearComponents = CPS::IdentifiedObject::List();
107  Bool mFreqParallel = false;
109  Bool mInitialized = false;
110 
111  // #### Initialization ####
115  Real mSteadStIniAccLimit = 0.0001;
116 
117  // #### Task dependencies und scheduling ####
119  std::shared_ptr<Scheduler> mScheduler;
121  CPS::Task::List mTasks;
124 
126  std::vector<Interface::Ptr> mInterfaces;
127 
128  struct LoggerMapping {
130  DataLogger::Ptr logger;
133  };
134 
136  DataLogger::List mLoggers;
137 
139  void create();
141  template <typename VarType> void createSolvers();
143  template <typename VarType> void createMNASolver();
145  void prepSchedule();
146 
148  int mMaxIterations = 10;
149 
150 public:
152  CPS::Logger::Log mLog;
153 
155  Simulation(String name, CommandLineArgs &args);
156 
158  Simulation(String name,
159  CPS::Logger::Level logLevel = CPS::Logger::Level::info);
160 
162  virtual ~Simulation() {}
163 
164  // #### Simulation Settings ####
166  void setSystem(const CPS::SystemTopology &system) { mSystem = system; }
168  void setTimeStep(Real timeStep) { **mTimeStep = timeStep; }
170  void setFinalTime(Real finalTime) { **mFinalTime = finalTime; }
172  void setDomain(CPS::Domain domain = CPS::Domain::DP) { mDomain = domain; }
174  void setSolverType(Solver::Type solverType = Solver::Type::MNA) {
175  mSolverType = solverType;
176  }
178  void setSolverAndComponentBehaviour(Solver::Behaviour behaviour) {
179  mSolverBehaviour = behaviour;
180  }
182  void setDirectLinearSolverImplementation(DirectLinearSolverImpl directImpl) {
183  mDirectImpl = directImpl;
184  }
186  void setDirectLinearSolverConfiguration(
187  const DirectLinearSolverConfiguration &configuration) {
188  mDirectLinearSolverConfiguration = configuration;
189  }
191  void setMaxNumberOfIterations(int maxIterations) {
192  mMaxIterations = maxIterations;
193  }
195  void doInitFromNodesAndTerminals(Bool f = true) {
196  mInitFromNodesAndTerminals = f;
197  }
199  void doSplitSubnets(Bool splitSubnets = true) {
200  **mSplitSubnets = splitSubnets;
201  }
203  void setTearingComponents(CPS::IdentifiedObject::List tearComponents =
204  CPS::IdentifiedObject::List()) {
205  mTearComponents = tearComponents;
206  }
208  void setScheduler(const std::shared_ptr<Scheduler> &scheduler) {
209  mScheduler = scheduler;
210  }
212  void doFrequencyParallelization(Bool value) { mFreqParallel = value; }
214  void doSystemMatrixRecomputation(Bool value) {
216  }
217 
218  // #### Initialization ####
220  void doSteadyStateInit(Bool f) { **mSteadyStateInit = f; }
225 
226  // #### Simulation Control ####
228  void initialize();
230  void start();
232  void stop();
234  Real next();
236  void run();
238  virtual Real step();
240  void sync() const;
242  void schedule();
243 
245  void addEvent(Event::Ptr e) { mEvents.addEvent(e); }
247  void addLogger(DataLogger::Ptr logger) { mLoggers.push_back(logger); }
249  void logStepTimes(String logName);
250 
252  void logLUTimes();
253 
255  void addInterface(Interface::Ptr eint) {
256  eint->setLogger(mLog);
257  mInterfaces.push_back(eint);
258  }
259 
260 #ifdef WITH_GRAPHVIZ
262  CPS::Graph::Graph dependencyGraph();
263 #endif
264 
265  // #### Getter ####
266  String name() const { return **mName; }
267  Real time() const { return mTime; }
268  Real finalTime() const { return **mFinalTime; }
269  Int timeStepCount() const { return mTimeStepCount; }
270  Real timeStep() const { return **mTimeStep; }
271  DataLogger::List &loggers() { return mLoggers; }
272  std::shared_ptr<Scheduler> scheduler() { return mScheduler; }
273  std::vector<Real> &stepTimes() { return mStepTimes; }
274 
275  // #### Set component attributes during simulation ####
277  // void setIdObjAttr(const String &comp, const String &attr, Real value);
278  // void setIdObjAttr(const String &comp, const String &attr, Complex value);
279 
280  // #### Get component attributes during simulation ####
281  CPS::AttributeBase::Ptr getIdObjAttribute(const String &comp,
282  const String &attr);
283 
284  void logIdObjAttribute(const String &comp, const String &attr);
286  void logAttribute(String name, CPS::AttributeBase::Ptr attr);
287 };
288 } // namespace DPsim
Base class of objects having attributes to access member variables.
Definition: AttributeList.h:20
std::unordered_map< CPS::Task::Ptr, std::deque< CPS::Task::Ptr > > Edges
Definition: Scheduler.h:31
The Simulation holds a SystemTopology and a Solver.
Definition: Simulation.h:34
void doFrequencyParallelization(Bool value)
Compute phasors of different frequencies in parallel.
Definition: Simulation.h:212
void setSteadStIniAccLimit(Real v)
set steady state initialization accuracy limit
Definition: Simulation.h:224
std::chrono::duration< double > mSimulationCalculationTime
Measured calculation time for simulation.
Definition: Simulation.h:73
void logLUTimes()
Write LU decomposition times measurements to log file.
Definition: Simulation.cpp:434
void sync() const
Synchronize simulation with remotes by exchanging intial state over interfaces.
Definition: Simulation.cpp:173
CPS::IdentifiedObject::List mTearComponents
Definition: Simulation.h:101
CPS::Logger::Level mLogLevel
Simulation log level.
Definition: Simulation.h:77
virtual Real step()
Solve system A * x = z for x and current time.
Definition: Simulation.cpp:405
Scheduler::Edges mTaskInEdges
Task dependencies as incoming / outgoing edges.
Definition: Simulation.h:123
const CPS::Attribute< Real >::Ptr mTimeStep
Simulation timestep.
Definition: Simulation.h:45
void setSteadStIniTimeLimit(Real v)
set steady state initialization time limit
Definition: Simulation.h:222
DataLogger::List mLoggers
The data loggers.
Definition: Simulation.h:136
const CPS::Attribute< Bool >::Ptr mSteadyStateInit
Definition: Simulation.h:56
Real next()
Run until next time step.
Definition: Simulation.cpp:386
void logStepTimes(String logName)
Write step time measurements to log file.
Definition: Simulation.cpp:420
String mSolverPluginName
If there we use a solver plugin, this specifies its name (excluding .so)
Definition: Simulation.h:41
void initialize()
Create solver instances etc.
Definition: Simulation.cpp:63
void setScheduler(const std::shared_ptr< Scheduler > &scheduler)
Set the scheduling method.
Definition: Simulation.h:208
std::vector< Real > mStepTimes
(Real) time needed for the timesteps
Definition: Simulation.h:79
const CPS::Attribute< Real >::Ptr mFinalTime
Final time of the simulation.
Definition: Simulation.h:43
void prepSchedule()
Prepare schedule for simulation.
Definition: Simulation.cpp:185
void setSolverAndComponentBehaviour(Solver::Behaviour behaviour)
set solver and component to initialization or simulation behaviour
Definition: Simulation.h:178
Int mTimeStepCount
Number of step which have been executed for this simulation.
Definition: Simulation.h:62
Real mSteadStIniTimeLimit
steady state initialization time limit
Definition: Simulation.h:113
virtual ~Simulation()
Desctructor.
Definition: Simulation.h:162
CPS::Task::List mTasks
List of all tasks to be scheduled.
Definition: Simulation.h:121
const CPS::Attribute< Bool >::Ptr mSplitSubnets
Definition: Simulation.h:51
std::shared_ptr< Scheduler > mScheduler
Scheduler used for task scheduling.
Definition: Simulation.h:119
std::vector< Interface::Ptr > mInterfaces
Vector of Interfaces.
Definition: Simulation.h:126
void create()
Helper function for constructors.
Definition: Simulation.cpp:53
CPS::SystemTopology mSystem
System list.
Definition: Simulation.h:66
Simulation(String name, CommandLineArgs &args)
Creates simulation with name and CommandLineArgs.
Definition: Simulation.cpp:41
CPS::AttributeBase::Ptr getIdObjAttribute(const String &comp, const String &attr)
CHECK: Can these be deleted? getIdObjAttribute + "**attr =" should suffice.
Definition: Simulation.cpp:440
void start()
Start simulation without advancing in time.
Definition: Simulation.cpp:337
EventQueue mEvents
The simulation event queue.
Definition: Simulation.h:64
void doSteadyStateInit(Bool f)
activate steady state initialization
Definition: Simulation.h:220
void addEvent(Event::Ptr e)
Schedule an event in the simulation.
Definition: Simulation.h:245
void schedule()
Create the schedule for the independent tasks.
Definition: Simulation.cpp:210
std::chrono::time_point< std::chrono::steady_clock > mSimulationStartTimePoint
Start time point to measure calculation time.
Definition: Simulation.h:69
Real mTime
Time variable that is incremented at every step.
Definition: Simulation.h:60
Bool mSystemMatrixRecomputation
Enable recomputation of system matrix during simulation.
Definition: Simulation.h:97
Real mSteadStIniAccLimit
steady state initialization accuracy limit
Definition: Simulation.h:115
void createMNASolver()
Subroutine for MNA only because there are many MNA options.
Definition: Simulation.cpp:129
void run()
Run simulation until total time is elapsed.
Definition: Simulation.cpp:395
void logAttribute(String name, CPS::AttributeBase::Ptr attr)
CHECK: Can we store the attribute name / UID intrinsically inside the attribute?
Definition: Simulation.cpp:468
void createSolvers()
Create solvers depending on simulation settings.
Definition: Simulation.cpp:88
const CPS::Attribute< String >::Ptr mName
Simulation name.
Definition: Simulation.h:39
CPS::Logger::Log mLog
Simulation logger.
Definition: Simulation.h:152
void stop()
Stop simulation including scheduler and interfaces.
Definition: Simulation.cpp:366
std::chrono::time_point< std::chrono::steady_clock > mSimulationEndTimePoint
End time point to measure calculation time.
Definition: Simulation.h:71
void addLogger(DataLogger::Ptr logger)
Add a new data logger.
Definition: Simulation.h:247
UInt downsampling
Downsampling.
Definition: Simulation.h:132
DataLogger::Ptr logger
Simulation data logger.
Definition: Simulation.h:130