Skip to content

Commit

Permalink
Implement logging of simulation status
Browse files Browse the repository at this point in the history
  • Loading branch information
rfranke committed Nov 26, 2016
1 parent 01ca4a3 commit d9f30dc
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 5 deletions.
14 changes: 9 additions & 5 deletions SimulationRuntime/cpp/Core/SimController/SimManager.cpp
Expand Up @@ -112,7 +112,11 @@ void SimManager::initialize()
throw ModelicaSimulationError(SIMMANAGER,"Could not get step-event system.");
}

_tStart = _config->getGlobalSettings()->getStartTime();
_tEnd = _config->getGlobalSettings()->getEndTime();

LOGGER_WRITE("SimManager: Start initialization",LC_INIT,LL_DEBUG);
LOGGER_STATUS_STARTING(_tStart, _tEnd);

// Reset debug ID
_dbgId = 0;
Expand Down Expand Up @@ -150,8 +154,6 @@ void SimManager::initialize()
else
_dimtimeevent = 0;

_tStart = _config->getGlobalSettings()->getStartTime();
_tEnd = _config->getGlobalSettings()->getEndTime();
// Set flag for endless simulation (if solver returns)
_continueSimulation = _tEnd > _tStart;

Expand Down Expand Up @@ -594,10 +596,10 @@ void SimManager::runSingleProcess()
_timeevent_system->handleTimeEvent(_timeEventCounter);
}

_solverTask = ISolver::SOLVERCALL(_solverTask | ISolver::RECORDCALL);
_solverTask = ISolver::SOLVERCALL(_solverTask | ISolver::RECORDCALL);
_solver->setStartTime(_tStart);
_solver->setEndTime(_tEnd);
_solver->solve(_solverTask);
_solver->solve(_solverTask);
_solverTask = ISolver::SOLVERCALL(_solverTask ^ ISolver::RECORDCALL);


Expand All @@ -607,7 +609,7 @@ void SimManager::runSingleProcess()
// Startzeit messen
_tClockStart = Time::Time().getSeconds();
*/
startTime = _tStart;
startTime = endTime = _tStart;
bool user_stop = false;

while (_continueSimulation)
Expand Down Expand Up @@ -722,6 +724,8 @@ void SimManager::runSingleProcess()
_step_event_system->setTerminal(true);
_cont_system->evaluateAll(IContinuous::CONTINUOUS); //Is this really necessary? The solver should have already calculated the "final time point"

LOGGER_STATUS("Finished", endTime, 0.0);

if (zeroVal_new)
delete[] zeroVal_new;

Expand Down
Expand Up @@ -9,6 +9,8 @@
#include <Core/Solver/SolverSettings.h>
#include <Core/SimulationSettings/IGlobalSettings.h>
#include <Core/Math/Constants.h>
#include <Core/System/FactoryExport.h>
#include <Core/Utils/extension/logger.hpp>

SolverDefaultImplementation::SolverDefaultImplementation(IMixedSystem* system, ISolverSettings* settings)
: SimulationMonitor()
Expand Down Expand Up @@ -198,6 +200,8 @@ void SolverDefaultImplementation::writeToFile(const int& stp, const double& t, c
}
#endif

LOGGER_STATUS("Running", t, h);

if(_settings->getGlobalSettings()->getOutputPointType()!= OPT_NONE)
{
IWriteOutput* writeoutput_system = dynamic_cast<IWriteOutput*>(_system);
Expand Down
5 changes: 5 additions & 0 deletions SimulationRuntime/cpp/Core/Utils/extension/logger.cpp
Expand Up @@ -18,6 +18,7 @@ Logger::Logger(LogSettings settings, bool enabled)
if (_instance != NULL)
delete _instance;
_instance = NULL;
_startTime = _endTime = 0.0;
}

Logger::~Logger()
Expand Down Expand Up @@ -46,6 +47,10 @@ void Logger::writeInternal(std::string msg, LogCategory cat, LogLevel lvl,
}
}

void Logger::statusInternal(const char *, double, double)
{
}

