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 white space, 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 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.
pip install -U pypiwin32Documentation on how to install Python packages can be found in Python's own docs as well as other online Resources.
#initial setup win32com
import win32com
#import client to use dispatching methods
from win32com import client
This will open the same Application Library where you can click "Simul8 Library".
Now, you can use Python to control your simulations.
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()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 = False34.76055145263672
0.8125
81.05886840820312
10.0This 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.
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.
For more information or to give us feedback about how you use Python with simulations, Contact Minitab.