DPsim
Loading...
Searching...
No Matches
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#include <spdlog/fmt/chrono.h>
14
15using namespace CPS;
16using namespace DPsim;
17
18RealTimeSimulation::RealTimeSimulation(String name, CommandLineArgs &args)
19 : Simulation(name, args), mTimer(){};
20
21RealTimeSimulation::RealTimeSimulation(String name, Logger::Level logLevel)
22 : Simulation(name, logLevel), mTimer() {
23
24 //addAttribute<Int >("overruns", nullptr, [=](){ return mTimer.overruns(); }, Flags::read);
25 //addAttribute<Int >("overruns", nullptr, nullptr, Flags::read);
26}
27
28void RealTimeSimulation::run(const Timer::StartClock::duration &startIn) {
29 run(Timer::StartClock::now() + startIn);
30}
31
32void RealTimeSimulation::run(const Timer::StartClock::time_point &startAt) {
33 if (!mInitialized)
34 initialize();
35
36 for (auto lg : mLoggers)
37 lg->start();
38
39 SPDLOG_LOGGER_INFO(mLog, "Opening interfaces.");
40
41 for (auto intf : mInterfaces)
42 intf->open();
43
44 sync();
45
46 SPDLOG_LOGGER_INFO(
47 mLog,
48 "Starting simulation at {:%Y-%m-%d %H:%M:%S} (delta_T = {} seconds)",
49 startAt, startAt - Timer::StartClock::now());
50
51 mTimer.setStartTime(startAt);
52 mTimer.setInterval(**mTimeStep);
53 mTimer.start();
54
55 // main loop
56 do {
57 mTimer.sleep();
58 step();
59
60 if (mTimer.ticks() == 1)
61 SPDLOG_LOGGER_INFO(mLog, "Simulation started.");
62 } while (mTime < **mFinalTime);
63
64 SPDLOG_LOGGER_INFO(mLog, "Simulation finished.");
65
66 mScheduler->stop();
67
68 for (auto intf : mInterfaces)
69 intf->close();
70
71 for (auto lg : mLoggers)
72 lg->stop();
73
74 mTimer.stop();
75}
The Simulation holds a SystemTopology and a Solver.
Definition Simulation.h:31
void sync() const
Synchronize simulation with remotes by exchanging intial state over interfaces.
virtual Real step()
Solve system A * x = z for x and current time.
const CPS::Attribute< Real >::Ptr mTimeStep
Simulation timestep.
Definition Simulation.h:42
void initialize()
Create solver instances etc.
const CPS::Attribute< Real >::Ptr mFinalTime
Final time of the simulation.
Definition Simulation.h:40
std::shared_ptr< Scheduler > mScheduler
Scheduler used for task scheduling.
Definition Simulation.h:118
std::vector< Interface::Ptr > mInterfaces
Vector of Interfaces.
Definition Simulation.h:125
Simulation(String name, CommandLineArgs &args)
Creates simulation with name and CommandLineArgs.
Real mTime
Time variable that is incremented at every step.
Definition Simulation.h:57
void run()
Run simulation until total time is elapsed.
DataLoggerInterface::List mLoggers
The data loggers.
Definition Simulation.h:135
CPS::Logger::Log mLog
Simulation logger.
Definition Simulation.h:151
void stop()
Stop real-time timer.
Definition Timer.cpp:125
void sleep()
Suspend thread execution until next tick.
Definition Timer.cpp:54