Simcenter Amesim – Simulation Scripting and API – Part 1

Have you reached the point where you’ve hit the Run Simulation button, and as the simulations start to run wonder if there might be a better way of setting up and running your study? For most of us, the fast simulation speed of Simcenter Amesim gets in the way of any deeper contemplation as results quickly become available, however in most cases the answer to the question above is undoubtedly yes. There is almost always something in the simulation process that can be improved.

In this week’s blog we take a closer look at Simulation Scripting and the Application Programming Interface (API) inside Simcenter Amesim. Typically, the need for automation and scripting arises when you find yourself doing repetitive tasks, such as importing yet another set of updated input data, or when setting up the same type of plots you always use in countless reports. Other situations may involve something more novel, such as using Simcenter Amesim for its simulation capabilities and then allowing some fancy piece of code to determine the system layout and component selection for you by running simulations and leveraging Simcenter Amesim’s API for model creation.

I should point out that a lot of engineers at Siemens put considerable effort into making the Simcenter tools as user friendly and simple to use as possible while at the same time retaining the flexibility and versatility us users crave. However, well intentioned and diligent engineers aside, there are bound to come up situations where you would like to test something completely new or add something currently not implemented. This is where Simulation Scripting and Simcenter Amesim’s API come in.

main2

 

In this first, out of two blog articles, we focus on what Simulation Scripting is and how it can be used. In the upcoming Simcenter Amesim post will follow-up on the details of Simcenter Amesim’s Application Programing Interface (API) and how this can be used during model creation.

The Simulation Scripting packages contain several classes and functions intended for automating the interaction with simulation models. The packages are supplied together with an installation of Simcenter Amesim and focus specifically on automating tasks related to the Parameter and Simulation modes. Such automation tasks may involve setting or retrieving parameter values, running models as temporal simulations or linear analysis, batch simulation containing multiple simulation cases, or to create and execute more complex pre/post-processing routines. The packages are developed to be able to write short scripts/programs in the coding languages Python, MATLAB and VBA.

main1

Above, The Simulation Scripting Reference for Python providing information on function and class syntax and use. Example showing functions for Batch simulation.

Compared to Simcenter Amesim’s API, which checks out a license once the import API command is run, the functions available under Simulation Scripting do not use a license directly. But because these commands are used together with already opened models, a license is required for the steps of opening and running models.

In addition to setting model and simulation parameters, functions for the built-in design exploration routines can be accessed through Simulation Scripting as well. These include commands for configuring and executing optimization runs, exploring different designs using Design of Experiments (DoE), or simulation using the Monte Carlo methods.

Let us continue by having a closer look at what a few lines of code in Python can achieve. In the following example an arbitrary model of a pump driven hydraulic jack is used to illustrate how a parameter can be manipulated and then run using a separate script.

Picture3

The parameter in question is the proportional gain, a constant, used in this system’s simple control loop. To monitor the changes made in the system, the piston’s displacement is collected and plotted using the script. If the save command is executed using a script, any results may also be viewed afterwards by opening the .ame file in Simcenter Amesim as usual.

Parameters and variables inside Simcenter Amesim are stored using specific names, or datapaths, in order to keep them apart. Thus, the datapath for the proportional gain in this model is k@elect01, following the naming convention: Parameter Name @ Component Alias. Component Aliases may be changed by right-clicking a component in Sketch mode and selecting Alias. This may be used to avoid unwieldy names such as x1@mass-friction2port, which is the datapath for the piston’s displacement used in this example.

code test1

 

The code shown above may be somewhat confusing for newcomers to Python code and simulation scripting in general, so let’s break it down to more manageable chunks of code.

code test2

First off, the different modules (library of functions etc.) are imported into the Python session. Among these is the Simulation Scripting specific module named “amesim”.code test3

 

Next, we use Simcenter Amesim’s circuit checker (AMECirChecker), called with the subprocess module, to verify the simulation model. In detail it unpacks the simulation model specified among the input arguments, checks its submodels and updates the model. Once completed a new .ame file replaces the old one. With the “-g” flag as an argument, the utility also compiles the model preparing it for execution.

This is then followed by the subprocess command Popen to open the newly checked file and retain it as an object throughout an active Python session.

code test4

The “ame get para name from ui” searches and finds the parameter name unique to the queried datapath within the model. The output is a new string object which in this case outputs: ‘GA00 instance 1 value of gain [null]’

A similar search is done for the piston displacement variable and in the line underneath, a list containing the constants to be used by the gain is defined.

code test5

“Ame get sim opt” or “get simulation options”, provides functions to configure your run parameters, such as maximum time-step and tolerance, or if additional computations like system energy should be calculated or not.

code test6Using a for-loop to step through the values contained in the list gain_values, the function “ame put p” or “amesim put parameter” is used to update the current value of the parameter within the model.

“ame run single” is called next to execute a single simulation run together with the previously updated simulation options as argument. This is followed by the function “ame load variable string” which “Extracts temporal results and variables names specified in a list of strings that describes the variables of the Simcenter Amesim model” using the variable name of interest.

 

code test7

The same simulation is also run as a single batch run instead of having to loop through multiple single runs. This approach uses the features found during a normal default batch run in Study Manager and use the commands “ame put batch”, “ame run batch”, and “ame get batch run status”. To configure this batch run, the command “ame put batch” takes in a python dictionary {} in order to describe the combination of parameter and value and that this is to form a new “set” in the Study Manager. The gain values are then added to this set using the line below.

We have now covered most of the Simcenter Amesim related functions used throughout this simple yet useful script. Hopefully this sparks some interest in what can be achieved using Simulation Scripting and that you have gained some understanding into the very basics of setting up an automated process for your simulation work. Next time we will have a closer look at Simcenter Amesim’s API and explore how this can be used for generating new models by coding in Python.

Author

Fabian Hasselby, M.Sc.

Fabian full volupe

Scroll to Top