With Simcenter Amesim's extensive API, you can write scripts in languages such as Python, MATLAB, and VBA to automate model interactions. This guide provides an example of how to read and write Global Parameters from an Excel CSV file. The main idea is to transform a list of dictionaries, which is how Simcenter Amesim handles global parameter configurations, into the rows and columns of a table, and vice-versa.
Amesim Python Shell Script
There are numerous ways to execute API scripts. You can write them as .py, .m, or .vbs files and run them from a command line (e.g., 'python script_name.py'); alternatively, using an IDE can streamline development, debugging, and execution, and scripts can be embedded in automated workflows... to name a few.
A convenient method for simple and brief Python scripts is to use the Amesim's built-in Python interpreter, which preconfigures the environment and basic Amesim modules.
- In Simcenter Amesim, select Tools > Python command interpreter.
The following welcome message is displayed:
Figure 1: Python Command Interpreter displayed
Writing Global Parameters to csv file
There are several ways to write parameters to an excel file. Using the Scripting API function,
amesim.amereadgp(sys_name, run_id)
we generate a list of dictionaries containing the global parameters data. These dictionaries can be easily transformed into the rows and columns of a CSV table, allowing for parameter writing in Excel.
For this example, we use the demo system ’FlatTwin’. Get the system from demos:
1. Select Help > Get demo. The Choose Demo dialog box opens.
2. Open the Tutorials folder and select FlatTwin.ame.
3. Click on Copy and open. The system below is shown.
Figure 5. Flat Twin demo system
4. Go to Parameter mode and look at the six global parameters.
Figure 6. System Global Parameters
5. Start the Python shell script with the previously described method and look at the current directory, this is the path where the Excel file is going to be created
Figure 7. Python command interpreter
6. Type or copy/paste the following code and execute it by pressing
Enter twice. Ensure the specified model name ('FlatTwin') corresponds with your open model. To use this script with other models modify sname, and csvname accordingly
(be careful with indentation of 'writer') .
_________________________________________________________________________________________
import csv
sname = 'FlatTwin'
csvname = 'FlatTwin_GPs.csv'
gp_dict_list, ret_stat = amereadgp(sname)
headers = gp_dict_list[0].keys()
with open(csvname, 'w', newline='') as file:
writer = csv.DictWriter(file, fieldnames=headers)
writer.writeheader()
writer.writerows(gp_dict_list)
͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞
7. A .csv file has been created in the current directory, which in this case is C:\AMETest. Open the file and check that all parameters are exported as expected. The first row of the file allows you to define the data type for each column according to the Global Parameter Setup of the Amesim model.
Figure 8. Excel data created
Reading Global Parameters from csv fileWe follow a similar approach, but this time we read a CSV file and convert its contents into a list of dictionaries, the we can call
amesim.amewritegp(sys_name, gp_list)
to write the data back into the Amesim model.
1. Open the desired file, and specify the key-value for each column, as seen below
Figure 9. Data to export with key-value in the first row
We have changed some of the values (displayed in bold font) to notice the change after we import them.
Note: Each element of a global parameter list is described by a dictionary having the following key-value pairs:
- pcustom: '1' for parameters of customized objects; '0' otherwise
- ptype: 'integer', 'real' or 'text'
- pname: name of parameter
- ptitle: full title of parameter
- pvalue: value (as a string)
- pmin: min value (as a string, empty for 'text' type)
- pmax: max value (as a string, empty for 'text' type)
- pdef: default value (as a string)
- punit: unit for 'real' type, an empty string otherwise
- pcirscope: global parameter circuit scope id, empty for non custom parameters
- pdatapath: global parameter data path, empty for non custom parameters
This step is crucial since we are specifying the type of data that Amesim is going to insert into the Global Parameter Setup XML (.amegp).
2. Save the file as .csv, for this example we save as 'FlatTwin_GPs_Edited.csv'
3. Start the Python shell script
Figure 10. Python Command Interpreter
4. Type or copy/paste the following code and run it
_________________________________________________________________________________________
import csv
sname = 'FlatTwin'
csvname = 'FlatTwin_GPs_Edited.csv'
gp_dict_list = []
with open(csvname, 'r') as data:
for line in csv.DictReader(data):
gp_dict_list.append(line)
amewritegp(sname, gp_dict_list)
͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞ ͞
A new .amegp XML has been created into the current directory. Open the Global Parameter Setup, and overwrite (or delete and load) the current parameters with the new file.
Figure 11. Load new Global Parameters file
The new Global Parameter Setup has been created