Skip to content

Commit 5d013b7

Browse files
niklworsadrpo
authored andcommitted
[cppRuntime] Added config flags to configure use of zeromq communication in
simulation runtime.The ports for publish and subscribe can be configured. The simulation runtime can then communicate over these ports with other applications via zeroMQ.
1 parent e9fc5e1 commit 5d013b7

File tree

23 files changed

+204
-82
lines changed

23 files changed

+204
-82
lines changed

OMCompiler/Compiler/Template/CodegenCpp.tpl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,14 +1529,14 @@ template simulationMainRunScript(SimCode simCode ,Text& extraFuncs,Text& extraFu
15291529

15301530
let libFolder =simulationLibDir(simulationCodeTarget(),simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace)
15311531
let libPaths = makefileParams.libPaths |> path => path; separator=";"
1532-
1532+
let zermMQParams = if getConfigBool(USE_ZEROMQ_IN_SIM) then '-u true -p <%getConfigInt(ZEROMQ_PUB_PORT)%> -s <%getConfigInt(ZEROMQ_SUB_PORT)%> -i <%getConfigInt(ZEROMQ_SIM_ID)%>' else ''
15331533
match makefileParams.platform
15341534
case "linux32"
15351535
case "linux64" then
15361536
<<
15371537
#!/bin/sh
15381538
<%preRunCommandLinux%>
1539-
<%execCommandLinux%> ./<%fileNamePrefixx%> <%execParameters%> <%outputParameter%> $*
1539+
<%execCommandLinux%> ./<%fileNamePrefixx%> <%execParameters%> <%zermMQParams%> <%outputParameter%> $*
15401540
>>
15411541
case "win32"
15421542
case "win64" then
@@ -1545,7 +1545,7 @@ template simulationMainRunScript(SimCode simCode ,Text& extraFuncs,Text& extraFu
15451545
<%preRunCommandWindows%>
15461546
REM ::export PATH=<%libFolder%>:$PATH REPLACE C: with /C/
15471547
SET PATH=<%home%>/bin;<%libFolder%>;<%libPaths%>;%PATH%
1548-
<%moLib%>/<%fileNamePrefixx%>.exe <%execParameters%> <%outputParameter%>
1548+
<%moLib%>/<%fileNamePrefixx%>.exe <%execParameters%> <%zermMQParams%> <%outputParameter%>
15491549
>>
15501550
end match
15511551
end match

OMCompiler/Compiler/Template/SimCodeTV.mo

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3758,6 +3758,10 @@ package Flags
37583758
constant ConfigFlag Load_PACKAGE_FILE;
37593759
constant ConfigFlag SINGLE_INSTANCE_AGLSOLVER;
37603760
constant ConfigFlag LINEARIZATION_DUMP_LANGUAGE;
3761+
constant ConfigFlag USE_ZEROMQ_IN_SIM;
3762+
constant ConfigFlag ZEROMQ_PUB_PORT;
3763+
constant ConfigFlag ZEROMQ_SUB_PORT;
3764+
constant ConfigFlag ZEROMQ_SIM_ID;
37613765
function set
37623766
input DebugFlag inFlag;
37633767
input Boolean inValue;

OMCompiler/Compiler/Util/Flags.mo

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1518,6 +1518,22 @@ constant ConfigFlag FULL_ASSC = CONFIG_FLAG(133, "fullASSC",
15181518
NONE(), EXTERNAL(), BOOL_FLAG(false), NONE(),
15191519
Util.gettext("Enables full equation replacement for BLT transformation from the ASSC algorithm."));
15201520

1521+
constant ConfigFlag USE_ZEROMQ_IN_SIM = CONFIG_FLAG(134, "useZeroMQInSim",
1522+
NONE(), EXTERNAL(), BOOL_FLAG(false), NONE(),
1523+
Util.gettext("Configures to use zeroMQ in simulation runtime to exchange information via ZeroMQ with other applications"));
1524+
1525+
constant ConfigFlag ZEROMQ_PUB_PORT = CONFIG_FLAG(135, "zeroMQPubPort",
1526+
NONE(), EXTERNAL(), INT_FLAG(3203), NONE(),
1527+
Util.gettext("Configures port number for simulation runtime to send information via ZeroMQ"));
1528+
1529+
constant ConfigFlag ZEROMQ_SUB_PORT = CONFIG_FLAG(136, "zeroMQSubPort",
1530+
NONE(), EXTERNAL(), INT_FLAG(3204), NONE(),
1531+
Util.gettext("Configures port number for simulation runtime to receive information via ZeroMQ"));
1532+
1533+
constant ConfigFlag ZEROMQ_SIM_ID = CONFIG_FLAG(137, "zeroMQSimID",
1534+
NONE(), EXTERNAL(), INT_FLAG(-1), NONE(),
1535+
Util.gettext("Configures the ID with which the simulation is labelled for zeroMQ communication."));
1536+
15211537
protected
15221538
// This is a list of all configuration flags. A flag can not be used unless it's
15231539
// in this list, and the list is checked at initialization so that all flags are
@@ -1655,7 +1671,11 @@ constant list<ConfigFlag> allConfigFlags = {
16551671
STRICT,
16561672
LINEARIZATION_DUMP_LANGUAGE,
16571673
NO_ASSC,
1658-
FULL_ASSC
1674+
FULL_ASSC,
1675+
USE_ZEROMQ_IN_SIM,
1676+
ZEROMQ_PUB_PORT,
1677+
ZEROMQ_SUB_PORT,
1678+
ZEROMQ_SIM_ID
16591679
};
16601680

16611681
public function new

OMCompiler/SimulationRuntime/cpp/Core/ReduceDAE/com/ModelicaCompiler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ _model(model)
3333
std::cout << "Intialize OMC, use gcc" << std::endl;
3434

3535

36-
status = InitOMC(&_omcPtr,"gcc",omhome);
36+
status = InitOMC(&_omcPtr,"gcc",omhome,"");
3737
if(status > 0)
3838
{
3939
std::cout << "..ok" << std::endl;

OMCompiler/SimulationRuntime/cpp/Core/SimController/FactoryExport.cpp

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,8 @@
44
*/
55
#include <Core/ModelicaDefine.h>
66
#include <Core/Modelica.h>
7-
#if defined(__vxworks) || defined(__TRICORE__)
87

9-
10-
11-
#include <Core/SimController/ISimController.h>
12-
#include <Core/SimController/SimController.h>
13-
#include <Core/SimController/SimObjects.h>
14-
15-
extern "C" ISimController* createSimController(PATH library_path, PATH modelicasystem_path)
16-
{
17-
return new SimController(library_path, modelicasystem_path);
18-
}
19-
20-
shared_ptr<ISimObjects> createSimObjects(PATH library_path, PATH modelicasystem_path,IGlobalSettings* settings)
21-
{
22-
return shared_ptr<ISimObjects>(new SimObjects(library_path, modelicasystem_path,settings));
23-
}
24-
25-
#elif defined(SIMSTER_BUILD)
26-
27-
28-
29-
#include <Core/SimController/ISimController.h>
30-
#include <Core/SimController/SimController.h>
31-
/*Simster factory*/
32-
extern "C" void BOOST_EXTENSION_EXPORT_DECL extension_export_simcontroller(boost::extensions::factory_map & fm)
33-
{
34-
fm.get<ISimController,int,PATH,PATH>()[1].set<SimController>();
35-
// fm.get<ISimData,int>()[1].set<SimData>();
36-
}
37-
38-
#elif defined(OMC_BUILD) && !defined(RUNTIME_STATIC_LINKING)
8+
#if defined(OMC_BUILD) && !defined(RUNTIME_STATIC_LINKING)
399

4010
#include <Core/SimController/ISimController.h>
4111
#include <Core/SimController/SimController.h>
@@ -45,6 +15,8 @@ using boost::extensions::factory;
4515
BOOST_EXTENSION_TYPE_MAP_FUNCTION {
4616
types.get<std::map<std::string, factory<ISimController,PATH,PATH> > >()
4717
["SimController"].set<SimController>();
18+
types.get<std::map<std::string, factory<ISimController, PATH, PATH,bool> > >()
19+
["SimController"].set<SimController>();
4820
types.get<std::map<std::string, factory<ISimObjects,PATH,PATH,IGlobalSettings* > > >()
4921
["SimObjects"].set<SimObjects>();
5022
}

OMCompiler/SimulationRuntime/cpp/Core/SimController/SimController.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ try
179179
global_settings->setSolverThreads(simsettings.solverThreads);
180180
global_settings->setInputPath(simsettings.inputPath);
181181
global_settings->setOutputPath(simsettings.outputPath);
182+
global_settings->setZeroMQPubPort(simsettings.zeroMQPubPort);
183+
global_settings->setZeroMQSubPort(simsettings.zeroMQSubPort);
184+
182185

183186
_simMgr = shared_ptr<SimManager>(new SimManager(mixedsystem, _config.get()));
184187

@@ -210,6 +213,7 @@ catch (ModelicaSimulationError& ex)
210213
if(_startZeroMQ)
211214
{
212215
#if defined(USE_ZEROMQ)
216+
_communicator->initialize(global_settings->getZeroMQPubPort(), global_settings->getZeroMQSubPort(), global_settings->getSimulationID());
213217
_communicator->startThreads(_simMgr, global_settings, mixedsystem, _sim_objects, modelKey);
214218
_communicator->waitForAllThreads(120);
215219
#elif defined(USE_ZEROMQ)

OMCompiler/SimulationRuntime/cpp/Core/SimController/threading/Communicator.cpp

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,9 @@ Communicator::Communicator()
1313
, _simstopped(true)
1414
,_guistopped(true)
1515
, _stop(false)
16+
,_isInitialized(false)
1617
{
17-
try
18-
{
19-
20-
21-
_notify = shared_ptr<INotify>(new ToZeroMQEvent());
22-
23-
24-
}
25-
catch (std::exception& ex)
26-
{
27-
28-
std::string error(ex.what());
29-
30-
}
18+
3119
}
3220

3321

@@ -40,6 +28,25 @@ Communicator::~Communicator()
4028

4129

4230

31+
}
32+
33+
void Communicator::initialize(int pubPort, int subPort, int simulationID)
34+
{
35+
try
36+
{
37+
38+
39+
_notify = shared_ptr<INotify>(new ToZeroMQEvent( pubPort, subPort, simulationID));
40+
41+
42+
}
43+
catch (std::exception & ex)
44+
{
45+
46+
std::string error(ex.what());
47+
48+
}
49+
_isInitialized = true;
4350
}
4451
/**
4552
Waits for all threads to end.

OMCompiler/SimulationRuntime/cpp/Core/SimController/threading/SimulationThread.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void SimulationThread::Run(shared_ptr<SimManager> simManager, shared_ptr<IGlobal
4949
if (starting)
5050
{
5151
_communicator->setSimStarted();
52-
simManager->initialize();
52+
5353

5454
#ifdef RUNTIME_PROFILING
5555
if (MeasureTime::getInstance() != NULL)

OMCompiler/SimulationRuntime/cpp/Core/SimController/threading/ToZeroMQEvent.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
// Short alias for this namespace
99
namespace pt = boost::property_tree;
1010

11-
ToZeroMQEvent::ToZeroMQEvent()
11+
ToZeroMQEvent::ToZeroMQEvent(int pubPort, int subPort, int simulationID)
1212
:ctx_(1),
1313
publisher_(ctx_, ZMQ_PUB),
1414
subscriber_(ctx_, ZMQ_SUB),
15-
_simulation_id("")
15+
_simulation_id(std::to_string(simulationID))
1616
{
17-
publisher_.connect("tcp://127.0.0.1:3203");
18-
subscriber_.connect("tcp://127.0.0.1:3204");
17+
publisher_.connect("tcp://127.0.0.1:" + to_string(pubPort));
18+
subscriber_.connect("tcp://127.0.0.1:" + to_string(subPort));
1919
subscriber_.setsockopt(ZMQ_SUBSCRIBE, "OMCSimultionThread", 18);
2020
//Needed to establish connection
2121
std::this_thread::sleep_for(std::chrono::milliseconds(500));

OMCompiler/SimulationRuntime/cpp/Core/SimulationSettings/GlobalSettings.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ GlobalSettings::GlobalSettings()
2323
, _outputPointType(OPT_ALL)
2424
, _alarm_time(0)
2525
, _outputFormat(MAT)
26+
,_zeroMQ_pub_port(3203)
27+
,_zeroMQ_sub_port(3204)
28+
,_simulation_id(-1)
2629
{
2730
}
2831

@@ -229,4 +232,32 @@ int GlobalSettings::getSolverThreads()
229232
{
230233
_outputFormat = outputFormat;
231234
}
235+
236+
237+
void GlobalSettings::setZeroMQPubPort(int pubPort)
238+
{
239+
_zeroMQ_pub_port = pubPort;
240+
}
241+
int GlobalSettings::getZeroMQPubPort()
242+
{
243+
return _zeroMQ_pub_port;
244+
}
245+
246+
void GlobalSettings::setZeroMQSubPort(int subPort)
247+
{
248+
_zeroMQ_sub_port = subPort;
249+
}
250+
int GlobalSettings::getZeroMQSubPort()
251+
{
252+
return _zeroMQ_sub_port;
253+
}
254+
255+
void GlobalSettings::setSimulationID(int id)
256+
{
257+
_simulation_id = id;
258+
}
259+
int GlobalSettings::getSimulationID()
260+
{
261+
return _simulation_id;
262+
}
232263
/** @} */ // end of coreSimulationSettings

OMCompiler/SimulationRuntime/cpp/Include/Core/SimController/ISimController.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ struct SimSettings
2525
EmitResults emitResults;
2626
string inputPath;
2727
string outputPath;
28+
bool useZeroMQ;
29+
int zeroMQPubPort;
30+
int zeroMQSubPort;
31+
int simulationID;
2832
};
2933

3034
/**

OMCompiler/SimulationRuntime/cpp/Include/Core/SimController/threading/Communicator.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Communicator
2727
virtual void startPause();
2828
virtual void stopPause();
2929
virtual bool shouldStop();
30-
30+
virtual void initialize(int pubPort, int subPort, int simulationID);
3131

3232
void notifyResults(double time);
3333

@@ -61,7 +61,7 @@ class Communicator
6161
bool _paused;
6262
double _end_time;
6363
shared_ptr<Runnable> _sim_thread;
64-
64+
bool _isInitialized;
6565

6666
};
6767

OMCompiler/SimulationRuntime/cpp/Include/Core/SimController/threading/ToZeroMQEvent.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
class ToZeroMQEvent: public INotify
1010
{
1111
public:
12-
ToZeroMQEvent( );
12+
ToZeroMQEvent(int pubPort, int subPort, int simulationID);
1313

1414
~ToZeroMQEvent();
1515
virtual void NotifyStarted();

OMCompiler/SimulationRuntime/cpp/Include/Core/SimulationSettings/GlobalSettings.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ class GlobalSettings : public IGlobalSettings
6464
virtual void setSolverThreads(int);
6565
virtual int getSolverThreads();
6666

67+
virtual void setZeroMQPubPort(int);
68+
virtual int getZeroMQPubPort();
69+
70+
virtual void setZeroMQSubPort(int);
71+
virtual int getZeroMQSubPort() ;
72+
73+
virtual void setSimulationID(int);
74+
virtual int getSimulationID();
6775
private:
6876
double
6977
_startTime, ///< Start time of integration (default: 0.0)
@@ -89,5 +97,8 @@ class GlobalSettings : public IGlobalSettings
8997

9098
int _solverThreads;
9199
OutputFormat _outputFormat;
100+
int _zeroMQ_pub_port;
101+
int _zeroMQ_sub_port;
102+
int _simulation_id;
92103
};
93104
/** @} */ // end of coreSimulationSettings

OMCompiler/SimulationRuntime/cpp/Include/Core/SimulationSettings/IGlobalSettings.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,15 @@ class IGlobalSettings
103103

104104
virtual void setSolverThreads(int) = 0;
105105
virtual int getSolverThreads() = 0;
106+
107+
virtual void setZeroMQPubPort(int) = 0;
108+
virtual int getZeroMQPubPort() = 0;
109+
110+
virtual void setZeroMQSubPort(int) = 0;
111+
virtual int getZeroMQSubPort() = 0;
112+
113+
114+
virtual void setSimulationID(int) = 0;
115+
virtual int getSimulationID() = 0;
106116
};
107117
/** @} */ // end of coreSimulationSettings

OMCompiler/SimulationRuntime/cpp/Include/FMU/FMUGlobalSettings.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,11 @@ class FMUGlobalSettings : public IGlobalSettings
5959
virtual int getSolverThreads() { return 1; };
6060
virtual OutputFormat getOutputFormat() {return EMPTY;};
6161
virtual void setOutputFormat(OutputFormat) {};
62+
virtual void setZeroMQPubPort(int) {};
63+
virtual int getZeroMQPubPort() { return -1; };
64+
virtual void setZeroMQSubPort(int) {};
65+
virtual int getZeroMQSubPort() { return -1; };
66+
virtual void setSimulationID(int) {};
67+
virtual int getSimulationID() { return -1; };
6268
private:
6369
};

