Automatically Configure and Run a FlexSim Model

Automatically Configure and Run a FlexSim Model

jackie_ho
Not applicable
5,066 Views
20 Replies
Message 1 of 21

Automatically Configure and Run a FlexSim Model

jackie_ho
Not applicable

If I have a model that is built and ready, is it possible for FlexSim to import various object and model parameters automatically? And run the simulation automatically?

Accepted solutions (1)
5,067 Views
20 Replies
Replies (20)
Message 2 of 21

jeff_nordgren
Not applicable
Accepted solution

A FlexSim model has an On Model Open trigger. Here you could put whatever logic you like, including importing data from Excel or a database, setting the runspeed, and starting the model run. This screenshot shows how to add an On Model Open trigger to your model:

61-flexsim-onmodelopen.png

If you want the model to open automatically when FlexSim starts, you can launch FlexSim and open the model at the same time by double-clicking the model file.

Message 3 of 21

Ben_WilsonADSK
Community Manager
Community Manager

Command line options

Alternatively, you can launch FlexSim from a Command Prompt or a batch file, by executing a line like this (this text probably wraps in this web view, but in the command prompt it should all go on a single line):

"C:\Program Files\FlexSim 2016\program\flexsim.exe" 
"C:\Users\username\Documents\Flexsim 16 Projects\test.fsm" /maintenance 
nogui_disablemsg_runscript /scriptpath C:\myscript.txt

This will open FlexSim, open the model at the specified path, set a flag to suppress showing FlexSim's interface, set a flag to suppress any msg() command popups from appearing, and execute the file C:\myscript.txt as FlexScript.

All of those command line features are optional. For instance, remove the nogui flag to launch FlexSim with the normal interface. You can put FlexScript code into myscript.txt to run custom behaviors in the model, such as resetting and running the model, but that is optional. Remove the runscript flag, plus the "/scriptpath C:\myscript.txt" portion from the line if you don't wish to run a FlexScript file on model load.

Using an external script file that runs upon opening the model is an alternative to having the OnModelOpen trigger coded into your model. Instead you can have an external script file that can run upon model open, that can be separately and independently edited without modifying anything inside the model file.

Attached is a zip file containing 3 files that show an example of how you can silently run a FlexSim model without an interface and then export results to a text file.

Extract these 3 files to your computer somewhere (I put them on my Desktop).

Edit runScriptDemo.bat so that it has correct paths to where you have installed FlexSim, where the model is, and where the script file is.

"C:\Program Files\FlexSim 2016\program\flexsim.exe" "C:\Users\username\Desktop\test.fsm" /maintenance nogui_disablemsg_runscript /scriptpath "C:\Users\username\Desktop\script.txt"

The script file (script.txt) has code to run the model:

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

The Sink's OnEntry trigger in the test.fsm model has code that exports results as a file and closes FlexSim.

cmdexit();
Message 4 of 21

bernard_laporteFYUJD
Participant
Participant

Hi, Ben.

I'm looking for a way to run some automation as well and I was wondering if it is possible to control FlexSim using MS PowerShell, a much newer solution, instead of CMD Prompt, an old and far more limited solution.

I do know it is possible to call the CMD from PowerShell and use the solution above, but it's not a good solution. A more elegant one would be calling FlexSim directly from PowerShell.

I couldn't find any information here at the forum or in the FlexSim user manual.

I would appreciate any suggestions.

Thanks!

Message 5 of 21

philboboADSK
Autodesk
Autodesk

Use the Start-Process cmdlet.

Start-Process -FilePath "C:\Program Files\FlexSim 2018\program\flexsim.exe" -ArgumentList "C:\Users\phil.bobo\Desktop\test.fsm /maintenance runscript /scriptpath C:\Users\phil.bobo\Desktop\script.txt"


Phil BoBo
Sr. Manager, Software Development
Message 6 of 21

amerloQ2XZU
Advocate
Advocate

Hi @phil.bobo,

do you know if there is a trouble when the script is saved in a folder which name contains a space?

I executed your code in two different ways and I obtained two different results.

First test works without space and results is good.

Start-Process -FilePath "C:\Program Files\FlexSim 2018\program\flexsim.exe" "/maintenance runscript /scriptpath C:\Users\amerl\Desktop\Folder_Without_Space\FlexScript.txt"

Second test works with space and the script isn't executed.

Start-Process -FilePath "C:\Program Files\FlexSim 2018\program\flexsim.exe" "/maintenance runscript /scriptpath C:\Users\amerl\Desktop\folder with space\FlexScript.txt"

The file FlexScript.txt simply contains this code

