Cancel
Showing results for 
Search instead for 
Did you mean: 

Data Processing Tool for Simcenter Amesim

Siemens Valued Contributor Siemens Valued Contributor
Siemens Valued Contributor

Simcenter Amesim and Jupyter

 

When working with 1D System Simulation models, there is nearly always some requirement to do some pre and/or post processing with the data, either in the creation of tables from test data or just to compare simulation results with validation data.

Python is extensively used for this type of analysis. Simcenter Amesim includes a python interpreter within the installation, it is used within the Apps and has a large number API for integration of workflows.

Commands can be executed from a command prompt or terminal but it can be useful to have a more rich interface for visualising the data. The Jupyter notebook is one such tool.

 

"The Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text. Uses include: data cleaning and transformation, numerical simulation, statistical modeling, data visualization, machine learning, and much more."

 

This kind of tool can be used for documenting models, pre and post processing steps including the code required to execute then.

It can also be used to create a interactive user interface to the Amesim model, which is something we will show in this post.

 

Installation

Assumes that Simcenter Amesim is already installed and the $AME environment variable is correctly setup.

1. From a command prompt add the jupyter notebook

  • > python -m pip install jupyter 

2. Install additional widgets needed for the interactive part

  • > python -m pip install ipywidgets

3. The notebook can then be started from another command prompt, first enable the widgets, then start the notebook

  • > jupyter nbextension enable --py widgetsnbextension
  • > jupyter notebook

If the above step doesn't work, the jupyter executable will be located at $AME\sys\python\win64\Scripts, this should be added to the Path enviornment variable.

Example 1 - External plot using Amesim plotting function

Python modules

  • os, gives access to eoperating system files, folders etc
  • amepyplot, Amesim plotting tools

qthelp://lmsimagine.lab/ame_dir/doc/html/manuals/AMESim/Customization/Plot_API/index.html

 

Plot external to Jupyter

The %gui qt part will force the plot to be created in an external applicaiton

 

In [3]:
import os
import amepyplot
%gui qt

plot = amepyplot.PlotWidget() plot.setMinimumSize(800, 500) graph = plot.getGraph(0,0) #Setup a sinus curve x_data = [x/10.0 for x in range(100)] y_data = [math.sin(x) for x in x_data] x_item = amepyplot.Item(x_data, 'time', 'ms') y_item = amepyplot.Item(y_data, 'sin(x)') curve = amepyplot.Curve2D(x_item, y_item) graph.addCurve(curve) plot.show()

 

 

Example 2 - Interactive embedded plot

A more interesting application could be to create an interactive embedded plot. We will allow a user to

  1. Modify a parameter from the browser
  2. Automatically run the Simcenter Amesim simulation and retrieve the results
  3. Display the updated results in the browser

In this case we have a very simple Amesim model as follows

simple_msd.png

 

The user will be able to modify the size of the step, which has been setup as a global variable and plot the response of the mass

 

Additional python modules are needed to do this

  • ame_apy, used to modify, execute the model and return the results

qthelp://lmsimagine.lab/ame_dir/doc/html/manuals/API/Circuit_API/Python_language/Function.html

  • numpy, fundamental scientific computing module
  • matplotlib, python based 2D plotting library
  • ipywidgets, to display buttons and slides in Jupyter

Import python modules and move working directory to location of model

 

In [4]:
import ame_apy as apy
import numpy as np
import matplotlib.pyplot as plt
%matplotlib notebook

import ipywidgets as widgets
from ipywidgets import interactive
from IPython.display import display

os.chdir('D:\\Ametest\\MSD') # replace this with location of the model

 

 

Create a function to run the model

  • Start the API
  • Open the Amesim model
  • Set the global parameters we will define in a dictionary
  • Run the simluation
  • Retrieve the results defined in a dictionary
  • Output the time series data

 

In [5]:
amemod = 'msd.ame'

apy.AMEInitAPI()
apy.AMEOpenAmeFile(amemod)

def runAmeModel(ameModel,parList,varList):
    data = {}
    for p in parList:
        apy.AMESetGlobalParameterValue(p,str(parList[p]))
        
    apy.AMERunSimulation(ameModel)
    
    for i, v in enumerate(varList):
        var = apy.AMEGetVariableValues(varList[v])
        if i==0:
            data['time'] = np.asarray([x[0] for x in var])
        data[v] = np.asarray([x[1] for x in var])
       
    return data

 

 

Create the interactive plot

First we define 3 functions,

  • update_plot, this will just refresh the shown plot
  • interactiveAmePlot, will receive a new parameter, run the model and get the results and create a x, y plot
  • reset_plot, will just clear the overlaid plots

Then,

  • Create a figure to plot to and display it
  • Create an interactive display with a button and slider, as the slide is modified the simulation is re-ran and the plots updated

 

In [7]:
%matplotlib notebook

def update_plot():
    fig = plt.gcf()
    fig.canvas.draw_idle()

def interactiveAmePlot(p=100):
    amemod = 'MSD'
    pN = {'F_cmd':p}
    vN = {'X_mass':'sink@x_mass'}
    
    data = runAmeModel(amemod,pN,vN)
    ax = plt.gca()
    ax.plot(data['time'],data['X_mass'],label='p={}'.format(p))
    ax.legend()
    
    update_plot()
    
def reset_plot(b):
    ax = plt.gca()
    ax.clear()
    interactiveAmePlot()
    
fig = plt.figure()
ax = fig.add_subplot(111)
plt.show()

b = widgets.Button(description='Reset')
b.on_click(reset_plot)
display(b)
y = interactive(interactiveAmePlot,p=(0,1000,50))
display(y)

multi_plots.png

 

We will be able to change the parameter and see updated results on the plot using the automatically generated widgets. The plot can be cleared by pressing the Reset button.

 

After finishing, close the Amesim model and API

 

In [8]:
apy.AMECloseCircuit(False) # don't save the model
apy.AMECloseAPI()

 

Conclusions

Hope this post was useful, good luck experimenting!