We have been shortlisted! We are delighted to be nominated for 5 Tech for Good awards for our work with South African charity Shout It Now. Read more here.
Python and Simul8 COM
Python was first introduced in the early 1990s by Dutch engineer Guido Van Rossum. The design philosophy of Python emphasizes code readability and significant use of whitespace, with the Python community emphasizing the aesthetics of code.
There are many applications in which Python can be used, but it is most commonly used for: building desktop and web applications; solving scientific and statistical problems; providing scripting support for other programming languages; and more recently for Artificial Intelligence and Data Science.
There are many benefits to using Python with Simul8, some of these include:
- An even deeper analysis of your simulation results
- More data visualization options that will “wow” your stakeholders
- Can be faster than other data analytics packages
There is little wonder why Python has become one of the most popular programming languages and in this help file, we will show you how to set up Python to work with and connect to Simul8 by using simple examples. The following examples have been built using Python 3.9.10 in Python’s built-in IDLE shell.
Initial Set up
Step 1
To use Python with Simul8, there are a few settings and packages that you will need to install.
- Install the 32-Bit version of Python: Simul8 is available as a 32- or 64-bit application. You will need to make sure you use the corresponding version of Python.
- Install Win32Com: You must also install Win32Com to be able to query COM applications. For seasoned Python users, this is simply a PIP installation of pywin32 for Python 3.9.10 as seen in the example code below.
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBpip install -U pypiwin32
Documentation on how to install Python packages can be found in Python’s own docs as well as other online resources.
Step 2
Next within the Python IDLE you will need to import this using these commands:
#initial setup win32com import win32com #import client to use dispatching methods from win32com import client
Step 3
Initiate makepy.py File: The Makepy.py file is a Win32Com specific file which registers and creates COM modules out of COM-enabled applications. You have two choices when it comes to initiating this file:
- Navigate to the file in the file browser, double click on the file and it will bring up the Application Library. From here, you can scroll to find “Simul8 Library” and then click OK. This will generate the Simul8 module to be used. This file will normally be found somewhere like: C:\Users\{username}\AppData\Local\Programs\Python\Python38-64\Lib\site-packages\win32com\client or wherever you have Python installed.
- Open your Python shell of choice (e.g. Python IDLE) and use the following commands:
This will open the same Application Library where you can click on “Simul8 Library”.
That’s it! You’re now ready to use Python to control your simulations.
Example commands in Python: Application in Simul8 Basic
The following commands cover the Python's interaction with Simul8 Basic. They can be used for opening up Simul8, opening a specific file, running the simulation, and exiting Simul8. This can be done using Python IDLE, which we recommend using.
#this command opens up Simul8 S8 = win32com.client.Dispatch("Simul8.S8Simulation")
#this command opens up a specific Simul8 file - the ‘r’ stands for # -raw strings which is useful when dealing with file paths S8.Open(r"C:\Users\{username}\Documents\PythonTest.s8")
#this runs the simulation for a certain amount of time S8.RunSim(1000)
#this command closes Simul8 down S8 = None
Example commands in Python: Application in Simul8 Professional
If you are using Simul8 Professional, you can control more events in Simul8 through Python. In this example, we will show how Python handles events such as when the simulation is opened, when the simulation has finished a run or when a custom event is called.
For triggering such events, we firstly need to create our Event Handler. Here, after having opened Simul8 and our model (as shown previously), we want to create an OnS8SimulationEndRun event which will trigger whenever the simulation is finished running.
Once the simulation is finished running, I want it to print to the screen the results that I have saved in the Results Manager. In my simulation I want to know: the Average Time in System, the Activity’s Average Use, the Activity’s Working Percentage, and the Queue’s maximum size.
class EventHandler: def OnS8SimulationEndRun(self): n=1 while (n <= S8.ResultsCount): print(S8.Results(n)) n +=1
We now need to connect our instance of Simul8 together with the EventHandler class. We can do this using the Win32Com client command: WithEvents.
events = win32com.client.WithEvents(S8, EventHandler)
Let’s run our simulation for 2000 minutes and see what results we get.
S8.RunSim(2000)
As you can see, the results from the Results Manager have been printed to the screen!
34.76055145263672 0.8125 81.05886840820312 10.0
This shows the Average Time in System as 34.76 minutes, the Activity’s Average Use as 0.81, the Activity’s Working percentage as 81% and the Maximum Queue Size is 10 Work Items.
Finally, to close the simulation, simply use the following command:
S8 = None
In this example, we have simply printed results to the shell, but you can extend this to write to and update a CSV file or do any other data manipulation that you would like.
Conclusion
As you can see, Python allows you to control and manipulate your simulation without having to press the Play button! Python also offers the freedom and flexibility to do more in-depth data analysis and visualization of your simulation results.
We can’t wait to see how our users take advantage of using Python with their simulations! Get in touch with our team for more information or to give us your feedback.