msg("Hello","World",1);

Any idea?

Thanks for the support!

0 Likes
Message 7 of 21

philboboADSK
Autodesk
Autodesk

Command line parameters with spaces need to be wrapped in quotes.

Since you are passing a string within a string literal, you need to use quotes within the string.

You can either escape the inner quote marks with a grave accent ` character:

Start-Process -FilePath "C:\Program Files\FlexSim 2018\program\flexsim.exe" -ArgumentList "/maintenance runscript /scriptpath `"C:\Users\amerl\Desktop\folder with space\FlexScript.txt`""

Or you can use single quotes for the outer string and double quotes for the inner string:

Start-Process -FilePath "C:\Program Files\FlexSim 2018\program\flexsim.exe" -ArgumentList '/maintenance runscript /scriptpath "C:\Users\amerl\Desktop\folder with space\FlexScript.txt"'


Phil BoBo
Sr. Manager, Software Development
Message 8 of 21

amerloQ2XZU
Advocate
Advocate

Thanks @phil.bobo!

I tried all possible combination to pass a string and I forgot grave accent!

Now it works!

0 Likes
Message 9 of 21

wang_z2
Not applicable

Hi,Ben

I use your way to launch Flexsim

But it will take a long time to run in my model(almost 20 mins)

Although my model is more complicated, I don’t think it takes so long.

my computer have i7 8th CPU,32G Ram,so it shouldn't be the reason for my computer.

0 Likes
Message 10 of 21

Ben_WilsonADSK
Community Manager
Community Manager

@Wang Z2,

Loading a model from the command line and starting the simulation via code doesn't automatically make the runspeed go at its maximum, the way background simulations do when using the experimenter or optimizer.

Instead, you need to use a script to set the runspeed to some high value, like:

runspeed(99999999);

Or something similar.

You can also confirm a typical run duration for your model by running it normally in FlexSim at max speed. If it can finish in 4-5 minutes through the regular interface, it should finish in 4-5 minutes when started from the command line.

0 Likes
Message 11 of 21

royve
Explorer
Explorer

Hi everyone!

As somebody tried to import from excel while launching the model from a shell?

To the files shared in this post, I tried adding this line:
excelmultitableimport();

to
'onModelReset' trigger and/or
'script.txt'

without constant success.

Thanks!

0 Likes
Message 12 of 21

tanner_p
Not applicable

@erik.rv2,

I suggest posting a new question (this one is answered and fairly outdated) and referencing this question with a link. It sounds like you're looking for guidance on automating an Excel import, which we're happy to help with.

Message 13 of 21

Ben_WilsonADSK
Community Manager
Community Manager

Another command line parameter:

You can pass the command line parameter /excludemodules modulename1,modulename2,etc to exclude specific modules.

https://answers.flexsim.com/answers/95256/view.html

0 Likes
Message 14 of 21

zhu_m3
Not applicable

@Ben Wilson

This is a good way!

Recently, I'm studying simulation-based optimization, which needs to use flexsim to evaluate objective function. In c++ using your method has solved the problem, but it consumes a lot of time because it needs to open and close flexsim constantly(the objective function needs to be evaluated many times).

More details: Interaction of simulation and optimization and How to obtain the simulation data after executing go() in script console?

So, I wonder if there is any way to save time, thanks!

0 Likes
Message 15 of 21

rafael_b22
Not applicable

Ben,

How did you tell FlexSim to save the output into the "results.txt" file?

0 Likes
Message 16 of 21

philboboADSK
Autodesk
Autodesk

The Sink's OnEntry trigger has code that writes the results.txt file using FlexScript commands:

1666368764445.png



Phil BoBo
Sr. Manager, Software Development
0 Likes
Message 17 of 21

rafael_b22
Not applicable
Thanks a lot!
0 Likes
Message 18 of 21

rafael_b22
Not applicable

What could be the reason that when I run your model I get an average time of 0 and WIP of 0 in the resulsts.txt file?

0 Likes
Message 19 of 21

philboboADSK
Autodesk
Autodesk

Starting in FlexSim 22.1, the 3D objects no longer update the WorkInProgress or TimeInSystem Tracked Variables.

https://docs.flexsim.com/en/22.2/Reference/ReleaseNotesAndHistory/ReleaseNotesAndHistory.html

This is a sample model from 2016. Recent versions of FlexSim have better features for recording statistics. The point of the model is to demonstrate programmatically running FlexSim, not how to best collect and report statistics.



Phil BoBo
Sr. Manager, Software Development
0 Likes
Message 20 of 21

rafael_b22
Not applicable
Thank you. Is there a work around?
0 Likes