When the scripting files are properly installed in your scripting working folder, we can run some tutorial scripts. Tutorial scripts are step by step demonstrations of usage of the scripting functionality in 20-sim. These scripts can be found in the tutorials subfolder of the scripting working folder. We will start with a basic script that opens and runs a 20-sim model.
|2.||Open IPython (Interactive Python shell) from the Start menu (under 20-sim 5.0).|
|3.||In IPython, change the local working directory to the tutorial folder inside your scripting working folder . E.g . type:|
|4.||In IPython, execute the following command (e.g. type the following case sensitive command followed by ENTER):|
Note that the run command is specific for IPython. For a standard Python session, you can start this script on the command line using: python.exe menu.py. This command will show a menu with several options including T for Tutorials.
IPython session for the tutorials
|5.||Select option T - Tutorials (press t, ENTER) to show the tutorial menu:|
- Tutorial menu -
Select a tutorial:
1 - Run a simulation.
2 - Set a parameter in 20-sim, then run a simulation.
3 - Execute multiple runs with a changing parameter.
4 - Basic simulation result analysis.
5 - Read a parameter from a CSV file and set it in 20-sim.
6 - Retrieve 20-sim model variables and their properties.
Or choose a menu option:
Q - Quit
I - Show the introduction text again.
Your choice > 1
|6.||Press ENTER again to show the available tutorials and choose option 1 Run a simulation followed by ENTER.|
In this tutorial the scripting interface will:
- Open a 20-sim model (starting 20-sim if necessary)
- Process and run the model
- Close the 20-sim model
The Python scripting interface will now connect to 20-sim.
If 20-sim is not running it will be started automatically.
Connecting, please wait...
The scripting interface has successfully connected to 20-sim.
The tutorial model will be opened.
If you still have an open model. SAVE YOUR MODEL, unsaved changes will be overwritten.
The model ControlledSystem.emx has been opened in 20-sim.
The model will be processed and simulated.
The 20-sim plot window will open.
|10.||Press ENTER to load the model ControlledSystem.emx in 20-sim and to simulate it.|
The tutorial will now close the 20-sim model and exit.
|11.||Press ENTER to close the simulation and this 20-sim model|
Do you want to see the source code? [y/N]
To see how the script is made, you can inspect it by choosing y. This will print the relevant script lines on the Python console. You can also open the real script in a text editor like Notepad by opening the file: C:\Users\yourusername\Documents\20simscripting\Python\tutorials\run_simulation.py.
The important functions / lines of the runSimulation script are:
|•||import controllab: Tell Python to load the Controllab package with the 20-sim scripting functions in the XXSim() class.|
|•||my20sim = controllab.XXSim(): create a 20-sim scripting object|
|•||my20sim.connect(): This command opens a connection to 20-sim.|
|•||my20sim.set_scriptmode(): Tell 20-sim that we are in scripting mode (does not show confirmation dialogs)|
|•||my20sim.open_model(): This command opens a model in 20-sim by giving the file name including the full path.|
|•||my20sim.process_model(): This command will process the model.|
|•||my20sim.run(): This command will run a simulation.|
|•||my20sim.close_model(): This command will remove the simulation model from 20-sim.|
These functions are the basis of scripting in 20-sim and will be present in this order in most scripts.