Skip to content

Running a simulation

Gguidini edited this page May 12, 2021 · 5 revisions

A simulation in HMR Sim has 2 important stages:

  • Parse stage. During this stage the simulation config and map are parsed, creating a Simulation instance. Then the systems definitions to be used are added to the instance, completing the simulation build.
  • Run stage. During this stage the simulation instance created is effectively executed.

This page gives details about the run stage. You need to perform the parse stage before this one check the build a simulation guide.

Once the simulation as been parsed, you still need to load system definitions to the Simulator instance, that is, include the system instances to the simulator. If the system is an esper system this can be achieved using the Simulator.add_system method. If the system is a simpy system, than you should use the Simulator.add_des_system method. The difference between the two is explained on the systems guide. Some systems also require to be initialized before being added to the simulation. That depends on the system. A list of available systems and how to use them is also included in the systems guide.

After loading the systems in your simulator instance, simply execute the method Simulator.run to start the simulator. The end and clean-up of the simulation is part of the Simulator configuration.

Ending simulations

There are 2 ways of ending a simulation:

  1. By using the duration option on the simulation configuration. In this case the simulation will run for duration simulation seconds (not wall clock seconds!)
  2. By executing a special EXIT_EVENT that finishes the simulation. This event can be accessed at Simulator.EXIT_EVENT or by any system at kwargs['_KILL_SWITCH']. To trigger this event call its succeed method.

Notice that the simulation clock and the wall clock will likely not be synchronized. That means you can decide what a simulation second represents in relation to the wall clock second.

Clean-up

You can pass a clean-up function when creating the Simulator instance. This function must have no arguments and return None. It will be the last thing executed by the simulator. You may use it to close open file descriptors, close waiting threads, etc.