AmesimKnowledge

Advanced examples > Linear analysis

Python

Copy

import amepyplot
import scipy
import scipy.signal
import numpy
from PySide6 import QtWidgets
import amesim

app = QtWidgets.QApplication.instance()
if app is None:
    app = QtWidgets.QApplication([])

plot = amepyplot.PlotWidget()

# Retrieve linearization times (if needed)
times=amesim.amela('scrdemo2')

# Run the simulation
amesim.amerunsingle('scrdemo2')

# select the index of the jacfile to use (use 1 to display the second linearization time)
jacfileIndex = 0

[A, B, C, D, x, u, y, t, S] = amesim.ameloadj('scrdemo2', jacfileIndex)

wrange = 2*scipy.pi*numpy.logspace(-1,2,400)

# uindex is the index of the control variable (Note that the indexes start at 0)
uindex = 0
# yindex is the index of the observer variable (Note that the indexes start at 0)
yindex = 0

# Compute bode using scipy
sys_a = scipy.matrix(A)
sys_b = numpy.zeros((len(B[0]), 1))
for k in range(len(B[0])):
   sys_b[k, 0] = B[0][k][uindex]
sys_c = scipy.matrix(C[yindex])
sys_d = D[0][yindex][uindex]

system = scipy.signal.lti(sys_a, sys_b, sys_c, sys_d)
wout, y = scipy.signal.freqresp(system, [w for w in wrange if w])  # eliminate w = 0 values
mag = abs(y)
phase = numpy.unwrap(numpy.arctan2(y.imag, y.real)) * 180.0 / scipy.pi

freq_range = wout / (2 * scipy.pi)
mag_dB = 20 * numpy.lib.scimath.log10(mag)

# Create 2 graphs on the plot window
plot.setRowCount(2)

# Creation of the Bode graph (Magnitude)
x_item = amepyplot.Item(freq_range, 'Frequency', 'Hz')
y_item = amepyplot.Item(mag_dB, 'Gain', 'dB')
ampl_curve = amepyplot.Curve2D(x_item, y_item, title='Amplitude of frequency response')
ampl_graph = plot.getGraph(0, 0)
ampl_graph.addCurve(ampl_curve)
ampl_graph.xAxis().setLogScale(True)

# Creation of the Bode graph (Phase)
y_item = amepyplot.Item(phase, 'Phase', 'deg')
phase_curve = amepyplot.Curve2D(x_item, y_item, title='Phase of frequency response')
phase_graph = plot.getGraph(1, 0)
phase_graph.addCurve(phase_curve)
phase_graph.xAxis().setLogScale(True)

plot.setWindowTitle('Bode plot at t = {} s'.format(times[jacfileIndex]))
plot.resize(500, 400)
plot.show()

app.exec()

Figure 24: Bode plot at t=0s

Figure 25: Bode plot at t=0.04s

Source: https://docs.sw.siemens.com/en-US/doc/254352342/PL20250521841123434.amesim_collection.Scripting/xid1133463 · retrieved 2026-07-17