void Logger::setEnabledInternal(bool enabled)
{
_isEnabled = enabled;
Expand Down
26 changes: 26 additions & 0 deletions SimulationRuntime/cpp/Include/Core/Utils/extension/logger.hpp
Expand Up @@ -20,13 +20,19 @@
#define LOGGER_WRITE_END(cat, lvl) Logger::writeEnd(cat, lvl)
#define LOGGER_WRITE_VECTOR(name, vec, dim, lc, ll) \
Logger::writeVector(name, vec, dim, lc, ll)
#define LOGGER_STATUS_STARTING(startTime, endTime) \
Logger::statusStarting(startTime, endTime)
#define LOGGER_STATUS(phase, currentTime, currentStepSize) \
Logger::status(phase, currentTime, currentStepSize)
#else
#define LOGGER_IS_SET(cat, lvl) false
#define LOGGER_WRITE(msg, cat, lvl)
#define LOGGER_WRITE_TUPLE(msg, mode)
#define LOGGER_WRITE_BEGIN(msg, cat, lvl)
#define LOGGER_WRITE_END(cat, lvl)
#define LOGGER_WRITE_VECTOR(name, vec, dim, lc, ll)
#define LOGGER_STATUS_STARTING(startTime, endTime)
#define LOGGER_STATUS(phase, currentTime, currentStepSize)
#endif //USE_LOGGER

class BOOST_EXTENSION_LOGGER_DECL Logger
Expand Down Expand Up @@ -85,6 +91,21 @@ class BOOST_EXTENSION_LOGGER_DECL Logger
}
}

static inline void statusStarting(double startTime, double endTime)
{
if (_instance) {
_instance->_startTime = startTime;
_instance->_endTime = endTime;
_instance->statusInternal("Starting", startTime, 0.0);
}
}

static inline void status(const char *phase, double currentTime, double currentStepSize)
{
if (_instance)
_instance->statusInternal(phase, currentTime, currentStepSize);
}

static void setEnabled(bool enabled)
{
getInstance()->setEnabledInternal(enabled);
Expand Down Expand Up @@ -128,6 +149,9 @@ class BOOST_EXTENSION_LOGGER_DECL Logger
virtual void writeInternal(std::string msg, LogCategory cat, LogLevel lvl,
LogStructure ls);

virtual void statusInternal(const char *phase, double currentTime,
double currentStepSize);

virtual void setEnabledInternal(bool enabled);
virtual bool isEnabledInternal();

Expand All @@ -136,6 +160,8 @@ class BOOST_EXTENSION_LOGGER_DECL Logger
std::string getLevel(LogLevel lvl) const;

static Logger* _instance;
double _startTime;
double _endTime;
LogSettings _logSettings;
bool _isEnabled;
};
Expand Down
21 changes: 21 additions & 0 deletions SimulationRuntime/cpp/SimCoreFactory/OMCFactory/OMCFactory.cpp
Expand Up @@ -62,6 +62,27 @@ class LoggerXMLTCP: public LoggerXML
std::cout << _sstream.str();
}

virtual void statusInternal(const char *phase, double currentTime, double currentStepSize)
{
int completion = _endTime <= _startTime? 0:
(int)((currentTime - _startTime) / (_endTime - _startTime) * 10000);
if (_logSettings.format == LF_XMLTCP) {
_sstream.str("");
_sstream << "<status phase=\"" << phase
<< "\" time=\"" << currentTime
<< "\" currentStepSize=\"" << currentStepSize
<< "\" progress=\"" << completion
<< "\" />" << std::endl;
_socket.send(boost::asio::buffer(_sstream.str()));
}
else {
// send status in old format for backwards compatibility
_sstream.str("");
_sstream << completion << " " << phase << std::endl;
_socket.send(boost::asio::buffer(_sstream.str()));
}
}

boost::asio::io_service _ios;
boost::asio::ip::tcp::endpoint _endpoint;
boost::asio::ip::tcp::socket _socket;
Expand Down

0 comments on commit d9f30dc

Please sign in to comment.