Changeset c2c9092 in OpenModelica


Ignore:
Timestamp:
2020-10-21T18:53:49+02:00 (4 years ago)
Author:
Adrian Pop <adrian.pop@…>
Branches:
maintenance/v1.16
Children:
ef0683e
Parents:
dae55cf1
git-author:
Niklas Worschech <niklas.worschech@…> (09/17/20 14:30:27)
git-committer:
Adrian Pop <adrian.pop@…> (10/21/20 18:53:49)
Message:

[omiscpp] handle abort calls from fmu's in omsicpp simulation, send error messages via zeromq

Location:
OMCompiler/SimulationRuntime/OMSICpp
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • OMCompiler/SimulationRuntime/OMSICpp/omsi/src/omsi.cpp

    rce9a30f rc2c9092  
    4848
    4949//OpenModelica Simulation Interface
     50
     51
     52#include <csignal>
     53
     54extern "C" void handle_aborts(int signal_number)
     55{
     56   
     57    std::string error = std::string("Abort was called with error code: ") + to_string(signal_number);
     58    throw ModelicaSimulationError(MODEL_EQ_SYSTEM, error);
     59       
     60}
     61
     62
     63
     64
     65
    5066#include <omsi.h>
    5167
     
    5672
    5773
     74
     75
     76
     77
    5878#if defined(_MSC_VER) || defined(__MINGW32__)
    5979#include <tchar.h>
     80
    6081
    6182int _tmain(int argc, const _TCHAR* argv[])
     
    6485#endif
    6586{
     87   
     88    //use handle_aborts for abort() calls
     89    signal(SIGABRT, &handle_aborts);
    6690    // default program options
    6791    std::map<std::string, std::string> opts;
  • OMCompiler/SimulationRuntime/OMSICpp/runtime/include/Core/SimController/threading/Communicator.h

    r573788c rc2c9092  
    3232    void notifyResults(double time);
    3333
    34     void setSimStoped();
     34    void setSimStoped(bool success, string erro_message = string("no error has occurred"));
    3535    void setSimStarted();
    3636    void setSimStopedByException(std::exception& except);
  • OMCompiler/SimulationRuntime/OMSICpp/runtime/include/Core/SimController/threading/INotify.h

    r573788c rc2c9092  
    4343    Simulation has finished
    4444    */
    45     virtual void NotifyFinish() = 0;
     45    virtual void NotifyFinish(bool success, string erro_message =  string("no error has occurred")) = 0;
    4646
    4747    /**
  • OMCompiler/SimulationRuntime/OMSICpp/runtime/include/Core/SimController/threading/ToZeroMQEvent.h

    r573788c rc2c9092  
    1515        virtual void NotifyStarted();
    1616    virtual void NotifyResults(double progress);
    17     virtual void NotifyFinish();
     17    virtual void NotifyFinish(bool success, string erro_message = string("no error has occurred"));
    1818    virtual void NotifyException(std::string message);
    1919        virtual void NotifyWaitForStarting();
  • OMCompiler/SimulationRuntime/OMSICpp/runtime/include/Core/System/OMSUSystem.h

    rce9a30f rc2c9092  
    1313*
    1414*****************************************************************************/
     15
     16
     17
    1518typedef vector<tuple<fmi2_value_reference_t, unsigned int>> out_vars_vr_t;
     19
     20
     21
    1622
    1723class omsi_me;
  • OMCompiler/SimulationRuntime/OMSICpp/runtime/src/Core/SimController/SimController.cpp

    r3d14a91e rc2c9092  
    102102    shared_ptr<IMixedSystem> system = createSystem(modelLib, modelKey, _config->getGlobalSettings());
    103103
    104     if (system)
    105         std::cout << "1 system  is here " << modelKey << std::endl;
    106     else
    107         std::cout << "1 no system is here " << modelKey << std::endl;
     104   
    108105
    109106    _systems[modelKey] = system;
     
    225222#endif
    226223
     224       
     225    if (!_startZeroMQ)
     226    {
     227        //initialize for zeromq simulation is done in simulation thread
    227228        _simMgr->initialize();
    228 
    229 
    230 }
     229    }
     230
     231    }
    231232    catch (ModelicaSimulationError& ex)
    232233    {
  • OMCompiler/SimulationRuntime/OMSICpp/runtime/src/Core/SimController/threading/Communicator.cpp

    r3d14a91e rc2c9092  
    184184Indicates   simulation thread is finished
    185185*/
    186 void Communicator::setSimStoped()
     186void Communicator::setSimStoped(bool success, string erro_message)
    187187{
    188188    std::lock_guard<std::mutex> lockGuard(_mutex);
     
    191191    _simstopped = true;
    192192    _stop = true;
    193     _notify->NotifyFinish();
     193    _notify->NotifyFinish(success,erro_message);
    194194    _simulation_finish.notify_all();
    195195}
     
    236236{
    237237
    238     setSimStoped();
     238   
     239    std::lock_guard<std::mutex> lockGuard(_mutex);
     240    //cout << "sim stoped" << std::endl;
     241    _paused = false;
     242    _simstopped = true;
     243    _stop = true;
     244   
    239245    if (_notify)
    240246        _notify->NotifyException(except.what());
     247   
     248    _simulation_finish.notify_all();
    241249
    242250}
     
    257265{
    258266    std::lock_guard<std::mutex> lockGuard(_mutex);
    259     cout << "gui stoped" << std::endl;
     267    //cout << "gui stoped" << std::endl;
    260268    _guistopped = true;
    261269   
  • OMCompiler/SimulationRuntime/OMSICpp/runtime/src/Core/SimController/threading/SimulationThread.cpp

    r3d14a91e rc2c9092  
    3131{
    3232 
    33    
     33 
    3434   
    3535    try
     
    4747
    4848        _simManager = simManager;
     49       
    4950       bool starting = _communicator->waitForSimulationStarting(1);
     51     
    5052       if (starting)
    5153       {
    5254           _communicator->setSimStarted();
    53          
     55          simManager->initialize();
    5456
    5557#ifdef RUNTIME_PROFILING
     
    6163#endif
    6264           high_resolution_clock::time_point t_s = high_resolution_clock::now();
     65         
    6366           simManager->runSimulation();
    6467           high_resolution_clock::time_point t1 = high_resolution_clock::now();
    6568           seconds elapsed = duration_cast<std::chrono::seconds>(t1 - t_s);
    66            cout << "time for simulation: " << elapsed.count();
     69           
    6770
    6871
     
    104107               simData->addTimeEntries(time_values);
    105108           }
    106            _communicator->setSimStoped();
     109           
     110           _communicator->setSimStoped(true);
    107111       }
    108112       else
    109113       {
    110114           string error = string("Simulation failed for ") + modelKey;
     115            _communicator->setSimStoped(false,error);
    111116           throw ModelicaSimulationError(SIMMANAGER, error);
    112117       }
     
    116121    {
    117122        string error = add_error_info(string("Simulation failed for ") + modelKey, ex.what(), ex.getErrorID());
     123        //_communicator->setSimStopedByException(ex);
     124   
     125        _communicator->setSimStoped(false,error);
    118126        throw ModelicaSimulationError(SIMMANAGER, error, "", ex.isSuppressed());
    119127    }
  • OMCompiler/SimulationRuntime/OMSICpp/runtime/src/Core/SimController/threading/ToZeroMQEvent.cpp

    r573788c rc2c9092  
    2525    std::this_thread::sleep_for(std::chrono::milliseconds(500));
    2626
    27     //std::cout << "pub port: " << to_string(pubPort)<< " sub port: " << to_string(subPort) <<  " job id " << zeroMQJobiID << " server thread id " << zeromq_simultaion_thread_id << std::endl;
     27   
    2828}
    2929ToZeroMQEvent::~ToZeroMQEvent()
     
    5959void ToZeroMQEvent::NotifyWaitForStarting()
    6060{
    61     //std::cout << "Wating for ID" << std::endl;
     61   
    6262    s_sendmore(publisher_, _zeromq_server_id);
    6363    s_sendmore(publisher_, "SimulationThreadWatingForID");
     
    8686    if (!message.empty())
    8787    {
    88        // std::cout << "received topic" << message << std::endl;
     88       
    8989        //  Read message contents
    9090        std::string type = s_recv(subscriber_,false);
    91         std::cout << "received type " << type << std::endl;
     91       
    9292        if (type == "StopSimulationThread")
    9393        {
     
    101101
    102102
    103 void ToZeroMQEvent::NotifyFinish()
     103void ToZeroMQEvent::NotifyFinish(bool success, string erro_message)
    104104{
    105105    if (!_zeromq_job_id.empty())
     
    107107        s_sendmore(publisher_, _zeromq_client_id);
    108108        s_sendmore(publisher_, "SimulationFinished");
    109         s_send(publisher_, "{\"Succeeded\":true,\"JobId\":\"" + _zeromq_job_id + "\",\"ResultFile\":\"\",\"Error\":\"\"}");
     109        string sim_success;
     110        if(success)
     111            sim_success = "true";
     112        else
     113            sim_success = "false";
     114        string finished = string("{\"Succeeded\":") + sim_success + string(",\"JobId\":\"") + _zeromq_job_id + string("\",\"ResultFile\":\"\",\"Error\":\"") + erro_message +string("\"}");
     115            s_send(publisher_,finished.c_str());
    110116    }
    111117    else
     
    115121void ToZeroMQEvent::NotifyException(std::string message)
    116122{
    117 
     123if (!_zeromq_job_id.empty())
     124    {
     125        s_sendmore(publisher_, _zeromq_client_id);
     126        s_sendmore(publisher_, "SimulationFinished");
     127        string finished = string("{\"Succeeded\":false,\"JobId\":\"") + _zeromq_job_id + string("\",\"ResultFile\":\"\",\"Error\":\"") + message + string("\"}");
     128     
     129        s_send(publisher_,finished.c_str());
     130    }
     131    else
     132        throw ModelicaSimulationError(SIMMANAGER, "No simulation id received");
    118133
    119134}
     
    127142        s_send(publisher_, "{\"JobId\":\"" + _zeromq_job_id + "\"}");
    128143    }
     144     else
     145        throw ModelicaSimulationError(SIMMANAGER, "No simulation id received");
    129146}
    130147
Note: See TracChangeset for help on using the changeset viewer.