OMCompiler/SimulationRuntime/cpp/Include/FMU2/FMU2GlobalSettings.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,11 @@ class FMU2GlobalSettings : public IGlobalSettings
9191
virtual int getSolverThreads() { return 1; };
9292
virtual OutputFormat getOutputFormat() {return EMPTY;};
9393
virtual void setOutputFormat(OutputFormat) {};
94+
virtual void setZeroMQPubPort(int) {};
95+
virtual int getZeroMQPubPort() { return -1; };
96+
virtual void setZeroMQSubPort(int) {};
97+
virtual int getZeroMQSubPort() { return -1; };
98+
virtual void setSimulationID(int) {};
99+
virtual int getSimulationID() { return -1; };
94100
};
95101
/** @} */ // end of fmu2

OMCompiler/SimulationRuntime/cpp/Include/SimCoreFactory/OMCFactory/OMCFactory.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,6 @@ class OMCFactory
9797
unordered_set<string> _argumentsToIgnore; //a set of arguments that should be ignored
9898
std::map<string, string> _argumentsToReplace; //a mapping to replace arguments (e.g. -r=... -> -F=...)
9999
std::string _overrideOMEdit; // unrecognized options if called from OMEdit
100+
bool _use_zeroMQ;
100101
};
101102
/** @} */ // end of simcorefactoryOMCFactory

0 commit comments

Comments
 (0)