Sunday, June 16, 2024

SimPy, To Simulate With Python

- Advertisement -

Real-life activities are a constant interaction between components and their environment. A discrete system is no different, constantly taking part in process after process, all the while involved with other systems and its surroundings. Each of these interactions is termed an event, which forms the basis of how these systems work. This article looks at SimPy, a tool that would help you simulate these processes and events using Python generators. SimPy is a process based discrete event simulation framework.

Discrete event simulation with SimPy and Maya, a 3D computer graphics software
Discrete event simulation with SimPy and Maya, a 3D computer graphics software (Image courtesy:

With SimPy, you can implement asynchronous networks or multi-agent systems involving both real and simulated communication, and its processes can model active components like customers, vehicles or agents.

Little things thoughtfully put together

“If you break SimPy down, it is just an asynchronous Box159event dispatcher,” say the developers of this simulation tool. Priority, simulation time, event identifier, call-backs and return values are the defining factors of SimPy’s functionality, apart from being the differentiators and the deciders.

- Advertisement -

Every event needs to be handled uniquely. If you have a digital board display and you need to show certain phrases in a given order and for a given time, start by setting the environment for this. Here, this could be the necessary criterion for each display or setting up of the setup itself. Each of these is a process that would be triggered by an event like the closing of a switch.

SimPy makes sure that each process happens smoothly, suspending it when an event occurs and resuming it once triggered. A single event can trigger multiple processes, and these happen in the order in which these yield the event.

It need not always go as planned. Sometimes, the process gets into a never-ending loop. To avoid such incidents, SimPy offers Timeout feature that is triggered after a certain amount of time and puts the process to sleep. There is also the case of one process being influenced by another.

Take the case of an electric car. The car cannot start until its battery is charged. But you might choose to begin driving the car while the battery is still not fully-charged. Calling these in terms of processes brings two of the most obvious features, Wait and Interrupt.

When there is demand. Taking the case of the electric car a little further, let us look at a scenario of charging it—a charging station equipped with two charging units and four cars waiting in queue. To model such a scenario, SimPy introduces the concept of shared resources in first-in-first-out (FIFO) manner.

Shared resources come in handy while modelling limited-capacity congestion points like servers, checkout counters and tunnels. While the first process gets the resource first, it can be configured to release the resource when the job is done, or the release could be automated. The resource is then handed over to the process waiting in line.

Talking about resources, you can choose the resource according to the specific task at hand. Dealing with simulation of a water tank would need a container, or if the system is about a fruits shop with a variety of elements, a store comes to the aid. If you are modelling a machine shop with machines having variable attributes, go for FillerStore. The above is the case of going around the homogenous slots of a resource to get what you need.

Environment is as important as the process

When the very functioning of one’s system depends Box299on its interaction with its surroundings, the environment takes precedence. A common base environment is generally used as the norm, performing simulations as fast as possible, while a real-time application demands a separate environment for itself. This could be when the system involves hardware or human interaction. SimPy takes this factor into account and provides separate environments for the two.

Further, the way SimPy works, environment is simply not the factor that encompasses the whole, but it is this simulation environment that manages simulation time, scheduling and processing of events, and takes care of execution. You can flow through this processing in different ways: step through in steps, run for a given time, run till simulation stops on its own or flag the trigger of an event.

A peek-and-step feature lets you step through the simulation event-by-event. To integrate the simulation via the graphical user interface, you can repeatedly call a for-loop as below, with repeatedly increasing values, thereby letting you update the process bar according to the progress of the simulation:


How to check what is happening

It is the basic code that you write in SimPy that decides the entire process at hand. It is then obvious to want to observe how the system is functioning, or maybe to cross-check expected behaviour. With monitoring in SimPy, you can monitor the various processes at hand, how each parameter is handled, the way resources are allocated and used, and trace every event of the simulation.

Monitoring can alert you when a certain event happens or if a certain event does not happen. This could be done at regular intervals or you can even set the monitoring routine to begin if and when something goes wayward, and choose to store the results as a file, a list or directly in the database.

Simply using SimPy

Though it is theoretically possible to perform continuous simulations on SimPy, as a library built for discrete simulation, it does not offer any special features for the same. On the other hand, SimPy is overkill for fixed-step-size processes that do not interact with each other or share resources. It is up to you to exploit the built-in features for the right applications and ease the load.

Download latest version of the software: click here

Priya Ravindran is M.Sc (electronics) from VIT University, Vellore, Tamil Nadu. She loves to explore new avenues and is passionate about writing


Unique DIY Projects

Electronics News

Truly Innovative Tech

MOst Popular Videos

Electronics Components