|
Simo 0.0.1
|
The document highlights some location in the source of FirstModule.cc to better understand how to write your own Module class and how to use it in SimoSim, a general purpose executable to run simulations.
It is assumed that this example is executed inside the nix development environment provided by ../flake.nix.
Modules are the basic block of the simulation. They have the ability to schedule events in the simulation loop. To create a new module, it is enough to inherit from Simo::Module class.
All the classed are declared in the include Simo/Simo.h and they are contained in the Simo namespace.
The initialize method allows to initialize a module with the initial state of the simulation.
Context& sim_ctx_p is a data-structure that represents the global state of the simulation. Events are scheduled using this class. It is good practice to call the method of the inherited class Module::initialize to start the initialization. This will set the simulation context and do checks on the parameters.
const Parameters& parameters stores a set of parameters that the class can query and use. In the example, the period is fetched and set:
In the initialization, a first event is scheduled at time zero to call the log_event method:
Other interesting content in the initialize method:
event_log runs first at time zero, and it re-schedules itself every period time units using the simulation context. Look at Simo/core/Context.h for the difference between schedule_at and schedule_in . Source of the method:
This function above increase the statistic, and it logs the event in the logger.
Parameters class defines a set of parameters. Parameters are organized in a trie-like structure. The easiest way to define parameters for a new module is to extend the Parameters class, and add parameter definitions in the constructor.
SimoSim is an executable able to detect shared libraries at runtime that contains modules. A shared library needs to expose a function named simo_get_collectionand add SIMO_COLLECTION_DECLARATION only once per library. With this function, SimoSim can load a collection of factories. A Factory tells SimoSim how to instantiate a module and the associated parameters.
Use the usual CMake commands to build the shared library that contains the FirstModule. Note it is not required to link against the shared library of Simo in this case: only the header files are sufficient. The build process will create a shared library named libFirstModuleCollection.<extension>.
Assuming:
This command will run the simulation as specified in system_config.yaml. Please look at this file and look at the structure. Note:
Try to create your own module and pack it into a collection.
A more complete example is the PingPongCollection at tests/collection/PingPongCollection.cc.
The second example will show how to deal with module ports.