question

Farouq Halawa avatar image
0 Likes"
Farouq Halawa asked Jason Lightfoot commented

Running a simulation model using python

I have a simulation model that I would like to run externally multiple times using python.

I am looking for commands that do the following:

1) Order the software to start running (I tried this but did not work). My model is called Test2, and I have a code written on a scrip text file resetmodel(); runspeed(10000); go();

Code in python uses the following extensions to run the script and the simulation model (Test2). I used this code from a previous post:

C:\Users\fhalawa\OneDrive\Desktop\Test2.fsm" /maintenance nogui_disablemsg_runscript /scriptpath "C:\Users\username\Desktop\script.txt

The problem that I have, is that when I run the code, felxsim model opens externally but does not start working.

2) My second issue: How can I use Python (or the external language) to let objects such as pyxis machine or bed connect to an existing node on the model? What commands should be used? The picture attached shows an example. What I want to do is to ask the model to move the pyxis machine from current location and move it to the x, y coordinates of the node in the attached picture and connect the machine to that node and re-run the model (all this externally).

example.jpg

FlexSim HC 5.3.6
Choose One
pythondevelopmentscriptcommand line
example.jpg (111.6 KiB)
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

London MT avatar image
0 Likes"
London MT answered London MT commented

You can find how to launch Flexsim from command prompt here.

In Python, there are a few libraries that can execute commands (e.g. os, subprocess.popen).

The most well known is os:

import os
os.system("command")
· 2
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Farouq Halawa avatar image Farouq Halawa commented ·

Hi,

I am using this library: subprocess.Popen(), the problem is that when I run it the simulation model does not really start working, so how to execute all the commands one by one ? Here are the commands I need

#subprocess.Popen(r'"C:\Program Files\FlexSimHC5.3\program\flexsim.exe" "C:\Users\fhalawa\OneDrive\Desktop\Test2.fsm" /maintenance nogui_disablemsg_runscript /scriptpath "C:\Users\username\Desktop\script.txt', shell=True)

The script file has the following code

resetmodel(); runspeed(10000); go();

Do you have a better idea on how to let the model that is called (Test2) open, and then I can control it through python so that I can ask it to work, ask it to change location of objects to specific nodes I want and so on.

Thanks

0 Likes 0 ·
London MT avatar image London MT Farouq Halawa commented ·

Hi Farouq,

I don't have Python installed on my work computer, so I cannot verify what you say.

Did you check if the process is running?

process = subprocess.Popen("command")
print(process.Poll())

It is running in the background so you don't see it appearing on your screen.

To manipulate Flexsim from a Python script is rather difficult, my feeling would say. Someone else ?

0 Likes 0 ·
Jason Lightfoot avatar image
1 Like"
Jason Lightfoot answered Jason Lightfoot commented

Why not use the webserver and submit a JSON job request?

· 2
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Farouq Halawa avatar image Farouq Halawa commented ·

Can you please elaborate on this to me? Thanks for your reply

0 Likes 0 ·
Jason Lightfoot avatar image Jason Lightfoot ♦♦ Farouq Halawa commented ·

Just for running in the background - search for webserver in the manual and look in the jobs section.

  • webserver.dll?submitjob should be the action of an HTML form like this one containing a job description in JSON format. The reply will be an id for use with getjobresults queries.


eg.

<form action="http://127.0.0.1/webserver.dll?submitjob" method="POST">
	<input type="hidden" name="job" value='{"modelname":"my%20model","timeout":3600,"priority":5,"setupcommands":
		[{"command":"settable=modelparameters","data":"values=[[\"firstParam\",0],[\"secondParam\",2],[\"thirdParam\",\"hello\"]]"},
		{"command":"setrunspeed=100000000"},{"command":"setstoptime=86400"},{"command":"toolsnodefunction=setupscript"},{"command":"run"}],
		"resultcommands":[{"command":"getnodedata=/Tools/TrackedVariables/WorkInProgress"},{"command":"getnodedata=/Tools/modeloutput"}]}'>
	<input type="submit">
</form>
1 Like 1 ·