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 and our Simul8 COM Commands. 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
The following commands covers a basic Python interaction with Simul8. This script can be used to open up Simul8, opening a specific file, running the simulation, and exiting Simul8.
import win32com.client import pythoncom # This class will handle the events, we define these before we initialize the Simulation class EventHandler: def OnS8SimulationOpened(self): print("Simulation Opened") def OnS8SimulationEndRun(self): print('Simulation Run ') global listenForMessages S8.Close() # Finally it is important to trigger the end of our python loop. # This could be in any kind of event but the end of the run is often a useful place. listenForMessages = False # Initialize COM pythoncom.CoInitialize() # Note: Here you are giving control of Simul8 to COM. #You may not be able to control this instance manually until you release it from COM or close your kernel. S8 = win32com.client.Dispatch("Simul8.S8Simulation") # Register Event Handler events = win32com.client.WithEvents(S8, EventHandler) # Open Simulation try: S8.Open(r"C:\Users\devteam\Desktop\COM Example 2.S8") except Exception as e: print(f"Error opening simulation: {e}") exit() # Initialize a looping variable from the Event Handler section, this helps us control the commands and when they are excecuted # This can be called whatever you want global listenForMessages listenForMessages = True # Run Simulation S8.RunSim(4000) # Start a loop to listen for events # This stops python from going any further before the simulation has run and the end condition is met. try: while listenForMessages: pythoncom.PumpWaitingMessages() except Exception as e: print(f"Error processing messages: {e}") finally: # Uninitialize COM pythoncom.CoUninitialize()
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 you can print the run results into the Python shell. For triggering such event, we firstly need to copy the start of our Event Handler section (as shown previously) into the IDE. To get the Results of the run printed once the run has completed the OnS8SimulationEnd Run event needs to be altred as in the example below.
def OnS8SimulationEndRun(self): print('Simulation Run ') global waitForEvents try: n = 1 while n <= S8.ResultsCount: print(S8.Results(n)) n += 1 except Exception as e: print(f"Error accessing results: {e}") finally: S8.Close() # Finally again it is important to trigger the end of our python loop. # This could be in any kind of event but I find at the end of the run most useful. waitForEvents = False
We can then add the rest of the script as in the first example and run it. As you can see, the results from the Results Manager have now 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.
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.