Demo 1: Run a SIMetrix Schematic

Download demo 1 Schematic

Download demo 1 Python script

In this first example, a SIMetrix schematic is opened and then run. Data is obtained from a node in the schematic and used to calculate the RMS value of the output using native Python code. Although easily accomplished using SIMetrix features alone, this example demonstrates how to run simulations and retrieve data from them.

Let’s work through the Python file. Firstly the opening import lines:

# import simetrix module
import simetrix as sx
import math

The first import line imports simetrix classes and functions to be referenced using the prefix sx. We also import the math module for basic math functions, in this case sqrt().

Next, a function to calculate RMS using Python code is presented:

# Function to calculate RMS
def rms(y, x) :
    prev_y  = y[0]
    prev_x  = x[0]
    ss = 0

    for idx, v in enumerate(y):
        time = x[idx]

        ss = ss + (v*v + prev_y*prev_y + v*prev_y)/3 * (time-prev_x)
        prev_y = v
        prev_x = time

    return math.sqrt(ss/prev_x)

The next line opens a SIMetrix schematic and displays it in the GUI. If the second argument, sx.SchematicOptions.VISIBLE were omitted, the schematic file would be opened and read internally but not put on visible display.

schem = sx.openSchematic("amp.sxsch", sx.SchematicOptions.VISIBLE)

Next the simulation is run. This returns a simulation object stored in the sim variable. This contains details about the run including data generated and the status of the run, that is whether it completed successfully along with error information if it failed for some reason.

sim = schem.run()

The next two lines:

dataGroup = sim.primaryGroup

vout = dataGroup.vectorOfName("VOUT")

obtain the data group then from the data group the simulation data for node VOUT can be obtained. The vout variable is a GroupVector object, is equivalent to a SIMetrix vector and carries the same information. (Refer to the API documentation for full details). Here we will use the ydata and xdata properties to pass to our RMS function to display the RMS value of the waveform:

print("RMS=", rms(vout.ydata, vout.xdata))