DPsim
RealTimeSimulation.cpp
1 /* Copyright 2017-2021 Institute for Automation of Complex Power Systems,
2  * EONERC, RWTH Aachen University
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
7  *********************************************************************************/
8 
9 #include <chrono>
10 #include <ctime>
11 #include <dpsim/RealTimeSimulation.h>
12 #include <iomanip>
13 
14 using namespace CPS;
15 using namespace DPsim;
16 
17 RealTimeSimulation::RealTimeSimulation(String name, CommandLineArgs &args)
18  : Simulation(name, args), mTimer(){};
19 
20 RealTimeSimulation::RealTimeSimulation(String name, Logger::Level logLevel)
21  : Simulation(name, logLevel), mTimer() {
22 
23  //addAttribute<Int >("overruns", nullptr, [=](){ return mTimer.overruns(); }, Flags::read);
24  //addAttribute<Int >("overruns", nullptr, nullptr, Flags::read);
25 }
26 
27 void RealTimeSimulation::run(const Timer::StartClock::duration &startIn) {
28  run(Timer::StartClock::now() + startIn);
29 }
30 
31 void RealTimeSimulation::run(const Timer::StartClock::time_point &startAt) {
32  if (!mInitialized)
33  initialize();
34 
35  SPDLOG_LOGGER_INFO(mLog, "Opening interfaces.");
36 
37  for (auto intf : mInterfaces)
38  intf->open();
39 
40  sync();
41 
42  auto now_time = std::chrono::system_clock::to_time_t(startAt);
43  SPDLOG_LOGGER_INFO(mLog, "Starting simulation at {} (delta_T = {} seconds)",
44  std::put_time(std::localtime(&now_time), "%F %T"),
45  std::chrono::duration_cast<std::chrono::seconds>(
46  startAt - Timer::StartClock::now())
47  .count());
48 
49  mTimer.setStartTime(startAt);
50  mTimer.setInterval(**mTimeStep);
51  mTimer.start();
52 
53  // main loop
54  do {
55  mTimer.sleep();
56  step();
57 
58  if (mTimer.ticks() == 1)
59  SPDLOG_LOGGER_INFO(mLog, "Simulation started.");
60  } while (mTime < **mFinalTime);
61 
62  SPDLOG_LOGGER_INFO(mLog, "Simulation finished.");
63 
64  mScheduler->stop();
65 
66  for (auto intf : mInterfaces)
67  intf->close();
68 
69  for (auto lg : mLoggers)
70  lg->close();
71 
72  mTimer.stop();
73 }
The Simulation holds a SystemTopology and a Solver.
Definition: Simulation.h:34
void sync() const
Synchronize simulation with remotes by exchanging intial state over interfaces.
Definition: Simulation.cpp:173
virtual Real step()
Solve system A * x = z for x and current time.
Definition: Simulation.cpp:405
const CPS::Attribute< Real >::Ptr mTimeStep
Simulation timestep.
Definition: Simulation.h:45
DataLogger::List mLoggers
The data loggers.
Definition: Simulation.h:136
void initialize()
Create solver instances etc.
Definition: Simulation.cpp:63
const CPS::Attribute< Real >::Ptr mFinalTime
Final time of the simulation.
Definition: Simulation.h:43
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
Real mTime
Time variable that is incremented at every step.
Definition: Simulation.h:60
void run()
Run simulation until total time is elapsed.
Definition: Simulation.cpp:395
CPS::Logger::Log mLog
Simulation logger.
Definition: Simulation.h:152
void stop()
Stop real-time timer.
Definition: Timer.cpp:111
void sleep()
Suspend thread execution until next tick.
Definition: Timer.cpp:45