Skip to content

Commit

Permalink
[omiscpp] handle abort calls from fmu's in omsicpp simulation, send e…
Browse files Browse the repository at this point in the history
…rror messages via zeromq
  • Loading branch information
niklwors authored and adrpo committed Oct 21, 2020
1 parent dae55cf commit c2c9092
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 24 deletions.
24 changes: 24 additions & 0 deletions OMCompiler/SimulationRuntime/OMSICpp/omsi/src/omsi.cpp
Expand Up @@ -47,6 +47,22 @@


//OpenModelica Simulation Interface


#include <csignal>

extern "C" void handle_aborts(int signal_number)
{

std::string error = std::string("Abort was called with error code: ") + to_string(signal_number);
throw ModelicaSimulationError(MODEL_EQ_SYSTEM, error);

}





#include <omsi.h>


Expand All @@ -55,14 +71,22 @@
namespace fs = boost::filesystem;






#if defined(_MSC_VER) || defined(__MINGW32__)
#include <tchar.h>


int _tmain(int argc, const _TCHAR* argv[])
#else
int main(int argc, const char* argv[])
#endif
{

//use handle_aborts for abort() calls
signal(SIGABRT, &handle_aborts);
// default program options
std::map<std::string, std::string> opts;

Expand Down
Expand Up @@ -31,7 +31,7 @@ class Communicator

void notifyResults(double time);

void setSimStoped();
void setSimStoped(bool success, string erro_message = string("no error has occurred"));
void setSimStarted();
void setSimStopedByException(std::exception& except);
void setGuiStoped();
Expand Down
Expand Up @@ -42,7 +42,7 @@
/**
Simulation has finished
*/
virtual void NotifyFinish() = 0;
virtual void NotifyFinish(bool success, string erro_message = string("no error has occurred")) = 0;

/**
Simulation throws an exception
Expand Down
Expand Up @@ -14,7 +14,7 @@
~ToZeroMQEvent();
virtual void NotifyStarted();
virtual void NotifyResults(double progress);
virtual void NotifyFinish();
virtual void NotifyFinish(bool success, string erro_message = string("no error has occurred"));
virtual void NotifyException(std::string message);
virtual void NotifyWaitForStarting();
virtual bool AskForStop();
Expand Down
Expand Up @@ -12,8 +12,14 @@ class DiscreteEvents;
*
*
*****************************************************************************/



typedef vector<tuple<fmi2_value_reference_t, unsigned int>> out_vars_vr_t;




class omsi_me;

class OMSUSystem : public IContinuous, public IEvent, public IStepEvent, public IStateSelection, public ITime,
Expand Down
Expand Up @@ -101,10 +101,7 @@ weak_ptr<IMixedSystem> SimController::LoadSystem(string modelLib, string modelKe
//create system
shared_ptr<IMixedSystem> system = createSystem(modelLib, modelKey, _config->getGlobalSettings());

if (system)
std::cout << "1 system is here " << modelKey << std::endl;
else
std::cout << "1 no system is here " << modelKey << std::endl;


_systems[modelKey] = system;
return system;
Expand Down Expand Up @@ -224,10 +221,14 @@ void SimController::Start(SimSettings simsettings, string modelKey)
}
#endif


if (!_startZeroMQ)
{
//initialize for zeromq simulation is done in simulation thread
_simMgr->initialize();
}


}
}
catch (ModelicaSimulationError& ex)
{
string error = add_error_info(string("Simulation failed for ") + simsettings.outputfile_name, ex.what(), ex.getErrorID());
Expand Down
Expand Up @@ -183,14 +183,14 @@ void Communicator::notifyResults(double time)
/**
Indicates simulation thread is finished
*/
void Communicator::setSimStoped()
void Communicator::setSimStoped(bool success, string erro_message)
{
std::lock_guard<std::mutex> lockGuard(_mutex);
//cout << "sim stoped" << std::endl;
_paused = false;
_simstopped = true;
_stop = true;
_notify->NotifyFinish();
_notify->NotifyFinish(success,erro_message);
_simulation_finish.notify_all();
}
/**
Expand Down Expand Up @@ -235,9 +235,17 @@ Indicates simulation thread is finished
void Communicator::setSimStopedByException(std::exception& except)
{

setSimStoped();

std::lock_guard<std::mutex> lockGuard(_mutex);
//cout << "sim stoped" << std::endl;
_paused = false;
_simstopped = true;
_stop = true;

if (_notify)
_notify->NotifyException(except.what());

_simulation_finish.notify_all();

}
/**
Expand All @@ -256,7 +264,7 @@ Indicates when progress thread is finished
void Communicator::setGuiStoped()
{
std::lock_guard<std::mutex> lockGuard(_mutex);
cout << "gui stoped" << std::endl;
//cout << "gui stoped" << std::endl;
_guistopped = true;

_simulation_finish.notify_all();
Expand Down
Expand Up @@ -30,7 +30,7 @@ Run method of the simulation thread in which the simulation is executed
void SimulationThread::Run(shared_ptr<SimManager> simManager, shared_ptr<IGlobalSettings> global_settings, shared_ptr<IMixedSystem> system, shared_ptr<ISimObjects> sim_objects, string modelKey)
{



try
{
Expand All @@ -46,11 +46,13 @@ void SimulationThread::Run(shared_ptr<SimManager> simManager, shared_ptr<IGlobal
#endif

_simManager = simManager;

bool starting = _communicator->waitForSimulationStarting(1);

if (starting)
{
_communicator->setSimStarted();

simManager->initialize();

#ifdef RUNTIME_PROFILING
if (MeasureTime::getInstance() != NULL)
Expand All @@ -60,10 +62,11 @@ void SimulationThread::Run(shared_ptr<SimManager> simManager, shared_ptr<IGlobal
}
#endif
high_resolution_clock::time_point t_s = high_resolution_clock::now();

simManager->runSimulation();
high_resolution_clock::time_point t1 = high_resolution_clock::now();
seconds elapsed = duration_cast<std::chrono::seconds>(t1 - t_s);
cout << "time for simulation: " << elapsed.count();



if (global_settings->getOutputFormat() == BUFFER)
Expand Down Expand Up @@ -103,18 +106,23 @@ void SimulationThread::Run(shared_ptr<SimManager> simManager, shared_ptr<IGlobal
vector<double> time_values = history->getTimeEntries();
simData->addTimeEntries(time_values);
}
_communicator->setSimStoped();

_communicator->setSimStoped(true);
}
else
{
string error = string("Simulation failed for ") + modelKey;
_communicator->setSimStoped(false,error);
throw ModelicaSimulationError(SIMMANAGER, error);
}

}
catch (ModelicaSimulationError& ex)
{
string error = add_error_info(string("Simulation failed for ") + modelKey, ex.what(), ex.getErrorID());
//_communicator->setSimStopedByException(ex);

_communicator->setSimStoped(false,error);
throw ModelicaSimulationError(SIMMANAGER, error, "", ex.isSuppressed());
}

Expand Down
Expand Up @@ -24,7 +24,7 @@ ToZeroMQEvent::ToZeroMQEvent(int pubPort, int subPort, string zeroMQJobiID, stri
//Needed to establish connection
std::this_thread::sleep_for(std::chrono::milliseconds(500));

//std::cout << "pub port: " << to_string(pubPort)<< " sub port: " << to_string(subPort) << " job id " << zeroMQJobiID << " server thread id " << zeromq_simultaion_thread_id << std::endl;

}
ToZeroMQEvent::~ToZeroMQEvent()
{
Expand Down Expand Up @@ -58,7 +58,7 @@ void ToZeroMQEvent::NotifyResults(double progress)
}
void ToZeroMQEvent::NotifyWaitForStarting()
{
//std::cout << "Wating for ID" << std::endl;

s_sendmore(publisher_, _zeromq_server_id);
s_sendmore(publisher_, "SimulationThreadWatingForID");
s_send(publisher_, "{\"jobId\":\"" + _zeromq_job_id + "\"}");
Expand All @@ -85,10 +85,10 @@ bool ToZeroMQEvent::AskForStop()
std::string message = s_recv(subscriber_, false);
if (!message.empty())
{
// std::cout << "received topic" << message << std::endl;

// Read message contents
std::string type = s_recv(subscriber_,false);
std::cout << "received type " << type << std::endl;

if (type == "StopSimulationThread")
{

Expand All @@ -100,21 +100,36 @@ bool ToZeroMQEvent::AskForStop()
}


void ToZeroMQEvent::NotifyFinish()
void ToZeroMQEvent::NotifyFinish(bool success, string erro_message)
{
if (!_zeromq_job_id.empty())
{
s_sendmore(publisher_, _zeromq_client_id);
s_sendmore(publisher_, "SimulationFinished");
s_send(publisher_, "{\"Succeeded\":true,\"JobId\":\"" + _zeromq_job_id + "\",\"ResultFile\":\"\",\"Error\":\"\"}");
string sim_success;
if(success)
sim_success = "true";
else
sim_success = "false";
string finished = string("{\"Succeeded\":") + sim_success + string(",\"JobId\":\"") + _zeromq_job_id + string("\",\"ResultFile\":\"\",\"Error\":\"") + erro_message +string("\"}");
s_send(publisher_,finished.c_str());
}
else
throw ModelicaSimulationError(SIMMANAGER, "No simulation id received");
}

void ToZeroMQEvent::NotifyException(std::string message)
{

if (!_zeromq_job_id.empty())
{
s_sendmore(publisher_, _zeromq_client_id);
s_sendmore(publisher_, "SimulationFinished");
string finished = string("{\"Succeeded\":false,\"JobId\":\"") + _zeromq_job_id + string("\",\"ResultFile\":\"\",\"Error\":\"") + message + string("\"}");

s_send(publisher_,finished.c_str());
}
else
throw ModelicaSimulationError(SIMMANAGER, "No simulation id received");

}

Expand All @@ -126,5 +141,7 @@ void ToZeroMQEvent::NotifyStarted()
s_sendmore(publisher_, "SimulationStarted");
s_send(publisher_, "{\"JobId\":\"" + _zeromq_job_id + "\"}");
}
else
throw ModelicaSimulationError(SIMMANAGER, "No simulation id received");
}

0 comments on commit c2c9092

Please sign in to comment.